add StringUtils

This commit is contained in:
Brenton Bostick 2023-01-24 11:50:00 -05:00
parent c6adfd9d67
commit 58e3b8c5ad
3 changed files with 130 additions and 3 deletions

View File

@ -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<VirtualNetworkConf
boolean routesEqual = rCurrent.equals(rNew);
if (this.nwid != cfg.nwid) {
Log.i(TAG, "nwid Changed. Old: " + Long.toHexString(this.nwid) + " (" + Long.toString(this.nwid) + "), " +
"New: " + Long.toHexString(cfg.nwid) + " (" + Long.toString(cfg.nwid) + ")");
Log.i(TAG, "nwid Changed. Old: " + StringUtils.networkIdToString(this.nwid) + " (" + this.nwid + "), " +
"New: " + StringUtils.networkIdToString(cfg.nwid) + " (" + cfg.nwid + ")");
}
if (this.mac != cfg.mac) {
Log.i(TAG, "MAC Changed. Old: " + Long.toHexString(this.mac) + ", New: " + Long.toHexString(cfg.mac));
Log.i(TAG, "MAC Changed. Old: " + StringUtils.macAddressToString(this.mac) + ", New: " + StringUtils.macAddressToString(cfg.mac));
}
if (!this.name.equals(cfg.name)) {

View File

@ -0,0 +1,52 @@
/*
* ZeroTier One - Network Virtualization Everywhere
* Copyright (C) 2011-2023 ZeroTier, Inc. https://www.zerotier.com/
*/
package com.zerotier.sdk.util;
public class StringUtils {
/**
* Convert mac address to string.
*
* @param mac MAC address
* @return string in XX:XX:XX:XX:XX:XX format
*/
public static String macAddressToString(long mac) {
int[] macChars = new int[6];
for (int i = 0; i < 6; i++) {
macChars[i] = (int) (mac % 256);
mac >>= 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);
}
}

View File

@ -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");
}
}