source: tests/check_util.c @ 25c4c78

Last change on this file since 25c4c78 was 269580c, checked in by dequis <dx@…>, at 2014-07-24T03:51:07Z

Add limit param to split_command_parts(), fix twitter quotes bug

Only took me a few months to write. I even added a test case.

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