Fill tests for bind.
[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         inet_pton(AF_INET, "254.254.254.254", &addr.sin_addr.s_addr);
84         rc = sw_bind(sockfd, (struct sockaddr *) &addr, sizeof(addr));
85
86         /*
87          * We are unable to properly handle address validation in the raw
88          * socket based implementation.
89          * To be updated when porting to the kernel.
90          */
91         test(rc < 0 && errno == EADDRNOTAVAIL);
92
93         sw_close(sockfd);
94 }
95
96 static void bind_ok(void)
97 {
98         struct sockaddr_sw addr;
99         int sockfd;
100         int rc;
101
102         sockfd = sw_socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
103         memset(&addr, 0, sizeof(addr));
104         addr.sin_addr.s_addr = INADDR_ANY;
105         rc = sw_bind(sockfd, (struct sockaddr *) &addr, sizeof(addr));
106
107         test(rc == 0);
108
109         sw_close(sockfd);
110 }
111
112 static void bind_address_in_use(void)
113 {
114         struct sockaddr_sw addr1, addr2;
115         int sockfd1, sockfd2;
116         int rc;
117
118         sockfd1 = sw_socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
119         memset(&addr1, 0, sizeof(addr1));
120         addr1.sin_addr.s_addr = INADDR_ANY;
121         rc = sw_bind(sockfd1, (struct sockaddr *) &addr1, sizeof(addr1));
122         dprintf("after first sw_bind rc = %d\n", rc);
123
124         sockfd2 = sw_socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
125         memset(&addr2, 0, sizeof(addr2));
126         addr2.sin_addr.s_addr = INADDR_ANY;
127         rc = sw_bind(sockfd2, (struct sockaddr *) &addr2, sizeof(addr2));
128         dprintf("after second sw_bind rc = %d\n", rc);
129
130         test(rc < 0 && errno == EADDRINUSE);
131
132         sw_close(sockfd1);
133         sw_close(sockfd2);
134 }
135
136 static void bind_socket_already_bound(void)
137 {
138         struct sockaddr_sw addr1, addr2;
139         int sockfd;
140         int rc;
141
142         sockfd = sw_socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
143         memset(&addr1, 0, sizeof(addr1));
144         addr1.sin_addr.s_addr = INADDR_ANY;
145         rc = sw_bind(sockfd, (struct sockaddr *) &addr1, sizeof(addr1));
146
147         memset(&addr2, 0, sizeof(addr2));
148         addr2.sin_addr.s_addr = INADDR_ANY;
149         addr2.sw_hash.h_array[0] = 0xFF;        /* chage hash ("port") */
150         rc = sw_bind(sockfd, (struct sockaddr *) &addr2, sizeof(addr2));
151
152         test(rc < 0 && errno == EINVAL);
153
154         sw_close(sockfd);
155 }