source: trunk/src/cutest_sh_utils.c@ 148

Last change on this file since 148 was 93, checked in by rainer, 18 years ago

Add check for PCI ROMs; fix ticket #51 (symlinks in root directory reported with leading double slash).

File size: 12.7 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
[76]9void Test_sl_strlcpy (CuTest *tc) {
10 int ret;
11 char out[] = "aaaaaa";
12 char in[] = "bbb";
13
14 ret = sl_strlcpy (NULL, NULL, 0);
15 CuAssertIntEquals(tc, ret, SL_ENONE);
16
17 ret = sl_strlcpy (NULL, in, 0);
18 CuAssertIntEquals(tc, ret, SL_ENULL);
19
20 ret = sl_strlcpy (out, NULL, 0);
21 CuAssertIntEquals(tc, ret, SL_ENONE);
22
23 ret = sl_strlcpy (out, in, 0);
24 CuAssertIntEquals(tc, ret, SL_ENONE);
25
26 ret = sl_strlcpy (out, NULL, 7);
27 CuAssertIntEquals(tc, ret, SL_ENONE);
28 CuAssertStrEquals(tc, "", out);
29
30 out[0] = 'a';
31 ret = sl_strlcpy (out, in, 4);
32 CuAssertIntEquals(tc, ret, SL_ENONE);
33 CuAssertStrEquals(tc, "bbb", out);
34 CuAssertStrEquals(tc, "aa", &out[4]);
35
36 return;
37}
38
39void Test_sl_strlcat (CuTest *tc) {
40 int ret;
41 char out[16] = "aaaaaa";
42 char in[16] = "bbb";
43
44 ret = sl_strlcat (NULL, NULL, 0);
45 CuAssertIntEquals(tc, ret, SL_ENONE);
46
47 ret = sl_strlcat (NULL, in, 0);
48 CuAssertIntEquals(tc, ret, SL_ENONE);
49
50 ret = sl_strlcat (out, NULL, 0);
51 CuAssertIntEquals(tc, ret, SL_ENONE);
52
53 ret = sl_strlcat (out, in, 0);
54 CuAssertIntEquals(tc, ret, SL_ENONE);
55
56 ret = sl_strlcat (out, NULL, sizeof(out));
57 CuAssertIntEquals(tc, ret, SL_ENONE);
58 CuAssertStrEquals(tc, "aaaaaa", out);
59
60 ret = sl_strlcat (out, in, 7);
61 CuAssertIntEquals(tc, ret, SL_ETRUNC);
62 CuAssertStrEquals(tc, "aaaaaa", out);
63
64 ret = sl_strlcat (out, in, 8);
65 CuAssertIntEquals(tc, ret, SL_ETRUNC);
66 CuAssertStrEquals(tc, "aaaaaab", out);
67
68 ret = sl_strlcat (out, in, sizeof(out));
69 CuAssertIntEquals(tc, ret, SL_ENONE);
70 CuAssertStrEquals(tc, "aaaaaabbbb", out);
71
72 CuAssertStrEquals(tc, "bbb", in);
73
74 return;
75}
76
[68]77void Test_sh_util_acl_compact (CuTest *tc) {
78 char * ret = 0;
79 char inp1[] = "user::r--\nuser:lisa:rwx\t\t#effective: r--\ngroup::r--\ngroup:toolies:rw- #effective: r--\nmask::r--\nother::r--\n";
80 char inp2[] = "use\n\nuser:lisa:rwx\t\t#effective: r--\ngroup::r--\ngroup:toolies:rw- #effective: r--\nmask::r--\nother::r--\n";
81 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";
82
83 ret = sh_util_acl_compact (inp1, strlen(inp1));
84 CuAssertPtrNotNull(tc, ret);
85 CuAssertStrEquals(tc, "u::r--,u:lisa:rwx,g::r--,g:toolies:rw-,m::r--,o::r--",
86 ret);
87
88 ret = sh_util_acl_compact (inp2, strlen(inp2));
89 CuAssertPtrNotNull(tc, ret);
90 CuAssertStrEquals(tc, "use,u:lisa:rwx,g::r--,g:toolies:rw-,m::r--,o::r--",
91 ret);
92
93 ret = sh_util_acl_compact (inp3, strlen(inp3));
94 CuAssertPtrNotNull(tc, ret);
95 CuAssertStrEquals(tc, "u:eR:r--,u:lisa:rwx,g::r--,g:toolies:rw-,m::r--,o::r--",
96 ret);
97
98 return;
99}
100
[34]101void Test_sh_util_strdup_ok (CuTest *tc) {
102 char * ret = 0;
103 char inp[] = "foobar";
104
105 ret = sh_util_strdup(inp);
106 CuAssertPtrNotNull(tc, ret);
107 CuAssert(tc, "expected inp != ret, but inp == ret", (inp != ret));
108 CuAssertStrEquals(tc, "foobar", ret);
109 return;
110}
111
112void Test_sh_util_strconcat_ok (CuTest *tc) {
113 char * ret = 0;
114
115 ret = sh_util_strconcat("foo", NULL);
116 CuAssertPtrNotNull(tc, ret);
117 CuAssertStrEquals(tc, "foo", ret);
118
119 ret = sh_util_strconcat("foo", "bar", NULL);
120 CuAssertPtrNotNull(tc, ret);
121 CuAssertStrEquals(tc, "foobar", ret);
122
123 ret = sh_util_strconcat("/", "foo", "/", "bar", NULL);
124 CuAssertPtrNotNull(tc, ret);
125 CuAssertStrEquals(tc, "/foo/bar", ret);
126
127 return;
128}
129
130void Test_sh_util_dirname_ok (CuTest *tc) {
131 char * ret = 0;
132
133 char input0[] = "/foo/bar";
134 char res0[] = "/foo";
135
136 char input1[] = "/foo/bar/";
137 char res1[] = "/foo";
138
139 char input2[] = "/foo";
140 char res2[] = "/";
141
142 char input3[] = "/";
143 char res3[] = "/";
144
[93]145 char input4[] = "///foo//bar";
146 char res4[] = "///foo";
147
148 char input5[] = "//foo///bar///";
149 char res5[] = "//foo";
150
151 char input6[] = "///";
152 char res6[] = "///";
153
154 char input7[] = "//f///b///";
155 char res7[] = "//f";
156
157 char input8[] = "/f/b/";
158 char res8[] = "/f";
159
160 char input9[] = "/f/b";
161 char res9[] = "/f";
162
[34]163 ret = sh_util_dirname(input0);
164 CuAssertPtrNotNull(tc, ret);
165 CuAssertStrEquals(tc, res0, ret);
166
167 ret = sh_util_dirname(input1);
168 CuAssertPtrNotNull(tc, ret);
169 CuAssertStrEquals(tc, res1, ret);
170
171 ret = sh_util_dirname(input2);
172 CuAssertPtrNotNull(tc, ret);
173 CuAssertStrEquals(tc, res2, ret);
174
175 ret = sh_util_dirname(input3);
176 CuAssertPtrNotNull(tc, ret);
177 CuAssertStrEquals(tc, res3, ret);
[93]178
179 ret = sh_util_dirname(input4);
180 CuAssertPtrNotNull(tc, ret);
181 CuAssertStrEquals(tc, res4, ret);
182
183 ret = sh_util_dirname(input5);
184 CuAssertPtrNotNull(tc, ret);
185 CuAssertStrEquals(tc, res5, ret);
186
187 ret = sh_util_dirname(input6);
188 CuAssertPtrNotNull(tc, ret);
189 CuAssertStrEquals(tc, res6, ret);
190
191 ret = sh_util_dirname(input7);
192 CuAssertPtrNotNull(tc, ret);
193 CuAssertStrEquals(tc, res7, ret);
194
195 ret = sh_util_dirname(input8);
196 CuAssertPtrNotNull(tc, ret);
197 CuAssertStrEquals(tc, res8, ret);
198
199 ret = sh_util_dirname(input9);
200 CuAssertPtrNotNull(tc, ret);
201 CuAssertStrEquals(tc, res9, ret);
[34]202 return;
203}
204
205void Test_sh_util_basename_ok (CuTest *tc) {
206 char * ret = 0;
207
208 char input0[] = "/foo/bar";
209 char res0[] = "bar";
210
211 char input1[] = "/foo/";
212 char res1[] = "foo";
213
214 char input2[] = "/foo";
215 char res2[] = "foo";
216
217 char input3[] = "/";
218 char res3[] = "/";
219
220 char input4[] = "/foo/bar/";
221 char res4[] = "bar";
222
[93]223 char input5[] = "/foo///bar///";
224 char res5[] = "bar";
225
226 char input6[] = "//foo";
227 char res6[] = "foo";
228
[34]229 ret = sh_util_basename(input0);
230 CuAssertPtrNotNull(tc, ret);
231 CuAssertStrEquals(tc, res0, ret);
232
233 ret = sh_util_basename(input1);
234 CuAssertPtrNotNull(tc, ret);
235 CuAssertStrEquals(tc, res1, ret);
236
237 ret = sh_util_basename(input2);
238 CuAssertPtrNotNull(tc, ret);
239 CuAssertStrEquals(tc, res2, ret);
240
241 ret = sh_util_basename(input3);
242 CuAssertPtrNotNull(tc, ret);
243 CuAssertStrEquals(tc, res3, ret);
244
245 ret = sh_util_basename(input4);
246 CuAssertPtrNotNull(tc, ret);
247 CuAssertStrEquals(tc, res4, ret);
248
[93]249 ret = sh_util_basename(input5);
250 CuAssertPtrNotNull(tc, ret);
251 CuAssertStrEquals(tc, res5, ret);
252
253 ret = sh_util_basename(input6);
254 CuAssertPtrNotNull(tc, ret);
255 CuAssertStrEquals(tc, res6, ret);
256
[34]257 return;
258}
259
[68]260void Test_sh_util_utf8_ok (CuTest *tc) {
261 int ret = 0;
262#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
263 unsigned char seq[16];
264 unsigned char input[16] = "foobar";
265
266 seq[0] = 0x00;
267 ret = sh_util_valid_utf8(seq);
268 CuAssertIntEquals(tc, ret, S_TRUE);
269
270 seq[0] = 0xd7; seq[1] = 0x90; seq[2] = 0x00;
271 ret = sh_util_valid_utf8(seq);
272 CuAssertIntEquals(tc, ret, S_TRUE);
273
274 seq[0] = 0xed; seq[1] = 0x9f; seq[2] = 0xbf; seq[3] = 0x00;
275 ret = sh_util_valid_utf8(seq);
276 CuAssertIntEquals(tc, ret, S_TRUE);
277
278 seq[0] = 0xee; seq[1] = 0x80; seq[2] = 0x80; seq[3] = 0x00;
279 ret = sh_util_valid_utf8(seq);
280 CuAssertIntEquals(tc, ret, S_TRUE);
281
282 seq[0] = 0xef; seq[1] = 0xbf; seq[2] = 0xbd; seq[3] = 0x00;
283 ret = sh_util_valid_utf8(seq);
284 CuAssertIntEquals(tc, ret, S_TRUE);
285
286 seq[0] = 0xf4; seq[1] = 0x8f; seq[2] = 0xbf; seq[3] = 0xbf; seq[4] = 0x00;
287 ret = sh_util_valid_utf8(seq);
288 CuAssertIntEquals(tc, ret, S_TRUE);
289
290 seq[0] = 0xf4; seq[1] = 0x90; seq[2] = 0x80; seq[3] = 0x80; seq[4] = 0x00;
291 ret = sh_util_valid_utf8(seq);
292 CuAssertIntEquals(tc, ret, S_TRUE);
293
294 seq[0] = 0xd7; seq[1] = 0x90; seq[2] = 0xd7; seq[3] = 0x90; seq[4] = 0x00;
295 ret = sh_util_valid_utf8(seq);
296 CuAssertIntEquals(tc, ret, S_TRUE);
297
298 /* cont. char */
299
300 seq[0] = 0x80; seq[1] = 0x00;
301 ret = sh_util_valid_utf8(seq);
302 CuAssertIntEquals(tc, ret, S_FALSE);
303
304 seq[0] = 0xbf; seq[1] = 0x00;
305 ret = sh_util_valid_utf8(seq);
306 CuAssertIntEquals(tc, ret, S_FALSE);
307
308 /* overlong */
309
310 seq[0] = 0xc0; seq[1] = 0xaf; seq[2] = 0x00;
311 ret = sh_util_valid_utf8(seq);
312 CuAssertIntEquals(tc, ret, S_FALSE);
313
314 seq[0] = 0xe0; seq[1] = 0x8f; seq[2] = 0xaf; seq[3] = 0x00;
315 ret = sh_util_valid_utf8(seq);
316 CuAssertIntEquals(tc, ret, S_FALSE);
317
318 seq[0] = 0xf0; seq[1] = 0x80; seq[2] = 0x80; seq[3] = 0xaf; seq[4] = 0x00;
319 ret = sh_util_valid_utf8(seq);
320 CuAssertIntEquals(tc, ret, S_FALSE);
321
322 /* overlong */
323
324 seq[0] = 0xc1; seq[1] = 0xbf; seq[2] = 0x00;
325 ret = sh_util_valid_utf8(seq);
326 CuAssertIntEquals(tc, ret, S_FALSE);
327
328 seq[0] = 0xe0; seq[1] = 0x9f; seq[2] = 0xbf; seq[3] = 0x00;
329 ret = sh_util_valid_utf8(seq);
330 CuAssertIntEquals(tc, ret, S_FALSE);
331
332 seq[0] = 0xf0; seq[1] = 0x8f; seq[2] = 0xbf; seq[3] = 0xbf; seq[4] = 0x00;
333 ret = sh_util_valid_utf8(seq);
334 CuAssertIntEquals(tc, ret, S_FALSE);
335
336 /* overlong */
337
338 seq[0] = 0xc0; seq[1] = 0x80; seq[2] = 0x00;
339 ret = sh_util_valid_utf8(seq);
340 CuAssertIntEquals(tc, ret, S_FALSE);
341
342 seq[0] = 0xe0; seq[1] = 0x80; seq[2] = 0x80; seq[3] = 0x00;
343 ret = sh_util_valid_utf8(seq);
344 CuAssertIntEquals(tc, ret, S_FALSE);
345
346 seq[0] = 0xf0; seq[1] = 0x80; seq[2] = 0x80; seq[3] = 0x80; seq[4] = 0x00;
347 ret = sh_util_valid_utf8(seq);
348 CuAssertIntEquals(tc, ret, S_FALSE);
349
350 /* cont missing */
351
352 seq[0] = 0xd7; seq[1] = 0x20; seq[3] = 0x00;
353 ret = sh_util_valid_utf8(seq);
354 CuAssertIntEquals(tc, ret, S_FALSE);
355
356 seq[0] = 0xee; seq[1] = 0x80; seq[2] = 0x20; seq[3] = 0x00;
357 ret = sh_util_valid_utf8(seq);
358 CuAssertIntEquals(tc, ret, S_FALSE);
359
360 seq[0] = 0xf4; seq[1] = 0x8f; seq[2] = 0xbf; seq[3] = 0x20; seq[4] = 0x00;
361 ret = sh_util_valid_utf8(seq);
362 CuAssertIntEquals(tc, ret, S_FALSE);
363
[76]364 /* switch on utf8 checking for sh_util_obscurename() */
[68]365
[76]366 ret = sh_util_obscure_utf8("Y");
367 CuAssertIntEquals(tc, ret, 0);
368
[68]369 ret = sh_util_obscure_ok ("0x01,0x02,0x03");
370 CuAssertIntEquals(tc, ret, 0);
371
372 ret = sh_util_valid_utf8 (input);
373 CuAssertIntEquals(tc, ret, S_TRUE);
[76]374 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
375 CuAssertIntEquals(tc, ret, 0);
[68]376
377 input[0] = '\t';
378 ret = sh_util_valid_utf8 (input);
379 CuAssertIntEquals(tc, ret, S_FALSE);
[76]380 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
381 CuAssertIntEquals(tc, ret, -1);
[68]382
383 input[0] = 0x01;
384 ret = sh_util_valid_utf8 (input);
385 CuAssertIntEquals(tc, ret, S_TRUE);
[76]386 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
387 CuAssertIntEquals(tc, ret, 0);
[68]388
389 input[0] = 0x02;
390 ret = sh_util_valid_utf8 (input);
391 CuAssertIntEquals(tc, ret, S_TRUE);
[76]392 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
393 CuAssertIntEquals(tc, ret, 0);
[68]394
395 input[0] = 0x03;
396 ret = sh_util_valid_utf8 (input);
397 CuAssertIntEquals(tc, ret, S_TRUE);
[76]398 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
399 CuAssertIntEquals(tc, ret, 0);
[68]400
401 input[0] = 0x04;
402 ret = sh_util_valid_utf8 (input);
403 CuAssertIntEquals(tc, ret, S_FALSE);
[76]404 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
405 CuAssertIntEquals(tc, ret, -1);
[68]406
[76]407 input[0] = 'f';
408 ret = sh_util_valid_utf8 (input);
409 CuAssertIntEquals(tc, ret, S_TRUE);
410 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
411 CuAssertIntEquals(tc, ret, 0);
[68]412
[76]413 input[5] = ' ';
414 ret = sh_util_valid_utf8 (input);
415 CuAssertIntEquals(tc, ret, S_FALSE);
416 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
417 CuAssertIntEquals(tc, ret, -1);
418
419 input[5] = 'r'; input[3] = ' ';
420 ret = sh_util_valid_utf8 (input);
421 CuAssertIntEquals(tc, ret, S_TRUE);
422 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
423 CuAssertIntEquals(tc, ret, 0);
424
425
[68]426#else
427 CuAssertIntEquals(tc, ret, 0);
428#endif
429}
430
[21]431void Test_sh_util_obscure_ok (CuTest *tc) {
432
433 int ret = 0;
[22]434#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
[21]435 char input[16] = "foobar";
436
[76]437 /* switch off utf8 checking for sh_util_obscurename() */
438
439 ret = sh_util_obscure_utf8("N");
440 CuAssertIntEquals(tc, ret, 0);
441
[21]442 ret = sh_util_obscure_ok ("0xA1,0xA2,0xA3");
443 CuAssertIntEquals(tc, ret, 0);
444
445 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
446 CuAssertIntEquals(tc, ret, 0);
447
448 input[0] = '\t';
449 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
450 CuAssertIntEquals(tc, ret, -1);
451
452 input[0] = 0xA1;
453 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
454 CuAssertIntEquals(tc, ret, 0);
455
456 input[0] = 0xA2;
457 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
458 CuAssertIntEquals(tc, ret, 0);
459
460 input[0] = 0xA3;
461 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
462 CuAssertIntEquals(tc, ret, 0);
463
464 input[0] = 0xA4;
465 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
466 CuAssertIntEquals(tc, ret, -1);
467
[76]468 input[0] = 'f';
469 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
470 CuAssertIntEquals(tc, ret, 0);
471
472 input[5] = ' ';
473 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
474 CuAssertIntEquals(tc, ret, -1);
475
476 input[5] = 'r'; input[3] = ' ';
477 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
478 CuAssertIntEquals(tc, ret, 0);
[21]479#else
480 CuAssertIntEquals(tc, ret, 0);
481#endif
482}
483
484
Note: See TracBrowser for help on using the repository browser.