Add counters for MPTP buffers and syscalls.
[swifty.git] / src / raw / test / test_sw_bind.c
1 /*
2  * Test sw_bind "syscall".
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/types.h>
9 #include <unistd.h>
10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <arpa/inet.h>
13 #include <errno.h>
14
15 #include "swift_raw.h"
16 #include "swift_types.h"
17
18 #include "debug.h"
19 #include "util.h"
20
21 #include "test_sw.h"
22 #include "test.h"
23
24 static void bind_dummy(void);
25 static void bind_invalid_descriptor(void);
26 static void bind_descriptor_not_a_socket(void);
27 static void bind_invalid_ip_address(void);
28 static void bind_ok(void);
29 static void bind_address_in_use(void);
30 static void bind_socket_already_bound(void);
31
32 void bind_test_suite(void)
33 {
34         start_suite();
35         bind_dummy();
36         bind_invalid_descriptor();
37         bind_descriptor_not_a_socket();
38         bind_invalid_ip_address();
39         bind_ok();
40         bind_address_in_use();
41         bind_socket_already_bound();
42 }
43
44 static void bind_dummy(void)
45 {
46         test(1 == 1);
47 }
48
49 static void bind_invalid_descriptor(void)
50 {
51         struct sockaddr_sw addr;
52         int rc;
53
54         rc = sw_bind(-2, (struct sockaddr *) &addr, sizeof(addr));
55
56         test(rc < 0 && errno == EBADF);
57 }
58
59 static void bind_descriptor_not_a_socket(void)
60 {
61         struct sockaddr_sw addr;
62         int fd = dup(STDOUT_FILENO);
63         int rc;
64
65         rc = sw_bind(fd, (struct sockaddr *) &addr, sizeof(addr));
66
67         /*
68          * We are unable to properly handle checking whether the file
69          * descriptor is a socket in the raw socket based implementation.
70          * To be updated when porting to the kernel.
71          */
72         test(rc < 0 && errno == ENOTSOCK);
73 }
74
75 static void bind_invalid_ip_address(void)
76 {
77         struct sockaddr_sw addr;
78         int sockfd;
79         int rc;
80
81         sockfd = sw_socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
82         memset(&addr, 0, sizeof(addr));
83         addr.sin_family = AF_INET;
84         inet_pton(AF_INET, "254.254.254.254", &addr.sin_addr.s_addr);
85         rc = sw_bind(sockfd, (struct sockaddr *) &addr, sizeof(addr));
86
87         /*
88          * We are unable to properly handle address validation in the raw
89          * socket based implementation.
90          * To be updated when porting to the kernel.
91          */
92         test(rc < 0 && errno == EADDRNOTAVAIL);
93
94         sw_close(sockfd);
95 }
96
97 static void bind_ok(void)
98 {
99         struct sockaddr_sw addr;
100         int sockfd;
101         int rc;
102
103         sockfd = sw_socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
104         memset(&addr, 0, sizeof(addr));
105         addr.sin_family = AF_INET;
106         addr.sin_addr.s_addr = INADDR_ANY;
107         rc = sw_bind(sockfd, (struct sockaddr *) &addr, sizeof(addr));
108
109         test(rc == 0);
110
111         sw_close(sockfd);
112 }
113
114 static void bind_address_in_use(void)
115 {
116         struct sockaddr_sw addr1, addr2;
117         int sockfd1, sockfd2;
118         int rc;
119
120         sockfd1 = sw_socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
121         memset(&addr1, 0, sizeof(addr1));
122         addr1.sin_family = AF_INET;
123         addr1.sin_addr.s_addr = INADDR_ANY;
124         rc = sw_bind(sockfd1, (struct sockaddr *) &addr1, sizeof(addr1));
125         dprintf("after first sw_bind rc = %d\n", rc);
126
127         sockfd2 = sw_socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
128         memset(&addr2, 0, sizeof(addr2));
129         addr2.sin_family = AF_INET;
130         addr2.sin_addr.s_addr = INADDR_ANY;
131         rc = sw_bind(sockfd2, (struct sockaddr *) &addr2, sizeof(addr2));
132         dprintf("after second sw_bind rc = %d\n", rc);
133
134         test(rc < 0 && errno == EADDRINUSE);
135
136         sw_close(sockfd1);
137         sw_close(sockfd2);
138 }
139
140 static void bind_socket_already_bound(void)
141 {
142         struct sockaddr_sw addr1, addr2;
143         int sockfd;
144         int rc;
145
146         sockfd = sw_socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
147         memset(&addr1, 0, sizeof(addr1));
148         addr1.sin_family = AF_INET;
149         addr1.sin_addr.s_addr = INADDR_ANY;
150         rc = sw_bind(sockfd, (struct sockaddr *) &addr1, sizeof(addr1));
151
152         memset(&addr2, 0, sizeof(addr2));
153         addr2.sin_family = AF_INET;
154         addr2.sin_addr.s_addr = INADDR_ANY;
155         addr2.sw_hash.h_array[0] = 0xFF;        /* chage hash ("port") */
156         rc = sw_bind(sockfd, (struct sockaddr *) &addr2, sizeof(addr2));
157
158         test(rc < 0 && errno == EINVAL);
159
160         sw_close(sockfd);
161 }