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 "common/encoding.hpp"
30*cfb92d14SAndroid Build Coastguard Worker #include "net/ip6_headers.hpp"
31*cfb92d14SAndroid Build Coastguard Worker
32*cfb92d14SAndroid Build Coastguard Worker #include "test_util.hpp"
33*cfb92d14SAndroid Build Coastguard Worker
34*cfb92d14SAndroid Build Coastguard Worker namespace ot {
35*cfb92d14SAndroid Build Coastguard Worker namespace Ip6 {
36*cfb92d14SAndroid Build Coastguard Worker
VerifyVersionTcFlow(const Header & aHeader,uint8_t aDscp,Ecn aEcn,uint32_t aFlow)37*cfb92d14SAndroid Build Coastguard Worker void VerifyVersionTcFlow(const Header &aHeader, uint8_t aDscp, Ecn aEcn, uint32_t aFlow)
38*cfb92d14SAndroid Build Coastguard Worker {
39*cfb92d14SAndroid Build Coastguard Worker uint8_t expectedTc = static_cast<uint8_t>((aDscp << 2) + aEcn);
40*cfb92d14SAndroid Build Coastguard Worker uint32_t expectedVerTcFlow = 0x60000000 + (static_cast<uint32_t>(expectedTc) << 20) + aFlow;
41*cfb92d14SAndroid Build Coastguard Worker
42*cfb92d14SAndroid Build Coastguard Worker printf("%08x {dscp:%d, ecn:%d, flow:%d}\n", aHeader.GetVerionTrafficClassFlow(), aHeader.GetDscp(),
43*cfb92d14SAndroid Build Coastguard Worker aHeader.GetEcn(), aHeader.GetFlow());
44*cfb92d14SAndroid Build Coastguard Worker
45*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aHeader.IsVersion6());
46*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aHeader.GetDscp() == aDscp);
47*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aHeader.GetEcn() == aEcn);
48*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aHeader.GetFlow() == aFlow);
49*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aHeader.GetTrafficClass() == expectedTc);
50*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aHeader.GetVerionTrafficClassFlow() == expectedVerTcFlow);
51*cfb92d14SAndroid Build Coastguard Worker }
52*cfb92d14SAndroid Build Coastguard Worker
TestIp6Header(void)53*cfb92d14SAndroid Build Coastguard Worker void TestIp6Header(void)
54*cfb92d14SAndroid Build Coastguard Worker {
55*cfb92d14SAndroid Build Coastguard Worker static constexpr uint16_t kPayloadLength = 650;
56*cfb92d14SAndroid Build Coastguard Worker static constexpr uint8_t kHopLimit = 0xd1;
57*cfb92d14SAndroid Build Coastguard Worker
58*cfb92d14SAndroid Build Coastguard Worker const uint32_t kFlows[] = {0x0, 0x1, 0xfff, 0xffff, 0xff000, 0xfffff};
59*cfb92d14SAndroid Build Coastguard Worker const uint8_t kDscps[] = {0x0, 0x1, 0x3, 0xf, 0x30, 0x2f, 0x3f};
60*cfb92d14SAndroid Build Coastguard Worker const Ecn kEcns[] = {kEcnNotCapable, kEcnCapable0, kEcnCapable1, kEcnMarked};
61*cfb92d14SAndroid Build Coastguard Worker
62*cfb92d14SAndroid Build Coastguard Worker Header header;
63*cfb92d14SAndroid Build Coastguard Worker Address source;
64*cfb92d14SAndroid Build Coastguard Worker Address destination;
65*cfb92d14SAndroid Build Coastguard Worker const uint8_t *headerBytes = reinterpret_cast<const uint8_t *>(&header);
66*cfb92d14SAndroid Build Coastguard Worker
67*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(source.FromString("0102:0304:0506:0708:090a:0b0c:0d0e:0f12"), "Address::FromString() failed");
68*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(destination.FromString("1122:3344:5566::7788:99aa:bbcc:ddee:ff23"), "Address::FromString() failed");
69*cfb92d14SAndroid Build Coastguard Worker
70*cfb92d14SAndroid Build Coastguard Worker header.InitVersionTrafficClassFlow();
71*cfb92d14SAndroid Build Coastguard Worker VerifyVersionTcFlow(header, kDscpCs0, kEcnNotCapable, 0);
72*cfb92d14SAndroid Build Coastguard Worker
73*cfb92d14SAndroid Build Coastguard Worker header.Clear();
74*cfb92d14SAndroid Build Coastguard Worker header.InitVersionTrafficClassFlow();
75*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(header.IsValid());
76*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(header.GetPayloadLength() == 0);
77*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(header.GetNextHeader() == 0);
78*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(header.GetHopLimit() == 0);
79*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(header.GetSource().IsUnspecified());
80*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(header.GetDestination().IsUnspecified());
81*cfb92d14SAndroid Build Coastguard Worker
82*cfb92d14SAndroid Build Coastguard Worker header.SetPayloadLength(kPayloadLength);
83*cfb92d14SAndroid Build Coastguard Worker header.SetNextHeader(kProtoUdp);
84*cfb92d14SAndroid Build Coastguard Worker header.SetHopLimit(kHopLimit);
85*cfb92d14SAndroid Build Coastguard Worker header.SetSource(source);
86*cfb92d14SAndroid Build Coastguard Worker header.SetDestination(destination);
87*cfb92d14SAndroid Build Coastguard Worker
88*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(header.IsValid());
89*cfb92d14SAndroid Build Coastguard Worker VerifyVersionTcFlow(header, kDscpCs0, kEcnNotCapable, 0);
90*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(header.GetPayloadLength() == kPayloadLength);
91*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(header.GetNextHeader() == kProtoUdp);
92*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(header.GetHopLimit() == kHopLimit);
93*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(header.GetSource() == source);
94*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(header.GetDestination() == destination);
95*cfb92d14SAndroid Build Coastguard Worker
96*cfb92d14SAndroid Build Coastguard Worker // Verify the offsets to different fields.
97*cfb92d14SAndroid Build Coastguard Worker
98*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(BigEndian::ReadUint16(headerBytes + Header::kPayloadLengthFieldOffset) == kPayloadLength,
99*cfb92d14SAndroid Build Coastguard Worker "kPayloadLengthFieldOffset is incorrect");
100*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(headerBytes[Header::kNextHeaderFieldOffset] == kProtoUdp, "kNextHeaderFieldOffset is incorrect");
101*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(headerBytes[Header::kHopLimitFieldOffset] == kHopLimit, "kHopLimitFieldOffset is incorrect");
102*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(memcmp(&headerBytes[Header::kSourceFieldOffset], &source, sizeof(source)) == 0,
103*cfb92d14SAndroid Build Coastguard Worker "kSourceFieldOffset is incorrect");
104*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(memcmp(&headerBytes[Header::kDestinationFieldOffset], &destination, sizeof(destination)) == 0,
105*cfb92d14SAndroid Build Coastguard Worker "kSourceFieldOffset is incorrect");
106*cfb92d14SAndroid Build Coastguard Worker
107*cfb92d14SAndroid Build Coastguard Worker for (uint32_t flow : kFlows)
108*cfb92d14SAndroid Build Coastguard Worker {
109*cfb92d14SAndroid Build Coastguard Worker for (uint8_t dscp : kDscps)
110*cfb92d14SAndroid Build Coastguard Worker {
111*cfb92d14SAndroid Build Coastguard Worker for (Ecn ecn : kEcns)
112*cfb92d14SAndroid Build Coastguard Worker {
113*cfb92d14SAndroid Build Coastguard Worker printf("Expecting {dscp:%-2d, ecn:%d, flow:%-7d} => ", dscp, ecn, flow);
114*cfb92d14SAndroid Build Coastguard Worker header.SetEcn(ecn);
115*cfb92d14SAndroid Build Coastguard Worker header.SetDscp(dscp);
116*cfb92d14SAndroid Build Coastguard Worker header.SetFlow(flow);
117*cfb92d14SAndroid Build Coastguard Worker VerifyVersionTcFlow(header, dscp, ecn, flow);
118*cfb92d14SAndroid Build Coastguard Worker }
119*cfb92d14SAndroid Build Coastguard Worker }
120*cfb92d14SAndroid Build Coastguard Worker }
121*cfb92d14SAndroid Build Coastguard Worker
122*cfb92d14SAndroid Build Coastguard Worker // Verify out of range values.
123*cfb92d14SAndroid Build Coastguard Worker header.InitVersionTrafficClassFlow();
124*cfb92d14SAndroid Build Coastguard Worker
125*cfb92d14SAndroid Build Coastguard Worker header.SetFlow(0xff000001);
126*cfb92d14SAndroid Build Coastguard Worker VerifyVersionTcFlow(header, 0, kEcnNotCapable, 1);
127*cfb92d14SAndroid Build Coastguard Worker
128*cfb92d14SAndroid Build Coastguard Worker header.SetDscp(0xef);
129*cfb92d14SAndroid Build Coastguard Worker VerifyVersionTcFlow(header, 0x2f, kEcnNotCapable, 1);
130*cfb92d14SAndroid Build Coastguard Worker }
131*cfb92d14SAndroid Build Coastguard Worker
132*cfb92d14SAndroid Build Coastguard Worker } // namespace Ip6
133*cfb92d14SAndroid Build Coastguard Worker } // namespace ot
134*cfb92d14SAndroid Build Coastguard Worker
main(void)135*cfb92d14SAndroid Build Coastguard Worker int main(void)
136*cfb92d14SAndroid Build Coastguard Worker {
137*cfb92d14SAndroid Build Coastguard Worker ot::Ip6::TestIp6Header();
138*cfb92d14SAndroid Build Coastguard Worker printf("All tests passed\n");
139*cfb92d14SAndroid Build Coastguard Worker return 0;
140*cfb92d14SAndroid Build Coastguard Worker }
141