Path: blob/master/test/jdk/java/net/InetAddress/GetLocalHostWithSM.java
41149 views
/*1* Copyright (c) 2002, 2013, 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 4531817 802624526* @library /test/lib27* @summary Inet[46]Address.localHost need doPrivileged28* @run main/othervm -Djava.security.manager=allow GetLocalHostWithSM29* @run main/othervm -Djava.security.manager=allow -Djava.net.preferIPv4Stack=true GetLocalHostWithSM30* @run main/othervm -Djava.security.manager=allow -Djava.net.preferIPv6Addresses=true GetLocalHostWithSM31* files needed: GetLocalHostWithSM.java, MyPrincipal.java, and policy.file32*/3334import java.net.*;3536import javax.security.auth.Subject;37import java.security.Principal;38import java.security.PrivilegedExceptionAction;39import java.util.*;4041import jdk.test.lib.net.IPSupport;4243public class GetLocalHostWithSM {4445public static void main(String[] args) throws Exception {46IPSupport.throwSkippedExceptionIfNonOperational();4748// try setting the local hostname49InetAddress localHost = InetAddress.getLocalHost();50if (localHost.isLoopbackAddress()) {51System.err.println("Local host name is resolved into a loopback address. Quit now!");52return;53}54System.setProperty("host.name", localHost.55getHostName());56String policyFileName = System.getProperty("test.src", ".") +57"/" + "policy.file";58System.setProperty("java.security.policy", policyFileName);59System.setSecurityManager(new SecurityManager());6061InetAddress localHost1 = null;62InetAddress localHost2 = null;6364localHost1 = InetAddress.getLocalHost();6566Subject mySubject = new Subject();67MyPrincipal userPrincipal = new MyPrincipal("test");68mySubject.getPrincipals().add(userPrincipal);69localHost2 = (InetAddress)Subject.doAsPrivileged(mySubject,70new MyAction(), null);7172if (localHost1.equals(localHost2)) {73System.out.println("localHost1 = " + localHost1);74throw new RuntimeException("InetAddress.getLocalHost() test " +75" fails. localHost2 should be " +76" the real address instead of " +77" the loopback address."+localHost2);78}79}80}818283class MyAction implements PrivilegedExceptionAction {8485public Object run() throws Exception {86return InetAddress.getLocalHost();87}88}899091