Skip to content

Commit

Permalink
Add unit test support for Mbed OS (#8403)
Browse files Browse the repository at this point in the history
* Register missing tests:
- TestChannel
- TestExchangeManager
- TestReliableMessageProtocol

* Add output name and dir for mdns libraries

* Prevent inclusion of main from TestInetLayer in Mbed library

* Prevent build of stdio logging on Mbed OS

* Add Mbed TLS SHA1 support for Mbed.

This is needed by some of the unit tests.

* Disable argparser test for Mbed OS

getopt behavior with newlib is different from _standard_ on desktop.

* Add missing header to QRCodeTLV test

* Prevent inclusion of cpp files in payload test.

The functions tested are not marked as static but aren't exposed
publicly either. Forward declaration is used in tests to import them.

* Add Mbed OS test driver.

* Add debug and build script for Mbed OS test driver

* Improve Mbed OS timing primitive precisions.

Base primitive used was the OS tick which changes every ms. Instead
use a low power timer to improve the accuracy to the us range.

* Proper shutdown of Mbed OS platform manager

* Update Mbed OS dependencies.

* Platform Manager fixes (#189)

* Fix PlatformManagerImple and BLEMangerImpl
Add double BLE initialization protection
Improve stop and start event loop

* Address inet layer test inclusion

* Increase number of timer in test to prevent exhaustion.

* Add workflow to build mbed os unit test

* Move Mbed unit test build script in dedicated folder.

This helps when integration tests scripts are added.

* Increase Mbed OS unit test stack size.
Help coping with SecureSession tests.

* Revert changes in TestHelpers.h
Revert changes in mdns BUILD.gn files
Move function declaration from TestManualCode.cpp to ManualSetupPayloadParser.h
Cleanup mbed unit test CMakeLists.txt file

* Capitalize ManualSetupPayloadParser functions names
Fix TestHelper global variables types
Add extractPayload function to header file and capitalize the name

* Add unit-test buidling to Build example - Mbed OS workflow
Remove Build unit test - Mbed OS workflow

* Fix unit tests binary file name

* Fix multiple definition of chip::Platform:PersistedStorage::Read/Write

* Changes restyle

* Fix TestMdnsCache - use usleep intead of sleep

* Udpate unit tests app with a new logging approach

* Add all flash and debug options for unit tests app in launch.json

* Restore setting print_lib in mbed_app.json

* Mbed unit tests CHIPProjectConfig.h cleanup

* Change files expose from file static to class static

* Add detach function to Mbed event timeout class
Fix Mbed event queue shutdown in Platform Manager

* Fix using SystemLayerSocketsLoop in PlatformManagerImpl

* Fix controller tests - not using data_model for mbed platform

* Restyle changes

* Remove unit test artifacts from mbed-os workflow

Co-authored-by: ATmobica <[email protected]>
Co-authored-by: ATmobica <[email protected]>
  • Loading branch information
3 people authored Oct 6, 2021
1 parent 98568b3 commit d48d754
Show file tree
Hide file tree
Showing 42 changed files with 731 additions and 132 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/examples-mbed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ jobs:
examples/lighting-app/mbed/build-CY8CPROTO_062_4343W/release/chip-mbed-lighting-app-example \
/tmp/bloat_reports/
- name: Build unit tests
timeout-minutes: 10
run: scripts/tests/mbed/mbed_unit_tests.sh -b=$APP_TARGET -p=$APP_PROFILE

- name: Copy aside build products
run: |
mkdir -p /tmp/output_binaries/$BUILD_TYPE-build
Expand All @@ -104,7 +108,7 @@ jobs:
if: ${{ !env.ACT }}
with:
name:
${{ env.BUILD_TYPE }}-binaries-${{env.APP_TARGET}}-${{ env.APP_PROFILE}}-build-${{
${{ env.BUILD_TYPE }}-binaries-${{ env.APP_TARGET }}-${{ env.APP_PROFILE }}-build-${{
steps.outsuffix.outputs.value }}
path: /tmp/output_binaries/${{ env.BUILD_TYPE }}-build

Expand Down
119 changes: 114 additions & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,14 @@
},

{
"name": "Debug Mbed CY8CPROTO_062_4343W",
"name": "Debug Mbed examples CY8CPROTO_062_4343W",
"type": "cortex-debug",
"request": "launch",
"cwd": "${workspaceRoot}/examples/${input:mbedApp}/mbed",
"executable": "./build-CY8CPROTO_062_4343W/${input:mbedDebugProfile}/chip-mbed-${input:mbedApp}-example",
"servertype": "openocd",
"openocdPath": "${env:OPENOCD_PATH/bin}",
"armToolchainPath": "${env:MBED_GCC_ARM_PATH}",
"searchDir": [
"${workspaceRoot}/config/mbed/scripts",
"${env:OPENOCD_PATH/scripts}"
Expand All @@ -117,12 +119,13 @@
},

{
"name": "Debug Mbed CY8CPROTO_062_4343W [remote]",
"name": "Debug Mbed examples CY8CPROTO_062_4343W [remote]",
"type": "cortex-debug",
"request": "launch",
"cwd": "${workspaceRoot}/examples/${input:mbedApp}/mbed",
"executable": "./build-CY8CPROTO_062_4343W/${input:mbedDebugProfile}/chip-mbed-${input:mbedApp}-example",
"servertype": "external",
"armToolchainPath": "${env:MBED_GCC_ARM_PATH}",
"gdbTarget": "host.docker.internal:3334", //port 3333 for the CM0+, 3334 for the CM4
"overrideLaunchCommands": [
"-enable-pretty-printing",
Expand All @@ -143,12 +146,14 @@
},

{
"name": "Flash Mbed CY8CPROTO_062_4343W",
"name": "Flash Mbed examples CY8CPROTO_062_4343W",
"type": "cortex-debug",
"request": "launch",
"cwd": "${workspaceRoot}/examples/${input:mbedApp}/mbed",
"executable": "./build-CY8CPROTO_062_4343W/${input:mbedFlashProfile}/chip-mbed-${input:mbedApp}-example",
"servertype": "openocd",
"openocdPath": "${env:OPENOCD_PATH/bin}",
"armToolchainPath": "${env:MBED_GCC_ARM_PATH}",
"searchDir": [
"${workspaceRoot}/config/mbed/scripts",
"${env:OPENOCD_PATH/scripts}"
Expand All @@ -166,12 +171,13 @@
},

{
"name": "Flash Mbed CY8CPROTO_062_4343W [remote]",
"name": "Flash Mbed examples CY8CPROTO_062_4343W [remote]",
"type": "cortex-debug",
"request": "launch",
"cwd": "${workspaceRoot}/examples/${input:mbedApp}/mbed",
"executable": "./build-CY8CPROTO_062_4343W/${input:mbedFlashProfile}/chip-mbed-${input:mbedApp}-example.hex",
"executable": "./build-CY8CPROTO_062_4343W/${input:mbedFlashProfile}/chip-mbed-${input:mbedApp}-example",
"servertype": "external",
"armToolchainPath": "${env:MBED_GCC_ARM_PATH}",
"gdbTarget": "host.docker.internal:3334", //port 3333 for the CM0+, 3334 for the CM4
"overrideLaunchCommands": [
"monitor reset halt",
Expand All @@ -180,6 +186,109 @@
"quit"
],
"showDevDebugOutput": false // When set to true, displays output of GDB.
},

{
"name": "Debug Mbed Unit Tests CY8CPROTO_062_4343W",
"type": "cortex-debug",
"request": "launch",
"cwd": "${workspaceRoot}/src/test_driver/mbed",
"executable": "./build-CY8CPROTO_062_4343W/${input:mbedDebugProfile}/chip-mbed-unit-tests",
"servertype": "openocd",
"openocdPath": "${env:OPENOCD_PATH/bin}",
"armToolchainPath": "${env:MBED_GCC_ARM_PATH}",
"searchDir": [
"${workspaceRoot}/config/mbed/scripts",
"${env:OPENOCD_PATH}/scripts"
],
"configFiles": ["CY8CPROTO_062_4343W.tcl"],
"overrideLaunchCommands": [
"-enable-pretty-printing",
"monitor program {./build-CY8CPROTO_062_4343W/${input:mbedDebugProfile}/chip-mbed-unit-tests.hex}",
"monitor reset run",
"monitor sleep 200",
"monitor psoc6 reset_halt sysresetreq"
],
"numberOfProcessors": 2,
"targetProcessor": 1, // Set to 0 for the CM0+, set to 1 for the CM4
"overrideRestartCommands": [
"monitor reset init",
"monitor reset run",
"monitor sleep 200",
"monitor psoc6 reset_halt sysresetreq"
],
"runToMain": true, // if true, program will halt at main. Not used for a restart
"showDevDebugOutput": false // When set to true, displays output of GDB.
},

{
"name": "Debug Mbed Unit Tests CY8CPROTO_062_4343W [remote]",
"type": "cortex-debug",
"request": "launch",
"cwd": "${workspaceRoot}/src/test_driver/mbed",
"executable": "./build-CY8CPROTO_062_4343W/${input:mbedDebugProfile}/chip-mbed-unit-tests",
"servertype": "external",
"armToolchainPath": "${env:MBED_GCC_ARM_PATH}",
"gdbTarget": "host.docker.internal:3334", //port 3333 for the CM0+, 3334 for the CM4
"overrideLaunchCommands": [
"-enable-pretty-printing",
"monitor reset halt",
"load ./build-CY8CPROTO_062_4343W/${input:mbedDebugProfile}/chip-mbed-unit-tests.hex",
"monitor reset run",
"monitor sleep 200",
"monitor psoc6 reset_halt sysresetreq"
],
"overrideRestartCommands": [
"monitor reset init",
"monitor reset run",
"monitor sleep 200",
"monitor psoc6 reset_halt sysresetreq"
],
"runToMain": true, // if true, program will halt at main. Not used for a restart
"showDevDebugOutput": false // When set to true, displays output of GDB.
},

{
"name": "Flash Mbed Unit Tests CY8CPROTO_062_4343W",
"type": "cortex-debug",
"request": "launch",
"cwd": "${workspaceRoot}/src/test_driver/mbed",
"executable": "./build-CY8CPROTO_062_4343W/${input:mbedFlashProfile}/chip-mbed-unit-tests",
"servertype": "openocd",
"openocdPath": "${env:OPENOCD_PATH/bin}",
"armToolchainPath": "${env:MBED_GCC_ARM_PATH}",
"searchDir": [
"${workspaceRoot}/config/mbed/scripts",
"${env:OPENOCD_PATH/scripts}"
],
"configFiles": ["CY8CPROTO_062_4343W.tcl"],
"overrideLaunchCommands": [
"monitor reset halt",
"monitor program {./build-CY8CPROTO_062_4343W/${input:mbedFlashProfile}/chip-mbed-unit-tests}",
"monitor reset run",
"quit"
],
"numberOfProcessors": 2,
"targetProcessor": 1, // Set to 0 for the CM0+, set to 1 for the CM4
"showDevDebugOutput": false // When set to true, displays output of GDB.
},

{
"name": "Flash Mbed Unit Tests CY8CPROTO_062_4343W [remote]",
"type": "cortex-debug",
"request": "launch",
"cwd": "${workspaceRoot}/src/test_driver/mbed",
"executable": "./build-CY8CPROTO_062_4343W/${input:mbedFlashProfile}/chip-mbed-unit-tests",
"servertype": "external",
"armToolchainPath": "${env:MBED_GCC_ARM_PATH}",
"gdbTarget": "host.docker.internal:3334", //port 3333 for the CM0+, 3334 for the CM4
"overrideLaunchCommands": [
"monitor reset halt",
"load ./build-CY8CPROTO_062_4343W/${input:mbedFlashProfile}/chip-mbed-unit-tests.hex",
"monitor reset run",
"quit"
],
"showDevDebugOutput": false // When set to true, displays output of GDB.
}
],
"inputs": [
Expand Down
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
{
"label": "Run Mbed Unit Tests",
"type": "shell",
"command": "scripts/tests/mbed_tests.sh",
"command": "scripts/tests/mbed/mbed_unit_tests.sh",
"args": [
"-c=${input:mbedCommand}",
"-b=${input:mbedTarget}",
Expand Down
2 changes: 1 addition & 1 deletion build/chip/chip_test_suite.gni
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ template("chip_test_suite") {
# Tests such as TestInetEndPoint need to exercise the system event
# loop however they do not seem to include a logging binding so this
# is forcefully added here.
if (current_os != "zephyr") {
if (current_os != "zephyr" && current_os != "mbed") {
public_deps += [ "${chip_root}/src/platform/logging:stdio" ]
}
}
Expand Down
1 change: 1 addition & 0 deletions config/mbed/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ chip_gn_arg_string("mbed_cxx" ${CMAKE_CXX_COMPILER})
chip_gn_arg_string("chip_project_config_include" "${CHIP_PROJECT_CONFIG}")
chip_gn_arg_bool ("is_debug" CONFIG_DEBUG)
chip_gn_arg_bool ("chip_build_tests" CONFIG_CHIP_BUILD_TESTS)
chip_gn_arg_bool ("chip_monolithic_tests" CONFIG_CHIP_BUILD_TESTS)
chip_gn_arg_bool ("chip_build_libshell" CONFIG_CHIP_LIB_SHELL)
chip_gn_arg_bool ("chip_with_platform_mbedtls" CONFIG_CHIP_WITH_EXTERNAL_MBEDTLS)
chip_gn_arg_bool ("chip_bypass_rendezvous" CONFIG_CHIP_BYPASS_RENDEZVOUS)
Expand Down
27 changes: 1 addition & 26 deletions config/mbed/chip-gn/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,7 @@ group("mbed") {
deps = [ "${chip_root}/src/lib" ]

if (chip_build_tests) {
# TODO: Enable all the tests. Use the src:tests target.
# deps += [ "${chip_root}/src:tests" ]
deps += [
"${chip_root}/src/app/tests",
"${chip_root}/src/ble/tests",
"${chip_root}/src/credentials/tests",
"${chip_root}/src/crypto/tests",
"${chip_root}/src/inet/tests",
"${chip_root}/src/lib/asn1/tests",
"${chip_root}/src/lib/core/tests",
"${chip_root}/src/lib/mdns/minimal/core/tests",
"${chip_root}/src/lib/mdns/minimal/records/tests",
"${chip_root}/src/lib/mdns/minimal/responders/tests",
"${chip_root}/src/lib/mdns/minimal/tests",

# "${chip_root}/src/lib/support/tests",
"${chip_root}/src/messaging/tests",
"${chip_root}/src/platform/tests",
"${chip_root}/src/protocols/bdx/tests",
"${chip_root}/src/setup_payload/tests",
"${chip_root}/src/system/tests",

# "${chip_root}/src/transport/raw/tests",
"${chip_root}/src/transport/retransmit/tests",
"${chip_root}/src/transport/tests",
]
deps += [ "${chip_root}/src:tests" ]
}
}

Expand Down
1 change: 1 addition & 0 deletions config/mbed/mbedtls/chip_mbedtls_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#define MBEDTLS_PEM_WRITE_C
#define MBEDTLS_NO_PLATFORM_ENTROPY
#define MBEDTLS_PKCS5_C
#define MBEDTLS_SHA1_C
#undef MBEDTLS_NET_C
#undef MBEDTLS_TIMING_C
#undef MBEDTLS_FS_IO
Expand Down
125 changes: 125 additions & 0 deletions scripts/tests/mbed/mbed_unit_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#!/usr/bin/env bash

#
# Copyright (c) 2021 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

cd "$(dirname "$0")/../../.."
CHIP_ROOT="$PWD"
cd "$CHIP_ROOT/src/test_driver/mbed/"

SUPPORTED_TOOLCHAIN=(GCC_ARM ARM)
SUPPORTED_TARGET_BOARD=(CY8CPROTO_062_4343W)
SUPPORTED_PROFILES=(release develop debug)
SUPPORTED_COMMAND=(build flash build-flash)

TARGET_BOARD=CY8CPROTO_062_4343W
TOOLCHAIN=GCC_ARM
PROFILE=release
COMMAND=build

for i in "$@"; do
case $i in
-b=* | --board=*)
TARGET_BOARD="${i#*=}"
shift
;;
-t=* | --toolchain=*)
TOOLCHAIN="${i#*=}"
shift
;;
-p=* | --profile=*)
PROFILE="${i#*=}"
shift
;;
-c=* | --command=*)
COMMAND="${i#*=}"
shift
;;
*)
# unknown option
;;
esac
done

if [[ ! " ${SUPPORTED_TARGET_BOARD[@]} " =~ " ${TARGET_BOARD} " ]]; then
echo "ERROR: Target $TARGET_BOARD not supported"
exit 1
fi

if [[ ! " ${SUPPORTED_TOOLCHAIN[@]} " =~ " ${TOOLCHAIN} " ]]; then
echo "ERROR: Toolchain $TOOLCHAIN not supported"
exit 1
fi

if [[ ! " ${SUPPORTED_PROFILES[@]} " =~ " ${PROFILE} " ]]; then
echo "ERROR: Profile $PROFILE not supported"
exit 1
fi

if [[ ! " ${SUPPORTED_COMMAND[@]} " =~ " ${COMMAND} " ]]; then
echo "ERROR: Command $COMMAND not supported"
exit 1
fi

# Activate Matter environment
source "$CHIP_ROOT"/scripts/activate.sh

BUILD_DIRECTORY="build-$TARGET_BOARD/$PROFILE/"

set -e # Exit immediately if a command exits with a non-zero status.
# set -x # Print commands and their arguments as they are executed.

if [[ "$COMMAND" == *"build"* ]]; then
echo "Build $APP app for $TARGET_BOARD target with $TOOLCHAIN toolchain and $PROFILE profile"

# Config directory setup
MBED_CONFIG_PATH=./cmake_build/"$TARGET_BOARD"/develop/"$TOOLCHAIN"/

# Override Mbed OS path to development directory
MBED_OS_PATH="$CHIP_ROOT"/third_party/mbed-os/repo

# Create symlinks to mbed-os submodule
ln -sfTr "$MBED_OS_PATH" "mbed-os"

# Create symlinks to mbed-os-posix-socket submodule
MBED_OS_POSIX_SOCKET_PATH="$CHIP_ROOT"/third_party/mbed-os-posix-socket/repo
ln -sfTr "$MBED_OS_POSIX_SOCKET_PATH" "mbed-os-posix-socket"

# Generate config file for selected target, toolchain and hardware
mbed-tools configure -t "$TOOLCHAIN" -m "$TARGET_BOARD"

# Remove old artifacts to force linking
rm -rf "$BUILD_DIRECTORY/chip-"*

# Create output directory and copy config file there.
mkdir -p "$BUILD_DIRECTORY"
cp -f "$MBED_CONFIG_PATH"/mbed_config.cmake "$BUILD_DIRECTORY"/mbed_config.cmake

# Build application
cmake -S "./" -B "$BUILD_DIRECTORY" -GNinja -DCMAKE_BUILD_TYPE="$PROFILE"
cmake --build "$BUILD_DIRECTORY"
fi

if [[ "$COMMAND" == *"flash"* ]]; then

echo "Flash Unit Tests app to $TARGET_BOARD target [$TOOLCHAIN toolchain, $PROFILE profile]"

# Flash scripts path setup
MBED_FLASH_SCRIPTS_PATH=$CHIP_ROOT/config/mbed/scripts

# Flash application
openocd -f "$MBED_FLASH_SCRIPTS_PATH/$TARGET_BOARD".tcl -c "program $BUILD_DIRECTORY/chip-tests verify reset exit"
fi
Loading

0 comments on commit d48d754

Please sign in to comment.