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 <stdarg.h>
30
31 #include "test_platform.h"
32
33 #include <openthread/config.h>
34
35 #include "common/debug.hpp"
36 #include "common/message.hpp"
37 #include "instance/instance.hpp"
38
39 #include "test_util.h"
40
41 namespace ot {
42
43 #define kNumTestMessages 5
44
45 static Instance *sInstance;
46 static MessagePool *sMessagePool;
47
48 // This function verifies the content of the message queue to match the passed in messages
VerifyMessageQueueContent(MessageQueue & aMessageQueue,int aExpectedLength,...)49 void VerifyMessageQueueContent(MessageQueue &aMessageQueue, int aExpectedLength, ...)
50 {
51 const MessageQueue &constQueue = aMessageQueue;
52 va_list args;
53 Message *message;
54 Message *msgArg;
55
56 va_start(args, aExpectedLength);
57
58 if (aExpectedLength == 0)
59 {
60 message = aMessageQueue.GetHead();
61 VerifyOrQuit(message == nullptr, "not empty when expected len is zero.");
62 }
63 else
64 {
65 for (message = aMessageQueue.GetHead(); message != nullptr; message = message->GetNext())
66 {
67 VerifyOrQuit(aExpectedLength != 0, "contains more entries than expected");
68
69 msgArg = va_arg(args, Message *);
70 VerifyOrQuit(msgArg == message, "content does not match what is expected.");
71
72 aExpectedLength--;
73 }
74
75 VerifyOrQuit(aExpectedLength == 0, "fewer entries than expected");
76 }
77
78 va_end(args);
79
80 // Check range-based `for` loop iteration using non-const iterator
81
82 message = aMessageQueue.GetHead();
83
84 for (Message &msg : aMessageQueue)
85 {
86 VerifyOrQuit(message == &msg, "`for` loop iteration does not match expected");
87 message = message->GetNext();
88 }
89
90 VerifyOrQuit(message == nullptr, "`for` loop iteration resulted in fewer entries than expected");
91
92 // Check range-base `for` iteration using const iterator
93
94 message = aMessageQueue.GetHead();
95
96 for (const Message &constMsg : constQueue)
97 {
98 VerifyOrQuit(message == &constMsg, "`for` loop iteration does not match expected");
99 message = message->GetNext();
100 }
101
102 VerifyOrQuit(message == nullptr, "`for` loop iteration resulted in fewer entries than expected");
103 }
104
TestMessageQueue(void)105 void TestMessageQueue(void)
106 {
107 MessageQueue messageQueue;
108 Message *messages[kNumTestMessages];
109 MessageQueue::Info info;
110
111 sInstance = testInitInstance();
112 VerifyOrQuit(sInstance != nullptr);
113
114 sMessagePool = &sInstance->Get<MessagePool>();
115
116 for (Message *&msg : messages)
117 {
118 msg = sMessagePool->Allocate(Message::kTypeIp6);
119 VerifyOrQuit(msg != nullptr, "Message::Allocate() failed");
120 }
121
122 VerifyMessageQueueContent(messageQueue, 0);
123
124 // Enqueue 1 message and remove it
125 messageQueue.Enqueue(*messages[0]);
126 VerifyMessageQueueContent(messageQueue, 1, messages[0]);
127 messageQueue.Dequeue(*messages[0]);
128 VerifyMessageQueueContent(messageQueue, 0);
129
130 // Enqueue 1 message at head and remove it
131 messageQueue.Enqueue(*messages[0], MessageQueue::kQueuePositionHead);
132 VerifyMessageQueueContent(messageQueue, 1, messages[0]);
133 messageQueue.Dequeue(*messages[0]);
134 VerifyMessageQueueContent(messageQueue, 0);
135
136 // Enqueue 5 messages
137 messageQueue.Enqueue(*messages[0]);
138 VerifyMessageQueueContent(messageQueue, 1, messages[0]);
139 messageQueue.Enqueue(*messages[1]);
140 VerifyMessageQueueContent(messageQueue, 2, messages[0], messages[1]);
141 messageQueue.Enqueue(*messages[2]);
142 VerifyMessageQueueContent(messageQueue, 3, messages[0], messages[1], messages[2]);
143 messageQueue.Enqueue(*messages[3]);
144 VerifyMessageQueueContent(messageQueue, 4, messages[0], messages[1], messages[2], messages[3]);
145 messageQueue.Enqueue(*messages[4]);
146 VerifyMessageQueueContent(messageQueue, 5, messages[0], messages[1], messages[2], messages[3], messages[4]);
147
148 // Check the GetInfo()
149 memset(&info, 0, sizeof(info));
150 messageQueue.GetInfo(info);
151 VerifyOrQuit(info.mNumMessages == 5, "MessageQueue::GetInfo() failed.");
152
153 // Remove from head
154 messageQueue.Dequeue(*messages[0]);
155 VerifyMessageQueueContent(messageQueue, 4, messages[1], messages[2], messages[3], messages[4]);
156
157 // Remove a message in middle
158 messageQueue.Dequeue(*messages[3]);
159 VerifyMessageQueueContent(messageQueue, 3, messages[1], messages[2], messages[4]);
160
161 // Remove from tail
162 messageQueue.Dequeue(*messages[4]);
163 VerifyMessageQueueContent(messageQueue, 2, messages[1], messages[2]);
164
165 // Add after remove
166 messageQueue.Enqueue(*messages[0]);
167 VerifyMessageQueueContent(messageQueue, 3, messages[1], messages[2], messages[0]);
168 messageQueue.Enqueue(*messages[3]);
169 VerifyMessageQueueContent(messageQueue, 4, messages[1], messages[2], messages[0], messages[3]);
170
171 // Remove from middle
172 messageQueue.Dequeue(*messages[2]);
173 VerifyMessageQueueContent(messageQueue, 3, messages[1], messages[0], messages[3]);
174
175 // Add to head
176 messageQueue.Enqueue(*messages[2], MessageQueue::kQueuePositionHead);
177 VerifyMessageQueueContent(messageQueue, 4, messages[2], messages[1], messages[0], messages[3]);
178
179 // Remove from head
180 messageQueue.Dequeue(*messages[2]);
181 VerifyMessageQueueContent(messageQueue, 3, messages[1], messages[0], messages[3]);
182
183 // Remove from head
184 messageQueue.Dequeue(*messages[1]);
185 VerifyMessageQueueContent(messageQueue, 2, messages[0], messages[3]);
186
187 // Add to head
188 messageQueue.Enqueue(*messages[1], MessageQueue::kQueuePositionHead);
189 VerifyMessageQueueContent(messageQueue, 3, messages[1], messages[0], messages[3]);
190
191 // Add to tail
192 messageQueue.Enqueue(*messages[2], MessageQueue::kQueuePositionTail);
193 VerifyMessageQueueContent(messageQueue, 4, messages[1], messages[0], messages[3], messages[2]);
194
195 // Remove all messages.
196 messageQueue.Dequeue(*messages[3]);
197 VerifyMessageQueueContent(messageQueue, 3, messages[1], messages[0], messages[2]);
198 messageQueue.Dequeue(*messages[1]);
199 VerifyMessageQueueContent(messageQueue, 2, messages[0], messages[2]);
200 messageQueue.Dequeue(*messages[2]);
201 VerifyMessageQueueContent(messageQueue, 1, messages[0]);
202 messageQueue.Dequeue(*messages[0]);
203 VerifyMessageQueueContent(messageQueue, 0);
204
205 // Range-based `for` and dequeue during iteration
206
207 for (uint16_t removeIndex = 0; removeIndex < 5; removeIndex++)
208 {
209 uint16_t index = 0;
210
211 messageQueue.Enqueue(*messages[0]);
212 messageQueue.Enqueue(*messages[1]);
213 messageQueue.Enqueue(*messages[2]);
214 messageQueue.Enqueue(*messages[3]);
215 messageQueue.Enqueue(*messages[4]);
216 VerifyMessageQueueContent(messageQueue, 5, messages[0], messages[1], messages[2], messages[3], messages[4]);
217
218 // While iterating over the queue remove the entry at `removeIndex`
219 for (Message &message : messageQueue)
220 {
221 if (index == removeIndex)
222 {
223 messageQueue.Dequeue(message);
224 }
225
226 VerifyOrQuit(&message == messages[index++]);
227 }
228
229 index = 0;
230
231 // Iterate over the queue and remove all
232 for (Message &message : messageQueue)
233 {
234 if (index == removeIndex)
235 {
236 index++;
237 }
238
239 VerifyOrQuit(&message == messages[index++]);
240 messageQueue.Dequeue(message);
241 }
242
243 VerifyMessageQueueContent(messageQueue, 0);
244 }
245
246 testFreeInstance(sInstance);
247 }
248
249 // This function verifies the content of the message queue to match the passed in messages
VerifyMessageQueueContentUsingOtApi(otMessageQueue * aQueue,int aExpectedLength,...)250 void VerifyMessageQueueContentUsingOtApi(otMessageQueue *aQueue, int aExpectedLength, ...)
251 {
252 va_list args;
253 otMessage *message;
254 otMessage *msgArg;
255
256 va_start(args, aExpectedLength);
257
258 if (aExpectedLength == 0)
259 {
260 message = otMessageQueueGetHead(aQueue);
261 VerifyOrQuit(message == nullptr, "not empty when expected len is zero.");
262 }
263 else
264 {
265 for (message = otMessageQueueGetHead(aQueue); message != nullptr;
266 message = otMessageQueueGetNext(aQueue, message))
267 {
268 VerifyOrQuit(aExpectedLength != 0, "more entries than expected");
269
270 msgArg = va_arg(args, otMessage *);
271 VerifyOrQuit(msgArg == message, "does not match what is expected.");
272
273 aExpectedLength--;
274 }
275
276 VerifyOrQuit(aExpectedLength == 0, "less entries than expected");
277 }
278
279 va_end(args);
280 }
281
282 // This test checks all the OpenThread C APIs for `otMessageQueue`
TestMessageQueueOtApis(void)283 void TestMessageQueueOtApis(void)
284 {
285 otMessage *messages[kNumTestMessages];
286 otMessage *message;
287 otMessageQueue queue, queue2;
288
289 sInstance = testInitInstance();
290 VerifyOrQuit(sInstance != nullptr);
291
292 for (otMessage *&msg : messages)
293 {
294 msg = otIp6NewMessage(sInstance, nullptr);
295 VerifyOrQuit(msg != nullptr, "otIp6NewMessage() failed.");
296 }
297
298 otMessageQueueInit(&queue);
299 otMessageQueueInit(&queue2);
300
301 // Check an empty queue.
302 VerifyMessageQueueContentUsingOtApi(&queue, 0);
303
304 // Add message to the queue and check the content
305 otMessageQueueEnqueue(&queue, messages[0]);
306 VerifyMessageQueueContentUsingOtApi(&queue, 1, messages[0]);
307 otMessageQueueEnqueue(&queue, messages[1]);
308 VerifyMessageQueueContentUsingOtApi(&queue, 2, messages[0], messages[1]);
309 otMessageQueueEnqueueAtHead(&queue, messages[2]);
310 VerifyMessageQueueContentUsingOtApi(&queue, 3, messages[2], messages[0], messages[1]);
311 otMessageQueueEnqueue(&queue, messages[3]);
312 VerifyMessageQueueContentUsingOtApi(&queue, 4, messages[2], messages[0], messages[1], messages[3]);
313
314 // Remove elements and check the content
315 otMessageQueueDequeue(&queue, messages[1]);
316 VerifyMessageQueueContentUsingOtApi(&queue, 3, messages[2], messages[0], messages[3]);
317 otMessageQueueDequeue(&queue, messages[0]);
318 VerifyMessageQueueContentUsingOtApi(&queue, 2, messages[2], messages[3]);
319 otMessageQueueDequeue(&queue, messages[3]);
320 VerifyMessageQueueContentUsingOtApi(&queue, 1, messages[2]);
321
322 // Check the failure cases for otMessageQueueGetNext()
323 message = otMessageQueueGetNext(&queue, nullptr);
324 VerifyOrQuit(message == nullptr, "otMessageQueueGetNext(queue, nullptr) did not return nullptr.");
325 message = otMessageQueueGetNext(&queue, messages[1]);
326 VerifyOrQuit(message == nullptr, "otMessageQueueGetNext() did not return nullptr for a message not in the queue.");
327
328 // Check the failure case when attempting to do otMessageQueueGetNext() but passing in a wrong queue pointer.
329 otMessageQueueEnqueue(&queue2, messages[0]);
330 VerifyMessageQueueContentUsingOtApi(&queue2, 1, messages[0]);
331 otMessageQueueEnqueue(&queue2, messages[1]);
332 VerifyMessageQueueContentUsingOtApi(&queue2, 2, messages[0], messages[1]);
333
334 message = otMessageQueueGetNext(&queue2, messages[0]);
335 VerifyOrQuit(message == messages[1], "otMessageQueueGetNext() failed");
336 message = otMessageQueueGetNext(&queue, messages[0]);
337 VerifyOrQuit(message == nullptr, "otMessageQueueGetNext() did not return nullptr for message not in the queue.");
338
339 // Remove all element and make sure queue is empty
340 otMessageQueueDequeue(&queue, messages[2]);
341 VerifyMessageQueueContentUsingOtApi(&queue, 0);
342
343 testFreeInstance(sInstance);
344 }
345
346 } // namespace ot
347
main(void)348 int main(void)
349 {
350 ot::TestMessageQueue();
351 ot::TestMessageQueueOtApis();
352 printf("All tests passed\n");
353 return 0;
354 }
355