Path: blob/master/test/jdk/sun/security/util/RegisteredDomain/ParseNames.java
66646 views
/*1* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8228969 8244087 825526626* @modules java.base/sun.security.util27* @summary unit test for RegisteredDomain28*/2930import java.io.BufferedReader;31import java.io.InputStreamReader;32import java.io.File;33import java.io.FileInputStream;34import java.util.Objects;35import java.util.Optional;36import sun.security.util.RegisteredDomain;3738public class ParseNames {3940public static void main(String[] args) throws Exception {41String dir = System.getProperty("test.src", ".");42File f = new File(dir, "tests.dat");43try (FileInputStream fis = new FileInputStream(f)) {44InputStreamReader r = new InputStreamReader(fis, "UTF-8");45BufferedReader reader = new BufferedReader(r);4647String s;48int linenumber = 0;49boolean allTestsPass = true;5051while ((s = reader.readLine()) != null) {52linenumber++;53if ("".equals(s) || s.charAt(0) == '#') {54continue;55}56String[] tokens = s.split("\\s+");57if (tokens.length != 3) {58throw new Exception(59String.format("Line %d: test data format incorrect",60linenumber));61}62if (tokens[1].equals("null")) {63tokens[1] = null;64}65if (tokens[2].equals("null")) {66tokens[2] = null;67}68allTestsPass &= runTest(linenumber, tokens[0],69tokens[1], tokens[2]);70}71if (allTestsPass) {72System.out.println("Test passed.");73} else {74throw new Exception("Test failed.");75}76}77}7879private static boolean runTest(int lnum, String target,80String expPubSuffix, String expRegDomain) {8182System.out.println("target:" + target);83Optional<RegisteredDomain> rd = RegisteredDomain.from(target);84String regName = rd.map(RegisteredDomain::name).orElse(null);85if (!Objects.equals(expRegDomain, regName)) {86System.out.printf(87"Line %d: %s, Expected registered domain: %s, Got: %s\n",88lnum, target, expRegDomain, regName);89return false;90}9192if (expRegDomain == null) {93return true;94}9596String pubSuffix = rd.map(RegisteredDomain::publicSuffix).orElse(null);97if (!Objects.equals(expPubSuffix, pubSuffix)) {98System.out.printf(99"Line %d: %s, Expected public suffix: %s, Got: %s\n",100lnum, target, expPubSuffix, pubSuffix);101return false;102}103104return true;105}106}107108109