swrap: fix possible memory leak between swrap_recvmsg_{before,after}_unix()
[socket_wrapper.git] / tests / test_thread_echo_tcp_write_read.c
1 #include <stdarg.h>
2 #include <stddef.h>
3 #include <setjmp.h>
4 #include <cmocka.h>
5
6 #include <pthread.h>
7
8 #include "config.h"
9 #include "torture.h"
10
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 <stdlib.h>
17 #include <stdio.h>
18 #include <unistd.h>
19
20 #define NUM_THREADS 10
21
22 static int setup_echo_srv_tcp_ipv4(void **state)
23 {
24         torture_setup_echo_srv_tcp_ipv4(state);
25
26         return 0;
27 }
28
29 static int teardown(void **state)
30 {
31         torture_teardown_echo_srv(state);
32
33         return 0;
34 }
35
36 static void *thread_worker(void *arg)
37 {
38         struct torture_address addr = {
39                 .sa_socklen = sizeof(struct sockaddr_in),
40         };
41         ssize_t ret;
42         int rc;
43         int i;
44         int s;
45
46         (void) arg; /* unused */
47
48         s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
49         assert_int_not_equal(s, -1);
50
51         addr.sa.in.sin_family = AF_INET;
52         addr.sa.in.sin_port = htons(torture_server_port());
53
54         rc = inet_pton(addr.sa.in.sin_family,
55                        torture_server_address(AF_INET),
56                        &addr.sa.in.sin_addr);
57         assert_int_equal(rc, 1);
58
59         rc = connect(s, &addr.sa.s, addr.sa_socklen);
60         assert_return_code(rc, errno);
61
62         for (i = 0; i < 10; i++) {
63                 char send_buf[64] = {0};
64                 char recv_buf[64] = {0};
65
66                 snprintf(send_buf, sizeof(send_buf), "packet.%d", i);
67
68                 ret = write(s,
69                             send_buf,
70                             sizeof(send_buf));
71                 assert_return_code(ret, errno);
72
73                 ret = read(s,
74                            recv_buf,
75                            sizeof(recv_buf));
76                 assert_int_not_equal(ret, -1);
77
78                 assert_memory_equal(send_buf, recv_buf, sizeof(send_buf));
79         }
80
81         close(s);
82         return NULL;
83 }
84
85 static void test_write_read_ipv4(void **state)
86 {
87         pthread_attr_t pthread_custom_attr;
88         pthread_t threads[NUM_THREADS];
89         int i;
90
91         (void) state; /* unused */
92
93         pthread_attr_init(&pthread_custom_attr);
94
95         for (i = 0; i < NUM_THREADS; i++) {
96                 pthread_create(&threads[i],
97                                &pthread_custom_attr,
98                                thread_worker,
99                                NULL);
100         }
101
102         for (i = 0; i < NUM_THREADS; i++) {
103                 pthread_join(threads[i], NULL);
104         }
105
106         pthread_attr_destroy(&pthread_custom_attr);
107 }
108
109 int main(void) {
110         int rc;
111
112         const struct CMUnitTest tcp_write_tests[] = {
113                 cmocka_unit_test_setup_teardown(test_write_read_ipv4,
114                                                 setup_echo_srv_tcp_ipv4,
115                                                 teardown),
116         };
117
118         rc = cmocka_run_group_tests(tcp_write_tests, NULL, NULL);
119
120         return rc;
121 }