]> begriffs open source - cmsis-freertos/blob - Test/CMock/queue/tracing/queue_registry_utest.c
Updated pack to FreeRTOS 10.4.6
[cmsis-freertos] / Test / CMock / queue / tracing / queue_registry_utest.c
1 /*
2  * FreeRTOS V202111.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * https://www.FreeRTOS.org
23  * https://github.com/FreeRTOS
24  *
25  */
26 /*! @file queue_registry_utest.c */
27
28 /* C runtime includes. */
29 #include <stdlib.h>
30 #include <stdbool.h>
31 #include <string.h>
32 #include <assert.h>
33
34 #include "../queue_utest_common.h"
35
36 /* Queue includes */
37 #include "FreeRTOS.h"
38 #include "FreeRTOSConfig.h"
39 #include "queue.h"
40
41 /* ============================  GLOBAL VARIABLES =========================== */
42
43 /**
44  * @brief Copy of QueueRegistryItem_t from queue.c to allow clearing items between test cases.
45  */
46 typedef struct QUEUE_REGISTRY_ITEM
47 {
48     const char * pcQueueName;
49     QueueHandle_t xHandle;
50 } xQueueRegistryItem;
51 typedef xQueueRegistryItem QueueRegistryItem_t;
52
53 /* Access the xQueueRegistry to clear between test cases */
54 extern PRIVILEGED_DATA QueueRegistryItem_t xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
55
56 /* ==========================  CALLBACK FUNCTIONS =========================== */
57
58 /* ============================= Unity Fixtures ============================= */
59
60 void setUp( void )
61 {
62     commonSetUp();
63     /* Clear the queue registry between test cases */
64     memset( &xQueueRegistry, 0, ( configQUEUE_REGISTRY_SIZE * sizeof( QueueRegistryItem_t ) ) );
65 }
66
67 void tearDown( void )
68 {
69     commonTearDown();
70 }
71
72 void suiteSetUp()
73 {
74     commonSuiteSetUp();
75 }
76
77 int suiteTearDown( int numFailures )
78 {
79     return commonSuiteTearDown( numFailures );
80 }
81
82 /* ===========================  Helper functions ============================ */
83
84 static bool helper_find_in_queue_registry( QueueHandle_t xQueue,
85                                            const char * pcQueueName )
86 {
87     for( int i = 0; i < configQUEUE_REGISTRY_SIZE; i++ )
88     {
89         if( ( xQueueRegistry[ i ].pcQueueName == pcQueueName ) &&
90             ( xQueueRegistry[ i ].xHandle == xQueue ) )
91         {
92             return true;
93         }
94     }
95
96     return false;
97 }
98
99 static bool helper_find_handle_in_queue_registry( QueueHandle_t xQueue )
100 {
101     for( int i = 0; i < configQUEUE_REGISTRY_SIZE; i++ )
102     {
103         if( xQueueRegistry[ i ].xHandle == xQueue )
104         {
105             return true;
106         }
107     }
108
109     return false;
110 }
111
112 /* ==============================  Test Cases =============================== */
113
114 /**
115  * @brief Test vQueueAddToRegistry with a NULL QueueHandle_t
116  * @details Verify that a NULL QueueHandle_t results in the string being stored
117  *  to the QueueRegistry and a configASSERT failure.
118  * @coverage vQueueAddToRegistry
119  **/
120 void test_vQueueAddToRegistry_null_xQueue( void )
121 {
122     const char * pcFakeStringPtr = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();
123
124     /* Expect a failed configASSERT when adding a NULL xQueue to the QueueRegistry */
125     fakeAssertExpectFail();
126
127     vQueueAddToRegistry( NULL, pcFakeStringPtr );
128     TEST_ASSERT_TRUE( fakeAssertGetFlagAndClear() );
129
130     TEST_ASSERT_TRUE( helper_find_in_queue_registry( NULL, pcFakeStringPtr ) );
131 }
132
133 /**
134  * @brief Test vQueueAddToRegistry with a NULL pcQueueName
135  * @details Verify that a NULL pcQueueName results in the NULL string being stored
136  * in the QueueRegistry and a configASSERT failure.
137  * @coverage vQueueAddToRegistry
138  **/
139 void test_vQueueAddToRegistry_null_pcQueueName( void )
140 {
141     QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();
142
143     vQueueAddToRegistry( xFakeHandle, NULL );
144
145     TEST_ASSERT_FALSE( helper_find_in_queue_registry( xFakeHandle, NULL ) );
146 }
147
148 /**
149  * @brief Test vQueueAddToRegistry with a valid xQueue and pcQueueName
150  * @details Verify that calling vQueueAddToRegistry with a valid xQueue and
151  * pcQueueName stores the tuple.
152  * @coverage vQueueAddToRegistry
153  **/
154 void test_vQueueAddToRegistry_success( void )
155 {
156     QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();
157     const char * pcFakeString = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();
158
159     /* Add an item to the registry */
160     vQueueAddToRegistry( xFakeHandle, pcFakeString );
161
162     /* Verify that the value was added to the registry */
163     TEST_ASSERT_TRUE( helper_find_in_queue_registry( xFakeHandle, pcFakeString ) );
164 }
165
166 /**
167  * @brief Test vQueueAddToRegistry with the same QueueHandle_t twice
168  * @details Verify that a given QueueHandle_t can be added to the queue registry
169  * multiple times and that the l
170  * @coverage vQueueAddToRegistry
171  **/
172 void test_vQueueAddToRegistry_twice( void )
173 {
174     QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();
175     const char * pcFakeString1 = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();
176     const char * pcFakeString2 = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();
177
178     /* Add an item to the registry */
179     vQueueAddToRegistry( xFakeHandle, pcFakeString1 );
180
181     TEST_ASSERT_TRUE( helper_find_in_queue_registry( xFakeHandle, pcFakeString1 ) );
182
183     vQueueAddToRegistry( xFakeHandle, pcFakeString2 );
184
185     /* Verify that pcFakeString2 is now in the queue registry */
186     TEST_ASSERT_TRUE( helper_find_in_queue_registry( xFakeHandle, pcFakeString2 ) );
187
188     /* Verify that pcFakeString1 is no longer in the queue registry */
189     TEST_ASSERT_FALSE( helper_find_in_queue_registry( xFakeHandle, pcFakeString1 ) );
190
191     vQueueUnregisterQueue( xFakeHandle );
192
193     /* Verify that pcFakeString2 has been removed from the registry */
194     TEST_ASSERT_FALSE( helper_find_in_queue_registry( xFakeHandle, pcFakeString2 ) );
195 }
196
197 /**
198  * @brief Test vQueueAddToRegistry with a queue registry that is already full.
199  * @details Verify that a call to vQueueAddToRegistry with a full queue registry
200  * fails silently.
201  * @coverage vQueueAddToRegistry
202  **/
203 void test_vQueueAddToRegistry_full( void )
204 {
205     TEST_ASSERT_TRUE( configQUEUE_REGISTRY_SIZE < UINT32_MAX );
206
207     /* Fill the queue registry and verify that the max items were successfully stored.
208      * Start at i=1 since a NULL / 0 pcQueueName denotes an empty queue registry location */
209     for( BaseType_t i = 1; i <= configQUEUE_REGISTRY_SIZE; i++ )
210     {
211         QueueHandle_t fakeHandle = ( QueueHandle_t ) i;
212         const char * fakeString = ( char * ) i;
213
214         /* Add our fake QueueHandle_t and const char* to the registry */
215         vQueueAddToRegistry( fakeHandle, fakeString );
216
217         /* Verify that the fake queue handle was added to the registry */
218         TEST_ASSERT_EQUAL( pcQueueGetName( fakeHandle ), fakeString );
219     }
220
221     /* Prepare one more fake item to add to the registry */
222     QueueHandle_t fakeHandle = ( QueueHandle_t ) ( configQUEUE_REGISTRY_SIZE + 1 );
223     const char * fakeString = ( char * ) ( configQUEUE_REGISTRY_SIZE + 1 );
224
225     /* Add one more item */
226     vQueueAddToRegistry( fakeHandle, fakeString );
227
228     TEST_ASSERT_FALSE( helper_find_in_queue_registry( fakeHandle, fakeString ) );
229 }
230
231 /**
232  * @brief Test pcQueueGetName with a NULL QueueHandle_t
233  * @details Verify that a NULL QueueHandle_t can be used as a lookup value with
234  * pcQueueGetName, but causes a failed configASSERT.
235  * @coverage pcQueueGetName
236  **/
237 void test_pcQueueGetName_null_xQueue( void )
238 {
239     const char * pcFakeString = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();
240
241     /* Expect a failed configASSERT when adding a NULL xQueue to the QueueRegistry */
242     fakeAssertExpectFail();
243
244     vQueueAddToRegistry( NULL, pcFakeString );
245     fakeAssertGetFlagAndClear();
246
247     TEST_ASSERT_TRUE( helper_find_in_queue_registry( NULL, pcFakeString ) );
248
249     /* Expect a failed configASSERT when pcQueueGetName with a NULL xQueue */
250     fakeAssertExpectFail();
251
252     /* Validate the value returned by pcQueueGetName */
253     TEST_ASSERT_EQUAL( pcQueueGetName( NULL ), pcFakeString );
254     TEST_ASSERT_TRUE( fakeAssertGetFlagAndClear() );
255 }
256
257 /**
258  * @brief Test pcQueueGetName with an xQueue handle that was not registered.
259  * @details Verify that a call to pcQueueGetName with an unregistered xQueue
260  * returns a NULL pointer.
261  * @coverage pcQueueGetName
262  **/
263 void test_pcQueueGetName_not_registered( void )
264 {
265     QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();
266     const char * pcFakeString = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();
267
268     /* Add an item to the registry */
269     vQueueAddToRegistry( xFakeHandle, pcFakeString );
270
271     /* Verify the value returned by pcQueueGetName matches the value added to the registry */
272     TEST_ASSERT_EQUAL( pcQueueGetName( xFakeHandle ), pcFakeString );
273
274     vQueueUnregisterQueue( xFakeHandle );
275
276     /* Verify the value returned by pcQueueGetName matches the value added to the registry */
277     TEST_ASSERT_EQUAL( NULL, pcQueueGetName( xFakeHandle ) );
278 }
279
280 /**
281  * @brief Test pcQueueGetName with an xQueue handle that was previously registered.
282  * @details Verify that a call to pcQueueGetName with a registered xQueue handle
283  * returns the correct pointer
284  * @coverage pcQueueGetName
285  **/
286 void test_pcQueueGetName_registered( void )
287 {
288     QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();
289     const char * pcFakeString = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();
290
291     /* Add an item to the registry */
292     vQueueAddToRegistry( xFakeHandle, pcFakeString );
293
294     /* Verify the value returned by pcQueueGetName matches the value added to the registry */
295     TEST_ASSERT_EQUAL( pcQueueGetName( xFakeHandle ), pcFakeString );
296 }
297
298 /**
299  * @brief Test vQueueUnregisterQueue with a NULL xQueue handle
300  * @details Verify that calling vQueueUnregisterQueue with a NULL xQueue results
301  * in a configASSERT failure.
302  * @coverage vQueueUnregisterQueue
303  **/
304 void test_vQueueUnregisterQueue_null_handle( void )
305 {
306     fakeAssertExpectFail();
307
308     vQueueUnregisterQueue( NULL );
309
310     TEST_ASSERT_TRUE( fakeAssertGetFlagAndClear() );
311 }
312
313 /**
314  * @brief Test vQueueUnregisterQueue with an unregistered xQueue handle
315  * @details Verify that calling vQueueUnregisterQueue does not result in an assertion.
316  * @coverage vQueueUnregisterQueue
317  **/
318 void test_vQueueUnregisterQueue_queue_not_registered( void )
319 {
320     QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();
321
322     vQueueUnregisterQueue( xFakeHandle );
323 }
324
325 /**
326  * @brief Test vQueueUnregisterQueue on a registered xQueue
327  * @details Verify that calling vQueueUnregisterQueue with a registered xQueue
328  * removes the xQueue from the Queue Registry and does not result in a
329  * configASSERT failure.
330  * @coverage vQueueUnregisterQueue
331  **/
332 void test_vQueueUnregisterQueue( void )
333 {
334     QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();
335     const char * pcFakeString = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();
336
337     /* Add an item to the registry */
338     vQueueAddToRegistry( xFakeHandle, pcFakeString );
339
340     TEST_ASSERT_TRUE( helper_find_handle_in_queue_registry( xFakeHandle ) );
341     TEST_ASSERT_EQUAL( pcFakeString, pcQueueGetName( xFakeHandle ) );
342
343     vQueueUnregisterQueue( xFakeHandle );
344
345     TEST_ASSERT_FALSE( helper_find_handle_in_queue_registry( xFakeHandle ) );
346 }
347
348 /**
349  * @brief Test two subsequent calls to vQueueUnregisterQueue on a registered xQueue
350  * @details Verify that calling vQueueUnregisterQueue twice on a registered xQueue
351  * succeeds the first time and results in no change on the second call.
352  * @coverage vQueueUnregisterQueue
353  **/
354 void test_vQueueUnregisterQueue_twice( void )
355 {
356     QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();
357     const char * pcFakeString = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();
358
359     /* Add an item to the registry */
360     vQueueAddToRegistry( xFakeHandle, pcFakeString );
361
362     /* Verify that the value was added to the registry */
363     TEST_ASSERT_TRUE( helper_find_handle_in_queue_registry( xFakeHandle ) );
364
365     vQueueUnregisterQueue( xFakeHandle );
366
367     TEST_ASSERT_FALSE( helper_find_handle_in_queue_registry( xFakeHandle ) );
368
369     vQueueUnregisterQueue( xFakeHandle );
370
371     TEST_ASSERT_FALSE( helper_find_handle_in_queue_registry( xFakeHandle ) );
372 }
373
374 /**
375  * @brief Test that vQueueDelete removes the xQueue from the Queue Registry
376  * @details Verify that vQueueDelete removes a queue from the Queue Registry
377  *  by calling vQueueUnregisterQueue.
378  * @coverage vQueueDelete vQueueUnregisterQueue
379  **/
380 void test_vQueueDelete_vQueueUnregisterQueue( void )
381 {
382     QueueHandle_t xQueue = xQueueCreate( 1, sizeof( uint32_t ) );
383     const char * xQueueName = "Testing 123";
384
385     /* Add the queue to the registry */
386     vQueueAddToRegistry( xQueue, xQueueName );
387
388     /* Verify the value returned by pcQueueGetName matches the value added to the registry */
389     TEST_ASSERT_EQUAL( xQueueName, pcQueueGetName( xQueue ) );
390
391     vQueueDelete( xQueue );
392
393     /* Verify the value returned by pcQueueGetName is now NULL */
394     TEST_ASSERT_EQUAL( NULL, pcQueueGetName( xQueue ) );
395 }