test/jdk/sun/security/util/RegisteredDomain/ParseNames.java
changeset 58830 fa0b9f9c597a
equal deleted inserted replaced
58829:13ad9a1bac18 58830:fa0b9f9c597a
       
     1 /*
       
     2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 8228969
       
    27  * @modules java.base/sun.security.util
       
    28  * @summary unit test for RegisteredDomain
       
    29  */
       
    30 
       
    31 import java.io.BufferedReader;
       
    32 import java.io.InputStreamReader;
       
    33 import java.io.File;
       
    34 import java.io.FileInputStream;
       
    35 import java.util.Objects;
       
    36 import java.util.Optional;
       
    37 import sun.security.util.RegisteredDomain;
       
    38 
       
    39 public class ParseNames {
       
    40 
       
    41     public static void main(String[] args) throws Exception {
       
    42         String dir = System.getProperty("test.src", ".");
       
    43         File f = new File(dir, "tests.dat");
       
    44         try (FileInputStream fis = new FileInputStream(f)) {
       
    45             InputStreamReader r = new InputStreamReader(fis, "UTF-8");
       
    46             BufferedReader reader = new BufferedReader(r);
       
    47 
       
    48             String s;
       
    49             int linenumber = 0;
       
    50             boolean allTestsPass = true;
       
    51 
       
    52             while ((s = reader.readLine()) != null) {
       
    53                 linenumber++;
       
    54                 if ("".equals(s) || s.charAt(0) == '#') {
       
    55                     continue;
       
    56                 }
       
    57                 String[] tokens = s.split("\\s+");
       
    58                 if (tokens.length != 3) {
       
    59                     throw new Exception(
       
    60                         String.format("Line %d: test data format incorrect",
       
    61                                       linenumber));
       
    62                 }
       
    63                 if (tokens[1].equals("null")) {
       
    64                     tokens[1] = null;
       
    65                 }
       
    66                 if (tokens[2].equals("null")) {
       
    67                     tokens[2] = null;
       
    68                 }
       
    69                 allTestsPass &= runTest(linenumber, tokens[0],
       
    70                                         tokens[1], tokens[2]);
       
    71             }
       
    72             if (allTestsPass) {
       
    73                 System.out.println("Test passed.");
       
    74             } else {
       
    75                 throw new Exception("Test failed.");
       
    76             }
       
    77         }
       
    78     }
       
    79 
       
    80     private static boolean runTest(int lnum, String target,
       
    81                                    String expPubSuffix, String expRegDomain) {
       
    82 
       
    83         System.out.println("target:" + target);
       
    84         Optional<RegisteredDomain> rd = RegisteredDomain.from(target);
       
    85         String regName = rd.map(RegisteredDomain::name).orElse(null);
       
    86         if (!Objects.equals(expRegDomain, regName)) {
       
    87             System.out.printf(
       
    88                 "Line %d: %s, Expected registered domain: %s, Got: %s\n",
       
    89                 lnum, target, expRegDomain, regName);
       
    90             return false;
       
    91         }
       
    92 
       
    93         if (expRegDomain == null) {
       
    94             return true;
       
    95         }
       
    96 
       
    97         String pubSuffix = rd.map(RegisteredDomain::publicSuffix).orElse(null);
       
    98         if (!Objects.equals(expPubSuffix, pubSuffix)) {
       
    99             System.out.printf(
       
   100                 "Line %d: %s, Expected public suffix: %s, Got: %s\n",
       
   101                 lnum, target, expPubSuffix, pubSuffix);
       
   102             return false;
       
   103         }
       
   104 
       
   105         return true;
       
   106     }
       
   107 }