source: trunk/src/cutest_sh_utils.c@ 73

Last change on this file since 73 was 68, checked in by rainer, 18 years ago

Update trunk to samhain 2.3

File size: 8.0 KB
RevLine 
[21]1
2#include "config_xor.h"
3
4#include <string.h>
5#include "CuTest.h"
6#include "samhain.h"
7#include "sh_utils.h"
8
[68]9void Test_sh_util_acl_compact (CuTest *tc) {
10 char * ret = 0;
11 char inp1[] = "user::r--\nuser:lisa:rwx\t\t#effective: r--\ngroup::r--\ngroup:toolies:rw- #effective: r--\nmask::r--\nother::r--\n";
12 char inp2[] = "use\n\nuser:lisa:rwx\t\t#effective: r--\ngroup::r--\ngroup:toolies:rw- #effective: r--\nmask::r--\nother::r--\n";
13 char inp3[] = "user:\177\145\177\122:r--\nuser:lisa:rwx\t\t#effective: r--\ngroup::r--\ngroup:toolies:rw- #effective: r--\nmask::r--\nother::r--\n";
14
15 ret = sh_util_acl_compact (inp1, strlen(inp1));
16 CuAssertPtrNotNull(tc, ret);
17 CuAssertStrEquals(tc, "u::r--,u:lisa:rwx,g::r--,g:toolies:rw-,m::r--,o::r--",
18 ret);
19
20 ret = sh_util_acl_compact (inp2, strlen(inp2));
21 CuAssertPtrNotNull(tc, ret);
22 CuAssertStrEquals(tc, "use,u:lisa:rwx,g::r--,g:toolies:rw-,m::r--,o::r--",
23 ret);
24
25 ret = sh_util_acl_compact (inp3, strlen(inp3));
26 CuAssertPtrNotNull(tc, ret);
27 CuAssertStrEquals(tc, "u:eR:r--,u:lisa:rwx,g::r--,g:toolies:rw-,m::r--,o::r--",
28 ret);
29
30 return;
31}
32
[34]33void Test_sh_util_strdup_ok (CuTest *tc) {
34 char * ret = 0;
35 char inp[] = "foobar";
36
37 ret = sh_util_strdup(inp);
38 CuAssertPtrNotNull(tc, ret);
39 CuAssert(tc, "expected inp != ret, but inp == ret", (inp != ret));
40 CuAssertStrEquals(tc, "foobar", ret);
41 return;
42}
43
44void Test_sh_util_strconcat_ok (CuTest *tc) {
45 char * ret = 0;
46
47 ret = sh_util_strconcat("foo", NULL);
48 CuAssertPtrNotNull(tc, ret);
49 CuAssertStrEquals(tc, "foo", ret);
50
51 ret = sh_util_strconcat("foo", "bar", NULL);
52 CuAssertPtrNotNull(tc, ret);
53 CuAssertStrEquals(tc, "foobar", ret);
54
55 ret = sh_util_strconcat("/", "foo", "/", "bar", NULL);
56 CuAssertPtrNotNull(tc, ret);
57 CuAssertStrEquals(tc, "/foo/bar", ret);
58
59 return;
60}
61
62void Test_sh_util_dirname_ok (CuTest *tc) {
63 char * ret = 0;
64
65 char input0[] = "/foo/bar";
66 char res0[] = "/foo";
67
68 char input1[] = "/foo/bar/";
69 char res1[] = "/foo";
70
71 char input2[] = "/foo";
72 char res2[] = "/";
73
74 char input3[] = "/";
75 char res3[] = "/";
76
77 ret = sh_util_dirname(input0);
78 CuAssertPtrNotNull(tc, ret);
79 CuAssertStrEquals(tc, res0, ret);
80
81 ret = sh_util_dirname(input1);
82 CuAssertPtrNotNull(tc, ret);
83 CuAssertStrEquals(tc, res1, ret);
84
85 ret = sh_util_dirname(input2);
86 CuAssertPtrNotNull(tc, ret);
87 CuAssertStrEquals(tc, res2, ret);
88
89 ret = sh_util_dirname(input3);
90 CuAssertPtrNotNull(tc, ret);
91 CuAssertStrEquals(tc, res3, ret);
92 return;
93}
94
95void Test_sh_util_basename_ok (CuTest *tc) {
96 char * ret = 0;
97
98 char input0[] = "/foo/bar";
99 char res0[] = "bar";
100
101 char input1[] = "/foo/";
102 char res1[] = "foo";
103
104 char input2[] = "/foo";
105 char res2[] = "foo";
106
107 char input3[] = "/";
108 char res3[] = "/";
109
110 char input4[] = "/foo/bar/";
111 char res4[] = "bar";
112
113 ret = sh_util_basename(input0);
114 CuAssertPtrNotNull(tc, ret);
115 CuAssertStrEquals(tc, res0, ret);
116
117 ret = sh_util_basename(input1);
118 CuAssertPtrNotNull(tc, ret);
119 CuAssertStrEquals(tc, res1, ret);
120
121 ret = sh_util_basename(input2);
122 CuAssertPtrNotNull(tc, ret);
123 CuAssertStrEquals(tc, res2, ret);
124
125 ret = sh_util_basename(input3);
126 CuAssertPtrNotNull(tc, ret);
127 CuAssertStrEquals(tc, res3, ret);
128
129 ret = sh_util_basename(input4);
130 CuAssertPtrNotNull(tc, ret);
131 CuAssertStrEquals(tc, res4, ret);
132
133 return;
134}
135
[68]136void Test_sh_util_utf8_ok (CuTest *tc) {
137 int ret = 0;
138#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
139 unsigned char seq[16];
140 unsigned char input[16] = "foobar";
141
142 seq[0] = 0x00;
143 ret = sh_util_valid_utf8(seq);
144 CuAssertIntEquals(tc, ret, S_TRUE);
145
146 seq[0] = 0xd7; seq[1] = 0x90; seq[2] = 0x00;
147 ret = sh_util_valid_utf8(seq);
148 CuAssertIntEquals(tc, ret, S_TRUE);
149
150 seq[0] = 0xed; seq[1] = 0x9f; seq[2] = 0xbf; seq[3] = 0x00;
151 ret = sh_util_valid_utf8(seq);
152 CuAssertIntEquals(tc, ret, S_TRUE);
153
154 seq[0] = 0xee; seq[1] = 0x80; seq[2] = 0x80; seq[3] = 0x00;
155 ret = sh_util_valid_utf8(seq);
156 CuAssertIntEquals(tc, ret, S_TRUE);
157
158 seq[0] = 0xef; seq[1] = 0xbf; seq[2] = 0xbd; seq[3] = 0x00;
159 ret = sh_util_valid_utf8(seq);
160 CuAssertIntEquals(tc, ret, S_TRUE);
161
162 seq[0] = 0xf4; seq[1] = 0x8f; seq[2] = 0xbf; seq[3] = 0xbf; seq[4] = 0x00;
163 ret = sh_util_valid_utf8(seq);
164 CuAssertIntEquals(tc, ret, S_TRUE);
165
166 seq[0] = 0xf4; seq[1] = 0x90; seq[2] = 0x80; seq[3] = 0x80; seq[4] = 0x00;
167 ret = sh_util_valid_utf8(seq);
168 CuAssertIntEquals(tc, ret, S_TRUE);
169
170 seq[0] = 0xd7; seq[1] = 0x90; seq[2] = 0xd7; seq[3] = 0x90; seq[4] = 0x00;
171 ret = sh_util_valid_utf8(seq);
172 CuAssertIntEquals(tc, ret, S_TRUE);
173
174 /* cont. char */
175
176 seq[0] = 0x80; seq[1] = 0x00;
177 ret = sh_util_valid_utf8(seq);
178 CuAssertIntEquals(tc, ret, S_FALSE);
179
180 seq[0] = 0xbf; seq[1] = 0x00;
181 ret = sh_util_valid_utf8(seq);
182 CuAssertIntEquals(tc, ret, S_FALSE);
183
184 /* overlong */
185
186 seq[0] = 0xc0; seq[1] = 0xaf; seq[2] = 0x00;
187 ret = sh_util_valid_utf8(seq);
188 CuAssertIntEquals(tc, ret, S_FALSE);
189
190 seq[0] = 0xe0; seq[1] = 0x8f; seq[2] = 0xaf; seq[3] = 0x00;
191 ret = sh_util_valid_utf8(seq);
192 CuAssertIntEquals(tc, ret, S_FALSE);
193
194 seq[0] = 0xf0; seq[1] = 0x80; seq[2] = 0x80; seq[3] = 0xaf; seq[4] = 0x00;
195 ret = sh_util_valid_utf8(seq);
196 CuAssertIntEquals(tc, ret, S_FALSE);
197
198 /* overlong */
199
200 seq[0] = 0xc1; seq[1] = 0xbf; seq[2] = 0x00;
201 ret = sh_util_valid_utf8(seq);
202 CuAssertIntEquals(tc, ret, S_FALSE);
203
204 seq[0] = 0xe0; seq[1] = 0x9f; seq[2] = 0xbf; seq[3] = 0x00;
205 ret = sh_util_valid_utf8(seq);
206 CuAssertIntEquals(tc, ret, S_FALSE);
207
208 seq[0] = 0xf0; seq[1] = 0x8f; seq[2] = 0xbf; seq[3] = 0xbf; seq[4] = 0x00;
209 ret = sh_util_valid_utf8(seq);
210 CuAssertIntEquals(tc, ret, S_FALSE);
211
212 /* overlong */
213
214 seq[0] = 0xc0; seq[1] = 0x80; seq[2] = 0x00;
215 ret = sh_util_valid_utf8(seq);
216 CuAssertIntEquals(tc, ret, S_FALSE);
217
218 seq[0] = 0xe0; seq[1] = 0x80; seq[2] = 0x80; seq[3] = 0x00;
219 ret = sh_util_valid_utf8(seq);
220 CuAssertIntEquals(tc, ret, S_FALSE);
221
222 seq[0] = 0xf0; seq[1] = 0x80; seq[2] = 0x80; seq[3] = 0x80; seq[4] = 0x00;
223 ret = sh_util_valid_utf8(seq);
224 CuAssertIntEquals(tc, ret, S_FALSE);
225
226 /* cont missing */
227
228 seq[0] = 0xd7; seq[1] = 0x20; seq[3] = 0x00;
229 ret = sh_util_valid_utf8(seq);
230 CuAssertIntEquals(tc, ret, S_FALSE);
231
232 seq[0] = 0xee; seq[1] = 0x80; seq[2] = 0x20; seq[3] = 0x00;
233 ret = sh_util_valid_utf8(seq);
234 CuAssertIntEquals(tc, ret, S_FALSE);
235
236 seq[0] = 0xf4; seq[1] = 0x8f; seq[2] = 0xbf; seq[3] = 0x20; seq[4] = 0x00;
237 ret = sh_util_valid_utf8(seq);
238 CuAssertIntEquals(tc, ret, S_FALSE);
239
240
241 ret = sh_util_obscure_ok ("0x01,0x02,0x03");
242 CuAssertIntEquals(tc, ret, 0);
243
244 ret = sh_util_valid_utf8 (input);
245 CuAssertIntEquals(tc, ret, S_TRUE);
246
247 input[0] = '\t';
248 ret = sh_util_valid_utf8 (input);
249 CuAssertIntEquals(tc, ret, S_FALSE);
250
251 input[0] = 0x01;
252 ret = sh_util_valid_utf8 (input);
253 CuAssertIntEquals(tc, ret, S_TRUE);
254
255 input[0] = 0x02;
256 ret = sh_util_valid_utf8 (input);
257 CuAssertIntEquals(tc, ret, S_TRUE);
258
259 input[0] = 0x03;
260 ret = sh_util_valid_utf8 (input);
261 CuAssertIntEquals(tc, ret, S_TRUE);
262
263 input[0] = 0x04;
264 ret = sh_util_valid_utf8 (input);
265 CuAssertIntEquals(tc, ret, S_FALSE);
266
267
268#else
269 CuAssertIntEquals(tc, ret, 0);
270#endif
271}
272
[21]273void Test_sh_util_obscure_ok (CuTest *tc) {
274
275 int ret = 0;
[22]276#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
[21]277 char input[16] = "foobar";
278
279 ret = sh_util_obscure_ok ("0xA1,0xA2,0xA3");
280 CuAssertIntEquals(tc, ret, 0);
281
282 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
283 CuAssertIntEquals(tc, ret, 0);
284
285 input[0] = '\t';
286 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
287 CuAssertIntEquals(tc, ret, -1);
288
289 input[0] = 0xA1;
290 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
291 CuAssertIntEquals(tc, ret, 0);
292
293 input[0] = 0xA2;
294 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
295 CuAssertIntEquals(tc, ret, 0);
296
297 input[0] = 0xA3;
298 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
299 CuAssertIntEquals(tc, ret, 0);
300
301 input[0] = 0xA4;
302 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
303 CuAssertIntEquals(tc, ret, -1);
304
305#else
306 CuAssertIntEquals(tc, ret, 0);
307#endif
308}
309
310
Note: See TracBrowser for help on using the repository browser.