diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f0019a1..fafd5447 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,8 @@ endif() if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) include(CTest) include(AddUnitTest) + # Include helpers mocks. + add_subdirectory(applications/helpers) endif() diff --git a/applications/helpers/device_advisor/CMakeLists.txt b/applications/helpers/device_advisor/CMakeLists.txt index fc0cf022..c450213f 100644 --- a/applications/helpers/device_advisor/CMakeLists.txt +++ b/applications/helpers/device_advisor/CMakeLists.txt @@ -2,17 +2,21 @@ # # SPDX-License-Identifier: MIT -add_library(helpers-device-advisor - src/aws_device_advisor_task.c -) +if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING) + # left empty for future mocks. +else() + add_library(helpers-device-advisor + src/aws_device_advisor_task.c + ) -target_include_directories(helpers-device-advisor - PUBLIC - inc -) + target_include_directories(helpers-device-advisor + PUBLIC + inc + ) -target_link_libraries(helpers-device-advisor - coremqtt - coremqtt-agent - helpers-events -) + target_link_libraries(helpers-device-advisor + coremqtt + coremqtt-agent + helpers-events + ) +endif() diff --git a/applications/helpers/events/CMakeLists.txt b/applications/helpers/events/CMakeLists.txt index e2bcc490..0303d35a 100644 --- a/applications/helpers/events/CMakeLists.txt +++ b/applications/helpers/events/CMakeLists.txt @@ -1,16 +1,20 @@ -# Copyright 2023 Arm Limited and/or its affiliates +# Copyright 2023-2024 Arm Limited and/or its affiliates # # SPDX-License-Identifier: MIT -add_library(helpers-events - src/events.c -) +if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING) + add_subdirectory(mocks) +else() + add_library(helpers-events + src/events.c + ) -target_include_directories(helpers-events - PUBLIC - inc -) + target_include_directories(helpers-events + PUBLIC + inc + ) -target_link_libraries(helpers-events - freertos_kernel -) + target_link_libraries(helpers-events + freertos_kernel + ) +endif() diff --git a/applications/helpers/events/mocks/CMakeLists.txt b/applications/helpers/events/mocks/CMakeLists.txt new file mode 100644 index 00000000..56251220 --- /dev/null +++ b/applications/helpers/events/mocks/CMakeLists.txt @@ -0,0 +1,17 @@ +# Copyright 2024 Arm Limited and/or its affiliates +# +# SPDX-License-Identifier: MIT + +add_library(helpers-events-mock + src/events.c +) + +target_include_directories(helpers-events-mock + PUBLIC + inc +) + +target_link_libraries(helpers-events-mock + PRIVATE + fff +) diff --git a/applications/helpers/events/mocks/inc/events.h b/applications/helpers/events/mocks/inc/events.h new file mode 100644 index 00000000..c49eeb32 --- /dev/null +++ b/applications/helpers/events/mocks/inc/events.h @@ -0,0 +1,20 @@ +/* Copyright 2023-2024 Arm Limited and/or its affiliates + * + * SPDX-License-Identifier: MIT + */ + +#ifndef EVENT_H +#define EVENT_H + +#include "fff.h" + +#define EVENT_MASK_MQTT_INIT 0x02 +#define EVENT_MASK_MQTT_CONNECTED 0x04 + +typedef void * EventGroupHandle_t; + +extern EventGroupHandle_t xSystemEvents; + +DECLARE_FAKE_VOID_FUNC( vWaitUntilNetworkIsUp ); + +#endif /* EVENT_H */ diff --git a/components/freertos_kernel/mocks/inc/FreeRTOS.h b/applications/helpers/events/mocks/src/events.c similarity index 55% rename from components/freertos_kernel/mocks/inc/FreeRTOS.h rename to applications/helpers/events/mocks/src/events.c index 6eda672b..5edba0fe 100644 --- a/components/freertos_kernel/mocks/inc/FreeRTOS.h +++ b/applications/helpers/events/mocks/src/events.c @@ -3,9 +3,8 @@ * SPDX-License-Identifier: MIT */ -#ifndef INC_FREERTOS_H -#define INC_FREERTOS_H +#include "events.h" -#define configTICK_RATE_HZ ( 1000 ) +EventGroupHandle_t xSystemEvents; -#endif +DEFINE_FAKE_VOID_FUNC( vWaitUntilNetworkIsUp ); diff --git a/applications/helpers/hdlcd/CMakeLists.txt b/applications/helpers/hdlcd/CMakeLists.txt index 28037499..256b6fc7 100644 --- a/applications/helpers/hdlcd/CMakeLists.txt +++ b/applications/helpers/hdlcd/CMakeLists.txt @@ -2,19 +2,23 @@ # # SPDX-License-Identifier: MIT -add_library(helpers-hdlcd STATIC) +if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING) + # left empty for future mocks. +else() + add_library(helpers-hdlcd STATIC) -target_include_directories(helpers-hdlcd - PUBLIC - . -) + target_include_directories(helpers-hdlcd + PUBLIC + . + ) -target_sources(helpers-hdlcd - PUBLIC - hdlcd_helper.c -) + target_sources(helpers-hdlcd + PUBLIC + hdlcd_helper.c + ) -target_link_libraries(helpers-hdlcd - PUBLIC - fri-bsp -) + target_link_libraries(helpers-hdlcd + PUBLIC + fri-bsp + ) +endif() diff --git a/applications/helpers/logging/CMakeLists.txt b/applications/helpers/logging/CMakeLists.txt index a55668b5..53328da7 100644 --- a/applications/helpers/logging/CMakeLists.txt +++ b/applications/helpers/logging/CMakeLists.txt @@ -1,16 +1,20 @@ -# Copyright 2023 Arm Limited and/or its affiliates +# Copyright 2023-2024 Arm Limited and/or its affiliates # # SPDX-License-Identifier: MIT -add_library(helpers-logging - src/iot_logging_task_dynamic_buffers.c -) +if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING) + add_subdirectory(mocks) +else() + add_library(helpers-logging + src/iot_logging_task_dynamic_buffers.c + ) -target_include_directories(helpers-logging - PUBLIC - inc -) + target_include_directories(helpers-logging + PUBLIC + inc + ) -target_link_libraries(helpers-logging - freertos_kernel -) + target_link_libraries(helpers-logging + freertos_kernel + ) +endif() diff --git a/applications/helpers/logging/mocks/CMakeLists.txt b/applications/helpers/logging/mocks/CMakeLists.txt new file mode 100644 index 00000000..4f97915f --- /dev/null +++ b/applications/helpers/logging/mocks/CMakeLists.txt @@ -0,0 +1,17 @@ +# Copyright 2024 Arm Limited and/or its affiliates +# +# SPDX-License-Identifier: MIT + +add_library(helpers-logging-mock + INTERFACE +) + +target_include_directories(helpers-logging-mock + INTERFACE + inc +) + +target_link_libraries(helpers-logging-mock + INTERFACE + fff +) diff --git a/applications/helpers/logging/mocks/inc/logging_levels.h b/applications/helpers/logging/mocks/inc/logging_levels.h new file mode 100644 index 00000000..cc8b8792 --- /dev/null +++ b/applications/helpers/logging/mocks/inc/logging_levels.h @@ -0,0 +1,28 @@ +/* + * FreeRTOS Common V1.1.3 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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 LOGGING_LEVELS_H_ +#define LOGGING_LEVELS_H_ + +#endif /* LOGGING_LEVELS_H_ */ diff --git a/applications/helpers/logging/mocks/inc/logging_stack.h b/applications/helpers/logging/mocks/inc/logging_stack.h new file mode 100644 index 00000000..b01f00f4 --- /dev/null +++ b/applications/helpers/logging/mocks/inc/logging_stack.h @@ -0,0 +1,48 @@ +/* + * FreeRTOS Common V1.1.3 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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 LOGGING_STACK_H_ +#define LOGGING_STACK_H_ + +#include "fff.h" + +DECLARE_FAKE_VOID_FUNC_VARARG( SdkLogError, + const char *, + ... ); +#define LogError( message ) ( SdkLogError message ) +/* The macro suppresses errors for now. */ +DECLARE_FAKE_VOID_FUNC_VARARG( SdkLogWarn, + const char *, + ... ); +#define LogWarn( message ) ( SdkLogError message ) +DECLARE_FAKE_VOID_FUNC_VARARG( SdkLogInfo, + const char *, + ... ); +#define LogInfo( message ) ( SdkLogInfo message ) +DECLARE_FAKE_VOID_FUNC_VARARG( SdkLogDebug, + const char *, + ... ); +#define LogDebug( message ) ( SdkLogDebug message ) + +#endif /* ifndef LOGGING_STACK_H_ */ diff --git a/applications/helpers/provisioning/CMakeLists.txt b/applications/helpers/provisioning/CMakeLists.txt index 6961a5af..1dac4020 100644 --- a/applications/helpers/provisioning/CMakeLists.txt +++ b/applications/helpers/provisioning/CMakeLists.txt @@ -2,93 +2,97 @@ # # SPDX-License-Identifier: MIT -find_package(Python3) - -#FIXME: This is needed in order to run prepoccessor on the linker script. -# The GCC prepoccessor puts std defines if -g3 is set. -if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU") - string(REPLACE "-g3" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS}) -endif() - -list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake) +if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING) + # left empty for future mocks. +else() + find_package(Python3) -include(SetProvisioningLinkOptions) + #FIXME: This is needed in order to run prepoccessor on the linker script. + # The GCC prepoccessor puts std defines if -g3 is set. + if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU") + string(REPLACE "-g3" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS}) + endif() -add_executable(provisioning_data provisioning_data.c) -target_include_directories(provisioning_data - PUBLIC - ${CMAKE_CURRENT_BINARY_DIR} - ${CMAKE_CURRENT_LIST_DIR} - ${CMAKE_CURRENT_LIST_DIR}/inc -) + list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake) -target_link_libraries(provisioning_data PRIVATE - fri-bsp -) + include(SetProvisioningLinkOptions) -ExternalProject_Get_Property(trusted_firmware-m-build BINARY_DIR) + add_executable(provisioning_data provisioning_data.c) + target_include_directories(provisioning_data + PUBLIC + ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_CURRENT_LIST_DIR} + ${CMAKE_CURRENT_LIST_DIR}/inc + ) -if(FREERTOS_LIBRARIES_INTEGRATION_TESTS EQUAL 1) - set( - CODE_SIGNING_PUBLIC_KEY_PEM_PATH - ${freertos_libraries_integration_tests_SOURCE_DIR}/src/ota/test_files/tfm-rsa-sha256-signer.crt.pem + target_link_libraries(provisioning_data PRIVATE + fri-bsp ) -else() - set( - CODE_SIGNING_PUBLIC_KEY_PEM_PATH - ${BINARY_DIR}/api_ns/image_signing/keys/image_ns_signing_public_key.pem + + ExternalProject_Get_Property(trusted_firmware-m-build BINARY_DIR) + + if(FREERTOS_LIBRARIES_INTEGRATION_TESTS EQUAL 1) + set( + CODE_SIGNING_PUBLIC_KEY_PEM_PATH + ${freertos_libraries_integration_tests_SOURCE_DIR}/src/ota/test_files/tfm-rsa-sha256-signer.crt.pem + ) + else() + set( + CODE_SIGNING_PUBLIC_KEY_PEM_PATH + ${BINARY_DIR}/api_ns/image_signing/keys/image_ns_signing_public_key.pem + ) + endif() + + add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/aws_clientcredential_keys.h + DEPENDS ${AWS_CLIENT_PRIVATE_KEY_PEM_PATH} + DEPENDS ${AWS_CLIENT_CERTIFICATE_PEM_PATH} + COMMAND + ${Python3_EXECUTABLE} ${IOT_REFERENCE_ARM_CORSTONE3XX_SOURCE_DIR}/tools/scripts/generate_credentials_header.py + ${CMAKE_CURRENT_BINARY_DIR} + --path-to-client-private-key-pem ${AWS_CLIENT_PRIVATE_KEY_PEM_PATH} + --path-to-client-certificate-pem ${AWS_CLIENT_CERTIFICATE_PEM_PATH} + --path-to-code-signing-public-key-pem ${CODE_SIGNING_PUBLIC_KEY_PEM_PATH} ) -endif() -add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/aws_clientcredential_keys.h - DEPENDS ${AWS_CLIENT_PRIVATE_KEY_PEM_PATH} - DEPENDS ${AWS_CLIENT_CERTIFICATE_PEM_PATH} - COMMAND - ${Python3_EXECUTABLE} ${IOT_REFERENCE_ARM_CORSTONE3XX_SOURCE_DIR}/tools/scripts/generate_credentials_header.py - ${CMAKE_CURRENT_BINARY_DIR} - --path-to-client-private-key-pem ${AWS_CLIENT_PRIVATE_KEY_PEM_PATH} - --path-to-client-certificate-pem ${AWS_CLIENT_CERTIFICATE_PEM_PATH} - --path-to-code-signing-public-key-pem ${CODE_SIGNING_PUBLIC_KEY_PEM_PATH} -) - -add_custom_target(aws_clientcredential_keys_header - SOURCES - ${CMAKE_CURRENT_BINARY_DIR}/aws_clientcredential_keys.h -) - -add_dependencies(aws_clientcredential_keys_header trusted_firmware-m-build) -add_dependencies(provisioning_data aws_clientcredential_keys_header) - -if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU") - target_link_options(provisioning_data - PRIVATE - "-nostartfiles" + add_custom_target(aws_clientcredential_keys_header + SOURCES + ${CMAKE_CURRENT_BINARY_DIR}/aws_clientcredential_keys.h ) - target_add_scatter_file(provisioning_data ${CMAKE_CURRENT_LIST_DIR}/provisioning_data.ld) -else() - target_add_scatter_file(provisioning_data ${CMAKE_CURRENT_LIST_DIR}/provisioning_data.sct) -endif() -list(APPEND CMAKE_MODULE_PATH ${IOT_REFERENCE_ARM_CORSTONE3XX_SOURCE_DIR}/tools/cmake) -include(ConvertElfToBin) + add_dependencies(aws_clientcredential_keys_header trusted_firmware-m-build) + add_dependencies(provisioning_data aws_clientcredential_keys_header) + + if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU") + target_link_options(provisioning_data + PRIVATE + "-nostartfiles" + ) + target_add_scatter_file(provisioning_data ${CMAKE_CURRENT_LIST_DIR}/provisioning_data.ld) + else() + target_add_scatter_file(provisioning_data ${CMAKE_CURRENT_LIST_DIR}/provisioning_data.sct) + endif() -add_custom_command(OUTPUT provisioning_data.bin - DEPENDS $/provisioning_data${CMAKE_EXECUTABLE_SUFFIX} -) + list(APPEND CMAKE_MODULE_PATH ${IOT_REFERENCE_ARM_CORSTONE3XX_SOURCE_DIR}/tools/cmake) + include(ConvertElfToBin) -target_elf_to_bin(provisioning_data provisioning_data) + add_custom_command(OUTPUT provisioning_data.bin + DEPENDS $/provisioning_data${CMAKE_EXECUTABLE_SUFFIX} + ) -add_custom_target(provisioning_data_bin ALL - SOURCES provisioning_data.bin -) + target_elf_to_bin(provisioning_data provisioning_data) -add_library(provisioning-lib - dev_mode_key_provisioning.c - ${corepkcs11_SOURCE_DIR}/source/dependency/3rdparty/mbedtls_utils/mbedtls_utils.c -) + add_custom_target(provisioning_data_bin ALL + SOURCES provisioning_data.bin + ) -target_link_libraries(provisioning-lib - corepkcs11 - freertos_kernel - mbedtls -) + add_library(provisioning-lib + dev_mode_key_provisioning.c + ${corepkcs11_SOURCE_DIR}/source/dependency/3rdparty/mbedtls_utils/mbedtls_utils.c + ) + + target_link_libraries(provisioning-lib + corepkcs11 + freertos_kernel + mbedtls + ) +endif() # BUILD_TESTING AND NOT CMAKE_CROSS_COMPILING diff --git a/applications/helpers/sntp/CMakeLists.txt b/applications/helpers/sntp/CMakeLists.txt index e52382f2..f85451ea 100644 --- a/applications/helpers/sntp/CMakeLists.txt +++ b/applications/helpers/sntp/CMakeLists.txt @@ -2,25 +2,29 @@ # # SPDX-License-Identifier: MIT -add_library(helpers-sntp - src/sntp_client_task.c -) +if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING) + # left empty for future mocks. +else() + add_library(helpers-sntp + src/sntp_client_task.c + ) -target_include_directories(helpers-sntp - PUBLIC - inc -) + target_include_directories(helpers-sntp + PUBLIC + inc + ) -target_include_directories(coresntp-config - INTERFACE - inc -) + target_include_directories(coresntp-config + INTERFACE + inc + ) -target_link_libraries(helpers-sntp - backoff-algorithm - connectivity-stack - corepkcs11 - coresntp - helpers-logging - mbedtls -) + target_link_libraries(helpers-sntp + backoff-algorithm + connectivity-stack + corepkcs11 + coresntp + helpers-logging + mbedtls + ) +endif() diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index d3758f80..78e778d1 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -20,4 +20,8 @@ elseif(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING) add_subdirectory(freertos_kernel) add_subdirectory(aws_iot) + + add_subdirectory(connectivity) + + add_subdirectory(security) endif() diff --git a/components/aws_iot/coremqtt/CMakeLists.txt b/components/aws_iot/coremqtt/CMakeLists.txt index c5e3b98f..24283d3c 100644 --- a/components/aws_iot/coremqtt/CMakeLists.txt +++ b/components/aws_iot/coremqtt/CMakeLists.txt @@ -3,7 +3,7 @@ # SPDX-License-Identifier: MIT if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING) - add_subdirectory(mocks) + add_subdirectory(library_mocks) else() set(coremqtt_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/library diff --git a/components/aws_iot/coremqtt/mocks/CMakeLists.txt b/components/aws_iot/coremqtt/library_mocks/CMakeLists.txt similarity index 73% rename from components/aws_iot/coremqtt/mocks/CMakeLists.txt rename to components/aws_iot/coremqtt/library_mocks/CMakeLists.txt index 0061b82d..e8e2e264 100644 --- a/components/aws_iot/coremqtt/mocks/CMakeLists.txt +++ b/components/aws_iot/coremqtt/library_mocks/CMakeLists.txt @@ -2,15 +2,18 @@ # # SPDX-License-Identifier: MIT -add_library(coremqtt-mock INTERFACE) +add_library(coremqtt-mock + src/core_mqtt.c +) target_include_directories(coremqtt-mock - INTERFACE + PUBLIC inc + interface ../integration/inc ) target_link_libraries(coremqtt-mock - INTERFACE + PRIVATE fff ) diff --git a/components/aws_iot/coremqtt/library_mocks/inc/core_mqtt.h b/components/aws_iot/coremqtt/library_mocks/inc/core_mqtt.h new file mode 100644 index 00000000..04f76811 --- /dev/null +++ b/components/aws_iot/coremqtt/library_mocks/inc/core_mqtt.h @@ -0,0 +1,54 @@ +/* + * coreMQTT v2.1.1 + * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2023-2024 Arm Limited and/or its affiliates + * + * 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 CORE_MQTT_H +#define CORE_MQTT_H + +#include "fff.h" +#include +#include + +#include "core_mqtt_serializer.h" + +typedef enum MQTTSubAckStatus +{ + MQTTSubAckFailure = 0x80 +} MQTTSubAckStatus_t; + +typedef uint32_t (* MQTTGetCurrentTimeFunc_t )( void ); + +DECLARE_FAKE_VALUE_FUNC( const char *, + MQTT_Status_strerror, + MQTTStatus_t ); + +DECLARE_FAKE_VALUE_FUNC( MQTTStatus_t, + MQTT_Connect, + void *, + const MQTTConnectInfo_t *, + const MQTTPublishInfo_t *, + uint32_t, + bool * ); + +#endif /* CORE_MQTT_H */ diff --git a/components/aws_iot/coremqtt/library_mocks/inc/core_mqtt_serializer.h b/components/aws_iot/coremqtt/library_mocks/inc/core_mqtt_serializer.h new file mode 100644 index 00000000..86ca96f3 --- /dev/null +++ b/components/aws_iot/coremqtt/library_mocks/inc/core_mqtt_serializer.h @@ -0,0 +1,82 @@ +/* + * coreMQTT v2.1.1 + * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * 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 CORE_MQTT_SERIALIZER_H +#define CORE_MQTT_SERIALIZER_H + +#include +#include + +typedef enum MQTTStatus +{ + MQTTSuccess = 0, + MQTTBadParameter, + MQTTSendFailed +} MQTTStatus_t; + +typedef struct MQTTConnectInfo +{ + bool cleanSession; + uint16_t keepAliveSeconds; + const char * pClientIdentifier; + uint16_t clientIdentifierLength; + const char * pUserName; + uint16_t userNameLength; + const char * pPassword; + uint16_t passwordLength; +} MQTTConnectInfo_t; + +typedef enum MQTTQoS +{ + MQTTQoS0 = 0, + MQTTQoS1 = 1, + MQTTQoS2 = 2 +} MQTTQoS_t; + +typedef struct MQTTPublishInfo +{ + MQTTQoS_t qos; + bool retain; + bool dup; + const char * pTopicName; + uint16_t topicNameLength; + const void * pPayload; + size_t payloadLength; +} MQTTPublishInfo_t; + +typedef struct MQTTSubscribeInfo +{ + MQTTQoS_t qos; + const char * pTopicFilter; + uint16_t topicFilterLength; +} MQTTSubscribeInfo_t; + +typedef struct MQTTFixedBuffer +{ + uint8_t * pBuffer; + size_t size; +} MQTTFixedBuffer_t; + +#endif /* ifndef CORE_MQTT_SERIALIZER_H */ diff --git a/components/aws_iot/coremqtt/library_mocks/interface/transport_interface.h b/components/aws_iot/coremqtt/library_mocks/interface/transport_interface.h new file mode 100644 index 00000000..2ec44736 --- /dev/null +++ b/components/aws_iot/coremqtt/library_mocks/interface/transport_interface.h @@ -0,0 +1,38 @@ +/* + * coreMQTT v2.1.1 + * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * 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 TRANSPORT_INTERFACE_H_ +#define TRANSPORT_INTERFACE_H_ + +typedef struct TransportInterface +{ + int recv; + int send; + int writev; + int * pNetworkContext; +} TransportInterface_t; + + +#endif /* ifndef TRANSPORT_INTERFACE_H_ */ diff --git a/components/aws_iot/coremqtt/library_mocks/src/core_mqtt.c b/components/aws_iot/coremqtt/library_mocks/src/core_mqtt.c new file mode 100644 index 00000000..22c968f1 --- /dev/null +++ b/components/aws_iot/coremqtt/library_mocks/src/core_mqtt.c @@ -0,0 +1,38 @@ +/* + * coreMQTT v2.1.1 + * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * 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. + */ + +#include "core_mqtt.h" + +DEFINE_FAKE_VALUE_FUNC( const char *, + MQTT_Status_strerror, + MQTTStatus_t ); + +DEFINE_FAKE_VALUE_FUNC( MQTTStatus_t, + MQTT_Connect, + void *, + const MQTTConnectInfo_t *, + const MQTTPublishInfo_t *, + uint32_t, + bool * ); diff --git a/components/aws_iot/coremqtt/mocks/inc/core_mqtt.h b/components/aws_iot/coremqtt/mocks/inc/core_mqtt.h deleted file mode 100644 index 8589830b..00000000 --- a/components/aws_iot/coremqtt/mocks/inc/core_mqtt.h +++ /dev/null @@ -1,9 +0,0 @@ -/* Copyright 2023-2024 Arm Limited and/or its affiliates - * - * SPDX-License-Identifier: MIT - */ - -#ifndef CORE_MQTT_H -#define CORE_MQTT_H - -#endif // CORE_MQTT_H diff --git a/components/aws_iot/coremqtt_agent/CMakeLists.txt b/components/aws_iot/coremqtt_agent/CMakeLists.txt index 68e3b984..cb7cafc9 100644 --- a/components/aws_iot/coremqtt_agent/CMakeLists.txt +++ b/components/aws_iot/coremqtt_agent/CMakeLists.txt @@ -3,7 +3,7 @@ # SPDX-License-Identifier: MIT if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING) - add_subdirectory(mocks) + add_subdirectory(library_mocks) else() set(coremqtt_agent_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/library diff --git a/components/aws_iot/coremqtt_agent/integration/CMakeLists.txt b/components/aws_iot/coremqtt_agent/integration/CMakeLists.txt index 4c20335a..142d2487 100644 --- a/components/aws_iot/coremqtt_agent/integration/CMakeLists.txt +++ b/components/aws_iot/coremqtt_agent/integration/CMakeLists.txt @@ -3,6 +3,7 @@ # SPDX-License-Identifier: MIT if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING) + add_subdirectory(integration_mocks) add_subdirectory(tests) else() include(${coremqtt_agent_SOURCE_DIR}/mqttAgentFilePaths.cmake) diff --git a/components/aws_iot/coremqtt_agent/integration/inc/mqtt_agent_task.h b/components/aws_iot/coremqtt_agent/integration/inc/mqtt_agent_task.h index a3b0fa9e..0cb71855 100644 --- a/components/aws_iot/coremqtt_agent/integration/inc/mqtt_agent_task.h +++ b/components/aws_iot/coremqtt_agent/integration/inc/mqtt_agent_task.h @@ -1,7 +1,7 @@ /* * FreeRTOS V202012.00 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * Copyright 2023 Arm Limited and/or its affiliates + * Copyright 2023-2024 Arm Limited and/or its affiliates * * * Permission is hereby granted, free of charge, to any person obtaining a copy of @@ -33,9 +33,17 @@ #include "FreeRTOS.h" #include "task.h" +#ifdef UNIT_TESTING + +/* Transport interface header file. + * Needed to compile UNIT_TESTING function prototypes. */ + #include "transport_interface_api.h" +#endif /* UNIT_TESTING */ + /* MQTT library includes. */ #include "core_mqtt_config.h" #include "core_mqtt_agent.h" +#include "core_mqtt_serializer.h" /** * @brief Defines the structure to use as the command callback context in this @@ -48,9 +56,6 @@ struct MQTTAgentCommandContext void * pArgs; }; -void vWaitUntilMQTTAgentReady( void ); -void vWaitUntilMQTTAgentConnected( void ); -bool xIsMqttAgentConnected( void ); void vStartMqttAgentTask( void ); #endif /* MQTT_AGENT_H */ diff --git a/components/aws_iot/coremqtt_agent/integration/integration_mocks/CMakeLists.txt b/components/aws_iot/coremqtt_agent/integration/integration_mocks/CMakeLists.txt new file mode 100644 index 00000000..97d04240 --- /dev/null +++ b/components/aws_iot/coremqtt_agent/integration/integration_mocks/CMakeLists.txt @@ -0,0 +1,22 @@ +# Copyright 2023-2024 Arm Limited and/or its affiliates +# +# SPDX-License-Identifier: MIT + +add_library(coremqtt-agent-integration-mock + src/freertos_agent_message.c + src/freertos_command_pool.c + src/subscription_manager.c +) + +target_include_directories(coremqtt-agent-integration-mock + PUBLIC + inc +) + +target_link_libraries(coremqtt-agent-integration-mock + PRIVATE + fff + coremqtt-mock + coremqtt-agent-mock + freertos-kernel-mock +) diff --git a/components/aws_iot/coremqtt_agent/integration/integration_mocks/inc/freertos_agent_message.h b/components/aws_iot/coremqtt_agent/integration/integration_mocks/inc/freertos_agent_message.h new file mode 100644 index 00000000..9573f451 --- /dev/null +++ b/components/aws_iot/coremqtt_agent/integration/integration_mocks/inc/freertos_agent_message.h @@ -0,0 +1,54 @@ +/* + * FreeRTOS V202104.00 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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 + * + */ + +#ifndef FREERTOS_AGENT_MESSAGE_H +#define FREERTOS_AGENT_MESSAGE_H + +#include +#include +#include "core_mqtt_agent_message_interface.h" +#include "fff.h" +#include "queue.h" + +struct MQTTAgentMessageContext +{ + QueueHandle_t queue; +}; + +DECLARE_FAKE_VALUE_FUNC( bool, + Agent_MessageSend, + MQTTAgentMessageContext_t *, + MQTTAgentCommand_t * const *, + uint32_t ); +DECLARE_FAKE_VALUE_FUNC( bool, + Agent_MessageReceive, + MQTTAgentMessageContext_t *, + MQTTAgentCommand_t **, + uint32_t ); + +#endif /* FREERTOS_AGENT_MESSAGE_H */ diff --git a/components/aws_iot/coremqtt_agent/integration/integration_mocks/inc/freertos_command_pool.h b/components/aws_iot/coremqtt_agent/integration/integration_mocks/inc/freertos_command_pool.h new file mode 100644 index 00000000..59619a85 --- /dev/null +++ b/components/aws_iot/coremqtt_agent/integration/integration_mocks/inc/freertos_command_pool.h @@ -0,0 +1,46 @@ +/* + * FreeRTOS V202104.00 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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 + * + */ + +#ifndef FREERTOS_COMMAND_POOL_H +#define FREERTOS_COMMAND_POOL_H + +#include "fff.h" +#include "core_mqtt_agent_message_interface.h" +#include +#include + +DECLARE_FAKE_VOID_FUNC( Agent_InitializePool ); +DECLARE_FAKE_VALUE_FUNC( MQTTAgentCommand_t *, + Agent_GetCommand, + uint32_t ); +DECLARE_FAKE_VALUE_FUNC( bool, + Agent_ReleaseCommand, + MQTTAgentCommand_t * ); + + +#endif /* FREERTOS_COMMAND_POOL_H */ diff --git a/components/aws_iot/coremqtt_agent/integration/integration_mocks/inc/subscription_manager.h b/components/aws_iot/coremqtt_agent/integration/integration_mocks/inc/subscription_manager.h new file mode 100644 index 00000000..d0f9aa86 --- /dev/null +++ b/components/aws_iot/coremqtt_agent/integration/integration_mocks/inc/subscription_manager.h @@ -0,0 +1,55 @@ +/* + * FreeRTOS V202011.00 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.] + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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://aws.amazon.com/freertos + * + */ + +#ifndef SUBSCRIPTION_MANAGER_H +#define SUBSCRIPTION_MANAGER_H + + +#include "fff.h" +#include +#include "core_mqtt_serializer.h" + +typedef struct SubscriptionElement +{ + int usFilterStringLength; + const char * pcSubscriptionFilterString; +} SubscriptionElement_t; + +#ifndef SUBSCRIPTION_MANAGER_MAX_SUBSCRIPTIONS + #define SUBSCRIPTION_MANAGER_MAX_SUBSCRIPTIONS 10U +#endif + +DECLARE_FAKE_VOID_FUNC( removeSubscription, + const char *, + uint16_t ); + +DECLARE_FAKE_VALUE_FUNC( bool, + handleIncomingPublishes, + MQTTPublishInfo_t * ); + +#endif /* SUBSCRIPTION_MANAGER_H */ diff --git a/components/aws_iot/coremqtt_agent/integration/integration_mocks/src/freertos_agent_message.c b/components/aws_iot/coremqtt_agent/integration/integration_mocks/src/freertos_agent_message.c new file mode 100644 index 00000000..9a471884 --- /dev/null +++ b/components/aws_iot/coremqtt_agent/integration/integration_mocks/src/freertos_agent_message.c @@ -0,0 +1,40 @@ +/* + * FreeRTOS V202104.00 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2023-2024 Arm Limited and/or its affiliates + * + * + * 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 + * + */ + +#include "freertos_agent_message.h" + +DEFINE_FAKE_VALUE_FUNC( bool, + Agent_MessageSend, + MQTTAgentMessageContext_t *, + MQTTAgentCommand_t * const *, + uint32_t ); +DEFINE_FAKE_VALUE_FUNC( bool, + Agent_MessageReceive, + MQTTAgentMessageContext_t *, + MQTTAgentCommand_t * *, + uint32_t ); diff --git a/components/aws_iot/coremqtt_agent/integration/integration_mocks/src/freertos_command_pool.c b/components/aws_iot/coremqtt_agent/integration/integration_mocks/src/freertos_command_pool.c new file mode 100644 index 00000000..40a4c439 --- /dev/null +++ b/components/aws_iot/coremqtt_agent/integration/integration_mocks/src/freertos_command_pool.c @@ -0,0 +1,38 @@ +/* + * FreeRTOS V202104.00 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2023-2024 Arm Limited and/or its affiliates + * + * + * 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 + * + */ + + +#include "freertos_command_pool.h" + +DEFINE_FAKE_VOID_FUNC( Agent_InitializePool ); +DEFINE_FAKE_VALUE_FUNC( MQTTAgentCommand_t *, + Agent_GetCommand, + uint32_t ); +DEFINE_FAKE_VALUE_FUNC( bool, + Agent_ReleaseCommand, + MQTTAgentCommand_t * ); diff --git a/components/aws_iot/coremqtt_agent/integration/integration_mocks/src/subscription_manager.c b/components/aws_iot/coremqtt_agent/integration/integration_mocks/src/subscription_manager.c new file mode 100644 index 00000000..00d782ae --- /dev/null +++ b/components/aws_iot/coremqtt_agent/integration/integration_mocks/src/subscription_manager.c @@ -0,0 +1,39 @@ +/* + * FreeRTOS V202212.00 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2023-2024 Arm Limited and/or its affiliates + * + * + * 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 + * + */ + +#include "subscription_manager.h" + +DEFINE_FAKE_VOID_FUNC( removeSubscription, + const char *, + uint16_t ); + +DEFINE_FAKE_VALUE_FUNC( bool, + handleIncomingPublishes, + MQTTPublishInfo_t * ); + +SubscriptionElement_t xGlobalSubscriptionList[ SUBSCRIPTION_MANAGER_MAX_SUBSCRIPTIONS ]; diff --git a/components/aws_iot/coremqtt_agent/integration/src/mqtt_agent_task.c b/components/aws_iot/coremqtt_agent/integration/src/mqtt_agent_task.c index e2ed89d8..b147e7a1 100644 --- a/components/aws_iot/coremqtt_agent/integration/src/mqtt_agent_task.c +++ b/components/aws_iot/coremqtt_agent/integration/src/mqtt_agent_task.c @@ -1,7 +1,7 @@ /* * FreeRTOS V202012.00 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * Copyright 2023 Arm Limited and/or its affiliates + * Copyright 2023-2024 Arm Limited and/or its affiliates * * * Permission is hereby granted, free of charge, to any person obtaining a copy of @@ -40,14 +40,20 @@ /* includes for TFM */ #include "psa/crypto.h" +#include "psa/crypto_types.h" #include "psa/error.h" /* Demo config includes. */ #include "demo_config.h" +#include "FreeRTOSConfig.h" #include "iot_default_root_certificates.h" /* MQTT library includes. */ +#include "core_mqtt_agent_message_interface.h" #include "core_mqtt_agent.h" +#include "core_mqtt_config.h" +#include "core_mqtt.h" +#include "transport_interface.h" /* MQTT Agent ports. */ #include "freertos_agent_message.h" @@ -74,6 +80,17 @@ #endif #include "logging_stack.h" +/* Kernel includes */ +#include "event_groups.h" +#include "queue.h" + +/* Provides external linkage only when running unit test */ +#ifdef UNIT_TESTING + #define STATIC /* as nothing */ +#else /* ifdef UNIT_TESTING */ + #define STATIC static +#endif /* UNIT_TESTING */ + /** * @brief Dimensions the buffer used to serialize and deserialize MQTT packets. * @note Specified in bytes. Must be large enough to hold the maximum @@ -191,56 +208,7 @@ extern SubscriptionElement_t xGlobalSubscriptionList[ SUBSCRIPTION_MANAGER_MAX_S /*-----------------------------------------------------------*/ -/** - * @brief Task for MQTT agent. - * Task runs MQTT agent command loop, which returns only when the user disconnects - * MQTT, terminates agent, or the mqtt connection is broken. If the mqtt connection is broken, the task - * tries to reconnect to the broker. - * - * @param[in] pParam Can be used to pass down functionality to the agent task - */ -static void prvMQTTAgentTask( void * pParam ); - -/** - * @brief Retry logic to establish a connection to the MQTT broker. - * - * If the connection fails, keep retrying with exponentially increasing - * timeout value, until max retries, max timeout or successful connect. - * - * @param[in] pNetworkContext Network context to connect on. - * @return int pdFALSE if connection failed after retries. - */ -static BaseType_t prvSocketConnect( NetworkContext_t * pNetworkContext ); - -/** - * @brief Disconnects from the MQTT broker. - * Initiates an MQTT disconnect and then teardown underlying TCP connection. - * - */ -static void prvDisconnectFromMQTTBroker( void ); - -/** - * @brief Initializes an MQTT context, including transport interface and - * network buffer. - * - * @return `MQTTSuccess` if the initialization succeeds, else `MQTTBadParameter`. - */ -static MQTTStatus_t prvMqttInit( void ); - -/** - * @brief Sends an MQTT Connect packet over the already connected TCP socket. - * - * @param[in] pxMQTTContext MQTT context pointer. - * @param[in] xCleanSession If a clean session should be established. - * - * @return `MQTTSuccess` if connection succeeds, else appropriate error code - * from MQTT_Connect. - */ -static MQTTStatus_t prvMQTTConnect( void ); - -/*-----------------------------------------------------------*/ - -static uint32_t prvGetTimeMs( void ) +STATIC uint32_t prvGetTimeMs( void ) { TickType_t xTickCount = 0; uint32_t ulTimeMs = 0UL; @@ -258,7 +226,7 @@ static uint32_t prvGetTimeMs( void ) return ulTimeMs; } -static UBaseType_t prvGetRandomNumber( void ) +STATIC UBaseType_t prvGetRandomNumber( void ) { psa_status_t xPsaStatus = PSA_ERROR_PROGRAMMER_ERROR; UBaseType_t uxRandomValue = 0U; @@ -269,16 +237,22 @@ static UBaseType_t prvGetRandomNumber( void ) { LogError( ( "psa_generate_random failed with %d.", xPsaStatus ) ); LogError( ( "Using xTaskGetTickCount() as random number generator" ) ); - } - else - { uxRandomValue = xTaskGetTickCount(); } return uxRandomValue; } -static BaseType_t prvSocketConnect( NetworkContext_t * pxNetworkContext ) +/** + * @brief Retry logic to establish a connection to the MQTT broker. + * + * If the connection fails, keep retrying with exponentially increasing + * timeout value, until max retries, max timeout or successful connect. + * + * @param[in] pNetworkContext Network context to connect on. + * @return int pdFALSE if connection failed after retries. + */ +STATIC BaseType_t prvSocketConnect( NetworkContext_t * pxNetworkContext ) { BaseType_t xConnected = pdFAIL; @@ -341,20 +315,23 @@ static BaseType_t prvSocketConnect( NetworkContext_t * pxNetworkContext ) return xConnected; } -static BaseType_t prvSocketDisconnect( NetworkContext_t * pxNetworkContext ) +STATIC BaseType_t prvSocketDisconnect( NetworkContext_t * pxNetworkContext ) { BaseType_t xDisconnected = pdFAIL; LogInfo( ( "Disconnecting TLS connection.\n" ) ); - Transport_Disconnect( pxNetworkContext ); - xDisconnected = pdPASS; + xDisconnected = pdFAIL; - ( void ) xEventGroupClearBits( xSystemEvents, EVENT_MASK_MQTT_CONNECTED ); + if( Transport_Disconnect( pxNetworkContext ) == TRANSPORT_STATUS_SUCCESS ) + { + xDisconnected = pdPASS; + ( void ) xEventGroupClearBits( xSystemEvents, EVENT_MASK_MQTT_CONNECTED ); + } return xDisconnected; } -static void prvIncomingPublishCallback( MQTTAgentContext_t * pMqttAgentContext, +STATIC void prvIncomingPublishCallback( MQTTAgentContext_t * pMqttAgentContext, uint16_t packetId, MQTTPublishInfo_t * pxPublishInfo ) { @@ -374,13 +351,15 @@ static void prvIncomingPublishCallback( MQTTAgentContext_t * pMqttAgentContext, * write the message ID, which is restored afterwards. */ char * pcLocation = ( char * ) &( pxPublishInfo->pTopicName[ pxPublishInfo->topicNameLength ] ); char cOriginalChar = *pcLocation; - *pcLocation = 0x00; - LogWarn( ( "Received an unsolicited publish from topic %s", pxPublishInfo->pTopicName ) ); - *pcLocation = cOriginalChar; + /* Copy to safe buffer for printing. */ + char printBuffer[ pxPublishInfo->topicNameLength + 1 ]; + memcpy( printBuffer, &( pxPublishInfo->pTopicName ), pxPublishInfo->topicNameLength ); + printBuffer[ pxPublishInfo->topicNameLength ] = 0x00; /* ends in '\0' */ + LogWarn( ( "Received an unsolicited publish from topic %s", printBuffer ) ); } } -static void prvReSubscriptionCommandCallback( MQTTAgentCommandContext_t * pxCommandContext, +STATIC void prvReSubscriptionCommandCallback( MQTTAgentCommandContext_t * pxCommandContext, MQTTAgentReturnInfo_t * pxReturnInfo ) { MQTTAgentSubscribeArgs_t * pxSubscribeArgs = ( MQTTAgentSubscribeArgs_t * ) pxCommandContext; @@ -412,7 +391,13 @@ static void prvReSubscriptionCommandCallback( MQTTAgentCommandContext_t * pxComm } } -static MQTTStatus_t prvMQTTInit( void ) +/** + * @brief Initializes an MQTT context, including transport interface and + * network buffer. + * + * @return `MQTTSuccess` if the initialization succeeds, else `MQTTBadParameter`. + */ +STATIC MQTTStatus_t prvMQTTInit( void ) { TransportInterface_t xTransport = { 0 }; MQTTStatus_t xReturn; @@ -465,7 +450,7 @@ static MQTTStatus_t prvMQTTInit( void ) return xReturn; } -static MQTTStatus_t prvHandleResubscribe( void ) +STATIC MQTTStatus_t prvHandleResubscribe( void ) { MQTTStatus_t xResult = MQTTBadParameter; uint32_t ulIndex = 0U; @@ -527,7 +512,16 @@ static MQTTStatus_t prvHandleResubscribe( void ) return xResult; } -static MQTTStatus_t prvMQTTConnect( void ) +/** + * @brief Sends an MQTT Connect packet over the already connected TCP socket. + * + * @param[in] pxMQTTContext MQTT context pointer. + * @param[in] xCleanSession If a clean session should be established. + * + * @return `MQTTSuccess` if connection succeeds, else appropriate error code + * from MQTT_Connect. + */ +STATIC MQTTStatus_t prvMQTTConnect( void ) { MQTTStatus_t xResult; bool xSessionPresent = false; @@ -588,7 +582,7 @@ static MQTTStatus_t prvMQTTConnect( void ) return xResult; } -static void prvDisconnectCommandCallback( MQTTAgentCommandContext_t * pxCommandContext, +STATIC void prvDisconnectCommandCallback( MQTTAgentCommandContext_t * pxCommandContext, MQTTAgentReturnInfo_t * pxReturnInfo ) { pxCommandContext->xReturnStatus = pxReturnInfo->returnCode; @@ -599,7 +593,11 @@ static void prvDisconnectCommandCallback( MQTTAgentCommandContext_t * pxCommandC } } -static void prvDisconnectFromMQTTBroker( void ) +/** + * @brief Disconnects from the MQTT broker. + * Initiates an MQTT disconnect and then teardown underlying TCP connection. + */ +STATIC void prvDisconnectFromMQTTBroker( void ) { static MQTTAgentCommandContext_t xCommandContext = { 0 }; static MQTTAgentCommandInfo_t xCommandParams = { 0 }; @@ -625,10 +623,21 @@ static void prvDisconnectFromMQTTBroker( void ) pdMS_TO_TICKS( MQTT_AGENT_MS_TO_WAIT_FOR_NOTIFICATION ) ); /* End TLS session, then close TCP connection. */ - prvSocketDisconnect( &xNetworkContextMqtt ); + if( prvSocketDisconnect( &xNetworkContextMqtt ) != pdPASS ) + { + LogError( ( "Failed to disconnect socket." ) ); + } } -static void prvMQTTAgentTask( void * pParam ) +/** + * @brief Task for MQTT agent. + * Task runs MQTT agent command loop, which returns only when the user disconnects + * MQTT, terminates agent, or the mqtt connection is broken. If the mqtt connection is broken, the task + * tries to reconnect to the broker. + * + * @param[in] pParam Can be used to pass down functionality to the agent task + */ +STATIC void prvMQTTAgentTask( void * pParam ) { BaseType_t xResult; MQTTStatus_t xMQTTStatus = MQTTSuccess; diff --git a/components/aws_iot/coremqtt_agent/integration/tests/CMakeLists.txt b/components/aws_iot/coremqtt_agent/integration/tests/CMakeLists.txt index 4325da7b..d53f8a3b 100644 --- a/components/aws_iot/coremqtt_agent/integration/tests/CMakeLists.txt +++ b/components/aws_iot/coremqtt_agent/integration/tests/CMakeLists.txt @@ -2,21 +2,52 @@ # # SPDX-License-Identifier: MIT +# Add application-specific mocks. +# E.g. app_config.h since it varies by application. +add_subdirectory(config_mocks) + add_executable(freertos-agent-message-test test_freertos_agent_message.cpp ../src/freertos_agent_message.c ) - target_include_directories(freertos-agent-message-test PRIVATE ../inc ) - target_link_libraries(freertos-agent-message-test PRIVATE fff coremqtt-agent-mock + coremqtt-agent-integration-mock + coremqtt-mock freertos-kernel-mock ) - iot_reference_arm_corstone3xx_add_test(freertos-agent-message-test) + + +add_executable(mqtt-agent-task-test + test_mqtt_agent_task.cpp + ../src/mqtt_agent_task.c +) +target_include_directories(mqtt-agent-task-test + PRIVATE + . + ../../library_mocks/inc + + ../inc +) +target_link_libraries(mqtt-agent-task-test + PRIVATE + fff + backoff-algorithm-mock + coremqtt-agent-integration-mock + coremqtt-agent-test-config-mocks + coremqtt-mock + freertos-kernel-mock + freertos-plus-tcp-integration-mock + helpers-events-mock + helpers-logging-mock + mbedtls-mock + trusted-firmware-m-mock +) +iot_reference_arm_corstone3xx_add_test(mqtt-agent-task-test) diff --git a/components/aws_iot/coremqtt_agent/integration/tests/config_mocks/CMakeLists.txt b/components/aws_iot/coremqtt_agent/integration/tests/config_mocks/CMakeLists.txt new file mode 100644 index 00000000..9afdbd80 --- /dev/null +++ b/components/aws_iot/coremqtt_agent/integration/tests/config_mocks/CMakeLists.txt @@ -0,0 +1,17 @@ +# Copyright 2024 Arm Limited and/or its affiliates +# +# SPDX-License-Identifier: MIT + +# Add helpers for testing mqtt_agent_task.c. Helpers are application-specific. +# E.g. app_config.h is a helper since it varies by application. +add_library(coremqtt-agent-test-config-mocks + INTERFACE +) +target_include_directories(coremqtt-agent-test-config-mocks + INTERFACE + inc +) +target_link_libraries(coremqtt-agent-test-config-mocks + INTERFACE + fff +) diff --git a/components/aws_iot/coremqtt_agent/integration/tests/config_mocks/inc/FreeRTOSConfig.h b/components/aws_iot/coremqtt_agent/integration/tests/config_mocks/inc/FreeRTOSConfig.h new file mode 100644 index 00000000..bcb73ba5 --- /dev/null +++ b/components/aws_iot/coremqtt_agent/integration/tests/config_mocks/inc/FreeRTOSConfig.h @@ -0,0 +1,35 @@ +/* + * FreeRTOS Kernel V10.4.1 + * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright (c) 2022-2024, Arm Limited and Contributors. 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. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +#ifndef FREERTOS_CONFIG_H +#define FREERTOS_CONFIG_H + +DECLARE_FAKE_VOID_FUNC( vAssertCalled, + const char *, + unsigned long ); +#define configASSERT( x ) if( ( x ) == 0 ) vAssertCalled( __FILE__, __LINE__ ); + +#endif /* FREERTOS_CONFIG_H */ diff --git a/components/aws_iot/coremqtt_agent/integration/tests/config_mocks/inc/app_config.h b/components/aws_iot/coremqtt_agent/integration/tests/config_mocks/inc/app_config.h new file mode 100644 index 00000000..e58503c1 --- /dev/null +++ b/components/aws_iot/coremqtt_agent/integration/tests/config_mocks/inc/app_config.h @@ -0,0 +1,12 @@ +/* Copyright 2023-2024 Arm Limited and/or its affiliates + * + * SPDX-License-Identifier: MIT + */ + +#ifndef APP_CONFIG_H +#define APP_CONFIG_H + +#define appCONFIG_MQTT_AGENT_TASK_STACK_SIZE 4096 +#define appCONFIG_MQTT_AGENT_TASK_PRIORITY 2 + +#endif diff --git a/components/aws_iot/coremqtt_agent/integration/tests/config_mocks/inc/core_mqtt_config.h b/components/aws_iot/coremqtt_agent/integration/tests/config_mocks/inc/core_mqtt_config.h new file mode 100644 index 00000000..f7ee6e72 --- /dev/null +++ b/components/aws_iot/coremqtt_agent/integration/tests/config_mocks/inc/core_mqtt_config.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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. + * + * http://www.FreeRTOS.org + * http://aws.amazon.com/freertos + * + */ + +#ifndef CORE_MQTT_CONFIG_H +#define CORE_MQTT_CONFIG_H + +/* There is another config file in coremqtt_agent/mocks/inc that takes precedence over this one. */ + +#define MQTT_AGENT_COMMAND_QUEUE_LENGTH ( 32 ) +#define MQTT_COMMAND_CONTEXTS_POOL_SIZE ( 32 ) + +#endif /* ifndef CORE_MQTT_CONFIG_H */ diff --git a/components/aws_iot/coremqtt_agent/integration/tests/config_mocks/inc/core_pkcs11_config.h b/components/aws_iot/coremqtt_agent/integration/tests/config_mocks/inc/core_pkcs11_config.h new file mode 100644 index 00000000..0d385d31 --- /dev/null +++ b/components/aws_iot/coremqtt_agent/integration/tests/config_mocks/inc/core_pkcs11_config.h @@ -0,0 +1,34 @@ +/* + * Amazon FreeRTOS V1.1.4 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright (c) 2022-2024, Arm Limited and Contributors. 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. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +#ifndef _AWS_PKCS11_CONFIG_H_ +#define _AWS_PKCS11_CONFIG_H_ + +#define pkcs11configLABEL_DEVICE_CERTIFICATE_FOR_TLS "Device Cert" +#define pkcs11configLABEL_DEVICE_PRIVATE_KEY_FOR_TLS "Device Priv TLS Key" +#define configPKCS11_DEFAULT_USER_PIN "0000" + +#endif /* _AWS_PKCS11_CONFIG_H_ include guard. */ diff --git a/components/aws_iot/coremqtt_agent/integration/tests/config_mocks/inc/demo_config.h b/components/aws_iot/coremqtt_agent/integration/tests/config_mocks/inc/demo_config.h new file mode 100644 index 00000000..8d60cfc3 --- /dev/null +++ b/components/aws_iot/coremqtt_agent/integration/tests/config_mocks/inc/demo_config.h @@ -0,0 +1,36 @@ +/* + * FreeRTOS V202010.00 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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. + * + * http://www.FreeRTOS.org + * http://aws.amazon.com/freertos + * + */ + +#ifndef DEMO_CONFIG_H +#define DEMO_CONFIG_H + +#define democonfigMQTT_BROKER_ENDPOINT "dummy endpoint" +#define democonfigMQTT_BROKER_PORT 8883 +#define democonfigCLIENT_IDENTIFIER "dummy client identifier" + +#endif /* DEMO_CONFIG_H */ diff --git a/components/aws_iot/coremqtt_agent/integration/tests/config_mocks/inc/iot_default_root_certificates.h b/components/aws_iot/coremqtt_agent/integration/tests/config_mocks/inc/iot_default_root_certificates.h new file mode 100644 index 00000000..6585c51e --- /dev/null +++ b/components/aws_iot/coremqtt_agent/integration/tests/config_mocks/inc/iot_default_root_certificates.h @@ -0,0 +1,34 @@ +/* + * FreeRTOS Common V1.2.0 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +#ifndef __DEFAULT__ROOT__CERTIFICATES__H__ +#define __DEFAULT__ROOT__CERTIFICATES__H__ + +static const char tlsATS1_ROOT_CERTIFICATE_PEM[] = "dummy"; +static const uint32_t tlsATS1_ROOT_CERTIFICATE_LENGTH = sizeof( tlsATS1_ROOT_CERTIFICATE_PEM ); + +#endif /* ifndef __DEFAULT__ROOT__CERTIFICATES__H__ */ diff --git a/components/aws_iot/coremqtt_agent/integration/tests/test_mqtt_agent_task.cpp b/components/aws_iot/coremqtt_agent/integration/tests/test_mqtt_agent_task.cpp new file mode 100644 index 00000000..00c94c44 --- /dev/null +++ b/components/aws_iot/coremqtt_agent/integration/tests/test_mqtt_agent_task.cpp @@ -0,0 +1,731 @@ +/* Copyright 2024 Arm Limited and/or its affiliates + * + * SPDX-License-Identifier: MIT + */ + +#include "fff.h" + +#include "gtest/gtest.h" + +#include + +using namespace std; + +extern "C" { + #include "FreeRTOSConfig.h" + #include "backoff_algorithm.h" + #include "core_mqtt_agent_message_interface.h" + #include "core_mqtt_serializer.h" + #include "event_groups.h" + #include "events.h" + #include "logging_stack.h" + #include "mqtt_agent_task.h" + #include "psa/crypto.h" + #include "psa/error.h" + #include "task.h" + #include "queue.h" + + /* + Exposed static functions to test + The below functions are found in `mqtt_agent_task.c`, which is + found by the inclusion of `mqtt_agent_task.h`. + */ + + extern void prvMQTTAgentTask( void * pParam ); + extern BaseType_t prvSocketConnect( NetworkContext_t * pNetworkContext ); + extern void prvDisconnectFromMQTTBroker( void ); + extern MQTTStatus_t prvMQTTInit( void ); + extern MQTTStatus_t prvMQTTConnect( void ); + extern uint32_t prvGetTimeMs( void ); + extern UBaseType_t prvGetRandomNumber( void ); + extern BaseType_t prvSocketConnect( NetworkContext_t * pxNetworkContext ); + extern BaseType_t prvSocketDisconnect( NetworkContext_t * pxNetworkContext ); + extern void prvIncomingPublishCallback( MQTTAgentContext_t * pMqttAgentContext, + uint16_t packetId, + MQTTPublishInfo_t * pxPublishInfo ); + extern void prvReSubscriptionCommandCallback( MQTTAgentCommandContext_t * pxCommandContext, + MQTTAgentReturnInfo_t * pxReturnInfo ); + extern MQTTStatus_t prvHandleResubscribe( void ); + extern MQTTStatus_t prvMQTTConnect( void ); + extern void prvDisconnectCommandCallback( MQTTAgentCommandContext_t * pxCommandContext, + MQTTAgentReturnInfo_t * pxReturnInfo ); + extern void prvDisconnectFromMQTTBroker( void ); + extern void prvMQTTAgentTask( void * pParam ); + + /* Directly copy-paste mock headers from the file under test's directory. + Otherwise, the non-mock files are detected. */ + + /* subscription_manager.h */ + #ifndef SUBSCRIPTION_MANAGER_H + #define SUBSCRIPTION_MANAGER_H + typedef struct SubscriptionElement { + int usFilterStringLength; + const char * pcSubscriptionFilterString; + } SubscriptionElement_t; + + #ifndef SUBSCRIPTION_MANAGER_MAX_SUBSCRIPTIONS + #define SUBSCRIPTION_MANAGER_MAX_SUBSCRIPTIONS 10U + #endif + + DECLARE_FAKE_VOID_FUNC( removeSubscription, + const char *, + uint16_t ); + + DECLARE_FAKE_VALUE_FUNC( bool, + handleIncomingPublishes, + MQTTPublishInfo_t * ); + + DEFINE_FAKE_VOID_FUNC( removeSubscription, + const char *, + uint16_t ); + + DEFINE_FAKE_VALUE_FUNC( bool, + handleIncomingPublishes, + MQTTPublishInfo_t * ); + + SubscriptionElement_t xGlobalSubscriptionList[ SUBSCRIPTION_MANAGER_MAX_SUBSCRIPTIONS ]; + #endif /* SUBSCRIPTION_MANAGER_H */ + + /* freertos_command_pool.h */ + #ifndef FREERTOS_COMMAND_POOL_H + #define FREERTOS_COMMAND_POOL_H + DECLARE_FAKE_VOID_FUNC( Agent_InitializePool ); + DECLARE_FAKE_VALUE_FUNC( MQTTAgentCommand_t *, + Agent_GetCommand, + uint32_t ); + DECLARE_FAKE_VALUE_FUNC( bool, + Agent_ReleaseCommand, + MQTTAgentCommand_t * ); + DEFINE_FAKE_VOID_FUNC( Agent_InitializePool ); + DEFINE_FAKE_VALUE_FUNC( MQTTAgentCommand_t *, + Agent_GetCommand, + uint32_t ); + DEFINE_FAKE_VALUE_FUNC( bool, + Agent_ReleaseCommand, + MQTTAgentCommand_t * ); + #endif /* FREERTOS_COMMAND_POOL_H */ + + /* Functions usually defined by main.c */ + DEFINE_FAKE_VOID_FUNC( vAssertCalled, + const char *, + unsigned long ); + DEFINE_FAKE_VOID_FUNC_VARARG( SdkLogError, + const char *, + ... ); + DEFINE_FAKE_VOID_FUNC_VARARG( SdkLogWarn, + const char *, + ... ); + DEFINE_FAKE_VOID_FUNC_VARARG( SdkLogInfo, + const char *, + ... ); + DEFINE_FAKE_VOID_FUNC_VARARG( SdkLogDebug, + const char *, + ... ); + +} + +DEFINE_FFF_GLOBALS + +#define ASSERTION_FAIL int + +/* Mock for vAssertCalled */ +void throw_assertion_failure ( const char * pcFile, + unsigned long ulLine ) { + throw (1); + /* + Behaviour wanted: + - Encounters assertion fail, stops running any more code. E.g. does not go to next line. + - But checks all assertions in the google test program hold. + */ +} + +/* Being under this test class denotes a valid test that needs a corresponding fix, but we do not want clogging the testsuite. */ +class SkipTest : public ::testing::Test { + protected: + void SetUp() override { + GTEST_SKIP() << "Skipping all tests under this suite"; + } +}; + +class TestMqttAgentTask : public ::testing::Test { +public: + TestMqttAgentTask() + { + RESET_FAKE(Agent_InitializePool); + RESET_FAKE(BackoffAlgorithm_InitializeParams); + RESET_FAKE(BackoffAlgorithm_GetNextBackoff); + RESET_FAKE(handleIncomingPublishes); + RESET_FAKE(MQTTAgent_CancelAll); + RESET_FAKE(MQTTAgent_CommandLoop); + RESET_FAKE(MQTTAgent_Init); + RESET_FAKE(MQTTAgent_ResumeSession) + RESET_FAKE(MQTTAgent_Subscribe); + RESET_FAKE(MQTT_Connect); + RESET_FAKE(MQTT_Status_strerror); + RESET_FAKE(psa_generate_random); + RESET_FAKE(SdkLogError); + RESET_FAKE(SdkLogInfo); + RESET_FAKE(SdkLogWarn); + RESET_FAKE(Transport_Disconnect); + RESET_FAKE(Transport_Connect); + RESET_FAKE(vAssertCalled); + RESET_FAKE(vTaskDelay); + RESET_FAKE(vTaskDelete); + RESET_FAKE(vWaitUntilNetworkIsUp); + RESET_FAKE(xEventGroupClearBits); + RESET_FAKE(xEventGroupSetBits); + RESET_FAKE(xTaskCreate); + RESET_FAKE(xTaskGetCurrentTaskHandle); + RESET_FAKE(xTaskGetTickCount); + RESET_FAKE(xTaskNotifyWait); + RESET_FAKE(xQueueCreateStatic); + + // Wrap functions expected to fail an assertion in EXPECT_THROW from GoogleTest. + vAssertCalled_fake.custom_fake = throw_assertion_failure; + } +}; + +/* Test helper functions */ +void expect_no_errors ( void ) { + ASSERT_EQ(SdkLogError_fake.call_count, 0); + ASSERT_EQ(vAssertCalled_fake.call_count, 0); +} +void expect_errors ( void ) { + ASSERT_NE(SdkLogError_fake.call_count + vAssertCalled_fake.call_count, 0) << "Expected an error reported by LogError or an assertion failure."; +} +// expect throws +void expect_errors_or_warnings ( void ) { + ASSERT_NE(SdkLogError_fake.call_count + SdkLogWarn_fake.call_count + vAssertCalled_fake.call_count, 0) << "Expected an error reported by LogError, LogWarn or an assertion failure."; +} +void expect_no_errors_or_warnings ( void ) { + ASSERT_EQ(SdkLogError_fake.call_count + SdkLogWarn_fake.call_count + vAssertCalled_fake.call_count, 0); +} +/* Custom fake for xEventGroupClearBits */ +int expect_clearing_MQTT_event_mask( void * unused, + const int mask ) { + EXPECT_EQ (mask, EVENT_MASK_MQTT_CONNECTED); + return 1; +} +int expect_mqtt_connected_event_mask (void * unused, + const int mask) { + EXPECT_EQ(mask, EVENT_MASK_MQTT_CONNECTED); + return 1; +} +/* Custom fake for xTaskGetTickCount() */ +TickType_t sharedCounter = 0; +TickType_t increment_shared_counter_and_return( void ) { + sharedCounter = sharedCounter + 1; + return sharedCounter; +} +/* Custom fake for psa_generate_random */ +psa_status_t set_random_variable_to_three_and_return_success (uint8_t * randomVar, size_t outputSize) { + *randomVar = 3; + return PSA_SUCCESS; +} +/* Custom fakes for xTaskNotify */ +BaseType_t return_pdpass_and_expect_task_handle_points_to_five (TaskHandle_t handle, uint32_t returnCode, eNotifyAction unused) { + EXPECT_EQ(*handle, 5); + return pdPASS; +} +BaseType_t return_pdpass_and_expect_mqtt_success_return_code (TaskHandle_t handle, uint32_t returnCode, eNotifyAction unused) { + EXPECT_EQ(returnCode, MQTTSuccess); + return pdPASS; +} +BaseType_t return_pdpass_and_expect_mqtt_bad_parameter_return_code (TaskHandle_t handle, uint32_t returnCode, eNotifyAction unused) { + EXPECT_EQ(returnCode, MQTTBadParameter); + return pdPASS; +} +/* Custom fake for Transport_Connect */ +TransportStatus_t check_if_timeout_less_than_ten_seconds ( + NetworkContext_t * pNetworkContext, + const ServerInfo_t * pServerInfo, + const TLSParams_t * pTLSParams, + uint32_t sendTimeoutMs, + uint32_t recvTimeoutMs ) { + uint32_t MAX_TIMEOUT_IN_MS = 10000; + EXPECT_LE(sendTimeoutMs, MAX_TIMEOUT_IN_MS); + EXPECT_LE(recvTimeoutMs, MAX_TIMEOUT_IN_MS); + return TRANSPORT_STATUS_SUCCESS; +} + +/* The file under test contains static functions which the tests in this file assume are made visible +by conditional compiling macros. This test verifies these macros are defined. */ +TEST_F(TestMqttAgentTask, Can_test_static_functions) { + #ifndef UNIT_TESTING + FAIL() << "The macro UNIT_TESTING is not defined, please add this to your CMake compile definitions."; + #endif /* UNIT_TESTING */ +} + +/* Testing vStartMqttAgentTask */ + +TEST_F(TestMqttAgentTask, Starting_the_agent_creates_a_task) +{ + ASSERT_EQ(xTaskCreate_fake.call_count, 0); + vStartMqttAgentTask (); + ASSERT_NE(xTaskCreate_fake.call_count, 0); + expect_no_errors(); +} + +/* Testing prvSocketConnect */ + +TEST_F(TestMqttAgentTask, Socket_connect_tries_to_make_a_connection) { + EXPECT_EQ(Transport_Connect_fake.call_count, 0); + prvSocketConnect( 0 ); + EXPECT_NE(Transport_Connect_fake.call_count, 0); +} +TEST_F(TestMqttAgentTask, Socket_connect_returns_success_on_successful_connection) { + Transport_Connect_fake.return_val = TRANSPORT_STATUS_SUCCESS; + EXPECT_EQ(prvSocketConnect ( 0 ), pdPASS); + expect_no_errors(); +} +TEST_F(TestMqttAgentTask, Socket_connect_does_not_error_on_unsuccessful_connection) { + Transport_Connect_fake.return_val = TRANSPORT_STATUS_CONNECT_FAILURE; + prvSocketConnect ( 0 ); + expect_no_errors(); +} +TEST_F(TestMqttAgentTask, Socket_connection_reattempt_does_not_continue_past_reasonable_time) { + Transport_Connect_fake.custom_fake = check_if_timeout_less_than_ten_seconds; + prvSocketConnect ( 0 ); + expect_no_errors(); +} +TEST_F(TestMqttAgentTask, Socket_connect_returns_failure_on_unsuccessful_connection) { + Transport_Connect_fake.return_val = TRANSPORT_STATUS_CONNECT_FAILURE; + EXPECT_EQ(prvSocketConnect ( 0 ), pdFALSE); + expect_no_errors(); +} + +/* Testing prvSocketDisconnect */ + +TEST_F(TestMqttAgentTask, Socket_disconnect_tries_to_close_a_connection) { + Transport_Disconnect_fake.return_val = TRANSPORT_STATUS_SUCCESS; + xEventGroupClearBits_fake.return_val = 1; + int dummy = 1; + EXPECT_EQ(Transport_Disconnect_fake.call_count, 0); + prvSocketDisconnect( &dummy ); + EXPECT_NE(Transport_Disconnect_fake.call_count, 0); +} +TEST_F(TestMqttAgentTask, Socket_disconnect_returns_success_when_disconnecting_succeeds) { + Transport_Disconnect_fake.return_val = TRANSPORT_STATUS_SUCCESS; + xEventGroupClearBits_fake.return_val = 1; + int dummy = 1; + EXPECT_EQ(prvSocketDisconnect( &dummy ), pdPASS); +} +TEST_F(TestMqttAgentTask, Socket_disconnect_returns_failure_when_disconnecting_fails) { + Transport_Disconnect_fake.return_val = TRANSPORT_STATUS_INVALID_PARAMETER; + xEventGroupClearBits_fake.return_val = 1; + int dummy = 1; + EXPECT_EQ(prvSocketDisconnect( &dummy ), pdFAIL); +} +TEST_F(TestMqttAgentTask, Socket_disconnect_informs_system_there_is_no_MQTT_connection_on_connection_closure) { + Transport_Disconnect_fake.return_val = TRANSPORT_STATUS_SUCCESS; + xEventGroupClearBits_fake.custom_fake = expect_clearing_MQTT_event_mask; + EXPECT_EQ(xEventGroupClearBits_fake.call_count, 0); + int dummy = 1; + prvSocketDisconnect( &dummy ); + EXPECT_NE(xEventGroupClearBits_fake.call_count, 0); +} +TEST_F(TestMqttAgentTask, Socket_disconnect_does_not_inform_system_there_is_no_MQTT_connection_if_connection_not_closed) { + Transport_Disconnect_fake.return_val = TRANSPORT_STATUS_INVALID_PARAMETER; + xEventGroupClearBits_fake.custom_fake = expect_clearing_MQTT_event_mask; + EXPECT_EQ(xEventGroupClearBits_fake.call_count, 0); + int dummy = 1; + prvSocketDisconnect( &dummy ); + EXPECT_EQ(xEventGroupClearBits_fake.call_count, 0); +} + +/* Testing prvDisconnectFromMQTTBroker */ + +TEST_F(TestMqttAgentTask, Disconnect_from_broker_tries_to_disconnect_from_MQTT_broker) { + int dummy = 1; + xTaskGetCurrentTaskHandle_fake.return_val = &dummy; + xTaskNotifyWait_fake.return_val = 1; + Transport_Disconnect_fake.return_val = TRANSPORT_STATUS_SUCCESS; + xEventGroupClearBits_fake.return_val = 1; + EXPECT_EQ(MQTTAgent_Disconnect_fake.call_count, 0); + prvDisconnectFromMQTTBroker(); + EXPECT_NE(MQTTAgent_Disconnect_fake.call_count, 0); +} +TEST_F(TestMqttAgentTask, Disconnect_from_broker_tries_to_close_TCP_connection_if_successful) { + int dummy = 1; + xTaskGetCurrentTaskHandle_fake.return_val = &dummy; + xTaskNotifyWait_fake.return_val = 1; + Transport_Disconnect_fake.return_val = TRANSPORT_STATUS_SUCCESS; + xEventGroupClearBits_fake.return_val = 1; + // Successful mqtt closure. + MQTTAgent_Disconnect_fake.return_val = MQTTSuccess; + EXPECT_EQ(Transport_Disconnect_fake.call_count, 0); + prvDisconnectFromMQTTBroker(); + EXPECT_NE(Transport_Disconnect_fake.call_count, 0); +} +TEST_F(TestMqttAgentTask, Disconnect_from_broker_errors_on_failure_to_close_TCP_connection) { + int dummy = 1; + xTaskGetCurrentTaskHandle_fake.return_val = &dummy; + xTaskNotifyWait_fake.return_val = 1; + xEventGroupClearBits_fake.return_val = 1; + // Fails to close mqtt connection. + MQTTAgent_Disconnect_fake.return_val = MQTTSuccess; + Transport_Disconnect_fake.return_val = TRANSPORT_STATUS_INVALID_PARAMETER; + prvDisconnectFromMQTTBroker(); + expect_errors(); +} +TEST_F(TestMqttAgentTask, Waits_for_MQTT_connection_to_close_before_trying_to_close_TCP_connection) { + int dummy = 1; + xTaskGetCurrentTaskHandle_fake.return_val = &dummy; + xTaskNotifyWait_fake.return_val = 1; + xEventGroupClearBits_fake.return_val = 1; + // Closes mqtt connection but fails for TCP. + MQTTAgent_Disconnect_fake.return_val = MQTTSuccess; + Transport_Disconnect_fake.return_val = TRANSPORT_STATUS_SUCCESS; + EXPECT_EQ(xTaskNotifyWait_fake.call_count, 0); + prvDisconnectFromMQTTBroker(); + EXPECT_NE(xTaskNotifyWait_fake.call_count, 0); + + // Check this is not the case if MQTT closure fails. + RESET_FAKE(xTaskNotifyWait); + MQTTAgent_Disconnect_fake.return_val = MQTTBadParameter; + EXPECT_EQ(xTaskNotifyWait_fake.call_count, 0) << "Failed to reset fake."; + EXPECT_THROW(prvDisconnectFromMQTTBroker(); + EXPECT_EQ(xTaskNotifyWait_fake.call_count, 0), ASSERTION_FAIL); +} +TEST_F(TestMqttAgentTask, Disconnect_from_broker_errors_on_failure_to_close_MQTT_connection) { + int dummy = 1; + xTaskGetCurrentTaskHandle_fake.return_val = &dummy; + xTaskNotifyWait_fake.return_val = 1; + Transport_Disconnect_fake.return_val = TRANSPORT_STATUS_SUCCESS; + xEventGroupClearBits_fake.return_val = 1; + // Fails to close mqtt connection. + MQTTAgent_Disconnect_fake.return_val = MQTTBadParameter; + Transport_Disconnect_fake.return_val = TRANSPORT_STATUS_SUCCESS; + EXPECT_THROW(prvDisconnectFromMQTTBroker(), ASSERTION_FAIL); + expect_errors(); +} + +/* Testing prvMQTTConnect */ + +class TestMqttAgentTaskConnect : public TestMqttAgentTask { +public: + TestMqttAgentTaskConnect() + { + /* The below may be overwritten within individual tests */ + MQTTAgent_CancelAll_fake.return_val = MQTTSuccess; + MQTT_Connect_fake.return_val = MQTTSuccess; + MQTTAgent_ResumeSession_fake.return_val = MQTTSuccess; + xEventGroupSetBits_fake.return_val = 1; + /* Make prvHandleResubscribe produce a defined output */ + int dummy = 1; + xTaskGetCurrentTaskHandle_fake.return_val = &dummy; + xTaskNotifyWait_fake.return_val = 1; + Transport_Disconnect_fake.return_val = TRANSPORT_STATUS_SUCCESS; + xEventGroupClearBits_fake.return_val = 1; + MQTTAgent_Disconnect_fake.return_val = MQTTSuccess; + } +}; +TEST_F(TestMqttAgentTaskConnect, MQTT_connect_tries_to_create_a_connection) { + EXPECT_EQ(MQTT_Connect_fake.call_count, 0); + prvMQTTConnect(); + EXPECT_NE(MQTT_Connect_fake.call_count, 0); +} +TEST_F(TestMqttAgentTaskConnect, MQTT_connect_returns_failure_if_connection_fails) { + MQTT_Connect_fake.return_val = MQTTBadParameter; + EXPECT_NE(prvMQTTConnect(), MQTTSuccess); +} +TEST_F(TestMqttAgentTaskConnect, MQTT_connect_returns_success_if_connection_succeeds) { + MQTT_Connect_fake.return_val = MQTTSuccess; + EXPECT_EQ(prvMQTTConnect(), MQTTSuccess); +} +// This test would be ideal, but is not possible to write neatly for the file. +// TEST_F(TestMqttAgentTask, MQTT_connect_updates_system_flags_when_creating_a_new_connection) { +// EXPECT_EQ(xEventGroupSetBits_fake.call_count, 0); +// prvMQTTConnect(); +// EXPECT_NE(xEventGroupSetBits_fake.call_count, 0); +// } +TEST_F(TestMqttAgentTaskConnect, MQTT_connect_does_not_set_incorrect_system_flags_when_creating_a_new_connection) { + xEventGroupSetBits_fake.custom_fake = expect_mqtt_connected_event_mask; + prvMQTTConnect(); +} + +/* Testing prvGetTimeMs */ + +/* Testing prvGetRandomNumber */ + +TEST_F(TestMqttAgentTask, Random_generation_calls_random_library_function) { + psa_generate_random_fake.return_val = PSA_SUCCESS; + xTaskGetTickCount_fake.return_val = 3; + EXPECT_EQ(psa_generate_random_fake.call_count, 0); + prvGetRandomNumber(); + EXPECT_NE(psa_generate_random_fake.call_count, 0); +} +TEST_F(TestMqttAgentTask, Random_generation_does_not_error_if_random_library_function_succeeds) { + psa_generate_random_fake.return_val = PSA_SUCCESS; + xTaskGetTickCount_fake.return_val = 3; + prvGetRandomNumber(); + expect_no_errors(); +} +TEST_F(TestMqttAgentTask, Random_generation_reports_library_failure) { + psa_generate_random_fake.return_val = PSA_ERROR_PROGRAMMER_ERROR; + xTaskGetTickCount_fake.return_val = 3; + prvGetRandomNumber(); + expect_errors_or_warnings(); +} +TEST_F(TestMqttAgentTask, Random_generation_gives_same_output_for_same_value_given_by_random_library) { + // Randomizing outputs should be handled by correct library functions only. + xTaskGetTickCount_fake.custom_fake = increment_shared_counter_and_return; + psa_generate_random_fake.custom_fake = set_random_variable_to_three_and_return_success; + UBaseType_t expected = prvGetRandomNumber(); + for (int count=0; count < 5; count++) { + EXPECT_EQ(expected, prvGetRandomNumber()); + } +} + +/* Testing prvIncomingPublishCallback */ + +TEST_F(TestMqttAgentTask, Publish_callback_calls_publish_handler) { + bool handled = true; + handleIncomingPublishes_fake.return_val = handled; + + EXPECT_EQ(handleIncomingPublishes_fake.call_count, 0); + int dummy = 5; + MQTTAgentContext_t mqttAgentContext = { &dummy }; + uint16_t dummyId = 10; + MQTTPublishInfo_t xPublishInfo; + xPublishInfo.qos = MQTTQoS0; + xPublishInfo.retain = true; + xPublishInfo.dup = true; + xPublishInfo.pTopicName = "dummy"; + xPublishInfo.topicNameLength = 6; + xPublishInfo.pPayload = &dummy; + xPublishInfo.payloadLength = 1; + prvIncomingPublishCallback(&mqttAgentContext, dummyId, &xPublishInfo); + EXPECT_NE(handleIncomingPublishes_fake.call_count, 0); +} +TEST_F(TestMqttAgentTask, Publish_callback_does_not_error_if_publish_handled) { + bool handled = true; + handleIncomingPublishes_fake.return_val = handled; + + int dummy = 5; + MQTTAgentContext_t mqttAgentContext = { &dummy }; + uint16_t dummyId = 10; + MQTTPublishInfo_t xPublishInfo; + xPublishInfo.qos = MQTTQoS0; + xPublishInfo.retain = true; + xPublishInfo.dup = true; + xPublishInfo.pTopicName = "dummy"; + xPublishInfo.topicNameLength = 6; + xPublishInfo.pPayload = &dummy; + xPublishInfo.payloadLength = 1; + prvIncomingPublishCallback(&mqttAgentContext, dummyId, &xPublishInfo); + expect_no_errors_or_warnings(); +} +TEST_F(TestMqttAgentTask, Publish_callback_generates_log_if_handler_fails) { + bool handled = false; + handleIncomingPublishes_fake.return_val = handled; + + int dummy = 5; + MQTTAgentContext_t mqttAgentContext = { &dummy }; + uint16_t dummyId = 10; + MQTTPublishInfo_t xPublishInfo; + xPublishInfo.qos = MQTTQoS0; + xPublishInfo.retain = true; + xPublishInfo.dup = true; + xPublishInfo.pTopicName = "dummy"; + xPublishInfo.topicNameLength = 6; + xPublishInfo.pPayload = &dummy; + xPublishInfo.payloadLength = 1; + prvIncomingPublishCallback(&mqttAgentContext, dummyId, &xPublishInfo); + expect_errors_or_warnings(); +} + +/* Testing prvReSubscriptionCommandCallback */ + +TEST_F(TestMqttAgentTask, Command_callback_does_not_error_if_given_MQTT_success) { + // All topic filters are already in subscription list. + MQTTAgentCommandContext_t pxCommandContext = {}; + uint8_t subackCodes[] ={1,2,3}; + MQTTAgentReturnInfo_t returnInfo = {MQTTSuccess, subackCodes}; + prvReSubscriptionCommandCallback(&pxCommandContext, &returnInfo); + expect_no_errors(); +} + +/* Testing prvMQTTInit */ + +TEST_F(TestMqttAgentTask, MQTT_init_tries_to_create_a_command_queue) { + QueueDefinition queue = {10}; + xQueueCreateStatic_fake.return_val = &queue; + MQTTAgent_Init_fake.return_val = MQTTSuccess; + xEventGroupSetBits_fake.return_val = 1; + + EXPECT_EQ(xQueueCreateStatic_fake.call_count, 0); + prvMQTTInit(); + EXPECT_NE(xQueueCreateStatic_fake.call_count, 0); +} +TEST_F(TestMqttAgentTask, MQTT_init_errors_if_command_queue_creation_returns_nullptr) { + // E.g. happens if out of memory. + xQueueCreateStatic_fake.return_val = nullptr; + MQTTAgent_Init_fake.return_val = MQTTSuccess; + xEventGroupSetBits_fake.return_val = 1; + + EXPECT_EQ(xQueueCreateStatic_fake.call_count, 0); + EXPECT_THROW(prvMQTTInit(), ASSERTION_FAIL); + EXPECT_NE(xQueueCreateStatic_fake.call_count, 0); +} +TEST_F(TestMqttAgentTask, MQTT_init_tries_to_initialise_MQTT_library) { + QueueDefinition queue = {10}; + xQueueCreateStatic_fake.return_val = &queue; + MQTTAgent_Init_fake.return_val = MQTTSuccess; + xEventGroupSetBits_fake.return_val = 1; + + EXPECT_EQ(MQTTAgent_Init_fake.call_count, 0); + prvMQTTInit(); + EXPECT_NE(MQTTAgent_Init_fake.call_count, 0); +} +TEST_F(TestMqttAgentTask, MQTT_init_errors_if_cannot_initialise_MQTT_library) { + QueueDefinition queue = {10}; + xQueueCreateStatic_fake.return_val = &queue; + MQTTAgent_Init_fake.return_val = MQTTBadParameter; + xEventGroupSetBits_fake.return_val = 1; + + prvMQTTInit(); + expect_errors(); +} +TEST_F(TestMqttAgentTask, MQTT_init_returns_success_if_successful) { + QueueDefinition queue = {10}; + xQueueCreateStatic_fake.return_val = &queue; + MQTTAgent_Init_fake.return_val = MQTTSuccess; + xEventGroupSetBits_fake.return_val = 1; + + EXPECT_EQ(prvMQTTInit(), MQTTSuccess); +} +TEST_F(TestMqttAgentTask, MQTT_init_sets_system_MQTT_init_event_flag_if_successful) { + QueueDefinition queue = {10}; + xQueueCreateStatic_fake.return_val = &queue; + MQTTAgent_Init_fake.return_val = MQTTSuccess; + xEventGroupSetBits_fake.return_val = 1; + + EXPECT_EQ(xEventGroupSetBits_fake.call_count, 0); + prvMQTTInit(); + EXPECT_NE(xEventGroupSetBits_fake.call_count, 0); +} + +/* Testing prvHandleResubscribe */ +TEST_F(TestMqttAgentTask, Resubscribe_returns_success_if_subscription_succeeds) { + MQTTAgent_Subscribe_fake.return_val = MQTTSuccess; + MQTT_Status_strerror_fake.return_val = "dummy"; + EXPECT_EQ(prvHandleResubscribe(), MQTTSuccess); +} + +/* Testing prvDisconnectCommandCallback */ + +TEST_F(TestMqttAgentTask, Callback_for_MQTT_disconnect_tries_to_notify_task_waiting_on_MQTT_disconnection) { + xTaskNotify_fake.return_val = pdPASS; + + int dummyTask = 5; + MQTTAgentCommandContext_t xCommandContext; + xCommandContext.xTaskToNotify = &dummyTask; + MQTTAgentReturnInfo_t xReturnInfo; + xReturnInfo.returnCode = MQTTSuccess; + + EXPECT_EQ(xTaskNotify_fake.call_count, 0); + prvDisconnectCommandCallback(&xCommandContext, &xReturnInfo); + EXPECT_NE(xTaskNotify_fake.call_count, 0); +} +TEST_F(TestMqttAgentTask, Callback_for_MQTT_disconnect_does_not_notify_if_no_tasks_are_waiting) { + xTaskNotify_fake.return_val = pdPASS; + + MQTTAgentCommandContext_t xCommandContext; + xCommandContext.xTaskToNotify = nullptr; + MQTTAgentReturnInfo_t xReturnInfo; + xReturnInfo.returnCode = MQTTSuccess; + + EXPECT_EQ(xTaskNotify_fake.call_count, 0); + prvDisconnectCommandCallback(&xCommandContext, &xReturnInfo); + EXPECT_EQ(xTaskNotify_fake.call_count, 0); +} +TEST_F(TestMqttAgentTask, Callback_for_MQTT_disconnect_notifies_tasks_with_correct_MQTT_status_return_code) { + xTaskNotify_fake.custom_fake = return_pdpass_and_expect_mqtt_success_return_code; + + int dummyTask = 5; + MQTTAgentCommandContext_t xCommandContext; + xCommandContext.xTaskToNotify = &dummyTask; + MQTTAgentReturnInfo_t xReturnInfo; + xReturnInfo.returnCode = MQTTBadParameter; + + xTaskNotify_fake.custom_fake = return_pdpass_and_expect_mqtt_bad_parameter_return_code; + EXPECT_EQ(xTaskNotify_fake.call_count, 0); + prvDisconnectCommandCallback(&xCommandContext, &xReturnInfo); + EXPECT_NE(xTaskNotify_fake.call_count, 0); + + xReturnInfo.returnCode = MQTTBadParameter; + prvDisconnectCommandCallback(&xCommandContext, &xReturnInfo); +} +TEST_F(TestMqttAgentTask, Callback_for_MQTT_disconnect_notifies_the_correct_task) { + xTaskNotify_fake.custom_fake = return_pdpass_and_expect_task_handle_points_to_five; + + int dummyTask = 5; + MQTTAgentCommandContext_t xCommandContext; + xCommandContext.xTaskToNotify = &dummyTask; + MQTTAgentReturnInfo_t xReturnInfo; + xReturnInfo.returnCode = MQTTSuccess; + + EXPECT_EQ(xTaskNotify_fake.call_count, 0); + prvDisconnectCommandCallback(&xCommandContext, &xReturnInfo); + EXPECT_NE(xTaskNotify_fake.call_count, 0); +} + +/* Testing prvMQTTAgentTask */ + +class TestMqttAgentTaskMainFunction : public TestMqttAgentTask { +public: + TestMqttAgentTaskMainFunction() + { + /* So that MQTT can initialise */ + QueueDefinition queue = {10}; + xQueueCreateStatic_fake.return_val = &queue; + MQTTAgent_Init_fake.return_val = MQTTSuccess; + xEventGroupSetBits_fake.return_val = 1; + /* So socket can connect */ + Transport_Connect_fake.return_val = TRANSPORT_STATUS_SUCCESS; + /* So random number generation succeeds */ + psa_generate_random_fake.return_val = PSA_SUCCESS; + xTaskGetTickCount_fake.return_val = 3; + /* So MQTT connection succeedds */ + MQTTAgent_CancelAll_fake.return_val = MQTTSuccess; + MQTT_Connect_fake.return_val = MQTTSuccess; + MQTTAgent_ResumeSession_fake.return_val = MQTTSuccess; + int dummy = 1; + xTaskGetCurrentTaskHandle_fake.return_val = &dummy; + xTaskNotifyWait_fake.return_val = 1; + xEventGroupClearBits_fake.return_val = 1; + MQTTAgent_Disconnect_fake.return_val = MQTTSuccess; + /* Main command loop does not usually terminate, but for these tests to terminate it needs to exit */ + MQTTAgent_CommandLoop_fake.return_val = MQTTSuccess; + /* For the main function to have defined output. */ + BackoffAlgorithm_GetNextBackoff_fake.return_val = BackoffAlgorithmSuccess; + } +}; + +TEST_F(TestMqttAgentTaskMainFunction, Agent_task_waits_for_network_to_start) { + EXPECT_EQ(vWaitUntilNetworkIsUp_fake.call_count, 0); + prvMQTTAgentTask ( nullptr ); + EXPECT_NE(vWaitUntilNetworkIsUp_fake.call_count, 0); +} +TEST_F(TestMqttAgentTaskMainFunction, Agent_task_initialises_MQTT_library) { + EXPECT_EQ(MQTTAgent_Init_fake.call_count, 0); + prvMQTTAgentTask ( nullptr ); + EXPECT_NE(MQTTAgent_Init_fake.call_count, 0); +} +TEST_F(TestMqttAgentTaskMainFunction, Agent_task_cannot_continue_if_mqtt_initialisation_fails) { + /* MQTT needs a command queue and the agent to be intialised */ + QueueDefinition queue = {10}; + xQueueCreateStatic_fake.return_val = &queue; + MQTTAgent_Init_fake.return_val = MQTTBadParameter; + xEventGroupSetBits_fake.return_val = 1; + + MQTTAgent_Init_fake.return_val = MQTTBadParameter; + + EXPECT_THROW(prvMQTTAgentTask( nullptr ), ASSERTION_FAIL); +} +TEST_F(TestMqttAgentTaskMainFunction, Agent_task_initialises_MQTT_pool) { + EXPECT_EQ(Agent_InitializePool_fake.call_count, 0); + prvMQTTAgentTask ( nullptr ); + EXPECT_NE(Agent_InitializePool_fake.call_count, 0); +} diff --git a/components/aws_iot/coremqtt_agent/mocks/CMakeLists.txt b/components/aws_iot/coremqtt_agent/library_mocks/CMakeLists.txt similarity index 69% rename from components/aws_iot/coremqtt_agent/mocks/CMakeLists.txt rename to components/aws_iot/coremqtt_agent/library_mocks/CMakeLists.txt index 5f43b093..251378b4 100644 --- a/components/aws_iot/coremqtt_agent/mocks/CMakeLists.txt +++ b/components/aws_iot/coremqtt_agent/library_mocks/CMakeLists.txt @@ -2,15 +2,18 @@ # # SPDX-License-Identifier: MIT -add_library(coremqtt-agent-mock INTERFACE) +add_library(coremqtt-agent-mock + src/core_mqtt_agent.c +) target_include_directories(coremqtt-agent-mock - INTERFACE + PUBLIC inc ) target_link_libraries(coremqtt-agent-mock - INTERFACE + PRIVATE fff coremqtt-mock + freertos-kernel-mock ) diff --git a/components/aws_iot/coremqtt_agent/library_mocks/inc/core_mqtt_agent.h b/components/aws_iot/coremqtt_agent/library_mocks/inc/core_mqtt_agent.h new file mode 100644 index 00000000..dda0dfeb --- /dev/null +++ b/components/aws_iot/coremqtt_agent/library_mocks/inc/core_mqtt_agent.h @@ -0,0 +1,105 @@ +/* + * coreMQTT Agent v1.2.0 + * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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 CORE_MQTT_AGENT_H +#define CORE_MQTT_AGENT_H + +#include +#include + +#include "core_mqtt.h" +#include "core_mqtt_agent_message_interface.h" +#include "fff.h" +#include "transport_interface.h" + +struct MQTTAgentCommand +{ + void * pArgs; +}; +typedef struct MQTTAgentCommand MQTTAgentCommand_t; + +typedef struct MQTTAgentContext +{ + void * mqttContext; +} MQTTAgentContext_t; + +typedef struct MQTTAgentCommandContext MQTTAgentCommandContext_t; + +typedef struct MQTTAgentReturnInfo +{ + MQTTStatus_t returnCode; + uint8_t * pSubackCodes; +} MQTTAgentReturnInfo_t; + +typedef struct MQTTAgentSubscribeArgs +{ + MQTTSubscribeInfo_t * pSubscribeInfo; + size_t numSubscriptions; +} MQTTAgentSubscribeArgs_t; + +typedef struct MQTTAgentCommandInfo +{ + void * cmdCompleteCallback; + void * pCmdCompleteCallbackContext; + int blockTimeMs; +} MQTTAgentCommandInfo_t; + +typedef void (* MQTTAgentIncomingPublishCallback_t )( MQTTAgentContext_t * pMqttAgentContext, + uint16_t packetId, + MQTTPublishInfo_t * pPublishInfo ); + +DECLARE_FAKE_VALUE_FUNC( MQTTStatus_t, + MQTTAgent_CommandLoop, + MQTTAgentContext_t * ); + +DECLARE_FAKE_VALUE_FUNC( MQTTStatus_t, + MQTTAgent_ResumeSession, + MQTTAgentContext_t *, + bool ); +DECLARE_FAKE_VALUE_FUNC( MQTTStatus_t, + MQTTAgent_Disconnect, + const MQTTAgentContext_t *, + const MQTTAgentCommandInfo_t * ); + +DECLARE_FAKE_VALUE_FUNC( MQTTStatus_t, + MQTTAgent_Init, + MQTTAgentContext_t *, + const MQTTAgentMessageInterface_t *, + const MQTTFixedBuffer_t *, + const TransportInterface_t *, + MQTTGetCurrentTimeFunc_t, + MQTTAgentIncomingPublishCallback_t, + int * ) + +DECLARE_FAKE_VALUE_FUNC( MQTTStatus_t, + MQTTAgent_Subscribe, + const MQTTAgentContext_t *, + MQTTAgentSubscribeArgs_t *, + const MQTTAgentCommandInfo_t * ); + +DECLARE_FAKE_VALUE_FUNC( MQTTStatus_t, + MQTTAgent_CancelAll, + MQTTAgentContext_t * ); + +#endif /* CORE_MQTT_AGENT_H */ diff --git a/components/aws_iot/coremqtt_agent/library_mocks/inc/core_mqtt_agent_message_interface.h b/components/aws_iot/coremqtt_agent/library_mocks/inc/core_mqtt_agent_message_interface.h new file mode 100644 index 00000000..fc5afb09 --- /dev/null +++ b/components/aws_iot/coremqtt_agent/library_mocks/inc/core_mqtt_agent_message_interface.h @@ -0,0 +1,61 @@ +/* + * coreMQTT Agent v1.2.0 + * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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 CORE_MQTT_AGENT_MESSAGE_INTERFACE_H +#define CORE_MQTT_AGENT_MESSAGE_INTERFACE_H + +#include "fff.h" + +#include +#include + +/* Note: do not define MQTTAgentMessageContext explicitly. + * It is sometimes user-defined. E.g. a .h file outside of this coremqtt_agent directory + * will define MQTTAgentMessageContext, then include this header file. + */ + +typedef struct MQTTAgentMessageContext MQTTAgentMessageContext_t; +typedef struct MQTTAgentCommand MQTTAgentCommand_t; + +typedef bool ( * MQTTAgentMessageSend_t )( MQTTAgentMessageContext_t * pMsgCtx, + MQTTAgentCommand_t * const * pCommandToSend, + uint32_t blockTimeMs ); + +typedef bool ( * MQTTAgentMessageRecv_t )( MQTTAgentMessageContext_t * pMsgCtx, + MQTTAgentCommand_t ** pReceivedCommand, + uint32_t blockTimeMs ); + +typedef MQTTAgentCommand_t * ( * MQTTAgentCommandGet_t )( uint32_t blockTimeMs ); +typedef bool ( * MQTTAgentCommandRelease_t )( MQTTAgentCommand_t * pCommandToRelease ); + +typedef struct MQTTAgentMessageInterface +{ + MQTTAgentMessageContext_t * pMsgCtx; + MQTTAgentMessageSend_t send; + MQTTAgentMessageRecv_t recv; + MQTTAgentCommandGet_t getCommand; + MQTTAgentCommandRelease_t releaseCommand; +} MQTTAgentMessageInterface_t; + +#endif /* CORE_MQTT_AGENT_MESSAGE_INTERFACE_H */ diff --git a/components/aws_iot/coremqtt_agent/library_mocks/src/core_mqtt_agent.c b/components/aws_iot/coremqtt_agent/library_mocks/src/core_mqtt_agent.c new file mode 100644 index 00000000..6efa4ada --- /dev/null +++ b/components/aws_iot/coremqtt_agent/library_mocks/src/core_mqtt_agent.c @@ -0,0 +1,60 @@ +/* + * coreMQTT Agent v1.2.0 + * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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. + */ + +#include "core_mqtt_agent.h" + +#include "fff.h" + +DEFINE_FAKE_VALUE_FUNC( MQTTStatus_t, + MQTTAgent_CommandLoop, + MQTTAgentContext_t * ); + +DEFINE_FAKE_VALUE_FUNC( MQTTStatus_t, + MQTTAgent_ResumeSession, + MQTTAgentContext_t *, + bool ); +DEFINE_FAKE_VALUE_FUNC( MQTTStatus_t, + MQTTAgent_Disconnect, + const MQTTAgentContext_t *, + const MQTTAgentCommandInfo_t * ); + +DEFINE_FAKE_VALUE_FUNC( MQTTStatus_t, + MQTTAgent_Init, + MQTTAgentContext_t *, + const MQTTAgentMessageInterface_t *, + const MQTTFixedBuffer_t *, + const TransportInterface_t *, + MQTTGetCurrentTimeFunc_t, + MQTTAgentIncomingPublishCallback_t, + int * ) + +DEFINE_FAKE_VALUE_FUNC( MQTTStatus_t, + MQTTAgent_Subscribe, + const MQTTAgentContext_t *, + MQTTAgentSubscribeArgs_t *, + const MQTTAgentCommandInfo_t * ); + +DEFINE_FAKE_VALUE_FUNC( MQTTStatus_t, + MQTTAgent_CancelAll, + MQTTAgentContext_t * ); diff --git a/components/aws_iot/coremqtt_agent/mocks/inc/core_mqtt_agent.h b/components/aws_iot/coremqtt_agent/mocks/inc/core_mqtt_agent.h deleted file mode 100644 index 81c2550d..00000000 --- a/components/aws_iot/coremqtt_agent/mocks/inc/core_mqtt_agent.h +++ /dev/null @@ -1,22 +0,0 @@ -/* Copyright 2023-2024 Arm Limited and/or its affiliates - * - * SPDX-License-Identifier: MIT - */ - -#ifndef CORE_MQTT_AGENT_H - #define CORE_MQTT_AGENT_H - - #ifdef __cplusplus - extern "C" { - #endif - - struct MQTTAgentCommand - { - void * pArgs; - }; - - #ifdef __cplusplus - } - #endif - -#endif // CORE_MQTT_AGENT_H diff --git a/components/aws_iot/coremqtt_agent/mocks/inc/core_mqtt_agent_message_interface.h b/components/aws_iot/coremqtt_agent/mocks/inc/core_mqtt_agent_message_interface.h deleted file mode 100644 index 666ce4f7..00000000 --- a/components/aws_iot/coremqtt_agent/mocks/inc/core_mqtt_agent_message_interface.h +++ /dev/null @@ -1,27 +0,0 @@ -/* Copyright 2023-2024 Arm Limited and/or its affiliates - * - * SPDX-License-Identifier: MIT - */ - -#ifndef CORE_MQTT_AGENT_MESSAGE_INTERFACE_H - #define CORE_MQTT_AGENT_MESSAGE_INTERFACE_H - - #ifdef __cplusplus - extern "C" { - #endif - - #include "fff.h" - - #include "queue.h" - - #include "core_mqtt_agent.h" - - typedef struct MQTTAgentMessageContext MQTTAgentMessageContext_t; - typedef struct MQTTAgentCommand MQTTAgentCommand_t; - - - #ifdef __cplusplus - } - #endif - -#endif // CORE_MQTT_AGENT_MESSAGE_INTERFACE_H diff --git a/components/aws_iot/ota_for_aws_iot_embedded_sdk/integration/CMakeLists.txt b/components/aws_iot/ota_for_aws_iot_embedded_sdk/integration/CMakeLists.txt index 7247b85b..96b70d2b 100644 --- a/components/aws_iot/ota_for_aws_iot_embedded_sdk/integration/CMakeLists.txt +++ b/components/aws_iot/ota_for_aws_iot_embedded_sdk/integration/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright 2023 Arm Limited and/or its affiliates +# Copyright 2023-2024 Arm Limited and/or its affiliates # # SPDX-License-Identifier: MIT @@ -27,6 +27,7 @@ target_link_libraries(ota-for-aws-iot-embedded-sdk coremqtt coremqtt-agent freertos-ota-pal-psa + helpers-events helpers-logging tinycbor ) diff --git a/components/aws_iot/ota_for_aws_iot_embedded_sdk/integration/src/ota_agent_task.c b/components/aws_iot/ota_for_aws_iot_embedded_sdk/integration/src/ota_agent_task.c index a38cf3f0..a9a845d4 100644 --- a/components/aws_iot/ota_for_aws_iot_embedded_sdk/integration/src/ota_agent_task.c +++ b/components/aws_iot/ota_for_aws_iot_embedded_sdk/integration/src/ota_agent_task.c @@ -35,6 +35,7 @@ #include "app_config.h" #include "mqtt_agent_task.h" +#include "events.h" /* includes for TFM */ #include "psa/update.h" diff --git a/components/connectivity/backoff_algorithm/CMakeLists.txt b/components/connectivity/backoff_algorithm/CMakeLists.txt index c162c045..542c3f82 100644 --- a/components/connectivity/backoff_algorithm/CMakeLists.txt +++ b/components/connectivity/backoff_algorithm/CMakeLists.txt @@ -1,11 +1,15 @@ -# Copyright 2023 Arm Limited and/or its affiliates +# Copyright 2023-2024 Arm Limited and/or its affiliates # # SPDX-License-Identifier: MIT -set(backoff_algorithm_SOURCE_DIR - ${CMAKE_CURRENT_LIST_DIR}/library - CACHE INTERNAL - "Path to backoffAlgorithm source code" -) +if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING) + add_subdirectory(library_mocks) +else () + set(backoff_algorithm_SOURCE_DIR + ${CMAKE_CURRENT_LIST_DIR}/library + CACHE INTERNAL + "Path to backoffAlgorithm source code" + ) -add_subdirectory(integration) + add_subdirectory(integration) +endif() diff --git a/components/connectivity/backoff_algorithm/library_mocks/CMakeLists.txt b/components/connectivity/backoff_algorithm/library_mocks/CMakeLists.txt new file mode 100644 index 00000000..61af3e0e --- /dev/null +++ b/components/connectivity/backoff_algorithm/library_mocks/CMakeLists.txt @@ -0,0 +1,17 @@ +# Copyright 2023-2024 Arm Limited and/or its affiliates +# +# SPDX-License-Identifier: MIT + +add_library(backoff-algorithm-mock + src/backoff_algorithm.c +) + +target_include_directories(backoff-algorithm-mock + PUBLIC + inc +) + +target_link_libraries(backoff-algorithm-mock + PRIVATE + fff +) diff --git a/components/connectivity/backoff_algorithm/library_mocks/inc/backoff_algorithm.h b/components/connectivity/backoff_algorithm/library_mocks/inc/backoff_algorithm.h new file mode 100644 index 00000000..f5a6c94a --- /dev/null +++ b/components/connectivity/backoff_algorithm/library_mocks/inc/backoff_algorithm.h @@ -0,0 +1,59 @@ +/* + * backoffAlgorithm v1.3.0 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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 BACKOFF_ALGORITHM_H_ +#define BACKOFF_ALGORITHM_H_ + +#include "fff.h" +#include + +#define BACKOFF_ALGORITHM_RETRY_FOREVER ( UINT32_MAX ) + +typedef enum BackoffAlgorithmStatus +{ + BackoffAlgorithmSuccess = 0, + BackoffAlgorithmRetriesExhausted +} BackoffAlgorithmStatus_t; + +typedef struct BackoffAlgorithmContext +{ + int dummy; +} BackoffAlgorithmContext_t; + +DECLARE_FAKE_VALUE_FUNC( BackoffAlgorithmStatus_t, + BackoffAlgorithm_GetNextBackoff, + BackoffAlgorithmContext_t *, + uint32_t, + uint16_t * ); + + +DECLARE_FAKE_VOID_FUNC( BackoffAlgorithm_InitializeParams, + BackoffAlgorithmContext_t *, + uint16_t, + uint16_t, + uint32_t ); + +#endif /* ifndef BACKOFF_ALGORITHM_H_ */ diff --git a/components/connectivity/backoff_algorithm/library_mocks/src/backoff_algorithm.c b/components/connectivity/backoff_algorithm/library_mocks/src/backoff_algorithm.c new file mode 100644 index 00000000..1c50f077 --- /dev/null +++ b/components/connectivity/backoff_algorithm/library_mocks/src/backoff_algorithm.c @@ -0,0 +1,40 @@ +/* + * backoffAlgorithm v1.3.0 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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. + */ + +#include "backoff_algorithm.h" + +DEFINE_FAKE_VALUE_FUNC( BackoffAlgorithmStatus_t, + BackoffAlgorithm_GetNextBackoff, + BackoffAlgorithmContext_t *, + uint32_t, + uint16_t * ); + + +DEFINE_FAKE_VOID_FUNC( BackoffAlgorithm_InitializeParams, + BackoffAlgorithmContext_t *, + uint16_t, + uint16_t, + uint32_t ); diff --git a/components/connectivity/freertos_plus_tcp/CMakeLists.txt b/components/connectivity/freertos_plus_tcp/CMakeLists.txt index bc43b12e..342f8aeb 100644 --- a/components/connectivity/freertos_plus_tcp/CMakeLists.txt +++ b/components/connectivity/freertos_plus_tcp/CMakeLists.txt @@ -2,21 +2,25 @@ # # SPDX-License-Identifier: MIT -set(freertos_plus_tcp_SOURCE_DIR - ${CMAKE_CURRENT_LIST_DIR}/library - CACHE INTERNAL - "Path to FreeRTOS-Plus-TCP source code" -) +if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING) + add_subdirectory(library_mocks) +else () + set(freertos_plus_tcp_SOURCE_DIR + ${CMAKE_CURRENT_LIST_DIR}/library + CACHE INTERNAL + "Path to FreeRTOS-Plus-TCP source code" + ) -if (${ARM_CORSTONE_BSP_TARGET_PLATFORM} STREQUAL "corstone300" OR ${ARM_CORSTONE_BSP_TARGET_PLATFORM} STREQUAL "corstone310") - set(FREERTOS_PLUS_TCP_NETWORK_IF "MPS3_AN552" CACHE STRING "FreeRTOS Plus TCP Network Interface selection") -elseif (${ARM_CORSTONE_BSP_TARGET_PLATFORM} STREQUAL "corstone315") - set(FREERTOS_PLUS_TCP_NETWORK_IF "MPS4_CS315" CACHE STRING "FreeRTOS Plus TCP Network Interface selection") -endif() + if (${ARM_CORSTONE_BSP_TARGET_PLATFORM} STREQUAL "corstone300" OR ${ARM_CORSTONE_BSP_TARGET_PLATFORM} STREQUAL "corstone310") + set(FREERTOS_PLUS_TCP_NETWORK_IF "MPS3_AN552" CACHE STRING "FreeRTOS Plus TCP Network Interface selection") + elseif (${ARM_CORSTONE_BSP_TARGET_PLATFORM} STREQUAL "corstone315") + set(FREERTOS_PLUS_TCP_NETWORK_IF "MPS4_CS315" CACHE STRING "FreeRTOS Plus TCP Network Interface selection") + endif() -set(FREERTOS_PLUS_TCP_BUFFER_ALLOCATION "2" CACHE STRING "FreeRTOS buffer allocation model number. 1 .. 2.") + set(FREERTOS_PLUS_TCP_BUFFER_ALLOCATION "2" CACHE STRING "FreeRTOS buffer allocation model number. 1 .. 2.") -add_subdirectory(library/source) -add_subdirectory(library/tools) + add_subdirectory(library/source) + add_subdirectory(library/tools) +endif() add_subdirectory(integration) diff --git a/components/connectivity/freertos_plus_tcp/integration/CMakeLists.txt b/components/connectivity/freertos_plus_tcp/integration/CMakeLists.txt index ce388b28..fb42225e 100644 --- a/components/connectivity/freertos_plus_tcp/integration/CMakeLists.txt +++ b/components/connectivity/freertos_plus_tcp/integration/CMakeLists.txt @@ -1,29 +1,34 @@ -# Copyright 2023 Arm Limited and/or its affiliates +# Copyright 2023-2024 Arm Limited and/or its affiliates # # SPDX-License-Identifier: MIT -target_sources(freertos_plus_tcp - PRIVATE - src/network_startup.c - src/transport_mbedtls.c -) +if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING) + add_subdirectory(integration_mocks) + add_library(connectivity-stack-mock ALIAS freertos-plus-tcp-integration-mock) +else () + target_sources(freertos_plus_tcp + PRIVATE + src/network_startup.c + src/transport_mbedtls.c + ) -target_include_directories(freertos_plus_tcp SYSTEM - PUBLIC - inc -) + target_include_directories(freertos_plus_tcp SYSTEM + PUBLIC + inc + ) -target_link_libraries(freertos_plus_tcp - PUBLIC - # TODO: The CS315 network interface layer inside the FreeRTOS TCP/IP stack - # https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/source/portable/NetworkInterface/MPS4_CS315/Device/Include/SSE315.h#L62 - # is done in such a way that it depends on fri-bsp. This dependency - # should be removed. - fri-bsp - PRIVATE - coremqtt - helpers-events - iot-tls -) + target_link_libraries(freertos_plus_tcp + PUBLIC + # TODO: The CS315 network interface layer inside the FreeRTOS TCP/IP stack + # https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/source/portable/NetworkInterface/MPS4_CS315/Device/Include/SSE315.h#L62 + # is done in such a way that it depends on fri-bsp. This dependency + # should be removed. + fri-bsp + PRIVATE + coremqtt + helpers-events + iot-tls + ) -add_library(connectivity-stack ALIAS freertos_plus_tcp) + add_library(connectivity-stack ALIAS freertos_plus_tcp) +endif() diff --git a/components/connectivity/freertos_plus_tcp/integration/integration_mocks/CMakeLists.txt b/components/connectivity/freertos_plus_tcp/integration/integration_mocks/CMakeLists.txt new file mode 100644 index 00000000..2931eb2c --- /dev/null +++ b/components/connectivity/freertos_plus_tcp/integration/integration_mocks/CMakeLists.txt @@ -0,0 +1,17 @@ +# Copyright 2023-2024 Arm Limited and/or its affiliates +# +# SPDX-License-Identifier: MIT + +add_library(freertos-plus-tcp-integration-mock + src/transport_interface_api.c +) + +target_include_directories(freertos-plus-tcp-integration-mock + PUBLIC + inc +) + +target_link_libraries(freertos-plus-tcp-integration-mock + PRIVATE + fff +) diff --git a/components/connectivity/freertos_plus_tcp/integration/integration_mocks/inc/transport_interface_api.h b/components/connectivity/freertos_plus_tcp/integration/integration_mocks/inc/transport_interface_api.h new file mode 100644 index 00000000..03381d94 --- /dev/null +++ b/components/connectivity/freertos_plus_tcp/integration/integration_mocks/inc/transport_interface_api.h @@ -0,0 +1,80 @@ +/* + * FreeRTOS Transport Secure Sockets V1.0.1 + * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +#ifndef TRANSPORT_INTERFACE_API_H +#define TRANSPORT_INTERFACE_API_H + +#include "fff.h" +#include +#include + +typedef int NetworkContext_t; +typedef enum TransportStatus +{ + TRANSPORT_STATUS_SUCCESS = 0, + TRANSPORT_STATUS_INVALID_PARAMETER, + TRANSPORT_STATUS_CONNECT_FAILURE +} TransportStatus_t; + +typedef struct TLSParams +{ + const char * pAlpnProtos; + bool disableSni; + size_t maxFragmentLength; + const char * pRootCa; + size_t rootCaSize; + char * pPrivateKeyLabel; + char * pClientCertLabel; + char * pLoginPIN; +} TLSParams_t; + + +typedef struct ServerInfo +{ + const char * pHostName; + size_t hostNameLength; + uint16_t port; +} ServerInfo_t; + +static int Transport_Send = 1; +static int Transport_Recv = 2; +/* The above two lines are functions, but for */ +/* mqtt_agent_task they are not called merely assigned. */ + +DECLARE_FAKE_VALUE_FUNC( TransportStatus_t, + Transport_Connect, + NetworkContext_t *, + const ServerInfo_t *, + const TLSParams_t *, + uint32_t, + uint32_t ); +DECLARE_FAKE_VALUE_FUNC( TransportStatus_t, + Transport_Disconnect, + NetworkContext_t * ); + + +#endif /* TRANSPORT_INTERFACE_API_H */ diff --git a/components/connectivity/freertos_plus_tcp/integration/integration_mocks/src/transport_interface_api.c b/components/connectivity/freertos_plus_tcp/integration/integration_mocks/src/transport_interface_api.c new file mode 100644 index 00000000..4de3e50c --- /dev/null +++ b/components/connectivity/freertos_plus_tcp/integration/integration_mocks/src/transport_interface_api.c @@ -0,0 +1,39 @@ +/* + * FreeRTOS Transport Secure Sockets V1.0.1 + * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +#include "transport_interface_api.h" + +DEFINE_FAKE_VALUE_FUNC( TransportStatus_t, + Transport_Connect, + NetworkContext_t *, + const ServerInfo_t *, + const TLSParams_t *, + uint32_t, + uint32_t ); +DEFINE_FAKE_VALUE_FUNC( TransportStatus_t, + Transport_Disconnect, + NetworkContext_t * ); diff --git a/components/connectivity/freertos_plus_tcp/library_mocks/CMakeLists.txt b/components/connectivity/freertos_plus_tcp/library_mocks/CMakeLists.txt new file mode 100644 index 00000000..0e917489 --- /dev/null +++ b/components/connectivity/freertos_plus_tcp/library_mocks/CMakeLists.txt @@ -0,0 +1,15 @@ +# Copyright 2024 Arm Limited and/or its affiliates +# +# SPDX-License-Identifier: MIT + +add_library(freertos-plus-tcp-mock INTERFACE) + +target_include_directories(freertos-plus-tcp-mock + INTERFACE + inc +) + +target_link_libraries(freertos-plus-tcp-mock + INTERFACE + fff +) diff --git a/components/connectivity/iot_socket/CMakeLists.txt b/components/connectivity/iot_socket/CMakeLists.txt index 45a7a8fa..8d5ae16e 100644 --- a/components/connectivity/iot_socket/CMakeLists.txt +++ b/components/connectivity/iot_socket/CMakeLists.txt @@ -1,11 +1,13 @@ -# Copyright 2023 Arm Limited and/or its affiliates +# Copyright 2023-2024 Arm Limited and/or its affiliates # # SPDX-License-Identifier: MIT -set(iot_socket_SOURCE_DIR - ${CMAKE_CURRENT_LIST_DIR}/library - CACHE INTERNAL - "Path to IoT Socket source code" -) +if(CMAKE_CROSSCOMPILING) + set(iot_socket_SOURCE_DIR + ${CMAKE_CURRENT_LIST_DIR}/library + CACHE INTERNAL + "Path to IoT Socket source code" + ) -add_subdirectory(integration) + add_subdirectory(integration) +endif() diff --git a/components/connectivity/iot_vsocket/CMakeLists.txt b/components/connectivity/iot_vsocket/CMakeLists.txt index 82eeb1c9..12cd4d07 100644 --- a/components/connectivity/iot_vsocket/CMakeLists.txt +++ b/components/connectivity/iot_vsocket/CMakeLists.txt @@ -1,17 +1,21 @@ -# Copyright 2023 Arm Limited and/or its affiliates +# Copyright 2023-2024 Arm Limited and/or its affiliates # # SPDX-License-Identifier: MIT -set(iot_vsocket_SOURCE_DIR - ${CMAKE_CURRENT_LIST_DIR}/library - CACHE INTERNAL - "Path to Arm Virtual Hardware - IoT Virtual Socket source code" -) +if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING) + add_subdirectory(library_mocks) +else () + set(iot_vsocket_SOURCE_DIR + ${CMAKE_CURRENT_LIST_DIR}/library + CACHE INTERNAL + "Path to Arm Virtual Hardware - IoT Virtual Socket source code" + ) -include(ApplyPatches) + include(ApplyPatches) -set(PATCH_FILES_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/integration/patches") -set(PATCH_FILES "${PATCH_FILES_DIRECTORY}/0001-Replace-CMSIS-RTX-APIs-with-FreeRTOS-APIs.patch") -iot_reference_arm_corstone3xx_apply_patches("${iot_vsocket_SOURCE_DIR}" "${PATCH_FILES}") + set(PATCH_FILES_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/integration/patches") + set(PATCH_FILES "${PATCH_FILES_DIRECTORY}/0001-Replace-CMSIS-RTX-APIs-with-FreeRTOS-APIs.patch") + iot_reference_arm_corstone3xx_apply_patches("${iot_vsocket_SOURCE_DIR}" "${PATCH_FILES}") +endif() add_subdirectory(integration) diff --git a/components/connectivity/iot_vsocket/integration/CMakeLists.txt b/components/connectivity/iot_vsocket/integration/CMakeLists.txt index e01ec9ae..b89ab1e4 100644 --- a/components/connectivity/iot_vsocket/integration/CMakeLists.txt +++ b/components/connectivity/iot_vsocket/integration/CMakeLists.txt @@ -1,26 +1,30 @@ -# Copyright 2023 Arm Limited and/or its affiliates +# Copyright 2023-2024 Arm Limited and/or its affiliates # # SPDX-License-Identifier: MIT -add_library(iot-vsocket - ${iot_vsocket_SOURCE_DIR}/interface/vsocket/iot_socket.c - src/network_startup.c - src/transport_tls_iot_socket.c -) +if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING) + add_subdirectory(integration_mocks) + add_library(connectivity-stack-mock ALIAS iot-vsocket-integration-mock) +else() + add_library(iot-vsocket + ${iot_vsocket_SOURCE_DIR}/interface/vsocket/iot_socket.c + src/network_startup.c + src/transport_tls_iot_socket.c + ) -target_include_directories(iot-vsocket - PUBLIC - inc - ${iot_vsocket_SOURCE_DIR}/interface/include -) + target_include_directories(iot-vsocket + PUBLIC + inc + ${iot_vsocket_SOURCE_DIR}/interface/include + ) -target_link_libraries(iot-vsocket - PRIVATE - coremqtt - helpers-events - fri-bsp - iot-socket-api - iot-tls -) - -add_library(connectivity-stack ALIAS iot-vsocket) + target_link_libraries(iot-vsocket + PRIVATE + coremqtt + helpers-events + fri-bsp + iot-socket-api + iot-tls + ) + add_library(connectivity-stack ALIAS iot-vsocket) +endif() diff --git a/components/connectivity/iot_vsocket/integration/integration_mocks/CMakeLists.txt b/components/connectivity/iot_vsocket/integration/integration_mocks/CMakeLists.txt new file mode 100644 index 00000000..8ddb2d65 --- /dev/null +++ b/components/connectivity/iot_vsocket/integration/integration_mocks/CMakeLists.txt @@ -0,0 +1,17 @@ +# Copyright 2023-2024 Arm Limited and/or its affiliates +# +# SPDX-License-Identifier: MIT + +add_library(iot-vsocket-integration-mock + src/transport_interface_api.c +) + +target_include_directories(iot-vsocket-integration-mock + PUBLIC + inc +) + +target_link_libraries(iot-vsocket-integration-mock + PRIVATE + fff +) diff --git a/components/connectivity/iot_vsocket/integration/integration_mocks/inc/transport_interface_api.h b/components/connectivity/iot_vsocket/integration/integration_mocks/inc/transport_interface_api.h new file mode 100644 index 00000000..30d4ec64 --- /dev/null +++ b/components/connectivity/iot_vsocket/integration/integration_mocks/inc/transport_interface_api.h @@ -0,0 +1,78 @@ +/* + * FreeRTOS Transport Secure Sockets V1.0.1 + * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +#ifndef TRANSPORT_INTERFACE_API_H +#define TRANSPORT_INTERFACE_API_H + +#include "fff.h" +#include +#include + +typedef int NetworkContext_t; +typedef enum TransportStatus +{ + TRANSPORT_STATUS_SUCCESS = 0, + TRANSPORT_STATUS_CONNECT_FAILURE +} TransportStatus_t; + +typedef struct TLSParams +{ + const char * pAlpnProtos; + bool disableSni; + size_t maxFragmentLength; + const char * pRootCa; + size_t rootCaSize; + char * pPrivateKeyLabel; + char * pClientCertLabel; + char * pLoginPIN; +} TLSParams_t; + +typedef struct ServerInfo +{ + const char * pHostName; + size_t hostNameLength; + uint16_t port; +} ServerInfo_t; + +/* The below two are usually functions, but for */ +/* mqtt_agent_task they are not called merely assigned. */ +static int Transport_Send = 1; +static int Transport_Recv = 2; + +DECLARE_FAKE_VALUE_FUNC( TransportStatus_t, + Transport_Connect, + NetworkContext_t *, + const ServerInfo_t *, + const TLSParams_t *, + uint32_t, + uint32_t ); + +DECLARE_FAKE_VALUE_FUNC( TransportStatus_t, + Transport_Disconnect, + NetworkContext_t * ); + +#endif /* TRANSPORT_INTERFACE_API_H */ diff --git a/components/connectivity/iot_vsocket/integration/integration_mocks/src/transport_interface_api.c b/components/connectivity/iot_vsocket/integration/integration_mocks/src/transport_interface_api.c new file mode 100644 index 00000000..e01defd1 --- /dev/null +++ b/components/connectivity/iot_vsocket/integration/integration_mocks/src/transport_interface_api.c @@ -0,0 +1,40 @@ +/* + * FreeRTOS Transport Secure Sockets V1.0.1 + * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +#include "transport_interface_api.h" + +DEFINE_FAKE_VALUE_FUNC( TransportStatus_t, + Transport_Connect, + NetworkContext_t *, + const ServerInfo_t *, + const TLSParams_t *, + uint32_t, + uint32_t ); + +DEFINE_FAKE_VALUE_FUNC( TransportStatus_t, + Transport_Disconnect, + NetworkContext_t * ); diff --git a/components/connectivity/iot_vsocket/library_mocks/CMakeLists.txt b/components/connectivity/iot_vsocket/library_mocks/CMakeLists.txt new file mode 100644 index 00000000..eaf11f16 --- /dev/null +++ b/components/connectivity/iot_vsocket/library_mocks/CMakeLists.txt @@ -0,0 +1,16 @@ +# Copyright 2024 Arm Limited and/or its affiliates +# +# SPDX-License-Identifier: MIT + +add_library(iot-vsocket-mock INTERFACE +) + +target_include_directories(iot-vsocket-mock + INTERFACE + inc +) + +target_link_libraries(iot-vsocket-mock + INTERFACE + fff +) diff --git a/components/freertos_kernel/CMakeLists.txt b/components/freertos_kernel/CMakeLists.txt index 116bf3c5..20011e2f 100644 --- a/components/freertos_kernel/CMakeLists.txt +++ b/components/freertos_kernel/CMakeLists.txt @@ -3,7 +3,7 @@ # SPDX-License-Identifier: MIT if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING) - add_subdirectory(mocks) + add_subdirectory(library_mocks) else() set(freertos_kernel_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/library diff --git a/components/freertos_kernel/mocks/CMakeLists.txt b/components/freertos_kernel/library_mocks/CMakeLists.txt similarity index 88% rename from components/freertos_kernel/mocks/CMakeLists.txt rename to components/freertos_kernel/library_mocks/CMakeLists.txt index 03a74269..95c178de 100644 --- a/components/freertos_kernel/mocks/CMakeLists.txt +++ b/components/freertos_kernel/library_mocks/CMakeLists.txt @@ -3,6 +3,8 @@ # SPDX-License-Identifier: MIT add_library(freertos-kernel-mock + src/event_groups.c + src/tasks.c src/queue.c ) diff --git a/components/freertos_kernel/library_mocks/inc/FreeRTOS.h b/components/freertos_kernel/library_mocks/inc/FreeRTOS.h new file mode 100644 index 00000000..6e45d25c --- /dev/null +++ b/components/freertos_kernel/library_mocks/inc/FreeRTOS.h @@ -0,0 +1,47 @@ +/* + * FreeRTOS Kernel V11.1.0 + * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + * + */ + +#ifndef INC_FREERTOS_H +#define INC_FREERTOS_H + +#include "fff.h" +#include "portmacro.h" + +typedef int StaticQueue_t; + + + +/* This file also contains some definitions usually found in + * 'FreeRTOSConfig_target.h'. + * See below. */ +#define configTICK_RATE_HZ ( 1000 ) +#define TICKS_TO_pdMS( xTicks ) ( ( uint32_t ) xTicks ) + +#endif /* INC_FREERTOS_H */ diff --git a/components/freertos_kernel/library_mocks/inc/event_groups.h b/components/freertos_kernel/library_mocks/inc/event_groups.h new file mode 100644 index 00000000..e69b7b08 --- /dev/null +++ b/components/freertos_kernel/library_mocks/inc/event_groups.h @@ -0,0 +1,47 @@ +/* + * FreeRTOS Kernel V11.1.0 + * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + * + */ + +#ifndef EVENT_GROUPS_H +#define EVENT_GROUPS_H + + +#include "fff.h" + +DECLARE_FAKE_VALUE_FUNC( int, + xEventGroupClearBits, + void *, + const int ); + +DECLARE_FAKE_VALUE_FUNC( int, + xEventGroupSetBits, + void *, + const int ); + +#endif /* EVENT_GROUPS_H */ diff --git a/components/freertos_kernel/library_mocks/inc/portmacro.h b/components/freertos_kernel/library_mocks/inc/portmacro.h new file mode 100644 index 00000000..3dd06dd8 --- /dev/null +++ b/components/freertos_kernel/library_mocks/inc/portmacro.h @@ -0,0 +1,38 @@ +/* + * FreeRTOS Kernel V11.1.0 + * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2023-2024 Arm Limited and/or its affiliates + * + * + * 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + * + */ + +#ifndef PORTMACRO_H +#define PORTMACRO_H + +typedef long BaseType_t; +typedef unsigned short UBaseType_t; +typedef unsigned long TickType_t; + +#endif /* ifndef PORTMACRO_H */ diff --git a/components/freertos_kernel/library_mocks/inc/projdefs.h b/components/freertos_kernel/library_mocks/inc/projdefs.h new file mode 100644 index 00000000..c2c8d632 --- /dev/null +++ b/components/freertos_kernel/library_mocks/inc/projdefs.h @@ -0,0 +1,47 @@ +/* + * FreeRTOS Kernel V11.1.0 + * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + * + */ + +#ifndef PROJDEFS_H +#define PROJDEFS_H + +#include "portmacro.h" + +typedef void (* TaskFunction_t)( void * ); +#define pdFALSE ( ( BaseType_t ) 0 ) +#define pdTRUE ( ( BaseType_t ) 1 ) + +#define pdPASS ( pdTRUE ) +#define pdFAIL ( pdFALSE ) + +#ifndef pdMS_TO_TICKS + #define pdMS_TO_TICKS( xTimeInMs ) ( ( TickType_t ) ( ( ( TickType_t ) ( xTimeInMs ) * ( TickType_t ) configTICK_RATE_HZ ) / ( TickType_t ) 1000U ) ) +#endif + +#endif /* PROJDEFS_H */ diff --git a/components/freertos_kernel/library_mocks/inc/queue.h b/components/freertos_kernel/library_mocks/inc/queue.h new file mode 100644 index 00000000..7a61375d --- /dev/null +++ b/components/freertos_kernel/library_mocks/inc/queue.h @@ -0,0 +1,50 @@ +/* + * FreeRTOS Kernel V11.1.0 + * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + * + */ + +#ifndef QUEUE_H +#define QUEUE_H + +#include +#include "fff.h" +#include "portmacro.h" +#include "projdefs.h" +#include "FreeRTOS.h" + +struct QueueDefinition +{ + int dummy; +}; +typedef struct QueueDefinition * QueueHandle_t; + +DECLARE_FAKE_VALUE_FUNC( BaseType_t, xQueueSendToBack, QueueHandle_t, const void *, TickType_t ); +DECLARE_FAKE_VALUE_FUNC( BaseType_t, xQueueReceive, QueueHandle_t, void *, TickType_t ); +DECLARE_FAKE_VALUE_FUNC( QueueHandle_t, xQueueCreateStatic, const UBaseType_t, const UBaseType_t, uint8_t *, StaticQueue_t * ); + +#endif /* QUEUE_H */ diff --git a/components/freertos_kernel/library_mocks/inc/semphr.h b/components/freertos_kernel/library_mocks/inc/semphr.h new file mode 100644 index 00000000..c05c5ffd --- /dev/null +++ b/components/freertos_kernel/library_mocks/inc/semphr.h @@ -0,0 +1,35 @@ +/* + * FreeRTOS Kernel V11.1.0 + * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + * + */ + +#ifndef SEMAPHORE_H +#define SEMAPHORE_H + + +#endif /* SEMAPHORE_H */ diff --git a/components/freertos_kernel/library_mocks/inc/task.h b/components/freertos_kernel/library_mocks/inc/task.h new file mode 100644 index 00000000..8a724c03 --- /dev/null +++ b/components/freertos_kernel/library_mocks/inc/task.h @@ -0,0 +1,72 @@ +/* + * FreeRTOS Kernel V11.1.0 + * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + * + */ + +#ifndef INC_TASK_H +#define INC_TASK_H + +#include "fff.h" +#include "portmacro.h" +#include "projdefs.h" +#include + +typedef int * TaskHandle_t; + +typedef enum +{ + eNoAction = 0, + eSetValueWithOverwrite +} eNotifyAction; + +DECLARE_FAKE_VALUE_FUNC( TickType_t, xTaskGetTickCount ); +DECLARE_FAKE_VALUE_FUNC( BaseType_t, + xTaskCreate, + TaskFunction_t, + const char * const, + const uint16_t, + void * const, + UBaseType_t, + TaskHandle_t * const ); +DECLARE_FAKE_VALUE_FUNC( BaseType_t, + xTaskNotify, + TaskHandle_t, + uint32_t, + eNotifyAction ); +DECLARE_FAKE_VALUE_FUNC( TaskHandle_t, xTaskGetCurrentTaskHandle ); +DECLARE_FAKE_VALUE_FUNC( BaseType_t, + xTaskNotifyWait, + int, + int, + void *, + TickType_t ); + +DECLARE_FAKE_VOID_FUNC( vTaskDelete, TaskHandle_t ); +DECLARE_FAKE_VOID_FUNC( vTaskDelay, const TickType_t ); + +#endif /* INC_TASK_H */ diff --git a/components/freertos_kernel/library_mocks/src/event_groups.c b/components/freertos_kernel/library_mocks/src/event_groups.c new file mode 100644 index 00000000..2f7fad40 --- /dev/null +++ b/components/freertos_kernel/library_mocks/src/event_groups.c @@ -0,0 +1,42 @@ +/* + * FreeRTOS Kernel V11.1.0 + * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + * + */ + +#include "fff.h" +#include "event_groups.h" + +DEFINE_FAKE_VALUE_FUNC( int, + xEventGroupClearBits, + void *, + const int ); + +DEFINE_FAKE_VALUE_FUNC( int, + xEventGroupSetBits, + void *, + const int ); diff --git a/components/freertos_kernel/library_mocks/src/queue.c b/components/freertos_kernel/library_mocks/src/queue.c new file mode 100644 index 00000000..dfab68fb --- /dev/null +++ b/components/freertos_kernel/library_mocks/src/queue.c @@ -0,0 +1,36 @@ +/* + * FreeRTOS Kernel V11.1.0 + * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + * + */ + +#include "queue.h" +#include "fff.h" + +DEFINE_FAKE_VALUE_FUNC( BaseType_t, xQueueSendToBack, QueueHandle_t, const void *, TickType_t ); +DEFINE_FAKE_VALUE_FUNC( BaseType_t, xQueueReceive, QueueHandle_t, void *, TickType_t ); +DEFINE_FAKE_VALUE_FUNC( QueueHandle_t, xQueueCreateStatic, const UBaseType_t, const UBaseType_t, uint8_t *, StaticQueue_t * ); diff --git a/components/freertos_kernel/library_mocks/src/tasks.c b/components/freertos_kernel/library_mocks/src/tasks.c new file mode 100644 index 00000000..bc2d8ecc --- /dev/null +++ b/components/freertos_kernel/library_mocks/src/tasks.c @@ -0,0 +1,57 @@ +/* + * FreeRTOS Kernel V11.1.0 + * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2024 Arm Limited and/or its affiliates + * + * + * 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + * + */ + +#include "task.h" +#include "fff.h" + +DEFINE_FAKE_VALUE_FUNC( TickType_t, xTaskGetTickCount ); +DEFINE_FAKE_VALUE_FUNC( BaseType_t, + xTaskCreate, + TaskFunction_t, + const char * const, + const uint16_t, + void * const, + UBaseType_t, + TaskHandle_t * const ); +DEFINE_FAKE_VALUE_FUNC( BaseType_t, + xTaskNotify, + TaskHandle_t, + uint32_t, + eNotifyAction ); +DEFINE_FAKE_VALUE_FUNC( TaskHandle_t, xTaskGetCurrentTaskHandle ); +DEFINE_FAKE_VALUE_FUNC( BaseType_t, + xTaskNotifyWait, + int, + int, + void *, + TickType_t ); + +DEFINE_FAKE_VOID_FUNC( vTaskDelete, TaskHandle_t ); +DEFINE_FAKE_VOID_FUNC( vTaskDelay, const TickType_t ); diff --git a/components/freertos_kernel/mocks/inc/portmacro.h b/components/freertos_kernel/mocks/inc/portmacro.h deleted file mode 100644 index ea215c0d..00000000 --- a/components/freertos_kernel/mocks/inc/portmacro.h +++ /dev/null @@ -1,21 +0,0 @@ -/* Copyright 2023-2024 Arm Limited and/or its affiliates - * - * SPDX-License-Identifier: MIT - */ - -#ifndef PORTMACRO_H - #define PORTMACRO_H - - #ifdef __cplusplus - extern "C" { - #endif - - typedef long BaseType_t; - typedef unsigned short UBaseType_t; - typedef unsigned long TickType_t; - - #ifdef __cplusplus - } - #endif - -#endif /* ifndef PORTMACRO_H */ diff --git a/components/freertos_kernel/mocks/inc/projdefs.h b/components/freertos_kernel/mocks/inc/projdefs.h deleted file mode 100644 index 4f19a0bb..00000000 --- a/components/freertos_kernel/mocks/inc/projdefs.h +++ /dev/null @@ -1,22 +0,0 @@ -/* Copyright 2023-2024 Arm Limited and/or its affiliates - * - * SPDX-License-Identifier: MIT - */ - -#ifndef PROJDEFS_H -#define PROJDEFS_H - -#include "portmacro.h" - -typedef void (* TaskFunction_t)( void * ); -#define pdFALSE ( ( BaseType_t ) 0 ) -#define pdTRUE ( ( BaseType_t ) 1 ) - -#define pdPASS ( pdTRUE ) -#define pdFAIL ( pdFALSE ) - -#ifndef pdMS_TO_TICKS - #define pdMS_TO_TICKS( xTimeInMs ) ( ( TickType_t ) ( ( ( TickType_t ) ( xTimeInMs ) * ( TickType_t ) configTICK_RATE_HZ ) / ( TickType_t ) 1000U ) ) -#endif - -#endif // PROJDEFS_H diff --git a/components/freertos_kernel/mocks/inc/queue.h b/components/freertos_kernel/mocks/inc/queue.h deleted file mode 100644 index 5cb532cc..00000000 --- a/components/freertos_kernel/mocks/inc/queue.h +++ /dev/null @@ -1,30 +0,0 @@ -/* Copyright 2023-2024 Arm Limited and/or its affiliates - * - * SPDX-License-Identifier: MIT - */ - -#ifndef QUEUE_H - #define QUEUE_H - - #ifdef __cplusplus - extern "C" { - #endif - - #include "fff.h" - #include "portmacro.h" - #include "projdefs.h" - - struct QueueDefinition - { - int dummy; - }; - typedef struct QueueDefinition * QueueHandle_t; - - DECLARE_FAKE_VALUE_FUNC( BaseType_t, xQueueSendToBack, QueueHandle_t, const void *, TickType_t ); - DECLARE_FAKE_VALUE_FUNC( BaseType_t, xQueueReceive, QueueHandle_t, void *, TickType_t ); - - #ifdef __cplusplus - } - #endif - -#endif // QUEUE_H diff --git a/components/freertos_kernel/mocks/src/queue.c b/components/freertos_kernel/mocks/src/queue.c deleted file mode 100644 index a6a6eb46..00000000 --- a/components/freertos_kernel/mocks/src/queue.c +++ /dev/null @@ -1,10 +0,0 @@ -/* Copyright 2023-2024 Arm Limited and/or its affiliates - * - * SPDX-License-Identifier: MIT - */ - -#include "queue.h" -#include "fff.h" - -DEFINE_FAKE_VALUE_FUNC( BaseType_t, xQueueSendToBack, QueueHandle_t, const void *, TickType_t ); -DEFINE_FAKE_VALUE_FUNC( BaseType_t, xQueueReceive, QueueHandle_t, void *, TickType_t ); diff --git a/components/security/freertos_ota_pal_psa/CMakeLists.txt b/components/security/freertos_ota_pal_psa/CMakeLists.txt index 92dbdaec..5595b52c 100644 --- a/components/security/freertos_ota_pal_psa/CMakeLists.txt +++ b/components/security/freertos_ota_pal_psa/CMakeLists.txt @@ -1,19 +1,21 @@ -# Copyright 2023 Arm Limited and/or its affiliates +# Copyright 2023-2024 Arm Limited and/or its affiliates # # SPDX-License-Identifier: MIT -set(freertos_ota_pal_psa_SOURCE_DIR - ${CMAKE_CURRENT_LIST_DIR}/library - CACHE INTERNAL - "Path to FreeRTOS OTA PAL based on PSA API source code" -) +if(CMAKE_CROSSCOMPILING) + set(freertos_ota_pal_psa_SOURCE_DIR + ${CMAKE_CURRENT_LIST_DIR}/library + CACHE INTERNAL + "Path to FreeRTOS OTA PAL based on PSA API source code" + ) -include(ApplyPatches) + include(ApplyPatches) -set(PATCH_FILES_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/integration/patches") -set(PATCH_FILES - "${PATCH_FILES_DIRECTORY}/0001-ota-abort-Fix-successful-abortion-check.patch" -) -iot_reference_arm_corstone3xx_apply_patches("${freertos_ota_pal_psa_SOURCE_DIR}" "${PATCH_FILES}") + set(PATCH_FILES_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/integration/patches") + set(PATCH_FILES + "${PATCH_FILES_DIRECTORY}/0001-ota-abort-Fix-successful-abortion-check.patch" + ) + iot_reference_arm_corstone3xx_apply_patches("${freertos_ota_pal_psa_SOURCE_DIR}" "${PATCH_FILES}") -add_subdirectory(integration) + add_subdirectory(integration) +endif() diff --git a/components/security/freertos_pkcs11_psa/CMakeLists.txt b/components/security/freertos_pkcs11_psa/CMakeLists.txt index def400cf..ae4551b2 100644 --- a/components/security/freertos_pkcs11_psa/CMakeLists.txt +++ b/components/security/freertos_pkcs11_psa/CMakeLists.txt @@ -1,11 +1,13 @@ -# Copyright 2023 Arm Limited and/or its affiliates +# Copyright 2023-2024 Arm Limited and/or its affiliates # # SPDX-License-Identifier: MIT -set(freertos_pkcs11_psa_SOURCE_DIR +if(CMAKE_CROSSCOMPILING) + set(freertos_pkcs11_psa_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/library CACHE INTERNAL "Path to FreeRTOS PKCS#11 to PSA shim layer source code" -) + ) -add_subdirectory(integration) + add_subdirectory(integration) +endif() diff --git a/components/security/mbedtls/CMakeLists.txt b/components/security/mbedtls/CMakeLists.txt index 7294f149..0273f3f5 100644 --- a/components/security/mbedtls/CMakeLists.txt +++ b/components/security/mbedtls/CMakeLists.txt @@ -1,21 +1,24 @@ -# Copyright 2023 Arm Limited and/or its affiliates +# Copyright 2023-2024 Arm Limited and/or its affiliates # # SPDX-License-Identifier: MIT -set(mbedtls_SOURCE_DIR - ${CMAKE_CURRENT_LIST_DIR}/library - CACHE INTERNAL - "Path to Mbed TLS source code" -) +if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING) + add_subdirectory(library_mocks) +else () + set(mbedtls_SOURCE_DIR + ${CMAKE_CURRENT_LIST_DIR}/library + CACHE INTERNAL + "Path to Mbed TLS source code" + ) + # MbedTLS library configuration + # Programs and testing builds are not required + # Note: Mbed TLS specifies an old version of CMake which does not fully support + # overriding a CACHE variable with a regular one, so we need to use the CACHE + # keyword here. + set(ENABLE_PROGRAMS OFF CACHE BOOL "" FORCE) + set(ENABLE_TESTING OFF CACHE BOOL "" FORCE) -# MbedTLS library configuration -# Programs and testing builds are not required -# Note: Mbed TLS specifies an old version of CMake which does not fully support -# overriding a CACHE variable with a regular one, so we need to use the CACHE -# keyword here. -set(ENABLE_PROGRAMS OFF CACHE BOOL "" FORCE) -set(ENABLE_TESTING OFF CACHE BOOL "" FORCE) + add_subdirectory(library) -add_subdirectory(library) - -add_subdirectory(integration) + add_subdirectory(integration) +endif() diff --git a/components/security/mbedtls/library_mocks/CMakeLists.txt b/components/security/mbedtls/library_mocks/CMakeLists.txt new file mode 100644 index 00000000..4ef331aa --- /dev/null +++ b/components/security/mbedtls/library_mocks/CMakeLists.txt @@ -0,0 +1,17 @@ +# Copyright 2023-2024 Arm Limited and/or its affiliates +# +# SPDX-License-Identifier: MIT + +add_library(mbedtls-mock + src/psa/crypto.c +) + +target_include_directories(mbedtls-mock + PUBLIC + inc +) + +target_link_libraries(mbedtls-mock + PRIVATE + fff +) diff --git a/components/security/mbedtls/library_mocks/inc/psa/crypto.h b/components/security/mbedtls/library_mocks/inc/psa/crypto.h new file mode 100644 index 00000000..d07170b2 --- /dev/null +++ b/components/security/mbedtls/library_mocks/inc/psa/crypto.h @@ -0,0 +1,17 @@ +/* + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + */ + +#ifndef PSA_CRYPTO_H +#define PSA_CRYPTO_H + +#include "fff.h" + +#include "crypto_types.h" +#include +#include + +DECLARE_FAKE_VALUE_FUNC( psa_status_t, psa_generate_random, uint8_t *, size_t ); + +#endif /* PSA_CRYPTO_H */ diff --git a/components/security/mbedtls/library_mocks/inc/psa/crypto_types.h b/components/security/mbedtls/library_mocks/inc/psa/crypto_types.h new file mode 100644 index 00000000..1123f7f1 --- /dev/null +++ b/components/security/mbedtls/library_mocks/inc/psa/crypto_types.h @@ -0,0 +1,15 @@ +/* + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + */ + +#ifndef PSA_CRYPTO_TYPES_H +#define PSA_CRYPTO_TYPES_H + +#include + +#ifndef PSA_SUCCESS + typedef int32_t psa_status_t; +#endif + +#endif /* PSA_CRYPTO_TYPES_H */ diff --git a/components/security/mbedtls/library_mocks/src/psa/crypto.c b/components/security/mbedtls/library_mocks/src/psa/crypto.c new file mode 100644 index 00000000..ea0db0b4 --- /dev/null +++ b/components/security/mbedtls/library_mocks/src/psa/crypto.c @@ -0,0 +1,12 @@ +/* + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + */ + +#include "psa/crypto.h" +#include "psa/crypto_types.h" + +DEFINE_FAKE_VALUE_FUNC( psa_status_t, + psa_generate_random, + uint8_t *, + size_t ); diff --git a/components/security/trusted_firmware-m/CMakeLists.txt b/components/security/trusted_firmware-m/CMakeLists.txt index 82ac35f3..9a259c67 100644 --- a/components/security/trusted_firmware-m/CMakeLists.txt +++ b/components/security/trusted_firmware-m/CMakeLists.txt @@ -2,10 +2,15 @@ # # SPDX-License-Identifier: MIT + set(trusted_firmware-m_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/library CACHE INTERNAL "Path to Trusted Firmware-M source code" ) -add_subdirectory(integration) +if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING) + add_subdirectory(library_mocks) +else() + add_subdirectory(integration) +endif() diff --git a/components/security/trusted_firmware-m/library_mocks/CMakeLists.txt b/components/security/trusted_firmware-m/library_mocks/CMakeLists.txt new file mode 100644 index 00000000..1c3bd3e7 --- /dev/null +++ b/components/security/trusted_firmware-m/library_mocks/CMakeLists.txt @@ -0,0 +1,15 @@ +# Copyright 2023-2024 Arm Limited and/or its affiliates +# +# SPDX-License-Identifier: MIT + +add_library(trusted-firmware-m-mock INTERFACE) + +target_include_directories(trusted-firmware-m-mock + INTERFACE + inc +) + +target_link_libraries(trusted-firmware-m-mock + INTERFACE + fff +) diff --git a/components/security/trusted_firmware-m/library_mocks/inc/psa/crypto_types.h b/components/security/trusted_firmware-m/library_mocks/inc/psa/crypto_types.h new file mode 100644 index 00000000..7f059aa0 --- /dev/null +++ b/components/security/trusted_firmware-m/library_mocks/inc/psa/crypto_types.h @@ -0,0 +1,13 @@ +/* + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + */ + +#ifndef PSA_CRYPTO_TYPES_H +#define PSA_CRYPTO_TYPES_H + +#ifndef PSA_SUCCESS + typedef int32_t psa_status_t; +#endif + +#endif /* PSA_CRYPTO_TYPES_H */ diff --git a/components/security/trusted_firmware-m/library_mocks/inc/psa/error.h b/components/security/trusted_firmware-m/library_mocks/inc/psa/error.h new file mode 100644 index 00000000..aa0c20d9 --- /dev/null +++ b/components/security/trusted_firmware-m/library_mocks/inc/psa/error.h @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2019-2024, Arm Limited. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + * + */ + +#ifndef __PSA_ERROR_H__ +#define __PSA_ERROR_H__ + +#define PSA_SUCCESS ( ( psa_status_t ) 0 ) + +#define PSA_ERROR_PROGRAMMER_ERROR ( ( psa_status_t ) -129 ) + + +#endif /* __PSA_ERROR_H__ */ diff --git a/docs/unit_testing.md b/docs/unit_testing.md index 96660c62..dec9dc18 100644 --- a/docs/unit_testing.md +++ b/docs/unit_testing.md @@ -14,7 +14,7 @@ This tutorial will lay out the steps for adding unit tests for IoT Reference Int The IoT Reference Integration for Arm Corstone-3xx aims to provide mocked symbols for all components it fetches. This allows for unit tests to be written for the component's integration code as well as for other components's integration code that depend on it. -`coreMQTT Agent` and `FreeRTOS-Kernel` mocks will be used as examples for which symbols to mock and how to mock them. The mocks can be found [here](../components/aws_iot/coremqtt_agent/mocks) and [here](../components/freertos_kernel/mocks) respectively. +`coreMQTT Agent` and `FreeRTOS-Kernel` mocks will be used as examples for which symbols to mock and how to mock them. The mocks can be found [here](../components/aws_iot/coremqtt_agent/library_mocks) and [here](../components/freertos_kernel/library_mocks) respectively. The `coreMQTT Agent` mocks are used to write unit test for the `coreMQTT Agent`'s integration code. The unit test can be found [here](../components/aws_iot/coremqtt_agent/integration/tests/test_freertos_agent_message.cpp). @@ -29,7 +29,7 @@ Within the IoT Reference Integration for Arm Corstone-3xx, we unit test any inte A mock is defined by the user and is intended to simulate existing symbols from a component. This allows for the unit test to precisely control the execution path in the file under test for each test case. The mocks are defined in the `components` sub-directory for the component mocked. The sub-directory structure is as follows: ``` tree ├── ${REAL_LIBRARY_NAME}/ -│ ├── mocks/ +│ ├── library_mocks/ │ │ ├── inc/ │ │ ├── src/ │ │ ├── CMakeLists.txt @@ -50,7 +50,7 @@ For example, if a header file is included as such: The folder structure within the `inc` directory should include any sub-directory, where the header file is located. In this case, the structure would be as follows: ``` tree ├── component/ -│ ├── mocks/ +│ ├── library_mocks/ │ │ ├── inc/ │ │ | ├── component/ │ │ | | ├── header_1.h @@ -71,7 +71,7 @@ As the IoT Reference Integration for Arm Corstone-3xx utilises CMake as its buil To ensure the mocks directory is built only for unit testing, list it in CMake with a condition. The [coreMQTT Agent component CMake][inclusion-of-mocks-subdir] illustrates this: ```cmake if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING) - add_subdirectory(mocks) + add_subdirectory(library_mocks) else() endif() @@ -81,12 +81,12 @@ This ensures that the mock is included in the build only when `CMAKE_CROSSCOMPIL The CMAKE_CROSSCOMPILING flag is set by CMake when not building for the host machine (e.g. for Corstone-300). This is only disabled when we are building unit tests for our host machine and for CI. For future-proofing, we also use the BUILD_TESTING flag that is set by CTest. -The `mocks` directory must be added before the `tests` directory. This ensures the mocked CMake library target can be referenced in the test's `CMakeLists.txt` to link it. This subdirectory is linked in the component [main][inclusion-of-mocks-subdir] `CMakeLists.txt` file. +The `library_mocks` directory must be added before the `tests` directory. This ensures the mocked CMake library target can be referenced in the test's `CMakeLists.txt` to link it. This subdirectory is linked in the component [main][inclusion-of-mocks-subdir] `CMakeLists.txt` file. ### Mocked component library -The library for the mocked component should be added in the `CMakeLists.txt` within the `mocks` sub-directory. The name of the mocked component should follow the name of the CMake library where the real symbols normally are with the added `-mock` suffix. -Use the CMake `add_library()` function to create the CMake library target. Source files that implement the mocked symbols should be added to that library, as seen for the `FreeRTOS-Kernel Mocks` [here](../components/freertos_kernel/mocks). +The CMake library target for the mocked component should be added in the `CMakeLists.txt` within the `mocks` sub-directory. The name of the mocked component should follow the name of the CMake library where the real symbols normally are with the added `-mock` suffix. +Use the CMake `add_library()` function to create the CMake library target. Source files that implement the mocked symbols should be added to that library, as seen for the `FreeRTOS-Kernel Mocks` [here](../components/freertos_kernel/library_mocks). #### Example for adding CMake library @@ -122,6 +122,22 @@ target_link_libraries(${REAL_LIBRARY_NAME}-mock ) ``` +#### Example for adding CMake library (with no .c source files) + +```cmake +add_library(${REAL_LIBRARY_NAME}-mock INTERFACE) +target_include_directories(${REAL_LIBRARY_NAME}-mock + INTERFACE + inc +) +target_link_libraries(${REAL_LIBRARY_NAME}-mock + INTERFACE + fff + v +) +``` +In summary: replace every occurrence of the `PRIVATE` and `PUBLIC` access modifiers in the example above with `INTERFACE`. + #### Example for adding a CMake library which contains only header files The primary difference is that instead of using ```PRIVATE``` and ```PUBLIC``` modifiers, we use ```INTERFACE```. Additionally, ```add_library``` must indicate this library is only an interface. @@ -443,13 +459,64 @@ iot_reference_arm_corstone3xx_add_test(${FILE_UNDER_TEST_KEBAB_CASE}-test) * `target_link_libraries`: Links any library required to successfully build the executable. This includes `fff` as well as any required mocked CMake library. * `iot_reference_arm_corstone3xx_add_test`: A helper function to make the test suite discoverable by GoogleTest. -##### Handling dependencies in the same component as the file under test +#### The different types of mock directory + +We will walk through the mocks in `components/aws_iot/coremqtt_agent`. +The directory structure for mocks is: +``` tree +coremqtt_agent +├── library_mocks +├── integration +├── library +├── CMakeLists.txt +``` +This top-level contains the following elements: +- `library` is the third-party library code we want to isolate when unit testing. +- `library_mocks` contains mocks for the `library` components. It will contain a `CMakeLists.txt` file that defines the library mocks, which are exported as the library `coremqtt-agent-mock`. +- `CMakeLists.txt` is set to add `library_mocks` and `integration` as subdirectories if we are unit testing, otherwise it adds `integration` and `library`. +This assumes that `integration` contains mocks. Otherwise, `CMakeLists.txt` will not add `integration` if we are unit testing. +- `integration` has the following structure: +``` tree +coremqtt_agent / integration +├── inc +├── integration_mocks +├── src +├── tests +├── CMakeLists.txt +``` +These elements are summarized: +- `inc` and `src` contain the integration code we are testing. +- `integration_mocks` contains mocks for the integration code within `inc` and `src`. This is exported as `coremqtt-agent-integration-mock`. +We mock integration code because if two integration files depend on each other, we need to isolate them from each other during testing. Otherwise, bugs in one file could cause failures on the other file's tests. +- `tests` has the following structure: +``` tree +coremqtt_agent / integration / tests +├── config_mocks +├── CMakeLists.txt +├── test_freertos_agent_message.cpp +... other '.cpp' test files. +``` +The `config_mocks` folder in this directory contains mocks for config files in the `applications/` project root directory. For example, `applications/keyword_detection/configs/app_config/app_config.h` is mocked in this directory. This does not include mocks that are stored in `applications/helpers`, such as `applications/helpers/events/events.h`. These helper mocks are within `applications/helpers//mocks`, where `` could be `events`. + +To summarise, there are three separate `mocks` subdirectories. From top to bottom of the directory tree: +1. Library `library_mocks` for coremqtt_agent. +2. Integration `integration_mocks` for our coremqtt_agent integration code. +3. Application-specific `config_mocks`. +Additionally, some mocks for `applications/helpers` can be found within `applications/helpers//mocks`. For example, there are mocks in `applications/helpers/events/mocks/...`. -Assume that the file under test is dependent on a file in the same directory or component as it. This is illustrated below. +It is important to note that application mocks are only supported for helpers and config files, not for all application code. + +#### Handling dependencies in the same component as the file under test + +Assume that the file under test is dependent on a file in the same directory or component as it. We also assume that some other test file somewhere uses a mocked version of `${FILE_UNDER_TEST}`. This is illustrated below. ``` tree -├── mocks -│ ├── inc/${DEPENDENCY}.h -│ ├── src/${DEPENDENCY}.c +├── integration_mocks +│ ├── inc +│ │ ├── ${DEPENDENCY}.h +│ │ ├── ${FILE_UNDER_TEST}.h +│ ├── src +│ │ ├── ${DEPENDENCY}.c +│ │ ├── ${FILE_UNDER_TEST}.c ├── inc/ │ ├── ${FILE_UNDER_TEST}.h │ ├── ${DEPENDENCY}.h @@ -462,31 +529,12 @@ Assume that the file under test is dependent on a file in the same directory or ... ``` -This introduces complications to your CMakeLists.txt files. The problems are: -1. How can I include the integration `${FILE_UNDER_TEST}.c` but the mock `${DEPENDENCY}.c`? -2. How can I include the integration `${FILE_UNDER_TEST}.h` but the mock `${DEPENDENCY}.h`? - - -To include the `.h` files in the correct manner, we advise copy-and-pasting (as will be described shortly). However, if the `${FILE_UNDER_TEST}` does not have a mock created for it, it is possible to instead include `../../mocks/inc`, and `../inc` as follows: -``` -target_include_directories(mqtt-agent-task-test - PRIVATE - . - - # Include the mocks of header files of files belonging to - # this component so they can be mocked. - # This needs to be done before including ../inc - ../../mocks/inc - - ../inc -) -``` -The reason we include `../../mocks/inc` first is so that the include guards for the files in `../../mocks` are run first, so any guarded definitions in `../inc` are not included afterwards. - -The above breaks if you create a mock for `${FILE_UNDER_TEST}.h` at any point. This mock header will be detected first (and set include guards) when trying to test `${FILE_UNDER_TEST}.c`. Then the real header file `${FILE_UNDER_TEST}.h ` will not be included. So some definitions that are not in the mock header but are needed to test `${FILE_UNDER_TEST}.c` may be missing. +We want to test `src/${FILE_UNDER_TEST}.c`, using the real `inc/${FILE_UNDER_TEST}.h` but the mock `integration_mocks/inc/${DEPENDENCY}.h` and `integration_mocks/src/${DEPENDENCY}.c`. +It is not possible to do this easily using `CMakeLists.txt` and cover every use case. +You should not simply include this component's `-integration-mocks` library in `tests/CMakeLists.txt`, as this will cause `tests/test_${FILE_UNDER_TEST}.cpp` to find `integration_mocks/inc/${FILE_UNDER_TEST}.h` instead of the real `inc/${FILE_UNDER_TEST}.h`. -A more robust way of including the mock `${DEPENDENCY}.h` and `${DEPENDENCY}.c` files is to directly copy-and-paste them into the top of the C++ test file. -In our example, this means copy-and-pasting contents to the following location in `test_${FILE_UNDER_TEST}.cpp`: +The advised way of including the mock `integration_mocks/inc/${DEPENDENCY}.h` and `integration_mocks/src/${DEPENDENCY}.c` files is to directly copy-and-paste them into the top of the C++ test file. +In our example, this means copy-and-pasting contents to the following location in `tests/test_${FILE_UNDER_TEST}.cpp`: ```c++ #include "fff.h" @@ -503,9 +551,9 @@ extern "C" { /* Directly copy-paste mock headers from the file under test's directory. Otherwise, the non-mock files are detected. */ - /* Contents of `${DEPENDENCY}.h` */ + /* Contents of `integration_mocks/inc/${DEPENDENCY}.h` */ - /* Contents of `${DEPENDENCY}.c` */ + /* Contents of `integration_mocks/src/${DEPENDENCY}.c` */ } @@ -515,6 +563,60 @@ DEFINE_FFF_GLOBALS ``` +#### Exposing static functions for testing + +Some files in the repository contain static functions which need to be tested. See `components/aws_iot/coremqtt_agent/integration/src/mqtt_agent_task.c` for an example. + +The advised method of exposing these functions for testing is as follows: + +1. Define the following STATIC macro. +```c +#ifdef UNIT_TESTING + #define STATIC /* as nothing */ +#else /* ifdef UNIT_TESTING */ + #define STATIC static +#endif /* UNIT_TESTING */ +``` +2. Replace the `static` scope of the functions under test with `STATIC`. +```c +static int dummy ( void ) { + return 1; +} +``` +Now becomes +```c +STATIC int dummy ( void ) { + return 1; +} +``` +3. In the C++ test file, `extern` the function definitions. Also ensure that `${FILE_UNDER_TEST}.h` is included. +For example: +```cpp +/* ... other contents ... */ + +#include "${FILE_UNDER_TEST}.h" + +/* ... other contents ... */ + +using namespace std; + +extern "C" { + +/* ... other contents ... */ + + extern int dummy ( void ); + +/* ... other contents ... */ + +} + +DEFINE_FFF_GLOBALS + +/* ... other contents ... */ +``` + +These static functions are now only visible to the file, and any unit testing files that explicitly `extern` them. + ## Building and running unit tests Two commands will be required to build and run the unit tests. The first step is to configure the unit test build directory, this is done with the following CMake command: @@ -535,7 +637,7 @@ This will build and then run the tests. You will see an output for each test, wi [creating-mocked-component-cmake-target]:#creating-mocked-component-cmake-target-library [what-is-a-mock]:#what-is-a-mock [coremqtt-agent-message-interface-header-real]:https://github.com/FreeRTOS/coreMQTT-Agent/blob/main/source/include/core_mqtt_agent_message_interface.h -[coremqtt-agent-message-interface-header-mock]: ../components/aws_iot/coremqtt_agent/mocks/inc/core_mqtt_agent_message_interface.h +[coremqtt-agent-message-interface-header-mock]: ../components/aws_iot/coremqtt_agent/library_mocks/inc/core_mqtt_agent_message_interface.h [fff-cheat-sheet]:https://github.com/meekrosoft/fff#cheat-sheet [mocking-functions]:#mocking-functions [google-test]: https://github.com/google/googletest/tree/main diff --git a/release_changes/202407311009.change b/release_changes/202407311009.change new file mode 100644 index 00000000..43c57c77 --- /dev/null +++ b/release_changes/202407311009.change @@ -0,0 +1,2 @@ +coremqtt-agent-unit-test: Add tests for mqtt_agent_task.c. +coremqtt-agent-task: Debug mqtt_agent_task.c diff --git a/tools/cmake/AddUnitTest.cmake b/tools/cmake/AddUnitTest.cmake index 94abff2e..26a162fd 100644 --- a/tools/cmake/AddUnitTest.cmake +++ b/tools/cmake/AddUnitTest.cmake @@ -11,6 +11,11 @@ if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING) GTest::gtest_main ) + target_compile_definitions (${target} + PRIVATE + UNIT_TESTING + ) + gtest_discover_tests(${target}) endfunction() endif()