Path: blob/master/test/jdk/javax/net/ssl/ServerName/IllegalSNIName.java
41152 views
/*1* Copyright (c) 2013, 2021, 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 8020842 826196926* @summary SNIHostName does not throw IAE when hostname doesn't conform to27* RFC 3490 or ends with a trailing dot28*/2930import javax.net.ssl.SNIHostName;31import java.nio.charset.StandardCharsets;32import java.util.HexFormat;3334public class IllegalSNIName {3536private static void checkHostname(String hostname) throws Exception {37try {38new SNIHostName(hostname);39throw new RuntimeException("Expected to get IllegalArgumentException for "40+ hostname);41} catch (IllegalArgumentException iae) {42// That's the right behavior.43}44}4546private static void checkHostname(byte[] encodedHostname) throws Exception {47try {48new SNIHostName(encodedHostname);49throw new RuntimeException("Expected to get IllegalArgumentException for "50+ HexFormat.ofDelimiter(":").formatHex(encodedHostname));51} catch (IllegalArgumentException iae) {52// That's the right behavior.53}54}5556public static void main(String[] args) throws Exception {57String[] illegalNames = {58"example\u3002\u3002com",59"example..com",60"com\u3002",61"com.",62".",63"example^com"64};6566for (String name : illegalNames) {67checkHostname(name);68checkHostname(name.getBytes(StandardCharsets.UTF_8));69}70}71}727374