Fixed issues with mac address conversion + added unit test
This commit is contained in:
parent
d3bf811b21
commit
bdbb8da05f
|
@ -242,12 +242,13 @@ object Mac {
|
||||||
* Converts a long into a properly formatted lower-case string
|
* Converts a long into a properly formatted lower-case string
|
||||||
*/
|
*/
|
||||||
fun toStringLowerCase(mac: Long): String {
|
fun toStringLowerCase(mac: Long): String {
|
||||||
// we only use the right-most 6 bytes (of 8 bytes).
|
|
||||||
val macBytes = toBytes(mac)
|
val macBytes = toBytes(mac)
|
||||||
|
|
||||||
// mac should have 6 bytes.
|
// mac should have AT LEAST bytes.
|
||||||
val buf = StringBuilder()
|
val buf = StringBuilder()
|
||||||
(0..8).forEach { index ->
|
|
||||||
|
// we only use the right-most 6 bytes (of 8 bytes).
|
||||||
|
(2..7).forEach { index ->
|
||||||
val byte = macBytes[index]
|
val byte = macBytes[index]
|
||||||
if (buf.isNotEmpty()) {
|
if (buf.isNotEmpty()) {
|
||||||
buf.append(':')
|
buf.append(':')
|
||||||
|
@ -257,7 +258,7 @@ object Mac {
|
||||||
buf.append('0')
|
buf.append('0')
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.append(Integer.toHexString(byte.toInt() and 0xFF))
|
buf.append(Integer.toHexString(byte.toUByte().toInt()))
|
||||||
}
|
}
|
||||||
|
|
||||||
return buf.toString()
|
return buf.toString()
|
||||||
|
@ -268,9 +269,9 @@ object Mac {
|
||||||
// we only use the right-most 6 bytes.
|
// we only use the right-most 6 bytes.
|
||||||
val macBytes = toBytes(mac)
|
val macBytes = toBytes(mac)
|
||||||
|
|
||||||
// mac should have 6 bytes.
|
// we only use the right-most 6 bytes (of 8 bytes).
|
||||||
var bytesWritten = 0
|
var bytesWritten = 0
|
||||||
(0..8).forEach { index ->
|
(2..7).forEach { index ->
|
||||||
val byte = macBytes[index]
|
val byte = macBytes[index]
|
||||||
|
|
||||||
if (bytesWritten != 0) {
|
if (bytesWritten != 0) {
|
||||||
|
@ -346,7 +347,12 @@ object Mac {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun toLong(mac: ByteArray): Long {
|
fun toLong(mac: ByteArray): Long {
|
||||||
return ((mac[5].toLong() and 0xff) + (mac[4].toLong() and 0xff shl 8) + (mac[3].toLong() and 0xff shl 16) + (mac[2].toLong() and 0xff shl 24) + (mac[1].toLong() and 0xff shl 32) + (mac[0].toLong() and 0xff shl 40))
|
return ((mac[5].toLong() and 0xff) +
|
||||||
|
(mac[4].toLong() and 0xff shl 8) +
|
||||||
|
(mac[3].toLong() and 0xff shl 16) +
|
||||||
|
(mac[2].toLong() and 0xff shl 24) +
|
||||||
|
(mac[1].toLong() and 0xff shl 32) +
|
||||||
|
(mac[0].toLong() and 0xff shl 40))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun toLong(mac: String, delimiter: MacDelimiter? = MacDelimiter.COLON): Long {
|
fun toLong(mac: String, delimiter: MacDelimiter? = MacDelimiter.COLON): Long {
|
||||||
|
@ -370,8 +376,7 @@ object Mac {
|
||||||
|
|
||||||
for (i in 0 until MAC_ADDRESS_LENGTH) {
|
for (i in 0 until MAC_ADDRESS_LENGTH) {
|
||||||
val element = elements[i]
|
val element = elements[i]
|
||||||
addressInBytes[i] = element!!.toInt(16)
|
addressInBytes[i] = element!!.toInt(16).toByte()
|
||||||
.toByte()
|
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Common.logger.error("Error parsing MAC address '{}'", mac, e)
|
Common.logger.error("Error parsing MAC address '{}'", mac, e)
|
||||||
|
|
|
@ -745,6 +745,11 @@ class NetUtilTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testMac() {
|
||||||
|
assertEquals("12:12:12:12:12:12", Mac.toStringLowerCase(Mac.toLong("12:12:12:12:12:12")))
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testIsPrivate() {
|
fun testIsPrivate() {
|
||||||
assertTrue(IPv4.isPrivate("192.168.23.102"))
|
assertTrue(IPv4.isPrivate("192.168.23.102"))
|
||||||
|
@ -755,27 +760,27 @@ class NetUtilTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testIp4Range() {
|
fun testIp4Range() {
|
||||||
assertTrue("", IPv4.isInRange(IPv4.toInt("10.10.10.5"), IPv4.toInt("10.10.10.10"), 24))
|
assertTrue(IPv4.isInRange("10.10.10.5", "10.10.10.10", 24))
|
||||||
assertTrue("", IPv4.isInRange(IPv4.toInt("10.0.0.5"), IPv4.toInt("10.10.10.10"), 8))
|
assertTrue(IPv4.isInRange("10.0.0.5", "10.10.10.10", 8))
|
||||||
assertFalse("", IPv4.isInRange(IPv4.toInt("11.0.0.5"), IPv4.toInt("10.10.10.10"), 8))
|
assertFalse(IPv4.isInRange("11.0.0.5", "10.10.10.10", 8))
|
||||||
assertTrue("", IPv4.isInRange(IPv4.toInt("11.0.0.5"), IPv4.toInt("10.10.10.10"), 1))
|
assertTrue(IPv4.isInRange("11.0.0.5", "10.10.10.10", 1))
|
||||||
assertTrue("", IPv4.isInRange(IPv4.toInt("11.0.0.5"), IPv4.toInt("10.10.10.10"), 0))
|
assertTrue(IPv4.isInRange("11.0.0.5", "10.10.10.10", 0))
|
||||||
assertFalse("", IPv4.isInRange(IPv4.toInt("11.0.0.5"), IPv4.toInt("10.10.10.10"), 32))
|
assertFalse(IPv4.isInRange("11.0.0.5", "10.10.10.10", 32))
|
||||||
assertTrue("", IPv4.isInRange(IPv4.toInt("10.10.10.10"), IPv4.toInt("10.10.10.10"), 32))
|
assertTrue(IPv4.isInRange("10.10.10.10", "10.10.10.10", 32))
|
||||||
assertTrue("", IPv4.isInRange(IPv4.toInt("10.10.10.10"), IPv4.toInt("10.10.10.10"), 31))
|
assertTrue(IPv4.isInRange("10.10.10.10", "10.10.10.10", 31))
|
||||||
assertTrue("", IPv4.isInRange(IPv4.toInt("10.10.10.10"), IPv4.toInt("10.10.10.10"), 30))
|
assertTrue(IPv4.isInRange("10.10.10.10", "10.10.10.10", 30))
|
||||||
assertTrue("", IPv4.isInRange(IPv4.toInt("192.168.42.14"), IPv4.toInt("192.168.0.0"), 16))
|
assertTrue(IPv4.isInRange("192.168.42.14", "192.168.0.0", 16))
|
||||||
assertTrue("", IPv4.isInRange(IPv4.toInt("192.168.0.0"), IPv4.toInt("192.168.0.0"), 16))
|
assertTrue(IPv4.isInRange("192.168.0.0", "192.168.0.0", 16))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testCidr() {
|
fun testCidr() {
|
||||||
assertTrue("", IPv4.isValidCidr("10.10.10.5/0"))
|
assertTrue(IPv4.isValidCidr("10.10.10.5/0"))
|
||||||
assertTrue("", IPv4.isValidCidr("10.10.10.5/24"))
|
assertTrue(IPv4.isValidCidr("10.10.10.5/24"))
|
||||||
assertTrue("", IPv4.isValidCidr("10.10.10.5/32"))
|
assertTrue(IPv4.isValidCidr("10.10.10.5/32"))
|
||||||
assertFalse("", IPv4.isValidCidr("10.10.10.5/33"))
|
assertFalse(IPv4.isValidCidr("10.10.10.5/33"))
|
||||||
assertFalse("", IPv4.isValidCidr("10.10.10.5/-1"))
|
assertFalse(IPv4.isValidCidr("10.10.10.5/-1"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue