/* test_cfg.c - simple test for the cfg module This file is part of the nss-ldapd library. Copyright (C) 2007 Arthur de Jong This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "config.h" #include #include #include /* we include cfg.c because we want to test the static methods */ #include "nslcd/cfg.c" #define assertstreq(str1,str2) \ (assertstreq_impl(str1,str2,"strcmp(" __STRING(str1) "," __STRING(str2) ")==0", \ __FILE__, __LINE__, __ASSERT_FUNCTION)) /* method for determening string equalness */ static void assertstreq_impl(const char *str1,const char *str2, const char *assertion,const char *file, int line,const char *function) { if (strcmp(str1,str2)!=0) __assert_fail(assertion,file,line,function); } static void test_xstrdup(void) { static const char *foo="testString123"; char *bar; bar=xstrdup(foo); /* we should have a new value */ assert(bar!=NULL); /* the contents should be the same */ assertstreq(foo,bar); /* but the pointer should be different */ assert(foo!=bar); /* free memory */ free(bar); } static void test_add_uris(void) { static struct ldap_config cfg; int i; /* set up config */ cfg_defaults(&cfg); assert(cfg.ldc_uris[0]==NULL); /* add a uri */ add_uri(__FILE__,__LINE__,&cfg,"ldap://localhost"); assert(cfg.ldc_uris[0]!=NULL); assert(cfg.ldc_uris[1]==NULL); /* add some more uris */ for (i=1;ilsd_base,"mybase"); assertstreq(lsd->lsd_filter,"myfilter"); /* try passing a NULL value */ lsd=NULL; alloc_lsd(&lsd); assert(lsd!=NULL); assert(lsd!=&mylsd); assert(lsd->lsd_base==NULL); assert(lsd->lsd_scope==-1); assert(lsd->lsd_filter==NULL); assert(lsd->lsd_next==NULL); } static void test_tokenize(void) { const char **opts; int nopts; /* this also has memory leaks (the strdup() calls) */ opts=tokenize(__FILE__,__LINE__,strdup("this is a simple line"),&nopts); assert(nopts==5); assertstreq(opts[0],"this"); assertstreq(opts[1],"is"); assertstreq(opts[2],"a"); assertstreq(opts[3],"simple"); assertstreq(opts[4],"line"); opts=tokenize(__FILE__,__LINE__,strdup("this contains \"quoted text\""),&nopts); assert(nopts==3); assertstreq(opts[0],"this"); assertstreq(opts[1],"contains"); assertstreq(opts[2],"\"quoted text\""); } /* the main program... */ int main(int UNUSED(argc),char UNUSED(*argv[])) { test_xstrdup(); test_add_uris(); test_parse_boolean(); test_parse_scope(); test_parse_map(); test_parse_map_statement(); test_alloc_lsd(); test_tokenize(); return EXIT_SUCCESS; }