Debian Codegraph @

tests/test_util.c

raw symbols SCIP
1 #include "../src/config.h"
2 #include "../src/util.h"
3
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unity.h>
7
8 void setUp(void)
9 {
10 Config_init();
11 }
12
13 void tearDown(void)
14 {
15 // clean stuff up here
16 }
17
18 void test_path_append(void)
19 {
20 char *result;
21
22 // Case 1: No trailing slash in path, no leading slash in filename
23 result = path_append("/home/user", "docs");
24 TEST_ASSERT_NOT_NULL(result);
25 TEST_ASSERT_EQUAL_STRING("/home/user/docs", result);
26 FREE(result);
27
28 // Case 2: Trailing slash in path
29 result = path_append("/home/user/", "docs");
30 TEST_ASSERT_NOT_NULL(result);
31 TEST_ASSERT_EQUAL_STRING("/home/user/docs", result);
32 FREE(result);
33
34 // Case 3: Leading slash in filename
35 result = path_append("/home/user", "/docs");
36 TEST_ASSERT_NOT_NULL(result);
37 TEST_ASSERT_EQUAL_STRING("/home/user/docs", result);
38 FREE(result);
39
40 // Case 4: Both have slash
41 result = path_append("/home/user/", "/docs");
42 // The implementation now handles redundant slashes and normalizes them.
43 // So it will be /home/user/docs.
44 TEST_ASSERT_NOT_NULL(result);
45 TEST_ASSERT_EQUAL_STRING("/home/user/docs", result);
46 FREE(result);
47
48 // Case 4b: Multiple leading slashes in filename, trailing slash in path
49 result = path_append("/home/user/", "//docs");
50 TEST_ASSERT_NOT_NULL(result);
51 TEST_ASSERT_EQUAL_STRING("/home/user/docs", result);
52 FREE(result);
53
54 // Case 4c: Multiple leading slashes in filename, no trailing slash in path
55 result = path_append("/home/user", "//docs");
56 TEST_ASSERT_NOT_NULL(result);
57 TEST_ASSERT_EQUAL_STRING("/home/user/docs", result);
58 FREE(result);
59 }
60
61 void test_generate_md5sum(void)
62 {
63 char *hash1 = generate_md5sum("httpdirfs");
64 TEST_ASSERT_NOT_NULL(hash1);
65 TEST_ASSERT_EQUAL_STRING("210a6f1303020274c9fa9c51a5538778", hash1);
66 FREE(hash1);
67
68 char *hash2 = generate_md5sum("");
69 TEST_ASSERT_NOT_NULL(hash2);
70 TEST_ASSERT_EQUAL_STRING("d41d8cd98f00b204e9800998ecf8427e", hash2);
71 FREE(hash2);
72 }
73
74 void test_str_to_hex(void)
75 {
76 char *hex1 = str_to_hex("A!");
77 TEST_ASSERT_NOT_NULL(hex1);
78 TEST_ASSERT_EQUAL_STRING("4121", hex1);
79 FREE(hex1);
80
81 char *hex2 = str_to_hex("");
82 TEST_ASSERT_NOT_NULL(hex2);
83 TEST_ASSERT_EQUAL_STRING("", hex2);
84 FREE(hex2);
85 }
86
87 void test_generate_salt(void)
88 {
89 char *salt1 = generate_salt();
90 char *salt2 = generate_salt();
91 TEST_ASSERT_NOT_NULL(salt1);
92 TEST_ASSERT_NOT_NULL(salt2);
93 TEST_ASSERT_EQUAL_INT(36, strlen(salt1));
94 TEST_ASSERT_EQUAL_INT(36, strlen(salt2));
95 // Test randomness
96 TEST_ASSERT_NOT_EQUAL(0, strcmp(salt1, salt2));
97 FREE(salt1);
98 FREE(salt2);
99 }
100
101 void test_realloc_size_zero(void)
102 {
103 char *ptr = CALLOC(10, sizeof(char));
104 TEST_ASSERT_NOT_NULL(ptr);
105
106 ptr = REALLOC(ptr, 0);
107 TEST_ASSERT_NULL(ptr);
108 }
109
110 void test_memory_tracking(void)
111 {
112 // Test CALLOC wrapper
113 int *arr = CALLOC(10, sizeof(int));
114 TEST_ASSERT_NOT_NULL(arr);
115 for (int i = 0; i < 10; i++) {
116 TEST_ASSERT_EQUAL_INT(0, arr[i]);
117 arr[i] = i;
118 }
119
120 // Test REALLOC with size expansion
121 // Allocate a large filler block to consume adjacent heap space
122 int *filler = CALLOC(100000, sizeof(int));
123 TEST_ASSERT_NOT_NULL(filler);
124
125 arr = REALLOC(arr, 200000 * sizeof(int));
126 TEST_ASSERT_NOT_NULL(arr);
127 // Note: relocation may or may not occur; both are valid REALLOC behavior
128
129 for (int i = 0; i < 10; i++) {
130 TEST_ASSERT_EQUAL_INT(i, arr[i]);
131 }
132
133 // Test STRDUP / STRNDUP
134 char *s = STRDUP("httpdirfs_test");
135 TEST_ASSERT_NOT_NULL(s);
136 TEST_ASSERT_EQUAL_STRING("httpdirfs_test", s);
137
138 char *s2 = STRNDUP(s, 9);
139 TEST_ASSERT_NOT_NULL(s2);
140 TEST_ASSERT_EQUAL_STRING("httpdirfs", s2);
141
142 // Test FREE wrapper
143 FREE(filler);
144 TEST_ASSERT_NULL(filler);
145
146 FREE(arr);
147 TEST_ASSERT_NULL(arr);
148
149 FREE(s);
150 TEST_ASSERT_NULL(s);
151
152 FREE(s2);
153 TEST_ASSERT_NULL(s2);
154 }
155
156 int main(void)
157 {
158 UNITY_BEGIN();
159 RUN_TEST(test_path_append);
160 RUN_TEST(test_generate_md5sum);
161 RUN_TEST(test_str_to_hex);
162 RUN_TEST(test_generate_salt);
163 RUN_TEST(test_realloc_size_zero);
164 RUN_TEST(test_memory_tracking);
165 return UNITY_END();
166 }