source: trunk/src/cutest_sh_utils.c@ 84

Last change on this file since 84 was 76, checked in by rainer, 18 years ago

Fix for ticket #38 (csv escaping) and #39 (building on cygwin). Also optimize a bit.

File size: 11.5 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_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
145 ret = sh_util_dirname(input0);
146 CuAssertPtrNotNull(tc, ret);
147 CuAssertStrEquals(tc, res0, ret);
148
149 ret = sh_util_dirname(input1);
150 CuAssertPtrNotNull(tc, ret);
151 CuAssertStrEquals(tc, res1, ret);
152
153 ret = sh_util_dirname(input2);
154 CuAssertPtrNotNull(tc, ret);
155 CuAssertStrEquals(tc, res2, ret);
156
157 ret = sh_util_dirname(input3);
158 CuAssertPtrNotNull(tc, ret);
159 CuAssertStrEquals(tc, res3, ret);
160 return;
161}
162
163void Test_sh_util_basename_ok (CuTest *tc) {
164 char * ret = 0;
165
166 char input0[] = "/foo/bar";
167 char res0[] = "bar";
168
169 char input1[] = "/foo/";
170 char res1[] = "foo";
171
172 char input2[] = "/foo";
173 char res2[] = "foo";
174
175 char input3[] = "/";
176 char res3[] = "/";
177
178 char input4[] = "/foo/bar/";
179 char res4[] = "bar";
180
181 ret = sh_util_basename(input0);
182 CuAssertPtrNotNull(tc, ret);
183 CuAssertStrEquals(tc, res0, ret);
184
185 ret = sh_util_basename(input1);
186 CuAssertPtrNotNull(tc, ret);
187 CuAssertStrEquals(tc, res1, ret);
188
189 ret = sh_util_basename(input2);
190 CuAssertPtrNotNull(tc, ret);
191 CuAssertStrEquals(tc, res2, ret);
192
193 ret = sh_util_basename(input3);
194 CuAssertPtrNotNull(tc, ret);
195 CuAssertStrEquals(tc, res3, ret);
196
197 ret = sh_util_basename(input4);
198 CuAssertPtrNotNull(tc, ret);
199 CuAssertStrEquals(tc, res4, ret);
200
201 return;
202}
203
204void Test_sh_util_utf8_ok (CuTest *tc) {
205 int ret = 0;
206#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
207 unsigned char seq[16];
208 unsigned char input[16] = "foobar";
209
210 seq[0] = 0x00;
211 ret = sh_util_valid_utf8(seq);
212 CuAssertIntEquals(tc, ret, S_TRUE);
213
214 seq[0] = 0xd7; seq[1] = 0x90; seq[2] = 0x00;
215 ret = sh_util_valid_utf8(seq);
216 CuAssertIntEquals(tc, ret, S_TRUE);
217
218 seq[0] = 0xed; seq[1] = 0x9f; seq[2] = 0xbf; seq[3] = 0x00;
219 ret = sh_util_valid_utf8(seq);
220 CuAssertIntEquals(tc, ret, S_TRUE);
221
222 seq[0] = 0xee; seq[1] = 0x80; seq[2] = 0x80; seq[3] = 0x00;
223 ret = sh_util_valid_utf8(seq);
224 CuAssertIntEquals(tc, ret, S_TRUE);
225
226 seq[0] = 0xef; seq[1] = 0xbf; seq[2] = 0xbd; seq[3] = 0x00;
227 ret = sh_util_valid_utf8(seq);
228 CuAssertIntEquals(tc, ret, S_TRUE);
229
230 seq[0] = 0xf4; seq[1] = 0x8f; seq[2] = 0xbf; seq[3] = 0xbf; seq[4] = 0x00;
231 ret = sh_util_valid_utf8(seq);
232 CuAssertIntEquals(tc, ret, S_TRUE);
233
234 seq[0] = 0xf4; seq[1] = 0x90; seq[2] = 0x80; seq[3] = 0x80; seq[4] = 0x00;
235 ret = sh_util_valid_utf8(seq);
236 CuAssertIntEquals(tc, ret, S_TRUE);
237
238 seq[0] = 0xd7; seq[1] = 0x90; seq[2] = 0xd7; seq[3] = 0x90; seq[4] = 0x00;
239 ret = sh_util_valid_utf8(seq);
240 CuAssertIntEquals(tc, ret, S_TRUE);
241
242 /* cont. char */
243
244 seq[0] = 0x80; seq[1] = 0x00;
245 ret = sh_util_valid_utf8(seq);
246 CuAssertIntEquals(tc, ret, S_FALSE);
247
248 seq[0] = 0xbf; seq[1] = 0x00;
249 ret = sh_util_valid_utf8(seq);
250 CuAssertIntEquals(tc, ret, S_FALSE);
251
252 /* overlong */
253
254 seq[0] = 0xc0; seq[1] = 0xaf; seq[2] = 0x00;
255 ret = sh_util_valid_utf8(seq);
256 CuAssertIntEquals(tc, ret, S_FALSE);
257
258 seq[0] = 0xe0; seq[1] = 0x8f; seq[2] = 0xaf; seq[3] = 0x00;
259 ret = sh_util_valid_utf8(seq);
260 CuAssertIntEquals(tc, ret, S_FALSE);
261
262 seq[0] = 0xf0; seq[1] = 0x80; seq[2] = 0x80; seq[3] = 0xaf; seq[4] = 0x00;
263 ret = sh_util_valid_utf8(seq);
264 CuAssertIntEquals(tc, ret, S_FALSE);
265
266 /* overlong */
267
268 seq[0] = 0xc1; seq[1] = 0xbf; seq[2] = 0x00;
269 ret = sh_util_valid_utf8(seq);
270 CuAssertIntEquals(tc, ret, S_FALSE);
271
272 seq[0] = 0xe0; seq[1] = 0x9f; seq[2] = 0xbf; seq[3] = 0x00;
273 ret = sh_util_valid_utf8(seq);
274 CuAssertIntEquals(tc, ret, S_FALSE);
275
276 seq[0] = 0xf0; seq[1] = 0x8f; seq[2] = 0xbf; seq[3] = 0xbf; seq[4] = 0x00;
277 ret = sh_util_valid_utf8(seq);
278 CuAssertIntEquals(tc, ret, S_FALSE);
279
280 /* overlong */
281
282 seq[0] = 0xc0; seq[1] = 0x80; seq[2] = 0x00;
283 ret = sh_util_valid_utf8(seq);
284 CuAssertIntEquals(tc, ret, S_FALSE);
285
286 seq[0] = 0xe0; seq[1] = 0x80; seq[2] = 0x80; seq[3] = 0x00;
287 ret = sh_util_valid_utf8(seq);
288 CuAssertIntEquals(tc, ret, S_FALSE);
289
290 seq[0] = 0xf0; seq[1] = 0x80; seq[2] = 0x80; seq[3] = 0x80; seq[4] = 0x00;
291 ret = sh_util_valid_utf8(seq);
292 CuAssertIntEquals(tc, ret, S_FALSE);
293
294 /* cont missing */
295
296 seq[0] = 0xd7; seq[1] = 0x20; seq[3] = 0x00;
297 ret = sh_util_valid_utf8(seq);
298 CuAssertIntEquals(tc, ret, S_FALSE);
299
300 seq[0] = 0xee; seq[1] = 0x80; seq[2] = 0x20; seq[3] = 0x00;
301 ret = sh_util_valid_utf8(seq);
302 CuAssertIntEquals(tc, ret, S_FALSE);
303
304 seq[0] = 0xf4; seq[1] = 0x8f; seq[2] = 0xbf; seq[3] = 0x20; seq[4] = 0x00;
305 ret = sh_util_valid_utf8(seq);
306 CuAssertIntEquals(tc, ret, S_FALSE);
307
308 /* switch on utf8 checking for sh_util_obscurename() */
309
310 ret = sh_util_obscure_utf8("Y");
311 CuAssertIntEquals(tc, ret, 0);
312
313 ret = sh_util_obscure_ok ("0x01,0x02,0x03");
314 CuAssertIntEquals(tc, ret, 0);
315
316 ret = sh_util_valid_utf8 (input);
317 CuAssertIntEquals(tc, ret, S_TRUE);
318 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
319 CuAssertIntEquals(tc, ret, 0);
320
321 input[0] = '\t';
322 ret = sh_util_valid_utf8 (input);
323 CuAssertIntEquals(tc, ret, S_FALSE);
324 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
325 CuAssertIntEquals(tc, ret, -1);
326
327 input[0] = 0x01;
328 ret = sh_util_valid_utf8 (input);
329 CuAssertIntEquals(tc, ret, S_TRUE);
330 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
331 CuAssertIntEquals(tc, ret, 0);
332
333 input[0] = 0x02;
334 ret = sh_util_valid_utf8 (input);
335 CuAssertIntEquals(tc, ret, S_TRUE);
336 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
337 CuAssertIntEquals(tc, ret, 0);
338
339 input[0] = 0x03;
340 ret = sh_util_valid_utf8 (input);
341 CuAssertIntEquals(tc, ret, S_TRUE);
342 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
343 CuAssertIntEquals(tc, ret, 0);
344
345 input[0] = 0x04;
346 ret = sh_util_valid_utf8 (input);
347 CuAssertIntEquals(tc, ret, S_FALSE);
348 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
349 CuAssertIntEquals(tc, ret, -1);
350
351 input[0] = 'f';
352 ret = sh_util_valid_utf8 (input);
353 CuAssertIntEquals(tc, ret, S_TRUE);
354 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
355 CuAssertIntEquals(tc, ret, 0);
356
357 input[5] = ' ';
358 ret = sh_util_valid_utf8 (input);
359 CuAssertIntEquals(tc, ret, S_FALSE);
360 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
361 CuAssertIntEquals(tc, ret, -1);
362
363 input[5] = 'r'; input[3] = ' ';
364 ret = sh_util_valid_utf8 (input);
365 CuAssertIntEquals(tc, ret, S_TRUE);
366 ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
367 CuAssertIntEquals(tc, ret, 0);
368
369
370#else
371 CuAssertIntEquals(tc, ret, 0);
372#endif
373}
374
375void Test_sh_util_obscure_ok (CuTest *tc) {
376
377 int ret = 0;
378#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
379 char input[16] = "foobar";
380
381 /* switch off utf8 checking for sh_util_obscurename() */
382
383 ret = sh_util_obscure_utf8("N");
384 CuAssertIntEquals(tc, ret, 0);
385
386 ret = sh_util_obscure_ok ("0xA1,0xA2,0xA3");
387 CuAssertIntEquals(tc, ret, 0);
388
389 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
390 CuAssertIntEquals(tc, ret, 0);
391
392 input[0] = '\t';
393 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
394 CuAssertIntEquals(tc, ret, -1);
395
396 input[0] = 0xA1;
397 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
398 CuAssertIntEquals(tc, ret, 0);
399
400 input[0] = 0xA2;
401 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
402 CuAssertIntEquals(tc, ret, 0);
403
404 input[0] = 0xA3;
405 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
406 CuAssertIntEquals(tc, ret, 0);
407
408 input[0] = 0xA4;
409 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
410 CuAssertIntEquals(tc, ret, -1);
411
412 input[0] = 'f';
413 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
414 CuAssertIntEquals(tc, ret, 0);
415
416 input[5] = ' ';
417 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
418 CuAssertIntEquals(tc, ret, -1);
419
420 input[5] = 'r'; input[3] = ' ';
421 ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
422 CuAssertIntEquals(tc, ret, 0);
423#else
424 CuAssertIntEquals(tc, ret, 0);
425#endif
426}
427
428
Note: See TracBrowser for help on using the repository browser.