1 /*
2  * Copyright (c) 2011, 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 /* @test
25  * @bug 7003398
26  * @run main/othervm -Djava.security.manager=allow Equals
27  */
28 
29 import java.io.ByteArrayOutputStream;
30 import java.io.PrintStream;
31 import java.net.InetAddress;
32 import java.net.NetworkInterface;
33 import java.net.SocketException;
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.Enumeration;
37 import java.util.HashMap;
38 
39 public class Equals {
40 
41     static final boolean isWindows = System.getProperty("os.name").startsWith("Windows");
42 
main(String args[])43     public static void main(String args[]) throws Exception {
44         ByteArrayOutputStream baos = new ByteArrayOutputStream();
45         PrintStream bufferedOut = new PrintStream(baos);
46 
47         Enumeration<NetworkInterface> nifs1 = NetworkInterface.getNetworkInterfaces();
48         HashMap<String,Integer> hashes = new HashMap<>();
49         HashMap<String,NetworkInterface> nicMap = new HashMap<>();
50 
51         while (nifs1.hasMoreElements()) {
52             NetworkInterface ni = nifs1.nextElement();
53             hashes.put(ni.getName(),ni.hashCode());
54             nicMap.put(ni.getName(),ni);
55             displayInterfaceInformation(ni, bufferedOut);
56             bufferedOut.flush();
57         }
58 
59         System.setSecurityManager(new SecurityManager());
60 
61         Enumeration<NetworkInterface> nifs2 = NetworkInterface.getNetworkInterfaces();
62         while (nifs2.hasMoreElements()) {
63             NetworkInterface ni = nifs2.nextElement();
64 
65             // JDK-8022963, Skip (Windows)Teredo Tunneling Pseudo-Interface
66             String dName = ni.getDisplayName();
67             if (isWindows && dName != null && dName.contains("Teredo"))
68                 continue;
69 
70             NetworkInterface niOrig = nicMap.get(ni.getName());
71 
72             int h = ni.hashCode();
73             if (h != hashes.get(ni.getName())) {
74                 System.out.printf("%nSystem information:%n");
75                 System.out.printf("%s", baos.toString("UTF8"));
76                 System.out.printf("%nni.hashCode() returned %d, expected %d, for:%n",
77                                   h, hashes.get(ni.getName()));
78                 displayInterfaceInformation(ni, System.out);
79                 throw new RuntimeException("Hashcodes different for " +
80                         ni.getName());
81             }
82             if (!ni.equals(niOrig)) {
83                 System.out.printf("%nSystem information:%n");
84                 System.out.printf("%s", baos.toString("UTF8"));
85                 System.out.printf("%nExpected the following interfaces to be the same:%n");
86                 displayInterfaceInformation(niOrig, System.out);
87                 displayInterfaceInformation(ni, System.out);
88                 throw new RuntimeException("equality different for " +
89                         ni.getName());
90             }
91         }
92     }
93 
displayInterfaceInformation(NetworkInterface netint, PrintStream out)94     static void displayInterfaceInformation(NetworkInterface netint,
95                                             PrintStream out) throws SocketException {
96         out.printf("Display name: %s%n", netint.getDisplayName());
97         out.printf("Name: %s%n", netint.getName());
98         Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
99 
100         for (InetAddress inetAddress : Collections.list(inetAddresses))
101             out.printf("InetAddress: %s%n", inetAddress);
102 
103         out.printf("Up? %s%n", netint.isUp());
104         out.printf("Loopback? %s%n", netint.isLoopback());
105         out.printf("PointToPoint? %s%n", netint.isPointToPoint());
106         out.printf("Supports multicast? %s%n", netint.supportsMulticast());
107         out.printf("Virtual? %s%n", netint.isVirtual());
108         out.printf("Hardware address: %s%n",
109                     Arrays.toString(netint.getHardwareAddress()));
110         out.printf("MTU: %s%n", netint.getMTU());
111         out.printf("Index: %s%n", netint.getIndex());
112         out.printf("%n");
113      }
114 }
115 
116