1 | #include <stdlib.h> |
---|
2 | #include <glib.h> |
---|
3 | #include <gmodule.h> |
---|
4 | #include <check.h> |
---|
5 | #include <string.h> |
---|
6 | #include "irc.h" |
---|
7 | #include "set.h" |
---|
8 | #include "misc.h" |
---|
9 | #include "url.h" |
---|
10 | |
---|
11 | START_TEST(test_strip_linefeed) |
---|
12 | { |
---|
13 | int i; |
---|
14 | const char *get[] = { "Test", "Test\r", "Test\rX\r", NULL }; |
---|
15 | const char *expected[] = { "Test", "Test", "TestX", NULL }; |
---|
16 | |
---|
17 | for (i = 0; get[i]; i++) { |
---|
18 | char copy[20]; |
---|
19 | strcpy(copy, get[i]); |
---|
20 | strip_linefeed(copy); |
---|
21 | fail_unless(strcmp(copy, expected[i]) == 0, |
---|
22 | "(%d) strip_linefeed broken: %s -> %s (expected: %s)", |
---|
23 | i, get[i], copy, expected[i]); |
---|
24 | } |
---|
25 | } |
---|
26 | END_TEST |
---|
27 | |
---|
28 | START_TEST(test_strip_newlines) |
---|
29 | { |
---|
30 | int i; |
---|
31 | const char *get[] = { "Test", "Test\r\n", "Test\nX\n", NULL }; |
---|
32 | const char *expected[] = { "Test", "Test ", "Test X ", NULL }; |
---|
33 | |
---|
34 | for (i = 0; get[i]; i++) { |
---|
35 | char copy[20], *ret; |
---|
36 | strcpy(copy, get[i]); |
---|
37 | ret = strip_newlines(copy); |
---|
38 | fail_unless(strcmp(copy, expected[i]) == 0, |
---|
39 | "(%d) strip_newlines broken: %s -> %s (expected: %s)", |
---|
40 | i, get[i], copy, expected[i]); |
---|
41 | fail_unless(copy == ret, "Original string not returned"); |
---|
42 | } |
---|
43 | } |
---|
44 | END_TEST |
---|
45 | |
---|
46 | START_TEST(test_set_url_http) |
---|
47 | { |
---|
48 | url_t url; |
---|
49 | |
---|
50 | fail_if(0 == url_set(&url, "http://host/")); |
---|
51 | fail_unless(!strcmp(url.host, "host")); |
---|
52 | fail_unless(!strcmp(url.file, "/")); |
---|
53 | fail_unless(!strcmp(url.user, "")); |
---|
54 | fail_unless(!strcmp(url.pass, "")); |
---|
55 | fail_unless(url.proto == PROTO_HTTP); |
---|
56 | fail_unless(url.port == 80); |
---|
57 | } |
---|
58 | END_TEST |
---|
59 | |
---|
60 | START_TEST(test_set_url_https) |
---|
61 | { |
---|
62 | url_t url; |
---|
63 | |
---|
64 | fail_if(0 == url_set(&url, "https://ahost/AimeeMann")); |
---|
65 | fail_unless(!strcmp(url.host, "ahost")); |
---|
66 | fail_unless(!strcmp(url.file, "/AimeeMann")); |
---|
67 | fail_unless(!strcmp(url.user, "")); |
---|
68 | fail_unless(!strcmp(url.pass, "")); |
---|
69 | fail_unless(url.proto == PROTO_HTTPS); |
---|
70 | fail_unless(url.port == 443); |
---|
71 | } |
---|
72 | END_TEST |
---|
73 | |
---|
74 | START_TEST(test_set_url_port) |
---|
75 | { |
---|
76 | url_t url; |
---|
77 | |
---|
78 | fail_if(0 == url_set(&url, "https://ahost:200/Lost/In/Space")); |
---|
79 | fail_unless(!strcmp(url.host, "ahost")); |
---|
80 | fail_unless(!strcmp(url.file, "/Lost/In/Space")); |
---|
81 | fail_unless(!strcmp(url.user, "")); |
---|
82 | fail_unless(!strcmp(url.pass, "")); |
---|
83 | fail_unless(url.proto == PROTO_HTTPS); |
---|
84 | fail_unless(url.port == 200); |
---|
85 | } |
---|
86 | END_TEST |
---|
87 | |
---|
88 | START_TEST(test_set_url_username) |
---|
89 | { |
---|
90 | url_t url; |
---|
91 | |
---|
92 | fail_if(0 == url_set(&url, "socks4://user@ahost/Space")); |
---|
93 | fail_unless(!strcmp(url.host, "ahost")); |
---|
94 | fail_unless(!strcmp(url.file, "/Space")); |
---|
95 | fail_unless(!strcmp(url.user, "user")); |
---|
96 | fail_unless(!strcmp(url.pass, "")); |
---|
97 | fail_unless(url.proto == PROTO_SOCKS4); |
---|
98 | fail_unless(url.port == 1080); |
---|
99 | } |
---|
100 | END_TEST |
---|
101 | |
---|
102 | START_TEST(test_set_url_username_pwd) |
---|
103 | { |
---|
104 | url_t url; |
---|
105 | |
---|
106 | fail_if(0 == url_set(&url, "socks5://user:pass@ahost/")); |
---|
107 | fail_unless(!strcmp(url.host, "ahost")); |
---|
108 | fail_unless(!strcmp(url.file, "/")); |
---|
109 | fail_unless(!strcmp(url.user, "user")); |
---|
110 | fail_unless(!strcmp(url.pass, "pass")); |
---|
111 | fail_unless(url.proto == PROTO_SOCKS5); |
---|
112 | fail_unless(url.port == 1080); |
---|
113 | } |
---|
114 | END_TEST |
---|
115 | |
---|
116 | struct { |
---|
117 | char *orig; |
---|
118 | int line_len; |
---|
119 | char *wrapped; |
---|
120 | } word_wrap_tests[] = { |
---|
121 | { |
---|
122 | "Line-wrapping is not as easy as it seems?", |
---|
123 | 16, |
---|
124 | "Line-wrapping is\nnot as easy as\nit seems?" |
---|
125 | }, |
---|
126 | { |
---|
127 | "Line-wrapping is not as easy as it seems?", |
---|
128 | 8, |
---|
129 | "Line-\nwrapping\nis not\nas easy\nas it\nseems?" |
---|
130 | }, |
---|
131 | { |
---|
132 | "Line-wrapping is\nnot as easy as it seems?", |
---|
133 | 8, |
---|
134 | "Line-\nwrapping\nis\nnot as\neasy as\nit\nseems?" |
---|
135 | }, |
---|
136 | { |
---|
137 | "a aa aaa aaaa aaaaa aaaaaa aaaaaaa aaaaaaaa", |
---|
138 | 5, |
---|
139 | "a aa\naaa\naaaa\naaaaa\naaaaa\na\naaaaa\naa\naaaaa\naaa", |
---|
140 | }, |
---|
141 | { |
---|
142 | "aaaaaaaa aaaaaaa aaaaaa aaaaa aaaa aaa aa a", |
---|
143 | 5, |
---|
144 | "aaaaa\naaa\naaaaa\naa\naaaaa\na\naaaaa\naaaa\naaa\naa a", |
---|
145 | }, |
---|
146 | { |
---|
147 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
---|
148 | 5, |
---|
149 | "aaaaa\naaaaa\naaaaa\naaaaa\naaaaa\naaaaa\naaaaa\na", |
---|
150 | }, |
---|
151 | { |
---|
152 | "áááááááááá", |
---|
153 | 11, |
---|
154 | "ááááá\nááááá", |
---|
155 | }, |
---|
156 | { |
---|
157 | NULL |
---|
158 | } |
---|
159 | }; |
---|
160 | |
---|
161 | START_TEST(test_word_wrap) |
---|
162 | { |
---|
163 | int i; |
---|
164 | |
---|
165 | for (i = 0; word_wrap_tests[i].orig && *word_wrap_tests[i].orig; i++) { |
---|
166 | char *wrapped = word_wrap(word_wrap_tests[i].orig, word_wrap_tests[i].line_len); |
---|
167 | |
---|
168 | fail_unless(strcmp(word_wrap_tests[i].wrapped, wrapped) == 0, |
---|
169 | "%s (line_len = %d) should wrap to `%s', not to `%s'", |
---|
170 | word_wrap_tests[i].orig, word_wrap_tests[i].line_len, |
---|
171 | word_wrap_tests[i].wrapped, wrapped); |
---|
172 | |
---|
173 | g_free(wrapped); |
---|
174 | } |
---|
175 | } |
---|
176 | END_TEST |
---|
177 | |
---|
178 | START_TEST(test_http_encode) |
---|
179 | { |
---|
180 | char s[80]; |
---|
181 | |
---|
182 | strcpy(s, "ee\xc3" "\xab" "ee!!..."); |
---|
183 | http_encode(s); |
---|
184 | fail_unless(strcmp(s, "ee%C3%ABee%21%21...") == 0); |
---|
185 | } |
---|
186 | END_TEST |
---|
187 | |
---|
188 | struct { |
---|
189 | int limit; |
---|
190 | char *command; |
---|
191 | char *expected[IRC_MAX_ARGS + 1]; |
---|
192 | } split_tests[] = { |
---|
193 | { |
---|
194 | 0, "account add etc \"user name with spaces\" 'pass\\ word'", |
---|
195 | { "account", "add", "etc", "user name with spaces", "pass\\ word", NULL }, |
---|
196 | }, |
---|
197 | { |
---|
198 | 0, "channel set group Close\\ friends", |
---|
199 | { "channel", "set", "group", "Close friends", NULL }, |
---|
200 | }, |
---|
201 | { |
---|
202 | 2, "reply wilmer \"testing in C is a PITA\", you said.", |
---|
203 | { "reply", "wilmer", "\"testing in C is a PITA\", you said.", NULL }, |
---|
204 | }, |
---|
205 | { |
---|
206 | 4, "one space two spaces limit limit", |
---|
207 | { "one", "space", "two", "spaces", "limit limit", NULL }, |
---|
208 | }, |
---|
209 | { |
---|
210 | 0, NULL, |
---|
211 | { NULL } |
---|
212 | }, |
---|
213 | }; |
---|
214 | |
---|
215 | START_TEST(test_split_command_parts) |
---|
216 | { |
---|
217 | int i; |
---|
218 | for (i = 0; split_tests[i].command; i++) { |
---|
219 | char *cmd = g_strdup(split_tests[i].command); |
---|
220 | char **split = split_command_parts(cmd, split_tests[i].limit); |
---|
221 | char **expected = split_tests[i].expected; |
---|
222 | |
---|
223 | int j; |
---|
224 | for (j = 0; split[j] && expected[j]; j++) { |
---|
225 | fail_unless(strcmp(split[j], expected[j]) == 0, |
---|
226 | "(%d) split_command_parts broken: split(\"%s\")[%d] -> %s (expected: %s)", |
---|
227 | i, split_tests[i].command, j, split[j], expected[j]); |
---|
228 | } |
---|
229 | g_free(cmd); |
---|
230 | } |
---|
231 | } |
---|
232 | END_TEST |
---|
233 | |
---|
234 | Suite *util_suite(void) |
---|
235 | { |
---|
236 | Suite *s = suite_create("Util"); |
---|
237 | TCase *tc_core = tcase_create("Core"); |
---|
238 | |
---|
239 | suite_add_tcase(s, tc_core); |
---|
240 | tcase_add_test(tc_core, test_strip_linefeed); |
---|
241 | tcase_add_test(tc_core, test_strip_newlines); |
---|
242 | tcase_add_test(tc_core, test_set_url_http); |
---|
243 | tcase_add_test(tc_core, test_set_url_https); |
---|
244 | tcase_add_test(tc_core, test_set_url_port); |
---|
245 | tcase_add_test(tc_core, test_set_url_username); |
---|
246 | tcase_add_test(tc_core, test_set_url_username_pwd); |
---|
247 | tcase_add_test(tc_core, test_word_wrap); |
---|
248 | tcase_add_test(tc_core, test_http_encode); |
---|
249 | tcase_add_test(tc_core, test_split_command_parts); |
---|
250 | return s; |
---|
251 | } |
---|