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/ip4_types.hpp"
31 
32 #include "test_util.hpp"
33 
34 namespace ot {
35 namespace Ip4 {
36 
VerifyEcnDscp(const Header & aHeader,uint8_t aDscp,Ecn aEcn)37 void VerifyEcnDscp(const Header &aHeader, uint8_t aDscp, Ecn aEcn)
38 {
39     uint8_t expectedDscpEcn = static_cast<uint8_t>((aDscp << 2) + aEcn);
40 
41     printf("%02x {dscp:%d, ecn:%d}\n", aHeader.GetDscpEcn(), aHeader.GetDscp(), aHeader.GetEcn());
42 
43     VerifyOrQuit(aHeader.GetDscp() == aDscp);
44     VerifyOrQuit(aHeader.GetEcn() == aEcn);
45     VerifyOrQuit(aHeader.GetDscpEcn() == expectedDscpEcn);
46 }
47 
TestIp4Header(void)48 void TestIp4Header(void)
49 {
50     static constexpr uint16_t kTotalLength = 84;
51     static constexpr uint8_t  kTtl         = 64;
52 
53     const uint8_t kDscps[]            = {0x0, 0x1, 0x3, 0xf, 0x30, 0x2f, 0x3f};
54     const Ecn     kEcns[]             = {Ecn::kEcnNotCapable, Ecn::kEcnCapable0, Ecn::kEcnCapable1, Ecn::kEcnMarked};
55     const uint8_t kExampleIp4Header[] = "\x45\x00\x00\x54\x23\xed\x00\x00\x40\x01\x41\xd1\x0a\x00\x00\xeb"
56                                         "\x0a\x00\x00\x01";
57 
58     Header         header;
59     Address        source;
60     Address        destination;
61     const uint8_t *headerBytes = reinterpret_cast<const uint8_t *>(&header);
62 
63     SuccessOrQuit(source.FromString("10.0.0.235"), "Address::FromString() failed");
64     SuccessOrQuit(destination.FromString("10.0.0.1"), "Address::FromString() failed");
65 
66     header.InitVersionIhl();
67 
68     header.Clear();
69     header.InitVersionIhl();
70     VerifyOrQuit(header.IsValid());
71     VerifyOrQuit(header.GetTotalLength() == 0);
72     VerifyOrQuit(header.GetProtocol() == 0);
73     VerifyOrQuit(header.GetTtl() == 0);
74     VerifyOrQuit(header.GetSource().mFields.m32 == 0);
75     VerifyOrQuit(header.GetDestination().mFields.m32 == 0);
76     VerifyOrQuit(header.GetFragmentOffset() == 0);
77 
78     header.SetTotalLength(kTotalLength);
79     header.SetProtocol(Ip4::kProtoIcmp);
80     header.SetTtl(kTtl);
81     header.SetSource(source);
82     header.SetDestination(destination);
83 
84     VerifyOrQuit(header.IsValid());
85     VerifyOrQuit(header.GetProtocol() == kProtoIcmp);
86     VerifyOrQuit(header.GetTtl() == kTtl);
87     VerifyOrQuit(header.GetSource() == source);
88     VerifyOrQuit(header.GetDestination() == destination);
89 
90     // Verify the offsets to different fields.
91 
92     VerifyOrQuit(BigEndian::ReadUint16(headerBytes + Header::kTotalLengthOffset) == kTotalLength,
93                  "kTotalLength is incorrect");
94     VerifyOrQuit(headerBytes[Header::kProtocolOffset] == kProtoIcmp, "kProtocol is incorrect");
95     VerifyOrQuit(headerBytes[Header::kTtlOffset] == kTtl, "kTtl is incorrect");
96     VerifyOrQuit(memcmp(&headerBytes[Header::kSourceAddressOffset], &source, sizeof(source)) == 0,
97                  "Source address is incorrect");
98     VerifyOrQuit(memcmp(&headerBytes[Header::kDestinationAddressOffset], &destination, sizeof(destination)) == 0,
99                  "Destination address is incorrect");
100 
101     for (uint8_t dscp : kDscps)
102     {
103         for (Ecn ecn : kEcns)
104         {
105             printf("Expecting {dscp:%-2d, ecn:%d} => ", dscp, ecn);
106             header.SetEcn(ecn);
107             header.SetDscp(dscp);
108             VerifyEcnDscp(header, dscp, ecn);
109         }
110     }
111 
112     memcpy(&header, kExampleIp4Header, sizeof(header));
113     VerifyOrQuit(header.IsValid());
114     VerifyOrQuit(memcmp(&headerBytes[Header::kSourceAddressOffset], &source, sizeof(source)) == 0,
115                  "Source address is incorrect");
116     VerifyOrQuit(memcmp(&headerBytes[Header::kDestinationAddressOffset], &destination, sizeof(destination)) == 0,
117                  "Destination address is incorrect");
118     VerifyOrQuit(BigEndian::ReadUint16(headerBytes + Header::kTotalLengthOffset) == kTotalLength,
119                  "kTotalLength is incorrect");
120     VerifyOrQuit(headerBytes[Header::kProtocolOffset] == kProtoIcmp, "kProtocol is incorrect");
121     VerifyOrQuit(headerBytes[Header::kTtlOffset] == kTtl, "kTtl is incorrect");
122 }
123 
124 } // namespace Ip4
125 } // namespace ot
126 
main(void)127 int main(void)
128 {
129     ot::Ip4::TestIp4Header();
130     printf("All tests passed\n");
131     return 0;
132 }
133