Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Adds lib/support/tests to the ESP32 unit-test target. Makes platform-specific fixes to ParseArgs and EncodeTlvElement. #36847

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,21 @@
}
},
{
"label": "QEMU: run esp32-qemu unit tests",
"label": "Build esp32-qemu unit tests",
"type": "shell",
"command": "scripts/tests/esp32_qemu_tests.sh /tmp/test_logs",
"command": "source scripts/activate.sh; src/test_driver/esp32/clean.sh; scripts/build/build_examples.py --target esp32-qemu-tests build",
"problemMatcher": []
},
{
"label": "Clean esp32-qemu unit tests",
"type": "shell",
"command": "src/test_driver/esp32/clean.sh",
"problemMatcher": []
},
{
"label": "Run esp32-qemu unit tests",
"type": "shell",
"command": "src/test_driver/esp32/run_qemu_image.py --verbose --file-image-list ./out/esp32-qemu-tests/test_images.txt",
"problemMatcher": []
},
{
Expand Down
5 changes: 5 additions & 0 deletions src/lib/support/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ static_library("support") {
if (chip_config_memory_debug_dmalloc) {
libs += [ "dmallocthcxx" ]
}

if (chip_device_platform == "esp32" || chip_device_platform == "openiotsdk") {
# Has non-posix implementation of getopt_long.
defines = [ "CONFIG_NON_POSIX_GETOPT_LONG" ]
}
}

source_set("test_utils") {
Expand Down
242 changes: 188 additions & 54 deletions src/lib/support/CHIPArgParser.cpp

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions src/lib/support/CHIPArgParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@
#include <stdio.h>
#include <stdlib.h>

#ifndef CHIP_CONFIG_NON_POSIX_LONG_OPT
#define CHIP_CONFIG_NON_POSIX_LONG_OPT 0
#endif

namespace chip {
namespace ArgParser {

Expand Down
6 changes: 6 additions & 0 deletions src/lib/support/jsontlv/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import("//build_overrides/chip.gni")
import("//build_overrides/jsoncpp.gni")
import("${chip_root}/src/platform/device.gni")

config("jsontlv_config") {
}
Expand Down Expand Up @@ -42,4 +43,9 @@ static_library("jsontlv") {
]

cflags = [ "-Wconversion" ]

if (chip_device_platform == "esp32") {
# malloc(0) is null on this platform.
defines = [ "CONFIG_MALLOC_0_IS_NULL" ]
}
}
8 changes: 7 additions & 1 deletion src/lib/support/jsontlv/JsonToTlv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,13 @@ CHIP_ERROR EncodeTlvElement(const Json::Value & val, TLV::TLVWriter & writer, co

Platform::ScopedMemoryBuffer<uint8_t> byteString;
byteString.Alloc(BASE64_MAX_DECODED_LEN(static_cast<uint16_t>(encodedLen)));
VerifyOrReturnError(byteString.Get() != nullptr, CHIP_ERROR_NO_MEMORY);

// On a platform where malloc(0) is null (which could be misinterpreted as "out of memory")
// we should skip this check if it's a zero-length string.
#ifdef CONFIG_MALLOC_0_IS_NULL
if (encodedLen > 0)
#endif
VerifyOrReturnError(byteString.Get() != nullptr, CHIP_ERROR_NO_MEMORY);

auto decodedLen = Base64Decode(valAsString.c_str(), static_cast<uint16_t>(encodedLen), byteString.Get());
VerifyOrReturnError(decodedLen < UINT16_MAX, CHIP_ERROR_INVALID_ARGUMENT);
Expand Down
1 change: 1 addition & 0 deletions src/test_driver/esp32/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ esp32_unit_test(NAME testMinimalMdnsResponders LIBRARY MinimalMdnsRespondersTest
esp32_unit_test(NAME testMdns LIBRARY MdnsTests)
esp32_unit_test(NAME testRetransmit LIBRARY RetransmitTests)
esp32_unit_test(NAME testSetupPayload LIBRARY SetupPayloadTests)
esp32_unit_test(NAME testSupport LIBRARY SupportTests)
esp32_unit_test(NAME testSystemLayer LIBRARY SystemLayerTests)
esp32_unit_test(NAME testUserDirectedCommissioning LIBRARY UserDirectedCommissioningTests)

Expand Down
2 changes: 1 addition & 1 deletion src/test_driver/esp32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ To build all unit tests:
$ source scripts/activate.sh
$ scripts/build/build_examples.py --target esp32-qemu-tests build

This generates a list of QEMU images in `out/esp32-qemu-tests/`
This generates a set of QEMU images in `out/esp32-qemu-tests/`

There is one image for each test directory (i.e. each chip_test_suite). So for
example `src/inet/tests` builds to `out/esp32-qemu-tests/testInetLayer.img`
Expand Down
25 changes: 25 additions & 0 deletions src/test_driver/esp32/clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

#
# Copyright (c) 2024 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.
#

#
# This script cleans the files generated by building the unit tests.
#

set -ex

rm -rf out/esp32-qemu-tests src/test_driver/esp32/managed_components src/test_driver/esp32/sdkconfig src/test_driver/esp32/dependencies.lock
Copy link
Contributor

@shripad621git shripad621git Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get rid of this file by directly calling the cleanup command in the example directory either using the above command or idf.py fullclean?

Copy link
Contributor Author

@feasel0 feasel0 Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing idf.py fullclean removes managed_components but not the build output since it's looking for src/test_driver/esp32/build subdirectory, whereas the output is actually in out/<target-name>. It also does not remove dependencies.lock and sdkconfig.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

// Enable support functions for parsing command-line arguments
#define CHIP_CONFIG_ENABLE_ARG_PARSER 1
#define CHIP_CONFIG_NON_POSIX_LONG_OPT 1

// Enable building for unit testing
#define CONFIG_BUILD_FOR_HOST_UNIT_TEST 1
Loading