tests: add cache-test
[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 } tld_tests[] = {
13   /* NULL input. Not checked here because the API requires a valid hostname. */
14   /* { NULL, NULL }, */
15   /* Mixed case. Not checked because the API requires a valid hostname. */
16   /* { "COM", NULL }, */
17   /* { "example.COM", "example.com" }, */
18   /* { "WwW.example.COM", "example.com" }, */
19   /* Leading dot. */
20   { ".com", NULL },
21   { ".example", NULL },
22   { ".example.com", NULL },
23   { ".example.example", NULL },
24   /* TLD with only 1 rule. */
25   { "biz", NULL },
26   { "domain.biz", "domain.biz" },
27   { "b.domain.biz", "domain.biz" },
28   { "a.b.domain.biz", "domain.biz" },
29   /* TLD with some 2-level rules. */
30   { "com", NULL },
31   { "example.com", "example.com" },
32   { "b.example.com", "example.com" },
33   { "a.b.example.com", "example.com" },
34   { "uk.com", NULL },
35   { "example.uk.com", "example.uk.com" },
36   { "b.example.uk.com", "example.uk.com" },
37   { "a.b.example.uk.com", "example.uk.com" },
38   { "test.ac", "test.ac" },
39   /* TLD with only 1 (wildcard) rule. */
40   { "cy", NULL },
41   { "c.cy", NULL },
42   { "b.c.cy", "b.c.cy" },
43   { "a.b.c.cy", "b.c.cy" },
44   /* More complex TLD. */
45   { "jp", NULL },
46   { "test.jp", "test.jp" },
47   { "www.test.jp", "test.jp" },
48   { "ac.jp", NULL },
49   { "test.ac.jp", "test.ac.jp" },
50   { "www.test.ac.jp", "test.ac.jp" },
51   { "kyoto.jp", NULL },
52   { "minami.kyoto.jp", NULL },
53   { "b.minami.kyoto.jp", "b.minami.kyoto.jp" },
54   { "a.b.minami.kyoto.jp", "b.minami.kyoto.jp" },
55   { "pref.kyoto.jp", "pref.kyoto.jp" },
56   { "www.pref.kyoto.jp", "pref.kyoto.jp" },
57   { "city.kyoto.jp", "city.kyoto.jp" },
58   { "www.city.kyoto.jp", "city.kyoto.jp" },
59   /* TLD with a wildcard rule and exceptions. */
60   { "om", NULL },
61   { "test.om", NULL },
62   { "b.test.om", "b.test.om" },
63   { "a.b.test.om", "b.test.om" },
64   { "songfest.om", "songfest.om" },
65   { "www.songfest.om", "songfest.om" },
66   /* US K12. */
67   { "us", NULL },
68   { "test.us", "test.us" },
69   { "www.test.us", "test.us" },
70   { "ak.us", NULL },
71   { "test.ak.us", "test.ak.us" },
72   { "www.test.ak.us", "test.ak.us" },
73   { "k12.ak.us", NULL },
74   { "test.k12.ak.us", "test.k12.ak.us" },
75   { "www.test.k12.ak.us", "test.k12.ak.us" },
76   /* This is not in http://publicsuffix.org/list/test.txt but we want to check it anyway. */
77   { "co.uk", NULL },
78   /* The original list does not include non-ASCII tests. Let's add a couple. */
79   { "公司.cn", NULL },
80   { "a.b.åfjord.no", "b.åfjord.no" }
81 },
82 /* Non Internet TLDs have NULL as expected result
83  */
84 non_inet_tld_tests[] = {
85   /* Unlisted TLD.*/
86   { "example", NULL },
87   { "example.example", NULL },
88   { "b.example.example", NULL },
89   { "a.b.example.example", NULL },
90   /* Listed, but non-Internet, TLD. */
91   { "local", NULL },
92   { "example.local", NULL },
93   { "b.example.local", NULL },
94   { "a.b.example.local", NULL }
95 };
96
97 int
98 main (int argc, char **argv)
99 {
100         int i;
101
102         test_init (argc, argv, NULL);
103
104         errors = 0;
105         for (i = 0; i < G_N_ELEMENTS (tld_tests); ++i) {
106                 GError *error = NULL;
107                 gboolean is_public = soup_tld_domain_is_public_suffix (tld_tests[i].hostname);
108                 const char *base_domain = soup_tld_get_base_domain (tld_tests[i].hostname, &error);
109
110                 debug_printf (1, "Testing %s: ", tld_tests[i].hostname);
111
112                 if (is_public && tld_tests[i].result) {
113                         debug_printf (1, "ERROR: domain is public but base_domain is not NULL (%s)\n",
114                                       base_domain);
115                         ++errors;
116                 } else if (g_strcmp0 (tld_tests[i].result, base_domain)) {
117                         debug_printf (1, "ERROR: %s expected as base domain but got %s\n",
118                                       tld_tests[i].result, base_domain);
119                         ++errors;
120
121                 } else if (!tld_tests[i].result && !is_public &&
122                            !g_error_matches (error, SOUP_TLD_ERROR, SOUP_TLD_ERROR_INVALID_HOSTNAME)) {
123                         debug_printf (1, "ERROR: not public domain with NULL expected result\n");
124                         ++errors;
125                 } else
126                         debug_printf (1, "OK\n");
127
128                 g_clear_error(&error);
129         }
130
131         for (i = 0; i < G_N_ELEMENTS (non_inet_tld_tests); ++i) {
132                 gboolean is_public = soup_tld_domain_is_public_suffix (non_inet_tld_tests[i].hostname);
133                 const char *base_domain = soup_tld_get_base_domain (non_inet_tld_tests[i].hostname, NULL);
134
135                 debug_printf (1, "Testing %s: ", non_inet_tld_tests[i].hostname);
136
137                 if (is_public) {
138                         debug_printf (1, "ERROR: domain incorrectly clasified as public\n");
139                         ++errors;
140                 } else if (base_domain) {
141                         debug_printf (1, "ERROR: non NULL base domain (%s) for local url\n",
142                                       base_domain);
143                         ++errors;
144                 } else
145                         debug_printf (1, "OK\n");
146         }
147
148         test_cleanup ();
149
150         return errors != 0;
151 }