Path: blob/master/test/jdk/java/net/URL/TestIPv6Addresses.java
41149 views
/*1* Copyright (c) 2001, 2010, 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/* @test24* @bug 4451522 446048425* @run main/othervm -Djava.security.manager=allow TestIPv6Addresses26* @summary URI and URL getHost() methods don't comform to RFC 273227*/2829// Run in othervm -Djava.security.manager=allow because the tests sets a SecurityManager3031import java.net.*;3233public class TestIPv6Addresses {34public static void main(String[] args) {35try {36// testing InetAddress static constructors37InetAddress ia1 = InetAddress.getByName("fe80::a00:20ff:feae:45c9");38InetAddress ia2 = InetAddress.getByName("[fe80::a00:20ff:feae:45c9]");39System.out.println("InetAddress: "+ia1+" , "+ia2);40if (!ia1.equals(ia2)) {41throw new RuntimeException("InetAddress.getByName failed for"+42"literal IPv6 addresses");43}44// testing URL constructor, getHost and getAuthority45URL u1 = new URL("http", "fe80::a00:20ff:feae:45c9", 80, "/index.html");46URL u2 = new URL("http", "[fe80::a00:20ff:feae:45c9]", 80, "/index.html");47if (!u1.equals(u2)) {48throw new RuntimeException("URL constructor failed for"+49"literal IPv6 addresses");50}51if (!u1.getHost().equals(u2.getHost()) ||52!u1.getHost().equals("[fe80::a00:20ff:feae:45c9]")) {53throw new RuntimeException("URL.getHost() failed for"+54"literal IPv6 addresses");55}56if (!u1.getAuthority().equals("[fe80::a00:20ff:feae:45c9]:80")) {57throw new RuntimeException("URL.getAuthority() failed for"+58"literal IPv6 addresses");59}6061// need to test URI as well6263// testing SocketPermission to see whether it handles unambiguous cases64// testing getIP and getHost etc65SocketPermission sp1 =66new SocketPermission(u1.getHost()+":80-", "resolve");67SocketPermission sp2 =68new SocketPermission(ia1.getHostAddress()+":8080", "resolve");6970if (!sp1.implies(sp2)) {71throw new RuntimeException("SocketPermission implies doesn't work"+72" for literal IPv6 addresses");73}74} catch (Exception e) {75throw new RuntimeException(e.getMessage());76}77// bug 44604847879SecurityManager sm = new SecurityManager();80String strAddr = "::FFFF:127.0.0.1.2";8182try {83InetAddress addr = InetAddress.getByName(strAddr);84} catch (UnknownHostException e) {85// expected86}87System.setSecurityManager(sm);8889try {90InetAddress addr = InetAddress.getByName(strAddr);91} catch (java.security.AccessControlException e) {92// expected93} catch (UnknownHostException e) {94// expected95}9697}98}99100101