swrap: fix possible memory leak between swrap_recvmsg_{before,after}_unix()
[socket_wrapper.git] / tests / test_thread_sockets.c
1 #include "config.h"
2
3 #include <stdarg.h>
4 #include <stddef.h>
5 #include <setjmp.h>
6 #include <cmocka.h>
7
8 #include <pthread.h>
9
10 #include <stdlib.h>
11 #include <errno.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16 #include <unistd.h>
17 #include <grp.h>
18
19 #define NUM_THREADS 10
20
21 static void *thread_worker(void *arg)
22 {
23         int i;
24
25         (void) arg; /* unused */
26
27         for (i = 0; i < 1000; i++) {
28                 int s;
29
30                 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
31                 assert_return_code(s, errno);
32
33                 close(s);
34         }
35
36         return NULL;
37 }
38
39 static void test_threads_socket(void **state)
40 {
41         pthread_attr_t pthread_custom_attr;
42         pthread_t threads[NUM_THREADS];
43         int i;
44
45         (void) state; /* unused */
46
47         pthread_attr_init(&pthread_custom_attr);
48
49         for (i = 0; i < NUM_THREADS; i++) {
50                 pthread_create(&threads[i],
51                                &pthread_custom_attr,
52                                thread_worker,
53                                NULL);
54         }
55
56         for (i = 0; i < NUM_THREADS; i++) {
57                 pthread_join(threads[i], NULL);
58         }
59
60         pthread_attr_destroy(&pthread_custom_attr);
61 }
62
63 int main(void) {
64         int rc;
65
66         const struct CMUnitTest thread_tests[] = {
67                 cmocka_unit_test(test_threads_socket),
68         };
69
70         rc = cmocka_run_group_tests(thread_tests, NULL, NULL);
71
72         return rc;
73 }