summaryrefslogtreecommitdiffstats
path: root/luni
diff options
context:
space:
mode:
authorNeil Fuller <nfuller@google.com>2015-07-13 14:31:42 +0000
committerandroid-build-merger <android-build-merger@google.com>2015-07-13 14:31:42 +0000
commit88051ed35a134b2a06a60a414d3a5c1c92575091 (patch)
tree3e8036136adde4c420932270bc17c99b1263a53d /luni
parenta17cd65642e7f47b9ea23721c099d499ed4ec9f3 (diff)
parent2cc187049ed0a5fc6168ffdb8f837b59462e5b53 (diff)
downloadlibcore-88051ed35a134b2a06a60a414d3a5c1c92575091.zip
libcore-88051ed35a134b2a06a60a414d3a5c1c92575091.tar.gz
libcore-88051ed35a134b2a06a60a414d3a5c1c92575091.tar.bz2
Merge "Add testInterfaceProperties() . Move Ethernet verification out of testLoopback()." into kitkat-cts-dev automerge: 19679b0
automerge: 2cc1870 * commit '2cc187049ed0a5fc6168ffdb8f837b59462e5b53': Add testInterfaceProperties() . Move Ethernet verification out of testLoopback().
Diffstat (limited to 'luni')
-rw-r--r--luni/src/test/java/libcore/java/net/NetworkInterfaceTest.java45
1 files changed, 33 insertions, 12 deletions
diff --git a/luni/src/test/java/libcore/java/net/NetworkInterfaceTest.java b/luni/src/test/java/libcore/java/net/NetworkInterfaceTest.java
index b7c4bc6..116eb3f 100644
--- a/luni/src/test/java/libcore/java/net/NetworkInterfaceTest.java
+++ b/luni/src/test/java/libcore/java/net/NetworkInterfaceTest.java
@@ -22,14 +22,18 @@ import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
+import libcore.io.IoUtils;
public class NetworkInterfaceTest extends TestCase {
// http://code.google.com/p/android/issues/detail?id=13784
+ private final static int ARPHRD_ETHER = 1; // from if_arp.h
public void testIPv6() throws Exception {
NetworkInterface lo = NetworkInterface.getByName("lo");
Set<InetAddress> actual = new HashSet<InetAddress>(Collections.list(lo.getInetAddresses()));
@@ -70,24 +74,31 @@ public class NetworkInterfaceTest extends TestCase {
assertEquals(1, ifAddresses.size());
}
+ public void testInterfaceProperties() throws Exception {
+ for (NetworkInterface nif : Collections.list(NetworkInterface.getNetworkInterfaces())) {
+ assertEquals(nif, NetworkInterface.getByName(nif.getName()));
+ // Skip interfaces that are inactive
+ if (nif.isUp() == false) {
+ continue;
+ }
+ // Ethernet
+ if (isEthernet(nif.getName())) {
+ assertEquals(6, nif.getHardwareAddress().length);
+ for (InterfaceAddress ia : nif.getInterfaceAddresses()) {
+ if (ia.getAddress() instanceof Inet4Address) {
+ assertNotNull(ia.getBroadcast());
+ }
+ }
+ }
+ }
+ }
+
public void testLoopback() throws Exception {
- // We know lo shouldn't have a hardware address or an IPv4 broadcast address.
NetworkInterface lo = NetworkInterface.getByName("lo");
assertNull(lo.getHardwareAddress());
for (InterfaceAddress ia : lo.getInterfaceAddresses()) {
assertNull(ia.getBroadcast());
}
-
- // But eth0, if it exists, should...
- NetworkInterface eth0 = NetworkInterface.getByName("eth0");
- if (eth0 != null) {
- assertEquals(6, eth0.getHardwareAddress().length);
- for (InterfaceAddress ia : eth0.getInterfaceAddresses()) {
- if (ia.getAddress() instanceof Inet4Address) {
- assertNotNull(ia.getBroadcast());
- }
- }
- }
}
public void testDumpAll() throws Exception {
@@ -130,4 +141,14 @@ public class NetworkInterfaceTest extends TestCase {
allIndexes.add(nif.getIndex());
}
}
+
+ // Returns true if interface by name ifName is Ethernet
+ private boolean isEthernet(String ifName) throws Exception {
+ String s = IoUtils.readFileAsString("/sys/class/net/" + ifName + "/type").trim();
+ if (s.startsWith("0x")) {
+ return (Integer.parseInt(s.substring(2), 16) == ARPHRD_ETHER);
+ } else {
+ return (Integer.parseInt(s) == ARPHRD_ETHER);
+ }
+ }
}