xref: /aosp_15_r20/external/openthread/tests/unit/test_nat64.cpp (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker /*
2*cfb92d14SAndroid Build Coastguard Worker  *  Copyright (c) 2022, The OpenThread Authors.
3*cfb92d14SAndroid Build Coastguard Worker  *  All rights reserved.
4*cfb92d14SAndroid Build Coastguard Worker  *
5*cfb92d14SAndroid Build Coastguard Worker  *  Redistribution and use in source and binary forms, with or without
6*cfb92d14SAndroid Build Coastguard Worker  *  modification, are permitted provided that the following conditions are met:
7*cfb92d14SAndroid Build Coastguard Worker  *  1. Redistributions of source code must retain the above copyright
8*cfb92d14SAndroid Build Coastguard Worker  *     notice, this list of conditions and the following disclaimer.
9*cfb92d14SAndroid Build Coastguard Worker  *  2. Redistributions in binary form must reproduce the above copyright
10*cfb92d14SAndroid Build Coastguard Worker  *     notice, this list of conditions and the following disclaimer in the
11*cfb92d14SAndroid Build Coastguard Worker  *     documentation and/or other materials provided with the distribution.
12*cfb92d14SAndroid Build Coastguard Worker  *  3. Neither the name of the copyright holder nor the
13*cfb92d14SAndroid Build Coastguard Worker  *     names of its contributors may be used to endorse or promote products
14*cfb92d14SAndroid Build Coastguard Worker  *     derived from this software without specific prior written permission.
15*cfb92d14SAndroid Build Coastguard Worker  *
16*cfb92d14SAndroid Build Coastguard Worker  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*cfb92d14SAndroid Build Coastguard Worker  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*cfb92d14SAndroid Build Coastguard Worker  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*cfb92d14SAndroid Build Coastguard Worker  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*cfb92d14SAndroid Build Coastguard Worker  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*cfb92d14SAndroid Build Coastguard Worker  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*cfb92d14SAndroid Build Coastguard Worker  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*cfb92d14SAndroid Build Coastguard Worker  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*cfb92d14SAndroid Build Coastguard Worker  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*cfb92d14SAndroid Build Coastguard Worker  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*cfb92d14SAndroid Build Coastguard Worker  *  POSSIBILITY OF SUCH DAMAGE.
27*cfb92d14SAndroid Build Coastguard Worker  */
28*cfb92d14SAndroid Build Coastguard Worker 
29*cfb92d14SAndroid Build Coastguard Worker #include "net/nat64_translator.hpp"
30*cfb92d14SAndroid Build Coastguard Worker 
31*cfb92d14SAndroid Build Coastguard Worker #include "test_platform.h"
32*cfb92d14SAndroid Build Coastguard Worker #include "test_util.hpp"
33*cfb92d14SAndroid Build Coastguard Worker 
34*cfb92d14SAndroid Build Coastguard Worker #include "string.h"
35*cfb92d14SAndroid Build Coastguard Worker 
36*cfb92d14SAndroid Build Coastguard Worker #include "common/code_utils.hpp"
37*cfb92d14SAndroid Build Coastguard Worker #include "common/debug.hpp"
38*cfb92d14SAndroid Build Coastguard Worker #include "common/message.hpp"
39*cfb92d14SAndroid Build Coastguard Worker #include "instance/instance.hpp"
40*cfb92d14SAndroid Build Coastguard Worker #include "net/ip6.hpp"
41*cfb92d14SAndroid Build Coastguard Worker 
42*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE
43*cfb92d14SAndroid Build Coastguard Worker 
44*cfb92d14SAndroid Build Coastguard Worker namespace ot {
45*cfb92d14SAndroid Build Coastguard Worker namespace BorderRouter {
46*cfb92d14SAndroid Build Coastguard Worker 
47*cfb92d14SAndroid Build Coastguard Worker static ot::Instance *sInstance;
48*cfb92d14SAndroid Build Coastguard Worker 
DumpMessageInHex(const char * prefix,const uint8_t * aBuf,size_t aBufLen)49*cfb92d14SAndroid Build Coastguard Worker void DumpMessageInHex(const char *prefix, const uint8_t *aBuf, size_t aBufLen)
50*cfb92d14SAndroid Build Coastguard Worker {
51*cfb92d14SAndroid Build Coastguard Worker     // This function dumps all packets the output of this function can be imported to packet analyser for debugging.
52*cfb92d14SAndroid Build Coastguard Worker     printf("%s", prefix);
53*cfb92d14SAndroid Build Coastguard Worker     for (size_t i = 0; i < aBufLen; i++)
54*cfb92d14SAndroid Build Coastguard Worker     {
55*cfb92d14SAndroid Build Coastguard Worker         printf("%02x", aBuf[i]);
56*cfb92d14SAndroid Build Coastguard Worker     }
57*cfb92d14SAndroid Build Coastguard Worker     printf("\n");
58*cfb92d14SAndroid Build Coastguard Worker }
59*cfb92d14SAndroid Build Coastguard Worker 
CheckMessage(const Message & aMessage,const uint8_t * aExpectedMessage,size_t aExpectedMessageLen)60*cfb92d14SAndroid Build Coastguard Worker bool CheckMessage(const Message &aMessage, const uint8_t *aExpectedMessage, size_t aExpectedMessageLen)
61*cfb92d14SAndroid Build Coastguard Worker {
62*cfb92d14SAndroid Build Coastguard Worker     uint8_t  readMessage[OPENTHREAD_CONFIG_IP6_MAX_DATAGRAM_LENGTH];
63*cfb92d14SAndroid Build Coastguard Worker     uint16_t messageLength;
64*cfb92d14SAndroid Build Coastguard Worker     bool     success = true;
65*cfb92d14SAndroid Build Coastguard Worker 
66*cfb92d14SAndroid Build Coastguard Worker     success       = success && (aMessage.GetLength() == aExpectedMessageLen);
67*cfb92d14SAndroid Build Coastguard Worker     messageLength = aMessage.ReadBytes(0, readMessage, aMessage.GetLength());
68*cfb92d14SAndroid Build Coastguard Worker     success       = success && (aExpectedMessageLen == messageLength);
69*cfb92d14SAndroid Build Coastguard Worker     success       = success && (memcmp(readMessage, aExpectedMessage, aExpectedMessageLen) == 0);
70*cfb92d14SAndroid Build Coastguard Worker 
71*cfb92d14SAndroid Build Coastguard Worker     if (!success)
72*cfb92d14SAndroid Build Coastguard Worker     {
73*cfb92d14SAndroid Build Coastguard Worker         printf("Expected Message\n");
74*cfb92d14SAndroid Build Coastguard Worker         for (size_t i = 0; i < aExpectedMessageLen; i++)
75*cfb92d14SAndroid Build Coastguard Worker         {
76*cfb92d14SAndroid Build Coastguard Worker             printf("%02x%c", aExpectedMessage[i], " \n"[(i & 0xf) == 0xf]);
77*cfb92d14SAndroid Build Coastguard Worker         }
78*cfb92d14SAndroid Build Coastguard Worker         printf("\n");
79*cfb92d14SAndroid Build Coastguard Worker         printf("Actual Message\n");
80*cfb92d14SAndroid Build Coastguard Worker         for (uint16_t i = 0; i < messageLength; i++)
81*cfb92d14SAndroid Build Coastguard Worker         {
82*cfb92d14SAndroid Build Coastguard Worker             printf("%02x%c", readMessage[i], " \n"[(i & 0xf) == 0xf]);
83*cfb92d14SAndroid Build Coastguard Worker         }
84*cfb92d14SAndroid Build Coastguard Worker         printf("\n");
85*cfb92d14SAndroid Build Coastguard Worker     }
86*cfb92d14SAndroid Build Coastguard Worker 
87*cfb92d14SAndroid Build Coastguard Worker     return success;
88*cfb92d14SAndroid Build Coastguard Worker }
89*cfb92d14SAndroid Build Coastguard Worker 
90*cfb92d14SAndroid Build Coastguard Worker template <size_t N>
TestCase6To4(const char * aTestName,const uint8_t (& aIp6Message)[N],Nat64::Translator::Result aResult,const uint8_t * aOutMessage,size_t aOutMessageLen)91*cfb92d14SAndroid Build Coastguard Worker void TestCase6To4(const char *aTestName,
92*cfb92d14SAndroid Build Coastguard Worker                   const uint8_t (&aIp6Message)[N],
93*cfb92d14SAndroid Build Coastguard Worker                   Nat64::Translator::Result aResult,
94*cfb92d14SAndroid Build Coastguard Worker                   const uint8_t            *aOutMessage,
95*cfb92d14SAndroid Build Coastguard Worker                   size_t                    aOutMessageLen)
96*cfb92d14SAndroid Build Coastguard Worker {
97*cfb92d14SAndroid Build Coastguard Worker     Message *msg = sInstance->Get<Ip6::Ip6>().NewMessage(0);
98*cfb92d14SAndroid Build Coastguard Worker 
99*cfb92d14SAndroid Build Coastguard Worker     printf("Testing NAT64 6 to 4: %s\n", aTestName);
100*cfb92d14SAndroid Build Coastguard Worker 
101*cfb92d14SAndroid Build Coastguard Worker     VerifyOrQuit(msg != nullptr);
102*cfb92d14SAndroid Build Coastguard Worker     SuccessOrQuit(msg->AppendBytes(aIp6Message, N));
103*cfb92d14SAndroid Build Coastguard Worker 
104*cfb92d14SAndroid Build Coastguard Worker     DumpMessageInHex("I ", aIp6Message, N);
105*cfb92d14SAndroid Build Coastguard Worker 
106*cfb92d14SAndroid Build Coastguard Worker     VerifyOrQuit(sInstance->Get<Nat64::Translator>().TranslateFromIp6(*msg) == aResult);
107*cfb92d14SAndroid Build Coastguard Worker 
108*cfb92d14SAndroid Build Coastguard Worker     if (aOutMessage != nullptr)
109*cfb92d14SAndroid Build Coastguard Worker     {
110*cfb92d14SAndroid Build Coastguard Worker         DumpMessageInHex("O ", aOutMessage, aOutMessageLen);
111*cfb92d14SAndroid Build Coastguard Worker         VerifyOrQuit(CheckMessage(*msg, aOutMessage, aOutMessageLen));
112*cfb92d14SAndroid Build Coastguard Worker     }
113*cfb92d14SAndroid Build Coastguard Worker 
114*cfb92d14SAndroid Build Coastguard Worker     printf("  ... PASS\n");
115*cfb92d14SAndroid Build Coastguard Worker }
116*cfb92d14SAndroid Build Coastguard Worker 
117*cfb92d14SAndroid Build Coastguard Worker template <size_t N>
TestCase4To6(const char * aTestName,const uint8_t (& aIp4Message)[N],Nat64::Translator::Result aResult,const uint8_t * aOutMessage,size_t aOutMessageLen)118*cfb92d14SAndroid Build Coastguard Worker void TestCase4To6(const char *aTestName,
119*cfb92d14SAndroid Build Coastguard Worker                   const uint8_t (&aIp4Message)[N],
120*cfb92d14SAndroid Build Coastguard Worker                   Nat64::Translator::Result aResult,
121*cfb92d14SAndroid Build Coastguard Worker                   const uint8_t            *aOutMessage,
122*cfb92d14SAndroid Build Coastguard Worker                   size_t                    aOutMessageLen)
123*cfb92d14SAndroid Build Coastguard Worker {
124*cfb92d14SAndroid Build Coastguard Worker     Message *msg = sInstance->Get<Ip6::Ip6>().NewMessage(0);
125*cfb92d14SAndroid Build Coastguard Worker 
126*cfb92d14SAndroid Build Coastguard Worker     printf("Testing NAT64 4 to 6: %s\n", aTestName);
127*cfb92d14SAndroid Build Coastguard Worker 
128*cfb92d14SAndroid Build Coastguard Worker     VerifyOrQuit(msg != nullptr);
129*cfb92d14SAndroid Build Coastguard Worker     SuccessOrQuit(msg->AppendBytes(aIp4Message, N));
130*cfb92d14SAndroid Build Coastguard Worker 
131*cfb92d14SAndroid Build Coastguard Worker     DumpMessageInHex("I ", aIp4Message, N);
132*cfb92d14SAndroid Build Coastguard Worker 
133*cfb92d14SAndroid Build Coastguard Worker     VerifyOrQuit(sInstance->Get<Nat64::Translator>().TranslateToIp6(*msg) == aResult);
134*cfb92d14SAndroid Build Coastguard Worker 
135*cfb92d14SAndroid Build Coastguard Worker     if (aOutMessage != nullptr)
136*cfb92d14SAndroid Build Coastguard Worker     {
137*cfb92d14SAndroid Build Coastguard Worker         DumpMessageInHex("O ", aOutMessage, aOutMessageLen);
138*cfb92d14SAndroid Build Coastguard Worker         VerifyOrQuit(CheckMessage(*msg, aOutMessage, aOutMessageLen));
139*cfb92d14SAndroid Build Coastguard Worker     }
140*cfb92d14SAndroid Build Coastguard Worker 
141*cfb92d14SAndroid Build Coastguard Worker     printf("  ... PASS\n");
142*cfb92d14SAndroid Build Coastguard Worker }
143*cfb92d14SAndroid Build Coastguard Worker 
TestNat64(void)144*cfb92d14SAndroid Build Coastguard Worker void TestNat64(void)
145*cfb92d14SAndroid Build Coastguard Worker {
146*cfb92d14SAndroid Build Coastguard Worker     Ip6::Prefix  nat64prefix;
147*cfb92d14SAndroid Build Coastguard Worker     Ip4::Cidr    nat64cidr;
148*cfb92d14SAndroid Build Coastguard Worker     Ip6::Address ip6Source;
149*cfb92d14SAndroid Build Coastguard Worker     Ip6::Address ip6Dest;
150*cfb92d14SAndroid Build Coastguard Worker 
151*cfb92d14SAndroid Build Coastguard Worker     sInstance = testInitInstance();
152*cfb92d14SAndroid Build Coastguard Worker 
153*cfb92d14SAndroid Build Coastguard Worker     {
154*cfb92d14SAndroid Build Coastguard Worker         const uint8_t ip6Address[] = {0xfd, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
155*cfb92d14SAndroid Build Coastguard Worker                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
156*cfb92d14SAndroid Build Coastguard Worker         const uint8_t ip4Address[] = {192, 168, 123, 1};
157*cfb92d14SAndroid Build Coastguard Worker 
158*cfb92d14SAndroid Build Coastguard Worker         nat64cidr.Set(ip4Address, 32);
159*cfb92d14SAndroid Build Coastguard Worker         nat64prefix.Set(ip6Address, 96);
160*cfb92d14SAndroid Build Coastguard Worker         SuccessOrQuit(sInstance->Get<Nat64::Translator>().SetIp4Cidr(nat64cidr));
161*cfb92d14SAndroid Build Coastguard Worker         sInstance->Get<Nat64::Translator>().SetNat64Prefix(nat64prefix);
162*cfb92d14SAndroid Build Coastguard Worker     }
163*cfb92d14SAndroid Build Coastguard Worker 
164*cfb92d14SAndroid Build Coastguard Worker     {
165*cfb92d14SAndroid Build Coastguard Worker         // fd02::1               fd01::ac10:f3c5       UDP      52     43981 → 4660 Len=4
166*cfb92d14SAndroid Build Coastguard Worker         const uint8_t kIp6Packet[] = {
167*cfb92d14SAndroid Build Coastguard Worker             0x60, 0x08, 0x6e, 0x38, 0x00, 0x0c, 0x11, 0x40, 0xfd, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
168*cfb92d14SAndroid Build Coastguard Worker             0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfd, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
169*cfb92d14SAndroid Build Coastguard Worker             172,  16,   243,  197,  0xab, 0xcd, 0x12, 0x34, 0x00, 0x0c, 0xe3, 0x31, 0x61, 0x62, 0x63, 0x64,
170*cfb92d14SAndroid Build Coastguard Worker         };
171*cfb92d14SAndroid Build Coastguard Worker         // 192.168.123.1         172.16.243.197        UDP      32     43981 → 4660 Len=4
172*cfb92d14SAndroid Build Coastguard Worker         const uint8_t kIp4Packet[] = {0x45, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x40, 0x11, 0x9f,
173*cfb92d14SAndroid Build Coastguard Worker                                       0x4d, 192,  168,  123,  1,    172,  16,   243,  197,  0xab, 0xcd,
174*cfb92d14SAndroid Build Coastguard Worker                                       0x12, 0x34, 0x00, 0x0c, 0xa1, 0x8d, 0x61, 0x62, 0x63, 0x64};
175*cfb92d14SAndroid Build Coastguard Worker 
176*cfb92d14SAndroid Build Coastguard Worker         TestCase6To4("good v6 udp datagram", kIp6Packet, Nat64::Translator::kForward, kIp4Packet, sizeof(kIp4Packet));
177*cfb92d14SAndroid Build Coastguard Worker     }
178*cfb92d14SAndroid Build Coastguard Worker 
179*cfb92d14SAndroid Build Coastguard Worker     {
180*cfb92d14SAndroid Build Coastguard Worker         // 172.16.243.197        192.168.123.1         UDP      32     43981 → 4660 Len=4
181*cfb92d14SAndroid Build Coastguard Worker         const uint8_t kIp4Packet[] = {0x45, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x11, 0xa0,
182*cfb92d14SAndroid Build Coastguard Worker                                       0x4d, 172,  16,   243,  197,  192,  168,  123,  1,    0xab, 0xcd,
183*cfb92d14SAndroid Build Coastguard Worker                                       0x12, 0x34, 0x00, 0x0c, 0xa1, 0x8d, 0x61, 0x62, 0x63, 0x64};
184*cfb92d14SAndroid Build Coastguard Worker         // fd01::ac10:f3c5       fd02::1               UDP      52     43981 → 4660 Len=4
185*cfb92d14SAndroid Build Coastguard Worker         const uint8_t kIp6Packet[] = {
186*cfb92d14SAndroid Build Coastguard Worker             0x60, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x11, 0x3f, 0xfd, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
187*cfb92d14SAndroid Build Coastguard Worker             0x00, 0x00, 172,  16,   243,  197,  0xfd, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
188*cfb92d14SAndroid Build Coastguard Worker             0x00, 0x00, 0x00, 0x01, 0xab, 0xcd, 0x12, 0x34, 0x00, 0x0c, 0xe3, 0x31, 0x61, 0x62, 0x63, 0x64,
189*cfb92d14SAndroid Build Coastguard Worker         };
190*cfb92d14SAndroid Build Coastguard Worker 
191*cfb92d14SAndroid Build Coastguard Worker         TestCase4To6("good v4 udp datagram", kIp4Packet, Nat64::Translator::kForward, kIp6Packet, sizeof(kIp6Packet));
192*cfb92d14SAndroid Build Coastguard Worker     }
193*cfb92d14SAndroid Build Coastguard Worker 
194*cfb92d14SAndroid Build Coastguard Worker     {
195*cfb92d14SAndroid Build Coastguard Worker         // fd02::1               fd01::ac10:f3c5       TCP      64     43981 → 4660 [ACK] Seq=1 Ack=1 Win=1 Len=4
196*cfb92d14SAndroid Build Coastguard Worker         const uint8_t kIp6Packet[] = {
197*cfb92d14SAndroid Build Coastguard Worker             0x60, 0x08, 0x6e, 0x38, 0x00, 0x18, 0x06, 0x40, 0xfd, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
198*cfb92d14SAndroid Build Coastguard Worker             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfd, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
199*cfb92d14SAndroid Build Coastguard Worker             0x00, 0x00, 0x00, 0x00, 172,  16,   243,  197,  0xab, 0xcd, 0x12, 0x34, 0x87, 0x65, 0x43, 0x21,
200*cfb92d14SAndroid Build Coastguard Worker             0x12, 0x34, 0x56, 0x78, 0x50, 0x10, 0x00, 0x01, 0x5f, 0xf8, 0x00, 0x00, 0x61, 0x62, 0x63, 0x64,
201*cfb92d14SAndroid Build Coastguard Worker         };
202*cfb92d14SAndroid Build Coastguard Worker         // 192.168.123.1         172.16.243.197        TCP      44     43981 → 4660 [ACK] Seq=1 Ack=1 Win=1 Len=4
203*cfb92d14SAndroid Build Coastguard Worker         const uint8_t kIp4Packet[] = {0x45, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x40, 0x06, 0x9f,
204*cfb92d14SAndroid Build Coastguard Worker                                       0x4c, 192,  168,  123,  1,    172,  16,   243,  197,  0xab, 0xcd,
205*cfb92d14SAndroid Build Coastguard Worker                                       0x12, 0x34, 0x87, 0x65, 0x43, 0x21, 0x12, 0x34, 0x56, 0x78, 0x50,
206*cfb92d14SAndroid Build Coastguard Worker                                       0x10, 0x00, 0x01, 0x1e, 0x54, 0x00, 0x00, 0x61, 0x62, 0x63, 0x64};
207*cfb92d14SAndroid Build Coastguard Worker 
208*cfb92d14SAndroid Build Coastguard Worker         TestCase6To4("good v6 tcp datagram", kIp6Packet, Nat64::Translator::kForward, kIp4Packet, sizeof(kIp4Packet));
209*cfb92d14SAndroid Build Coastguard Worker     }
210*cfb92d14SAndroid Build Coastguard Worker 
211*cfb92d14SAndroid Build Coastguard Worker     {
212*cfb92d14SAndroid Build Coastguard Worker         // 172.16.243.197        192.168.123.1         TCP      44     43981 → 4660 [ACK] Seq=1 Ack=1 Win=1 Len=4
213*cfb92d14SAndroid Build Coastguard Worker         const uint8_t kIp4Packet[] = {0x45, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x40, 0x06, 0x9f,
214*cfb92d14SAndroid Build Coastguard Worker                                       0x4c, 172,  16,   243,  197,  192,  168,  123,  1,    0xab, 0xcd,
215*cfb92d14SAndroid Build Coastguard Worker                                       0x12, 0x34, 0x87, 0x65, 0x43, 0x21, 0x12, 0x34, 0x56, 0x78, 0x50,
216*cfb92d14SAndroid Build Coastguard Worker                                       0x10, 0x00, 0x01, 0x1e, 0x54, 0x00, 0x00, 0x61, 0x62, 0x63, 0x64};
217*cfb92d14SAndroid Build Coastguard Worker         // fd01::ac10:f3c5       fd02::1               TCP      64     43981 → 4660 [ACK] Seq=1 Ack=1 Win=1 Len=4
218*cfb92d14SAndroid Build Coastguard Worker         const uint8_t kIp6Packet[] = {
219*cfb92d14SAndroid Build Coastguard Worker             0x60, 0x00, 0x00, 0x00, 0x00, 0x18, 0x06, 0x40, 0xfd, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
220*cfb92d14SAndroid Build Coastguard Worker             0x00, 0x00, 0x00, 0x00, 172,  16,   243,  197,  0xfd, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
221*cfb92d14SAndroid Build Coastguard Worker             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xab, 0xcd, 0x12, 0x34, 0x87, 0x65, 0x43, 0x21,
222*cfb92d14SAndroid Build Coastguard Worker             0x12, 0x34, 0x56, 0x78, 0x50, 0x10, 0x00, 0x01, 0x5f, 0xf8, 0x00, 0x00, 0x61, 0x62, 0x63, 0x64,
223*cfb92d14SAndroid Build Coastguard Worker         };
224*cfb92d14SAndroid Build Coastguard Worker 
225*cfb92d14SAndroid Build Coastguard Worker         TestCase4To6("good v4 tcp datagram", kIp4Packet, Nat64::Translator::kForward, kIp6Packet, sizeof(kIp6Packet));
226*cfb92d14SAndroid Build Coastguard Worker     }
227*cfb92d14SAndroid Build Coastguard Worker 
228*cfb92d14SAndroid Build Coastguard Worker     {
229*cfb92d14SAndroid Build Coastguard Worker         // fd02::1         fd01::ac10:f3c5     ICMPv6   52     Echo (ping) request id=0xaabb, seq=1, hop limit=64
230*cfb92d14SAndroid Build Coastguard Worker         const uint8_t kIp6Packet[] = {
231*cfb92d14SAndroid Build Coastguard Worker             0x60, 0x08, 0x6e, 0x38, 0x00, 0x0c, 0x3a, 0x40, 0xfd, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
232*cfb92d14SAndroid Build Coastguard Worker             0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfd, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
233*cfb92d14SAndroid Build Coastguard Worker             172,  16,   243,  197,  0x80, 0x00, 0x76, 0x59, 0xaa, 0xbb, 0x00, 0x01, 0x61, 0x62, 0x63, 0x64,
234*cfb92d14SAndroid Build Coastguard Worker         };
235*cfb92d14SAndroid Build Coastguard Worker         // 192.168.123.1   172.16.243.197      ICMP     32     Echo (ping) request  id=0xaabb, seq=1/256, ttl=63
236*cfb92d14SAndroid Build Coastguard Worker         const uint8_t kIp4Packet[] = {0x45, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x40, 0x01, 0x9f,
237*cfb92d14SAndroid Build Coastguard Worker                                       0x5d, 192,  168,  123,  1,    172,  16,   243,  197,  0x08, 0x00,
238*cfb92d14SAndroid Build Coastguard Worker                                       0x88, 0x7c, 0xaa, 0xbb, 0x00, 0x01, 0x61, 0x62, 0x63, 0x64};
239*cfb92d14SAndroid Build Coastguard Worker 
240*cfb92d14SAndroid Build Coastguard Worker         TestCase6To4("good v6 icmp ping request datagram", kIp6Packet, Nat64::Translator::kForward, kIp4Packet,
241*cfb92d14SAndroid Build Coastguard Worker                      sizeof(kIp4Packet));
242*cfb92d14SAndroid Build Coastguard Worker     }
243*cfb92d14SAndroid Build Coastguard Worker 
244*cfb92d14SAndroid Build Coastguard Worker     {
245*cfb92d14SAndroid Build Coastguard Worker         // 172.16.243.197        192.168.123.1         ICMP     32     Echo (ping) reply    id=0xaabb, seq=1/256, ttl=63
246*cfb92d14SAndroid Build Coastguard Worker         const uint8_t kIp4Packet[] = {0x45, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x01, 0xa0,
247*cfb92d14SAndroid Build Coastguard Worker                                       0x5d, 172,  16,   243,  197,  192,  168,  123,  1,    0x00, 0x00,
248*cfb92d14SAndroid Build Coastguard Worker                                       0x90, 0x7c, 0xaa, 0xbb, 0x00, 0x01, 0x61, 0x62, 0x63, 0x64};
249*cfb92d14SAndroid Build Coastguard Worker         // fd01::ac10:f3c5       fd02::1               ICMPv6   52     Echo (ping) reply id=0xaabb, seq=1, hop limit=62
250*cfb92d14SAndroid Build Coastguard Worker         const uint8_t kIp6Packet[] = {
251*cfb92d14SAndroid Build Coastguard Worker             0x60, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x3a, 0x3f, 0xfd, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
252*cfb92d14SAndroid Build Coastguard Worker             0x00, 0x00, 172,  16,   243,  197,  0xfd, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
253*cfb92d14SAndroid Build Coastguard Worker             0x00, 0x00, 0x00, 0x01, 0x81, 0x00, 0x75, 0x59, 0xaa, 0xbb, 0x00, 0x01, 0x61, 0x62, 0x63, 0x64,
254*cfb92d14SAndroid Build Coastguard Worker         };
255*cfb92d14SAndroid Build Coastguard Worker 
256*cfb92d14SAndroid Build Coastguard Worker         TestCase4To6("good v4 icmp ping response datagram", kIp4Packet, Nat64::Translator::kForward, kIp6Packet,
257*cfb92d14SAndroid Build Coastguard Worker                      sizeof(kIp6Packet));
258*cfb92d14SAndroid Build Coastguard Worker     }
259*cfb92d14SAndroid Build Coastguard Worker 
260*cfb92d14SAndroid Build Coastguard Worker     {
261*cfb92d14SAndroid Build Coastguard Worker         // fd02::1               N/A                   IPv6     39     Invalid IPv6 header
262*cfb92d14SAndroid Build Coastguard Worker         const uint8_t kIp6Packet[] = {0x60, 0x08, 0x6e, 0x38, 0x00, 0x0c, 0x11, 0x40, 0xfd, 0x02, 0x00, 0x00, 0x00,
263*cfb92d14SAndroid Build Coastguard Worker                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfd, 0x01,
264*cfb92d14SAndroid Build Coastguard Worker                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 172,  16,   243};
265*cfb92d14SAndroid Build Coastguard Worker 
266*cfb92d14SAndroid Build Coastguard Worker         TestCase6To4("bad v6 datagram", kIp6Packet, Nat64::Translator::kDrop, nullptr, 0);
267*cfb92d14SAndroid Build Coastguard Worker     }
268*cfb92d14SAndroid Build Coastguard Worker 
269*cfb92d14SAndroid Build Coastguard Worker     {
270*cfb92d14SAndroid Build Coastguard Worker         // 172.16.243.197        N/A                   IPv4     19     [Malformed Packet]
271*cfb92d14SAndroid Build Coastguard Worker         const uint8_t kIp4Packet[] = {0x45, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x11,
272*cfb92d14SAndroid Build Coastguard Worker                                       0xa0, 0x4c, 172,  16,   243,  197,  192,  168,  123};
273*cfb92d14SAndroid Build Coastguard Worker 
274*cfb92d14SAndroid Build Coastguard Worker         TestCase4To6("bad v4 datagram", kIp4Packet, Nat64::Translator::kDrop, nullptr, 0);
275*cfb92d14SAndroid Build Coastguard Worker     }
276*cfb92d14SAndroid Build Coastguard Worker 
277*cfb92d14SAndroid Build Coastguard Worker     {
278*cfb92d14SAndroid Build Coastguard Worker         // 172.16.243.197        192.168.123.2         UDP      32     43981 → 4660 Len=4
279*cfb92d14SAndroid Build Coastguard Worker         const uint8_t kIp4Packet[] = {0x45, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x11, 0xa0,
280*cfb92d14SAndroid Build Coastguard Worker                                       0x4c, 172,  16,   243,  197,  192,  168,  123,  2,    0xab, 0xcd,
281*cfb92d14SAndroid Build Coastguard Worker                                       0x12, 0x34, 0x00, 0x0c, 0xa1, 0x8c, 0x61, 0x62, 0x63, 0x64};
282*cfb92d14SAndroid Build Coastguard Worker 
283*cfb92d14SAndroid Build Coastguard Worker         TestCase4To6("no v4 mapping", kIp4Packet, Nat64::Translator::kDrop, nullptr, 0);
284*cfb92d14SAndroid Build Coastguard Worker     }
285*cfb92d14SAndroid Build Coastguard Worker 
286*cfb92d14SAndroid Build Coastguard Worker     {
287*cfb92d14SAndroid Build Coastguard Worker         // fd02::2               fd01::ac10:f3c5       UDP      52     43981 → 4660 Len=4
288*cfb92d14SAndroid Build Coastguard Worker         const uint8_t kIp6Packet[] = {
289*cfb92d14SAndroid Build Coastguard Worker             0x60, 0x08, 0x6e, 0x38, 0x00, 0x0c, 0x11, 0x40, 0xfd, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
290*cfb92d14SAndroid Build Coastguard Worker             0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xfd, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
291*cfb92d14SAndroid Build Coastguard Worker             172,  16,   243,  197,  0xab, 0xcd, 0x12, 0x34, 0x00, 0x0c, 0xe3, 0x30, 0x61, 0x62, 0x63, 0x64,
292*cfb92d14SAndroid Build Coastguard Worker         };
293*cfb92d14SAndroid Build Coastguard Worker 
294*cfb92d14SAndroid Build Coastguard Worker         TestCase6To4("mapping pool exhausted", kIp6Packet, Nat64::Translator::kDrop, nullptr, 0);
295*cfb92d14SAndroid Build Coastguard Worker     }
296*cfb92d14SAndroid Build Coastguard Worker 
297*cfb92d14SAndroid Build Coastguard Worker     testFreeInstance(sInstance);
298*cfb92d14SAndroid Build Coastguard Worker }
299*cfb92d14SAndroid Build Coastguard Worker 
300*cfb92d14SAndroid Build Coastguard Worker } // namespace BorderRouter
301*cfb92d14SAndroid Build Coastguard Worker } // namespace ot
302*cfb92d14SAndroid Build Coastguard Worker 
303*cfb92d14SAndroid Build Coastguard Worker #endif // OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE
304*cfb92d14SAndroid Build Coastguard Worker 
main(void)305*cfb92d14SAndroid Build Coastguard Worker int main(void)
306*cfb92d14SAndroid Build Coastguard Worker {
307*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE
308*cfb92d14SAndroid Build Coastguard Worker     ot::BorderRouter::TestNat64();
309*cfb92d14SAndroid Build Coastguard Worker     printf("All tests passed\n");
310*cfb92d14SAndroid Build Coastguard Worker #else  // OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE
311*cfb92d14SAndroid Build Coastguard Worker     printf("NAT64 is not enabled\n");
312*cfb92d14SAndroid Build Coastguard Worker #endif // OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE
313*cfb92d14SAndroid Build Coastguard Worker     return 0;
314*cfb92d14SAndroid Build Coastguard Worker }
315