2 * Test sw_bind "syscall".
10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <arpa/inet.h>
15 #include "swift_raw.h"
16 #include "swift_types.h"
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);
32 void bind_test_suite(void)
36 bind_invalid_descriptor();
37 bind_descriptor_not_a_socket();
38 bind_invalid_ip_address();
40 bind_address_in_use();
41 bind_socket_already_bound();
44 static void bind_dummy(void)
49 static void bind_invalid_descriptor(void)
51 struct sockaddr_sw addr;
54 rc = sw_bind(-2, (struct sockaddr *) &addr, sizeof(addr));
56 test(rc < 0 && errno == EBADF);
59 static void bind_descriptor_not_a_socket(void)
61 struct sockaddr_sw addr;
62 int fd = dup(STDOUT_FILENO);
65 rc = sw_bind(fd, (struct sockaddr *) &addr, sizeof(addr));
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.
72 test(rc < 0 && errno == ENOTSOCK);
75 static void bind_invalid_ip_address(void)
77 struct sockaddr_sw addr;
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));
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.
91 test(rc < 0 && errno == EADDRNOTAVAIL);
96 static void bind_ok(void)
98 struct sockaddr_sw addr;
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));
112 static void bind_address_in_use(void)
114 struct sockaddr_sw addr1, addr2;
115 int sockfd1, sockfd2;
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);
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);
130 test(rc < 0 && errno == EADDRINUSE);
136 static void bind_socket_already_bound(void)
138 struct sockaddr_sw addr1, addr2;
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));
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));
152 test(rc < 0 && errno == EINVAL);