diff options
-rw-r--r-- | zephyr/shim/src/system.c | 6 | ||||
-rw-r--r-- | zephyr/test/system_shim/test_system.c | 67 |
2 files changed, 72 insertions, 1 deletions
diff --git a/zephyr/shim/src/system.c b/zephyr/shim/src/system.c index c2182e92c5..b3dd2c121b 100644 --- a/zephyr/shim/src/system.c +++ b/zephyr/shim/src/system.c @@ -159,9 +159,15 @@ test_mockable void system_hibernate(uint32_t seconds, uint32_t microseconds) return; } + /* + * Ignore infinite loop for coverage as the test would fail via timeout + * and not report regardless of executing code. + */ + /* LCOV_EXCL_START */ /* should never reach this point */ while (1) continue; + /* LCOV_EXCL_STOP */ } #ifdef CONFIG_PM diff --git a/zephyr/test/system_shim/test_system.c b/zephyr/test/system_shim/test_system.c index ddf992169a..e4b4ddebc6 100644 --- a/zephyr/test/system_shim/test_system.c +++ b/zephyr/test/system_shim/test_system.c @@ -3,6 +3,7 @@ * found in the LICENSE file. */ +#include <setjmp.h> #include <zephyr/device.h> #include <zephyr/drivers/bbram.h> #include <zephyr/fff.h> @@ -24,11 +25,14 @@ static char mock_data[64] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@"; FAKE_VALUE_FUNC(uint64_t, cros_system_native_posix_deep_sleep_ticks, - const struct device *) + const struct device *); +FAKE_VALUE_FUNC(int, cros_system_native_posix_hibernate, const struct device *, + uint32_t, uint32_t); static void system_before_after(void *test_data) { RESET_FAKE(cros_system_native_posix_deep_sleep_ticks); + RESET_FAKE(cros_system_native_posix_hibernate); } ZTEST_SUITE(system, NULL, NULL, system_before_after, system_before_after, NULL); @@ -85,6 +89,67 @@ ZTEST(system, test_system_set_get_scratchpad) zassert_equal(scratch_read, scratch_set); } +static jmp_buf jmp_hibernate; + +static int _test_cros_system_native_posix_hibernate(const struct device *dev, + uint32_t seconds, + uint32_t microseconds) +{ + longjmp(jmp_hibernate, 1); + + return 0; +} + +ZTEST(system, test_system_hibernate) +{ + /* + * Due to setjmp usage, this test provides no coverage, but does + * actually cover the code. This is due to a bug in LCOV. + */ + const struct device *sys_dev = device_get_binding("CROS_SYSTEM"); + int ret = setjmp(jmp_hibernate); + /* Validate 0th and last bit preserved*/ + uint32_t secs = BIT(31) + 1; + uint32_t msecs = BIT(31) + 3; + + zassert_not_null(sys_dev); + + cros_system_native_posix_hibernate_fake.custom_fake = + _test_cros_system_native_posix_hibernate; + + if (ret == 0) { + system_hibernate(secs, msecs); + } + + zassert_not_equal(ret, 0); + + zassert_equal(cros_system_native_posix_hibernate_fake.call_count, 1); + zassert_equal(cros_system_native_posix_hibernate_fake.arg0_val, + sys_dev); + zassert_equal(cros_system_native_posix_hibernate_fake.arg1_val, secs); + zassert_equal(cros_system_native_posix_hibernate_fake.arg2_val, msecs); +} + +ZTEST(system, test_system_hibernate__failure) +{ + const struct device *sys_dev = device_get_binding("CROS_SYSTEM"); + /* Validate 0th and last bit preserved*/ + uint32_t secs = BIT(31) + 1; + uint32_t msecs = BIT(31) + 3; + + zassert_not_null(sys_dev); + + cros_system_native_posix_hibernate_fake.return_val = -1; + + system_hibernate(secs, msecs); + + zassert_equal(cros_system_native_posix_hibernate_fake.call_count, 1); + zassert_equal(cros_system_native_posix_hibernate_fake.arg0_val, + sys_dev); + zassert_equal(cros_system_native_posix_hibernate_fake.arg1_val, secs); + zassert_equal(cros_system_native_posix_hibernate_fake.arg2_val, msecs); +} + ZTEST_USER(system, test_system_console_cmd__idlestats) { const struct device *sys_dev = device_get_binding("CROS_SYSTEM"); |