Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for and debug mqtt_agent_task.c #85

Merged
merged 7 commits into from
Sep 10, 2024

Conversation

RC-Repositories
Copy link
Contributor

@RC-Repositories RC-Repositories commented Sep 9, 2024

Description

This MR also adds a substantial number (>40) of FFF fakes to be used in future unit testing.

Test Steps

Run the following from the project's root directory:
rm -rf build_unit_test && cmake -S . -B build_unit_test -GNinja && cmake --build build_unit_test && ctest --test-dir build_unit_test --output-on-failure

Checklist:

  • I have tested my changes. No regression in existing tests.
  • I have modified and/or added unit-tests to cover the code changes in this Pull Request.

Related Issue

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Some files were used in mqtt_agent_task.c but were not directly
included.
Additionally makes includes explicit in freertos_command_pool.c and
freertos_agent_message.c.

Signed-off-by: Reuben Cartwright <[email protected]>
This commit expands mock coverage across the repo in order to test
core_mqtt_agent.c and freertos-command-pool.c.
Mock support is in the form of modifying CMakeLists.txt in
connectivity, aws_iot, and security components.
All .h and .c files needed by core-mqtt are also added.

This commit renames the mock directories.
Now mocks for libraries are contained in `library_mocks`
subdirectories, and mocks for integration code in `integration_mocks`
subdirectories.
Only these folder names have changed, they are still in the same parent
directories.
This decision was made to improve clarity of what each folder contains.

This commit also fixes missing `BUILD_TESTING` usages in
`CMakeLists.txt`.

This commit moves mocks for `applications/helpers` from the test
subdirectories into `applications/helpers//mocks`.

Updates mocks: freertos_kernel.

Adds (from scratch) mock support for:
security/mbedtls,
security/trusted_firmware-m,
connectivity/iot_vsocket,
connectivity/freertos_plus_tcp,
connectivity/backoff_algorithms,
coremqtt_agent aws_iot module.

Additionally, mocks application-specific config files for testing
core_mqtt_agent.
Store application-specific config files in `tests/mocks` directory to
avoid cluttering the `tests/` directory with non-test files.
Also adds a CMakeLists.txt file for this directory.

Also adds build_unit_test directory to .gitignore.

Signed-off-by: Reuben Cartwright <[email protected]>
This commit makes the static functions in mqtt_agent_task.c visible
if the UNIT_TESTING macro is defined.
This is done by:
1.  Define the `STATIC` macro in the header.
```
\#ifdef UNIT_TESTING
    #define STATIC /* as nothing */
\#else
    #define STATIC static
\#endif /* UNIT_TESTING */
```
2. Add prototypes to the header.
```
STATIC int foo(void);
```
3. Remove all other function prototypes in the .c file.

This is justified because:
- These functions need to be tested.
- Functions other than `vStartMqttAgentTask` cannot be refactored to be
non-static, as only `vStartMqttAgentTask` should be called in another
file.

Signed-off-by: Reuben Cartwright <[email protected]>
Adds 44 unit tests.

The CMakeLists.txt includes the mocks of header files of files
belonging to this component so they can be mocked.
This needs to be done before including ../inc.

Signed-off-by: Reuben Cartwright <[email protected]>
fix: prvGetRandomNumber does not use tick count if library function
succeeds.
fix: prvSocketDisconnect checks return value of Transport_Disconnect.
fix: prvSocketDisconnect only sets mqtt as closed in system flags if
closed successfully.
fix: prvDisconnectFromMqttBroker errors on failure to disconnect socket.
fix: when printing warning in prvIncomingPublishCallback, use buffer
instead of writing to potentially read-only string.
fix: transport_interface_api.h is only included in mqtt_agent_task.h if
unit testing. The file is not findable from mqtt_agent_task.h
outside of unit testing mode, but is needed to declare some of the
functions tested.
fix-style: include ordering in mqtt_agent_task.h.

Signed-off-by: Reuben Cartwright <[email protected]>
@RC-Repositories RC-Repositories requested a review from a team as a code owner September 9, 2024 15:29
Provides additional details on how the directory structure is laid out
for unit testing. Namely, the three types of `mocks` subdirectories
and what each contains.
Also fixes broken links due to mock subdirectory renaming.

Signed-off-by: Reuben Cartwright <[email protected]>
The only public function declared in `mqtt_agent_task.c` is
`vStartMqttAgentTask` as other functions were extracted to separate
files in previous work.
The prototypes for these other functions were left behind in
`mqtt_agent_task.h`. This commit removes them.

Also change `ota_agent_task.c` to include `events.h`, which is where
these function prototypes were placed in previous work.

Signed-off-by: Reuben Cartwright <[email protected]>
@RC-Repositories RC-Repositories force-pushed the add-mqtt-agent-unit-tests branch from 99313b7 to e3e44d2 Compare September 10, 2024 08:22
@urutva urutva merged commit 4d6635f into FreeRTOS:main Sep 10, 2024
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants