[c2fa827] | 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" |
---|
[0b9daac] | 8 | #include "lib/misc.h" |
---|
| 9 | #include "lib/url.h" |
---|
[c2fa827] | 10 | |
---|
[5ebff60] | 11 | START_TEST(test_strip_linefeed){ |
---|
[c2fa827] | 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); |
---|
[5ebff60] | 20 | fail_unless(strcmp(copy, expected[i]) == 0, |
---|
| 21 | "(%d) strip_linefeed broken: %s -> %s (expected: %s)", |
---|
| 22 | i, get[i], copy, expected[i]); |
---|
[c2fa827] | 23 | } |
---|
| 24 | } |
---|
| 25 | END_TEST |
---|
| 26 | |
---|
[1fc2958] | 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); |
---|
[5ebff60] | 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"); |
---|
[1fc2958] | 41 | } |
---|
| 42 | } |
---|
| 43 | END_TEST |
---|
| 44 | |
---|
[7bee5af] | 45 | START_TEST(test_set_url_http) |
---|
[5ebff60] | 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); |
---|
[7bee5af] | 55 | END_TEST |
---|
| 56 | |
---|
| 57 | START_TEST(test_set_url_https) |
---|
[5ebff60] | 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); |
---|
[7bee5af] | 67 | END_TEST |
---|
| 68 | |
---|
| 69 | START_TEST(test_set_url_port) |
---|
[5ebff60] | 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); |
---|
[7bee5af] | 79 | END_TEST |
---|
| 80 | |
---|
| 81 | START_TEST(test_set_url_username) |
---|
[5ebff60] | 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); |
---|
[7bee5af] | 91 | END_TEST |
---|
| 92 | |
---|
| 93 | START_TEST(test_set_url_username_pwd) |
---|
[5ebff60] | 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); |
---|
[7bee5af] | 103 | END_TEST |
---|
| 104 | |
---|
[5ebff60] | 105 | struct { |
---|
[d444c09] | 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 | NULL |
---|
| 142 | } |
---|
| 143 | }; |
---|
| 144 | |
---|
| 145 | START_TEST(test_word_wrap) |
---|
[5ebff60] | 146 | int i; |
---|
| 147 | |
---|
| 148 | for (i = 0; word_wrap_tests[i].orig && *word_wrap_tests[i].orig; i++) { |
---|
| 149 | char *wrapped = word_wrap(word_wrap_tests[i].orig, word_wrap_tests[i].line_len); |
---|
| 150 | |
---|
| 151 | fail_unless(strcmp(word_wrap_tests[i].wrapped, wrapped) == 0, |
---|
| 152 | "%s (line_len = %d) should wrap to `%s', not to `%s'", |
---|
| 153 | word_wrap_tests[i].orig, word_wrap_tests[i].line_len, |
---|
| 154 | word_wrap_tests[i].wrapped, wrapped); |
---|
| 155 | |
---|
| 156 | g_free(wrapped); |
---|
| 157 | } |
---|
[d444c09] | 158 | END_TEST |
---|
| 159 | |
---|
[b40e60d] | 160 | START_TEST(test_http_encode) |
---|
[5ebff60] | 161 | char s[80]; |
---|
| 162 | |
---|
| 163 | strcpy(s, "ee\xc3" "\xab" "ee!!..."); |
---|
| 164 | http_encode(s); |
---|
| 165 | fail_unless(strcmp(s, "ee%C3%ABee%21%21...") == 0); |
---|
[b40e60d] | 166 | END_TEST |
---|
| 167 | |
---|
[269580c] | 168 | struct { |
---|
| 169 | int limit; |
---|
| 170 | char *command; |
---|
[5ebff60] | 171 | char *expected[IRC_MAX_ARGS + 1]; |
---|
[269580c] | 172 | } split_tests[] = { |
---|
| 173 | { |
---|
| 174 | 0, "account add etc \"user name with spaces\" 'pass\\ word'", |
---|
[5ebff60] | 175 | { "account", "add", "etc", "user name with spaces", "pass\\ word", NULL }, |
---|
[269580c] | 176 | }, |
---|
| 177 | { |
---|
| 178 | 0, "channel set group Close\\ friends", |
---|
[5ebff60] | 179 | { "channel", "set", "group", "Close friends", NULL }, |
---|
[269580c] | 180 | }, |
---|
| 181 | { |
---|
| 182 | 2, "reply wilmer \"testing in C is a PITA\", you said.", |
---|
[5ebff60] | 183 | { "reply", "wilmer", "\"testing in C is a PITA\", you said.", NULL }, |
---|
[269580c] | 184 | }, |
---|
| 185 | { |
---|
| 186 | 4, "one space two spaces limit limit", |
---|
[5ebff60] | 187 | { "one", "space", "two", "spaces", "limit limit", NULL }, |
---|
[269580c] | 188 | }, |
---|
| 189 | { |
---|
| 190 | 0, NULL, |
---|
[5ebff60] | 191 | { NULL } |
---|
[269580c] | 192 | }, |
---|
| 193 | }; |
---|
| 194 | |
---|
| 195 | START_TEST(test_split_command_parts) |
---|
[5ebff60] | 196 | int i; |
---|
| 197 | for (i = 0; split_tests[i].command; i++) { |
---|
| 198 | char *cmd = g_strdup(split_tests[i].command); |
---|
| 199 | char **split = split_command_parts(cmd, split_tests[i].limit); |
---|
| 200 | char **expected = split_tests[i].expected; |
---|
| 201 | |
---|
| 202 | int j; |
---|
| 203 | for (j = 0; split[j] && expected[j]; j++) { |
---|
| 204 | fail_unless(strcmp(split[j], expected[j]) == 0, |
---|
| 205 | "(%d) split_command_parts broken: split(\"%s\")[%d] -> %s (expected: %s)", |
---|
| 206 | i, split_tests[i].command, j, split[j], expected[j]); |
---|
[269580c] | 207 | } |
---|
[5ebff60] | 208 | g_free(cmd); |
---|
| 209 | } |
---|
[269580c] | 210 | END_TEST |
---|
| 211 | |
---|
[5ebff60] | 212 | Suite *util_suite(void) |
---|
[c2fa827] | 213 | { |
---|
| 214 | Suite *s = suite_create("Util"); |
---|
| 215 | TCase *tc_core = tcase_create("Core"); |
---|
[5ebff60] | 216 | |
---|
| 217 | suite_add_tcase(s, tc_core); |
---|
| 218 | tcase_add_test(tc_core, test_strip_linefeed); |
---|
| 219 | tcase_add_test(tc_core, test_strip_newlines); |
---|
| 220 | tcase_add_test(tc_core, test_set_url_http); |
---|
| 221 | tcase_add_test(tc_core, test_set_url_https); |
---|
| 222 | tcase_add_test(tc_core, test_set_url_port); |
---|
| 223 | tcase_add_test(tc_core, test_set_url_username); |
---|
| 224 | tcase_add_test(tc_core, test_set_url_username_pwd); |
---|
| 225 | tcase_add_test(tc_core, test_word_wrap); |
---|
| 226 | tcase_add_test(tc_core, test_http_encode); |
---|
| 227 | tcase_add_test(tc_core, test_split_command_parts); |
---|
[c2fa827] | 228 | return s; |
---|
| 229 | } |
---|