diff options
author | Raul E Rangel <rrangel@chromium.org> | 2019-05-03 12:48:41 -0600 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2019-05-15 13:36:18 -0700 |
commit | aaba1d5efd51082d143ce2ac64e6caf9cb14d5e5 (patch) | |
tree | 2f270d3141f77192078c6578ef78648491477779 /test | |
parent | 1598a615eb150916ea323e212b87298c3a6fcfb6 (diff) | |
download | chrome-ec-aaba1d5efd51082d143ce2ac64e6caf9cb14d5e5.tar.gz |
ec: common: Make IS_ENABLED fail on unknown values
If IS_ENABLED is called with any unknown values, a compiler error will be
thrown. This change requires that the optimizer always be enabled,
otherwise errors will be thrown when a value is not defined.
BUG=none
BRANCH=none
TEST=make runtests TEST_LIST_HOST="is_enabled_error is_enabled"
Change-Id: I1b166311f81d07e48b3665f4bc0e9502d2ccc4c6
Signed-off-by: Raul E Rangel <rrangel@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1592728
Reviewed-by: Jett Rink <jettrink@chromium.org>
Reviewed-by: Jack Rosenthal <jrosenth@chromium.org>
Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
Diffstat (limited to 'test')
-rw-r--r-- | test/build.mk | 6 | ||||
-rw-r--r-- | test/is_enabled.c | 35 | ||||
-rw-r--r-- | test/is_enabled.tasklist | 9 | ||||
-rw-r--r-- | test/is_enabled_error.c | 27 | ||||
-rw-r--r-- | test/is_enabled_error.sh | 40 | ||||
-rw-r--r-- | test/is_enabled_error.tasklist | 9 |
6 files changed, 126 insertions, 0 deletions
diff --git a/test/build.mk b/test/build.mk index b5b6e8bbea..b668d1d06d 100644 --- a/test/build.mk +++ b/test/build.mk @@ -36,6 +36,8 @@ test-list-host += hooks test-list-host += host_command test-list-host += inductive_charging test-list-host += interrupt +test-list-host += is_enabled +test-list-host += is_enabled_error test-list-host += kb_8042 test-list-host += kb_mkbp test-list-host += kb_scan @@ -101,6 +103,7 @@ host_command-y=host_command.o inductive_charging-y=inductive_charging.o interrupt-scale=10 interrupt-y=interrupt.o +is_enabled-y=is_enabled.o kb_8042-y=kb_8042.o kb_mkbp-y=kb_mkbp.o kb_scan-y=kb_scan.o @@ -152,3 +155,6 @@ TPM2_ROOT := $(CROS_WORKON_SRCROOT)/src/third_party/tpm2 $(out)/RO/common/new_nvmem.o: CFLAGS += -I$(TPM2_ROOT) $(out)/RO/test/nvmem.o: CFLAGS += -I$(TPM2_ROOT) $(out)/RO/test/nvmem_tpm2_mock.o: CFLAGS += -I$(TPM2_ROOT) + +host-is_enabled_error: TEST_SCRIPT=is_enabled_error.sh +is_enabled_error-y=is_enabled_error.o.cmd diff --git a/test/is_enabled.c b/test/is_enabled.c new file mode 100644 index 0000000000..c994d84b45 --- /dev/null +++ b/test/is_enabled.c @@ -0,0 +1,35 @@ +/* Copyright 2019 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + * Test the IS_ENABLED macro. + */ +#include "common.h" +#include "test_util.h" + +#undef CONFIG_UNDEFINED +#define CONFIG_BLANK + +static int test_undef(void) +{ + TEST_ASSERT(IS_ENABLED(CONFIG_UNDEFINED) == 0); + + return EC_SUCCESS; +} + +static int test_blank(void) +{ + TEST_ASSERT(IS_ENABLED(CONFIG_BLANK) == 1); + + return EC_SUCCESS; +} + +void run_test(void) +{ + test_reset(); + + RUN_TEST(test_undef); + RUN_TEST(test_blank); + + test_print_result(); +} diff --git a/test/is_enabled.tasklist b/test/is_enabled.tasklist new file mode 100644 index 0000000000..b97ab17500 --- /dev/null +++ b/test/is_enabled.tasklist @@ -0,0 +1,9 @@ +/* Copyright (c) 2019 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/** + * See CONFIG_TASK_LIST in config.h for details. + */ +#define CONFIG_TEST_TASK_LIST /* No test task */ diff --git a/test/is_enabled_error.c b/test/is_enabled_error.c new file mode 100644 index 0000000000..2f17925337 --- /dev/null +++ b/test/is_enabled_error.c @@ -0,0 +1,27 @@ +/* Copyright 2019 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + * Test the IS_ENABLED macro fails on unexpected input. + */ +#include "common.h" +#include "test_util.h" + +#define CONFIG_VALUE TEST_VALUE + +static int test_invalid_value(void) +{ + /* This will cause a compilation error */ + TEST_ASSERT(IS_ENABLED(CONFIG_VALUE) == 0); + + return EC_ERROR_UNKNOWN; +} + +void run_test(void) +{ + test_reset(); + + RUN_TEST(test_invalid_value); + + test_print_result(); +} diff --git a/test/is_enabled_error.sh b/test/is_enabled_error.sh new file mode 100644 index 0000000000..1e5407f31f --- /dev/null +++ b/test/is_enabled_error.sh @@ -0,0 +1,40 @@ +#!/bin/bash -e +# Copyright 2019 The Chromium OS Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +TEST_DIR="$(dirname "${BASH_SOURCE[0]}")" + +TEST_CMD="$(cat "${TEST_DIR}/RO/test/is_enabled_error.o.cmd")" + +TEST_ERROR_COUNT=0 + +for test_value in 0 1 2 A "5 + 5"; do + echo -n "Running TEST_VALUE=${test_value}..." + TEST_CMD_COMPLETE="${TEST_CMD} \"-DTEST_VALUE=${test_value}\"" + if BUILD_OUTPUT="$(sh -c "$TEST_CMD_COMPLETE" 2>&1)"; then + echo "Fail" + echo "Compilation should not have succeeded for" \ + "TEST_VALUE=${test_value}" + echo "$BUILD_OUTPUT" + TEST_ERROR_COUNT=$((TEST_ERROR_COUNT+1)) + continue + fi + + EXPECTED_ERROR="CONFIG_VALUE must be <blank>, or not defined" + if grep -q "$EXPECTED_ERROR" <<< "$BUILD_OUTPUT"; then + echo "OK" + else + echo "Fail" + echo "Expected to find: $EXPECTED_ERROR" + echo "Actual error:" + echo "$BUILD_OUTPUT" + TEST_ERROR_COUNT=$((TEST_ERROR_COUNT+1)) + fi +done + +if [[ $TEST_ERROR_COUNT -eq 0 ]]; then + echo "Pass!" +else + echo "Fail! (${TEST_ERROR_COUNT} tests)" +fi diff --git a/test/is_enabled_error.tasklist b/test/is_enabled_error.tasklist new file mode 100644 index 0000000000..b97ab17500 --- /dev/null +++ b/test/is_enabled_error.tasklist @@ -0,0 +1,9 @@ +/* Copyright (c) 2019 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/** + * See CONFIG_TASK_LIST in config.h for details. + */ +#define CONFIG_TEST_TASK_LIST /* No test task */ |