1 package club.wpia.gigi.util;
3 import static org.junit.Assert.*;
5 import java.io.BufferedReader;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
9 import java.util.ArrayList;
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;
17 import club.wpia.gigi.util.PublicSuffixes;
19 @RunWith(Parameterized.class)
20 public class TestPublicSuffixes {
24 * http://mxr.mozilla.org/mozilla-central/source/netwerk/test/unit
25 * /data/test_psl.txt?raw=1
27 @Parameters(name = "publicSuffix({0}) = {1}")
28 public static Iterable<String[]> genParams() throws IOException {
29 BufferedReader br = null;
31 br = new BufferedReader(new InputStreamReader(TestPublicSuffixes.class.getResourceAsStream("TestPublicSuffixes.txt"), "UTF-8"));
32 ArrayList<String[]> result = new ArrayList<>();
34 while ((line = br.readLine()) != null) {
35 if (line.startsWith("//") || line.isEmpty()) {
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);
45 result.add(new String[] {
46 parse(parts[0]), parse(parts[1])
49 throw new Error("Unparsable line: " + line);
60 private static String parse(String data) {
61 if (data.equals("null")) {
64 if (data.startsWith("'") && data.endsWith("'")) {
65 return data.substring(1, data.length() - 1);
67 throw new Error("Syntax error with literal: " + data);
77 public void testPublicSuffix() {
79 domain = domain.toLowerCase();
81 String publicSuffix = PublicSuffixes.getInstance().getRegistrablePart(domain == null ? null : IDN.toASCII(domain));
82 assertEquals(suffix == null ? null : IDN.toASCII(suffix), publicSuffix);