3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
26 /*! @file queue_registry_utest.c */
28 /* C runtime includes. */
34 #include "../queue_utest_common.h"
38 #include "FreeRTOSConfig.h"
41 /* ============================ GLOBAL VARIABLES =========================== */
44 * @brief Copy of QueueRegistryItem_t from queue.c to allow clearing items between test cases.
46 typedef struct QUEUE_REGISTRY_ITEM
48 const char * pcQueueName;
49 QueueHandle_t xHandle;
51 typedef xQueueRegistryItem QueueRegistryItem_t;
53 /* Access the xQueueRegistry to clear between test cases */
54 extern PRIVILEGED_DATA QueueRegistryItem_t xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
56 /* ========================== CALLBACK FUNCTIONS =========================== */
58 /* ============================= Unity Fixtures ============================= */
63 /* Clear the queue registry between test cases */
64 memset( &xQueueRegistry, 0, ( configQUEUE_REGISTRY_SIZE * sizeof( QueueRegistryItem_t ) ) );
77 int suiteTearDown( int numFailures )
79 return commonSuiteTearDown( numFailures );
82 /* =========================== Helper functions ============================ */
84 static bool helper_find_in_queue_registry( QueueHandle_t xQueue,
85 const char * pcQueueName )
87 for( int i = 0; i < configQUEUE_REGISTRY_SIZE; i++ )
89 if( ( xQueueRegistry[ i ].pcQueueName == pcQueueName ) &&
90 ( xQueueRegistry[ i ].xHandle == xQueue ) )
99 static bool helper_find_handle_in_queue_registry( QueueHandle_t xQueue )
101 for( int i = 0; i < configQUEUE_REGISTRY_SIZE; i++ )
103 if( xQueueRegistry[ i ].xHandle == xQueue )
112 /* ============================== Test Cases =============================== */
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
120 void test_vQueueAddToRegistry_null_xQueue( void )
122 const char * pcFakeStringPtr = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();
124 /* Expect a failed configASSERT when adding a NULL xQueue to the QueueRegistry */
125 fakeAssertExpectFail();
127 vQueueAddToRegistry( NULL, pcFakeStringPtr );
128 TEST_ASSERT_TRUE( fakeAssertGetFlagAndClear() );
130 TEST_ASSERT_TRUE( helper_find_in_queue_registry( NULL, pcFakeStringPtr ) );
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
139 void test_vQueueAddToRegistry_null_pcQueueName( void )
141 QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();
143 vQueueAddToRegistry( xFakeHandle, NULL );
145 TEST_ASSERT_FALSE( helper_find_in_queue_registry( xFakeHandle, NULL ) );
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
154 void test_vQueueAddToRegistry_success( void )
156 QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();
157 const char * pcFakeString = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();
159 /* Add an item to the registry */
160 vQueueAddToRegistry( xFakeHandle, pcFakeString );
162 /* Verify that the value was added to the registry */
163 TEST_ASSERT_TRUE( helper_find_in_queue_registry( xFakeHandle, pcFakeString ) );
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
172 void test_vQueueAddToRegistry_twice( void )
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();
178 /* Add an item to the registry */
179 vQueueAddToRegistry( xFakeHandle, pcFakeString1 );
181 TEST_ASSERT_TRUE( helper_find_in_queue_registry( xFakeHandle, pcFakeString1 ) );
183 vQueueAddToRegistry( xFakeHandle, pcFakeString2 );
185 /* Verify that pcFakeString2 is now in the queue registry */
186 TEST_ASSERT_TRUE( helper_find_in_queue_registry( xFakeHandle, pcFakeString2 ) );
188 /* Verify that pcFakeString1 is no longer in the queue registry */
189 TEST_ASSERT_FALSE( helper_find_in_queue_registry( xFakeHandle, pcFakeString1 ) );
191 vQueueUnregisterQueue( xFakeHandle );
193 /* Verify that pcFakeString2 has been removed from the registry */
194 TEST_ASSERT_FALSE( helper_find_in_queue_registry( xFakeHandle, pcFakeString2 ) );
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
201 * @coverage vQueueAddToRegistry
203 void test_vQueueAddToRegistry_full( void )
205 TEST_ASSERT_TRUE( configQUEUE_REGISTRY_SIZE < UINT32_MAX );
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++ )
211 QueueHandle_t fakeHandle = ( QueueHandle_t ) i;
212 const char * fakeString = ( char * ) i;
214 /* Add our fake QueueHandle_t and const char* to the registry */
215 vQueueAddToRegistry( fakeHandle, fakeString );
217 /* Verify that the fake queue handle was added to the registry */
218 TEST_ASSERT_EQUAL( pcQueueGetName( fakeHandle ), fakeString );
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 );
225 /* Add one more item */
226 vQueueAddToRegistry( fakeHandle, fakeString );
228 TEST_ASSERT_FALSE( helper_find_in_queue_registry( fakeHandle, fakeString ) );
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
237 void test_pcQueueGetName_null_xQueue( void )
239 const char * pcFakeString = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();
241 /* Expect a failed configASSERT when adding a NULL xQueue to the QueueRegistry */
242 fakeAssertExpectFail();
244 vQueueAddToRegistry( NULL, pcFakeString );
245 fakeAssertGetFlagAndClear();
247 TEST_ASSERT_TRUE( helper_find_in_queue_registry( NULL, pcFakeString ) );
249 /* Expect a failed configASSERT when pcQueueGetName with a NULL xQueue */
250 fakeAssertExpectFail();
252 /* Validate the value returned by pcQueueGetName */
253 TEST_ASSERT_EQUAL( pcQueueGetName( NULL ), pcFakeString );
254 TEST_ASSERT_TRUE( fakeAssertGetFlagAndClear() );
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
263 void test_pcQueueGetName_not_registered( void )
265 QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();
266 const char * pcFakeString = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();
268 /* Add an item to the registry */
269 vQueueAddToRegistry( xFakeHandle, pcFakeString );
271 /* Verify the value returned by pcQueueGetName matches the value added to the registry */
272 TEST_ASSERT_EQUAL( pcQueueGetName( xFakeHandle ), pcFakeString );
274 vQueueUnregisterQueue( xFakeHandle );
276 /* Verify the value returned by pcQueueGetName matches the value added to the registry */
277 TEST_ASSERT_EQUAL( NULL, pcQueueGetName( xFakeHandle ) );
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
286 void test_pcQueueGetName_registered( void )
288 QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();
289 const char * pcFakeString = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();
291 /* Add an item to the registry */
292 vQueueAddToRegistry( xFakeHandle, pcFakeString );
294 /* Verify the value returned by pcQueueGetName matches the value added to the registry */
295 TEST_ASSERT_EQUAL( pcQueueGetName( xFakeHandle ), pcFakeString );
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
304 void test_vQueueUnregisterQueue_null_handle( void )
306 fakeAssertExpectFail();
308 vQueueUnregisterQueue( NULL );
310 TEST_ASSERT_TRUE( fakeAssertGetFlagAndClear() );
314 * @brief Test vQueueUnregisterQueue with an unregistered xQueue handle
315 * @details Verify that calling vQueueUnregisterQueue does not result in an assertion.
316 * @coverage vQueueUnregisterQueue
318 void test_vQueueUnregisterQueue_queue_not_registered( void )
320 QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();
322 vQueueUnregisterQueue( xFakeHandle );
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
332 void test_vQueueUnregisterQueue( void )
334 QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();
335 const char * pcFakeString = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();
337 /* Add an item to the registry */
338 vQueueAddToRegistry( xFakeHandle, pcFakeString );
340 TEST_ASSERT_TRUE( helper_find_handle_in_queue_registry( xFakeHandle ) );
341 TEST_ASSERT_EQUAL( pcFakeString, pcQueueGetName( xFakeHandle ) );
343 vQueueUnregisterQueue( xFakeHandle );
345 TEST_ASSERT_FALSE( helper_find_handle_in_queue_registry( xFakeHandle ) );
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
354 void test_vQueueUnregisterQueue_twice( void )
356 QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();
357 const char * pcFakeString = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();
359 /* Add an item to the registry */
360 vQueueAddToRegistry( xFakeHandle, pcFakeString );
362 /* Verify that the value was added to the registry */
363 TEST_ASSERT_TRUE( helper_find_handle_in_queue_registry( xFakeHandle ) );
365 vQueueUnregisterQueue( xFakeHandle );
367 TEST_ASSERT_FALSE( helper_find_handle_in_queue_registry( xFakeHandle ) );
369 vQueueUnregisterQueue( xFakeHandle );
371 TEST_ASSERT_FALSE( helper_find_handle_in_queue_registry( xFakeHandle ) );
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
380 void test_vQueueDelete_vQueueUnregisterQueue( void )
382 QueueHandle_t xQueue = xQueueCreate( 1, sizeof( uint32_t ) );
383 const char * xQueueName = "Testing 123";
385 /* Add the queue to the registry */
386 vQueueAddToRegistry( xQueue, xQueueName );
388 /* Verify the value returned by pcQueueGetName matches the value added to the registry */
389 TEST_ASSERT_EQUAL( xQueueName, pcQueueGetName( xQueue ) );
391 vQueueDelete( xQueue );
393 /* Verify the value returned by pcQueueGetName is now NULL */
394 TEST_ASSERT_EQUAL( NULL, pcQueueGetName( xQueue ) );