summaryrefslogtreecommitdiff
path: root/tests/tld-test.c
blob: d1f1de1feccae8bed92cf569f4940a2861dcf763 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2012 Igalia S.L.
 */

#include "test-utils.h"

/* From http://publicsuffix.org/list/test.txt */
static struct {
  const char *hostname;
  const char *result;
} tld_tests[] = {
  /* NULL input. Not checked here because the API requires a valid hostname. */
  /* { NULL, NULL }, */
  /* Mixed case. Not checked because the API requires a valid hostname. */
  /* { "COM", NULL }, */
  /* { "example.COM", "example.com" }, */
  /* { "WwW.example.COM", "example.com" }, */
  /* Leading dot. */
  { ".com", NULL },
  { ".example", NULL },
  { ".example.com", NULL },
  { ".example.example", NULL },
  /* TLD with only 1 rule. */
  { "biz", NULL },
  { "domain.biz", "domain.biz" },
  { "b.domain.biz", "domain.biz" },
  { "a.b.domain.biz", "domain.biz" },
  /* TLD with some 2-level rules. */
  { "com", NULL },
  { "example.com", "example.com" },
  { "b.example.com", "example.com" },
  { "a.b.example.com", "example.com" },
  { "uk.com", NULL },
  { "example.uk.com", "example.uk.com" },
  { "b.example.uk.com", "example.uk.com" },
  { "a.b.example.uk.com", "example.uk.com" },
  { "test.ac", "test.ac" },
  /* TLD with only 1 (wildcard) rule. */
  { "cy", NULL },
  { "c.cy", NULL },
  { "b.c.cy", "b.c.cy" },
  { "a.b.c.cy", "b.c.cy" },
  /* More complex TLD. */
  { "jp", NULL },
  { "test.jp", "test.jp" },
  { "www.test.jp", "test.jp" },
  { "ac.jp", NULL },
  { "test.ac.jp", "test.ac.jp" },
  { "www.test.ac.jp", "test.ac.jp" },
  { "kyoto.jp", NULL },
  { "minami.kyoto.jp", NULL },
  { "b.minami.kyoto.jp", "b.minami.kyoto.jp" },
  { "a.b.minami.kyoto.jp", "b.minami.kyoto.jp" },
  { "pref.kyoto.jp", "pref.kyoto.jp" },
  { "www.pref.kyoto.jp", "pref.kyoto.jp" },
  { "city.kyoto.jp", "city.kyoto.jp" },
  { "www.city.kyoto.jp", "city.kyoto.jp" },
  /* TLD with a wildcard rule and exceptions. */
  { "ck", NULL },
  { "test.ck", NULL },
  { "b.test.ck", "b.test.ck" },
  { "a.b.test.ck", "b.test.ck" },
  { "www.ck", "www.ck" },
  { "www.www.ck", "www.ck" },
  /* US K12. */
  { "us", NULL },
  { "test.us", "test.us" },
  { "www.test.us", "test.us" },
  { "ak.us", NULL },
  { "test.ak.us", "test.ak.us" },
  { "www.test.ak.us", "test.ak.us" },
  { "k12.ak.us", NULL },
  { "test.k12.ak.us", "test.k12.ak.us" },
  { "www.test.k12.ak.us", "test.k12.ak.us" },
  /* IDN labels. */
  { "食狮.com.cn", "食狮.com.cn" },
  { "食狮.公司.cn", "食狮.公司.cn" },
  { "www.食狮.公司.cn", "食狮.公司.cn" },
  { "shishi.公司.cn", "shishi.公司.cn" },
  { "公司.cn", NULL },
  { "食狮.中国", "食狮.中国" },
  { "www.食狮.中国", "食狮.中国" },
  { "shishi.中国", "shishi.中国" },
  { "中国", NULL },
  /* Same as above, but punycoded. */
  { "xn--85x722f.com.cn", "xn--85x722f.com.cn" },
  { "xn--85x722f.xn--55qx5d.cn", "xn--85x722f.xn--55qx5d.cn" },
  { "www.xn--85x722f.xn--55qx5d.cn", "xn--85x722f.xn--55qx5d.cn" },
  { "shishi.xn--55qx5d.cn", "shishi.xn--55qx5d.cn" },
  { "xn--55qx5d.cn", NULL },
  { "xn--85x722f.xn--fiqs8s", "xn--85x722f.xn--fiqs8s" },
  { "www.xn--85x722f.xn--fiqs8s", "xn--85x722f.xn--fiqs8s" },
  { "shishi.xn--fiqs8s", "shishi.xn--fiqs8s" },
  { "xn--fiqs8s", NULL },
  /* End of publicsuffix.org tests */

  /* Let's just double-check this one... */
  { "co.uk", NULL },
  { "test.co.uk", "test.co.uk" },
  { "www.test.co.uk", "test.co.uk" },

  /* Two levels of non-ASCII */
  { "våler.østfold.no", NULL },
  { "test.våler.østfold.no", "test.våler.østfold.no" },
  { "www.test.våler.østfold.no", "test.våler.østfold.no" },
  { "xn--vler-qoa.xn--stfold-9xa.no", NULL },
  { "test.xn--vler-qoa.xn--stfold-9xa.no", "test.xn--vler-qoa.xn--stfold-9xa.no" },
  { "www.test.xn--vler-qoa.xn--stfold-9xa.no", "test.xn--vler-qoa.xn--stfold-9xa.no" },
},
/* Non Internet TLDs have NULL as expected result
 */
non_inet_tld_tests[] = {
  /* Unlisted TLD.*/
  { "example", NULL },
  { "example.example", NULL },
  { "b.example.example", NULL },
  { "a.b.example.example", NULL },
  /* Listed, but non-Internet, TLD. */
  { "local", NULL },
  { "example.local", NULL },
  { "b.example.local", NULL },
  { "a.b.example.local", NULL }
};

int
main (int argc, char **argv)
{
	int i;

	test_init (argc, argv, NULL);

	errors = 0;
	for (i = 0; i < G_N_ELEMENTS (tld_tests); ++i) {
		GError *error = NULL;
		gboolean is_public = soup_tld_domain_is_public_suffix (tld_tests[i].hostname);
		const char *base_domain = soup_tld_get_base_domain (tld_tests[i].hostname, &error);

		debug_printf (1, "Testing %s: ", tld_tests[i].hostname);

		if (is_public && tld_tests[i].result) {
			debug_printf (1, "ERROR: domain is public but base_domain is not NULL (%s)\n",
				      base_domain);
			++errors;
		} else if (g_strcmp0 (tld_tests[i].result, base_domain)) {
			debug_printf (1, "ERROR: %s expected as base domain but got %s\n",
				      tld_tests[i].result, base_domain);
			++errors;

		} else if (!tld_tests[i].result && !is_public &&
			   !g_error_matches (error, SOUP_TLD_ERROR, SOUP_TLD_ERROR_INVALID_HOSTNAME)) {
			debug_printf (1, "ERROR: not public domain with NULL expected result\n");
			++errors;
		} else
			debug_printf (1, "OK\n");

		g_clear_error(&error);
	}

	debug_printf (1, "\n");

	for (i = 0; i < G_N_ELEMENTS (non_inet_tld_tests); ++i) {
		gboolean is_public = soup_tld_domain_is_public_suffix (non_inet_tld_tests[i].hostname);
		const char *base_domain = soup_tld_get_base_domain (non_inet_tld_tests[i].hostname, NULL);

		debug_printf (1, "Testing %s: ", non_inet_tld_tests[i].hostname);

		if (is_public) {
			debug_printf (1, "ERROR: domain incorrectly clasified as public\n");
			++errors;
		} else if (base_domain) {
			debug_printf (1, "ERROR: non NULL base domain (%s) for local url\n",
				      base_domain);
			++errors;
		} else
			debug_printf (1, "OK\n");
	}

	test_cleanup ();

	return errors != 0;
}