]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/util/TestPublicSuffixes.java
Implement use of Public Suffix List.
[gigi.git] / tests / org / cacert / gigi / util / TestPublicSuffixes.java
1 package org.cacert.gigi.util;
2
3 import static org.junit.Assert.*;
4
5 import java.net.IDN;
6
7 import org.junit.Test;
8
9 public class TestPublicSuffixes {
10
11     /**
12      * Taken from
13      * http://mxr.mozilla.org/mozilla-central/source/netwerk/test/unit
14      * /data/test_psl.txt?raw=1
15      */
16     @Test
17     public void testMozilla() {
18         // Any copyright is dedicated to the Public Domain.
19         // http://creativecommons.org/publicdomain/zero/1.0/
20
21         // null input.
22         checkPublicSuffix(null, null);
23         // Mixed case.
24         checkPublicSuffix("COM", null);
25         checkPublicSuffix("example.COM", "example.com");
26         checkPublicSuffix("WwW.example.COM", "example.com");
27         // Leading dot.
28         checkPublicSuffix(".com", null);
29         checkPublicSuffix(".example", null);
30         checkPublicSuffix(".example.com", null);
31         checkPublicSuffix(".example.example", null);
32         // Unlisted TLD.
33         /*
34          * checkPublicSuffix("example", null);
35          * checkPublicSuffix("example.example", "example.example");
36          * checkPublicSuffix("b.example.example", "example.example");
37          * checkPublicSuffix("a.b.example.example", "example.example");
38          */
39         // Listed, but non-Internet, TLD.
40         // checkPublicSuffix("local", null);
41         // checkPublicSuffix("example.local", null);
42         // checkPublicSuffix("b.example.local", null);
43         // checkPublicSuffix("a.b.example.local", null);
44         // TLD with only 1 rule.
45         checkPublicSuffix("biz", null);
46         checkPublicSuffix("domain.biz", "domain.biz");
47         checkPublicSuffix("b.domain.biz", "domain.biz");
48         checkPublicSuffix("a.b.domain.biz", "domain.biz");
49         // TLD with some 2-level rules.
50         checkPublicSuffix("com", null);
51         checkPublicSuffix("example.com", "example.com");
52         checkPublicSuffix("b.example.com", "example.com");
53         checkPublicSuffix("a.b.example.com", "example.com");
54         checkPublicSuffix("uk.com", null);
55         checkPublicSuffix("example.uk.com", "example.uk.com");
56         checkPublicSuffix("b.example.uk.com", "example.uk.com");
57         checkPublicSuffix("a.b.example.uk.com", "example.uk.com");
58         checkPublicSuffix("test.ac", "test.ac");
59         // TLD with only 1 (wildcard) rule.
60         checkPublicSuffix("cy", null);
61         checkPublicSuffix("c.cy", null);
62         checkPublicSuffix("b.c.cy", "b.c.cy");
63         checkPublicSuffix("a.b.c.cy", "b.c.cy");
64         // More complex TLD.
65         checkPublicSuffix("jp", null);
66         checkPublicSuffix("test.jp", "test.jp");
67         checkPublicSuffix("www.test.jp", "test.jp");
68         checkPublicSuffix("ac.jp", null);
69         checkPublicSuffix("test.ac.jp", "test.ac.jp");
70         checkPublicSuffix("www.test.ac.jp", "test.ac.jp");
71         checkPublicSuffix("kyoto.jp", null);
72         checkPublicSuffix("test.kyoto.jp", "test.kyoto.jp");
73         checkPublicSuffix("ide.kyoto.jp", null);
74         checkPublicSuffix("b.ide.kyoto.jp", "b.ide.kyoto.jp");
75         checkPublicSuffix("a.b.ide.kyoto.jp", "b.ide.kyoto.jp");
76         checkPublicSuffix("c.kobe.jp", null);
77         checkPublicSuffix("b.c.kobe.jp", "b.c.kobe.jp");
78         checkPublicSuffix("a.b.c.kobe.jp", "b.c.kobe.jp");
79         checkPublicSuffix("city.kobe.jp", "city.kobe.jp");
80         checkPublicSuffix("www.city.kobe.jp", "city.kobe.jp");
81         // TLD with a wildcard rule and exceptions.
82         checkPublicSuffix("ck", null);
83         checkPublicSuffix("test.ck", null);
84         checkPublicSuffix("b.test.ck", "b.test.ck");
85         checkPublicSuffix("a.b.test.ck", "b.test.ck");
86         checkPublicSuffix("www.ck", "www.ck");
87         checkPublicSuffix("www.www.ck", "www.ck");
88         // US K12.
89         checkPublicSuffix("us", null);
90         checkPublicSuffix("test.us", "test.us");
91         checkPublicSuffix("www.test.us", "test.us");
92         checkPublicSuffix("ak.us", null);
93         checkPublicSuffix("test.ak.us", "test.ak.us");
94         checkPublicSuffix("www.test.ak.us", "test.ak.us");
95         checkPublicSuffix("k12.ak.us", null);
96         checkPublicSuffix("test.k12.ak.us", "test.k12.ak.us");
97         checkPublicSuffix("www.test.k12.ak.us", "test.k12.ak.us");
98     }
99
100     @Test
101     public void testMozillaIDN() {
102         // IDN labels.
103         checkPublicSuffix("食狮.com.cn", "食狮.com.cn");
104         checkPublicSuffix("食狮.公司.cn", "食狮.公司.cn");
105         checkPublicSuffix("www.食狮.公司.cn", "食狮.公司.cn");
106         checkPublicSuffix("shishi.公司.cn", "shishi.公司.cn");
107         checkPublicSuffix("公司.cn", null);
108         checkPublicSuffix("食狮.中国", "食狮.中国");
109         checkPublicSuffix("www.食狮.中国", "食狮.中国");
110         checkPublicSuffix("shishi.中国", "shishi.中国");
111         checkPublicSuffix("中国", null);
112     }
113
114     @Test
115     public void testMozillaIDNPuny() {
116         // Same as above, but punycoded.
117         checkPublicSuffix("xn--85x722f.com.cn", "xn--85x722f.com.cn");
118         checkPublicSuffix("xn--85x722f.xn--55qx5d.cn", "xn--85x722f.xn--55qx5d.cn");
119         checkPublicSuffix("www.xn--85x722f.xn--55qx5d.cn", "xn--85x722f.xn--55qx5d.cn");
120         checkPublicSuffix("shishi.xn--55qx5d.cn", "shishi.xn--55qx5d.cn");
121         checkPublicSuffix("xn--55qx5d.cn", null);
122         checkPublicSuffix("xn--85x722f.xn--fiqs8s", "xn--85x722f.xn--fiqs8s");
123         checkPublicSuffix("www.xn--85x722f.xn--fiqs8s", "xn--85x722f.xn--fiqs8s");
124         checkPublicSuffix("shishi.xn--fiqs8s", "shishi.xn--fiqs8s");
125         checkPublicSuffix("xn--fiqs8s", null);
126
127     }
128
129     private void checkPublicSuffix(String domain, String suffix) {
130         if (domain != null) {
131             domain = domain.toLowerCase();
132         }
133         String publicSuffix = PublicSuffixes.getInstance().getRegistrablePart(domain == null ? null : IDN.toASCII(domain));
134         assertEquals(suffix == null ? null : IDN.toASCII(suffix), publicSuffix);
135     }
136 }