2
2
test_expr.c - simple tests for the expr module
3
3
This file is part of the nss-pam-ldapd library.
5
Copyright (C) 2009 Arthur de Jong
5
Copyright (C) 2009, 2011 Arthur de Jong
7
7
This library is free software; you can redistribute it and/or
8
8
modify it under the terms of the GNU Lesser General Public
26
26
#include <string.h>
27
27
#include <assert.h>
29
31
/* we include expr.c because we want to test the static methods */
30
32
#include "common/expr.c"
32
#ifndef __ASSERT_FUNCTION
33
#define __ASSERT_FUNCTION ""
34
#endif /* not __ASSERT_FUNCTION */
36
#define assertstreq(str1,str2) \
37
(assertstreq_impl(str1,str2,"strcmp(" __STRING(str1) "," __STRING(str2) ")==0", \
38
__FILE__, __LINE__, __ASSERT_FUNCTION))
41
#define __assert_fail(assertion,file,line,function) __assert(assertion,file,line)
43
/* method for determening string equalness */
44
static void assertstreq_impl(const char *str1,const char *str2,
45
const char *assertion,const char *file,
46
int line,const char *function)
48
if (strcmp(str1,str2)!=0)
49
__assert_fail(assertion,file,line,function);
52
34
static void test_parse_name(void)
78
62
assert(expr_parse("$test1",buffer,sizeof(buffer),expanderfn,NULL)!=NULL);
79
63
assertstreq(buffer,"foobar");
64
assert(expr_parse("\\$test1",buffer,sizeof(buffer),expanderfn,NULL)!=NULL);
65
assertstreq(buffer,"$test1");
80
66
assert(expr_parse("$empty",buffer,sizeof(buffer),expanderfn,NULL)!=NULL);
81
67
assertstreq(buffer,"");
68
assert(expr_parse("$foo1$empty-$foo2",buffer,sizeof(buffer),expanderfn,NULL)!=NULL);
69
assertstreq(buffer,"foobar-foobar");
70
assert(expr_parse("$foo1+$null+$foo2",buffer,sizeof(buffer),expanderfn,NULL)!=NULL);
71
assertstreq(buffer,"foobar++foobar");
82
72
assert(expr_parse("${test1}\\$",buffer,sizeof(buffer),expanderfn,NULL)!=NULL);
83
73
assertstreq(buffer,"foobar$");
84
74
assert(expr_parse("${test1:-default}",buffer,sizeof(buffer),expanderfn,NULL)!=NULL);
101
91
assertstreq(buffer,"afoobarbfoobarec");
102
92
assert(expr_parse("a${test1}b${test2:+${empty:-d$test4}e}c",buffer,sizeof(buffer),expanderfn,NULL)!=NULL);
103
93
assertstreq(buffer,"afoobarbdfoobarec");
94
/* these are errors */
95
assert(expr_parse("$&",buffer,sizeof(buffer),expanderfn,NULL)==NULL);
96
assert(expr_parse("${a",buffer,sizeof(buffer),expanderfn,NULL)==NULL);
106
99
static void test_buffer_overflow(void)