source: tests/check_util.c @ 2fe5eb9

Last change on this file since 2fe5eb9 was b40e60d, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-07-29T08:57:01Z

Fixing http_encode(): BitlBee now calls setlocale() (for nickname
transliteration to work), which changes the behaviour of isalpha() (turns
out it's not a simple macro). For HTTP-encoding, this sucks, especially
when doing OAuth (which is very picky about the way HTTP encoding is done).
This should fix problems some people were seeing with posting Twitter
messages containing accents.

  • Property mode set to 100644
File size: 4.8 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
171Suite *util_suite (void)
172{
173        Suite *s = suite_create("Util");
174        TCase *tc_core = tcase_create("Core");
175        suite_add_tcase (s, tc_core);
176        tcase_add_test (tc_core, test_strip_linefeed);
177        tcase_add_test (tc_core, test_strip_newlines);
178        tcase_add_test (tc_core, test_set_url_http);
179        tcase_add_test (tc_core, test_set_url_https);
180        tcase_add_test (tc_core, test_set_url_port);
181        tcase_add_test (tc_core, test_set_url_username);
182        tcase_add_test (tc_core, test_set_url_username_pwd);
183        tcase_add_test (tc_core, test_word_wrap);
184        tcase_add_test (tc_core, test_http_encode);
185        return s;
186}
Note: See TracBrowser for help on using the repository browser.