1 package org.cacert.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 @RunWith(Parameterized.class)
18 public class TestPublicSuffixes {
22 * http://mxr.mozilla.org/mozilla-central/source/netwerk/test/unit
23 * /data/test_psl.txt?raw=1
25 @Parameters(name = "publicSuffix({0}) = {1}")
26 public static Iterable<String[]> genParams() throws IOException {
27 BufferedReader br = null;
29 br = new BufferedReader(new InputStreamReader(TestPublicSuffixes.class.getResourceAsStream("TestPublicSuffixes.txt"), "UTF-8"));
30 ArrayList<String[]> result = new ArrayList<>();
32 while ((line = br.readLine()) != null) {
33 if (line.startsWith("//") || line.isEmpty()) {
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);
43 result.add(new String[] {
44 parse(parts[0]), parse(parts[1])
47 throw new Error("Unparsable line: " + line);
58 private static String parse(String data) {
59 if (data.equals("null")) {
62 if (data.startsWith("'") && data.endsWith("'")) {
63 return data.substring(1, data.length() - 1);
65 throw new Error("Syntax error with literal: " + data);
75 public void testPublicSuffix() {
77 domain = domain.toLowerCase();
79 String publicSuffix = PublicSuffixes.getInstance().getRegistrablePart(domain == null ? null : IDN.toASCII(domain));
80 assertEquals(suffix == null ? null : IDN.toASCII(suffix), publicSuffix);