test_echo_tcp_sendmsg_recvmsg_fd: add test_tcp_sendmsg_recvmsg_fd_different() tests
[socket_wrapper.git] / tests / test_echo_tcp_connect.c
1 #include <stdarg.h>
2 #include <stddef.h>
3 #include <setjmp.h>
4 #include <cmocka.h>
5
6 #include "config.h"
7 #include "torture.h"
8
9 #include <errno.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <unistd.h>
17
18 static int setup_echo_srv_tcp_ipv4(void **state)
19 {
20         torture_setup_echo_srv_tcp_ipv4(state);
21
22         return 0;
23 }
24
25 static int teardown(void **state)
26 {
27         torture_teardown_echo_srv(state);
28
29         return 0;
30 }
31
32 static void test_connect_broadcast_ipv4(void **state)
33 {
34         struct torture_address addr = {
35                 .sa_socklen = sizeof(struct sockaddr_in),
36         };
37         int rc;
38         int s;
39
40         (void) state; /* unused */
41
42         s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
43         assert_int_not_equal(s, -1);
44
45         addr.sa.in.sin_family = AF_INET;
46         addr.sa.in.sin_port = htons(torture_server_port());
47         addr.sa.in.sin_addr.s_addr = INADDR_BROADCAST;
48
49         /* We don't allow connect to broadcast addresses */
50         rc = connect(s, &addr.sa.s, addr.sa_socklen);
51         assert_int_equal(rc, -1);
52
53         close(s);
54 }
55
56 #ifdef HAVE_IPV6
57 static void test_connect_downgrade_ipv6(void **state)
58 {
59         struct torture_address addr = {
60                 .sa_socklen = sizeof(struct sockaddr_in),
61         };
62         int rc;
63         int s;
64
65         (void) state; /* unused */
66
67         s = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
68         assert_int_not_equal(s, -1);
69
70         addr.sa.in.sin_family = AF_INET;
71         addr.sa.in.sin_port = htons(torture_server_port());
72
73         rc = inet_pton(addr.sa.in.sin_family,
74                        torture_server_address(AF_INET),
75                        &addr.sa.in.sin_addr);
76         assert_int_equal(rc, 1);
77
78         /* Connect should downgrade to IPv4 and allow the connect */
79         rc = connect(s, &addr.sa.s, addr.sa_socklen);
80         assert_int_equal(rc, 0);
81
82         close(s);
83 }
84 #endif
85
86 int main(void) {
87         int rc;
88
89         const struct CMUnitTest tcp_connect_tests[] = {
90                 cmocka_unit_test(test_connect_broadcast_ipv4),
91 #ifdef HAVE_IPV6
92                 cmocka_unit_test(test_connect_downgrade_ipv6),
93 #endif
94         };
95
96         rc = cmocka_run_group_tests(tcp_connect_tests,
97                                     setup_echo_srv_tcp_ipv4,
98                                     teardown);
99
100         return rc;
101 }