[ip6-address] simplify FromString() (#6346)

This commit updates the implementation `Ip6::Address::FromString()`
simplifying it and handling more special cases where address starts
or ends with `::`. The unit test `test_ip6_address` is also updated
to cover some new test cases.
This commit is contained in:
Abtin Keshavarzian
2021-03-29 07:54:40 -07:00
committed by GitHub
parent d0bbfe6e4c
commit 4fb42e245b
3 changed files with 117 additions and 70 deletions
+23
View File
@@ -75,6 +75,11 @@ void TestIp6AddressFromString(void)
{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00},
ot::kErrorNone},
// Valid full address using capital letters.
{"0102:0304:0506:0708:090A:0B0C:0D0E:0F00",
{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00},
ot::kErrorNone},
// Valid full IPv6 address with mixed capital and small letters.
{"0102:0304:0506:0708:090a:0B0C:0d0E:0F00",
{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00},
@@ -98,6 +103,16 @@ void TestIp6AddressFromString(void)
// Unspecified address.
{"::", {0}, ot::kErrorNone},
// Starts with ::
{"::1:2:3:4",
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04},
ot::kErrorNone},
// Ends with ::
{"1001:2002:3003:4004::",
{0x10, 0x01, 0x20, 0x02, 0x30, 0x03, 0x40, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
ot::kErrorNone},
// Valid embedded IPv4 address.
{"64:ff9b::100.200.15.4",
{0x00, 0x64, 0xff, 0x9b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0xc8, 0x0f, 0x04},
@@ -108,6 +123,11 @@ void TestIp6AddressFromString(void)
{0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x0a, 0xbc, 0xde, 0xf1, 0x7f, 0x00, 0x00, 0x01},
ot::kErrorNone},
// Valid embedded IPv4 address.
{"1:2:3:4:5:6:127.1.2.3",
{0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x7f, 0x01, 0x02, 0x03},
ot::kErrorNone},
// Two :: should cause a parse error.
{"2001:db8::a::b", {0}, ot::kErrorParse},
@@ -129,6 +149,9 @@ void TestIp6AddressFromString(void)
// Invalid embedded IPv4 address.
{"64:ff9b::1.22.33.44.5", {0}, ot::kErrorParse},
// Too long with embedded IPv4 address.
{"1:2:3:4:5:6:7:127.1.2.3", {0}, ot::kErrorParse},
// Invalid embedded IPv4 address.
{".", {0}, ot::kErrorParse},