Blame tests/test_common.c

Packit 6bd9ab
/*
Packit 6bd9ab
   test_common.c - simple test for the common module
Packit 6bd9ab
   This file is part of the nss-pam-ldapd library.
Packit 6bd9ab
Packit 6bd9ab
   Copyright (C) 2008, 2009, 2011, 2012, 2013 Arthur de Jong
Packit 6bd9ab
Packit 6bd9ab
   This library is free software; you can redistribute it and/or
Packit 6bd9ab
   modify it under the terms of the GNU Lesser General Public
Packit 6bd9ab
   License as published by the Free Software Foundation; either
Packit 6bd9ab
   version 2.1 of the License, or (at your option) any later version.
Packit 6bd9ab
Packit 6bd9ab
   This library is distributed in the hope that it will be useful,
Packit 6bd9ab
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6bd9ab
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6bd9ab
   Lesser General Public License for more details.
Packit 6bd9ab
Packit 6bd9ab
   You should have received a copy of the GNU Lesser General Public
Packit 6bd9ab
   License along with this library; if not, write to the Free Software
Packit 6bd9ab
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit 6bd9ab
   02110-1301 USA
Packit 6bd9ab
*/
Packit 6bd9ab
Packit 6bd9ab
#include "config.h"
Packit 6bd9ab
Packit 6bd9ab
#include <stdio.h>
Packit 6bd9ab
#include <assert.h>
Packit 6bd9ab
#include <sys/stat.h>
Packit 6bd9ab
Packit 6bd9ab
#include "nslcd/common.h"
Packit 6bd9ab
#include "nslcd/cfg.h"
Packit 6bd9ab
#include "nslcd/log.h"
Packit 6bd9ab
Packit 6bd9ab
static void test_isvalidname(void)
Packit 6bd9ab
{
Packit 6bd9ab
  assert(isvalidname("arthur"));
Packit 6bd9ab
  assert(!isvalidname("-arthur"));
Packit 6bd9ab
  assert(isvalidname("arthur-is-nice"));
Packit 6bd9ab
  assert(isvalidname("sambamachine$"));
Packit 6bd9ab
  assert(isvalidname("foo\\bar"));
Packit 6bd9ab
  assert(!isvalidname("\\foo\\bar"));
Packit 6bd9ab
  assert(!isvalidname("foo\\bar\\"));
Packit 6bd9ab
  assert(isvalidname("me"));    /* try short name */
Packit 6bd9ab
  assert(isvalidname("f"));
Packit 6bd9ab
  assert(isvalidname("(foo bar)"));
Packit 6bd9ab
}
Packit 6bd9ab
Packit 6bd9ab
/* the main program... */
Packit 6bd9ab
int main(int UNUSED(argc), char UNUSED(*argv[]))
Packit 6bd9ab
{
Packit 6bd9ab
  char *srcdir;
Packit 6bd9ab
  char fname[100];
Packit 6bd9ab
  /* build the name of the file */
Packit 6bd9ab
  srcdir = getenv("srcdir");
Packit 6bd9ab
  if (srcdir == NULL)
Packit 6bd9ab
    srcdir = ".";
Packit 6bd9ab
  snprintf(fname, sizeof(fname), "%s/nslcd-test.conf", srcdir);
Packit 6bd9ab
  fname[sizeof(fname) - 1] = '\0';
Packit 6bd9ab
  /* ensure that file is not world readable for configuration parsing to
Packit 6bd9ab
     succeed */
Packit 6bd9ab
  (void)chmod(fname, (mode_t)0660);
Packit 6bd9ab
  /* initialize configuration */
Packit 6bd9ab
  cfg_init(fname);
Packit 6bd9ab
  /* partially initialize logging */
Packit 6bd9ab
  log_setdefaultloglevel(LOG_DEBUG);
Packit 6bd9ab
  /* run the tests */
Packit 6bd9ab
  test_isvalidname();
Packit 6bd9ab
  return 0;
Packit 6bd9ab
}