blob: f5430d233b533065b981ae764aaf1a2d81b38e11 [file] [log] [blame]
Gabor Toth914a10f2024-11-27 14:21:27 +01001/*
2 * Copyright (c) 2024, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#ifndef UNIT_TEST_UTILS_H
9#define UNIT_TEST_UTILS_H
10
11/*
12 * Add an expectOneCall() expectation with the defined "chained arguments" and return
13 * from the function if variable matches "stage". The macro allows defining all
14 * expectations for a test subject function in a single mocking function. The stage
15 * enables conditionally disabling expectations to match the point where the test subject
16 * will return with an error. This avoids setting unmeet expectations and removes test
17 * code duplication.
18 *
19 * Example usage (so not use apostrophes in the last argument!):
20 * MOCK_TILL_STAGE(variable, 1, "psa_cipher_encrypt_setup", .ignoreOtherParameters());
21 */
22#define MOCK_TILL_STAGE(variable, stage, function, chained_args) \
23 do { \
24 if ((variable) >= (stage)) \
25 mock().expectOneCall(function)chained_args; \
26 } while (0)
27
28/*
29 * Ignores all parameters of the mocked function
30 * Example usage:
31 * MOCK_IGNORE(psa_destroy_key)
32 */
33#define MOCK_IGNORE(func) mock().expectOneCall(#func).ignoreOtherParameters()
34
35/*
36 * Ignores all parameters of the mocked function and expects N calls
37 * Example usage:
38 * MOCK_IGNORE(psa_destroy_key, 5)
39 */
40#define MOCK_IGNORE_NCALL(func, N) mock().expectNCalls(N, #func).ignoreOtherParameters()
41
42/*
43 * Ignores all parameters of the mocked function, but returns the specified value
44 * Example usage:
45 * MOCK_RETVAL_ONLY(psa_destroy_key, PSA_ERROR_GENERIC_ERROR)
46 */
47#define MOCK_RETVAL_ONLY(func, retval) mock().expectOneCall(#func).andReturnValue(retval).ignoreOtherParameters()
48
49/*
50 * Ignores all parameters of the mocked function, except an output parameter
51 * Example usage:
52 * MOCK_OUTPUT_PARAMETER_ONLY(block_store_read, data_len, data_len);
53 */
54#define MOCK_OUTPUT_PARAMETER_ONLY(func, parameter, variable) mock().expectOneCall(#func).withOutputParameterReturning(#parameter, &variable, sizeof(variable)).ignoreOtherParameters()
55
56/*
57 * Ignores all parameters of the mocked function, except an output parameter and returns value
58 * Example usage:
59 * MOCK_OUTPUT_PARAMETER_RETVAL(block_store_write, num_written, num_written, PSA_ERROR_GENERIC_ERROR);
60 */
61#define MOCK_OUTPUT_PARAMETER_RETVAL(func, parameter, variable, retval) mock().expectOneCall(#func).withOutputParameterReturning(#parameter, &variable, sizeof(variable)).andReturnValue(retval).ignoreOtherParameters()
62
63/*
64 * Ignores all parameters of the mocked function, except an unsigned int paramter and returns value
65 * Example usage:
66 * MOCK_UINT_PARAMETER_RETVAL(psa_cipher_encrypt_setup, alg, PSA_ALG_ECB_NO_PADDING, PSA_ERROR_GENERIC_ERROR);
67 */
68#define MOCK_UINT_PARAMETER_RETVAL(func, parameter, value, retval) mock().expectOneCall(#func).withUnsignedIntParameter(#parameter, value).andReturnValue(retval).ignoreOtherParameters()
69
70#endif /* UNIT_TEST_UTILS_H */