diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2007-08-19 15:38:36 +0000 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2007-08-19 15:38:36 +0000 |
commit | 6f3c49ca44efcc81e074f544b423d24b023d16d0 (patch) | |
tree | 5b21e98eb74202e18c63e74c64a2718af16f9ba6 | |
parent | 66fb4ebdf605eba523f7b34e422a3c056d559c5d (diff) |
add some checks for the configuration module
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-ldapd@360 ef36b2f9-881f-0410-afb5-c4e39611909c
-rw-r--r-- | tests/Makefile.am | 7 | ||||
-rw-r--r-- | tests/test_cfg.c | 223 |
2 files changed, 228 insertions, 2 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index 8908a2d..b92d433 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -18,9 +18,9 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # 02110-1301 USA -TESTS = test_dict test_tio +TESTS = test_dict test_tio test_cfg -check_PROGRAMS = test_dict test_tio +check_PROGRAMS = test_dict test_tio test_cfg noinst_PROGRAMS = test_aliases test_ethers test_group test_hosts \ test_netgroup test_networks test_passwd test_protocols \ @@ -38,6 +38,9 @@ test_tio_SOURCES = test_tio.c ../common/tio.h ../common/tio.c test_tio_CFLAGS = -pthread test_tio_LDFLAGS = -pthread +test_cfg_SOURCES = test_cfg.c +test_cfg_LDADD = ../nslcd/log.o ../nslcd/attmap.o ../common/dict.o + common_SOURCES = ../nss/common.c ../nslcd.h ../nss/prototypes.h \ ../common/tio.c ../common/tio.h diff --git a/tests/test_cfg.c b/tests/test_cfg.c new file mode 100644 index 0000000..76aa92f --- /dev/null +++ b/tests/test_cfg.c @@ -0,0 +1,223 @@ +/* + 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 <stdio.h> +#include <string.h> +#include <assert.h> + +/* 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;i<NSS_LDAP_CONFIG_URI_MAX;i++) + { + add_uri(__FILE__,__LINE__,&cfg,"ldap://localhost"); + assert(cfg.ldc_uris[i]!=NULL); + assert(cfg.ldc_uris[i+1]==NULL); + } + /* inserting one more entry should call exit(): + add_uri(__FILE__,__LINE__,&cfg,"ldap://localhost"); + assert(cfg.ldc_uris[i]!=NULL); + assert(cfg.ldc_uris[i+1]==NULL); */ + /* there is no cfg_free() so we have a memory leak here */ +} + +static void test_parse_boolean(void) +{ + assert(parse_boolean(__FILE__,__LINE__,"True")==1); + assert(parse_boolean(__FILE__,__LINE__,"faLSe")==0); + assert(parse_boolean(__FILE__,__LINE__,"1")==1); + assert(parse_boolean(__FILE__,__LINE__,"oFF")==0); + assert(parse_boolean(__FILE__,__LINE__,"Yes")==1); + assert(parse_boolean(__FILE__,__LINE__,"No")==0); + /* most other values should call exit(): + assert(parse_boolean(__FILE__,__LINE__,"Foo")==0); */ +} + + +static void test_parse_scope(void) +{ + assert(parse_scope(__FILE__,__LINE__,"sUb")==LDAP_SCOPE_SUBTREE); + assert(parse_scope(__FILE__,__LINE__,"subtree")==LDAP_SCOPE_SUBTREE); + assert(parse_scope(__FILE__,__LINE__,"ONE")==LDAP_SCOPE_ONELEVEL); + assert(parse_scope(__FILE__,__LINE__,"oneLevel")==LDAP_SCOPE_ONELEVEL); + assert(parse_scope(__FILE__,__LINE__,"base")==LDAP_SCOPE_BASE); + assert(parse_scope(__FILE__,__LINE__,"bASe")==LDAP_SCOPE_BASE); + /* most other values should call exit(): + assert(parse_scope(__FILE__,__LINE__,"BSAE")==LDAP_SCOPE_BASE); */ +} + +static void test_parse_map(void) +{ + /* some general assertions */ + assert((LM_ALIASES!=LM_ETHERS)&&(LM_ALIASES!=LM_GROUP)&&(LM_ALIASES!=LM_HOSTS)&& + (LM_ALIASES!=LM_NETGROUP)&&(LM_ALIASES!=LM_NETWORKS)&&(LM_ALIASES!=LM_PASSWD)&& + (LM_ALIASES!=LM_PROTOCOLS)&&(LM_ALIASES!=LM_RPC)&&(LM_ALIASES!=LM_SERVICES)&& + (LM_ALIASES!=LM_SHADOW)); + assert((LM_ETHERS!=LM_GROUP)&&(LM_ETHERS!=LM_HOSTS)&&(LM_ETHERS!=LM_NETGROUP)&& + (LM_ETHERS!=LM_NETWORKS)&&(LM_ETHERS!=LM_PASSWD)&&(LM_ETHERS!=LM_PROTOCOLS)&& + (LM_ETHERS!=LM_RPC)&&(LM_ETHERS!=LM_SERVICES)&&(LM_ETHERS!=LM_SHADOW)); + assert((LM_GROUP!=LM_HOSTS)&&(LM_GROUP!=LM_NETGROUP)&&(LM_GROUP!=LM_NETWORKS)&& + (LM_GROUP!=LM_PASSWD)&&(LM_GROUP!=LM_PROTOCOLS)&&(LM_GROUP!=LM_RPC)&& + (LM_GROUP!=LM_SERVICES)&&(LM_GROUP!=LM_SHADOW)); + assert((LM_HOSTS!=LM_NETGROUP)&&(LM_HOSTS!=LM_NETWORKS)&&(LM_HOSTS!=LM_PASSWD)&& + (LM_HOSTS!=LM_PROTOCOLS)&&(LM_HOSTS!=LM_RPC)&&(LM_HOSTS!=LM_SERVICES)&& + (LM_HOSTS!=LM_SHADOW)); + assert((LM_NETGROUP!=LM_NETWORKS)&&(LM_NETGROUP!=LM_PASSWD)&&(LM_NETGROUP!=LM_PROTOCOLS)&& + (LM_NETGROUP!=LM_RPC)&&(LM_NETGROUP!=LM_SERVICES)&&(LM_NETGROUP!=LM_SHADOW)); + assert((LM_NETWORKS!=LM_PASSWD)&&(LM_NETWORKS!=LM_PROTOCOLS)&&(LM_NETWORKS!=LM_RPC)&& + (LM_NETWORKS!=LM_SERVICES)&&(LM_NETWORKS!=LM_SHADOW)); + assert((LM_PASSWD!=LM_PROTOCOLS)&&(LM_PASSWD!=LM_RPC)&&(LM_PASSWD!=LM_SERVICES)&& (LM_PASSWD!=LM_SHADOW)); + assert((LM_PROTOCOLS!=LM_RPC)&&(LM_PROTOCOLS!=LM_SERVICES)&&(LM_PROTOCOLS!=LM_SHADOW)); + assert((LM_RPC!=LM_SERVICES)&&(LM_RPC!=LM_SHADOW)); + assert((LM_SERVICES!=LM_SHADOW)); + /* test supported names */ + assert(parse_map(__FILE__,__LINE__,"alIas")==LM_ALIASES); + assert(parse_map(__FILE__,__LINE__,"AliasES")==LM_ALIASES); + assert(parse_map(__FILE__,__LINE__,"ether")==LM_ETHERS); + assert(parse_map(__FILE__,__LINE__,"ethers")==LM_ETHERS); + assert(parse_map(__FILE__,__LINE__,"group")==LM_GROUP); + /* assert(parse_map(__FILE__,__LINE__,"groups")==LM_GROUP); */ + assert(parse_map(__FILE__,__LINE__,"host")==LM_HOSTS); + assert(parse_map(__FILE__,__LINE__,"hosts")==LM_HOSTS); + assert(parse_map(__FILE__,__LINE__,"netgroup")==LM_NETGROUP); + /* assert(parse_map(__FILE__,__LINE__,"netgroups")==LM_NETGROUP); */ + assert(parse_map(__FILE__,__LINE__,"network")==LM_NETWORKS); + assert(parse_map(__FILE__,__LINE__,"networks")==LM_NETWORKS); + assert(parse_map(__FILE__,__LINE__,"passwd")==LM_PASSWD); + /* assert(parse_map(__FILE__,__LINE__,"passwds")==LM_PASSWD); */ + assert(parse_map(__FILE__,__LINE__,"protocol")==LM_PROTOCOLS); + assert(parse_map(__FILE__,__LINE__,"protocols")==LM_PROTOCOLS); + assert(parse_map(__FILE__,__LINE__,"rpc")==LM_RPC); + /* assert(parse_map(__FILE__,__LINE__,"rpcs")==LM_RPC); */ + assert(parse_map(__FILE__,__LINE__,"service")==LM_SERVICES); + assert(parse_map(__FILE__,__LINE__,"services")==LM_SERVICES); + assert(parse_map(__FILE__,__LINE__,"shadow")==LM_SHADOW); + /* assert(parse_map(__FILE__,__LINE__,"shadows")==LM_SHADOW); */ + /* most other values should call exit(): + assert(parse_map(__FILE__,__LINE__,"publickey")==LM_SERVICES); */ +} + +static void test_parse_map_statement(void) +{ + /* FIXME: implement */ +} + +static void test_alloc_lsd(void) +{ + struct ldap_service_search_descriptor mylsd; + struct ldap_service_search_descriptor *lsd; + /* set up mylsd */ + mylsd.lsd_base="mybase"; + mylsd.lsd_scope=-2; + mylsd.lsd_filter="myfilter"; + mylsd.lsd_next=NULL; + /* test passing existing lsd */ + lsd=&mylsd; + alloc_lsd(&lsd); + assert(lsd==&mylsd); + assertstreq(lsd->lsd_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; +} |