Path: blob/master/test/jdk/sun/security/x509/GeneralName/DNSNameTest.java
41152 views
/*1* Copyright (c) 2018, 2020, 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* @summary DNSName parsing tests26* @bug 8213952 818614327* @modules java.base/sun.security.x50928* @run testng DNSNameTest29*/3031import java.io.IOException;32import sun.security.x509.DNSName;3334import org.testng.annotations.DataProvider;35import org.testng.annotations.Test;3637import static org.testng.Assert.*;3839public class DNSNameTest {40@DataProvider(name = "goodNames")41public Object[][] goodNames() {42Object[][] data = {43{"abc.com"},44{"ABC.COM"},45{"a12.com"},46{"a1b2c3.com"},47{"1abc.com"},48{"123.com"},49{"abc.com-"}, // end with hyphen50{"a-b-c.com"}, // hyphens51};52return data;53}5455@DataProvider(name = "goodSanNames")56public Object[][] goodSanNames() {57Object[][] data = {58{"abc.com"},59{"ABC.COM"},60{"a12.com"},61{"a1b2c3.com"},62{"1abc.com"},63{"123.com"},64{"abc.com-"}, // end with hyphen65{"a-b-c.com"}, // hyphens66{"*.domain.com"}, // wildcard in 1st level subdomain67{"*.com"},68};69return data;70}7172@DataProvider(name = "badNames")73public Object[][] badNames() {74Object[][] data = {75{" 1abc.com"}, // begin with space76{"1abc.com "}, // end with space77{"1a bc.com "}, // no space allowed78{"-abc.com"}, // begin with hyphen79{"a..b"}, // ..80{".a"}, // begin with .81{"a."}, // end with .82{""}, // empty83{" "}, // space only84{"*.domain.com"}, // wildcard not allowed85{"a*.com"}, // only allow letter, digit, or hyphen86};87return data;88}8990@DataProvider(name = "badSanNames")91public Object[][] badSanNames() {92Object[][] data = {93{" 1abc.com"}, // begin with space94{"1abc.com "}, // end with space95{"1a bc.com "}, // no space allowed96{"-abc.com"}, // begin with hyphen97{"a..b"}, // ..98{".a"}, // begin with .99{"a."}, // end with .100{""}, // empty101{" "}, // space only102{"*"}, // wildcard only103{"*a.com"}, // partial wildcard disallowed104{"abc.*.com"}, // wildcard not allowed in 2nd level105{"*.*.domain.com"}, // double wildcard not allowed106{"a*.com"}, // only allow letter, digit, or hyphen107};108return data;109}110111112@Test(dataProvider = "goodNames")113public void testGoodDNSName(String dnsNameString) {114try {115DNSName dn = new DNSName(dnsNameString);116} catch (IOException e) {117fail("Unexpected IOException");118}119}120121@Test(dataProvider = "goodSanNames")122public void testGoodSanDNSName(String dnsNameString) {123try {124DNSName dn = new DNSName(dnsNameString, true);125} catch (IOException e) {126fail("Unexpected IOException");127}128}129130@Test(dataProvider = "badNames")131public void testBadDNSName(String dnsNameString) {132try {133DNSName dn = new DNSName(dnsNameString);134fail("IOException expected");135} catch (IOException e) {136if (!e.getMessage().contains("DNSName"))137fail("Unexpeceted message: " + e);138}139}140141@Test(dataProvider = "badSanNames")142public void testBadSanDNSName(String dnsNameString) {143try {144DNSName dn = new DNSName(dnsNameString, true);145fail("IOException expected");146} catch (IOException e) {147if (!e.getMessage().contains("DNSName"))148fail("Unexpeceted message: " + e);149}150}151}152153154