1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
/* libanode: the Anode C reference implementation
* Copyright (C) 2009 Adam Ierymenko <adam.ierymenko@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include "../dictionary.h"
static const char *HASH_TESTS[16] = {
"test",
"testt",
"",
"foo",
"fooo",
"1",
"2",
"3",
"4",
"11",
"22",
"33",
"44",
"adklfjklejrer",
"erngnetbekjrq",
"erklerqqqqre"
};
int diterate(void *arg,const char *key,const char *value)
{
printf(" %s: %s\n",key ? key : "(null)",value ? value : "(null)");
return 1;
}
int main(int argc,char **argv)
{
char tmp[1024];
char fuzzparam1[16],fuzzparam2[16],fuzzparam3[16];
struct AnodeDictionary d;
unsigned int i,j,k,cs;
srandom(time(0));
printf("Trying out hash function a little...\n");
for(i=0;i<16;++i)
printf(" %s: %u\n",HASH_TESTS[i],(unsigned int)AnodeDictionary__get_bucket(HASH_TESTS[i]));
for(cs=0;cs<2;++cs) {
printf("\nTesting with case sensitivity = %d\n",cs);
AnodeDictionary_init(&d,cs);
printf("\nTesting dictionary by adding and retrieving some keys...\n");
AnodeDictionary_put(&d,"test1","This is the first test");
AnodeDictionary_put(&d,"test2","This is the second test");
AnodeDictionary_put(&d,"test3","This is the third test (lower case)");
AnodeDictionary_put(&d,"TEST3","This is the third test (UPPER CASE)");
AnodeDictionary_iterate(&d,(void *)0,&diterate);
if (d.size != (cs ? 4 : 3)) {
printf("Failed (size).\n");
return 1;
}
AnodeDictionary_clear(&d);
if (d.size||(AnodeDictionary_get(&d,"test1"))) {
printf("Failed (clear).\n");
return 1;
}
printf("\nTesting read, trial 1: simple key=value with unterminated line\n");
strcpy(tmp,"foo=bar\nbar=baz\ntest1=Happy happy joyjoy!\ntest2=foobarbaz\nlinewithnocr=thisworked");
AnodeDictionary_read(&d,tmp,"\r\n","=","",'\\',0,0);
printf("Results:\n");
AnodeDictionary_iterate(&d,(void *)0,&diterate);
AnodeDictionary_clear(&d);
printf("\nTesting read, trial 2: key=value with escape chars, escaped CRs\n");
strcpy(tmp,"foo=bar\r\nbar==baz\nte\\=st1=\\=Happy happy joyjoy!\ntest2=foobarbaz\\\nfoobarbaz on next line\r\n");
AnodeDictionary_read(&d,tmp,"\r\n","=","",'\\',0,0);
printf("Results:\n");
AnodeDictionary_iterate(&d,(void *)0,&diterate);
AnodeDictionary_clear(&d);
printf("\nTesting read, trial 3: HTTP header-like dictionary\n");
strcpy(tmp,"Host: some.host.net\r\nX-Some-Header: foo bar\r\nX-Some-Other-Header: y0y0y0y0y0\r\n");
AnodeDictionary_read(&d,tmp,"\r\n",": ","",0,0,0);
printf("Results:\n");
AnodeDictionary_iterate(&d,(void *)0,&diterate);
AnodeDictionary_clear(&d);
printf("\nTesting read, trial 4: single line key/value\n");
strcpy(tmp,"Header: one line only");
AnodeDictionary_read(&d,tmp,"\r\n",": ","",0,0,0);
printf("Results:\n");
AnodeDictionary_iterate(&d,(void *)0,&diterate);
AnodeDictionary_clear(&d);
printf("\nFuzzing dictionary reader...\n"); fflush(stdout);
for(i=0;i<200000;++i) {
j = random() % (sizeof(tmp) - 1);
for(k=0;k<j;++k) {
tmp[k] = (char)((unsigned int)random() >> 3);
if (!tmp[k]) tmp[k] = 1;
}
tmp[j] = (char)0;
j = random() % (sizeof(fuzzparam1) - 1);
for(k=0;k<j;++k) {
fuzzparam1[k] = (char)((unsigned int)random() >> 3);
if (!fuzzparam1[k]) fuzzparam1[k] = 1;
}
fuzzparam1[j] = (char)0;
j = random() % (sizeof(fuzzparam2) - 1);
for(k=0;k<j;++k) {
fuzzparam1[k] = (char)((unsigned int)random() >> 3);
if (!fuzzparam2[k]) fuzzparam2[k] = 1;
}
fuzzparam2[j] = (char)0;
j = random() % (sizeof(fuzzparam3) - 1);
for(k=0;k<j;++k) {
fuzzparam3[k] = (char)((unsigned int)random() >> 3);
if (!fuzzparam3[k]) fuzzparam3[k] = 1;
}
fuzzparam3[j] = (char)0;
AnodeDictionary_read(&d,tmp,fuzzparam1,fuzzparam2,fuzzparam3,random() & 3,random() & 1,random() & 1);
AnodeDictionary_clear(&d);
}
AnodeDictionary_destroy(&d);
}
return 0;
}
|