Update to version 2.39.3
[profile/ivi/libsoup2.4.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 <glib.h>
7 #include <libsoup/soup.h>
8 #include <string.h>
9
10 #include "test-utils.h"
11
12 /* From http://publicsuffix.org/list/test.txt */
13 static struct {
14   const char *hostname;
15   const char *result;
16 } tld_tests[] = {
17   /* NULL input. Not checked here because the API requires a valid hostname. */
18   /* { NULL, NULL }, */
19   /* Mixed case. Not checked because the API requires a valid hostname. */
20   /* { "COM", NULL }, */
21   /* { "example.COM", "example.com" }, */
22   /* { "WwW.example.COM", "example.com" }, */
23   /* Leading dot. */
24   { ".com", NULL },
25   { ".example", NULL },
26   { ".example.com", NULL },
27   { ".example.example", NULL },
28   /* Unlisted TLD. Not checked because we do not want to force every URL to have a public suffix.*/
29   /* { "example", NULL }, */
30   /* { "example.example", NULL }, */
31   /* { "b.example.example", NULL }, */
32   /* { "a.b.example.example", NULL }, */
33   /* Listed, but non-Internet, TLD. */
34   /*{ "local", NULL }, */
35   /*{ "example.local", NULL }, */
36   /*{ "b.example.local", NULL }, */
37   /*{ "a.b.example.local", NULL }, */
38   /* TLD with only 1 rule. */
39   { "biz", NULL },
40   { "domain.biz", "domain.biz" },
41   { "b.domain.biz", "domain.biz" },
42   { "a.b.domain.biz", "domain.biz" },
43   /* TLD with some 2-level rules. */
44   { "com", NULL },
45   { "example.com", "example.com" },
46   { "b.example.com", "example.com" },
47   { "a.b.example.com", "example.com" },
48   { "uk.com", NULL },
49   { "example.uk.com", "example.uk.com" },
50   { "b.example.uk.com", "example.uk.com" },
51   { "a.b.example.uk.com", "example.uk.com" },
52   { "test.ac", "test.ac" },
53   /* TLD with only 1 (wildcard) rule. */
54   { "cy", NULL },
55   { "c.cy", NULL },
56   { "b.c.cy", "b.c.cy" },
57   { "a.b.c.cy", "b.c.cy" },
58   /* More complex TLD. */
59   { "jp", NULL },
60   { "test.jp", "test.jp" },
61   { "www.test.jp", "test.jp" },
62   { "ac.jp", NULL },
63   { "test.ac.jp", "test.ac.jp" },
64   { "www.test.ac.jp", "test.ac.jp" },
65   { "kyoto.jp", NULL },
66   { "c.kyoto.jp", NULL },
67   { "b.c.kyoto.jp", "b.c.kyoto.jp" },
68   { "a.b.c.kyoto.jp", "b.c.kyoto.jp" },
69   { "pref.kyoto.jp", "pref.kyoto.jp" }, /* Exception rule. */
70   { "www.pref.kyoto.jp", "pref.kyoto.jp" },     /* Exception rule. */
71   { "city.kyoto.jp", "city.kyoto.jp" }, /* Exception rule. */
72   { "www.city.kyoto.jp", "city.kyoto.jp" },     /* Exception rule. */
73   /* TLD with a wildcard rule and exceptions. */
74   { "om", NULL },
75   { "test.om", NULL },
76   { "b.test.om", "b.test.om" },
77   { "a.b.test.om", "b.test.om" },
78   { "songfest.om", "songfest.om" },
79   { "www.songfest.om", "songfest.om" },
80   /* US K12. */
81   { "us", NULL },
82   { "test.us", "test.us" },
83   { "www.test.us", "test.us" },
84   { "ak.us", NULL },
85   { "test.ak.us", "test.ak.us" },
86   { "www.test.ak.us", "test.ak.us" },
87   { "k12.ak.us", NULL },
88   { "test.k12.ak.us", "test.k12.ak.us" },
89   { "www.test.k12.ak.us", "test.k12.ak.us" },
90   /* This is not in http://publicsuffix.org/list/test.txt but we want to check it anyway. */
91   { "co.uk", NULL },
92   /* The original list does not include non-ASCII tests. Let's add a couple. */
93   { "公司.cn", NULL },
94   { "a.b.åfjord.no", "b.åfjord.no" }
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                gboolean is_public = soup_tld_domain_is_public_suffix (tld_tests[i].hostname);
107                const char *base_domain = soup_tld_get_base_domain (tld_tests[i].hostname, NULL);
108
109                if (tld_tests[i].result) {
110                        /* Public domains have NULL expected results. */
111                        if (is_public || g_strcmp0 (tld_tests[i].result, base_domain)) {
112                                debug_printf (1, "ERROR: %s got %s (%s expected)\n",
113                                              tld_tests[i].hostname, base_domain, tld_tests[i].result);
114                                ++errors;
115                        }
116                } else {
117                        /* If there is no expected result then either the domain is public or
118                         * the hostname invalid (for example starts with a leading dot).
119                         */
120                        if (!is_public && base_domain) {
121                                debug_printf (1, "ERROR: public domain %s got %s (none expected)\n",
122                                              tld_tests[i].hostname, base_domain);
123                                ++errors;
124                        }
125                }
126         }
127
128         test_cleanup ();
129
130         return errors != 0;
131 }