]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/util/TestPublicSuffixes.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / tests / club / wpia / gigi / util / TestPublicSuffixes.java
1 package club.wpia.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 import club.wpia.gigi.util.PublicSuffixes;
18
19 @RunWith(Parameterized.class)
20 public class TestPublicSuffixes {
21
22     /**
23      * Taken from
24      * http://mxr.mozilla.org/mozilla-central/source/netwerk/test/unit
25      * /data/test_psl.txt?raw=1
26      */
27     @Parameters(name = "publicSuffix({0}) = {1}")
28     public static Iterable<String[]> genParams() throws IOException {
29         BufferedReader br = null;
30         try {
31             br = new BufferedReader(new InputStreamReader(TestPublicSuffixes.class.getResourceAsStream("TestPublicSuffixes.txt"), "UTF-8"));
32             ArrayList<String[]> result = new ArrayList<>();
33             String line;
34             while ((line = br.readLine()) != null) {
35                 if (line.startsWith("//") || line.isEmpty()) {
36                     continue;
37                 }
38                 String parseSuffix = "checkPublicSuffix(";
39                 if (line.startsWith(parseSuffix)) {
40                     String data = line.substring(parseSuffix.length(), line.length() - 2);
41                     String[] parts = data.split(", ");
42                     if (parts.length != 2) {
43                         throw new Error("Syntax error in public suffix test data file: " + line);
44                     }
45                     result.add(new String[] {
46                             parse(parts[0]), parse(parts[1])
47                     });
48                 } else {
49                     throw new Error("Unparsable line: " + line);
50                 }
51             }
52             return result;
53         } finally {
54             if (br != null) {
55                 br.close();
56             }
57         }
58     }
59
60     private static String parse(String data) {
61         if (data.equals("null")) {
62             return null;
63         }
64         if (data.startsWith("'") && data.endsWith("'")) {
65             return data.substring(1, data.length() - 1);
66         }
67         throw new Error("Syntax error with literal: " + data);
68     }
69
70     @Parameter(0)
71     public String domain;
72
73     @Parameter(1)
74     public String suffix;
75
76     @Test
77     public void testPublicSuffix() {
78         if (domain != null) {
79             domain = domain.toLowerCase();
80         }
81         String publicSuffix = PublicSuffixes.getInstance().getRegistrablePart(domain == null ? null : IDN.toASCII(domain));
82         assertEquals(suffix == null ? null : IDN.toASCII(suffix), publicSuffix);
83     }
84 }