1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "it_mutex_test.h"
33
34 static pthread_mutex_t g_muxLock001;
35 static pthread_mutex_t g_muxLock002;
36 static pthread_mutex_t g_muxLock003;
37 static volatile int g_testToCount001 = 0;
38 static volatile int g_testToCount002 = 0;
39 static volatile int g_testToCount003 = 0;
40
ThreadFuncTest1(void * arg)41 static void *ThreadFuncTest1(void *arg)
42 {
43 int ret = pthread_mutex_lock(&g_muxLock001);
44 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
45 ret = pthread_mutex_consistent(&g_muxLock001);
46 ICUNIT_GOTO_EQUAL(ret, EINVAL, ret, EXIT);
47
48 g_testToCount001++;
49 return nullptr;
50
51 EXIT:
52 g_testToCount001 = -1;
53 return nullptr;
54 }
55
ThreadFuncTest2(void * arg)56 static void *ThreadFuncTest2(void *arg)
57 {
58 int ret = pthread_mutex_lock(&g_muxLock001);
59 ICUNIT_GOTO_EQUAL(ret, EOWNERDEAD, ret, EXIT);
60
61 g_testToCount002 = 1;
62
63 while (g_testToCount003 == 0) {
64 sleep(1);
65 }
66
67 g_testToCount002++;
68 return nullptr;
69
70 EXIT:
71 g_testToCount002 = -1;
72 return nullptr;
73 }
ThreadFuncTest3(void * arg)74 static void *ThreadFuncTest3(void *arg)
75 {
76 int ret = pthread_mutex_consistent(&g_muxLock001);
77 ICUNIT_GOTO_EQUAL(ret, EPERM, ret, EXIT);
78 g_testToCount003 = 1;
79
80 for (int i = 0; i < 5; i++) { // 5
81 sleep(1);
82 }
83
84 g_testToCount003++;
85 return nullptr;
86
87 EXIT:
88 g_testToCount003 = -1;
89 return nullptr;
90 }
91
Testcase(void)92 static int Testcase(void)
93 {
94 int ret;
95 pthread_t tid1, tid2, tid3;
96 pthread_mutexattr_t attr;
97 int robust;
98
99 pthread_mutexattr_init(&attr);
100
101 pthread_mutexattr_getrobust(&attr, &robust);
102 ICUNIT_ASSERT_EQUAL(robust, 0, robust);
103
104 ret = pthread_mutexattr_setrobust(&attr, 2); // 2
105 ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret);
106
107 ret = pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);
108 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
109
110 pthread_mutex_init(&g_muxLock002, &attr);
111 ret = pthread_mutex_consistent(&g_muxLock002);
112 ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret);
113
114 pthread_mutexattr_getrobust(&attr, &robust);
115 ICUNIT_ASSERT_EQUAL(robust, PTHREAD_MUTEX_ROBUST, robust);
116
117 pthread_mutex_init(&g_muxLock001, &attr);
118 pthread_mutex_init(&g_muxLock003, &attr);
119
120 ret = pthread_mutex_consistent(&g_muxLock003);
121 ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret);
122
123 ret = pthread_mutex_unlock(&g_muxLock003);
124 ICUNIT_ASSERT_EQUAL(ret, EPERM, ret);
125
126 pthread_mutexattr_destroy(&attr);
127
128 g_testToCount001 = 0;
129 g_testToCount002 = 0;
130 g_testToCount003 = 0;
131
132 ret = pthread_create(&tid1, nullptr, ThreadFuncTest1, nullptr);
133 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
134
135 ret = pthread_create(&tid2, nullptr, ThreadFuncTest2, nullptr);
136 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
137
138 while (g_testToCount002 == 0) {
139 sleep(1);
140 }
141
142 ret = pthread_create(&tid3, nullptr, ThreadFuncTest3, nullptr);
143 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
144
145 pthread_join(tid1, nullptr);
146 pthread_join(tid2, nullptr);
147 pthread_join(tid3, nullptr);
148
149 ICUNIT_ASSERT_NOT_EQUAL(g_testToCount001, -1, g_testToCount001);
150 ICUNIT_ASSERT_NOT_EQUAL(g_testToCount002, -1, g_testToCount002);
151 ICUNIT_ASSERT_NOT_EQUAL(g_testToCount003, -1, g_testToCount003);
152
153 pthread_mutex_destroy(&g_muxLock001);
154 pthread_mutex_destroy(&g_muxLock002);
155 pthread_mutex_destroy(&g_muxLock003);
156
157 return 0;
158 }
159
ItTestPthreadMutex024(void)160 void ItTestPthreadMutex024(void)
161 {
162 TEST_ADD_CASE("IT_POSIX_PTHREAD_MUTEX_024", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
163 }
164