2 * Copyright (c) 2009, 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include "poll-loop.h"
25 #include "socket-util.h"
27 #include "stream-ssl.h"
39 struct pstream *pstream;
43 check(int a, int b, const char *as, const char *file, int line)
46 ovs_fatal(0, "%s:%d: %s is %d but should be %d", file, line, as, a, b);
51 #define CHECK(A, B) check(A, B, #A, __FILE__, __LINE__)
54 check_errno(int a, int b, const char *as, const char *file, int line)
57 ovs_fatal(0, "%s:%d: %s is %d (%s) but should be %d (%s)",
58 file, line, as, a, strerror(abs(a)), b, strerror(abs(b)));
62 #define CHECK_ERRNO(A, B) check_errno(A, B, #A, __FILE__, __LINE__)
65 fpv_create(const char *type, struct fake_pvconn *fpv)
68 if (!strcmp(type, "unix")) {
69 static int unix_count = 0;
72 bind_path = xasprintf("fake-pvconn.%d", unix_count++);
73 fpv->pvconn_name = xasprintf("punix:%s", bind_path);
74 fpv->vconn_name = xasprintf("unix:%s", bind_path);
75 CHECK_ERRNO(pstream_open(fpv->pvconn_name, &fpv->pstream), 0);
77 } else if (!strcmp(type, "tcp") || !strcmp(type, "ssl")) {
78 char *s, *method, *port, *save_ptr = NULL;
81 open_name = xasprintf("p%s:0:127.0.0.1", type);
82 CHECK_ERRNO(pstream_open(open_name, &fpv->pstream), 0);
84 /* Extract bound port number from pstream name. */
85 s = xstrdup(pstream_get_name(fpv->pstream));
86 method = strtok_r(s, ":", &save_ptr);
87 port = strtok_r(NULL, ":", &save_ptr);
90 fpv->pvconn_name = xstrdup(pstream_get_name(fpv->pstream));
91 fpv->vconn_name = xasprintf("%s:127.0.0.1:%s", type, port);
100 static struct stream *
101 fpv_accept(struct fake_pvconn *fpv)
103 struct stream *stream;
105 CHECK_ERRNO(pstream_accept_block(fpv->pstream, &stream), 0);
111 fpv_close(struct fake_pvconn *fpv)
113 pstream_close(fpv->pstream);
118 fpv_destroy(struct fake_pvconn *fpv)
121 free(fpv->pvconn_name);
122 free(fpv->vconn_name);
125 /* Connects to a fake_pvconn with vconn_open(), then closes the listener and
126 * verifies that vconn_connect() reports 'expected_error'. */
128 test_refuse_connection(const char *type, int expected_error)
130 struct fake_pvconn fpv;
133 fpv_create(type, &fpv);
134 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP_VERSION, &vconn), 0);
137 CHECK_ERRNO(vconn_connect(vconn), expected_error);
142 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
143 * closes it immediately, and verifies that vconn_connect() reports
144 * 'expected_error'. */
146 test_accept_then_close(const char *type, int expected_error)
148 struct fake_pvconn fpv;
151 fpv_create(type, &fpv);
152 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP_VERSION, &vconn), 0);
154 stream_close(fpv_accept(&fpv));
156 CHECK_ERRNO(vconn_connect(vconn), expected_error);
161 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
162 * reads the hello message from it, then closes the connection and verifies
163 * that vconn_connect() reports 'expected_error'. */
165 test_read_hello(const char *type, int expected_error)
167 struct fake_pvconn fpv;
169 struct stream *stream;
171 fpv_create(type, &fpv);
172 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP_VERSION, &vconn), 0);
174 stream = fpv_accept(&fpv);
177 struct ofp_header hello;
180 retval = stream_recv(stream, &hello, sizeof hello);
181 if (retval == sizeof hello) {
182 CHECK(hello.version, OFP_VERSION);
183 CHECK(hello.type, OFPT_HELLO);
184 CHECK(hello.length, htons(sizeof hello));
187 CHECK_ERRNO(retval, -EAGAIN);
191 CHECK_ERRNO(vconn_connect(vconn), EAGAIN);
192 vconn_run_wait(vconn);
193 vconn_connect_wait(vconn);
194 stream_recv_wait(stream);
197 stream_close(stream);
198 CHECK_ERRNO(vconn_connect(vconn), expected_error);
202 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
203 * sends the 'out' bytes in 'out_size' to it (presumably an OFPT_HELLO
204 * message), then verifies that vconn_connect() reports
205 * 'expect_connect_error'. */
207 test_send_hello(const char *type, const void *out, size_t out_size,
208 int expect_connect_error)
210 struct fake_pvconn fpv;
212 bool read_hello, connected;
214 struct stream *stream;
217 fpv_create(type, &fpv);
218 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP_VERSION, &vconn), 0);
220 stream = fpv_accept(&fpv);
224 while (n_sent < out_size) {
227 retval = stream_send(stream, (char *) out + n_sent, out_size - n_sent);
230 } else if (retval == -EAGAIN) {
233 stream_recv_wait(stream);
234 vconn_connect_wait(vconn);
235 vconn_run_wait(vconn);
238 ovs_fatal(0, "stream_send returned unexpected value %d", retval);
242 read_hello = connected = false;
245 struct ofp_header hello;
246 int retval = stream_recv(stream, &hello, sizeof hello);
247 if (retval == sizeof hello) {
248 CHECK(hello.version, OFP_VERSION);
249 CHECK(hello.type, OFPT_HELLO);
250 CHECK(hello.length, htons(sizeof hello));
253 CHECK_ERRNO(retval, -EAGAIN);
259 int error = vconn_connect(vconn);
260 if (error == expect_connect_error) {
264 stream_close(stream);
269 CHECK_ERRNO(error, EAGAIN);
273 if (read_hello && connected) {
277 vconn_run_wait(vconn);
279 vconn_connect_wait(vconn);
282 stream_recv_wait(stream);
286 stream_close(stream);
287 CHECK_ERRNO(vconn_recv(vconn, &msg), EOF);
291 /* Try connecting and sending a normal hello, which should succeed. */
293 test_send_plain_hello(const char *type)
295 struct ofp_header hello;
297 hello.version = OFP_VERSION;
298 hello.type = OFPT_HELLO;
299 hello.length = htons(sizeof hello);
300 hello.xid = htonl(0x12345678);
301 test_send_hello(type, &hello, sizeof hello, 0);
304 /* Try connecting and sending an extra-long hello, which should succeed (since
305 * the specification says that implementations must accept and ignore extra
308 test_send_long_hello(const char *type)
310 struct ofp_header hello;
311 char buffer[sizeof hello * 2];
313 hello.version = OFP_VERSION;
314 hello.type = OFPT_HELLO;
315 hello.length = htons(sizeof buffer);
316 hello.xid = htonl(0x12345678);
317 memset(buffer, 0, sizeof buffer);
318 memcpy(buffer, &hello, sizeof hello);
319 test_send_hello(type, buffer, sizeof buffer, 0);
322 /* Try connecting and sending an echo request instead of a hello, which should
323 * fail with EPROTO. */
325 test_send_echo_hello(const char *type)
327 struct ofp_header echo;
329 echo.version = OFP_VERSION;
330 echo.type = OFPT_ECHO_REQUEST;
331 echo.length = htons(sizeof echo);
332 echo.xid = htonl(0x89abcdef);
333 test_send_hello(type, &echo, sizeof echo, EPROTO);
336 /* Try connecting and sending a hello packet that has its length field as 0,
337 * which should fail with EPROTO. */
339 test_send_short_hello(const char *type)
341 struct ofp_header hello;
343 memset(&hello, 0, sizeof hello);
344 test_send_hello(type, &hello, sizeof hello, EPROTO);
347 /* Try connecting and sending a hello packet that has a bad version, which
348 * should fail with EPROTO. */
350 test_send_invalid_version_hello(const char *type)
352 struct ofp_header hello;
354 hello.version = OFP_VERSION - 1;
355 hello.type = OFPT_HELLO;
356 hello.length = htons(sizeof hello);
357 hello.xid = htonl(0x12345678);
358 test_send_hello(type, &hello, sizeof hello, EPROTO);
362 main(int argc UNUSED, char *argv[])
364 set_program_name(argv[0]);
367 signal(SIGPIPE, SIG_IGN);
371 test_refuse_connection("unix", EPIPE);
372 test_accept_then_close("unix", EPIPE);
373 test_read_hello("unix", ECONNRESET);
374 test_send_plain_hello("unix");
375 test_send_long_hello("unix");
376 test_send_echo_hello("unix");
377 test_send_short_hello("unix");
378 test_send_invalid_version_hello("unix");
380 test_accept_then_close("tcp", ECONNRESET);
381 test_refuse_connection("tcp", ECONNRESET);
382 test_read_hello("tcp", ECONNRESET);
383 test_send_plain_hello("tcp");
384 test_send_long_hello("tcp");
385 test_send_echo_hello("tcp");
386 test_send_short_hello("tcp");
387 test_send_invalid_version_hello("tcp");
390 stream_ssl_set_private_key_file("testpki-privkey.pem");
391 stream_ssl_set_certificate_file("testpki-cert.pem");
392 stream_ssl_set_ca_cert_file("testpki-cacert.pem", false);
394 test_accept_then_close("ssl", EPROTO);
395 test_refuse_connection("ssl", ECONNRESET);
396 test_read_hello("ssl", ECONNRESET);
397 test_send_plain_hello("ssl");
398 test_send_long_hello("ssl");
399 test_send_echo_hello("ssl");
400 test_send_short_hello("ssl");
401 test_send_invalid_version_hello("ssl");
402 #endif /* HAVE_OPENSSL */