jdk/test/sun/security/krb5/auto/NoAddresses.java
changeset 36967 d041d2e80712
parent 31474 7b79555b8073
child 39049 1108f7f41940
--- a/jdk/test/sun/security/krb5/auto/NoAddresses.java	Sat Apr 09 20:12:13 2016 +0100
+++ b/jdk/test/sun/security/krb5/auto/NoAddresses.java	Mon Apr 11 03:00:50 2016 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2016 Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -24,6 +24,7 @@
 /*
  * @test
  * @bug 7032354
+ * @run main/othervm NoAddresses setup
  * @run main/othervm NoAddresses 1
  * @run main/othervm NoAddresses 2
  * @run main/othervm/fail NoAddresses 3
@@ -34,12 +35,24 @@
 import org.ietf.jgss.ChannelBinding;
 import sun.security.jgss.GSSUtil;
 import sun.security.krb5.Config;
+import java.io.PrintWriter;
+import java.io.FileWriter;
+import java.io.BufferedWriter;
 
 public class NoAddresses {
 
     public static void main(String[] args)
             throws Exception {
 
+        if (args[0].equalsIgnoreCase("setup")) {
+            // add a mapping of test host name to 127.0.0.1 to test's hosts file
+            InetAddress localHost = InetAddress.getLocalHost();
+            String localHostName = localHost.getHostName();
+            String hostsFileName = System.getProperty("test.src", ".") + "/TestHosts";
+            String loopBackAddress = "127.0.0.1";
+            System.setProperty("jdk.net.hosts.file", hostsFileName);
+            addMappingToHostsFile(localHostName, loopBackAddress, hostsFileName, true);
+        } else {
         OneKDC kdc = new OneKDC(null);
         kdc.writeJAASConf();
         KDC.saveConfig(OneKDC.KRB5_CONF, kdc,
@@ -79,3 +92,16 @@
         Context.handshake(c, s);
     }
 }
+
+    private static void addMappingToHostsFile (String host,
+                                               String addr,
+                                               String hostsFileName,
+                                               boolean append)
+                                             throws Exception {
+        String mapping = addr + " " + host;
+        try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter(
+                new FileWriter(hostsFileName, append)))) {
+            hfPWriter.println(mapping);
+        }
+    }
+}