Skip to content

Commit e809289

Browse files
Andrew Boienashif
authored andcommitted
tests: futex: improve coverage
Error cases weren't being tested; bring up coverage for kernel/futex.c up to 100% file/function/branch. Signed-off-by: Andrew Boie <[email protected]>
1 parent 0b0294d commit e809289

File tree

1 file changed

+37
-0
lines changed
  • tests/kernel/mem_protect/futex/src

1 file changed

+37
-0
lines changed

tests/kernel/mem_protect/futex/src/main.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <ztest.h>
88
#include <irq_offload.h>
9+
#include <sys/mutex.h>
910

1011
/* Macro declarations */
1112
#define TOTAL_THREADS_WAITING (3)
@@ -27,6 +28,9 @@ ZTEST_BMEM int timeout;
2728
ZTEST_BMEM int index[TOTAL_THREADS_WAITING];
2829
ZTEST_BMEM struct k_futex simple_futex;
2930
ZTEST_BMEM struct k_futex multiple_futex[TOTAL_THREADS_WAITING];
31+
struct k_futex no_access_futex;
32+
ZTEST_BMEM atomic_t not_a_futex;
33+
ZTEST_BMEM struct sys_mutex also_not_a_futex;
3034

3135
struct k_thread futex_tid;
3236
struct k_thread futex_wake_tid;
@@ -414,10 +418,43 @@ void test_multiple_futex_wait_wake(void)
414418
}
415419
}
416420

421+
void test_user_futex_bad(void)
422+
{
423+
int ret;
424+
425+
/* Is a futex, but no access to its memory */
426+
ret = k_futex_wait(&no_access_futex, 0, K_NO_WAIT);
427+
zassert_equal(ret, -EACCES, "shouldn't have been able to access");
428+
ret = k_futex_wake(&no_access_futex, false);
429+
zassert_equal(ret, -EACCES, "shouldn't have been able to access");
430+
431+
/* Access to memory, but not a kernel object */
432+
ret = k_futex_wait((struct k_futex *)&not_a_futex, 0, K_NO_WAIT);
433+
zassert_equal(ret, -EINVAL, "waited on non-futex");
434+
ret = k_futex_wake((struct k_futex *)&not_a_futex, false);
435+
zassert_equal(ret, -EINVAL, "woke non-futex");
436+
437+
/* Access to memory, but wrong object type */
438+
ret = k_futex_wait((struct k_futex *)&also_not_a_futex, 0, K_NO_WAIT);
439+
zassert_equal(ret, -EINVAL, "waited on non-futex");
440+
ret = k_futex_wake((struct k_futex *)&also_not_a_futex, false);
441+
zassert_equal(ret, -EINVAL, "woke non-futex");
442+
443+
/* Wait with unexpected value */
444+
atomic_set(&simple_futex.val, 100);
445+
ret = k_futex_wait(&simple_futex, 0, K_NO_WAIT);
446+
zassert_equal(ret, -EAGAIN, "waited when values did not match");
447+
448+
/* Timeout case */
449+
ret = k_futex_wait(&simple_futex, 100, K_NO_WAIT);
450+
zassert_equal(ret, -ETIMEDOUT, "didn't time out");
451+
}
452+
417453
/* ztest main entry*/
418454
void test_main(void)
419455
{
420456
ztest_test_suite(test_futex,
457+
ztest_user_unit_test(test_user_futex_bad),
421458
ztest_unit_test(test_futex_wait_forever_wake),
422459
ztest_unit_test(test_futex_wait_timeout_wake),
423460
ztest_unit_test(test_futex_wait_nowait_wake),

0 commit comments

Comments
 (0)