1 /*
2  *  Copyright (c) 2019-2022, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <limits.h>
30 
31 #include "common/array.hpp"
32 #include "common/encoding.hpp"
33 #include "common/string.hpp"
34 #include "net/ip4_types.hpp"
35 #include "net/ip6_address.hpp"
36 
37 #include "test_util.h"
38 
39 template <typename AddressType> struct TestVector
40 {
41     const char   *mString;
42     const uint8_t mAddr[sizeof(AddressType)];
43     ot::Error     mError;
44 };
45 
checkAddressFromString(TestVector<AddressType> * aTestVector)46 template <typename AddressType> static void checkAddressFromString(TestVector<AddressType> *aTestVector)
47 {
48     ot::Error   error;
49     AddressType address;
50 
51     address.Clear();
52 
53     error = address.FromString(aTestVector->mString);
54 
55     printf("%-42s -> %-42s\n", aTestVector->mString,
56            (error == ot::kErrorNone) ? address.ToString().AsCString() : "(parse error)");
57 
58     VerifyOrQuit(error == aTestVector->mError, "Address::FromString returned unexpected error code");
59 
60     if (error == ot::kErrorNone)
61     {
62         VerifyOrQuit(0 == memcmp(address.GetBytes(), aTestVector->mAddr, sizeof(AddressType)),
63                      "Address::FromString parsing failed");
64     }
65 }
66 
TestIp6AddressFromString(void)67 void TestIp6AddressFromString(void)
68 {
69     typedef TestVector<ot::Ip6::Address> Ip6AddressTestVector;
70 
71     Ip6AddressTestVector testVectors[] = {
72         // Valid full IPv6 address.
73         {"0102:0304:0506:0708:090a:0b0c:0d0e:0f00",
74          {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00},
75          ot::kErrorNone},
76 
77         // Valid full address using capital letters.
78         {"0102:0304:0506:0708:090A:0B0C:0D0E:0F00",
79          {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00},
80          ot::kErrorNone},
81 
82         // Valid full IPv6 address with mixed capital and small letters.
83         {"0102:0304:0506:0708:090a:0B0C:0d0E:0F00",
84          {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00},
85          ot::kErrorNone},
86 
87         // Short prefix and full IID.
88         {"fd11::abcd:e0e0:d10e:0001",
89          {0xfd, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xab, 0xcd, 0xe0, 0xe0, 0xd1, 0x0e, 0x00, 0x01},
90          ot::kErrorNone},
91 
92         // Valid IPv6 address with unnecessary :: symbol.
93         {"fd11:1234:5678:abcd::abcd:e0e0:d10e:1000",
94          {0xfd, 0x11, 0x12, 0x34, 0x56, 0x78, 0xab, 0xcd, 0xab, 0xcd, 0xe0, 0xe0, 0xd1, 0x0e, 0x10, 0x00},
95          ot::kErrorNone},
96 
97         // Short multicast address.
98         {"ff03::0b",
99          {0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b},
100          ot::kErrorNone},
101 
102         // Unspecified address.
103         {"::", {0}, ot::kErrorNone},
104 
105         // Starts with ::
106         {"::1:2:3:4",
107          {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04},
108          ot::kErrorNone},
109 
110         // Ends with ::
111         {"1001:2002:3003:4004::",
112          {0x10, 0x01, 0x20, 0x02, 0x30, 0x03, 0x40, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
113          ot::kErrorNone},
114 
115         // Valid embedded IPv4 address.
116         {"64:ff9b::100.200.15.4",
117          {0x00, 0x64, 0xff, 0x9b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0xc8, 0x0f, 0x04},
118          ot::kErrorNone},
119 
120         // Valid embedded IPv4 address.
121         {"2001:db8::abc:def1:127.0.0.1",
122          {0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x0a, 0xbc, 0xde, 0xf1, 0x7f, 0x00, 0x00, 0x01},
123          ot::kErrorNone},
124 
125         // Valid embedded IPv4 address.
126         {"1:2:3:4:5:6:127.1.2.3",
127          {0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x7f, 0x01, 0x02, 0x03},
128          ot::kErrorNone},
129 
130         // Two :: should cause a parse error.
131         {"2001:db8::a::b", {0}, ot::kErrorParse},
132 
133         // The "g" and "h" are not the hex characters.
134         {"2001:db8::abcd:efgh", {0}, ot::kErrorParse},
135 
136         // Too many colons.
137         {"1:2:3:4:5:6:7:8:9", {0}, ot::kErrorParse},
138 
139         // Too many characters in a single part.
140         {"2001:db8::abc:def12:1:2", {0}, ot::kErrorParse},
141 
142         // Invalid embedded IPv4 address.
143         {"64:ff9b::123.231.0.257", {0}, ot::kErrorParse},
144 
145         // Invalid embedded IPv4 address.
146         {"64:ff9b::1.22.33", {0}, ot::kErrorParse},
147 
148         // Invalid embedded IPv4 address.
149         {"64:ff9b::1.22.33.44.5", {0}, ot::kErrorParse},
150 
151         // Too long with embedded IPv4 address.
152         {"1:2:3:4:5:6:7:127.1.2.3", {0}, ot::kErrorParse},
153 
154         // Invalid embedded IPv4 address.
155         {".", {0}, ot::kErrorParse},
156 
157         // Invalid embedded IPv4 address.
158         {":.", {0}, ot::kErrorParse},
159 
160         // Invalid embedded IPv4 address.
161         {"::.", {0}, ot::kErrorParse},
162 
163         // Invalid embedded IPv4 address.
164         {":f:0:0:c:0:f:f:.", {0}, ot::kErrorParse},
165     };
166 
167     for (Ip6AddressTestVector &testVector : testVectors)
168     {
169         checkAddressFromString(&testVector);
170     }
171 
172     // Validate parsing all test vectors now as an IPv6 prefix.
173 
174     for (Ip6AddressTestVector &testVector : testVectors)
175     {
176         constexpr uint16_t kMaxString = 80;
177 
178         ot::Ip6::Prefix prefix;
179         char            string[kMaxString];
180         uint16_t        length;
181 
182         length = ot::StringLength(testVector.mString, kMaxString);
183         memcpy(string, testVector.mString, length);
184         VerifyOrQuit(length + sizeof("/128") <= kMaxString);
185         strcpy(&string[length], "/128");
186 
187         printf("%s\n", string);
188 
189         VerifyOrQuit(prefix.FromString(string) == testVector.mError);
190 
191         if (testVector.mError == ot::kErrorNone)
192         {
193             VerifyOrQuit(memcmp(prefix.GetBytes(), testVector.mAddr, sizeof(ot::Ip6::Address)) == 0);
194             VerifyOrQuit(prefix.GetLength() == 128);
195         }
196     }
197 }
198 
TestIp6PrefixFromString(void)199 void TestIp6PrefixFromString(void)
200 {
201     ot::Ip6::Prefix prefix;
202 
203     SuccessOrQuit(prefix.FromString("::/128"));
204     VerifyOrQuit(prefix.GetLength() == 128);
205 
206     SuccessOrQuit(prefix.FromString("::/0128"));
207     VerifyOrQuit(prefix.GetLength() == 128);
208 
209     SuccessOrQuit(prefix.FromString("::/5"));
210     VerifyOrQuit(prefix.GetLength() == 5);
211 
212     SuccessOrQuit(prefix.FromString("::/0"));
213     VerifyOrQuit(prefix.GetLength() == 0);
214 
215     VerifyOrQuit(prefix.FromString("::") == ot::kErrorParse);
216     VerifyOrQuit(prefix.FromString("::/") == ot::kErrorParse);
217     VerifyOrQuit(prefix.FromString("::/129") == ot::kErrorParse);
218     VerifyOrQuit(prefix.FromString(":: /12") == ot::kErrorParse);
219     VerifyOrQuit(prefix.FromString("::/a1") == ot::kErrorParse);
220     VerifyOrQuit(prefix.FromString("::/12 ") == ot::kErrorParse);
221 }
222 
TestIp4AddressFromString(void)223 void TestIp4AddressFromString(void)
224 {
225     typedef TestVector<ot::Ip4::Address> Ip4AddressTestVector;
226 
227     Ip4AddressTestVector testVectors[] = {
228         {"0.0.0.0", {0, 0, 0, 0}, ot::kErrorNone},
229         {"255.255.255.255", {255, 255, 255, 255}, ot::kErrorNone},
230         {"127.0.0.1", {127, 0, 0, 1}, ot::kErrorNone},
231         {"1.2.3.4", {1, 2, 3, 4}, ot::kErrorNone},
232         {"001.002.003.004", {1, 2, 3, 4}, ot::kErrorNone},
233         {"00000127.000.000.000001", {127, 0, 0, 1}, ot::kErrorNone},
234         {"123.231.0.256", {0}, ot::kErrorParse},    // Invalid byte value.
235         {"100123.231.0.256", {0}, ot::kErrorParse}, // Invalid byte value.
236         {"1.22.33", {0}, ot::kErrorParse},          // Too few bytes.
237         {"1.22.33.44.5", {0}, ot::kErrorParse},     // Too many bytes.
238         {"a.b.c.d", {0}, ot::kErrorParse},          // Wrong digit char.
239         {"123.23.45 .12", {0}, ot::kErrorParse},    // Extra space.
240         {".", {0}, ot::kErrorParse},                // Invalid.
241     };
242 
243     for (Ip4AddressTestVector &testVector : testVectors)
244     {
245         checkAddressFromString(&testVector);
246     }
247 }
248 
249 struct CidrTestVector
250 {
251     const char   *mString;
252     const uint8_t mAddr[sizeof(otIp4Address)];
253     const uint8_t mLength;
254     ot::Error     mError;
255 };
256 
checkCidrFromString(CidrTestVector * aTestVector)257 static void checkCidrFromString(CidrTestVector *aTestVector)
258 {
259     ot::Error     error;
260     ot::Ip4::Cidr cidr;
261 
262     cidr.Clear();
263 
264     error = cidr.FromString(aTestVector->mString);
265 
266     printf("%-42s -> %-42s\n", aTestVector->mString,
267            (error == ot::kErrorNone) ? cidr.ToString().AsCString() : "(parse error)");
268 
269     VerifyOrQuit(error == aTestVector->mError, "Address::FromString returned unexpected error code");
270 
271     if (error == ot::kErrorNone)
272     {
273         VerifyOrQuit(0 == memcmp(cidr.GetBytes(), aTestVector->mAddr, sizeof(aTestVector->mAddr)),
274                      "Cidr::FromString parsing failed");
275         VerifyOrQuit(cidr.mLength == aTestVector->mLength, "Cidr::FromString parsing failed");
276     }
277 }
278 
TestIp4CidrFromString(void)279 void TestIp4CidrFromString(void)
280 {
281     CidrTestVector testVectors[] = {
282         {"0.0.0.0/0", {0, 0, 0, 0}, 0, ot::kErrorNone},
283         {"255.255.255.255/32", {255, 255, 255, 255}, 32, ot::kErrorNone},
284         {"127.0.0.1/8", {127, 0, 0, 1}, 8, ot::kErrorNone},
285         {"1.2.3.4/24", {1, 2, 3, 4}, 24, ot::kErrorNone},
286         {"001.002.003.004/20", {1, 2, 3, 4}, 20, ot::kErrorNone},
287         {"00000127.000.000.000001/8", {127, 0, 0, 1}, 8, ot::kErrorNone},
288         // Valid suffix, invalid address
289         {"123.231.0.256/4", {0}, 0, ot::kErrorParse},    // Invalid byte value.
290         {"100123.231.0.256/4", {0}, 0, ot::kErrorParse}, // Invalid byte value.
291         {"1.22.33/4", {0}, 0, ot::kErrorParse},          // Too few bytes.
292         {"1.22.33.44.5/4", {0}, 0, ot::kErrorParse},     // Too many bytes.
293         {"a.b.c.d/4", {0}, 0, ot::kErrorParse},          // Wrong digit char.
294         {"123.23.45 .12/4", {0}, 0, ot::kErrorParse},    // Extra space.
295         {"./4", {0}, 0, ot::kErrorParse},                // Invalid.
296         // valid address, invalid suffix
297         {"1.2.3.4/33", {0}, 0, ot::kErrorParse},       // Prefix length too large
298         {"1.2.3.4/12345678", {0}, 0, ot::kErrorParse}, // Prefix length too large?
299         {"1.2.3.4/12a", {0}, 0, ot::kErrorParse},      // Extra char after prefix length.
300         {"1.2.3.4/-1", {0}, 0, ot::kErrorParse},       // Not even a non-negative integer.
301         {"1.2.3.4/3.14", {0}, 0, ot::kErrorParse},     // Not even a integer.
302         {"1.2.3.4/abcd", {0}, 0, ot::kErrorParse},     // Not even a number.
303         {"1.2.3.4/", {0}, 0, ot::kErrorParse},         // Where is the suffix?
304         {"1.2.3.4", {0}, 0, ot::kErrorParse},          // Where is the suffix?
305         // invalid address and invalid suffix
306         {"123.231.0.256/41", {0}, 0, ot::kErrorParse},     // Invalid byte value.
307         {"100123.231.0.256/abc", {0}, 0, ot::kErrorParse}, // Invalid byte value.
308         {"1.22.33", {0}, 0, ot::kErrorParse},              // Too few bytes.
309         {"1.22.33.44.5/36", {0}, 0, ot::kErrorParse},      // Too many bytes.
310         {"a.b.c.d/99", {0}, 0, ot::kErrorParse},           // Wrong digit char.
311         {"123.23.45 .12", {0}, 0, ot::kErrorParse},        // Extra space.
312         {".", {0}, 0, ot::kErrorParse},                    // Invalid.
313     };
314 
315     for (CidrTestVector &testVector : testVectors)
316     {
317         checkCidrFromString(&testVector);
318     }
319 }
320 
CheckPrefix(const ot::Ip6::Address & aAddress,const uint8_t * aPrefix,uint8_t aPrefixLength)321 bool CheckPrefix(const ot::Ip6::Address &aAddress, const uint8_t *aPrefix, uint8_t aPrefixLength)
322 {
323     // Check the first aPrefixLength bits of aAddress to match the given aPrefix.
324 
325     bool matches = true;
326 
327     for (uint8_t bit = 0; bit < aPrefixLength; bit++)
328     {
329         uint8_t index = bit / CHAR_BIT;
330         uint8_t mask  = (0x80 >> (bit % CHAR_BIT));
331 
332         if ((aAddress.mFields.m8[index] & mask) != (aPrefix[index] & mask))
333         {
334             matches = false;
335             break;
336         }
337     }
338 
339     return matches;
340 }
341 
CheckPrefixInIid(const ot::Ip6::InterfaceIdentifier & aIid,const uint8_t * aPrefix,uint8_t aPrefixLength)342 bool CheckPrefixInIid(const ot::Ip6::InterfaceIdentifier &aIid, const uint8_t *aPrefix, uint8_t aPrefixLength)
343 {
344     // Check the IID to contain the prefix bits (applicable when prefix length is longer than 64).
345 
346     bool matches = true;
347 
348     for (uint8_t bit = 64; bit < aPrefixLength; bit++)
349     {
350         uint8_t index = bit / CHAR_BIT;
351         uint8_t mask  = (0x80 >> (bit % CHAR_BIT));
352 
353         if ((aIid.mFields.m8[index - 8] & mask) != (aPrefix[index] & mask))
354         {
355             matches = false;
356             break;
357         }
358     }
359 
360     return matches;
361 }
362 
CheckInterfaceId(const ot::Ip6::Address & aAddress1,const ot::Ip6::Address & aAddress2,uint8_t aPrefixLength)363 bool CheckInterfaceId(const ot::Ip6::Address &aAddress1, const ot::Ip6::Address &aAddress2, uint8_t aPrefixLength)
364 {
365     // Check whether all the bits after aPrefixLength of the two given IPv6 Address match or not.
366 
367     bool matches = true;
368 
369     for (size_t bit = aPrefixLength; bit < sizeof(ot::Ip6::Address) * CHAR_BIT; bit++)
370     {
371         uint8_t index = bit / CHAR_BIT;
372         uint8_t mask  = (0x80 >> (bit % CHAR_BIT));
373 
374         if ((aAddress1.mFields.m8[index] & mask) != (aAddress2.mFields.m8[index] & mask))
375         {
376             matches = false;
377             break;
378         }
379     }
380 
381     return matches;
382 }
383 
TestIp6AddressSetPrefix(void)384 void TestIp6AddressSetPrefix(void)
385 {
386     const uint8_t kPrefixes[][OT_IP6_ADDRESS_SIZE] = {
387         {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
388         {0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55},
389         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
390         {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
391     };
392 
393     ot::Ip6::Address address;
394     ot::Ip6::Address allZeroAddress;
395     ot::Ip6::Address allOneAddress;
396     ot::Ip6::Prefix  ip6Prefix;
397 
398     allZeroAddress.Clear();
399     memset(&allOneAddress, 0xff, sizeof(allOneAddress));
400 
401     for (auto prefix : kPrefixes)
402     {
403         memcpy(address.mFields.m8, prefix, sizeof(address));
404         printf("Prefix is %s\n", address.ToString().AsCString());
405 
406         for (size_t prefixLength = 0; prefixLength <= sizeof(ot::Ip6::Address) * CHAR_BIT; prefixLength++)
407         {
408             ip6Prefix.Clear();
409             ip6Prefix.Set(prefix, prefixLength);
410 
411             address = allZeroAddress;
412             address.SetPrefix(ip6Prefix);
413             printf("   prefix-len:%-3zu --> %s\n", prefixLength, address.ToString().AsCString());
414             VerifyOrQuit(CheckPrefix(address, prefix, prefixLength), "Prefix does not match after SetPrefix()");
415             VerifyOrQuit(CheckInterfaceId(address, allZeroAddress, prefixLength),
416                          "SetPrefix changed bits beyond the prefix length");
417 
418             address = allOneAddress;
419             address.SetPrefix(ip6Prefix);
420             VerifyOrQuit(CheckPrefix(address, prefix, prefixLength), "Prefix does not match after SetPrefix()");
421             VerifyOrQuit(CheckInterfaceId(address, allOneAddress, prefixLength),
422                          "SetPrefix changed bits beyond the prefix length");
423 
424             address = allZeroAddress;
425             address.GetIid().ApplyPrefix(ip6Prefix);
426             VerifyOrQuit(CheckPrefixInIid(address.GetIid(), prefix, prefixLength), "IID is not correct");
427             VerifyOrQuit(CheckInterfaceId(address, allZeroAddress, prefixLength),
428                          "Iid:ApplyPrefrix() changed bits beyond the prefix length");
429 
430             address = allOneAddress;
431             address.GetIid().ApplyPrefix(ip6Prefix);
432             VerifyOrQuit(CheckPrefixInIid(address.GetIid(), prefix, prefixLength), "IID is not correct");
433             VerifyOrQuit(CheckInterfaceId(address, allOneAddress, prefixLength),
434                          "Iid:ApplyPrefrix() changed bits beyond the prefix length");
435         }
436     }
437 }
438 
PrefixFrom(const char * aAddressString,uint8_t aPrefixLength)439 ot::Ip6::Prefix PrefixFrom(const char *aAddressString, uint8_t aPrefixLength)
440 {
441     ot::Ip6::Prefix  prefix;
442     ot::Ip6::Address address;
443 
444     SuccessOrQuit(address.FromString(aAddressString));
445     prefix.Set(address.GetBytes(), aPrefixLength);
446 
447     return prefix;
448 }
449 
TestIp6Prefix(void)450 void TestIp6Prefix(void)
451 {
452     const uint8_t kPrefixes[][OT_IP6_ADDRESS_SIZE] = {
453         {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
454         {0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55},
455         {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
456     };
457 
458     ot::Ip6::Prefix  prefix;
459     ot::Ip6::Address address1, address2;
460 
461     for (auto prefixBytes : kPrefixes)
462     {
463         memcpy(address1.mFields.m8, prefixBytes, sizeof(address1));
464         address2 = address1;
465         address2.mFields.m8[0] ^= 0x80; // Change first bit.
466 
467         for (uint8_t prefixLength = 1; prefixLength <= ot::Ip6::Prefix::kMaxLength; prefixLength++)
468         {
469             prefix.Set(prefixBytes, prefixLength);
470 
471             printf("Prefix %s\n", prefix.ToString().AsCString());
472 
473             VerifyOrQuit(prefix.GetLength() == prefixLength);
474             VerifyOrQuit(prefix.IsValid());
475             VerifyOrQuit(prefix.IsEqual(prefixBytes, prefixLength));
476 
477             VerifyOrQuit(address1.MatchesPrefix(prefix));
478             VerifyOrQuit(!address2.MatchesPrefix(prefix));
479 
480             VerifyOrQuit(prefix == prefix);
481             VerifyOrQuit(!(prefix < prefix));
482 
483             for (uint8_t subPrefixLength = 1; subPrefixLength <= prefixLength; subPrefixLength++)
484             {
485                 ot::Ip6::Prefix subPrefix;
486 
487                 subPrefix.Set(prefixBytes, subPrefixLength);
488 
489                 VerifyOrQuit(prefix.ContainsPrefix(subPrefix));
490 
491                 if (prefixLength == subPrefixLength)
492                 {
493                     VerifyOrQuit(prefix == subPrefix);
494                     VerifyOrQuit(prefix.IsEqual(subPrefix.GetBytes(), subPrefix.GetLength()));
495                     VerifyOrQuit(!(subPrefix < prefix));
496                 }
497                 else
498                 {
499                     VerifyOrQuit(prefix != subPrefix);
500                     VerifyOrQuit(!prefix.IsEqual(subPrefix.GetBytes(), subPrefix.GetLength()));
501                     VerifyOrQuit(subPrefix < prefix);
502                 }
503             }
504 
505             for (uint8_t bitNumber = 0; bitNumber < prefixLength; bitNumber++)
506             {
507                 ot::Ip6::Prefix prefix2;
508                 uint8_t         mask  = static_cast<uint8_t>(1U << (7 - (bitNumber & 7)));
509                 uint8_t         index = (bitNumber / 8);
510                 bool            isPrefixSmaller;
511 
512                 prefix2 = prefix;
513                 VerifyOrQuit(prefix == prefix2);
514 
515                 // Flip the `bitNumber` bit between `prefix` and `prefix2`
516 
517                 prefix2.mPrefix.mFields.m8[index] ^= mask;
518                 VerifyOrQuit(prefix != prefix2);
519 
520                 isPrefixSmaller = ((prefix.GetBytes()[index] & mask) == 0);
521 
522                 VerifyOrQuit((prefix < prefix2) == isPrefixSmaller);
523                 VerifyOrQuit((prefix2 < prefix) == !isPrefixSmaller);
524             }
525         }
526     }
527 
528     {
529         struct TestCase
530         {
531             ot::Ip6::Prefix mPrefixA;
532             ot::Ip6::Prefix mPrefixB;
533         };
534 
535         TestCase kTestCases[] = {
536             {PrefixFrom("fd00::", 16), PrefixFrom("fd01::", 16)},
537             {PrefixFrom("fc00::", 16), PrefixFrom("fd00::", 16)},
538             {PrefixFrom("fd00::", 15), PrefixFrom("fd00::", 16)},
539             {PrefixFrom("fd00::", 16), PrefixFrom("fd00:0::", 32)},
540             {PrefixFrom("2001:0:0:0::", 64), PrefixFrom("fd00::", 8)},
541             {PrefixFrom("2001:dba::", 32), PrefixFrom("fd12:3456:1234:abcd::", 64)},
542             {PrefixFrom("910b:1000:0::", 48), PrefixFrom("910b:2000::", 32)},
543             {PrefixFrom("::", 0), PrefixFrom("fd00::", 8)},
544             {PrefixFrom("::", 0), PrefixFrom("::", 16)},
545             {PrefixFrom("fd00:2:2::", 33), PrefixFrom("fd00:2:2::", 35)},
546             {PrefixFrom("1:2:3:ffff::", 62), PrefixFrom("1:2:3:ffff::", 63)},
547         };
548 
549         printf("\nCompare Prefixes:\n");
550 
551         for (const TestCase &testCase : kTestCases)
552         {
553             printf(" %26s  <  %s\n", testCase.mPrefixA.ToString().AsCString(),
554                    testCase.mPrefixB.ToString().AsCString());
555             VerifyOrQuit(testCase.mPrefixA < testCase.mPrefixB);
556             VerifyOrQuit(!(testCase.mPrefixB < testCase.mPrefixA));
557         }
558     }
559 
560     // `IsLinkLocal()` - should contain `fe80::/10`.
561     VerifyOrQuit(PrefixFrom("fe80::", 10).IsLinkLocal());
562     VerifyOrQuit(PrefixFrom("fe80::", 11).IsLinkLocal());
563     VerifyOrQuit(PrefixFrom("fea0::", 16).IsLinkLocal());
564     VerifyOrQuit(!PrefixFrom("fe80::", 9).IsLinkLocal());
565     VerifyOrQuit(!PrefixFrom("ff80::", 10).IsLinkLocal());
566     VerifyOrQuit(!PrefixFrom("fe00::", 10).IsLinkLocal());
567     VerifyOrQuit(!PrefixFrom("fec0::", 10).IsLinkLocal());
568 
569     // `IsMulticast()` - should contain `ff00::/8`.
570     VerifyOrQuit(PrefixFrom("ff00::", 8).IsMulticast());
571     VerifyOrQuit(PrefixFrom("ff80::", 9).IsMulticast());
572     VerifyOrQuit(PrefixFrom("ffff::", 16).IsMulticast());
573     VerifyOrQuit(!PrefixFrom("ff00::", 7).IsMulticast());
574     VerifyOrQuit(!PrefixFrom("fe00::", 8).IsMulticast());
575 
576     // `IsUniqueLocal()` - should contain `fc00::/7`.
577     VerifyOrQuit(PrefixFrom("fc00::", 7).IsUniqueLocal());
578     VerifyOrQuit(PrefixFrom("fd00::", 8).IsUniqueLocal());
579     VerifyOrQuit(PrefixFrom("fc10::", 16).IsUniqueLocal());
580     VerifyOrQuit(!PrefixFrom("fc00::", 6).IsUniqueLocal());
581     VerifyOrQuit(!PrefixFrom("f800::", 7).IsUniqueLocal());
582     VerifyOrQuit(!PrefixFrom("fe00::", 7).IsUniqueLocal());
583 }
584 
TestIp6PrefixTidy(void)585 void TestIp6PrefixTidy(void)
586 {
587     struct TestVector
588     {
589         uint8_t     originalPrefix[OT_IP6_ADDRESS_SIZE];
590         const char *prefixStringAfterTidy[129];
591     };
592     const TestVector kPrefixes[] = {
593         {
594             .originalPrefix = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
595                                0xff},
596             .prefixStringAfterTidy =
597                 {
598                     "::/0",
599                     "8000::/1",
600                     "c000::/2",
601                     "e000::/3",
602                     "f000::/4",
603                     "f800::/5",
604                     "fc00::/6",
605                     "fe00::/7",
606                     "ff00::/8",
607                     "ff80::/9",
608                     "ffc0::/10",
609                     "ffe0::/11",
610                     "fff0::/12",
611                     "fff8::/13",
612                     "fffc::/14",
613                     "fffe::/15",
614                     "ffff::/16",
615                     "ffff:8000::/17",
616                     "ffff:c000::/18",
617                     "ffff:e000::/19",
618                     "ffff:f000::/20",
619                     "ffff:f800::/21",
620                     "ffff:fc00::/22",
621                     "ffff:fe00::/23",
622                     "ffff:ff00::/24",
623                     "ffff:ff80::/25",
624                     "ffff:ffc0::/26",
625                     "ffff:ffe0::/27",
626                     "ffff:fff0::/28",
627                     "ffff:fff8::/29",
628                     "ffff:fffc::/30",
629                     "ffff:fffe::/31",
630                     "ffff:ffff::/32",
631                     "ffff:ffff:8000::/33",
632                     "ffff:ffff:c000::/34",
633                     "ffff:ffff:e000::/35",
634                     "ffff:ffff:f000::/36",
635                     "ffff:ffff:f800::/37",
636                     "ffff:ffff:fc00::/38",
637                     "ffff:ffff:fe00::/39",
638                     "ffff:ffff:ff00::/40",
639                     "ffff:ffff:ff80::/41",
640                     "ffff:ffff:ffc0::/42",
641                     "ffff:ffff:ffe0::/43",
642                     "ffff:ffff:fff0::/44",
643                     "ffff:ffff:fff8::/45",
644                     "ffff:ffff:fffc::/46",
645                     "ffff:ffff:fffe::/47",
646                     "ffff:ffff:ffff::/48",
647                     "ffff:ffff:ffff:8000::/49",
648                     "ffff:ffff:ffff:c000::/50",
649                     "ffff:ffff:ffff:e000::/51",
650                     "ffff:ffff:ffff:f000::/52",
651                     "ffff:ffff:ffff:f800::/53",
652                     "ffff:ffff:ffff:fc00::/54",
653                     "ffff:ffff:ffff:fe00::/55",
654                     "ffff:ffff:ffff:ff00::/56",
655                     "ffff:ffff:ffff:ff80::/57",
656                     "ffff:ffff:ffff:ffc0::/58",
657                     "ffff:ffff:ffff:ffe0::/59",
658                     "ffff:ffff:ffff:fff0::/60",
659                     "ffff:ffff:ffff:fff8::/61",
660                     "ffff:ffff:ffff:fffc::/62",
661                     "ffff:ffff:ffff:fffe::/63",
662                     "ffff:ffff:ffff:ffff::/64",
663                     "ffff:ffff:ffff:ffff:8000::/65",
664                     "ffff:ffff:ffff:ffff:c000::/66",
665                     "ffff:ffff:ffff:ffff:e000::/67",
666                     "ffff:ffff:ffff:ffff:f000::/68",
667                     "ffff:ffff:ffff:ffff:f800::/69",
668                     "ffff:ffff:ffff:ffff:fc00::/70",
669                     "ffff:ffff:ffff:ffff:fe00::/71",
670                     "ffff:ffff:ffff:ffff:ff00::/72",
671                     "ffff:ffff:ffff:ffff:ff80::/73",
672                     "ffff:ffff:ffff:ffff:ffc0::/74",
673                     "ffff:ffff:ffff:ffff:ffe0::/75",
674                     "ffff:ffff:ffff:ffff:fff0::/76",
675                     "ffff:ffff:ffff:ffff:fff8::/77",
676                     "ffff:ffff:ffff:ffff:fffc::/78",
677                     "ffff:ffff:ffff:ffff:fffe::/79",
678                     "ffff:ffff:ffff:ffff:ffff::/80",
679                     "ffff:ffff:ffff:ffff:ffff:8000::/81",
680                     "ffff:ffff:ffff:ffff:ffff:c000::/82",
681                     "ffff:ffff:ffff:ffff:ffff:e000::/83",
682                     "ffff:ffff:ffff:ffff:ffff:f000::/84",
683                     "ffff:ffff:ffff:ffff:ffff:f800::/85",
684                     "ffff:ffff:ffff:ffff:ffff:fc00::/86",
685                     "ffff:ffff:ffff:ffff:ffff:fe00::/87",
686                     "ffff:ffff:ffff:ffff:ffff:ff00::/88",
687                     "ffff:ffff:ffff:ffff:ffff:ff80::/89",
688                     "ffff:ffff:ffff:ffff:ffff:ffc0::/90",
689                     "ffff:ffff:ffff:ffff:ffff:ffe0::/91",
690                     "ffff:ffff:ffff:ffff:ffff:fff0::/92",
691                     "ffff:ffff:ffff:ffff:ffff:fff8::/93",
692                     "ffff:ffff:ffff:ffff:ffff:fffc::/94",
693                     "ffff:ffff:ffff:ffff:ffff:fffe::/95",
694                     "ffff:ffff:ffff:ffff:ffff:ffff::/96",
695                     // Note: The result of /97 to /112 does not meet RFC requirements:
696                     // 4.2.2.  Handling One 16-Bit 0 Field
697                     // The symbol "::" MUST NOT be used to shorten just one 16-bit 0 field.
698                     "ffff:ffff:ffff:ffff:ffff:ffff:8000::/97",
699                     "ffff:ffff:ffff:ffff:ffff:ffff:c000::/98",
700                     "ffff:ffff:ffff:ffff:ffff:ffff:e000::/99",
701                     "ffff:ffff:ffff:ffff:ffff:ffff:f000::/100",
702                     "ffff:ffff:ffff:ffff:ffff:ffff:f800::/101",
703                     "ffff:ffff:ffff:ffff:ffff:ffff:fc00::/102",
704                     "ffff:ffff:ffff:ffff:ffff:ffff:fe00::/103",
705                     "ffff:ffff:ffff:ffff:ffff:ffff:ff00::/104",
706                     "ffff:ffff:ffff:ffff:ffff:ffff:ff80::/105",
707                     "ffff:ffff:ffff:ffff:ffff:ffff:ffc0::/106",
708                     "ffff:ffff:ffff:ffff:ffff:ffff:ffe0::/107",
709                     "ffff:ffff:ffff:ffff:ffff:ffff:fff0::/108",
710                     "ffff:ffff:ffff:ffff:ffff:ffff:fff8::/109",
711                     "ffff:ffff:ffff:ffff:ffff:ffff:fffc::/110",
712                     "ffff:ffff:ffff:ffff:ffff:ffff:fffe::/111",
713                     "ffff:ffff:ffff:ffff:ffff:ffff:ffff::/112",
714                     "ffff:ffff:ffff:ffff:ffff:ffff:ffff:8000/113",
715                     "ffff:ffff:ffff:ffff:ffff:ffff:ffff:c000/114",
716                     "ffff:ffff:ffff:ffff:ffff:ffff:ffff:e000/115",
717                     "ffff:ffff:ffff:ffff:ffff:ffff:ffff:f000/116",
718                     "ffff:ffff:ffff:ffff:ffff:ffff:ffff:f800/117",
719                     "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fc00/118",
720                     "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fe00/119",
721                     "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00/120",
722                     "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff80/121",
723                     "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffc0/122",
724                     "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffe0/123",
725                     "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff0/124",
726                     "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff8/125",
727                     "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffc/126",
728                     "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe/127",
729                     "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128",
730                 },
731         },
732     };
733 
734     printf("Tidy Prefixes:\n");
735 
736     for (auto test : kPrefixes)
737     {
738         for (uint16_t i = 0; i < ot::GetArrayLength(test.prefixStringAfterTidy); i++)
739         {
740             ot::Ip6::Prefix prefix, answer;
741 
742             SuccessOrQuit(answer.FromString(test.prefixStringAfterTidy[i]));
743             prefix.Set(test.originalPrefix, i);
744             prefix.Tidy();
745 
746             {
747                 ot::Ip6::Prefix::InfoString prefixString = prefix.ToString();
748 
749                 printf("Prefix: %-36s  TidyResult: %-36s\n", test.prefixStringAfterTidy[i],
750                        prefix.ToString().AsCString());
751 
752                 VerifyOrQuit(memcmp(answer.mPrefix.mFields.m8, prefix.mPrefix.mFields.m8,
753                                     sizeof(answer.mPrefix.mFields.m8)) == 0);
754                 VerifyOrQuit(prefix.mLength == answer.mLength);
755                 VerifyOrQuit(strcmp(test.prefixStringAfterTidy[i], prefixString.AsCString()) == 0);
756             }
757         }
758     }
759 }
760 
TestIp4Ip6Translation(void)761 void TestIp4Ip6Translation(void)
762 {
763     struct TestCase
764     {
765         const char *mPrefix;     // NAT64 prefix
766         uint8_t     mLength;     // Prefix length in bits
767         const char *mIp6Address; // Expected IPv6 address (with embedded IPv4 "192.0.2.33").
768     };
769 
770     // The test cases are from RFC 6052 - section 2.4
771 
772     const TestCase kTestCases[] = {
773         {"2001:db8::", 32, "2001:db8:c000:221::"},
774         {"2001:db8:100::", 40, "2001:db8:1c0:2:21::"},
775         {"2001:db8:122::", 48, "2001:db8:122:c000:2:2100::"},
776         {"2001:db8:122:300::", 56, "2001:db8:122:3c0:0:221::"},
777         {"2001:db8:122:344::", 64, "2001:db8:122:344:c0:2:2100::"},
778         {"2001:db8:122:344::", 96, "2001:db8:122:344::192.0.2.33"},
779         {"64:ff9b::", 96, "64:ff9b::192.0.2.33"},
780     };
781 
782     const uint8_t kIp4Address[] = {192, 0, 2, 33};
783 
784     ot::Ip4::Address ip4Address;
785 
786     printf("\nTestIp4Ip6Translation()\n");
787 
788     ip4Address.SetBytes(kIp4Address);
789 
790     for (const TestCase &testCase : kTestCases)
791     {
792         ot::Ip6::Prefix  prefix;
793         ot::Ip6::Address address;
794         ot::Ip6::Address expectedAddress;
795 
796         SuccessOrQuit(address.FromString(testCase.mPrefix));
797         prefix.Set(address.GetBytes(), testCase.mLength);
798 
799         SuccessOrQuit(expectedAddress.FromString(testCase.mIp6Address));
800 
801         address.SynthesizeFromIp4Address(prefix, ip4Address);
802 
803         printf("Prefix: %-26s IPv4Addr: %-12s Ipv6Address: %-36s Expected: %s (%s)\n", prefix.ToString().AsCString(),
804                ip4Address.ToString().AsCString(), address.ToString().AsCString(), testCase.mIp6Address,
805                expectedAddress.ToString().AsCString());
806 
807         VerifyOrQuit(address == expectedAddress, "Ip6::SynthesizeFromIp4Address() failed");
808     }
809 
810     for (const TestCase &testCase : kTestCases)
811     {
812         const ot::Ip4::Address expectedAddress = ip4Address;
813         ot::Ip4::Address       address;
814         ot::Ip6::Address       ip6Address;
815 
816         SuccessOrQuit(ip6Address.FromString(testCase.mIp6Address));
817 
818         address.ExtractFromIp6Address(testCase.mLength, ip6Address);
819 
820         printf("Ipv6Address: %-36s IPv4Addr: %-12s Expected: %s\n", testCase.mIp6Address,
821                address.ToString().AsCString(), expectedAddress.ToString().AsCString());
822 
823         VerifyOrQuit(address == expectedAddress, "Ip4::ExtractFromIp6Address() failed");
824     }
825 }
826 
TestIp4Cidr(void)827 void TestIp4Cidr(void)
828 {
829     using ot::Encoding::BigEndian::HostSwap32;
830     struct TestCase
831     {
832         const char    *mNetwork;
833         const uint8_t  mLength;
834         const uint32_t mHost;
835         const char    *mOutcome;
836     };
837 
838     const TestCase kTestCases[] = {
839         {"172.16.12.34", 32, 0x12345678, "172.16.12.34"},  {"172.16.12.34", 31, 0x12345678, "172.16.12.34"},
840         {"172.16.12.34", 30, 0x12345678, "172.16.12.32"},  {"172.16.12.34", 29, 0x12345678, "172.16.12.32"},
841         {"172.16.12.34", 28, 0x12345678, "172.16.12.40"},  {"172.16.12.34", 27, 0x12345678, "172.16.12.56"},
842         {"172.16.12.34", 26, 0x12345678, "172.16.12.56"},  {"172.16.12.34", 25, 0x12345678, "172.16.12.120"},
843         {"172.16.12.34", 24, 0x12345678, "172.16.12.120"}, {"172.16.12.34", 23, 0x12345678, "172.16.12.120"},
844         {"172.16.12.34", 22, 0x12345678, "172.16.14.120"}, {"172.16.12.34", 21, 0x12345678, "172.16.14.120"},
845         {"172.16.12.34", 20, 0x12345678, "172.16.6.120"},  {"172.16.12.34", 19, 0x12345678, "172.16.22.120"},
846         {"172.16.12.34", 18, 0x12345678, "172.16.22.120"}, {"172.16.12.34", 17, 0x12345678, "172.16.86.120"},
847         {"172.16.12.34", 16, 0x12345678, "172.16.86.120"}, {"172.16.12.34", 15, 0x12345678, "172.16.86.120"},
848         {"172.16.12.34", 14, 0x12345678, "172.16.86.120"}, {"172.16.12.34", 13, 0x12345678, "172.20.86.120"},
849         {"172.16.12.34", 12, 0x12345678, "172.20.86.120"}, {"172.16.12.34", 11, 0x12345678, "172.20.86.120"},
850         {"172.16.12.34", 10, 0x12345678, "172.52.86.120"}, {"172.16.12.34", 9, 0x12345678, "172.52.86.120"},
851         {"172.16.12.34", 8, 0x12345678, "172.52.86.120"},  {"172.16.12.34", 7, 0x12345678, "172.52.86.120"},
852         {"172.16.12.34", 6, 0x12345678, "174.52.86.120"},  {"172.16.12.34", 5, 0x12345678, "170.52.86.120"},
853         {"172.16.12.34", 4, 0x12345678, "162.52.86.120"},  {"172.16.12.34", 3, 0x12345678, "178.52.86.120"},
854         {"172.16.12.34", 2, 0x12345678, "146.52.86.120"},  {"172.16.12.34", 1, 0x12345678, "146.52.86.120"},
855         {"172.16.12.34", 0, 0x12345678, "18.52.86.120"},
856     };
857 
858     for (const TestCase &testCase : kTestCases)
859     {
860         ot::Ip4::Address network;
861         ot::Ip4::Cidr    cidr;
862         ot::Ip4::Address generated;
863 
864         SuccessOrQuit(network.FromString(testCase.mNetwork));
865         cidr.mAddress = network;
866         cidr.mLength  = testCase.mLength;
867 
868         generated.SynthesizeFromCidrAndHost(cidr, testCase.mHost);
869 
870         printf("CIDR: %-18s HostID: %-8x Host: %-14s Expected: %s\n", cidr.ToString().AsCString(), testCase.mHost,
871                generated.ToString().AsCString(), testCase.mOutcome);
872 
873         VerifyOrQuit(strcmp(generated.ToString().AsCString(), testCase.mOutcome) == 0,
874                      "Ip4::Address::SynthesizeFromCidrAndHost() failed");
875     }
876 }
877 
main(void)878 int main(void)
879 {
880     TestIp6AddressSetPrefix();
881     TestIp4AddressFromString();
882     TestIp6AddressFromString();
883     TestIp6PrefixFromString();
884     TestIp6Prefix();
885     TestIp6PrefixTidy();
886     TestIp4Ip6Translation();
887     TestIp4Cidr();
888     TestIp4CidrFromString();
889     printf("All tests passed\n");
890     return 0;
891 }
892