Fix the retry-on-broken-connection codepath for SoupRequest
[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   /* Unlisted TLD. Not checked because we do not want to force every URL to have a public suffix.*/
25   /* { "example", NULL }, */
26   /* { "example.example", NULL }, */
27   /* { "b.example.example", NULL }, */
28   /* { "a.b.example.example", NULL }, */
29   /* Listed, but non-Internet, TLD. */
30   /*{ "local", NULL }, */
31   /*{ "example.local", NULL }, */
32   /*{ "b.example.local", NULL }, */
33   /*{ "a.b.example.local", NULL }, */
34   /* TLD with only 1 rule. */
35   { "biz", NULL },
36   { "domain.biz", "domain.biz" },
37   { "b.domain.biz", "domain.biz" },
38   { "a.b.domain.biz", "domain.biz" },
39   /* TLD with some 2-level rules. */
40   { "com", NULL },
41   { "example.com", "example.com" },
42   { "b.example.com", "example.com" },
43   { "a.b.example.com", "example.com" },
44   { "uk.com", NULL },
45   { "example.uk.com", "example.uk.com" },
46   { "b.example.uk.com", "example.uk.com" },
47   { "a.b.example.uk.com", "example.uk.com" },
48   { "test.ac", "test.ac" },
49   /* TLD with only 1 (wildcard) rule. */
50   { "cy", NULL },
51   { "c.cy", NULL },
52   { "b.c.cy", "b.c.cy" },
53   { "a.b.c.cy", "b.c.cy" },
54   /* More complex TLD. */
55   { "jp", NULL },
56   { "test.jp", "test.jp" },
57   { "www.test.jp", "test.jp" },
58   { "ac.jp", NULL },
59   { "test.ac.jp", "test.ac.jp" },
60   { "www.test.ac.jp", "test.ac.jp" },
61   { "kyoto.jp", NULL },
62   { "c.kyoto.jp", NULL },
63   { "b.c.kyoto.jp", "b.c.kyoto.jp" },
64   { "a.b.c.kyoto.jp", "b.c.kyoto.jp" },
65   { "pref.kyoto.jp", "pref.kyoto.jp" }, /* Exception rule. */
66   { "www.pref.kyoto.jp", "pref.kyoto.jp" },     /* Exception rule. */
67   { "city.kyoto.jp", "city.kyoto.jp" }, /* Exception rule. */
68   { "www.city.kyoto.jp", "city.kyoto.jp" },     /* Exception rule. */
69   /* TLD with a wildcard rule and exceptions. */
70   { "om", NULL },
71   { "test.om", NULL },
72   { "b.test.om", "b.test.om" },
73   { "a.b.test.om", "b.test.om" },
74   { "songfest.om", "songfest.om" },
75   { "www.songfest.om", "songfest.om" },
76   /* US K12. */
77   { "us", NULL },
78   { "test.us", "test.us" },
79   { "www.test.us", "test.us" },
80   { "ak.us", NULL },
81   { "test.ak.us", "test.ak.us" },
82   { "www.test.ak.us", "test.ak.us" },
83   { "k12.ak.us", NULL },
84   { "test.k12.ak.us", "test.k12.ak.us" },
85   { "www.test.k12.ak.us", "test.k12.ak.us" },
86   /* This is not in http://publicsuffix.org/list/test.txt but we want to check it anyway. */
87   { "co.uk", NULL },
88   /* The original list does not include non-ASCII tests. Let's add a couple. */
89   { "公司.cn", NULL },
90   { "a.b.åfjord.no", "b.åfjord.no" }
91 };
92
93 int
94 main (int argc, char **argv)
95 {
96         int i;
97
98         test_init (argc, argv, NULL);
99
100         errors = 0;
101         for (i = 0; i < G_N_ELEMENTS (tld_tests); ++i) {
102                gboolean is_public = soup_tld_domain_is_public_suffix (tld_tests[i].hostname);
103                const char *base_domain = soup_tld_get_base_domain (tld_tests[i].hostname, NULL);
104
105                if (tld_tests[i].result) {
106                        /* Public domains have NULL expected results. */
107                        if (is_public || g_strcmp0 (tld_tests[i].result, base_domain)) {
108                                debug_printf (1, "ERROR: %s got %s (%s expected)\n",
109                                              tld_tests[i].hostname, base_domain, tld_tests[i].result);
110                                ++errors;
111                        }
112                } else {
113                        /* If there is no expected result then either the domain is public or
114                         * the hostname invalid (for example starts with a leading dot).
115                         */
116                        if (!is_public && base_domain) {
117                                debug_printf (1, "ERROR: public domain %s got %s (none expected)\n",
118                                              tld_tests[i].hostname, base_domain);
119                                ++errors;
120                        }
121                }
122         }
123
124         test_cleanup ();
125
126         return errors != 0;
127 }