add missing slash in %configure
[platform/upstream/libsoup.git] / tests / tld-test.c
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         /* TLD with only 1 rule. */
26         { "biz", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
27         { "domain.biz", "domain.biz", -1 },
28         { "b.domain.biz", "domain.biz", -1 },
29         { "a.b.domain.biz", "domain.biz", -1 },
30         /* TLD with some 2-level rules. */
31         { "com", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
32         { "example.com", "example.com", -1 },
33         { "b.example.com", "example.com", -1 },
34         { "a.b.example.com", "example.com", -1 },
35         { "uk.com", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
36         { "example.uk.com", "example.uk.com", -1 },
37         { "b.example.uk.com", "example.uk.com", -1 },
38         { "a.b.example.uk.com", "example.uk.com", -1 },
39         { "test.ac", "test.ac", -1 },
40         /* TLD with only 1 (wildcard) rule. */
41         { "cy", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
42         { "c.cy", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
43         { "b.c.cy", "b.c.cy", -1 },
44         { "a.b.c.cy", "b.c.cy", -1 },
45         /* More complex TLD. */
46         { "jp", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
47         { "test.jp", "test.jp", -1 },
48         { "www.test.jp", "test.jp", -1 },
49         { "ac.jp", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
50         { "test.ac.jp", "test.ac.jp", -1 },
51         { "www.test.ac.jp", "test.ac.jp", -1 },
52         { "kyoto.jp", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
53         { "minami.kyoto.jp", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
54         { "b.minami.kyoto.jp", "b.minami.kyoto.jp", -1 },
55         { "a.b.minami.kyoto.jp", "b.minami.kyoto.jp", -1 },
56         { "pref.kyoto.jp", "pref.kyoto.jp", -1 },
57         { "www.pref.kyoto.jp", "pref.kyoto.jp", -1 },
58         { "city.kyoto.jp", "city.kyoto.jp", -1 },
59         { "www.city.kyoto.jp", "city.kyoto.jp", -1 },
60         /* TLD with a wildcard rule and exceptions. */
61         { "ck", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
62         { "test.ck", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
63         { "b.test.ck", "b.test.ck", -1 },
64         { "a.b.test.ck", "b.test.ck", -1 },
65         { "www.ck", "www.ck", -1 },
66         { "www.www.ck", "www.ck", -1 },
67         /* US K12. */
68         { "us", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
69         { "test.us", "test.us", -1 },
70         { "www.test.us", "test.us", -1 },
71         { "ak.us", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
72         { "test.ak.us", "test.ak.us", -1 },
73         { "www.test.ak.us", "test.ak.us", -1 },
74         { "k12.ak.us", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
75         { "test.k12.ak.us", "test.k12.ak.us", -1 },
76         { "www.test.k12.ak.us", "test.k12.ak.us", -1 },
77         /* IDN labels. */
78         { "食狮.com.cn", "食狮.com.cn", -1 },
79         { "食狮.公司.cn", "食狮.公司.cn", -1 },
80         { "www.食狮.公司.cn", "食狮.公司.cn", -1 },
81         { "shishi.公司.cn", "shishi.公司.cn", -1 },
82         { "公司.cn", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
83         { "食狮.中国", "食狮.中国", -1 },
84         { "www.食狮.中国", "食狮.中国", -1 },
85         { "shishi.中国", "shishi.中国", -1 },
86         { "中国", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
87         /* Same as above, but punycoded. */
88         { "xn--85x722f.com.cn", "xn--85x722f.com.cn", -1 },
89         { "xn--85x722f.xn--55qx5d.cn", "xn--85x722f.xn--55qx5d.cn", -1 },
90         { "www.xn--85x722f.xn--55qx5d.cn", "xn--85x722f.xn--55qx5d.cn", -1 },
91         { "shishi.xn--55qx5d.cn", "shishi.xn--55qx5d.cn", -1 },
92         { "xn--55qx5d.cn", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
93         { "xn--85x722f.xn--fiqs8s", "xn--85x722f.xn--fiqs8s", -1 },
94         { "www.xn--85x722f.xn--fiqs8s", "xn--85x722f.xn--fiqs8s", -1 },
95         { "shishi.xn--fiqs8s", "shishi.xn--fiqs8s", -1 },
96         { "xn--fiqs8s", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
97         /* End of publicsuffix.org tests */
98
99         /* Let's just double-check this one... */
100         { "co.uk", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
101         { "test.co.uk", "test.co.uk", -1 },
102         { "www.test.co.uk", "test.co.uk", -1 },
103
104         /* Two levels of non-ASCII */
105         { "våler.østfold.no", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
106         { "test.våler.østfold.no", "test.våler.østfold.no", -1 },
107         { "www.test.våler.østfold.no", "test.våler.østfold.no", -1 },
108         { "xn--vler-qoa.xn--stfold-9xa.no", NULL, SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS },
109         { "test.xn--vler-qoa.xn--stfold-9xa.no", "test.xn--vler-qoa.xn--stfold-9xa.no", -1 },
110         { "www.test.xn--vler-qoa.xn--stfold-9xa.no", "test.xn--vler-qoa.xn--stfold-9xa.no", -1 },
111 },
112
113 /* Non Internet TLDs have NULL as expected result
114  */
115 non_inet_tld_tests[] = {
116         /* Unlisted TLD.*/
117         { "example", NULL },
118         { "example.example", NULL },
119         { "b.example.example", NULL },
120         { "a.b.example.example", NULL },
121         /* Listed, but non-Internet, TLD. */
122         { "local", NULL },
123         { "example.local", NULL },
124         { "b.example.local", NULL },
125         { "a.b.example.local", NULL }
126 };
127
128 static void
129 do_inet_tests (void)
130 {
131         int i;
132
133         for (i = 0; i < G_N_ELEMENTS (tld_tests); i++) {
134                 GError *error = NULL;
135                 gboolean is_public;
136                 const char *base_domain;
137
138                 debug_printf (1, "Testing %s\n", tld_tests[i].hostname);
139
140                 is_public = soup_tld_domain_is_public_suffix (tld_tests[i].hostname);
141                 base_domain = soup_tld_get_base_domain (tld_tests[i].hostname, &error);
142
143                 if (base_domain) {
144                         g_assert_no_error (error);
145                         g_assert_false (is_public);
146                         g_assert_cmpstr (base_domain, ==, tld_tests[i].result);
147                 } else {
148                         g_assert_null (tld_tests[i].result);
149                         g_assert_error (error, SOUP_TLD_ERROR, tld_tests[i].error);
150                         g_clear_error (&error);
151                 }
152         }
153 }
154
155 static void
156 do_non_inet_tests (void)
157 {
158         int i;
159
160         g_test_bug ("679230");
161         g_test_bug ("681085");
162
163         for (i = 0; i < G_N_ELEMENTS (non_inet_tld_tests); i++) {
164                 gboolean is_public;
165                 const char *base_domain;
166
167                 debug_printf (1, "Testing %s\n", non_inet_tld_tests[i].hostname);
168
169                 is_public = soup_tld_domain_is_public_suffix (non_inet_tld_tests[i].hostname);
170                 base_domain = soup_tld_get_base_domain (non_inet_tld_tests[i].hostname, NULL);
171
172                 g_assert_false (is_public);
173                 g_assert_null (base_domain);
174         }
175 }
176
177 int
178 main (int argc, char **argv)
179 {
180         int ret;
181
182         test_init (argc, argv, NULL);
183
184         g_test_add_func ("/tld/inet", do_inet_tests);
185         g_test_add_func ("/tld/non-inet", do_non_inet_tests);
186
187         ret = g_test_run ();
188
189         test_cleanup ();
190
191         return ret;
192 }