]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/util/TestPublicSuffixes.java
UPD: Public Suffixes Parameterized.
[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.io.BufferedReader;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.net.IDN;
9 import java.util.ArrayList;
10
11 import org.junit.Test;
12 import org.junit.runner.RunWith;
13 import org.junit.runners.Parameterized;
14 import org.junit.runners.Parameterized.Parameter;
15 import org.junit.runners.Parameterized.Parameters;
16
17 @RunWith(Parameterized.class)
18 public class TestPublicSuffixes {
19
20     /**
21      * Taken from
22      * http://mxr.mozilla.org/mozilla-central/source/netwerk/test/unit
23      * /data/test_psl.txt?raw=1
24      */
25     @Parameters(name = "publicSuffix({0}) = {1}")
26     public static Iterable<String[]> genParams() throws IOException {
27         BufferedReader br = new BufferedReader(new InputStreamReader(TestPublicSuffixes.class.getResourceAsStream("TestPublicSuffixes.txt"), "UTF-8"));
28         ArrayList<String[]> result = new ArrayList<>();
29         String line;
30         while ((line = br.readLine()) != null) {
31             if (line.startsWith("//") || line.isEmpty()) {
32                 continue;
33             }
34             String parseSuffix = "checkPublicSuffix(";
35             if (line.startsWith(parseSuffix)) {
36                 String data = line.substring(parseSuffix.length(), line.length() - 2);
37                 String[] parts = data.split(", ");
38                 if (parts.length != 2) {
39                     throw new Error("Syntax error in public suffix test data file: " + line);
40                 }
41                 result.add(new String[] {
42                         parse(parts[0]), parse(parts[1])
43                 });
44             } else {
45                 throw new Error("Unparsable line: " + line);
46             }
47         }
48
49         return result;
50     }
51
52     private static String parse(String data) {
53         if (data.equals("null")) {
54             return null;
55         }
56         if (data.startsWith("'") && data.endsWith("'")) {
57             return data.substring(1, data.length() - 1);
58         }
59         throw new Error("Syntax error with literal: " + data);
60     }
61
62     @Parameter(0)
63     public String domain;
64
65     @Parameter(1)
66     public String suffix;
67
68     @Test
69     public void testPublicSuffix() {
70         if (domain != null) {
71             domain = domain.toLowerCase();
72         }
73         String publicSuffix = PublicSuffixes.getInstance().getRegistrablePart(domain == null ? null : IDN.toASCII(domain));
74         assertEquals(suffix == null ? null : IDN.toASCII(suffix), publicSuffix);
75     }
76 }