3 * This file should be included from others, and the next symbols are expected
6 * Preprocessor constants:
7 * - One of TEST_[socket type]
8 * - TEST_NS should be set to the namespace the socket family that will be used
9 * - TEST_PROTOCOL should be set to the socket's protocol to be tested
10 * - TEST_HAVE_PAIR should be defined if the socket family have a "pair()"
12 * - TEST_CHECK_ADDR should be defined if name() should return the same
13 * sockaddr used when bind()ing.
16 * - test_address1 and test_address2 should be valid sockaddrs.
19 * - void clean_test_address(TEST_NS::sock& socket, const TEST_NS::sockaddr&)
20 * should be a function. It will be called before binding a socket to do any
21 * cleanup task for a particular address to be used. Can be a macro.
22 * - std::ostream& operator << (std::ostream& os, const TEST_NS::sockaddr&)
29 #define TEST_PF_UNIX 1
31 #define TEST_PF_UNIX 0
35 #define TEST_PF_INET 1
37 #define TEST_PF_INET 0
41 #define TEST_PF_TIPC 1
43 #define TEST_PF_TIPC 0
66 #define TEST_SEQPACKET 1
68 #define TEST_SEQPACKET 0
78 #define TEST_TYPE ::posixx::socket::DGRAM
81 #define TEST_TYPE ::posixx::socket::RDM
84 #define TEST_TYPE ::posixx::socket::STREAM
87 #define TEST_TYPE ::posixx::socket::SEQPACKET
90 #define TEST_TYPE ::posixx::socket::RAW
94 #if TEST_PF_UNIX // unix sockets are the only that provides pairs
96 BOOST_AUTO_TEST_CASE( pair_test )
98 TEST_NS::pair_type p = TEST_NS::pair(TEST_TYPE, TEST_PROTOCOL);
99 BOOST_CHECK_GE( p.first->fd(), 0 );
100 BOOST_CHECK_GE( p.second->fd(), 0 );
105 #endif // TEST_PF_UNIX
107 BOOST_AUTO_TEST_CASE( constructor_test )
109 TEST_NS::socket s(TEST_TYPE, TEST_PROTOCOL);
110 BOOST_CHECK_GE( s.fd(), 0 );
113 BOOST_AUTO_TEST_CASE( close_test )
115 TEST_NS::socket s(TEST_TYPE, TEST_PROTOCOL);
117 BOOST_CHECK_EQUAL( s.fd(), -1 );
120 BOOST_AUTO_TEST_CASE( options_test )
122 using ::posixx::socket::opt::REUSEADDR;
123 TEST_NS::socket s(TEST_TYPE, TEST_PROTOCOL);
124 s.opt< REUSEADDR >(true);
125 BOOST_CHECK_GE( s.opt< REUSEADDR >(), true );
128 BOOST_AUTO_TEST_CASE( bind_test )
130 TEST_NS::socket s(TEST_TYPE, TEST_PROTOCOL);
131 clean_test_address(s, test_address1);
132 s.bind(test_address1);
134 #if !TEST_PF_TIPC // TIPC returns a Port ID (and we use Port names)
135 BOOST_CHECK_EQUAL( s.name(), test_address1 );
140 #if TEST_SEQPACKET || TEST_STREAM
142 // TODO: test different flavors of send/recv.
144 BOOST_AUTO_TEST_CASE( listen_test )
146 TEST_NS::socket s(TEST_TYPE, TEST_PROTOCOL);
147 clean_test_address(s, test_address1);
148 s.bind(test_address1);
153 void set_blocking(int fd, bool blocking)
155 int flags = fcntl(fd, F_GETFL, 0);
158 fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
160 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
163 #if !TEST_PF_TIPC // TIPC can't connect a non-blocking socket
164 BOOST_AUTO_TEST_CASE( connect_accept_test )
166 TEST_NS::socket ss(TEST_TYPE, TEST_PROTOCOL);
167 clean_test_address(ss, test_address1);
168 ss.bind(test_address1);
170 TEST_NS::socket sc(TEST_TYPE, TEST_PROTOCOL);
171 set_blocking(sc, false);
173 sc.connect(test_address1);
174 } catch (const posixx::error& e) {
175 BOOST_REQUIRE_EQUAL( e.no, EINPROGRESS );
177 set_blocking(sc, true);
178 TEST_NS::socket* sa = ss.accept();
179 BOOST_CHECK_EQUAL( sc.peer_name(), test_address1 );
180 BOOST_CHECK_EQUAL( sc.peer_name(), sa->name() );
181 #if !TEST_PF_UNIX // Unix sockets has garbage in this addresses
182 BOOST_CHECK_EQUAL( sc.name(), sa->peer_name() );
183 #endif // TEST_PF_UNIX
186 #endif // !TEST_PF_TIPC
188 #endif // TEST_SEQPACKET || TEST_STREAM
191 #if TEST_DGRAM || TEST_RDM || TEST_RAW
193 BOOST_AUTO_TEST_CASE( dgram_sendto_recvfrom_test )
196 TEST_NS::socket s1(TEST_TYPE, TEST_PROTOCOL);
197 clean_test_address(s1, test_address1);
198 s1.bind(test_address1);
200 TEST_NS::socket s2(TEST_TYPE, TEST_PROTOCOL);
201 clean_test_address(s2, test_address2);
202 s2.bind(test_address2);
204 char buffer[] = "hello world!";
205 BOOST_CHECK_EQUAL( s1.send(buffer, sizeof(buffer), test_address2),
207 memset(buffer, 0, sizeof(buffer));
209 TEST_NS::sockaddr addr;
210 BOOST_CHECK_EQUAL( s2.recv(buffer, sizeof(buffer), addr),
212 BOOST_CHECK_EQUAL( buffer, "hello world!" );
213 #if !TEST_PF_TIPC // TIPC returns a Port ID (and we use Port names)
214 BOOST_CHECK_EQUAL( addr, test_address1 );
224 BOOST_AUTO_TEST_CASE( dgram_sendto_recvfrom_struct_test )
227 TEST_NS::socket s1(TEST_TYPE, TEST_PROTOCOL);
228 clean_test_address(s1, test_address1);
229 s1.bind(test_address1);
231 TEST_NS::socket s2(TEST_TYPE, TEST_PROTOCOL);
232 clean_test_address(s2, test_address2);
233 s2.bind(test_address2);
236 strcpy(d.msg, "hello world!");
237 d.number = -32495813;
238 s1.send_struct(d, test_address2);
239 memset(&d, 0, sizeof(data));
241 TEST_NS::sockaddr addr;
242 s2.recv_struct(d, addr);
243 BOOST_CHECK_EQUAL( d.msg, "hello world!" );
244 BOOST_CHECK_EQUAL( d.number, -32495813 );
247 #endif // TEST_DGRAM || TEST_RDM || TEST_RAW