test/jdk/java/net/InetAddress/InternalNameServiceWithHostsFileTest.java
changeset 54883 4ee117b890c5
parent 47216 71c04702a3d5
equal deleted inserted replaced
54882:b99e97bc5040 54883:4ee117b890c5
    54         byte[] expectedIpv4Address = { 10, 2, 3, 4 };
    54         byte[] expectedIpv4Address = { 10, 2, 3, 4 };
    55         //
    55         //
    56         byte[] expectedIpv6LocalhostAddress = { 0, 0, 0, 0, 0, 0, 0,
    56         byte[] expectedIpv6LocalhostAddress = { 0, 0, 0, 0, 0, 0, 0,
    57                 0, 0, 0, 0, 0, 0, 0, 0, 1 };
    57                 0, 0, 0, 0, 0, 0, 0, 0, 1 };
    58 
    58 
    59         try {
    59         // 10.2.3.4  testHost.testDomain
    60             // 10.2.3.4  testHost.testDomain
    60         testHostsMapping(expectedIpv4Address, "testHost.testDomain");
    61             testHostsMapping(expectedIpv4Address, "testHost.testDomain");
    61         // ::1     ip6-localhost ip6-loopback
    62             // ::1     ip6-localhost ip6-loopback
    62         testHostsMapping(expectedIpv6LocalhostAddress, "ip6-localhost");
    63             testHostsMapping(expectedIpv6LocalhostAddress, "ip6-localhost");
    63         // fe00::0 ip6-localnet
    64             // fe00::0 ip6-localnet
    64         testHostsMapping(expectedIpv6LocalAddress, "ip6-localnet");
    65             testHostsMapping(expectedIpv6LocalAddress, "ip6-localnet");
    65         // fe80::1 link-local-host
    66             // fe80::1 link-local-host
    66         testHostsMapping(expectedIpv6Address, "link-local-host");
    67             testHostsMapping(expectedIpv6Address, "link-local-host");
       
    68 
    67 
    69         } catch (UnknownHostException uhEx) {
    68         testReverseLookup("10.2.3.4", "testHost.testDomain");
    70             System.out.println("UHE unexpected caught == " + uhEx.getMessage());
    69 
    71         }
    70         testReverseLookup("::1", "ip6-localhost");
       
    71         testReverseLookup("0:0:0:0:0:0:0:1", "ip6-localhost");
       
    72         testReverseLookup("0000:0000:0000:0000:0000:0000:0000:0001", "ip6-localhost");
       
    73 
       
    74         testReverseLookup("fe00::0", "ip6-localnet");
       
    75         testReverseLookup("fe00:0:0:0:0:0:0:0", "ip6-localnet");
       
    76         testReverseLookup("fe00:0000:0000:0000:0000:0000:0000:0000", "ip6-localnet");
       
    77 
       
    78         testReverseLookup("fe80::1", "link-local-host");
       
    79         testReverseLookup("fe80:000:0:00:0:000:00:1", "link-local-host");
       
    80         testReverseLookup("fe80:0000:0000:0000:0000:0000:0000:0001", "link-local-host");
    72     }
    81     }
    73 
    82 
    74     private static void testHostsMapping(byte[] expectedIpAddress, String hostName)
    83     private static void testHostsMapping(byte[] expectedIpAddress, String hostName)
    75             throws UnknownHostException {
    84             throws UnknownHostException {
    76         InetAddress testAddress;
    85         InetAddress testAddress;
    92         System.out.println("retrieved address == "
   101         System.out.println("retrieved address == "
    93                 + Arrays.toString(rawIpAddress)
   102                 + Arrays.toString(rawIpAddress)
    94                 + " equal to expected address == "
   103                 + " equal to expected address == "
    95                 + Arrays.toString(expectedIpAddress));
   104                 + Arrays.toString(expectedIpAddress));
    96     }
   105     }
       
   106 
       
   107     private static void testReverseLookup(String numericHost, String expectedName)
       
   108             throws UnknownHostException {
       
   109         String lookupResult = InetAddress.getByName(numericHost).getHostName();
       
   110         if (!expectedName.equals(lookupResult)) {
       
   111             throw new RuntimeException(
       
   112                 String.format(
       
   113                     "reverse lookup of \"%s\" is \"%s\", should be \"%s\"\n",
       
   114                     numericHost, lookupResult, expectedName));
       
   115         }
       
   116     }
    97 }
   117 }