11 #include <sys/types.h>
16 #ifdef HAVE_SYS_SYSCALL_H
17 #include <sys/syscall.h>
23 #define NUM_THREADS 10
25 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
27 static void *uwrap_setgid_syscall(void *arg)
31 (void) arg; /* unused */
33 rc = syscall(SYS_setgid, 999);
34 assert_int_equal(rc, 0);
37 assert_int_equal(g, 999);
42 static void test_sync_setgid_syscall(void **state)
44 pthread_attr_t pthread_custom_attr;
45 pthread_t threads[NUM_THREADS];
49 (void) state; /* unused */
51 pthread_attr_init(&pthread_custom_attr);
55 for (i = 0; i < NUM_THREADS; i++) {
56 pthread_create(&threads[i],
62 /* wait for threaads */
63 for (i = 0; i < NUM_THREADS; i++) {
64 pthread_join(threads[i], NULL);
68 assert_int_equal(gn, go);
70 pthread_attr_destroy(&pthread_custom_attr);
73 static void *uwrap_setgid(void *arg)
76 (void) arg; /* unused */
79 assert_int_equal(rc, 0);
84 static void *uwrap_getgid(void *arg)
87 (void) arg; /* unused */
90 assert_int_equal(g, 1999);
95 static void test_sync_setgid(void **state)
97 pthread_attr_t pthread_custom_attr;
98 pthread_t threads[NUM_THREADS];
103 (void) state; /* unused */
105 pthread_attr_init(&pthread_custom_attr);
107 for (i = 0; i < NUM_THREADS; i++) {
108 pthread_create(&threads[i],
109 &pthread_custom_attr,
114 /* wait for threaads */
115 for (i = 0; i < NUM_THREADS; i++) {
116 pthread_join(threads[i], NULL);
120 assert_int_equal(g, 999);
123 assert_int_equal(rc, 0);
125 pthread_create(&threads[0],
126 &pthread_custom_attr,
130 pthread_join(threads[0], NULL);
133 assert_int_equal(g, 1999);
135 pthread_attr_destroy(&pthread_custom_attr);
139 static void *uwrap_setgroups(void *arg)
141 gid_t glist[] = { 100, 200, 300, 400, 500 };
144 (void) arg; /* unused */
146 rc = setgroups(ARRAY_SIZE(glist), glist);
147 assert_int_equal(rc, 0);
152 static void *uwrap_getgroups(void *arg)
154 gid_t glist[] = { 100, 200, 300, 400, 500 };
158 (void) arg; /* unused */
160 rc = getgroups(ARRAY_SIZE(rlist), rlist);
161 assert_int_equal(rc, 5);
163 assert_memory_equal(glist, rlist, sizeof(glist));
168 static void test_sync_setgroups(void **state)
170 gid_t glist[] = { 100, 200, 300, 400, 500 };
171 pthread_t threads[NUM_THREADS];
176 (void) state; /* unused */
178 for (i = 0; i < NUM_THREADS; i++) {
179 pthread_create(&threads[i],
185 /* wait for threaads */
186 for (i = 0; i < NUM_THREADS; i++) {
187 pthread_join(threads[i], NULL);
190 rc = getgroups(ARRAY_SIZE(rlist), rlist);
191 assert_int_equal(rc, 5);
193 assert_memory_equal(glist, rlist, sizeof(glist));
195 pthread_create(&threads[0],
200 pthread_join(threads[0], NULL);
204 static void *uwrap_create_thread_setgid(void *arg)
211 tmp = *((signed int *) arg);
214 g_a = (gid_t) *((unsigned int *) arg);
216 rc = syscall(SYS_setgid, g_a);
217 assert_int_equal(rc, 0);
220 assert_int_equal(g, g_a);
223 rc = pthread_create(&thread,
225 &uwrap_create_thread_setgid,
227 assert_int_equal(rc, 0);
229 pthread_join(thread, NULL);
232 assert_int_equal(g, g_a);
237 assert_int_equal(g, g_a);
243 static void test_thread_create_thread_setgid(void **state)
246 signed int tmp = 666;
250 (void) state; /* unused */
253 assert_int_equal(rc, 0);
255 assert_int_equal(g, 555);
257 pthread_create(&thread,
259 &uwrap_create_thread_setgid,
261 pthread_join(thread, NULL);
267 const struct CMUnitTest thread_tests[] = {
268 cmocka_unit_test(test_sync_setgid),
269 cmocka_unit_test(test_sync_setgid_syscall),
270 cmocka_unit_test(test_sync_setgroups),
271 cmocka_unit_test(test_thread_create_thread_setgid),
274 rc = cmocka_run_group_tests(thread_tests, NULL, NULL);