duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2002 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 4531817 |
| 27 | * @summary Inet[46]Address.localHost need doPrivileged |
| 28 | * @run main/othervm GetLocalHostWithSM |
| 29 | * files needed: GetLocalHostWithSM.java, MyPrincipal.java, and policy.file |
| 30 | */ |
| 31 | |
| 32 | import java.net.*; |
| 33 | |
| 34 | import javax.security.auth.Subject; |
| 35 | import java.security.Principal; |
| 36 | import java.security.PrivilegedExceptionAction; |
| 37 | import java.util.*; |
| 38 | |
| 39 | public class GetLocalHostWithSM { |
| 40 | |
| 41 | public static void main(String[] args) throws Exception { |
| 42 | |
| 43 | // try setting the local hostname |
| 44 | try { |
| 45 | System.setProperty("host.name", InetAddress. |
| 46 | getLocalHost(). |
| 47 | getHostName()); |
| 48 | } catch (UnknownHostException e) { |
| 49 | System.out.println("Cannot find the local hostname, " + |
| 50 | "no nameserver entry found"); |
| 51 | } |
| 52 | String policyFileName = System.getProperty("test.src", ".") + |
| 53 | "/" + "policy.file"; |
| 54 | System.setProperty("java.security.policy", policyFileName); |
| 55 | System.setSecurityManager(new SecurityManager()); |
| 56 | |
| 57 | InetAddress localHost1 = null; |
| 58 | InetAddress localHost2 = null; |
| 59 | |
| 60 | localHost1 = InetAddress.getLocalHost(); |
| 61 | |
| 62 | Subject mySubject = new Subject(); |
| 63 | MyPrincipal userPrincipal = new MyPrincipal("test"); |
| 64 | mySubject.getPrincipals().add(userPrincipal); |
| 65 | localHost2 = (InetAddress)Subject.doAsPrivileged(mySubject, |
| 66 | new MyAction(), null); |
| 67 | |
| 68 | if (localHost1.equals(localHost2)) { |
| 69 | throw new RuntimeException("InetAddress.getLocalHost() test " + |
| 70 | " fails. localHost2 should be " + |
| 71 | " the real address instead of " + |
| 72 | " the loopback address."+localHost2); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | |
| 78 | class MyAction implements PrivilegedExceptionAction { |
| 79 | |
| 80 | public Object run() throws Exception { |
| 81 | return InetAddress.getLocalHost(); |
| 82 | } |
| 83 | } |