Add the bytes field in the mptp_dest structure.
[swifty.git] / src / kernel / test / test_bind.c
1 /*
2  * Test 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_types.h"
16
17 #include "debug.h"
18 #include "util.h"
19
20 #include "test_sw.h"
21 #include "test.h"
22
23 static void bind_dummy(void);
24 static void bind_invalid_descriptor(void);
25 static void bind_descriptor_not_a_socket(void);
26 static void bind_invalid_ip_address(void);
27 static void bind_ok(void);
28 static void bind_address_in_use(void);
29 static void bind_socket_already_bound(void);
30
31 void bind_test_suite(void)
32 {
33         start_suite();
34         bind_dummy();
35         bind_invalid_descriptor();
36         bind_descriptor_not_a_socket();
37         bind_invalid_ip_address();
38         bind_ok();
39         bind_address_in_use();
40         bind_socket_already_bound();
41 }
42
43 static void bind_dummy(void)
44 {
45         test(1 == 1);
46 }
47
48 static void bind_invalid_descriptor(void)
49 {
50         struct sockaddr_sw addr;
51         int rc;
52
53         rc = bind(-2, (struct sockaddr *) &addr, sizeof(addr));
54
55         test(rc < 0 && errno == EBADF);
56 }
57
58 static void bind_descriptor_not_a_socket(void)
59 {
60         struct sockaddr_sw addr;
61         int fd = dup(STDOUT_FILENO);
62         int rc;
63
64         rc = bind(fd, (struct sockaddr *) &addr, sizeof(addr));
65
66         /*
67          * We are unable to properly handle checking whether the file
68          * descriptor is a socket in the raw socket based implementation.
69          * To be updated when porting to the kernel.
70          */
71         test(rc < 0 && errno == ENOTSOCK);
72 }
73
74 static void bind_invalid_ip_address(void)
75 {
76         struct sockaddr_sw addr;
77         int sockfd;
78         int rc;
79
80         sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
81         memset(&addr, 0, sizeof(addr));
82         addr.sin_family = AF_INET;
83         inet_pton(AF_INET, "254.254.254.254", &addr.sin_addr.s_addr);
84         rc = 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         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 = socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
103         memset(&addr, 0, sizeof(addr));
104         addr.sin_family = AF_INET;
105         addr.sin_addr.s_addr = INADDR_ANY;
106         rc = bind(sockfd, (struct sockaddr *) &addr, sizeof(addr));
107
108         test(rc == 0);
109
110         close(sockfd);
111 }
112
113 static void bind_address_in_use(void)
114 {
115         struct sockaddr_sw addr1, addr2;
116         int sockfd1, sockfd2;
117         int rc;
118
119         sockfd1 = socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
120         memset(&addr1, 0, sizeof(addr1));
121         addr1.sin_family = AF_INET;
122         addr1.sin_addr.s_addr = INADDR_ANY;
123         rc = bind(sockfd1, (struct sockaddr *) &addr1, sizeof(addr1));
124         dprintf("after first bind rc = %d\n", rc);
125
126         sockfd2 = socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
127         memset(&addr2, 0, sizeof(addr2));
128         addr2.sin_family = AF_INET;
129         addr2.sin_addr.s_addr = INADDR_ANY;
130         rc = bind(sockfd2, (struct sockaddr *) &addr2, sizeof(addr2));
131         dprintf("after second bind rc = %d\n", rc);
132
133         test(rc < 0 && errno == EADDRINUSE);
134
135         close(sockfd1);
136         close(sockfd2);
137 }
138
139 static void bind_socket_already_bound(void)
140 {
141         struct sockaddr_sw addr1, addr2;
142         int sockfd;
143         int rc;
144
145         sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
146         memset(&addr1, 0, sizeof(addr1));
147         addr1.sin_family = AF_INET;
148         addr1.sin_addr.s_addr = INADDR_ANY;
149         rc = bind(sockfd, (struct sockaddr *) &addr1, sizeof(addr1));
150
151         memset(&addr2, 0, sizeof(addr2));
152         addr2.sin_family = AF_INET;
153         addr2.sin_addr.s_addr = INADDR_ANY;
154         addr2.sw_hash.h_array[0] = 0xFF;        /* chage hash ("port") */
155         rc = bind(sockfd, (struct sockaddr *) &addr2, sizeof(addr2));
156
157         test(rc < 0 && errno == EINVAL);
158
159         close(sockfd);
160 }