Skip to content

Commit 93c377a

Browse files
committed
test: migrate IpParserUtilTest to kotest
1 parent b9afae2 commit 93c377a

File tree

2 files changed

+57
-110
lines changed

2 files changed

+57
-110
lines changed

library/src/test/java/com/alibaba/dcm/internal/IpParserUtilTest.java

Lines changed: 0 additions & 110 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.alibaba.dcm.internal
2+
3+
import io.kotest.assertions.throwables.shouldThrow
4+
import io.kotest.core.spec.style.FunSpec
5+
import io.kotest.matchers.collections.shouldHaveSize
6+
import io.kotest.matchers.shouldBe
7+
import java.net.InetAddress
8+
9+
/**
10+
* @author Jerry Lee (oldratlee at gmail dot com)
11+
*/
12+
class IpParserUtilTests : FunSpec({
13+
14+
test("ipv4 to ByteArray") {
15+
IpParserUtil.ip2ByteArray("192.168.0.13") shouldBe byteArrayOf(192.toByte(), 168.toByte(), 0.toByte(), 13)
16+
IpParserUtil.ip2ByteArray("10.192.255.0") shouldBe byteArrayOf(10, 192.toByte(), 255.toByte(), 0)
17+
18+
val ip = "10.1.1.1"
19+
val actualIpBytes = IpParserUtil.ip2ByteArray(ip)
20+
21+
actualIpBytes shouldBe byteArrayOf(10, 1, 1, 1)
22+
actualIpBytes shouldBe getIpByGetAllByName(ip)
23+
}
24+
25+
test("ipv6 to ByteArray") {
26+
val ip = "2404:6800:4005:80a:0:0:0:200e"
27+
val bytes = IpParserUtil.ip2ByteArray(ip)
28+
29+
bytes shouldBe getIpByGetAllByName(ip)
30+
}
31+
32+
mapOf(
33+
"a.1.1.1" to "ip to ByteArray: ipv4 with char exception",
34+
"-2.168.0.13" to "ip to ByteArray: ipv4 minus exception",
35+
"1.1.1.256" to "ip to ByteArray: ipv4 overflow exception",
36+
"192.168.0.13.1" to "ip to ByteArray: ipv4 too long exception",
37+
"2404:6800:4005:80a:0:0:0:200z" to "ip to ByteArray: ipv6 with char exception",
38+
"-2404:6800:4005:80a:0:0:0:200e" to "ip to ByteArray: ipv6 minus exception",
39+
"2404:6800:4005:80a:0:0:0:200:123" to "ip to ByteArray: ipv6 too long exception",
40+
).forEach { (ip, caseName) ->
41+
42+
test("test $caseName") {
43+
shouldThrow<IllegalArgumentException> {
44+
IpParserUtil.ip2ByteArray(ip)
45+
}.message shouldBe ip + INVALID_IP_ADDRESS
46+
}
47+
48+
}
49+
})
50+
51+
private fun getIpByGetAllByName(ip: String): ByteArray {
52+
val addresses = InetAddress.getAllByName(ip)
53+
addresses.shouldHaveSize(1)
54+
return addresses[0].address
55+
}
56+
57+
private const val INVALID_IP_ADDRESS = ": invalid IP address"

0 commit comments

Comments
 (0)