xref: /aosp_15_r20/external/openthread/tests/unit/test_heap.cpp (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker /*
2*cfb92d14SAndroid Build Coastguard Worker  *  Copyright (c) 2017, 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 <openthread/config.h>
30*cfb92d14SAndroid Build Coastguard Worker 
31*cfb92d14SAndroid Build Coastguard Worker #include "core/utils/heap.hpp"
32*cfb92d14SAndroid Build Coastguard Worker 
33*cfb92d14SAndroid Build Coastguard Worker #include <stdlib.h>
34*cfb92d14SAndroid Build Coastguard Worker 
35*cfb92d14SAndroid Build Coastguard Worker #include "common/debug.hpp"
36*cfb92d14SAndroid Build Coastguard Worker #include "crypto/aes_ccm.hpp"
37*cfb92d14SAndroid Build Coastguard Worker 
38*cfb92d14SAndroid Build Coastguard Worker #include "test_platform.h"
39*cfb92d14SAndroid Build Coastguard Worker #include "test_util.h"
40*cfb92d14SAndroid Build Coastguard Worker 
41*cfb92d14SAndroid Build Coastguard Worker namespace ot {
42*cfb92d14SAndroid Build Coastguard Worker 
43*cfb92d14SAndroid Build Coastguard Worker #if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
44*cfb92d14SAndroid Build Coastguard Worker 
45*cfb92d14SAndroid Build Coastguard Worker /**
46*cfb92d14SAndroid Build Coastguard Worker  * Verifies single variable allocating and freeing.
47*cfb92d14SAndroid Build Coastguard Worker  *
48*cfb92d14SAndroid Build Coastguard Worker  */
TestAllocateSingle(void)49*cfb92d14SAndroid Build Coastguard Worker void TestAllocateSingle(void)
50*cfb92d14SAndroid Build Coastguard Worker {
51*cfb92d14SAndroid Build Coastguard Worker     ot::Utils::Heap heap;
52*cfb92d14SAndroid Build Coastguard Worker 
53*cfb92d14SAndroid Build Coastguard Worker     const size_t totalSize = heap.GetFreeSize();
54*cfb92d14SAndroid Build Coastguard Worker 
55*cfb92d14SAndroid Build Coastguard Worker     {
56*cfb92d14SAndroid Build Coastguard Worker         void *p = heap.CAlloc(1, 0);
57*cfb92d14SAndroid Build Coastguard Worker         VerifyOrQuit(p == nullptr && totalSize == heap.GetFreeSize(), "TestAllocateSingle allocate 1 x 0 byte failed!");
58*cfb92d14SAndroid Build Coastguard Worker         heap.Free(p);
59*cfb92d14SAndroid Build Coastguard Worker 
60*cfb92d14SAndroid Build Coastguard Worker         p = heap.CAlloc(0, 1);
61*cfb92d14SAndroid Build Coastguard Worker         VerifyOrQuit(p == nullptr && totalSize == heap.GetFreeSize(), "TestAllocateSingle allocate 0 x 1 byte failed!");
62*cfb92d14SAndroid Build Coastguard Worker         heap.Free(p);
63*cfb92d14SAndroid Build Coastguard Worker     }
64*cfb92d14SAndroid Build Coastguard Worker 
65*cfb92d14SAndroid Build Coastguard Worker     for (size_t size = 1; size <= heap.GetCapacity(); ++size)
66*cfb92d14SAndroid Build Coastguard Worker     {
67*cfb92d14SAndroid Build Coastguard Worker         printf("%s allocating %zu bytes...\n", __func__, size);
68*cfb92d14SAndroid Build Coastguard Worker         void *p = heap.CAlloc(1, size);
69*cfb92d14SAndroid Build Coastguard Worker         VerifyOrQuit(p != nullptr && !heap.IsClean() && heap.GetFreeSize() + size <= totalSize, "allocating failed!");
70*cfb92d14SAndroid Build Coastguard Worker         memset(p, 0xff, size);
71*cfb92d14SAndroid Build Coastguard Worker         heap.Free(p);
72*cfb92d14SAndroid Build Coastguard Worker         VerifyOrQuit(heap.IsClean() && heap.GetFreeSize() == totalSize, "freeing failed!\n");
73*cfb92d14SAndroid Build Coastguard Worker     }
74*cfb92d14SAndroid Build Coastguard Worker }
75*cfb92d14SAndroid Build Coastguard Worker 
76*cfb92d14SAndroid Build Coastguard Worker /**
77*cfb92d14SAndroid Build Coastguard Worker  * Verifies randomly allocating and freeing variables.
78*cfb92d14SAndroid Build Coastguard Worker  *
79*cfb92d14SAndroid Build Coastguard Worker  * @param[in]   aSizeLimit  The maximum allocation size.
80*cfb92d14SAndroid Build Coastguard Worker  * @param[in]   aSeed       The seed for generating random sizes.
81*cfb92d14SAndroid Build Coastguard Worker  *
82*cfb92d14SAndroid Build Coastguard Worker  */
TestAllocateRandomly(size_t aSizeLimit,unsigned int aSeed)83*cfb92d14SAndroid Build Coastguard Worker void TestAllocateRandomly(size_t aSizeLimit, unsigned int aSeed)
84*cfb92d14SAndroid Build Coastguard Worker {
85*cfb92d14SAndroid Build Coastguard Worker     struct Node
86*cfb92d14SAndroid Build Coastguard Worker     {
87*cfb92d14SAndroid Build Coastguard Worker         Node  *mNext;
88*cfb92d14SAndroid Build Coastguard Worker         size_t mSize;
89*cfb92d14SAndroid Build Coastguard Worker     };
90*cfb92d14SAndroid Build Coastguard Worker 
91*cfb92d14SAndroid Build Coastguard Worker     ot::Utils::Heap heap;
92*cfb92d14SAndroid Build Coastguard Worker     Node            head;
93*cfb92d14SAndroid Build Coastguard Worker     size_t          nnodes = 0;
94*cfb92d14SAndroid Build Coastguard Worker 
95*cfb92d14SAndroid Build Coastguard Worker     srand(aSeed);
96*cfb92d14SAndroid Build Coastguard Worker 
97*cfb92d14SAndroid Build Coastguard Worker     const size_t totalSize = heap.GetFreeSize();
98*cfb92d14SAndroid Build Coastguard Worker     Node        *last      = &head;
99*cfb92d14SAndroid Build Coastguard Worker 
100*cfb92d14SAndroid Build Coastguard Worker     do
101*cfb92d14SAndroid Build Coastguard Worker     {
102*cfb92d14SAndroid Build Coastguard Worker         size_t size = sizeof(Node) + static_cast<size_t>(rand()) % aSizeLimit;
103*cfb92d14SAndroid Build Coastguard Worker         printf("TestAllocateRandomly allocating %zu bytes...\n", size);
104*cfb92d14SAndroid Build Coastguard Worker         last->mNext = static_cast<Node *>(heap.CAlloc(1, size));
105*cfb92d14SAndroid Build Coastguard Worker 
106*cfb92d14SAndroid Build Coastguard Worker         // No more memory for allocation.
107*cfb92d14SAndroid Build Coastguard Worker         if (last->mNext == nullptr)
108*cfb92d14SAndroid Build Coastguard Worker         {
109*cfb92d14SAndroid Build Coastguard Worker             break;
110*cfb92d14SAndroid Build Coastguard Worker         }
111*cfb92d14SAndroid Build Coastguard Worker 
112*cfb92d14SAndroid Build Coastguard Worker         VerifyOrQuit(last->mNext->mNext == nullptr, "TestAllocateRandomly memory not initialized to zero!");
113*cfb92d14SAndroid Build Coastguard Worker         last        = last->mNext;
114*cfb92d14SAndroid Build Coastguard Worker         last->mSize = size;
115*cfb92d14SAndroid Build Coastguard Worker         ++nnodes;
116*cfb92d14SAndroid Build Coastguard Worker 
117*cfb92d14SAndroid Build Coastguard Worker         // 50% probability to randomly free a node.
118*cfb92d14SAndroid Build Coastguard Worker         size_t freeIndex = static_cast<size_t>(rand()) % (nnodes * 2);
119*cfb92d14SAndroid Build Coastguard Worker 
120*cfb92d14SAndroid Build Coastguard Worker         if (freeIndex > nnodes)
121*cfb92d14SAndroid Build Coastguard Worker         {
122*cfb92d14SAndroid Build Coastguard Worker             freeIndex /= 2;
123*cfb92d14SAndroid Build Coastguard Worker 
124*cfb92d14SAndroid Build Coastguard Worker             Node *prev = &head;
125*cfb92d14SAndroid Build Coastguard Worker 
126*cfb92d14SAndroid Build Coastguard Worker             while (freeIndex--)
127*cfb92d14SAndroid Build Coastguard Worker             {
128*cfb92d14SAndroid Build Coastguard Worker                 prev = prev->mNext;
129*cfb92d14SAndroid Build Coastguard Worker             }
130*cfb92d14SAndroid Build Coastguard Worker 
131*cfb92d14SAndroid Build Coastguard Worker             Node *curr = prev->mNext;
132*cfb92d14SAndroid Build Coastguard Worker             printf("TestAllocateRandomly freeing %zu bytes...\n", curr->mSize);
133*cfb92d14SAndroid Build Coastguard Worker             prev->mNext = curr->mNext;
134*cfb92d14SAndroid Build Coastguard Worker             heap.Free(curr);
135*cfb92d14SAndroid Build Coastguard Worker 
136*cfb92d14SAndroid Build Coastguard Worker             if (last == curr)
137*cfb92d14SAndroid Build Coastguard Worker             {
138*cfb92d14SAndroid Build Coastguard Worker                 last = prev;
139*cfb92d14SAndroid Build Coastguard Worker             }
140*cfb92d14SAndroid Build Coastguard Worker 
141*cfb92d14SAndroid Build Coastguard Worker             --nnodes;
142*cfb92d14SAndroid Build Coastguard Worker         }
143*cfb92d14SAndroid Build Coastguard Worker     } while (true);
144*cfb92d14SAndroid Build Coastguard Worker 
145*cfb92d14SAndroid Build Coastguard Worker     last = head.mNext;
146*cfb92d14SAndroid Build Coastguard Worker 
147*cfb92d14SAndroid Build Coastguard Worker     while (last)
148*cfb92d14SAndroid Build Coastguard Worker     {
149*cfb92d14SAndroid Build Coastguard Worker         Node *next = last->mNext;
150*cfb92d14SAndroid Build Coastguard Worker         printf("TestAllocateRandomly freeing %zu bytes...\n", last->mSize);
151*cfb92d14SAndroid Build Coastguard Worker         heap.Free(last);
152*cfb92d14SAndroid Build Coastguard Worker         last = next;
153*cfb92d14SAndroid Build Coastguard Worker     }
154*cfb92d14SAndroid Build Coastguard Worker 
155*cfb92d14SAndroid Build Coastguard Worker     VerifyOrQuit(heap.IsClean() && heap.GetFreeSize() == totalSize,
156*cfb92d14SAndroid Build Coastguard Worker                  "TestAllocateRandomly heap not clean after freeing all!");
157*cfb92d14SAndroid Build Coastguard Worker }
158*cfb92d14SAndroid Build Coastguard Worker 
159*cfb92d14SAndroid Build Coastguard Worker /**
160*cfb92d14SAndroid Build Coastguard Worker  * Verifies allocating and free multiple variables.
161*cfb92d14SAndroid Build Coastguard Worker  */
TestAllocateMultiple(void)162*cfb92d14SAndroid Build Coastguard Worker void TestAllocateMultiple(void)
163*cfb92d14SAndroid Build Coastguard Worker {
164*cfb92d14SAndroid Build Coastguard Worker     for (unsigned int seed = 0; seed < 10; ++seed)
165*cfb92d14SAndroid Build Coastguard Worker     {
166*cfb92d14SAndroid Build Coastguard Worker         size_t sizeLimit = (1 << seed);
167*cfb92d14SAndroid Build Coastguard Worker         printf("TestAllocateRandomly(%zu, %u)...\n", sizeLimit, seed);
168*cfb92d14SAndroid Build Coastguard Worker         TestAllocateRandomly(sizeLimit, seed);
169*cfb92d14SAndroid Build Coastguard Worker     }
170*cfb92d14SAndroid Build Coastguard Worker }
171*cfb92d14SAndroid Build Coastguard Worker 
RunTimerTests(void)172*cfb92d14SAndroid Build Coastguard Worker void RunTimerTests(void)
173*cfb92d14SAndroid Build Coastguard Worker {
174*cfb92d14SAndroid Build Coastguard Worker     TestAllocateSingle();
175*cfb92d14SAndroid Build Coastguard Worker     TestAllocateMultiple();
176*cfb92d14SAndroid Build Coastguard Worker }
177*cfb92d14SAndroid Build Coastguard Worker 
178*cfb92d14SAndroid Build Coastguard Worker #endif // !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
179*cfb92d14SAndroid Build Coastguard Worker 
180*cfb92d14SAndroid Build Coastguard Worker } // namespace ot
181*cfb92d14SAndroid Build Coastguard Worker 
main(void)182*cfb92d14SAndroid Build Coastguard Worker int main(void)
183*cfb92d14SAndroid Build Coastguard Worker {
184*cfb92d14SAndroid Build Coastguard Worker #if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
185*cfb92d14SAndroid Build Coastguard Worker     ot::RunTimerTests();
186*cfb92d14SAndroid Build Coastguard Worker     printf("All tests passed\n");
187*cfb92d14SAndroid Build Coastguard Worker #endif // !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
188*cfb92d14SAndroid Build Coastguard Worker     return 0;
189*cfb92d14SAndroid Build Coastguard Worker }
190