Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,19 @@ public List<String> getMacAddress() {
if (this.macAddressList != null) {
return this.macAddressList;
}
this.macAddressList = new ArrayList<>();
List<String> list = new ArrayList<>();
List<InetAddress> inetAddresses = this.getLocalAllInetAddress();
if (inetAddresses != null && !inetAddresses.isEmpty()) {
// Get the Mac address of all network interfaces
List<String> list = new ArrayList<>();
Set<String> uniqueValues = new HashSet<>();
for (InetAddress inetAddress : inetAddresses) {
String macByInetAddress = this.getMacByInetAddress(inetAddress);
if (uniqueValues.add(macByInetAddress)) {
String macByInetAddress = this.getMacByInetAddress(inetAddress, false);
if (macByInetAddress != null && uniqueValues.add(macByInetAddress)) {
list.add(macByInetAddress);
}
}
this.macAddressList = list;
}
this.macAddressList = list;
return this.macAddressList;
}

Expand Down Expand Up @@ -99,6 +98,10 @@ public List<InetAddress> getLocalAllInetAddress() {
}

public String getMacByInetAddress(InetAddress inetAddr) {
return this.getMacByInetAddress(inetAddr, true);
}

private String getMacByInetAddress(InetAddress inetAddr, boolean required) {
byte[] mac;
try {
mac = NetworkInterface.getByInetAddress(inetAddr)
Expand All @@ -108,6 +111,12 @@ public String getMacByInetAddress(InetAddress inetAddr) {
"Failed to get mac address for inet address '%s'",
inetAddr));
}
if (mac == null) {
if (required) {
throw new IllegalStateException("Network interface has no hardware address");
}
return null;
}

StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
package org.apache.hugegraph.unit.license;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

import org.junit.Assume;
import org.junit.Test;

import org.apache.hugegraph.license.MachineInfo;
Expand Down Expand Up @@ -73,15 +77,89 @@ public void testGetLocalAllInetAddress() {
}

@Test
public void testGetMacByInetAddress() throws UnknownHostException {
public void testGetMacByInetAddress() throws Exception {
List<InetAddress> addressList = machineInfo.getLocalAllInetAddress();
for (InetAddress address : addressList) {
String mac = machineInfo.getMacByInetAddress(address);
Assert.assertTrue(MAC_PATTERN.matcher(mac).matches());
byte[] hardwareAddress = NetworkInterface.getByInetAddress(address)
.getHardwareAddress();
if (hardwareAddress == null) {
Throwable exception = Assert.assertThrows(RuntimeException.class,
() -> machineInfo.getMacByInetAddress(address));
Assert.assertFalse(exception instanceof NullPointerException);
} else {
String mac = machineInfo.getMacByInetAddress(address);
Assert.assertTrue(MAC_PATTERN.matcher(mac).matches());
}
}
InetAddress address = InetAddress.getByAddress(new byte[]{0, 0, 0, 0});
Assert.assertThrows(RuntimeException.class, () -> {
machineInfo.getMacByInetAddress(address);
});
Throwable exception = Assert.assertThrows(RuntimeException.class,
() -> machineInfo.getMacByInetAddress(address));
Assert.assertFalse(exception instanceof NullPointerException);
}

@Test
public void testGetMacAddressWithoutMac() throws SocketException {
InetAddress loopback = InetAddress.getLoopbackAddress();
Assume.assumeTrue(NetworkInterface.getByInetAddress(loopback)
.getHardwareAddress() == null);
MachineInfo info = new MachineInfo() {
@Override
public List<InetAddress> getLocalAllInetAddress() {
return Collections.singletonList(loopback);
}
};
Throwable exception = Assert.assertThrows(RuntimeException.class,
() -> info.getMacByInetAddress(loopback));
Assert.assertFalse(exception instanceof NullPointerException);
Assert.assertEquals("Network interface has no hardware address", exception.getMessage());
List<String> addresses = info.getMacAddress();
Assert.assertTrue(addresses.isEmpty());
Assert.assertSame(addresses, info.getMacAddress());
}

@Test
public void testGetMacAddressSkipsMissingAndDuplicateMac() throws SocketException {
InetAddress loopback = InetAddress.getLoopbackAddress();
Assume.assumeTrue(NetworkInterface.getByInetAddress(loopback)
.getHardwareAddress() == null);
InetAddress addressWithMac = null;
for (InetAddress address : machineInfo.getLocalAllInetAddress()) {
if (NetworkInterface.getByInetAddress(address).getHardwareAddress() != null) {
addressWithMac = address;
break;
}
}
Assume.assumeNotNull(addressWithMac);
InetAddress address = addressWithMac;
MachineInfo info = new MachineInfo() {
@Override
public List<InetAddress> getLocalAllInetAddress() {
return Arrays.asList(loopback, address, loopback, address);
}
};
List<String> addresses = info.getMacAddress();
Assert.assertEquals(1, addresses.size());
Assert.assertTrue(addresses.contains(machineInfo.getMacByInetAddress(address)));
Assert.assertSame(addresses, info.getMacAddress());
}

@Test
public void testGetMacAddressRetriesAfterLookupFailure() {
RuntimeException failure = new RuntimeException("Controlled interface enumeration failure");
int[] attempts = {0};
MachineInfo info = new MachineInfo() {
@Override
public List<InetAddress> getLocalAllInetAddress() {
if (++attempts[0] == 1) {
throw failure;
}
return Collections.emptyList();
}
};
Assert.assertSame(failure, Assert.assertThrows(RuntimeException.class, info::getMacAddress));
List<String> addresses = info.getMacAddress();
Assert.assertTrue(addresses.isEmpty());
Assert.assertSame(addresses, info.getMacAddress());
Assert.assertEquals(2, attempts[0]);
}
}
Loading