Debian Codegraph @

tests/check_util.c

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