diff options
-rw-r--r-- | common/mock/build.mk | 1 | ||||
-rw-r--r-- | common/mock/rollback_latest_mock.c | 61 | ||||
-rw-r--r-- | common/rollback.c | 2 | ||||
-rw-r--r-- | include/mock/rollback_latest_mock.h | 32 | ||||
-rw-r--r-- | test/build.mk | 2 | ||||
-rw-r--r-- | test/rollback_secret.c | 79 | ||||
-rw-r--r-- | test/rollback_secret.mocklist | 7 | ||||
-rw-r--r-- | test/rollback_secret.tasklist | 9 | ||||
-rw-r--r-- | test/test_config.h | 9 |
9 files changed, 201 insertions, 1 deletions
diff --git a/common/mock/build.mk b/common/mock/build.mk index a8b109429c..f2fca72328 100644 --- a/common/mock/build.mk +++ b/common/mock/build.mk @@ -13,6 +13,7 @@ mock-$(HAS_MOCK_FPSENSOR_DETECT) += fpsensor_detect_mock.o mock-$(HAS_MOCK_FPSENSOR_STATE) += fpsensor_state_mock.o mock-$(HAS_MOCK_MKBP_EVENTS) += mkbp_events_mock.o mock-$(HAS_MOCK_ROLLBACK) += rollback_mock.o +mock-$(HAS_MOCK_ROLLBACK_LATEST) += rollback_latest_mock.o mock-$(HAS_MOCK_TCPC) += tcpc_mock.o mock-$(HAS_MOCK_TCPM) += tcpm_mock.o mock-$(HAS_MOCK_TCPCI_I2C) += tcpci_i2c_mock.o diff --git a/common/mock/rollback_latest_mock.c b/common/mock/rollback_latest_mock.c new file mode 100644 index 0000000000..7931733dea --- /dev/null +++ b/common/mock/rollback_latest_mock.c @@ -0,0 +1,61 @@ +/* Copyright 2020 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/** + * @file + * @brief Mock rollback block library + */ + +#include <string.h> + +#include "common.h" +#include "compile_time_macros.h" +#include "util.h" +#include "mock/rollback_latest_mock.h" +#include "rollback_private.h" + +#ifndef TEST_BUILD +#error "Mocks should only be in the test build." +#endif + +struct mock_ctrl_latest_rollback mock_ctrl_latest_rollback = + MOCK_CTRL_DEFAULT_LATEST_ROLLBACK; + +static const struct rollback_data fake_latest_rollback_zeros = { + .cookie = 0, + .secret = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,}, + + .id = 0, + .rollback_min_version = 0, +}; + +static const struct rollback_data fake_latest_rollback_real = { + .cookie = 9, + .secret = { + 0xcf, 0xe3, 0x23, 0x76, 0x35, 0x04, 0xc2, 0x0f, 0x0d, 0xb6, 0x02, + 0xa9, 0x68, 0xba, 0x2a, 0x61, 0x86, 0x2a, 0x85, 0xd1, 0xca, 0x09, + 0x54, 0x8a, 0x6b, 0xe2, 0xe3, 0x38, 0xde, 0x5d, 0x59, 0x14,}, + .id = 2, + .rollback_min_version = 1, +}; + +/* Mock the rollback for unit or fuzz tests. */ +int get_latest_rollback(struct rollback_data *data) +{ + switch (mock_ctrl_latest_rollback.output_type) { + case GET_LATEST_ROLLBACK_FAIL: + return -5; + case GET_LATEST_ROLLBACK_ZEROS: + *data = fake_latest_rollback_zeros; + break; + case GET_LATEST_ROLLBACK_REAL: + *data = fake_latest_rollback_real; + break; + } + return EC_SUCCESS; +} diff --git a/common/rollback.c b/common/rollback.c index 7aaba72ebb..20dcbe4aa9 100644 --- a/common/rollback.c +++ b/common/rollback.c @@ -115,7 +115,7 @@ int read_rollback(int region, struct rollback_data *data) * Return most recent region index on success (>= 0, or 0 if no rollback * region is valid), negative value on error. */ -static int get_latest_rollback(struct rollback_data *data) +test_mockable_static int get_latest_rollback(struct rollback_data *data) { int ret = -1; int region; diff --git a/include/mock/rollback_latest_mock.h b/include/mock/rollback_latest_mock.h new file mode 100644 index 0000000000..b9e32b24dd --- /dev/null +++ b/include/mock/rollback_latest_mock.h @@ -0,0 +1,32 @@ +/* Copyright 2020 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/** + * @file + * @brief Controls for the mock rollback block library + */ + +#ifndef __MOCK_ROLLBACK_LATEST_MOCK_H +#define __MOCK_ROLLBACK_LATEST_MOCK_H + +#include <stdbool.h> + +enum mock_ctrl_latest_rollback_type { + GET_LATEST_ROLLBACK_FAIL, + GET_LATEST_ROLLBACK_ZEROS, + GET_LATEST_ROLLBACK_REAL, +}; + +struct mock_ctrl_latest_rollback { + enum mock_ctrl_latest_rollback_type output_type; +}; + +#define MOCK_CTRL_DEFAULT_LATEST_ROLLBACK \ + ((struct mock_ctrl_latest_rollback){ \ + .output_type = GET_LATEST_ROLLBACK_REAL }) + +extern struct mock_ctrl_latest_rollback mock_ctrl_latest_rollback; + +#endif /* __MOCK_ROLLBACK_LATEST_MOCK_H */ diff --git a/test/build.mk b/test/build.mk index 9ab2cf0f7a..73a67a0bb1 100644 --- a/test/build.mk +++ b/test/build.mk @@ -83,6 +83,7 @@ test-list-host += power_button test-list-host += printf test-list-host += queue test-list-host += rgb_keyboard +test-list-host += rollback_secret test-list-host += rsa test-list-host += rsa3 test-list-host += rtc @@ -223,6 +224,7 @@ printf-y=printf.o queue-y=queue.o rollback-y=rollback.o rollback_entropy-y=rollback_entropy.o +rollback_secret-y=rollback_secret.o rsa-y=rsa.o rsa3-y=rsa.o rtc-y=rtc.o diff --git a/test/rollback_secret.c b/test/rollback_secret.c new file mode 100644 index 0000000000..c3e23e4d11 --- /dev/null +++ b/test/rollback_secret.c @@ -0,0 +1,79 @@ +/* Copyright 2020 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <stdint.h> + +#include "mock/rollback_latest_mock.h" +#include "rollback.h" +#include "rollback_private.h" +#include "string.h" +#include "system.h" +#include "test_util.h" + +extern int get_latest_rollback(struct rollback_data *data); + +test_static int test_get_rollback_secret_latest_rollback_fail(void) +{ + struct rollback_data test_data; + uint8_t secret[32] = { 0 }; + + mock_ctrl_latest_rollback.output_type = GET_LATEST_ROLLBACK_FAIL; + TEST_ASSERT(get_latest_rollback(&test_data) == -5); + + TEST_ASSERT(rollback_get_secret(secret) == EC_ERROR_UNKNOWN); + + return EC_SUCCESS; +} + +test_static int test_get_rollback_secret_latest_rollback_secret_zeros(void) +{ + struct rollback_data test_data; + uint8_t secret[32]; + const uint8_t zeros_secret[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + }; + + mock_ctrl_latest_rollback.output_type = GET_LATEST_ROLLBACK_ZEROS; + TEST_ASSERT(get_latest_rollback(&test_data) == EC_SUCCESS); + TEST_ASSERT_ARRAY_EQ(test_data.secret, zeros_secret, + sizeof(zeros_secret)); + + TEST_ASSERT(rollback_get_secret(secret) == EC_ERROR_UNKNOWN); + + return EC_SUCCESS; +} + +test_static int test_get_rollback_secret_latest_rollback_secret_succeed(void) +{ + struct rollback_data test_data; + uint8_t secret[32]; + const uint8_t real_secret[] = { + 0xcf, 0xe3, 0x23, 0x76, 0x35, 0x04, 0xc2, 0x0f, + 0x0d, 0xb6, 0x02, 0xa9, 0x68, 0xba, 0x2a, 0x61, + 0x86, 0x2a, 0x85, 0xd1, 0xca, 0x09, 0x54, 0x8a, + 0x6b, 0xe2, 0xe3, 0x38, 0xde, 0x5d, 0x59, 0x14, + }; + + mock_ctrl_latest_rollback.output_type = GET_LATEST_ROLLBACK_REAL; + TEST_ASSERT(get_latest_rollback(&test_data) == EC_SUCCESS); + TEST_ASSERT_ARRAY_EQ(test_data.secret, real_secret, + sizeof(real_secret)); + + TEST_ASSERT(rollback_get_secret(secret) == EC_SUCCESS); + TEST_ASSERT_ARRAY_EQ(secret, test_data.secret, sizeof(secret)); + + return EC_SUCCESS; +} + +void run_test(int argc, const char **argv) +{ + RUN_TEST(test_get_rollback_secret_latest_rollback_fail); + RUN_TEST(test_get_rollback_secret_latest_rollback_secret_zeros); + RUN_TEST(test_get_rollback_secret_latest_rollback_secret_succeed); + test_print_result(); +} diff --git a/test/rollback_secret.mocklist b/test/rollback_secret.mocklist new file mode 100644 index 0000000000..1a22d1d4b2 --- /dev/null +++ b/test/rollback_secret.mocklist @@ -0,0 +1,7 @@ +/* Copyright 2020 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#define CONFIG_TEST_MOCK_LIST \ + MOCK(ROLLBACK_LATEST) diff --git a/test/rollback_secret.tasklist b/test/rollback_secret.tasklist new file mode 100644 index 0000000000..844ddb6c10 --- /dev/null +++ b/test/rollback_secret.tasklist @@ -0,0 +1,9 @@ +/* Copyright 2019 The ChromiumOS Authors + * 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 diff --git a/test/test_config.h b/test/test_config.h index d72a078434..e113024163 100644 --- a/test/test_config.h +++ b/test/test_config.h @@ -112,6 +112,15 @@ #define CONFIG_SHA256 #endif +#ifdef TEST_ROLLBACK_SECRET +#define CONFIG_ROLLBACK +#define CONFIG_ROLLBACK_SECRET_SIZE 32 +#define CONFIG_ROLLBACK_OFF 1 +#define CONFIG_ROLLBACK_SIZE 2 +#undef CONFIG_ROLLBACK_UPDATE +#define FP_CONTEXT_TPM_BYTES 32 +#endif + #ifdef TEST_MOTION_SENSE_FIFO #define CONFIG_ACCEL_FIFO #define CONFIG_ACCEL_FIFO_SIZE 256 |