From ee7a8f60abb29fac07c2570cb71ca5b6aab41897 Mon Sep 17 00:00:00 2001 From: mpilquist Date: Mon, 27 Sep 2021 08:39:24 -0400 Subject: [PATCH] Fix #317 --- shared/src/main/scala/com/comcast/ip4s/Host.scala | 8 +++++--- .../src/test/scala/com/comcast/ip4s/Ipv6AddressTest.scala | 5 +++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/shared/src/main/scala/com/comcast/ip4s/Host.scala b/shared/src/main/scala/com/comcast/ip4s/Host.scala index 8570ae6f..9761d656 100644 --- a/shared/src/main/scala/com/comcast/ip4s/Host.scala +++ b/shared/src/main/scala/com/comcast/ip4s/Host.scala @@ -576,10 +576,12 @@ object Ipv6Address extends Ipv6AddressCompanionPlatform { var beforeCondenser = true var suffix: List[Int] = Nil val trimmed = value.trim() + var result: Option[Ipv6Address] = null val fields = - if (trimmed.nonEmpty) trimmed.split(":") else Array.empty[String] + if (trimmed.contains(':')) trimmed.split(':') + else Array.empty[String] + // if (trimmed.nonEmpty) trimmed.split(':') else Array.empty[String] var idx = 0 - var result: Option[Ipv6Address] = null while (idx < fields.size && (result eq null)) { val field = fields(idx) if (field.isEmpty) { @@ -607,7 +609,7 @@ object Ipv6Address extends Ipv6AddressCompanionPlatform { } if (result ne null) { result - } else if (fields.isEmpty && (trimmed.isEmpty || trimmed == ":")) { + } else if (fields.isEmpty && (trimmed.isEmpty || trimmed != "::")) { None } else { val bytes = new Array[Byte](16) diff --git a/test-kit/shared/src/test/scala/com/comcast/ip4s/Ipv6AddressTest.scala b/test-kit/shared/src/test/scala/com/comcast/ip4s/Ipv6AddressTest.scala index 41026b37..4cab33c3 100644 --- a/test-kit/shared/src/test/scala/com/comcast/ip4s/Ipv6AddressTest.scala +++ b/test-kit/shared/src/test/scala/com/comcast/ip4s/Ipv6AddressTest.scala @@ -34,6 +34,7 @@ class Ipv6AddressTest extends BaseTestSuite { assertEquals(Ipv6Address.fromString(":"), None) assertEquals(Ipv6Address.fromString(" : "), None) } + test("parsing from string - does parse ::") { assertEquals(Ipv6Address.fromString("::").isDefined, true) assertEquals(Ipv6Address.fromString(" :: ").isDefined, true) @@ -46,6 +47,10 @@ class Ipv6AddressTest extends BaseTestSuite { } } + test("parsing from string - does not misinterpret hosts") { + assertEquals(Ipv6Address.fromString("db"), None) + } + test("support converting to uncondensed string form") { forAll(Gen.listOfN(16, Arbitrary.arbitrary[Byte])) { bytesList => if (bytesList.size == 16) {