|
17 | 17 |
|
18 | 18 | package org.apache.pekko.remote.artery |
19 | 19 |
|
| 20 | +import java.net.{ Inet6Address, InetAddress, InetSocketAddress } |
20 | 21 | import java.nio.charset.StandardCharsets |
21 | 22 |
|
22 | 23 | import org.apache.pekko |
| 24 | +import pekko.actor.{ Address, AddressFromURIString } |
23 | 25 | import pekko.util.ByteString |
24 | 26 |
|
25 | 27 | import org.scalatest.matchers.should.Matchers |
@@ -39,6 +41,54 @@ class ArterySettingsSpec extends AnyWordSpec with Matchers { |
39 | 41 |
|
40 | 42 | private def magic(s: String): ByteString = ByteString(s.getBytes(StandardCharsets.UTF_8)) |
41 | 43 |
|
| 44 | + "Artery hostname settings" must { |
| 45 | + "render the resolved IPv6 literal with brackets for canonical and bind hostnames" in { |
| 46 | + val loopback = InetAddress.getByName("::1") |
| 47 | + val expected = s"[${loopback.getHostAddress}]" |
| 48 | + val address = Address("pekko", "system", expected, 25520) |
| 49 | + val defaults = ConfigFactory.load().getConfig("pekko.remote.artery") |
| 50 | + val canonical = ArterySettings.getHostname("hostname", defaults.getConfig("canonical"), loopback) |
| 51 | + canonical shouldBe expected |
| 52 | + AddressFromURIString(Address("pekko", "system", canonical, 25520).toString) shouldBe address |
| 53 | + |
| 54 | + val bind = ConfigFactory.parseString("""hostname = "<getHostAddress>" """) |
| 55 | + val bindHost = ArterySettings.getHostname("hostname", bind, loopback) |
| 56 | + bindHost shouldBe expected |
| 57 | + new InetSocketAddress(bindHost, 25520).getAddress shouldBe loopback |
| 58 | + } |
| 59 | + |
| 60 | + "preserve the numeric scope of a bracketed IPv6 literal" in { |
| 61 | + val bytes = Array[Byte](0xFE.toByte, 0x80.toByte, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1) |
| 62 | + val linkLocal = Inet6Address.getByAddress(null, bytes, 7) |
| 63 | + val config = ConfigFactory.parseString("""hostname = "<getHostAddress>" """) |
| 64 | + |
| 65 | + val hostname = ArterySettings.getHostname("hostname", config, linkLocal) |
| 66 | + val expected = s"[${linkLocal.getHostAddress}]" |
| 67 | + hostname shouldBe expected |
| 68 | + AddressFromURIString(Address("pekko", "system", hostname, 25520).toString).host shouldBe Some(expected) |
| 69 | + } |
| 70 | + |
| 71 | + "bracket an IPv6 literal returned by <getHostName>" in { |
| 72 | + val loopback = InetAddress.getByAddress("::1", Array.fill[Byte](15)(0) :+ 1.toByte) |
| 73 | + val config = ConfigFactory.parseString("""hostname = "<getHostName>" """) |
| 74 | + |
| 75 | + ArterySettings.getHostname("hostname", config, loopback) shouldBe "[::1]" |
| 76 | + } |
| 77 | + |
| 78 | + "preserve IPv4, DNS names, and explicitly configured brackets" in { |
| 79 | + val loopback = InetAddress.getByAddress("local.example", Array[Byte](127, 0, 0, 1)) |
| 80 | + def hostname(value: String): String = |
| 81 | + ArterySettings.getHostname("hostname", ConfigFactory.parseString(s"""hostname = "$value" """), loopback) |
| 82 | + |
| 83 | + hostname("<getHostAddress>") shouldBe "127.0.0.1" |
| 84 | + hostname("<getHostName>") shouldBe "local.example" |
| 85 | + hostname("::1") shouldBe "[::1]" |
| 86 | + hostname("[::1]") shouldBe "[::1]" |
| 87 | + hostname("127.0.0.1") shouldBe "127.0.0.1" |
| 88 | + hostname("example.com") shouldBe "example.com" |
| 89 | + } |
| 90 | + } |
| 91 | + |
42 | 92 | "ArterySettings.TcpMagicValues" must { |
43 | 93 |
|
44 | 94 | // Held as a Seq rather than a Set: a Set hashes its members, and hashing a ByteString walks |
|