Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/InetAddress/GetLocalHostWithSM.java
41149 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
* @library /test/lib
28
* @summary Inet[46]Address.localHost need doPrivileged
29
* @run main/othervm -Djava.security.manager=allow GetLocalHostWithSM
30
* @run main/othervm -Djava.security.manager=allow -Djava.net.preferIPv4Stack=true GetLocalHostWithSM
31
* @run main/othervm -Djava.security.manager=allow -Djava.net.preferIPv6Addresses=true GetLocalHostWithSM
32
* files needed: GetLocalHostWithSM.java, MyPrincipal.java, and policy.file
33
*/
34
35
import java.net.*;
36
37
import javax.security.auth.Subject;
38
import java.security.Principal;
39
import java.security.PrivilegedExceptionAction;
40
import java.util.*;
41
42
import jdk.test.lib.net.IPSupport;
43
44
public class GetLocalHostWithSM {
45
46
public static void main(String[] args) throws Exception {
47
IPSupport.throwSkippedExceptionIfNonOperational();
48
49
// try setting the local hostname
50
InetAddress localHost = InetAddress.getLocalHost();
51
if (localHost.isLoopbackAddress()) {
52
System.err.println("Local host name is resolved into a loopback address. Quit now!");
53
return;
54
}
55
System.setProperty("host.name", localHost.
56
getHostName());
57
String policyFileName = System.getProperty("test.src", ".") +
58
"/" + "policy.file";
59
System.setProperty("java.security.policy", policyFileName);
60
System.setSecurityManager(new SecurityManager());
61
62
InetAddress localHost1 = null;
63
InetAddress localHost2 = null;
64
65
localHost1 = InetAddress.getLocalHost();
66
67
Subject mySubject = new Subject();
68
MyPrincipal userPrincipal = new MyPrincipal("test");
69
mySubject.getPrincipals().add(userPrincipal);
70
localHost2 = (InetAddress)Subject.doAsPrivileged(mySubject,
71
new MyAction(), null);
72
73
if (localHost1.equals(localHost2)) {
74
System.out.println("localHost1 = " + localHost1);
75
throw new RuntimeException("InetAddress.getLocalHost() test " +
76
" fails. localHost2 should be " +
77
" the real address instead of " +
78
" the loopback address."+localHost2);
79
}
80
}
81
}
82
83
84
class MyAction implements PrivilegedExceptionAction {
85
86
public Object run() throws Exception {
87
return InetAddress.getLocalHost();
88
}
89
}
90
91