diff --git a/java/src/com/zerotier/sdk/VirtualNetworkConfig.java b/java/src/com/zerotier/sdk/VirtualNetworkConfig.java index c7b48d5c5..0236f813c 100644 --- a/java/src/com/zerotier/sdk/VirtualNetworkConfig.java +++ b/java/src/com/zerotier/sdk/VirtualNetworkConfig.java @@ -29,6 +29,8 @@ package com.zerotier.sdk; import android.util.Log; +import com.zerotier.sdk.util.StringUtils; + import java.lang.Comparable; import java.lang.Override; import java.lang.String; @@ -88,11 +90,11 @@ public final class VirtualNetworkConfig implements Comparable>= 8; + } + + return String.format("%02x:%02x:%02x:%02x:%02x:%02x", macChars[5], macChars[4], macChars[3], macChars[2], macChars[1], macChars[0]); + } + + /** + * Convert long to hex string. + * + * @param networkId long + * @return string with 0 padding + */ + public static String networkIdToString(long networkId) { + return String.format("%016x", networkId); + } + + /** + * Convert node address to string. + * + * Node addresses are 40 bits, so print 10 hex characters. + * + * @param address Node address + * @return formatted string + */ + public static String addressToString(long address) { + return String.format("%010x", address); + } + + public static String etherTypeToString(long etherType) { + return String.format("%04x", etherType); + } +} diff --git a/java/test/com/zerotier/sdk/util/StringUtilsTest.java b/java/test/com/zerotier/sdk/util/StringUtilsTest.java new file mode 100644 index 000000000..257b14a99 --- /dev/null +++ b/java/test/com/zerotier/sdk/util/StringUtilsTest.java @@ -0,0 +1,73 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2023 ZeroTier, Inc. https://www.zerotier.com/ + */ + +package com.zerotier.sdk.util; + +import static com.google.common.truth.Truth.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class StringUtilsTest { + + public StringUtilsTest() { + } + + public String oldMacDisplay(long mac) { + + String macStr = Long.toHexString(mac); + + if (macStr.length() > 12) { + throw new RuntimeException(); + } + + while (macStr.length() < 12) { + //noinspection StringConcatenationInLoop + macStr = "0" + macStr; + } + + //noinspection StringBufferReplaceableByString + StringBuilder displayMac = new StringBuilder(); + displayMac.append(macStr.charAt(0)); + displayMac.append(macStr.charAt(1)); + displayMac.append(':'); + displayMac.append(macStr.charAt(2)); + displayMac.append(macStr.charAt(3)); + displayMac.append(':'); + displayMac.append(macStr.charAt(4)); + displayMac.append(macStr.charAt(5)); + displayMac.append(':'); + displayMac.append(macStr.charAt(6)); + displayMac.append(macStr.charAt(7)); + displayMac.append(':'); + displayMac.append(macStr.charAt(8)); + displayMac.append(macStr.charAt(9)); + displayMac.append(':'); + displayMac.append(macStr.charAt(10)); + displayMac.append(macStr.charAt(11)); + + return displayMac.toString(); + } + + @Test + public void testMacDisplay() { + + long mac1 = 1234567891; + assertThat(StringUtils.macAddressToString(mac1)).isEqualTo(oldMacDisplay(mac1)); + + long mac2 = 999999999; + assertThat(StringUtils.macAddressToString(mac2)).isEqualTo(oldMacDisplay(mac2)); + + long mac3 = 0x7fffffffffffL; + assertThat(StringUtils.macAddressToString(mac3)).isEqualTo(oldMacDisplay(mac3)); + assertThat(StringUtils.macAddressToString(mac3)).isEqualTo("7f:ff:ff:ff:ff:ff"); + + long mac4 = 0x7fafcf3f8fffL; + assertThat(StringUtils.macAddressToString(mac4)).isEqualTo(oldMacDisplay(mac4)); + assertThat(StringUtils.macAddressToString(mac4)).isEqualTo("7f:af:cf:3f:8f:ff"); + } +}