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