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