diff options
author | Tristan Honscheid <honscheid@google.com> | 2022-08-31 15:40:16 -0600 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-09-01 20:37:33 +0000 |
commit | d243e0376964036cda67a5f4f306d8bb615cf126 (patch) | |
tree | 0c32944b9419ffdbb7932386945ea0a01088b195 | |
parent | 7417a111b5fb4078ec17835960d4263813505bd5 (diff) | |
download | chrome-ec-d243e0376964036cda67a5f4f306d8bb615cf126.tar.gz |
zephyr: test: Test `dps` console command in `common/dps.c`
Test the `dps` console command
BRANCH=None
BUG=None
TEST=./twister
Signed-off-by: Tristan Honscheid <honscheid@google.com>
Change-Id: I89cbbf8658a6fbb25782e37048a3fa75d77e4fee
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3867045
Reviewed-by: Al Semjonovs <asemjonovs@google.com>
-rw-r--r-- | common/dps.c | 19 | ||||
-rw-r--r-- | include/dps.h | 4 | ||||
-rw-r--r-- | zephyr/test/drivers/dps/src/dps.c | 208 |
3 files changed, 228 insertions, 3 deletions
diff --git a/common/dps.c b/common/dps.c index ee2fd7e65d..e984c9e725 100644 --- a/common/dps.c +++ b/common/dps.c @@ -685,3 +685,22 @@ static enum ec_status hc_usb_pd_dps_control(struct host_cmd_handler_args *args) } DECLARE_HOST_COMMAND(EC_CMD_USB_PD_DPS_CONTROL, hc_usb_pd_dps_control, EC_VER_MASK(0)); + +#ifdef TEST_BUILD +__test_only bool dps_is_fake_enabled(void) +{ + return fake_enabled; +} +__test_only int dps_get_fake_mv(void) +{ + return fake_mv; +} +__test_only int dps_get_fake_ma(void) +{ + return fake_ma; +} +__test_only int *dps_get_debug_level(void) +{ + return &debug_level; +} +#endif /* TEST_BUILD */ diff --git a/include/dps.h b/include/dps.h index c49391499c..e9d1daa11b 100644 --- a/include/dps.h +++ b/include/dps.h @@ -72,6 +72,10 @@ void dps_update_stabilized_time(int port); __test_only void dps_enable(bool en); __test_only int dps_init(void); __test_only struct dps_config_t *dps_get_config(void); +__test_only bool dps_is_fake_enabled(void); +__test_only int dps_get_fake_mv(void); +__test_only int dps_get_fake_ma(void); +__test_only int *dps_get_debug_level(void); #endif #endif /* __CROS_EC_DPS__H */ diff --git a/zephyr/test/drivers/dps/src/dps.c b/zephyr/test/drivers/dps/src/dps.c index 27eef51b58..7c537fd27d 100644 --- a/zephyr/test/drivers/dps/src/dps.c +++ b/zephyr/test/drivers/dps/src/dps.c @@ -5,11 +5,15 @@ #include <zephyr/ztest.h> +#include "builtin/stdio.h" +#include "console.h" #include "dps.h" #include "test/drivers/test_state.h" +#include "timer.h" struct dps_fixture { struct dps_config_t saved_config; + int saved_debug_level; }; static void *dps_config_setup(void) @@ -17,13 +21,22 @@ static void *dps_config_setup(void) static struct dps_fixture fixture; fixture.saved_config = *dps_get_config(); + fixture.saved_debug_level = *dps_get_debug_level(); return &fixture; } +static void dps_config_before(void *data) +{ + dps_enable(true); +} + static void dps_config_after(void *data) { - *dps_get_config() = ((struct dps_fixture *)data)->saved_config; + struct dps_fixture *f = (struct dps_fixture *)data; + + *dps_get_config() = f->saved_config; + *dps_get_debug_level() = f->saved_debug_level; dps_enable(true); } @@ -56,5 +69,194 @@ ZTEST_F(dps, test_config) *config = fixture->saved_config; } -ZTEST_SUITE(dps, drivers_predicate_pre_main, dps_config_setup, NULL, - dps_config_after, NULL); +ZTEST(dps, console_cmd__print_info) +{ + /* Print current status to console */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps"), NULL); +} + +ZTEST(dps, console_cmd__enable) +{ + /* Disable DPS first, then try enabling */ + dps_enable(false); + zassert_false(dps_is_enabled(), NULL); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps en"), NULL); + + zassert_true(dps_is_enabled(), NULL); +} + +ZTEST(dps, console_cmd__disable) +{ + /* Should already by enabled due to before() function */ + zassume_true(dps_is_enabled(), NULL); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps dis"), NULL); + + zassert_false(dps_is_enabled(), NULL); +} + +ZTEST(dps, console_cmd__fakepwr_print) +{ + /* Print current fake power status to console */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps fakepwr"), NULL); +} + +ZTEST(dps, console_cmd__fakepwr_enable_disable) +{ + zassume_false(dps_is_fake_enabled(), + "fakepwr shouldn't be enabled by default"); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps fakepwr 100 200"), + NULL); + zassert_true(dps_is_fake_enabled(), NULL); + zassert_equal(100, dps_get_fake_mv(), "Got fake_mv=%d", + dps_get_fake_mv()); + zassert_equal(200, dps_get_fake_ma(), "Got fake_ma=%d", + dps_get_fake_ma()); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps fakepwr dis"), NULL); + zassert_false(dps_is_fake_enabled(), NULL); +} + +ZTEST(dps, console_cmd__fakepwr_invalid) +{ + /* Various invalid parameters */ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps fakepwr 100"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps fakepwr -100 -200"), + NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps fakepwr 100 -200"), + NULL); +} + +ZTEST(dps, console_cmd__debuglevel) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps debug 999"), NULL); + + zassert_equal(999, *dps_get_debug_level(), "Debug level is %d", + *dps_get_debug_level()); +} + +ZTEST(dps, console_cmd__setkmore) +{ + struct dps_config_t *config = dps_get_config(); + char cmd[32]; + + /* Try some invalid requests first */ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkmore"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkmore 101"), + NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkmore 0"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkmore -1"), NULL); + + zassert_true(crec_snprintf(cmd, sizeof(cmd), "dps setkmore %d", + config->k_less_pwr - 1) > 0, + NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), cmd), NULL); + + /* Adjust k_more_pwr to be one over k_less_pwr */ + zassert_true(crec_snprintf(cmd, sizeof(cmd), "dps setkmore %d", + config->k_less_pwr + 1) > 0, + NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); + + zassert_equal(config->k_less_pwr + 1, config->k_more_pwr, + "k_more_pwr is %d but should be %d", config->k_more_pwr, + config->k_less_pwr + 1); +} + +ZTEST(dps, console_cmd__setkless) +{ + struct dps_config_t *config = dps_get_config(); + char cmd[32]; + + /* Try some invalid requests first */ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkless"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkless 101"), + NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkless 0"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkless -1"), NULL); + + zassert_true(crec_snprintf(cmd, sizeof(cmd), "dps setkless %d", + config->k_more_pwr + 1) > 0, + NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), cmd), NULL); + + /* Adjust k_less_pwr to be one under k_more_pwr */ + zassert_true(crec_snprintf(cmd, sizeof(cmd), "dps setkless %d", + config->k_more_pwr - 1) > 0, + NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); + + zassert_equal(config->k_more_pwr - 1, config->k_less_pwr, + "k_less_pwr is %d but should be %d", config->k_less_pwr, + config->k_more_pwr - 1); +} + +ZTEST(dps, console_cmd__setksample) +{ + struct dps_config_t *config = dps_get_config(); + + /* Try some invalid requests first */ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setksample"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setksample -1"), + NULL); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps setksample 999"), + NULL); + + zassert_equal(999, config->k_sample, "k_sample is %d", + config->k_sample); +} + +ZTEST(dps, console_cmd__setkwindow) +{ + struct dps_config_t *config = dps_get_config(); + + /* Try some invalid requests first */ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkwin"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkwin -1"), NULL); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps setkwin 4"), NULL); + + zassert_equal(4, config->k_window, "k_window is %d", config->k_window); +} + +ZTEST(dps, console_cmd__settcheck) +{ + struct dps_config_t *config = dps_get_config(); + + /* Try some invalid requests first */ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps settcheck"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps settcheck -1"), + NULL); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps settcheck 5"), NULL); + + zassert_equal(5 * SECOND, config->t_check, "t_check is %d", + config->t_check); +} + +ZTEST(dps, console_cmd__settstable) +{ + struct dps_config_t *config = dps_get_config(); + + /* Try some invalid requests first */ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps settstable"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps settstable -1"), + NULL); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps settstable 6"), NULL); + + zassert_equal(6 * SECOND, config->t_stable, "t_stable is %d", + config->t_stable); +} + +ZTEST(dps, console_cmd__invalid) +{ + /* Non-existent subcommand should fail */ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps foobar xyz"), NULL); +} + +ZTEST_SUITE(dps, drivers_predicate_pre_main, dps_config_setup, + dps_config_before, dps_config_after, NULL); |