|
1 import java.lang.reflect.Field; |
|
2 import java.util.Hashtable; |
|
3 |
|
4 import javax.naming.Context; |
|
5 import javax.naming.NamingException; |
|
6 import javax.naming.spi.NamingManager; |
|
7 |
|
8 import com.sun.jndi.dns.DnsContext; |
|
9 |
|
10 /** |
|
11 * @test |
|
12 * @bug 6991580 |
|
13 * @summary IPv6 Nameservers in resolv.conf throws NumberFormatException |
|
14 * @run main/manual IPv6NameserverPlatformParsingTest |
|
15 * |
|
16 * In order to run this test be sure to place, for example, the following |
|
17 * snippet into your platform's {@code /etc/resolv.conf}: |
|
18 * <pre> |
|
19 * nameserver 127.0.0.1 |
|
20 * nameserver 2001:4860:4860::8888 |
|
21 * nameserver [::1]:5353 |
|
22 * nameserver 127.0.0.1:5353 |
|
23 * </pre> |
|
24 * |
|
25 * Then, run this test as manual jtreg test. |
|
26 * |
|
27 * @author Severin Gehwolf |
|
28 * |
|
29 */ |
|
30 public class IPv6NameserverPlatformParsingTest { |
|
31 |
|
32 private static boolean foundIPv6 = false; |
|
33 |
|
34 public static void main(String[] args) { |
|
35 Hashtable<String, String> env = new Hashtable<>(); |
|
36 env.put(Context.INITIAL_CONTEXT_FACTORY, com.sun.jndi.dns.DnsContextFactory.class.getName()); |
|
37 |
|
38 String[] servers; |
|
39 try { |
|
40 Context ctx = NamingManager.getInitialContext(env); |
|
41 if (!com.sun.jndi.dns.DnsContextFactory.platformServersAvailable()) { |
|
42 throw new RuntimeException("FAIL: no platform servers available, test does not make sense"); |
|
43 } |
|
44 DnsContext context = (DnsContext)ctx; |
|
45 servers = getServersFromContext(context); |
|
46 } catch (NamingException e) { |
|
47 throw new RuntimeException(e); |
|
48 } |
|
49 for (String server: servers) { |
|
50 System.out.println("DEBUG: 'nameserver = " + server + "'"); |
|
51 if (server.indexOf(':') >= 0 && server.indexOf('.') < 0) { |
|
52 System.out.println("DEBUG: ==> Found IPv6 address in servers list: " + server); |
|
53 foundIPv6 = true; |
|
54 } |
|
55 } |
|
56 try { |
|
57 new com.sun.jndi.dns.DnsClient(servers, 100, 1); |
|
58 } catch (NumberFormatException e) { |
|
59 throw new RuntimeException("FAIL: Tried to parse non-[]-encapsulated IPv6 address.", e); |
|
60 } catch (Exception e) { |
|
61 throw new RuntimeException("ERROR: Something unexpected happened."); |
|
62 } |
|
63 if (!foundIPv6) { |
|
64 // This is a manual test, since it requires changing /etc/resolv.conf on Linux/Unix |
|
65 // platforms. See comment as to how to run this test. |
|
66 throw new RuntimeException("ERROR: No IPv6 address returned from platform."); |
|
67 } |
|
68 System.out.println("PASS: Found IPv6 address and DnsClient parsed it correctly."); |
|
69 } |
|
70 |
|
71 private static String[] getServersFromContext(DnsContext context) { |
|
72 try { |
|
73 Field serversField = DnsContext.class.getDeclaredField("servers"); |
|
74 serversField.setAccessible(true); |
|
75 return (String[])serversField.get(context); |
|
76 } catch (Exception e) { |
|
77 throw new RuntimeException(e); |
|
78 } |
|
79 } |
|
80 |
|
81 } |