xref: /aosp_15_r20/external/openthread/tests/unit/test_toolchain.cpp (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker /*
2*cfb92d14SAndroid Build Coastguard Worker  *  Copyright (c) 2016, 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 <stdint.h>
30*cfb92d14SAndroid Build Coastguard Worker #include <stdio.h>
31*cfb92d14SAndroid Build Coastguard Worker 
32*cfb92d14SAndroid Build Coastguard Worker #include <openthread/ip6.h>
33*cfb92d14SAndroid Build Coastguard Worker #include <openthread/platform/toolchain.h>
34*cfb92d14SAndroid Build Coastguard Worker 
35*cfb92d14SAndroid Build Coastguard Worker #include "test_util.h"
36*cfb92d14SAndroid Build Coastguard Worker #include "thread/neighbor.hpp"
37*cfb92d14SAndroid Build Coastguard Worker 
38*cfb92d14SAndroid Build Coastguard Worker namespace ot {
39*cfb92d14SAndroid Build Coastguard Worker 
40*cfb92d14SAndroid Build Coastguard Worker extern "C" {
41*cfb92d14SAndroid Build Coastguard Worker uint32_t       otNetifAddress_Size_c();
42*cfb92d14SAndroid Build Coastguard Worker uint32_t       otNetifAddress_offset_mNext_c();
43*cfb92d14SAndroid Build Coastguard Worker otNetifAddress CreateNetif_c();
44*cfb92d14SAndroid Build Coastguard Worker }
45*cfb92d14SAndroid Build Coastguard Worker 
test_packed1(void)46*cfb92d14SAndroid Build Coastguard Worker void test_packed1(void)
47*cfb92d14SAndroid Build Coastguard Worker {
48*cfb92d14SAndroid Build Coastguard Worker     OT_TOOL_PACKED_BEGIN
49*cfb92d14SAndroid Build Coastguard Worker     struct packed_t
50*cfb92d14SAndroid Build Coastguard Worker     {
51*cfb92d14SAndroid Build Coastguard Worker         uint8_t  mByte;
52*cfb92d14SAndroid Build Coastguard Worker         uint32_t mWord;
53*cfb92d14SAndroid Build Coastguard Worker         uint16_t mShort;
54*cfb92d14SAndroid Build Coastguard Worker     } OT_TOOL_PACKED_END;
55*cfb92d14SAndroid Build Coastguard Worker 
56*cfb92d14SAndroid Build Coastguard Worker     static_assert(sizeof(packed_t) == 7, "packed_t should be packed to 7 bytes");
57*cfb92d14SAndroid Build Coastguard Worker 
58*cfb92d14SAndroid Build Coastguard Worker     VerifyOrQuit(sizeof(packed_t) == 7, "OT_TOOL_PACKED failed 1");
59*cfb92d14SAndroid Build Coastguard Worker }
60*cfb92d14SAndroid Build Coastguard Worker 
test_packed2(void)61*cfb92d14SAndroid Build Coastguard Worker void test_packed2(void)
62*cfb92d14SAndroid Build Coastguard Worker {
63*cfb92d14SAndroid Build Coastguard Worker     OT_TOOL_PACKED_BEGIN
64*cfb92d14SAndroid Build Coastguard Worker     struct packed_t
65*cfb92d14SAndroid Build Coastguard Worker     {
66*cfb92d14SAndroid Build Coastguard Worker         uint8_t mBytes[3];
67*cfb92d14SAndroid Build Coastguard Worker         uint8_t mByte;
68*cfb92d14SAndroid Build Coastguard Worker     } OT_TOOL_PACKED_END;
69*cfb92d14SAndroid Build Coastguard Worker 
70*cfb92d14SAndroid Build Coastguard Worker     static_assert(sizeof(packed_t) == 4, "packed_t should be packed to 4 bytes");
71*cfb92d14SAndroid Build Coastguard Worker 
72*cfb92d14SAndroid Build Coastguard Worker     VerifyOrQuit(sizeof(packed_t) == 4, "OT_TOOL_PACKED failed 2");
73*cfb92d14SAndroid Build Coastguard Worker }
74*cfb92d14SAndroid Build Coastguard Worker 
test_packed_union(void)75*cfb92d14SAndroid Build Coastguard Worker void test_packed_union(void)
76*cfb92d14SAndroid Build Coastguard Worker {
77*cfb92d14SAndroid Build Coastguard Worker     typedef struct
78*cfb92d14SAndroid Build Coastguard Worker     {
79*cfb92d14SAndroid Build Coastguard Worker         uint16_t mField;
80*cfb92d14SAndroid Build Coastguard Worker     } nested_t;
81*cfb92d14SAndroid Build Coastguard Worker 
82*cfb92d14SAndroid Build Coastguard Worker     OT_TOOL_PACKED_BEGIN
83*cfb92d14SAndroid Build Coastguard Worker     struct packed_t
84*cfb92d14SAndroid Build Coastguard Worker     {
85*cfb92d14SAndroid Build Coastguard Worker         uint8_t mBytes[3];
86*cfb92d14SAndroid Build Coastguard Worker         union
87*cfb92d14SAndroid Build Coastguard Worker         {
88*cfb92d14SAndroid Build Coastguard Worker             nested_t mNestedStruct;
89*cfb92d14SAndroid Build Coastguard Worker             uint8_t  mByte;
90*cfb92d14SAndroid Build Coastguard Worker         } OT_TOOL_PACKED_FIELD;
91*cfb92d14SAndroid Build Coastguard Worker     } OT_TOOL_PACKED_END;
92*cfb92d14SAndroid Build Coastguard Worker 
93*cfb92d14SAndroid Build Coastguard Worker     static_assert(sizeof(packed_t) == 5, "packed_t should be packed to 5 bytes");
94*cfb92d14SAndroid Build Coastguard Worker 
95*cfb92d14SAndroid Build Coastguard Worker     VerifyOrQuit(sizeof(packed_t) == 5, "OT_TOOL_PACKED failed 3");
96*cfb92d14SAndroid Build Coastguard Worker }
97*cfb92d14SAndroid Build Coastguard Worker 
test_packed_enum(void)98*cfb92d14SAndroid Build Coastguard Worker void test_packed_enum(void)
99*cfb92d14SAndroid Build Coastguard Worker {
100*cfb92d14SAndroid Build Coastguard Worker     Neighbor neighbor;
101*cfb92d14SAndroid Build Coastguard Worker     neighbor.SetState(Neighbor::kStateValid);
102*cfb92d14SAndroid Build Coastguard Worker 
103*cfb92d14SAndroid Build Coastguard Worker     // Make sure that when we read the 3 bit field it is read as unsigned, so it return '4'
104*cfb92d14SAndroid Build Coastguard Worker     VerifyOrQuit(neighbor.GetState() == Neighbor::kStateValid, "OT_TOOL_PACKED failed 4");
105*cfb92d14SAndroid Build Coastguard Worker }
106*cfb92d14SAndroid Build Coastguard Worker 
test_addr_sizes(void)107*cfb92d14SAndroid Build Coastguard Worker void test_addr_sizes(void)
108*cfb92d14SAndroid Build Coastguard Worker {
109*cfb92d14SAndroid Build Coastguard Worker     VerifyOrQuit(offsetof(otNetifAddress, mNext) == otNetifAddress_offset_mNext_c(),
110*cfb92d14SAndroid Build Coastguard Worker                  "mNext should offset the same in C & C++");
111*cfb92d14SAndroid Build Coastguard Worker     VerifyOrQuit(sizeof(otNetifAddress) == otNetifAddress_Size_c(), "otNetifAddress should the same in C & C++");
112*cfb92d14SAndroid Build Coastguard Worker }
113*cfb92d14SAndroid Build Coastguard Worker 
test_addr_bitfield(void)114*cfb92d14SAndroid Build Coastguard Worker void test_addr_bitfield(void) { VerifyOrQuit(CreateNetif_c().mScopeOverrideValid == true, "test_addr_size_cpp"); }
115*cfb92d14SAndroid Build Coastguard Worker 
test_packed_alignment(void)116*cfb92d14SAndroid Build Coastguard Worker void test_packed_alignment(void)
117*cfb92d14SAndroid Build Coastguard Worker {
118*cfb92d14SAndroid Build Coastguard Worker     OT_TOOL_PACKED_BEGIN
119*cfb92d14SAndroid Build Coastguard Worker     struct PackedStruct
120*cfb92d14SAndroid Build Coastguard Worker     {
121*cfb92d14SAndroid Build Coastguard Worker         uint32_t mUint32;
122*cfb92d14SAndroid Build Coastguard Worker         uint8_t  mByte;
123*cfb92d14SAndroid Build Coastguard Worker         uint16_t mUint16;
124*cfb92d14SAndroid Build Coastguard Worker     } OT_TOOL_PACKED_END;
125*cfb92d14SAndroid Build Coastguard Worker 
126*cfb92d14SAndroid Build Coastguard Worker     PackedStruct   packedStruct;
127*cfb92d14SAndroid Build Coastguard Worker     PackedStruct   packedStructCopy;
128*cfb92d14SAndroid Build Coastguard Worker     const uint8_t *packedStructBytes = reinterpret_cast<const uint8_t *>(&packedStruct);
129*cfb92d14SAndroid Build Coastguard Worker     uint8_t        buffer[sizeof(PackedStruct) * 2 + 1];
130*cfb92d14SAndroid Build Coastguard Worker 
131*cfb92d14SAndroid Build Coastguard Worker     VerifyOrQuit(sizeof(PackedStruct) == 7, "OT_TOOL_PACKED failed");
132*cfb92d14SAndroid Build Coastguard Worker 
133*cfb92d14SAndroid Build Coastguard Worker     packedStruct.mUint32 = 0x12345678;
134*cfb92d14SAndroid Build Coastguard Worker     packedStruct.mByte   = 0xfe;
135*cfb92d14SAndroid Build Coastguard Worker     packedStruct.mUint16 = 0xabcd;
136*cfb92d14SAndroid Build Coastguard Worker 
137*cfb92d14SAndroid Build Coastguard Worker     for (size_t start = 0; start < sizeof(PackedStruct); start++)
138*cfb92d14SAndroid Build Coastguard Worker     {
139*cfb92d14SAndroid Build Coastguard Worker         uint8_t *ptr = &buffer[start];
140*cfb92d14SAndroid Build Coastguard Worker 
141*cfb92d14SAndroid Build Coastguard Worker         memset(buffer, 0, sizeof(buffer));
142*cfb92d14SAndroid Build Coastguard Worker 
143*cfb92d14SAndroid Build Coastguard Worker         *reinterpret_cast<PackedStruct *>(ptr) = packedStruct;
144*cfb92d14SAndroid Build Coastguard Worker 
145*cfb92d14SAndroid Build Coastguard Worker         for (size_t i = 0; i < start; i++)
146*cfb92d14SAndroid Build Coastguard Worker         {
147*cfb92d14SAndroid Build Coastguard Worker             VerifyOrQuit(buffer[i] == 0, "OT_TOOL_PACKED alignment failed - pre-size write");
148*cfb92d14SAndroid Build Coastguard Worker         }
149*cfb92d14SAndroid Build Coastguard Worker 
150*cfb92d14SAndroid Build Coastguard Worker         VerifyOrQuit(memcmp(ptr, packedStructBytes, sizeof(PackedStruct)) == 0, "OT_TOOL_PACKED alignment failed");
151*cfb92d14SAndroid Build Coastguard Worker 
152*cfb92d14SAndroid Build Coastguard Worker         for (size_t i = start + sizeof(packedStruct); i < sizeof(buffer); i++)
153*cfb92d14SAndroid Build Coastguard Worker         {
154*cfb92d14SAndroid Build Coastguard Worker             VerifyOrQuit(buffer[i] == 0, "OT_TOOL_PACKED alignment failed - post-size write");
155*cfb92d14SAndroid Build Coastguard Worker         }
156*cfb92d14SAndroid Build Coastguard Worker 
157*cfb92d14SAndroid Build Coastguard Worker         memset(&packedStructCopy, 0, sizeof(PackedStruct));
158*cfb92d14SAndroid Build Coastguard Worker         packedStructCopy = *reinterpret_cast<PackedStruct *>(ptr);
159*cfb92d14SAndroid Build Coastguard Worker 
160*cfb92d14SAndroid Build Coastguard Worker         VerifyOrQuit(memcmp(&packedStructCopy, &packedStruct, sizeof(PackedStruct)) == 0,
161*cfb92d14SAndroid Build Coastguard Worker                      "OT_TOOL_PACKED failed - read error");
162*cfb92d14SAndroid Build Coastguard Worker     }
163*cfb92d14SAndroid Build Coastguard Worker }
164*cfb92d14SAndroid Build Coastguard Worker 
TestToolchain(void)165*cfb92d14SAndroid Build Coastguard Worker void TestToolchain(void)
166*cfb92d14SAndroid Build Coastguard Worker {
167*cfb92d14SAndroid Build Coastguard Worker     test_packed1();
168*cfb92d14SAndroid Build Coastguard Worker     test_packed2();
169*cfb92d14SAndroid Build Coastguard Worker     test_packed_union();
170*cfb92d14SAndroid Build Coastguard Worker     test_packed_enum();
171*cfb92d14SAndroid Build Coastguard Worker     test_addr_sizes();
172*cfb92d14SAndroid Build Coastguard Worker     test_addr_bitfield();
173*cfb92d14SAndroid Build Coastguard Worker     test_packed_alignment();
174*cfb92d14SAndroid Build Coastguard Worker }
175*cfb92d14SAndroid Build Coastguard Worker 
176*cfb92d14SAndroid Build Coastguard Worker } // namespace ot
177*cfb92d14SAndroid Build Coastguard Worker 
main(void)178*cfb92d14SAndroid Build Coastguard Worker int main(void)
179*cfb92d14SAndroid Build Coastguard Worker {
180*cfb92d14SAndroid Build Coastguard Worker     ot::TestToolchain();
181*cfb92d14SAndroid Build Coastguard Worker     printf("All tests passed\n");
182*cfb92d14SAndroid Build Coastguard Worker     return 0;
183*cfb92d14SAndroid Build Coastguard Worker }
184