From 3c2b1042280662cab2b50a2bc84c951c72c7edab Mon Sep 17 00:00:00 2001 From: Archit Gupta Date: Wed, 31 Jan 2024 15:19:11 -0800 Subject: [PATCH 1/4] Fix unit tests defining NetworkContext in config The NetworkContext struct should be defined in each c file, not in the config h file. The API uses it as an opaque type. CMock needs a workaround for it's c files though, as it does not support opaque types. --- test/unit-test/cmock_opaque_types.h | 36 +++++++++++++++++++++ test/unit-test/core_mqtt_config.h | 10 ++---- test/unit-test/core_mqtt_serializer_utest.c | 7 ++++ test/unit-test/core_mqtt_utest.c | 7 ++++ tools/cmock/project.yml | 2 ++ 5 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 test/unit-test/cmock_opaque_types.h diff --git a/test/unit-test/cmock_opaque_types.h b/test/unit-test/cmock_opaque_types.h new file mode 100644 index 000000000..35b9d2a8b --- /dev/null +++ b/test/unit-test/cmock_opaque_types.h @@ -0,0 +1,36 @@ +/* + * coreMQTT v2.1.0 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * SPDX-License-Identifier: MIT + * + * 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. + */ + +#ifndef CMOCK_OPAQUE_TYPES_H_ +#define CMOCK_OPAQUE_TYPES_H_ + +/* CMock does not support opaque types so needs concrete definitions for them. + * This file is included in CMock .c files. */ + +struct NetworkContext +{ + int a; +}; + +#endif /* ifndef CMOCK_OPAQUE_TYPES_H_ */ diff --git a/test/unit-test/core_mqtt_config.h b/test/unit-test/core_mqtt_config.h index ba1d9a02f..82cf83121 100644 --- a/test/unit-test/core_mqtt_config.h +++ b/test/unit-test/core_mqtt_config.h @@ -69,14 +69,8 @@ */ #define MQTT_MAX_CONNACK_RECEIVE_RETRY_COUNT ( 2U ) -/* Set network context to double pointer to buffer (uint8_t**). */ -struct NetworkContext -{ - uint8_t ** buffer; -}; +#define MQTT_SUB_UNSUB_MAX_VECTORS ( 6U ) -#define MQTT_SUB_UNSUB_MAX_VECTORS ( 6U ) - -#define MQTT_SEND_TIMEOUT_MS ( 20U ) +#define MQTT_SEND_TIMEOUT_MS ( 20U ) #endif /* ifndef CORE_MQTT_CONFIG_H_ */ diff --git a/test/unit-test/core_mqtt_serializer_utest.c b/test/unit-test/core_mqtt_serializer_utest.c index 648917ca1..87f0bd7da 100644 --- a/test/unit-test/core_mqtt_serializer_utest.c +++ b/test/unit-test/core_mqtt_serializer_utest.c @@ -27,12 +27,19 @@ * @brief Unit tests for functions in core_mqtt_serializer.h. */ #include +#include #include "unity.h" /* Include paths for public enums, structures, and macros. */ #include "core_mqtt_serializer.h" +/* Set network context to double pointer to buffer (uint8_t**). */ +struct NetworkContext +{ + uint8_t ** buffer; +}; + #define MQTT_MAX_REMAINING_LENGTH ( 268435455UL ) #define MQTT_PACKET_CONNACK_REMAINING_LENGTH ( ( uint8_t ) 2U ) /**< @brief A CONNACK packet always has a "Remaining length" of 2. */ #define MQTT_PACKET_CONNACK_SESSION_PRESENT_MASK ( ( uint8_t ) 0x01U ) /**< @brief The "Session Present" bit is always the lowest bit. */ diff --git a/test/unit-test/core_mqtt_utest.c b/test/unit-test/core_mqtt_utest.c index 97f87c080..d647fd2d5 100644 --- a/test/unit-test/core_mqtt_utest.c +++ b/test/unit-test/core_mqtt_utest.c @@ -28,6 +28,7 @@ */ #include #include +#include #include "unity.h" @@ -37,6 +38,12 @@ #include "mock_core_mqtt_serializer.h" #include "mock_core_mqtt_state.h" +/* Set network context to double pointer to buffer (uint8_t**). */ +struct NetworkContext +{ + uint8_t ** buffer; +}; + /** * @brief MQTT client identifier. */ diff --git a/tools/cmock/project.yml b/tools/cmock/project.yml index 8a90416b7..560136ebf 100644 --- a/tools/cmock/project.yml +++ b/tools/cmock/project.yml @@ -21,6 +21,8 @@ :includes: # This will add these includes to each mock. - - + :includes_c_post_header: + - :treat_externs: :exclude # Now the extern-ed functions will be mocked. :weak: __attribute__((weak)) :treat_externs: :include From cc37bb9de98a4c4e6cc54d3f5e08b946eefd6076 Mon Sep 17 00:00:00 2001 From: Archit Gupta Date: Thu, 1 Feb 2024 13:56:38 -0800 Subject: [PATCH 2/4] Fix CBMC config including NetworkContext definition The NetworkContext struct should be defined in each c file, not in the config h file. The API uses it as an opaque type. --- test/cbmc/include/core_mqtt_config.h | 6 ------ .../MQTT_GetIncomingPacketTypeAndLength_harness.c | 5 +++++ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/test/cbmc/include/core_mqtt_config.h b/test/cbmc/include/core_mqtt_config.h index 14d562336..c37d577e7 100644 --- a/test/cbmc/include/core_mqtt_config.h +++ b/test/cbmc/include/core_mqtt_config.h @@ -29,12 +29,6 @@ #ifndef CORE_MQTT_CONFIG_H_ #define CORE_MQTT_CONFIG_H_ -/* Mock a network context for the CBMC proofs. */ -struct NetworkContext -{ - int NetworkContext; -}; - /** * @brief Determines the maximum number of MQTT PUBLISH messages, pending * acknowledgement at a time, that are supported for incoming and outgoing diff --git a/test/cbmc/proofs/MQTT_GetIncomingPacketTypeAndLength/MQTT_GetIncomingPacketTypeAndLength_harness.c b/test/cbmc/proofs/MQTT_GetIncomingPacketTypeAndLength/MQTT_GetIncomingPacketTypeAndLength_harness.c index 09cee88f1..88d8c51a1 100644 --- a/test/cbmc/proofs/MQTT_GetIncomingPacketTypeAndLength/MQTT_GetIncomingPacketTypeAndLength_harness.c +++ b/test/cbmc/proofs/MQTT_GetIncomingPacketTypeAndLength/MQTT_GetIncomingPacketTypeAndLength_harness.c @@ -30,6 +30,11 @@ #include "network_interface_stubs.h" #include "mqtt_cbmc_state.h" +struct NetworkContext +{ + int NetworkContext; +}; + void harness() { /* NetworkContext_t is an application defined network interface context. It From 94f7185031571c2726a224a3e024a1ea502470f0 Mon Sep 17 00:00:00 2001 From: Archit Gupta Date: Wed, 31 Jan 2024 11:27:02 -0800 Subject: [PATCH 3/4] Remove config include from header core_mqtt_serializer.h included the user coreMQTT config, which spills the config header's contents into all consumers of coreMQTT's headers. Macros from the config are no longer used in the the API, so this is also no longer used for anything, so can be removed. --- source/include/core_mqtt_serializer.h | 12 ------------ test/cbmc/proofs/MQTT_Connect/MQTT_Connect_harness.c | 1 + test/cbmc/proofs/MQTT_Publish/MQTT_Publish_harness.c | 1 + .../proofs/MQTT_Subscribe/MQTT_Subscribe_harness.c | 1 + .../MQTT_Unsubscribe/MQTT_Unsubscribe_harness.c | 1 + test/unit-test/core_mqtt_utest.c | 2 ++ 6 files changed, 6 insertions(+), 12 deletions(-) diff --git a/source/include/core_mqtt_serializer.h b/source/include/core_mqtt_serializer.h index da2e7e4ed..f9d4cf920 100644 --- a/source/include/core_mqtt_serializer.h +++ b/source/include/core_mqtt_serializer.h @@ -42,18 +42,6 @@ #endif /* *INDENT-ON */ -/* MQTT_DO_NOT_USE_CUSTOM_CONFIG allows building the MQTT library - * without a custom config. If a custom config is provided, the - * MQTT_DO_NOT_USE_CUSTOM_CONFIG macro should not be defined. */ -#ifndef MQTT_DO_NOT_USE_CUSTOM_CONFIG - /* Include custom config file before other headers. */ - #include "core_mqtt_config.h" -#endif - -/* Include config defaults header to get default values of configs not - * defined in core_mqtt_config.h file. */ -#include "core_mqtt_config_defaults.h" - #include "transport_interface.h" /* MQTT packet types. */ diff --git a/test/cbmc/proofs/MQTT_Connect/MQTT_Connect_harness.c b/test/cbmc/proofs/MQTT_Connect/MQTT_Connect_harness.c index dce4d5642..7673ad311 100644 --- a/test/cbmc/proofs/MQTT_Connect/MQTT_Connect_harness.c +++ b/test/cbmc/proofs/MQTT_Connect/MQTT_Connect_harness.c @@ -28,6 +28,7 @@ */ #include "core_mqtt.h" #include "mqtt_cbmc_state.h" +#include "core_mqtt_config_defaults.h" /** * @brief Implement a get time function to return timeout after certain diff --git a/test/cbmc/proofs/MQTT_Publish/MQTT_Publish_harness.c b/test/cbmc/proofs/MQTT_Publish/MQTT_Publish_harness.c index 68d923be5..4f210e359 100644 --- a/test/cbmc/proofs/MQTT_Publish/MQTT_Publish_harness.c +++ b/test/cbmc/proofs/MQTT_Publish/MQTT_Publish_harness.c @@ -28,6 +28,7 @@ */ #include "core_mqtt.h" #include "mqtt_cbmc_state.h" +#include "core_mqtt_config_defaults.h" /** * @brief Implement a get time function to return timeout after certain diff --git a/test/cbmc/proofs/MQTT_Subscribe/MQTT_Subscribe_harness.c b/test/cbmc/proofs/MQTT_Subscribe/MQTT_Subscribe_harness.c index 6798c1c0c..113936018 100644 --- a/test/cbmc/proofs/MQTT_Subscribe/MQTT_Subscribe_harness.c +++ b/test/cbmc/proofs/MQTT_Subscribe/MQTT_Subscribe_harness.c @@ -28,6 +28,7 @@ */ #include "core_mqtt.h" #include "mqtt_cbmc_state.h" +#include "core_mqtt_config_defaults.h" /** * @brief Implement a get time function to return timeout after certain diff --git a/test/cbmc/proofs/MQTT_Unsubscribe/MQTT_Unsubscribe_harness.c b/test/cbmc/proofs/MQTT_Unsubscribe/MQTT_Unsubscribe_harness.c index a441c662d..ef26b2bcf 100644 --- a/test/cbmc/proofs/MQTT_Unsubscribe/MQTT_Unsubscribe_harness.c +++ b/test/cbmc/proofs/MQTT_Unsubscribe/MQTT_Unsubscribe_harness.c @@ -28,6 +28,7 @@ */ #include "core_mqtt.h" #include "mqtt_cbmc_state.h" +#include "core_mqtt_config_defaults.h" /** * @brief Implement a get time function to return timeout after certain diff --git a/test/unit-test/core_mqtt_utest.c b/test/unit-test/core_mqtt_utest.c index d647fd2d5..3e506f644 100644 --- a/test/unit-test/core_mqtt_utest.c +++ b/test/unit-test/core_mqtt_utest.c @@ -38,6 +38,8 @@ #include "mock_core_mqtt_serializer.h" #include "mock_core_mqtt_state.h" +#include "core_mqtt_config_defaults.h" + /* Set network context to double pointer to buffer (uint8_t**). */ struct NetworkContext { From a56cc280a9574428161bda4c554a64794870a2a7 Mon Sep 17 00:00:00 2001 From: Archit Gupta Date: Wed, 31 Jan 2024 11:32:00 -0800 Subject: [PATCH 4/4] Combine config default headers The loggging defaults were split out since they are not namespaced to coreMQTT, and they previously leaked to all files including a coreMQTT header. Splitting them allowed the logging defaults to only be pulled into coreMQTT source files. Now that no header files use the config headers, and thus all coreMQTT config only affects coreMQTT source files, the split is no longer needed. --- source/core_mqtt.c | 2 - source/core_mqtt_serializer.c | 2 - source/core_mqtt_state.c | 2 - source/include/core_mqtt_config_defaults.h | 80 +++++++++++++ source/include/core_mqtt_default_logging.h | 132 --------------------- 5 files changed, 80 insertions(+), 138 deletions(-) delete mode 100644 source/include/core_mqtt_default_logging.h diff --git a/source/core_mqtt.c b/source/core_mqtt.c index 8b72c5d17..92a93b697 100644 --- a/source/core_mqtt.c +++ b/source/core_mqtt.c @@ -35,8 +35,6 @@ /* Include config defaults header to get default values of configs. */ #include "core_mqtt_config_defaults.h" -#include "core_mqtt_default_logging.h" - #ifndef MQTT_PRE_SEND_HOOK /** diff --git a/source/core_mqtt_serializer.c b/source/core_mqtt_serializer.c index 537925996..91f968bbe 100644 --- a/source/core_mqtt_serializer.c +++ b/source/core_mqtt_serializer.c @@ -34,8 +34,6 @@ /* Include config defaults header to get default values of configs. */ #include "core_mqtt_config_defaults.h" -#include "core_mqtt_default_logging.h" - /** * @brief MQTT protocol version 3.1.1. */ diff --git a/source/core_mqtt_state.c b/source/core_mqtt_state.c index 9cd1f221c..cabc81622 100644 --- a/source/core_mqtt_state.c +++ b/source/core_mqtt_state.c @@ -33,8 +33,6 @@ /* Include config defaults header to get default values of configs. */ #include "core_mqtt_config_defaults.h" -#include "core_mqtt_default_logging.h" - /*-----------------------------------------------------------*/ /** diff --git a/source/include/core_mqtt_config_defaults.h b/source/include/core_mqtt_config_defaults.h index a573967f9..136faf8a2 100644 --- a/source/include/core_mqtt_config_defaults.h +++ b/source/include/core_mqtt_config_defaults.h @@ -195,6 +195,86 @@ #error MQTT_SEND_RETRY_TIMEOUT_MS is deprecated. Instead use MQTT_SEND_TIMEOUT_MS. #endif +/** + * @brief Macro that is called in the MQTT library for logging "Error" level + * messages. + * + * To enable error level logging in the MQTT library, this macro should be mapped to the + * application-specific logging implementation that supports error logging. + * + * @note This logging macro is called in the MQTT library with parameters wrapped in + * double parentheses to be ISO C89/C90 standard compliant. For a reference + * POSIX implementation of the logging macros, refer to core_mqtt_config.h files, and the + * logging-stack in demos folder of the + * [AWS IoT Embedded C SDK repository](https://github.com/aws/aws-iot-device-sdk-embedded-C). + * + * Default value: Error logging is turned off, and no code is generated for calls + * to the macro in the MQTT library on compilation. + */ +#ifndef LogError + #define LogError( message ) +#endif + +/** + * @brief Macro that is called in the MQTT library for logging "Warning" level + * messages. + * + * To enable warning level logging in the MQTT library, this macro should be mapped to the + * application-specific logging implementation that supports warning logging. + * + * @note This logging macro is called in the MQTT library with parameters wrapped in + * double parentheses to be ISO C89/C90 standard compliant. For a reference + * POSIX implementation of the logging macros, refer to core_mqtt_config.h files, and the + * logging-stack in demos folder of the + * [AWS IoT Embedded C SDK repository](https://github.com/aws/aws-iot-device-sdk-embedded-C/). + * + * Default value: Warning logs are turned off, and no code is generated for calls + * to the macro in the MQTT library on compilation. + */ +#ifndef LogWarn + #define LogWarn( message ) +#endif + +/** + * @brief Macro that is called in the MQTT library for logging "Info" level + * messages. + * + * To enable info level logging in the MQTT library, this macro should be mapped to the + * application-specific logging implementation that supports info logging. + * + * @note This logging macro is called in the MQTT library with parameters wrapped in + * double parentheses to be ISO C89/C90 standard compliant. For a reference + * POSIX implementation of the logging macros, refer to core_mqtt_config.h files, and the + * logging-stack in demos folder of the + * [AWS IoT Embedded C SDK repository](https://github.com/aws/aws-iot-device-sdk-embedded-C/). + * + * Default value: Info logging is turned off, and no code is generated for calls + * to the macro in the MQTT library on compilation. + */ +#ifndef LogInfo + #define LogInfo( message ) +#endif + +/** + * @brief Macro that is called in the MQTT library for logging "Debug" level + * messages. + * + * To enable debug level logging from MQTT library, this macro should be mapped to the + * application-specific logging implementation that supports debug logging. + * + * @note This logging macro is called in the MQTT library with parameters wrapped in + * double parentheses to be ISO C89/C90 standard compliant. For a reference + * POSIX implementation of the logging macros, refer to core_mqtt_config.h files, and the + * logging-stack in demos folder of the + * [AWS IoT Embedded C SDK repository](https://github.com/aws/aws-iot-device-sdk-embedded-C/). + * + * Default value: Debug logging is turned off, and no code is generated for calls + * to the macro in the MQTT library on compilation. + */ +#ifndef LogDebug + #define LogDebug( message ) +#endif + /* *INDENT-OFF* */ #ifdef __cplusplus } diff --git a/source/include/core_mqtt_default_logging.h b/source/include/core_mqtt_default_logging.h deleted file mode 100644 index 85f8011f0..000000000 --- a/source/include/core_mqtt_default_logging.h +++ /dev/null @@ -1,132 +0,0 @@ -/* - * coreMQTT v2.1.0 - * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * SPDX-License-Identifier: MIT - * - * 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. - */ - -/** - * @file core_mqtt_default_logging.h - * @brief This represents the default values for the logging macros for the MQTT - * library. - * - * @note This file SHOULD NOT be modified. If custom values are needed for - * any configuration macro, a core_mqtt_config.h file should be provided to - * the MQTT library to override the default values defined in this file. - * To use the custom config file, the MQTT_DO_NOT_USE_CUSTOM_CONFIG preprocessor - * macro SHOULD NOT be set. - */ - -#ifndef CORE_MQTT_DEFAULT_LOGGING_H_ -#define CORE_MQTT_DEFAULT_LOGGING_H_ - -/* *INDENT-OFF* */ -#ifdef __cplusplus - extern "C" { -#endif -/* *INDENT-ON* */ - -/** - * @brief Macro that is called in the MQTT library for logging "Error" level - * messages. - * - * To enable error level logging in the MQTT library, this macro should be mapped to the - * application-specific logging implementation that supports error logging. - * - * @note This logging macro is called in the MQTT library with parameters wrapped in - * double parentheses to be ISO C89/C90 standard compliant. For a reference - * POSIX implementation of the logging macros, refer to core_mqtt_config.h files, and the - * logging-stack in demos folder of the - * [AWS IoT Embedded C SDK repository](https://github.com/aws/aws-iot-device-sdk-embedded-C). - * - * Default value: Error logging is turned off, and no code is generated for calls - * to the macro in the MQTT library on compilation. - */ -#ifndef LogError - #define LogError( message ) -#endif - -/** - * @brief Macro that is called in the MQTT library for logging "Warning" level - * messages. - * - * To enable warning level logging in the MQTT library, this macro should be mapped to the - * application-specific logging implementation that supports warning logging. - * - * @note This logging macro is called in the MQTT library with parameters wrapped in - * double parentheses to be ISO C89/C90 standard compliant. For a reference - * POSIX implementation of the logging macros, refer to core_mqtt_config.h files, and the - * logging-stack in demos folder of the - * [AWS IoT Embedded C SDK repository](https://github.com/aws/aws-iot-device-sdk-embedded-C/). - * - * Default value: Warning logs are turned off, and no code is generated for calls - * to the macro in the MQTT library on compilation. - */ -#ifndef LogWarn - #define LogWarn( message ) -#endif - -/** - * @brief Macro that is called in the MQTT library for logging "Info" level - * messages. - * - * To enable info level logging in the MQTT library, this macro should be mapped to the - * application-specific logging implementation that supports info logging. - * - * @note This logging macro is called in the MQTT library with parameters wrapped in - * double parentheses to be ISO C89/C90 standard compliant. For a reference - * POSIX implementation of the logging macros, refer to core_mqtt_config.h files, and the - * logging-stack in demos folder of the - * [AWS IoT Embedded C SDK repository](https://github.com/aws/aws-iot-device-sdk-embedded-C/). - * - * Default value: Info logging is turned off, and no code is generated for calls - * to the macro in the MQTT library on compilation. - */ -#ifndef LogInfo - #define LogInfo( message ) -#endif - -/** - * @brief Macro that is called in the MQTT library for logging "Debug" level - * messages. - * - * To enable debug level logging from MQTT library, this macro should be mapped to the - * application-specific logging implementation that supports debug logging. - * - * @note This logging macro is called in the MQTT library with parameters wrapped in - * double parentheses to be ISO C89/C90 standard compliant. For a reference - * POSIX implementation of the logging macros, refer to core_mqtt_config.h files, and the - * logging-stack in demos folder of the - * [AWS IoT Embedded C SDK repository](https://github.com/aws/aws-iot-device-sdk-embedded-C/). - * - * Default value: Debug logging is turned off, and no code is generated for calls - * to the macro in the MQTT library on compilation. - */ -#ifndef LogDebug - #define LogDebug( message ) -#endif - -/* *INDENT-OFF* */ -#ifdef __cplusplus - } -#endif -/* *INDENT-ON* */ - -#endif /* ifndef CORE_MQTT_DEFAULT_LOGGING_H_ */