From 58ba5190a92c20d817ae0c7bfad17244eae01736 Mon Sep 17 00:00:00 2001 From: Christopher Friedt Date: Thu, 23 Nov 2023 11:47:21 -0500 Subject: [PATCH] tests: posix: add tests to ensure pthread_key_delete() works Ensure that the correct key is deleted via pthread_key_delete(). Signed-off-by: Christopher Friedt (cherry picked from commit 0e11bcf5a0e748c29925199d9cbb634979e495d1) --- tests/posix/common/src/pthread_key.c | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/posix/common/src/pthread_key.c b/tests/posix/common/src/pthread_key.c index 86a3109ea22fd0..4817aeb3a27dc2 100644 --- a/tests/posix/common/src/pthread_key.c +++ b/tests/posix/common/src/pthread_key.c @@ -213,3 +213,34 @@ ZTEST(posix_apis, test_posix_single_thread_multiple_keys) } printk("\n"); } + +ZTEST(posix_apis, test_key_resource_leak) +{ + pthread_key_t key; + + for (size_t i = 0; i < CONFIG_MAX_PTHREAD_KEY_COUNT; ++i) { + zassert_ok(pthread_key_create(&key, NULL), "failed to create key %zu", i); + zassert_ok(pthread_key_delete(key), "failed to delete key %zu", i); + } +} + +ZTEST(posix_apis, test_correct_key_is_deleted) +{ + pthread_key_t key; + size_t j = CONFIG_MAX_PTHREAD_KEY_COUNT - 1; + pthread_key_t keys[CONFIG_MAX_PTHREAD_KEY_COUNT]; + + for (size_t i = 0; i < ARRAY_SIZE(keys); ++i) { + zassert_ok(pthread_key_create(&keys[i], NULL), "failed to create key %zu", i); + } + + key = keys[j]; + zassert_ok(pthread_key_delete(keys[j])); + zassert_ok(pthread_key_create(&keys[j], NULL), "failed to create key %zu", j); + + zassert_equal(key, keys[j], "deleted key %x instead of key %x", keys[j], key); + + for (size_t i = 0; i < ARRAY_SIZE(keys); ++i) { + zassert_ok(pthread_key_delete(keys[i]), "failed to delete key %zu", i); + } +}