source: trunk/src/cutest_sh_utils.c@ 326

Last change on this file since 326 was 167, checked in by katerina, 17 years ago

First parts of changes for MODI_TXT

File size: 14.4 KB
Line 
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
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
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
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_base64_enc_ok (CuTest *tc) {
131 unsigned char out[64];
132 unsigned char ou2[64];
133 int ret;
134 unsigned char inp0[64] = "";
135 unsigned char inp1[64] = "A";
136 unsigned char inp2[64] = "AB";
137 unsigned char inp3[64] = "ABC";
138 unsigned char inp4[64] = "ABCD";
139
140 ret = sh_util_base64_enc (out, inp0, strlen((char*)inp0));
141 CuAssertIntEquals(tc, ret, 0);
142 CuAssertStrEquals(tc, "", (char*)out);
143 ret = sh_util_base64_dec (ou2, out, strlen((char*)out));
144 CuAssertIntEquals(tc, ret, 0);
145 CuAssertStrEquals(tc, (char*)inp0, (char*)ou2);
146
147 ret = sh_util_base64_enc (out, inp1, strlen((char*)inp1));
148 CuAssertIntEquals(tc, ret, 4);
149 CuAssertStrEquals(tc, "QQ??", (char*)out);
150 ret = sh_util_base64_dec (ou2, out, strlen((char*)out));
151 CuAssertStrEquals(tc, (char*)inp1, (char*)ou2);
152 CuAssertIntEquals(tc, 1, ret);
153
154 ret = sh_util_base64_enc (out, inp2, strlen((char*)inp2));
155 CuAssertIntEquals(tc, ret, 4);
156 CuAssertStrEquals(tc, "QUI?", (char*)out);
157 ret = sh_util_base64_dec (ou2, out, strlen((char*)out));
158 CuAssertStrEquals(tc, (char*)inp2, (char*)ou2);
159 CuAssertIntEquals(tc, 2, ret);
160
161 ret = sh_util_base64_enc (out, inp3, strlen((char*)inp3));
162 CuAssertIntEquals(tc, ret, 4);
163 CuAssertStrEquals(tc, "QUJD", (char*)out);
164 ret = sh_util_base64_dec (ou2, out, strlen((char*)out));
165 CuAssertStrEquals(tc, (char*)inp3, (char*)ou2);
166 CuAssertIntEquals(tc, 3, ret);
167
168 ret = sh_util_base64_enc (out, inp4, strlen((char*)inp4));
169 CuAssertIntEquals(tc, ret, 8);
170 CuAssertStrEquals(tc, "QUJDRA??", (char*)out);
171 ret = sh_util_base64_dec (ou2, out, strlen((char*)out));
172 CuAssertStrEquals(tc, (char*)inp4, (char*)ou2);
173 CuAssertIntEquals(tc, 4, ret);
174
175
176 return;
177}
178
179void Test_sh_util_dirname_ok (CuTest *tc) {
180 char * ret = 0;
181
182 char input0[] = "/foo/bar";
183 char res0[] = "/foo";
184
185 char input1[] = "/foo/bar/";
186 char res1[] = "/foo";
187
188 char input2[] = "/foo";
189 char res2[] = "/";
190
191 char input3[] = "/";
192 char res3[] = "/";
193
194 char input4[] = "///foo//bar";
195 char res4[] = "///foo";
196
197 char input5[] = "//foo///bar///";
198 char res5[] = "//foo";
199
200 char input6[] = "///";
201 char res6[] = "///";
202
203 char input7[] = "//f///b///";
204 char res7[] = "//f";
205
206 char input8[] = "/f/b/";
207 char res8[] = "/f";
208
209 char input9[] = "/e/b";
210 char res9[] = "/e";
211
212 ret = sh_util_dirname(input0);
213 CuAssertPtrNotNull(tc, ret);
214 CuAssertStrEquals(tc, res0, ret);
215
216 ret = sh_util_dirname(input1);
217 CuAssertPtrNotNull(tc, ret);
218 CuAssertStrEquals(tc, res1, ret);
219
220 ret = sh_util_dirname(input2);
221 CuAssertPtrNotNull(tc, ret);
222 CuAssertStrEquals(tc, res2, ret);
223
224 ret = sh_util_dirname(input3);
225 CuAssertPtrNotNull(tc, ret);
226 CuAssertStrEquals(tc, res3, ret);
227
228 ret = sh_util_dirname(input4);
229 CuAssertPtrNotNull(tc, ret);
230 CuAssertStrEquals(tc, res4, ret);
231
232 ret = sh_util_dirname(input5);
233 CuAssertPtrNotNull(tc, ret);
234 CuAssertStrEquals(tc, res5, ret);
235
236 ret = sh_util_dirname(input6);
237 CuAssertPtrNotNull(tc, ret);
238 CuAssertStrEquals(tc, res6, ret);
239
240 ret = sh_util_dirname(input7);
241 CuAssertPtrNotNull(tc, ret);
242 CuAssertStrEquals(tc, res7, ret);
243
244 ret = sh_util_dirname(input8);
245 CuAssertPtrNotNull(tc, ret);
246 CuAssertStrEquals(tc, res8, ret);
247
248 ret = sh_util_dirname(input9);
249 CuAssertPtrNotNull(tc, ret);
250 CuAssertStrEquals(tc, res9, ret);
251 return;
252}
253
254void Test_sh_util_basename_ok (CuTest *tc) {
255 char * ret = 0;
256
257 char input0[] = "/foo/bar";
258 char res0[] = "bar";
259
260 char input1[] = "/foo/";
261 char res1[] = "foo";
262
263 char input2[] = "/foo";
264 char res2[] = "foo";
265
266 char input3[] = "/";
267 char res3[] = "/";
268
269 char input4[] = "/foo/bar/";
270 char res4[] = "bar";
271
272 char input5[] = "/foo///bar///";
273 char res5[] = "bar";
274
275 char input6[] = "//foo";
276 char res6[] = "foo";
277
278 ret = sh_util_basename(input0);
279 CuAssertPtrNotNull(tc, ret);
280 CuAssertStrEquals(tc, res0, ret);
281
282 ret = sh_util_basename(input1);
283 CuAssertPtrNotNull(tc, ret);
284 CuAssertStrEquals(tc, res1, ret);
285
286 ret = sh_util_basename(input2);
287 CuAssertPtrNotNull(tc, ret);
288 CuAssertStrEquals(tc, res2, ret);
289
290 ret = sh_util_basename(input3);
291 CuAssertPtrNotNull(tc, ret);
292 CuAssertStrEquals(tc, res3, ret);
293
294 ret = sh_util_basename(input4);
295 CuAssertPtrNotNull(tc, ret);
296 CuAssertStrEquals(tc, res4, ret);
297
298 ret = sh_util_basename(input5);
299 CuAssertPtrNotNull(tc, ret);
300 CuAssertStrEquals(tc, res5, ret);
301
302 ret = sh_util_basename(input6);
303 CuAssertPtrNotNull(tc, ret);
304 CuAssertStrEquals(tc, res6, ret);
305
306 return;
307}
308
309void Test_sh_util_utf8_ok (CuTest *tc) {
310 int ret = 0;
311#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
312 unsigned char seq[16];
313 unsigned char input[16] = "foobar";
314
315 seq[0] = 0x00;
316 ret = sh_util_valid_utf8(seq);
317 CuAssertIntEquals(tc, ret, S_TRUE);
318
319 seq[0] = 0xd7; seq[1] = 0x90; seq[2] = 0x00;
320 ret = sh_util_valid_utf8(seq);
321 CuAssertIntEquals(tc, ret, S_TRUE);
322
323 seq[0] = 0xed; seq[1] = 0x9f; seq[2] = 0xbf; seq[3] = 0x00;
324 ret = sh_util_valid_utf8(seq);
325 CuAssertIntEquals(tc, ret, S_TRUE);
326
327 seq[0] = 0xee; seq[1] = 0x80; seq[2] = 0x80; seq[3] = 0x00;
328 ret = sh_util_valid_utf8(seq);
329 CuAssertIntEquals(tc, ret, S_TRUE);
330
331 seq[0] = 0xef; seq[1] = 0xbf; seq[2] = 0xbd; seq[3] = 0x00;
332 ret = sh_util_valid_utf8(seq);
333 CuAssertIntEquals(tc, ret, S_TRUE);
334
335 seq[0] = 0xf4; seq[1] = 0x8f; seq[2] = 0xbf; seq[3] = 0xbf; seq[4] = 0x00;
336 ret = sh_util_valid_utf8(seq);
337 CuAssertIntEquals(tc, ret, S_TRUE);
338
339 seq[0] = 0xf4; seq[1] = 0x90; seq[2] = 0x80; seq[3] = 0x80; seq[4] = 0x00;
340 ret = sh_util_valid_utf8(seq);
341 CuAssertIntEquals(tc, ret, S_TRUE);
342
343 seq[0] = 0xd7; seq[1] = 0x90; seq[2] = 0xd7; seq[3] = 0x90; seq[4] = 0x00;
344 ret = sh_util_valid_utf8(seq);
345 CuAssertIntEquals(tc, ret, S_TRUE);
346
347 /* cont. char */
348
349 seq[0] = 0x80; seq[1] = 0x00;
350 ret = sh_util_valid_utf8(seq);
351 CuAssertIntEquals(tc, ret, S_FALSE);
352
353 seq[0] = 0xbf; seq[1] = 0x00;
354 ret = sh_util_valid_utf8(seq);
355 CuAssertIntEquals(tc, ret, S_FALSE);
356
357 /* overlong */
358
359 seq[0] = 0xc0; seq[1] = 0xaf; seq[2] = 0x00;
360 ret = sh_util_valid_utf8(seq);
361 CuAssertIntEquals(tc, ret, S_FALSE);
362
363 seq[0] = 0xe0; seq[1] = 0x8f; seq[2] = 0xaf; seq[3] = 0x00;
364 ret = sh_util_valid_utf8(seq);
365 CuAssertIntEquals(tc, ret, S_FALSE);
366
367 seq[0] = 0xf0; seq[1] = 0x80; seq[2] = 0x80; seq[3] = 0xaf; seq[4] = 0x00;
368 ret = sh_util_valid_utf8(seq);
369 CuAssertIntEquals(tc, ret, S_FALSE);
370
371 /* overlong */
372
373 seq[0] = 0xc1; seq[1] = 0xbf; seq[2] = 0x00;
374 ret = sh_util_valid_utf8(seq);
375 CuAssertIntEquals(tc, ret, S_FALSE);
376
377 seq[0] = 0xe0; seq[1] = 0x9f; seq[2] = 0xbf; seq[3] = 0x00;
378 ret = sh_util_valid_utf8(seq);
379 CuAssertIntEquals(tc, ret, S_FALSE);
380
381 seq[0] = 0xf0; seq[1] = 0x8f; seq[2] = 0xbf; seq[3] = 0xbf; seq[4] = 0x00;
382 ret = sh_util_valid_utf8(seq);
383 CuAssertIntEquals(tc, ret, S_FALSE);
384
385 /* overlong */
386
387 seq[0] = 0xc0; seq[1] = 0x80; seq[2] = 0x00;
388 ret = sh_util_valid_utf8(seq);
389 CuAssertIntEquals(tc, ret, S_FALSE);
390
391 seq[0] = 0xe0; seq[1] = 0x80; seq[2] = 0x80; seq[3] = 0x00;
392 ret = sh_util_valid_utf8(seq);
393 CuAssertIntEquals(tc, ret, S_FALSE);
394
395 seq[0] = 0xf0; seq[1] = 0x80; seq[2] = 0x80; seq[3] = 0x80; seq[4] = 0x00;
396 ret = sh_util_valid_utf8(seq);
397 CuAssertIntEquals(tc, ret, S_FALSE);
398
399 /* cont missing */
400
401 seq[0] = 0xd7; seq[1] = 0x20; seq[3] = 0x00;
402 ret = sh_util_valid_utf8(seq);
403 CuAssertIntEquals(tc, ret, S_FALSE);
404
405 seq[0] = 0xee; seq[1] = 0x80; seq[2] = 0x20; seq[3] = 0x00;
406 ret = sh_util_valid_utf8(seq);
407 CuAssertIntEquals(tc, ret, S_FALSE);
408
409 seq[0] = 0xf4; seq[1] = 0x8f; seq[2] = 0xbf; seq[3] = 0x20; seq[4] = 0x00;
410 ret = sh_util_valid_utf8(seq);
411 CuAssertIntEquals(tc, ret, S_FALSE);
412
413 /* switch on utf8 checking for sh_util_obscurename() */
414
415 ret = sh_util_obscure_utf8("Y");
416 CuAssertIntEquals(tc, ret, 0);
417
418 ret = sh_util_obscure_ok ("0x01,0x02,0x03");
419 CuAssertIntEquals(tc, ret, 0);
420
421 ret = sh_util_valid_utf8 (input);
422 CuAssertIntEquals(tc, ret, S_TRUE);
423 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
424 CuAssertIntEquals(tc, ret, 0);
425
426 input[0] = '\t';
427 ret = sh_util_valid_utf8 (input);
428 CuAssertIntEquals(tc, ret, S_FALSE);
429 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
430 CuAssertIntEquals(tc, ret, -1);
431
432 input[0] = 0x01;
433 ret = sh_util_valid_utf8 (input);
434 CuAssertIntEquals(tc, ret, S_TRUE);
435 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
436 CuAssertIntEquals(tc, ret, 0);
437
438 input[0] = 0x02;
439 ret = sh_util_valid_utf8 (input);
440 CuAssertIntEquals(tc, ret, S_TRUE);
441 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
442 CuAssertIntEquals(tc, ret, 0);
443
444 input[0] = 0x03;
445 ret = sh_util_valid_utf8 (input);
446 CuAssertIntEquals(tc, ret, S_TRUE);
447 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
448 CuAssertIntEquals(tc, ret, 0);
449
450 input[0] = 0x04;
451 ret = sh_util_valid_utf8 (input);
452 CuAssertIntEquals(tc, ret, S_FALSE);
453 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
454 CuAssertIntEquals(tc, ret, -1);
455
456 input[0] = 'f';
457 ret = sh_util_valid_utf8 (input);
458 CuAssertIntEquals(tc, ret, S_TRUE);
459 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
460 CuAssertIntEquals(tc, ret, 0);
461
462 input[5] = ' ';
463 ret = sh_util_valid_utf8 (input);
464 CuAssertIntEquals(tc, ret, S_FALSE);
465 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
466 CuAssertIntEquals(tc, ret, -1);
467
468 input[5] = 'r'; input[3] = ' ';
469 ret = sh_util_valid_utf8 (input);
470 CuAssertIntEquals(tc, ret, S_TRUE);
471 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
472 CuAssertIntEquals(tc, ret, 0);
473
474
475#else
476 CuAssertIntEquals(tc, ret, 0);
477#endif
478}
479
480void Test_sh_util_obscure_ok (CuTest *tc) {
481
482 int ret = 0;
483#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
484 char input[16] = "foobar";
485
486 /* switch off utf8 checking for sh_util_obscurename() */
487
488 ret = sh_util_obscure_utf8("N");
489 CuAssertIntEquals(tc, ret, 0);
490
491 ret = sh_util_obscure_ok ("0xA1,0xA2,0xA3");
492 CuAssertIntEquals(tc, ret, 0);
493
494 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
495 CuAssertIntEquals(tc, ret, 0);
496
497 input[0] = '\t';
498 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
499 CuAssertIntEquals(tc, ret, -1);
500
501 input[0] = 0xA1;
502 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
503 CuAssertIntEquals(tc, ret, 0);
504
505 input[0] = 0xA2;
506 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
507 CuAssertIntEquals(tc, ret, 0);
508
509 input[0] = 0xA3;
510 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
511 CuAssertIntEquals(tc, ret, 0);
512
513 input[0] = 0xA4;
514 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
515 CuAssertIntEquals(tc, ret, -1);
516
517 input[0] = 'f';
518 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
519 CuAssertIntEquals(tc, ret, 0);
520
521 input[5] = ' ';
522 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
523 CuAssertIntEquals(tc, ret, -1);
524
525 input[5] = 'r'; input[3] = ' ';
526 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
527 CuAssertIntEquals(tc, ret, 0);
528#else
529 CuAssertIntEquals(tc, ret, 0);
530#endif
531}
532
533
Note: See TracBrowser for help on using the repository browser.