source: tests/check_util.c @ fca4683

Last change on this file since fca4683 was fca4683, checked in by dequis <dx@…>, at 2016-11-12T03:38:34Z

word_wrap: truncate utf8 safely

  • Property mode set to 100644
File size: 5.9 KB
Line 
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
11START_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}
25END_TEST
26
27START_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}
43END_TEST
44
45START_TEST(test_set_url_http)
46url_t url;
47
48fail_if(0 == url_set(&url, "http://host/"));
49fail_unless(!strcmp(url.host, "host"));
50fail_unless(!strcmp(url.file, "/"));
51fail_unless(!strcmp(url.user, ""));
52fail_unless(!strcmp(url.pass, ""));
53fail_unless(url.proto == PROTO_HTTP);
54fail_unless(url.port == 80);
55END_TEST
56
57START_TEST(test_set_url_https)
58url_t url;
59
60fail_if(0 == url_set(&url, "https://ahost/AimeeMann"));
61fail_unless(!strcmp(url.host, "ahost"));
62fail_unless(!strcmp(url.file, "/AimeeMann"));
63fail_unless(!strcmp(url.user, ""));
64fail_unless(!strcmp(url.pass, ""));
65fail_unless(url.proto == PROTO_HTTPS);
66fail_unless(url.port == 443);
67END_TEST
68
69START_TEST(test_set_url_port)
70url_t url;
71
72fail_if(0 == url_set(&url, "https://ahost:200/Lost/In/Space"));
73fail_unless(!strcmp(url.host, "ahost"));
74fail_unless(!strcmp(url.file, "/Lost/In/Space"));
75fail_unless(!strcmp(url.user, ""));
76fail_unless(!strcmp(url.pass, ""));
77fail_unless(url.proto == PROTO_HTTPS);
78fail_unless(url.port == 200);
79END_TEST
80
81START_TEST(test_set_url_username)
82url_t url;
83
84fail_if(0 == url_set(&url, "socks4://user@ahost/Space"));
85fail_unless(!strcmp(url.host, "ahost"));
86fail_unless(!strcmp(url.file, "/Space"));
87fail_unless(!strcmp(url.user, "user"));
88fail_unless(!strcmp(url.pass, ""));
89fail_unless(url.proto == PROTO_SOCKS4);
90fail_unless(url.port == 1080);
91END_TEST
92
93START_TEST(test_set_url_username_pwd)
94url_t url;
95
96fail_if(0 == url_set(&url, "socks5://user:pass@ahost/"));
97fail_unless(!strcmp(url.host, "ahost"));
98fail_unless(!strcmp(url.file, "/"));
99fail_unless(!strcmp(url.user, "user"));
100fail_unless(!strcmp(url.pass, "pass"));
101fail_unless(url.proto == PROTO_SOCKS5);
102fail_unless(url.port == 1080);
103END_TEST
104
105struct {
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
150START_TEST(test_word_wrap)
151int i;
152
153for (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}
163END_TEST
164
165START_TEST(test_http_encode)
166char s[80];
167
168strcpy(s, "ee\xc3" "\xab" "ee!!...");
169http_encode(s);
170fail_unless(strcmp(s, "ee%C3%ABee%21%21...") == 0);
171END_TEST
172
173struct {
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
200START_TEST(test_split_command_parts)
201int i;
202for (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}
215END_TEST
216
217Suite *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}
Note: See TracBrowser for help on using the repository browser.