summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSergio Villar Senin <svillar@igalia.com>2012-04-10 00:06:35 +0200
committerSergio Villar Senin <svillar@igalia.com>2012-06-14 20:08:09 +0200
commit5a30492229e3b09039d60979f74f6632767677d5 (patch)
treec7530a270becf09a915271f6f42f9ef9cad32a6c /tests
parent6a0d7574b74c9bc20e8bd43e18474bc43c5e4408 (diff)
downloadlibsoup-5a30492229e3b09039d60979f74f6632767677d5.tar.gz
libsoup-5a30492229e3b09039d60979f74f6632767677d5.tar.bz2
libsoup-5a30492229e3b09039d60979f74f6632767677d5.zip
soup-tld: added soup_tld_* utils
The soup_tld_* set of functions along with the list of public suffixes published in http://publicsuffix.org allows API clients to get the base domain of a given hostname or to check if a given domain is a public suffix (one under which internet users can register new names). https://bugzilla.gnome.org/show_bug.cgi?id=673802
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am3
-rw-r--r--tests/tld-test.c131
2 files changed, 134 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 81aaa867..37db420b 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -34,6 +34,7 @@ noinst_PROGRAMS = \
ssl-test \
streaming-test \
timeout-test \
+ tld-test \
uri-parsing \
$(CURL_TESTS) \
$(APACHE_TESTS) \
@@ -71,6 +72,7 @@ sniffing_test_SOURCES = sniffing-test.c $(TEST_SRCS)
ssl_test_SOURCES = ssl-test.c $(TEST_SRCS)
streaming_test_SOURCES = streaming-test.c $(TEST_SRCS)
timeout_test_SOURCES = timeout-test.c $(TEST_SRCS)
+tld_test_SOURCES = tld-test.c $(TEST_SRCS)
uri_parsing_SOURCES = uri-parsing.c $(TEST_SRCS)
xmlrpc_test_SOURCES = xmlrpc-test.c $(TEST_SRCS)
xmlrpc_server_test_SOURCES = xmlrpc-server-test.c $(TEST_SRCS)
@@ -102,6 +104,7 @@ TESTS = \
ssl-test \
streaming-test \
timeout-test \
+ tld-test \
uri-parsing \
$(APACHE_TESTS) \
$(CURL_TESTS) \
diff --git a/tests/tld-test.c b/tests/tld-test.c
new file mode 100644
index 00000000..7be0ae71
--- /dev/null
+++ b/tests/tld-test.c
@@ -0,0 +1,131 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2012 Igalia S.L.
+ */
+
+#include <glib.h>
+#include <libsoup/soup.h>
+#include <string.h>
+
+#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 },
+ /* Unlisted TLD. Not checked because we do not want to force every URL to have a public suffix.*/
+ /* { "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 }, */
+ /* 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 },
+ { "c.kyoto.jp", NULL },
+ { "b.c.kyoto.jp", "b.c.kyoto.jp" },
+ { "a.b.c.kyoto.jp", "b.c.kyoto.jp" },
+ { "pref.kyoto.jp", "pref.kyoto.jp" }, /* Exception rule. */
+ { "www.pref.kyoto.jp", "pref.kyoto.jp" }, /* Exception rule. */
+ { "city.kyoto.jp", "city.kyoto.jp" }, /* Exception rule. */
+ { "www.city.kyoto.jp", "city.kyoto.jp" }, /* Exception rule. */
+ /* TLD with a wildcard rule and exceptions. */
+ { "om", NULL },
+ { "test.om", NULL },
+ { "b.test.om", "b.test.om" },
+ { "a.b.test.om", "b.test.om" },
+ { "songfest.om", "songfest.om" },
+ { "www.songfest.om", "songfest.om" },
+ /* 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" },
+ /* This is not in http://publicsuffix.org/list/test.txt but we want to check it anyway. */
+ { "co.uk", NULL },
+ /* The original list does not include non-ASCII tests. Let's add a couple. */
+ { "公司.cn", NULL },
+ { "a.b.åfjord.no", "b.åfjord.no" }
+};
+
+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) {
+ 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, NULL);
+
+ if (tld_tests[i].result) {
+ /* Public domains have NULL expected results. */
+ if (is_public || g_strcmp0 (tld_tests[i].result, base_domain)) {
+ debug_printf (1, "ERROR: %s got %s (%s expected)\n",
+ tld_tests[i].hostname, base_domain, tld_tests[i].result);
+ ++errors;
+ }
+ } else {
+ /* If there is no expected result then either the domain is public or
+ * the hostname invalid (for example starts with a leading dot).
+ */
+ if (!is_public && base_domain) {
+ debug_printf (1, "ERROR: public domain %s got %s (none expected)\n",
+ tld_tests[i].hostname, base_domain);
+ ++errors;
+ }
+ }
+ }
+
+ test_cleanup ();
+
+ return errors != 0;
+}