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