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 = new BufferedReader(new InputStreamReader(TestPublicSuffixes.class.getResourceAsStream("TestPublicSuffixes.txt"), "UTF-8"));
28 ArrayList<String[]> result = new ArrayList<>();
30 while ((line = br.readLine()) != null) {
31 if (line.startsWith("//") || line.isEmpty()) {
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);
41 result.add(new String[] {
42 parse(parts[0]), parse(parts[1])
45 throw new Error("Unparsable line: " + line);
52 private static String parse(String data) {
53 if (data.equals("null")) {
56 if (data.startsWith("'") && data.endsWith("'")) {
57 return data.substring(1, data.length() - 1);
59 throw new Error("Syntax error with literal: " + data);
69 public void testPublicSuffix() {
71 domain = domain.toLowerCase();
73 String publicSuffix = PublicSuffixes.getInstance().getRegistrablePart(domain == null ? null : IDN.toASCII(domain));
74 assertEquals(suffix == null ? null : IDN.toASCII(suffix), publicSuffix);