Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2009-12-28 13:18:12 +0100
committerArthur de Jong <arthur@arthurdejong.org>2009-12-28 13:18:12 +0100
commit9c0cf90c858dd020756d7cd51661beedacdf9924 (patch)
tree215b28b4e0f754819590185e02e290df814c5b74 /tests
parent93182ac6939bece7e538bc06e5f09f47bcaddf8a (diff)
implement attribute mapping using shell-like expressions
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-pam-ldapd@1041 ef36b2f9-881f-0410-afb5-c4e39611909c
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am13
-rw-r--r--tests/test_cfg.c1
-rw-r--r--tests/test_common.c5
-rw-r--r--tests/test_expr.c146
-rw-r--r--tests/test_myldap.c5
5 files changed, 163 insertions, 7 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index feac01d..a0be82c 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,7 +1,7 @@
# Makefile.am - use automake to generate Makefile.in
#
# Copyright (C) 2006 West Consulting
-# Copyright (C) 2006, 2007, 2008 Arthur de Jong
+# Copyright (C) 2006, 2007, 2008, 2009 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
@@ -19,10 +19,10 @@
# 02110-1301 USA
TESTS = test_dict test_set test_tio test_cfg test_myldap.sh test_nsscmds.sh \
- test_getpeercred test_common
+ test_getpeercred test_common test_expr
check_PROGRAMS = test_dict test_set test_tio test_cfg test_myldap \
- test_getpeercred test_common
+ test_getpeercred test_common test_expr
EXTRA_PROGRAMS = test_aliases test_ethers test_group test_hosts \
test_netgroup test_networks test_passwd test_protocols \
@@ -52,14 +52,17 @@ test_tio_SOURCES = test_tio.c ../common/tio.h ../common/tio.c
test_tio_LDFLAGS = $(PTHREAD_CFLAGS) $(PTHREAD_LIBS)
test_cfg_SOURCES = test_cfg.c
-test_cfg_LDADD = ../nslcd/log.o ../nslcd/attmap.o \
+test_cfg_LDADD = ../nslcd/log.o ../nslcd/attmap.o \
../nslcd/common.o ../nslcd/myldap.o \
../nslcd/alias.o ../nslcd/ether.o ../nslcd/group.o \
../nslcd/host.o ../nslcd/netgroup.o ../nslcd/network.o \
../nslcd/passwd.o ../nslcd/protocol.o ../nslcd/rpc.o \
../nslcd/service.o ../nslcd/shadow.o \
@nslcd_LIBS@ ../common/libtio.a ../common/libdict.a \
- ../compat/libcompat.a
+ ../common/libexpr.a ../compat/libcompat.a
+
+test_expr_SOURCES = test_expr.c
+test_expr_LDADD = ../common/set.o ../common/dict.o
test_myldap_SOURCES = test_myldap.c
test_myldap_LDADD = ../nslcd/log.o ../nslcd/common.o ../nslcd/cfg.o \
diff --git a/tests/test_cfg.c b/tests/test_cfg.c
index 269b57c..dc558c2 100644
--- a/tests/test_cfg.c
+++ b/tests/test_cfg.c
@@ -206,6 +206,7 @@ static void test_read(void)
"base dc=test, dc=tld\n"
"base passwd ou=Some People,dc=test,dc=tld\n"
"map\tpasswd uid\t\tsAMAccountName\n"
+ "map passwd homeDirectory \"${homeDirectory:-/home/$uid}\"\n"
"filter group (&(objeclClass=posixGroup)(gid=1*))\n"
"\n"
"scope passwd one\n");
diff --git a/tests/test_common.c b/tests/test_common.c
index 89856d4..4831815 100644
--- a/tests/test_common.c
+++ b/tests/test_common.c
@@ -2,7 +2,7 @@
test_common.c - simple test for the common module
This file is part of the nss-pam-ldapd library.
- Copyright (C) 2008 Arthur de Jong
+ Copyright (C) 2008, 2009 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
@@ -32,6 +32,9 @@ const char **base_get_var(int UNUSED(map)) {return NULL;}
int *scope_get_var(int UNUSED(map)) {return NULL;}
const char **filter_get_var(int UNUSED(map)) {return NULL;}
const char **attmap_get_var(int UNUSED(map),const char UNUSED(*name)) {return NULL;}
+const char *attmap_get_value(MYLDAP_ENTRY UNUSED(*entry),const char UNUSED(*attr),char UNUSED(*buffer),size_t UNUSED(buflen)) {return "";}
+void *attmap_add_attributes(void UNUSED(*set),const char UNUSED(*attr)) {return NULL;}
+const char *attmap_set_mapping(const char UNUSED(**var),const char UNUSED(*value)) {return NULL;}
static void test_isvalidname(void)
{
diff --git a/tests/test_expr.c b/tests/test_expr.c
new file mode 100644
index 0000000..dfc00cb
--- /dev/null
+++ b/tests/test_expr.c
@@ -0,0 +1,146 @@
+/*
+ test_expr.c - simple tests for the expr module
+ This file is part of the nss-pam-ldapd library.
+
+ Copyright (C) 2009 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 expr.c because we want to test the static methods */
+#include "common/expr.c"
+
+#ifndef __ASSERT_FUNCTION
+#define __ASSERT_FUNCTION ""
+#endif /* not __ASSERT_FUNCTION */
+
+#define assertstreq(str1,str2) \
+ (assertstreq_impl(str1,str2,"strcmp(" __STRING(str1) "," __STRING(str2) ")==0", \
+ __FILE__, __LINE__, __ASSERT_FUNCTION))
+
+/* for Solaris: */
+#define __assert_fail(assertion,file,line,function) __assert(assertion,file,line)
+
+/* 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_parse_name(void)
+{
+ char buffer[20];
+ int i;
+ i=0;
+ assert(parse_name("fooBar",&i,buffer,sizeof(buffer))!=NULL);
+ assert(i==6);
+ i=0;
+ assert(parse_name("nameThatWillNotFitInBuffer",&i,buffer,sizeof(buffer))==NULL);
+ i=0;
+ assert(parse_name("foo Bar",&i,buffer,sizeof(buffer))!=NULL);
+ assert(i==3);
+ assertstreq(buffer,"foo");
+}
+
+static const char *expanderfn(const char *name,void UNUSED(*expander_attr))
+{
+ if (strcmp(name,"empty")==0)
+ return "";
+ else
+ return "foobar";
+}
+
+static void test_expr_parse(void)
+{
+ char buffer[1024];
+ assert(expr_parse("$test1",buffer,sizeof(buffer),expanderfn,NULL)!=NULL);
+ assertstreq(buffer,"foobar");
+ assert(expr_parse("$empty",buffer,sizeof(buffer),expanderfn,NULL)!=NULL);
+ assertstreq(buffer,"");
+ assert(expr_parse("${test1}\\$",buffer,sizeof(buffer),expanderfn,NULL)!=NULL);
+ assertstreq(buffer,"foobar$");
+ assert(expr_parse("${test1:-default}",buffer,sizeof(buffer),expanderfn,NULL)!=NULL);
+ assertstreq(buffer,"foobar");
+ assert(expr_parse("${empty:-default}",buffer,sizeof(buffer),expanderfn,NULL)!=NULL);
+ assertstreq(buffer,"default");
+ assert(expr_parse("${test1:+setset}",buffer,sizeof(buffer),expanderfn,NULL)!=NULL);
+ assertstreq(buffer,"setset");
+ assert(expr_parse("${empty:+setset}",buffer,sizeof(buffer),expanderfn,NULL)!=NULL);
+ assertstreq(buffer,"");
+ assert(expr_parse("${empty:-$test1}",buffer,sizeof(buffer),expanderfn,NULL)!=NULL);
+ assertstreq(buffer,"foobar");
+ assert(expr_parse("a/$test1/b",buffer,sizeof(buffer),expanderfn,NULL)!=NULL);
+ assertstreq(buffer,"a/foobar/b");
+ assert(expr_parse("a/$empty/b",buffer,sizeof(buffer),expanderfn,NULL)!=NULL);
+ assertstreq(buffer,"a//b");
+ assert(expr_parse("a${test1}b",buffer,sizeof(buffer),expanderfn,NULL)!=NULL);
+ assertstreq(buffer,"afoobarb");
+ assert(expr_parse("a${test1}b${test2:+${test3:-d$test4}e}c",buffer,sizeof(buffer),expanderfn,NULL)!=NULL);
+ assertstreq(buffer,"afoobarbfoobarec");
+ assert(expr_parse("a${test1}b${test2:+${empty:-d$test4}e}c",buffer,sizeof(buffer),expanderfn,NULL)!=NULL);
+ assertstreq(buffer,"afoobarbdfoobarec");
+}
+
+static void test_buffer_overflow(void)
+{
+ char buffer[10];
+ assert(expr_parse("$test1$empty$test1",buffer,sizeof(buffer),expanderfn,NULL)==NULL);
+ assert(expr_parse("long test value",buffer,sizeof(buffer),expanderfn,NULL)==NULL);
+ assert(expr_parse("${test1:-long test value}",buffer,sizeof(buffer),expanderfn,NULL)==NULL);
+}
+
+static void test_expr_vars(void)
+{
+ SET *set;
+ /* simple test */
+ set=set_new();
+ assert(expr_vars("$a",set)!=NULL);
+ assert(set_contains(set,"a"));
+ assert(!set_contains(set,"$a"));
+ set_free(set);
+ /* more elaborate test */
+ set=set_new();
+ assert(expr_vars("\"${gecos:-$cn}\"",set)!=NULL);
+ assert(set_contains(set,"gecos"));
+ assert(set_contains(set,"cn"));
+ set_free(set);
+ /* more elaborate test */
+ set=set_new();
+ assert(expr_vars("\"${homeDirectory:-/home/$uidNumber/$uid}\"",set)!=NULL);
+ assert(set_contains(set,"homeDirectory"));
+ assert(set_contains(set,"uidNumber"));
+ assert(set_contains(set,"uid"));
+ set_free(set);
+}
+
+/* the main program... */
+int main(int UNUSED(argc),char UNUSED(*argv[]))
+{
+ test_parse_name();
+ test_expr_parse();
+ test_buffer_overflow();
+ test_expr_vars();
+ return EXIT_SUCCESS;
+}
diff --git a/tests/test_myldap.c b/tests/test_myldap.c
index 7387d51..44dfc34 100644
--- a/tests/test_myldap.c
+++ b/tests/test_myldap.c
@@ -2,7 +2,7 @@
test_myldap.c - simple test for the myldap module
This file is part of the nss-pam-ldapd library.
- Copyright (C) 2007 Arthur de Jong
+ Copyright (C) 2007, 2008, 2009 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
@@ -45,6 +45,9 @@ const char **base_get_var(int UNUSED(map)) {return NULL;}
int *scope_get_var(int UNUSED(map)) {return NULL;}
const char **filter_get_var(int UNUSED(map)) {return &foo;}
const char **attmap_get_var(int UNUSED(map),const char UNUSED(*name)) {return &foo;}
+const char *attmap_get_value(MYLDAP_ENTRY UNUSED(*entry),const char UNUSED(*attr),char UNUSED(*buffer),size_t UNUSED(buflen)) {return "";}
+void *attmap_add_attributes(void UNUSED(*set),const char UNUSED(*attr)) {return NULL;}
+const char *attmap_set_mapping(const char UNUSED(**var),const char UNUSED(*value)) {return NULL;}
/* the maxium number of results to print (all results are retrieved) */
#define MAXRESULTS 10