author | coleenp |
Wed, 17 Jun 2015 21:44:48 +0000 | |
changeset 31362 | 8957ccbb5821 |
parent 26595 | 1f96f8175e05 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
17206
diff
changeset
|
2 |
* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
17206
3dfa000764e9
6594296: NetworkInterface.getHardwareAddress returns zero length byte array
chegar
parents:
5506
diff
changeset
|
24 |
/* @test |
26595
1f96f8175e05
8058216: NetworkInterface.getHardwareAddress can return zero length byte array when run with preferIPv4Stack
chegar
parents:
23591
diff
changeset
|
25 |
* @bug 4405354 6594296 8058216 |
1f96f8175e05
8058216: NetworkInterface.getHardwareAddress can return zero length byte array when run with preferIPv4Stack
chegar
parents:
23591
diff
changeset
|
26 |
* @run main Test |
1f96f8175e05
8058216: NetworkInterface.getHardwareAddress can return zero length byte array when run with preferIPv4Stack
chegar
parents:
23591
diff
changeset
|
27 |
* @run main/othervm -Djava.net.preferIPv4Stack=true Test |
17206
3dfa000764e9
6594296: NetworkInterface.getHardwareAddress returns zero length byte array
chegar
parents:
5506
diff
changeset
|
28 |
* @summary Basic tests for NetworkInterface |
2 | 29 |
*/ |
30 |
import java.net.NetworkInterface; |
|
31 |
import java.net.InetAddress; |
|
32 |
import java.util.Enumeration; |
|
33 |
||
34 |
public class Test { |
|
23591
1b50db37efd3
8038276: java/net/NetworkInterface/Test.java fails on Windows intermittently for Teredo Interface
chegar
parents:
23010
diff
changeset
|
35 |
static final boolean isWindows = System.getProperty("os.name").startsWith("Windows"); |
2 | 36 |
|
37 |
public static void main(String args[]) throws Exception { |
|
38 |
||
39 |
Enumeration nifs = NetworkInterface.getNetworkInterfaces(); |
|
40 |
||
41 |
while (nifs.hasMoreElements()) { |
|
42 |
NetworkInterface ni = (NetworkInterface)nifs.nextElement(); |
|
43 |
||
23591
1b50db37efd3
8038276: java/net/NetworkInterface/Test.java fails on Windows intermittently for Teredo Interface
chegar
parents:
23010
diff
changeset
|
44 |
//JDK-8038276: Should not test on Windows with Teredo Tunneling Pseudo-Interface |
1b50db37efd3
8038276: java/net/NetworkInterface/Test.java fails on Windows intermittently for Teredo Interface
chegar
parents:
23010
diff
changeset
|
45 |
String dName = ni.getDisplayName(); |
1b50db37efd3
8038276: java/net/NetworkInterface/Test.java fails on Windows intermittently for Teredo Interface
chegar
parents:
23010
diff
changeset
|
46 |
if (isWindows && dName != null && dName.contains("Teredo")) |
1b50db37efd3
8038276: java/net/NetworkInterface/Test.java fails on Windows intermittently for Teredo Interface
chegar
parents:
23010
diff
changeset
|
47 |
continue; |
1b50db37efd3
8038276: java/net/NetworkInterface/Test.java fails on Windows intermittently for Teredo Interface
chegar
parents:
23010
diff
changeset
|
48 |
|
2 | 49 |
String name = ni.getName(); |
50 |
System.out.println("\n" + name); |
|
51 |
||
52 |
/* |
|
53 |
* Enumeration the IP addresses on this interface |
|
54 |
*/ |
|
55 |
Enumeration addrs = ni.getInetAddresses(); |
|
56 |
while (addrs.hasMoreElements()) { |
|
57 |
InetAddress addr = (InetAddress)addrs.nextElement(); |
|
58 |
System.out.println(addr); |
|
59 |
if (NetworkInterface.getByInetAddress(addr) == null) { |
|
60 |
throw new Exception("getByInetAddress returned null"); |
|
61 |
} |
|
62 |
} |
|
63 |
System.out.println("getInetAddresses() test passed."); |
|
64 |
||
65 |
/* |
|
66 |
* Check equals and hashCode contract |
|
67 |
*/ |
|
68 |
NetworkInterface ni2 = NetworkInterface.getByName(name); |
|
69 |
if (!ni2.equals(ni)) { |
|
70 |
throw new Exception("getByName returned: " + ni2); |
|
71 |
} |
|
72 |
if (!ni.equals(ni2)) { |
|
73 |
throw new Exception("equals specification broken"); |
|
74 |
} |
|
75 |
System.out.println("equals() tests passed."); |
|
76 |
if (ni2.hashCode() != ni.hashCode()) { |
|
77 |
throw new Exception("hashCode contract broken"); |
|
78 |
} |
|
79 |
System.out.println("hashCode() test passed."); |
|
17206
3dfa000764e9
6594296: NetworkInterface.getHardwareAddress returns zero length byte array
chegar
parents:
5506
diff
changeset
|
80 |
|
3dfa000764e9
6594296: NetworkInterface.getHardwareAddress returns zero length byte array
chegar
parents:
5506
diff
changeset
|
81 |
byte[] ba = ni.getHardwareAddress(); |
3dfa000764e9
6594296: NetworkInterface.getHardwareAddress returns zero length byte array
chegar
parents:
5506
diff
changeset
|
82 |
if (ba != null && ba.length == 0) { |
3dfa000764e9
6594296: NetworkInterface.getHardwareAddress returns zero length byte array
chegar
parents:
5506
diff
changeset
|
83 |
throw new Exception("getHardwareAddress returned 0 length byte array"); |
3dfa000764e9
6594296: NetworkInterface.getHardwareAddress returns zero length byte array
chegar
parents:
5506
diff
changeset
|
84 |
} |
3dfa000764e9
6594296: NetworkInterface.getHardwareAddress returns zero length byte array
chegar
parents:
5506
diff
changeset
|
85 |
System.out.println("getHardwareAddress() test passed."); |
2 | 86 |
} |
87 |
||
88 |
// misc tests :- |
|
89 |
// getByXXX(null) should throw NPE |
|
90 |
// getByXXX("garbage") should return null |
|
91 |
||
92 |
System.out.println("\nMiscellenous tests: "); |
|
93 |
||
94 |
try { |
|
95 |
NetworkInterface.getByName(null); |
|
96 |
} catch (NullPointerException npe) { |
|
97 |
} |
|
98 |
System.out.println("getByName(null) test passed."); |
|
99 |
||
100 |
try { |
|
101 |
NetworkInterface.getByInetAddress(null); |
|
102 |
} catch (NullPointerException npe) { |
|
103 |
} |
|
104 |
System.out.println("getByInetAddress(null) test passed."); |
|
105 |
||
106 |
if (NetworkInterface.getByName("not-a-valid-name") != null) { |
|
107 |
throw new Exception |
|
108 |
("getByName returned unexpected interface: null expected"); |
|
109 |
} |
|
110 |
System.out.println("getByName(<unknown>) test passed."); |
|
111 |
||
112 |
InetAddress ia = InetAddress.getByName("255.255.255.255"); |
|
113 |
if (NetworkInterface.getByInetAddress(ia) != null) { |
|
114 |
throw new Exception |
|
115 |
("getByInetAddress returned unexpected interface: null expected"); |
|
116 |
} |
|
117 |
System.out.println("getByName(getByInetAddress(<unknown>) test passed."); |
|
118 |
||
119 |
} |
|
120 |
} |