source: trunk/src/cutest_slib.c@ 437

Last change on this file since 437 was 248, checked in by katerina, 15 years ago

Code to track down originating site for ticket #163.

File size: 2.5 KB
Line 
1
2#include "config_xor.h"
3
4#include <string.h>
5#include "CuTest.h"
6#include "samhain.h"
7
8void Test_sl_stale (CuTest *tc) {
9
10 extern int get_the_fd (SL_TICKET ticket);
11
12 int fd1, fd2, ret, line, val;
13 SL_TICKET tfd1, tfd2;
14 char * err1;
15 char err2[128];
16
17 line = __LINE__; tfd1 = sl_open_read(__FILE__, __LINE__, "/etc/group", SL_NOPRIV);
18 CuAssertTrue(tc, tfd1 > 0);
19
20 fd1 = get_the_fd(tfd1);
21 CuAssertTrue(tc, fd1 >= 0);
22
23 ret = close(fd1);
24 CuAssertTrue(tc, ret == 0);
25
26 tfd2 = sl_open_read(__FILE__, __LINE__, "/etc/group", SL_NOPRIV);
27 CuAssertTrue(tc, tfd2 > 0);
28 CuAssertTrue(tc, tfd2 != tfd1);
29
30 fd2 = get_the_fd(tfd2);
31 CuAssertIntEquals(tc, fd1, fd2);
32
33 err1 = sl_check_stale();
34 CuAssertTrue(tc, err1 != NULL);
35
36 sl_snprintf(err2, sizeof(err2),
37 "stale handle, %s, %d", __FILE__, line);
38 val = strcmp(err1, err2);
39 CuAssertIntEquals(tc, 0, val);
40}
41
42void Test_sl_snprintf (CuTest *tc) {
43
44 int ret = 0;
45 char input[16];
46
47 memset (&input, 'X', 16);
48 ret = sl_snprintf(input, 10, "%s\n", "01234567890123456789");
49 CuAssertIntEquals(tc, ret, 0);
50 CuAssertTrue(tc, input[9] == '\0');
51 CuAssertTrue(tc, input[10] == 'X');
52
53 memset (&input, 'X', 16);
54 ret = sl_snprintf(input, 4, "%d\n", "012345");
55 CuAssertIntEquals(tc, ret, 0);
56 CuAssertTrue(tc, input[3] == '\0');
57 CuAssertTrue(tc, input[4] == 'X');
58}
59
60void Test_sl_strcasecmp (CuTest *tc) {
61 char one[64], two[64];
62 int res;
63
64 strcpy(one, "foo");
65 strcpy(two, "foo");
66 res = sl_strcasecmp(one, two);
67 CuAssertIntEquals(tc, 0, res);
68
69 strcpy(one, "fo");
70 strcpy(two, "foo");
71 res = sl_strcasecmp(one, two);
72 CuAssertIntEquals(tc, -1, res);
73
74 strcpy(one, "foo");
75 strcpy(two, "fo");
76 res = sl_strcasecmp(one, two);
77 CuAssertIntEquals(tc, 1, res);
78
79 strcpy(one, "1234");
80 strcpy(two, "2345");
81 res = sl_strcasecmp(one, two);
82 CuAssertIntEquals(tc, -1, res);
83
84 strcpy(one, "234");
85 strcpy(two, "123");
86 res = sl_strcasecmp(one, two);
87 CuAssertIntEquals(tc, 1, res);
88
89 strcpy(one, "");
90 strcpy(two, "123");
91 res = sl_strcasecmp(one, two);
92 CuAssertIntEquals(tc, -1, res);
93
94 strcpy(one, "234");
95 strcpy(two, "");
96 res = sl_strcasecmp(one, two);
97 CuAssertIntEquals(tc, 1, res);
98
99 strcpy(one, "");
100 strcpy(two, "");
101 res = sl_strcasecmp(one, two);
102 CuAssertTrue(tc, res == 0);
103
104#ifndef SL_FAIL_ON_ERROR
105 res = sl_strcasecmp(NULL, two);
106 CuAssertIntEquals(tc, -1, res);
107
108 res = sl_strcasecmp(one, NULL);
109 CuAssertIntEquals(tc, 1, res);
110
111 res = sl_strcasecmp(NULL, NULL);
112 CuAssertTrue(tc, res != 0);
113#endif
114}
Note: See TracBrowser for help on using the repository browser.