1 #include <gtest/gtest.h>
2 #include <pthread.h>
3 #include <thread>
4 #include <threads.h>
5
6 using namespace testing::ext;
7
8 class ThreadTssTest : public testing::Test {
SetUp()9 void SetUp() override {}
TearDown()10 void TearDown() override {}
11 };
12
13 static int g_callCount = 0;
14
TssThreadFunc(void * ptr)15 static void TssThreadFunc(void* ptr)
16 {
17 if (ptr == nullptr) {
18 return;
19 }
20 ++g_callCount;
21 free(ptr);
22 }
23
24 /**
25 * @tc.name: tss_create_001
26 * @tc.desc: Successfully created key
27 * @tc.type: FUNC
28 * */
29 HWTEST_F(ThreadTssTest, tss_create_001, TestSize.Level1)
30 {
31 tss_t tssKey;
32 EXPECT_EQ(thrd_success, tss_create(&tssKey, nullptr));
33 tss_delete(tssKey);
34 }
35
36 /**
37 * @tc.name: tss_create_002
38 * @tc.desc: 1. Set the function for the key and call tss once in the main thread and sub thread respectively tss_set to
39 * determine if the function in the key will be called in the sub thread, but not in the main thread
40 * 2. Create and obtain key verification equality in the main thread, and verify equality in the sub thread.
41 * The sub thread key and the main thread key are not equal,
42 * @tc.type: FUNC
43 * */
44 HWTEST_F(ThreadTssTest, tss_create_002, TestSize.Level1)
45 {
46 tss_dtor_t dtor = TssThreadFunc;
47 tss_t tssKey;
48 const char* testChar = "0";
49 testChar = (char*)malloc(sizeof(testChar) + 1);
50 EXPECT_EQ(thrd_success, tss_create(&tssKey, dtor));
51 EXPECT_EQ(thrd_success, tss_set(tssKey, const_cast<char*>("1")));
52 EXPECT_STREQ("1", reinterpret_cast<char*>(tss_get(tssKey)));
53 EXPECT_EQ(0, g_callCount);
54
__anond22332c30102null55 std::thread([&tssKey, testChar] {
56 EXPECT_EQ(nullptr, tss_get(tssKey));
57 tss_set(tssKey, const_cast<char*>(testChar));
58 }).join();
59 EXPECT_EQ(1, g_callCount);
60 EXPECT_STREQ("1", reinterpret_cast<char*>(tss_get(tssKey)));
61
62 g_callCount = 0;
63 tss_delete(tssKey);
64 EXPECT_EQ(0, g_callCount);
65 }