1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 2012 Igalia S.L.
4 */
5
6 #include "test-utils.h"
7
8 /* From http://publicsuffix.org/list/test.txt */
9 static struct {
10 const char *hostname;
11 const char *result;
12 SoupTLDError error;
13 } tld_tests[] = {
14 /* NULL input. Not checked here because the API requires a valid hostname. */
15 /* { NULL, NULL, -1 }, */
16 /* Mixed case. Not checked because the API requires a valid hostname. */
17 /* { "COM", NULL, -1 }, */
18 /* { "example.COM", "example.com", -1 }, */
19 /* { "WwW.example.COM", "example.com", -1 }, */
20 /* Leading dot. */
21 { ".com", NULL, SOUP_TLD_ERROR_INVALID_HOSTNAME },
22 { ".example", NULL, SOUP_TLD_ERROR_INVALID_HOSTNAME },
23 { ".example.com", NULL, SOUP_TLD_ERROR_INVALID_HOSTNAME },
24 { ".example.example", NULL, SOUP_TLD_ERROR_INVALID_HOSTNAME },
25 /* Trailing dot. */
26 { ".com.", NULL, SOUP_TLD_ERROR_INVALID_HOSTNAME },
27 { "domain.biz.", "domain.biz.", -1 },
28 /* TLD with only 1 rule. */
29 { "biz", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
30 { "domain.biz", "domain.biz", -1 },
31 { "b.domain.biz", "domain.biz", -1 },
32 { "a.b.domain.biz", "domain.biz", -1 },
33 /* TLD with some 2-level rules. */
34 { "com", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
35 { "example.com", "example.com", -1 },
36 { "b.example.com", "example.com", -1 },
37 { "a.b.example.com", "example.com", -1 },
38 { "uk.com", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
39 { "example.uk.com", "example.uk.com", -1 },
40 { "b.example.uk.com", "example.uk.com", -1 },
41 { "a.b.example.uk.com", "example.uk.com", -1 },
42 { "test.ac", "test.ac", -1 },
43 /* TLD with only 1 (wildcard) rule. */
44 { "bd", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
45 { "c.bd", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
46 { "b.c.bd", "b.c.bd", -1 },
47 { "a.b.c.bd", "b.c.bd", -1 },
48 /* More complex TLD. */
49 { "jp", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
50 { "test.jp", "test.jp", -1 },
51 { "www.test.jp", "test.jp", -1 },
52 { "ac.jp", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
53 { "test.ac.jp", "test.ac.jp", -1 },
54 { "www.test.ac.jp", "test.ac.jp", -1 },
55 { "kyoto.jp", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
56 { "minami.kyoto.jp", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
57 { "b.minami.kyoto.jp", "b.minami.kyoto.jp", -1 },
58 { "a.b.minami.kyoto.jp", "b.minami.kyoto.jp", -1 },
59 { "pref.kyoto.jp", "pref.kyoto.jp", -1 },
60 { "www.pref.kyoto.jp", "pref.kyoto.jp", -1 },
61 { "city.kyoto.jp", "city.kyoto.jp", -1 },
62 { "www.city.kyoto.jp", "city.kyoto.jp", -1 },
63 /* TLD with a wildcard rule and exceptions. */
64 { "ck", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
65 { "test.ck", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
66 { "b.test.ck", "b.test.ck", -1 },
67 { "a.b.test.ck", "b.test.ck", -1 },
68 { "www.ck", "www.ck", -1 },
69 { "www.www.ck", "www.ck", -1 },
70 /* US K12. */
71 { "us", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
72 { "test.us", "test.us", -1 },
73 { "www.test.us", "test.us", -1 },
74 { "ak.us", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
75 { "test.ak.us", "test.ak.us", -1 },
76 { "www.test.ak.us", "test.ak.us", -1 },
77 { "k12.ak.us", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
78 { "test.k12.ak.us", "test.k12.ak.us", -1 },
79 { "www.test.k12.ak.us", "test.k12.ak.us", -1 },
80 /* IDN labels. */
81 { "食狮.com.cn", "食狮.com.cn", -1 },
82 { "食狮.公司.cn", "食狮.公司.cn", -1 },
83 { "www.食狮.公司.cn", "食狮.公司.cn", -1 },
84 { "shishi.公司.cn", "shishi.公司.cn", -1 },
85 { "公司.cn", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
86 { "食狮.中国", "食狮.中国", -1 },
87 { "www.食狮.中国", "食狮.中国", -1 },
88 { "shishi.中国", "shishi.中国", -1 },
89 { "中国", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
90 /* Same as above, but punycoded. */
91 { "xn--85x722f.com.cn", "xn--85x722f.com.cn", -1 },
92 { "xn--85x722f.xn--55qx5d.cn", "xn--85x722f.xn--55qx5d.cn", -1 },
93 { "www.xn--85x722f.xn--55qx5d.cn", "xn--85x722f.xn--55qx5d.cn", -1 },
94 { "shishi.xn--55qx5d.cn", "shishi.xn--55qx5d.cn", -1 },
95 { "xn--55qx5d.cn", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
96 { "xn--85x722f.xn--fiqs8s", "xn--85x722f.xn--fiqs8s", -1 },
97 { "www.xn--85x722f.xn--fiqs8s", "xn--85x722f.xn--fiqs8s", -1 },
98 { "shishi.xn--fiqs8s", "shishi.xn--fiqs8s", -1 },
99 { "xn--fiqs8s", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
100 /* End of publicsuffix.org tests */
101
102 /* Let's just double-check this one... */
103 { "co.uk", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
104 { "test.co.uk", "test.co.uk", -1 },
105 { "www.test.co.uk", "test.co.uk", -1 },
106
107 /* Two levels of non-ASCII */
108 { "våler.østfold.no", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
109 { "test.våler.østfold.no", "test.våler.østfold.no", -1 },
110 { "www.test.våler.østfold.no", "test.våler.østfold.no", -1 },
111 { "xn--vler-qoa.xn--stfold-9xa.no", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
112 { "test.xn--vler-qoa.xn--stfold-9xa.no", "test.xn--vler-qoa.xn--stfold-9xa.no", -1 },
113 { "www.test.xn--vler-qoa.xn--stfold-9xa.no", "test.xn--vler-qoa.xn--stfold-9xa.no", -1 },
114 },
115
116 /* Non Internet TLDs have NULL as expected result
117 */
118 non_inet_tld_tests[] = {
119 /* Unlisted TLD.*/
120 { "example", NULL },
121 { "example.example", NULL },
122 { "b.example.example", NULL },
123 { "a.b.example.example", NULL },
124 /* Listed, but non-Internet, TLD. */
125 { "local", NULL },
126 { "example.local", NULL },
127 { "b.example.local", NULL },
128 { "a.b.example.local", NULL }
129 };
130
131 static void
do_inet_tests(void)132 do_inet_tests (void)
133 {
134 int i;
135
136 for (i = 0; i < G_N_ELEMENTS (tld_tests); i++) {
137 GError *error = NULL;
138 gboolean is_public;
139 const char *base_domain;
140
141 debug_printf (1, "Testing %s\n", tld_tests[i].hostname);
142
143 is_public = soup_tld_domain_is_public_suffix (tld_tests[i].hostname);
144 base_domain = soup_tld_get_base_domain (tld_tests[i].hostname, &error);
145
146 if (base_domain) {
147 g_assert_no_error (error);
148 g_assert_false (is_public);
149 g_assert_cmpstr (base_domain, ==, tld_tests[i].result);
150 } else {
151 g_assert_null (tld_tests[i].result);
152 g_assert_error (error, SOUP_TLD_ERROR, tld_tests[i].error);
153 g_clear_error (&error);
154 }
155 }
156 }
157
158 static void
do_non_inet_tests(void)159 do_non_inet_tests (void)
160 {
161 int i;
162
163 g_test_bug ("679230");
164 g_test_bug ("681085");
165
166 for (i = 0; i < G_N_ELEMENTS (non_inet_tld_tests); i++) {
167 gboolean is_public;
168 const char *base_domain;
169
170 debug_printf (1, "Testing %s\n", non_inet_tld_tests[i].hostname);
171
172 is_public = soup_tld_domain_is_public_suffix (non_inet_tld_tests[i].hostname);
173 base_domain = soup_tld_get_base_domain (non_inet_tld_tests[i].hostname, NULL);
174
175 g_assert_false (is_public);
176 g_assert_null (base_domain);
177 }
178 }
179
180 int
main(int argc,char ** argv)181 main (int argc, char **argv)
182 {
183 int ret;
184
185 test_init (argc, argv, NULL);
186
187 g_test_add_func ("/tld/inet", do_inet_tests);
188 g_test_add_func ("/tld/non-inet", do_non_inet_tests);
189
190 ret = g_test_run ();
191
192 test_cleanup ();
193
194 return ret;
195 }
196