From f46eefcad2b32a171583c4f566c2efd0c0bd7127 Mon Sep 17 00:00:00 2001 From: Yicheng Li Date: Wed, 16 Oct 2019 16:52:27 -0700 Subject: fpsensor: Improve unit test for enable/disable positive match secret Initialize the local struct so that the test is not flaky. Also extract helper function and add more asserts to make the logic more explicit to readers. BUG=none BRANCH=nocturne TEST=make -j buildall Change-Id: I84ccd097b64c3ff304f4af228121b2cb989e6ab7 Signed-off-by: Yicheng Li Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1865956 Reviewed-by: Craig Hesling --- test/fpsensor.c | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/test/fpsensor.c b/test/fpsensor.c index 81441295ae..490c903ba3 100644 --- a/test/fpsensor.c +++ b/test/fpsensor.c @@ -603,6 +603,21 @@ test_static int test_fp_set_sensor_mode(void) return EC_SUCCESS; } +static int test_enable_positive_match_secret_once( + struct positive_match_secret_state *dumb_state) +{ + const int8_t kIndexToEnable = 0; + timestamp_t now = get_time(); + + TEST_ASSERT(fp_enable_positive_match_secret( + kIndexToEnable, dumb_state) == EC_SUCCESS); + TEST_ASSERT(dumb_state->template_matched == kIndexToEnable); + TEST_ASSERT(dumb_state->readable); + TEST_ASSERT(dumb_state->deadline.val == now.val + (5 * SECOND)); + + return EC_SUCCESS; +} + test_static int test_enable_positive_match_secret(void) { struct positive_match_secret_state dumb_state = { @@ -610,19 +625,15 @@ test_static int test_enable_positive_match_secret(void) .readable = false, .deadline.val = 0, }; - timestamp_t now = get_time(); - TEST_ASSERT(fp_enable_positive_match_secret(0, &dumb_state) == - EC_SUCCESS); - TEST_ASSERT(dumb_state.template_matched == 0); - TEST_ASSERT(dumb_state.readable == true); - TEST_ASSERT(dumb_state.deadline.val == now.val + (5 * SECOND)); + TEST_ASSERT(test_enable_positive_match_secret_once(&dumb_state) + == EC_SUCCESS); /* Trying to enable again before reading secret should fail. */ TEST_ASSERT(fp_enable_positive_match_secret(0, &dumb_state) == EC_ERROR_UNKNOWN); TEST_ASSERT(dumb_state.template_matched == FP_NO_SUCH_TEMPLATE); - TEST_ASSERT(dumb_state.readable == false); + TEST_ASSERT(!dumb_state.readable); TEST_ASSERT(dumb_state.deadline.val == 0); return EC_SUCCESS; @@ -630,13 +641,18 @@ test_static int test_enable_positive_match_secret(void) test_static int test_disable_positive_match_secret(void) { - struct positive_match_secret_state dumb_state; + struct positive_match_secret_state dumb_state = { + .template_matched = FP_NO_SUCH_TEMPLATE, + .readable = false, + .deadline.val = 0, + }; + + TEST_ASSERT(test_enable_positive_match_secret_once(&dumb_state) + == EC_SUCCESS); - TEST_ASSERT(fp_enable_positive_match_secret(0, &dumb_state) == - EC_SUCCESS); fp_disable_positive_match_secret(&dumb_state); TEST_ASSERT(dumb_state.template_matched == FP_NO_SUCH_TEMPLATE); - TEST_ASSERT(dumb_state.readable == false); + TEST_ASSERT(!dumb_state.readable); TEST_ASSERT(dumb_state.deadline.val == 0); return EC_SUCCESS; -- cgit v1.2.1