Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/InetAddress/GetLocalHostWithSM.java
47182 views
1
/*
2
* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/**
25
* @test
26
* @bug 4531817 8026245
27
* @summary Inet[46]Address.localHost need doPrivileged
28
* @run main/othervm GetLocalHostWithSM
29
* @run main/othervm -Djava.net.preferIPv4Stack=true GetLocalHostWithSM
30
* @run main/othervm -Djava.net.preferIPv6Addresses=true GetLocalHostWithSM
31
* files needed: GetLocalHostWithSM.java, MyPrincipal.java, and policy.file
32
*/
33
34
import java.net.*;
35
36
import javax.security.auth.Subject;
37
import java.security.Principal;
38
import java.security.PrivilegedExceptionAction;
39
import java.util.*;
40
41
public class GetLocalHostWithSM {
42
43
public static void main(String[] args) throws Exception {
44
45
// try setting the local hostname
46
InetAddress localHost = InetAddress.getLocalHost();
47
if (localHost.isLoopbackAddress()) {
48
System.err.println("Local host name is resolved into a loopback address. Quit now!");
49
return;
50
}
51
System.setProperty("host.name", localHost.
52
getHostName());
53
String policyFileName = System.getProperty("test.src", ".") +
54
"/" + "policy.file";
55
System.setProperty("java.security.policy", policyFileName);
56
System.setSecurityManager(new SecurityManager());
57
58
InetAddress localHost1 = null;
59
InetAddress localHost2 = null;
60
61
localHost1 = InetAddress.getLocalHost();
62
63
Subject mySubject = new Subject();
64
MyPrincipal userPrincipal = new MyPrincipal("test");
65
mySubject.getPrincipals().add(userPrincipal);
66
localHost2 = (InetAddress)Subject.doAsPrivileged(mySubject,
67
new MyAction(), null);
68
69
if (localHost1.equals(localHost2)) {
70
System.out.println("localHost1 = " + localHost1);
71
throw new RuntimeException("InetAddress.getLocalHost() test " +
72
" fails. localHost2 should be " +
73
" the real address instead of " +
74
" the loopback address."+localHost2);
75
}
76
}
77
}
78
79
80
class MyAction implements PrivilegedExceptionAction {
81
82
public Object run() throws Exception {
83
return InetAddress.getLocalHost();
84
}
85
}
86
87