summaryrefslogtreecommitdiff
path: root/FreeRTOS/Test/CMock/queue/tracing/queue_registry_utest.c
blob: 0eae6ba0a2302c0c9a18e4a0f8d7a35e1188f63c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * FreeRTOS V202111.00
 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * https://www.FreeRTOS.org
 * https://github.com/FreeRTOS
 *
 */
/*! @file queue_registry_utest.c */

/* C runtime includes. */
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>

#include "../queue_utest_common.h"

/* Queue includes */
#include "FreeRTOS.h"
#include "FreeRTOSConfig.h"
#include "queue.h"

/* ============================  GLOBAL VARIABLES =========================== */

/**
 * @brief Copy of QueueRegistryItem_t from queue.c to allow clearing items between test cases.
 */
typedef struct QUEUE_REGISTRY_ITEM
{
    const char * pcQueueName;
    QueueHandle_t xHandle;
} xQueueRegistryItem;
typedef xQueueRegistryItem QueueRegistryItem_t;

/* Access the xQueueRegistry to clear between test cases */
extern PRIVILEGED_DATA QueueRegistryItem_t xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];

/* ==========================  CALLBACK FUNCTIONS =========================== */

/* ============================= Unity Fixtures ============================= */

void setUp( void )
{
    commonSetUp();
    /* Clear the queue registry between test cases */
    memset( &xQueueRegistry, 0, ( configQUEUE_REGISTRY_SIZE * sizeof( QueueRegistryItem_t ) ) );
}

void tearDown( void )
{
    commonTearDown();
}

void suiteSetUp()
{
    commonSuiteSetUp();
}

int suiteTearDown( int numFailures )
{
    return commonSuiteTearDown( numFailures );
}

/* ===========================  Helper functions ============================ */

static bool helper_find_in_queue_registry( QueueHandle_t xQueue,
                                           const char * pcQueueName )
{
    for( int i = 0; i < configQUEUE_REGISTRY_SIZE; i++ )
    {
        if( ( xQueueRegistry[ i ].pcQueueName == pcQueueName ) &&
            ( xQueueRegistry[ i ].xHandle == xQueue ) )
        {
            return true;
        }
    }

    return false;
}

static bool helper_find_handle_in_queue_registry( QueueHandle_t xQueue )
{
    for( int i = 0; i < configQUEUE_REGISTRY_SIZE; i++ )
    {
        if( xQueueRegistry[ i ].xHandle == xQueue )
        {
            return true;
        }
    }

    return false;
}

/* ==============================  Test Cases =============================== */

/**
 * @brief Test vQueueAddToRegistry with a NULL QueueHandle_t
 * @details Verify that a NULL QueueHandle_t results in the string being stored
 *  to the QueueRegistry and a configASSERT failure.
 * @coverage vQueueAddToRegistry
 **/
void test_vQueueAddToRegistry_null_xQueue( void )
{
    const char * pcFakeStringPtr = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();

    /* Expect a failed configASSERT when adding a NULL xQueue to the QueueRegistry */
    fakeAssertExpectFail();

    vQueueAddToRegistry( NULL, pcFakeStringPtr );
    TEST_ASSERT_TRUE( fakeAssertGetFlagAndClear() );

    TEST_ASSERT_TRUE( helper_find_in_queue_registry( NULL, pcFakeStringPtr ) );
}

/**
 * @brief Test vQueueAddToRegistry with a NULL pcQueueName
 * @details Verify that a NULL pcQueueName results in the NULL string being stored
 * in the QueueRegistry and a configASSERT failure.
 * @coverage vQueueAddToRegistry
 **/
void test_vQueueAddToRegistry_null_pcQueueName( void )
{
    QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();

    vQueueAddToRegistry( xFakeHandle, NULL );

    TEST_ASSERT_FALSE( helper_find_in_queue_registry( xFakeHandle, NULL ) );
}

/**
 * @brief Test vQueueAddToRegistry with a valid xQueue and pcQueueName
 * @details Verify that calling vQueueAddToRegistry with a valid xQueue and
 * pcQueueName stores the tuple.
 * @coverage vQueueAddToRegistry
 **/
void test_vQueueAddToRegistry_success( void )
{
    QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();
    const char * pcFakeString = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();

    /* Add an item to the registry */
    vQueueAddToRegistry( xFakeHandle, pcFakeString );

    /* Verify that the value was added to the registry */
    TEST_ASSERT_TRUE( helper_find_in_queue_registry( xFakeHandle, pcFakeString ) );
}

/**
 * @brief Test vQueueAddToRegistry with the same QueueHandle_t twice
 * @details Verify that a given QueueHandle_t can be added to the queue registry
 * multiple times and that the l
 * @coverage vQueueAddToRegistry
 **/
void test_vQueueAddToRegistry_twice( void )
{
    QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();
    const char * pcFakeString1 = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();
    const char * pcFakeString2 = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();

    /* Add an item to the registry */
    vQueueAddToRegistry( xFakeHandle, pcFakeString1 );

    TEST_ASSERT_TRUE( helper_find_in_queue_registry( xFakeHandle, pcFakeString1 ) );

    vQueueAddToRegistry( xFakeHandle, pcFakeString2 );

    /* Verify that pcFakeString2 is now in the queue registry */
    TEST_ASSERT_TRUE( helper_find_in_queue_registry( xFakeHandle, pcFakeString2 ) );

    /* Verify that pcFakeString1 is no longer in the queue registry */
    TEST_ASSERT_FALSE( helper_find_in_queue_registry( xFakeHandle, pcFakeString1 ) );

    vQueueUnregisterQueue( xFakeHandle );

    /* Verify that pcFakeString2 has been removed from the registry */
    TEST_ASSERT_FALSE( helper_find_in_queue_registry( xFakeHandle, pcFakeString2 ) );
}

/**
 * @brief Test vQueueAddToRegistry with a queue registry that is already full.
 * @details Verify that a call to vQueueAddToRegistry with a full queue registry
 * fails silently.
 * @coverage vQueueAddToRegistry
 **/
void test_vQueueAddToRegistry_full( void )
{
    TEST_ASSERT_TRUE( configQUEUE_REGISTRY_SIZE < UINT32_MAX );

    /* Fill the queue registry and verify that the max items were successfully stored.
     * Start at i=1 since a NULL / 0 pcQueueName denotes an empty queue registry location */
    for( BaseType_t i = 1; i <= configQUEUE_REGISTRY_SIZE; i++ )
    {
        QueueHandle_t fakeHandle = ( QueueHandle_t ) i;
        const char * fakeString = ( char * ) i;

        /* Add our fake QueueHandle_t and const char* to the registry */
        vQueueAddToRegistry( fakeHandle, fakeString );

        /* Verify that the fake queue handle was added to the registry */
        TEST_ASSERT_EQUAL( pcQueueGetName( fakeHandle ), fakeString );
    }

    /* Prepare one more fake item to add to the registry */
    QueueHandle_t fakeHandle = ( QueueHandle_t ) ( configQUEUE_REGISTRY_SIZE + 1 );
    const char * fakeString = ( char * ) ( configQUEUE_REGISTRY_SIZE + 1 );

    /* Add one more item */
    vQueueAddToRegistry( fakeHandle, fakeString );

    TEST_ASSERT_FALSE( helper_find_in_queue_registry( fakeHandle, fakeString ) );
}

/**
 * @brief Test pcQueueGetName with a NULL QueueHandle_t
 * @details Verify that a NULL QueueHandle_t can be used as a lookup value with
 * pcQueueGetName, but causes a failed configASSERT.
 * @coverage pcQueueGetName
 **/
void test_pcQueueGetName_null_xQueue( void )
{
    const char * pcFakeString = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();

    /* Expect a failed configASSERT when adding a NULL xQueue to the QueueRegistry */
    fakeAssertExpectFail();

    vQueueAddToRegistry( NULL, pcFakeString );
    fakeAssertGetFlagAndClear();

    TEST_ASSERT_TRUE( helper_find_in_queue_registry( NULL, pcFakeString ) );

    /* Expect a failed configASSERT when pcQueueGetName with a NULL xQueue */
    fakeAssertExpectFail();

    /* Validate the value returned by pcQueueGetName */
    TEST_ASSERT_EQUAL( pcQueueGetName( NULL ), pcFakeString );
    TEST_ASSERT_TRUE( fakeAssertGetFlagAndClear() );
}

/**
 * @brief Test pcQueueGetName with an xQueue handle that was not registered.
 * @details Verify that a call to pcQueueGetName with an unregistered xQueue
 * returns a NULL pointer.
 * @coverage pcQueueGetName
 **/
void test_pcQueueGetName_not_registered( void )
{
    QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();
    const char * pcFakeString = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();

    /* Add an item to the registry */
    vQueueAddToRegistry( xFakeHandle, pcFakeString );

    /* Verify the value returned by pcQueueGetName matches the value added to the registry */
    TEST_ASSERT_EQUAL( pcQueueGetName( xFakeHandle ), pcFakeString );

    vQueueUnregisterQueue( xFakeHandle );

    /* Verify the value returned by pcQueueGetName matches the value added to the registry */
    TEST_ASSERT_EQUAL( NULL, pcQueueGetName( xFakeHandle ) );
}

/**
 * @brief Test pcQueueGetName with an xQueue handle that was previously registered.
 * @details Verify that a call to pcQueueGetName with a registered xQueue handle
 * returns the correct pointer
 * @coverage pcQueueGetName
 **/
void test_pcQueueGetName_registered( void )
{
    QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();
    const char * pcFakeString = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();

    /* Add an item to the registry */
    vQueueAddToRegistry( xFakeHandle, pcFakeString );

    /* Verify the value returned by pcQueueGetName matches the value added to the registry */
    TEST_ASSERT_EQUAL( pcQueueGetName( xFakeHandle ), pcFakeString );
}

/**
 * @brief Test vQueueUnregisterQueue with a NULL xQueue handle
 * @details Verify that calling vQueueUnregisterQueue with a NULL xQueue results
 * in a configASSERT failure.
 * @coverage vQueueUnregisterQueue
 **/
void test_vQueueUnregisterQueue_null_handle( void )
{
    fakeAssertExpectFail();

    vQueueUnregisterQueue( NULL );

    TEST_ASSERT_TRUE( fakeAssertGetFlagAndClear() );
}

/**
 * @brief Test vQueueUnregisterQueue with an unregistered xQueue handle
 * @details Verify that calling vQueueUnregisterQueue does not result in an assertion.
 * @coverage vQueueUnregisterQueue
 **/
void test_vQueueUnregisterQueue_queue_not_registered( void )
{
    QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();

    vQueueUnregisterQueue( xFakeHandle );
}

/**
 * @brief Test vQueueUnregisterQueue on a registered xQueue
 * @details Verify that calling vQueueUnregisterQueue with a registered xQueue
 * removes the xQueue from the Queue Registry and does not result in a
 * configASSERT failure.
 * @coverage vQueueUnregisterQueue
 **/
void test_vQueueUnregisterQueue( void )
{
    QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();
    const char * pcFakeString = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();

    /* Add an item to the registry */
    vQueueAddToRegistry( xFakeHandle, pcFakeString );

    TEST_ASSERT_TRUE( helper_find_handle_in_queue_registry( xFakeHandle ) );
    TEST_ASSERT_EQUAL( pcFakeString, pcQueueGetName( xFakeHandle ) );

    vQueueUnregisterQueue( xFakeHandle );

    TEST_ASSERT_FALSE( helper_find_handle_in_queue_registry( xFakeHandle ) );
}

/**
 * @brief Test two subsequent calls to vQueueUnregisterQueue on a registered xQueue
 * @details Verify that calling vQueueUnregisterQueue twice on a registered xQueue
 * succeeds the first time and results in no change on the second call.
 * @coverage vQueueUnregisterQueue
 **/
void test_vQueueUnregisterQueue_twice( void )
{
    QueueHandle_t xFakeHandle = ( QueueHandle_t ) ( BaseType_t ) getNextMonotonicTestValue();
    const char * pcFakeString = ( char * ) ( BaseType_t ) getNextMonotonicTestValue();

    /* Add an item to the registry */
    vQueueAddToRegistry( xFakeHandle, pcFakeString );

    /* Verify that the value was added to the registry */
    TEST_ASSERT_TRUE( helper_find_handle_in_queue_registry( xFakeHandle ) );

    vQueueUnregisterQueue( xFakeHandle );

    TEST_ASSERT_FALSE( helper_find_handle_in_queue_registry( xFakeHandle ) );

    vQueueUnregisterQueue( xFakeHandle );

    TEST_ASSERT_FALSE( helper_find_handle_in_queue_registry( xFakeHandle ) );
}

/**
 * @brief Test that vQueueDelete removes the xQueue from the Queue Registry
 * @details Verify that vQueueDelete removes a queue from the Queue Registry
 *  by calling vQueueUnregisterQueue.
 * @coverage vQueueDelete vQueueUnregisterQueue
 **/
void test_vQueueDelete_vQueueUnregisterQueue( void )
{
    QueueHandle_t xQueue = xQueueCreate( 1, sizeof( uint32_t ) );
    const char * xQueueName = "Testing 123";

    /* Add the queue to the registry */
    vQueueAddToRegistry( xQueue, xQueueName );

    /* Verify the value returned by pcQueueGetName matches the value added to the registry */
    TEST_ASSERT_EQUAL( xQueueName, pcQueueGetName( xQueue ) );

    vQueueDelete( xQueue );

    /* Verify the value returned by pcQueueGetName is now NULL */
    TEST_ASSERT_EQUAL( NULL, pcQueueGetName( xQueue ) );
}