1+ /*
2+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+ * SPDX-License-Identifier: Apache-2.0
4+ */
5+ package aws.smithy.kotlin.runtime.net
6+
7+ import kotlinx.coroutines.test.runTest
8+ import kotlin.test.*
9+
10+ class HostResolverTest {
11+ @Test
12+ fun testResolveLocalhost () = runTest {
13+ val addresses = HostResolver .Default .resolve(" localhost" )
14+ assertTrue(addresses.isNotEmpty())
15+
16+ addresses.forEach { addr ->
17+ assertEquals(" localhost" , addr.hostname)
18+ when (val ip = addr.address) {
19+ is IpV4Addr -> {
20+ assertEquals(4 , ip.octets.size)
21+ // localhost should resolve to 127.0.0.1
22+ assertContentEquals(byteArrayOf(127 , 0 , 0 , 1 ), ip.octets)
23+ }
24+ is IpV6Addr -> {
25+ assertEquals(16 , ip.octets.size)
26+ // ::1 in IPv6
27+ val expectedIpv6 = ByteArray (16 ) { 0 }
28+ expectedIpv6[15 ] = 1
29+ assertContentEquals(expectedIpv6, ip.octets)
30+ }
31+ }
32+ }
33+ }
34+
35+ @Test
36+ fun testResolveIpv4Address () = runTest {
37+ val addresses = HostResolver .Default .resolve(" 127.0.0.1" )
38+ assertTrue(addresses.isNotEmpty())
39+
40+ addresses.forEach { addr ->
41+ assertTrue(addr.address is IpV4Addr )
42+ assertContentEquals(byteArrayOf(127 , 0 , 0 , 1 ), addr.address.octets)
43+ }
44+ }
45+
46+ @Test
47+ fun testResolveIpv6Address () = runTest {
48+ val addresses = HostResolver .Default .resolve(" ::1" )
49+ assertTrue(addresses.isNotEmpty())
50+
51+ addresses.forEach { addr ->
52+ assertTrue(addr.address is IpV6Addr )
53+ val expectedBytes = ByteArray (16 ) { 0 }
54+ expectedBytes[15 ] = 1
55+ assertContentEquals(expectedBytes, addr.address.octets)
56+ }
57+ }
58+
59+ @Test
60+ fun testResolveExampleDomain () = runTest {
61+ val addresses = HostResolver .Default .resolve(" example.com" )
62+ assertNotNull(addresses)
63+ assertTrue(addresses.isNotEmpty())
64+
65+ addresses.forEach { addr ->
66+ assertEquals(" example.com" , addr.hostname)
67+ when (val ip = addr.address) {
68+ is IpV4Addr -> assertEquals(4 , ip.octets.size)
69+ is IpV6Addr -> assertEquals(16 , ip.octets.size)
70+ }
71+ }
72+ }
73+
74+ @Test
75+ fun testResolveInvalidDomain () = runTest {
76+ assertFails {
77+ HostResolver .Default .resolve(" this-domain-definitely-does-not-exist-12345.local" )
78+ }
79+ }
80+
81+ @Test
82+ fun testNoopMethods () {
83+ // Test no-op methods don't throw
84+ val dummyAddr = HostAddress (" test.com" , IpV4Addr (ByteArray (4 )))
85+ val resolver = HostResolver .Default
86+ resolver.reportFailure(dummyAddr)
87+ resolver.purgeCache(null )
88+ resolver.purgeCache(dummyAddr)
89+ }
90+ }
0 commit comments