]> git.proxmox.com Git - mirror_ovs.git/blob - tests/test-vconn.c
lib: Accomodate FreeBSD return value for ssl connection.
[mirror_ovs.git] / tests / test-vconn.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <config.h>
18 #include "vconn.h"
19 #include <errno.h>
20 #include <inttypes.h>
21 #include <signal.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include "command-line.h"
25 #include "ofp-msgs.h"
26 #include "ofp-util.h"
27 #include "ofpbuf.h"
28 #include "openflow/openflow.h"
29 #include "poll-loop.h"
30 #include "socket-util.h"
31 #include "stream.h"
32 #include "stream-ssl.h"
33 #include "timeval.h"
34 #include "util.h"
35 #include "vlog.h"
36
37 #undef NDEBUG
38 #include <assert.h>
39
40 struct fake_pvconn {
41 const char *type;
42 char *pvconn_name;
43 char *vconn_name;
44 struct pstream *pstream;
45 };
46
47 static void
48 check(int a, int b, const char *as, const char *file, int line)
49 {
50 if (a != b) {
51 ovs_fatal(0, "%s:%d: %s is %d but should be %d", file, line, as, a, b);
52 }
53 }
54
55
56 #define CHECK(A, B) check(A, B, #A, __FILE__, __LINE__)
57
58 static void
59 check_errno(int a, int b, const char *as, const char *file, int line)
60 {
61 if (a != b) {
62 char *str_b = strdup(strerror(abs(b)));
63 ovs_fatal(0, "%s:%d: %s is %d (%s) but should be %d (%s)",
64 file, line, as, a, strerror(abs(a)), b, str_b);
65 }
66 }
67
68 #define CHECK_ERRNO(A, B) check_errno(A, B, #A, __FILE__, __LINE__)
69
70 static void
71 fpv_create(const char *type, struct fake_pvconn *fpv)
72 {
73 #ifdef HAVE_OPENSSL
74 if (!strcmp(type, "ssl")) {
75 stream_ssl_set_private_key_file("testpki-privkey.pem");
76 stream_ssl_set_certificate_file("testpki-cert.pem");
77 stream_ssl_set_ca_cert_file("testpki-cacert.pem", false);
78 }
79 #endif
80
81 fpv->type = type;
82 if (!strcmp(type, "unix")) {
83 static int unix_count = 0;
84 char *bind_path;
85
86 bind_path = xasprintf("fake-pvconn.%d", unix_count++);
87 fpv->pvconn_name = xasprintf("punix:%s", bind_path);
88 fpv->vconn_name = xasprintf("unix:%s", bind_path);
89 CHECK_ERRNO(pstream_open(fpv->pvconn_name, &fpv->pstream,
90 DSCP_DEFAULT), 0);
91 free(bind_path);
92 } else if (!strcmp(type, "tcp") || !strcmp(type, "ssl")) {
93 char *s, *port, *save_ptr = NULL;
94 char *open_name;
95
96 open_name = xasprintf("p%s:0:127.0.0.1", type);
97 CHECK_ERRNO(pstream_open(open_name, &fpv->pstream, DSCP_DEFAULT), 0);
98
99 /* Extract bound port number from pstream name. */
100 s = xstrdup(pstream_get_name(fpv->pstream));
101 strtok_r(s, ":", &save_ptr);
102 port = strtok_r(NULL, ":", &save_ptr);
103
104 /* Save info. */
105 fpv->pvconn_name = xstrdup(pstream_get_name(fpv->pstream));
106 fpv->vconn_name = xasprintf("%s:127.0.0.1:%s", type, port);
107
108 free(open_name);
109 free(s);
110 } else {
111 abort();
112 }
113 }
114
115 static struct stream *
116 fpv_accept(struct fake_pvconn *fpv)
117 {
118 struct stream *stream;
119
120 CHECK_ERRNO(pstream_accept_block(fpv->pstream, &stream), 0);
121
122 return stream;
123 }
124
125 static void
126 fpv_close(struct fake_pvconn *fpv)
127 {
128 pstream_close(fpv->pstream);
129 fpv->pstream = NULL;
130 }
131
132 static void
133 fpv_destroy(struct fake_pvconn *fpv)
134 {
135 fpv_close(fpv);
136 free(fpv->pvconn_name);
137 free(fpv->vconn_name);
138 }
139
140 /* Connects to a fake_pvconn with vconn_open(), then closes the listener and
141 * verifies that vconn_connect() reports 'expected_error'. */
142 static void
143 test_refuse_connection(int argc OVS_UNUSED, char *argv[])
144 {
145 const char *type = argv[1];
146 struct fake_pvconn fpv;
147 struct vconn *vconn;
148 int error;
149
150 fpv_create(type, &fpv);
151 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
152 DSCP_DEFAULT), 0);
153 fpv_close(&fpv);
154 vconn_run(vconn);
155
156 error = vconn_connect_block(vconn);
157 if (!strcmp(type, "tcp")) {
158 if (error != ECONNRESET && error != EPIPE) {
159 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
160 error, strerror(error));
161 }
162 } else if (!strcmp(type, "unix")) {
163 CHECK_ERRNO(error, EPIPE);
164 } else if (!strcmp(type, "ssl")) {
165 if (error != EPROTO && error != ECONNRESET) {
166 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
167 error, strerror(error));
168 }
169 } else {
170 ovs_fatal(0, "invalid connection type %s", type);
171 }
172
173 vconn_close(vconn);
174 fpv_destroy(&fpv);
175 }
176
177 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
178 * closes it immediately, and verifies that vconn_connect() reports
179 * 'expected_error'. */
180 static void
181 test_accept_then_close(int argc OVS_UNUSED, char *argv[])
182 {
183 const char *type = argv[1];
184 struct fake_pvconn fpv;
185 struct vconn *vconn;
186 int error;
187
188 fpv_create(type, &fpv);
189 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
190 DSCP_DEFAULT), 0);
191 vconn_run(vconn);
192 stream_close(fpv_accept(&fpv));
193 fpv_close(&fpv);
194
195 error = vconn_connect_block(vconn);
196 if (!strcmp(type, "tcp") || !strcmp(type, "unix")) {
197 if (error != ECONNRESET && error != EPIPE) {
198 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
199 error, strerror(error));
200 }
201 } else {
202 CHECK_ERRNO(error, EPROTO);
203 }
204
205 vconn_close(vconn);
206 fpv_destroy(&fpv);
207 }
208
209 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
210 * reads the hello message from it, then closes the connection and verifies
211 * that vconn_connect() reports 'expected_error'. */
212 static void
213 test_read_hello(int argc OVS_UNUSED, char *argv[])
214 {
215 const char *type = argv[1];
216 struct fake_pvconn fpv;
217 struct vconn *vconn;
218 struct stream *stream;
219 int error;
220
221 fpv_create(type, &fpv);
222 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
223 DSCP_DEFAULT), 0);
224 vconn_run(vconn);
225 stream = fpv_accept(&fpv);
226 fpv_destroy(&fpv);
227 for (;;) {
228 struct ofp_header hello;
229 int retval;
230
231 retval = stream_recv(stream, &hello, sizeof hello);
232 if (retval == sizeof hello) {
233 enum ofpraw raw;
234
235 CHECK(hello.version, OFP10_VERSION);
236 CHECK(ofpraw_decode_partial(&raw, &hello, sizeof hello), 0);
237 CHECK(raw, OFPRAW_OFPT_HELLO);
238 CHECK(ntohs(hello.length), sizeof hello);
239 break;
240 } else {
241 CHECK_ERRNO(retval, -EAGAIN);
242 }
243
244 vconn_run(vconn);
245 CHECK_ERRNO(vconn_connect(vconn), EAGAIN);
246 vconn_run_wait(vconn);
247 vconn_connect_wait(vconn);
248 stream_recv_wait(stream);
249 poll_block();
250 }
251 stream_close(stream);
252 error = vconn_connect_block(vconn);
253 if (error != ECONNRESET && error != EPIPE) {
254 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
255 error, strerror(error));
256 }
257 vconn_close(vconn);
258 }
259
260 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
261 * sends the 'out' bytes in 'out_size' to it (presumably an OFPT_HELLO
262 * message), then verifies that vconn_connect() reports
263 * 'expect_connect_error'. */
264 static void
265 test_send_hello(const char *type, const void *out, size_t out_size,
266 int expect_connect_error)
267 {
268 struct fake_pvconn fpv;
269 struct vconn *vconn;
270 bool read_hello, connected;
271 struct ofpbuf *msg;
272 struct stream *stream;
273 size_t n_sent;
274
275 fpv_create(type, &fpv);
276 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
277 DSCP_DEFAULT), 0);
278 vconn_run(vconn);
279 stream = fpv_accept(&fpv);
280 fpv_destroy(&fpv);
281
282 n_sent = 0;
283 while (n_sent < out_size) {
284 int retval;
285
286 retval = stream_send(stream, (char *) out + n_sent, out_size - n_sent);
287 if (retval > 0) {
288 n_sent += retval;
289 } else if (retval == -EAGAIN) {
290 stream_run(stream);
291 vconn_run(vconn);
292 stream_recv_wait(stream);
293 vconn_connect_wait(vconn);
294 vconn_run_wait(vconn);
295 poll_block();
296 } else {
297 ovs_fatal(0, "stream_send returned unexpected value %d", retval);
298 }
299 }
300
301 read_hello = connected = false;
302 for (;;) {
303 if (!read_hello) {
304 struct ofp_header hello;
305 int retval = stream_recv(stream, &hello, sizeof hello);
306 if (retval == sizeof hello) {
307 enum ofpraw raw;
308
309 CHECK(hello.version, OFP10_VERSION);
310 CHECK(ofpraw_decode_partial(&raw, &hello, sizeof hello), 0);
311 CHECK(raw, OFPRAW_OFPT_HELLO);
312 CHECK(ntohs(hello.length), sizeof hello);
313 read_hello = true;
314 } else {
315 CHECK_ERRNO(retval, -EAGAIN);
316 }
317 }
318
319 vconn_run(vconn);
320 if (!connected) {
321 int error = vconn_connect(vconn);
322 if (error == expect_connect_error) {
323 if (!error) {
324 connected = true;
325 } else {
326 stream_close(stream);
327 vconn_close(vconn);
328 return;
329 }
330 } else {
331 CHECK_ERRNO(error, EAGAIN);
332 }
333 }
334
335 if (read_hello && connected) {
336 break;
337 }
338
339 vconn_run_wait(vconn);
340 if (!connected) {
341 vconn_connect_wait(vconn);
342 }
343 if (!read_hello) {
344 stream_recv_wait(stream);
345 }
346 poll_block();
347 }
348 stream_close(stream);
349 CHECK_ERRNO(vconn_recv_block(vconn, &msg), EOF);
350 vconn_close(vconn);
351 }
352
353 /* Try connecting and sending a normal hello, which should succeed. */
354 static void
355 test_send_plain_hello(int argc OVS_UNUSED, char *argv[])
356 {
357 const char *type = argv[1];
358 struct ofpbuf *hello;
359
360 hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP10_VERSION,
361 htonl(0x12345678), 0);
362 test_send_hello(type, hello->data, hello->size, 0);
363 ofpbuf_delete(hello);
364 }
365
366 /* Try connecting and sending an extra-long hello, which should succeed (since
367 * the specification says that implementations must accept and ignore extra
368 * data). */
369 static void
370 test_send_long_hello(int argc OVS_UNUSED, char *argv[])
371 {
372 const char *type = argv[1];
373 struct ofpbuf *hello;
374 enum { EXTRA_BYTES = 8 };
375
376 hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP10_VERSION,
377 htonl(0x12345678), EXTRA_BYTES);
378 ofpbuf_put_zeros(hello, EXTRA_BYTES);
379 ofpmsg_update_length(hello);
380 test_send_hello(type, hello->data, hello->size, 0);
381 ofpbuf_delete(hello);
382 }
383
384 /* Try connecting and sending an echo request instead of a hello, which should
385 * fail with EPROTO. */
386 static void
387 test_send_echo_hello(int argc OVS_UNUSED, char *argv[])
388 {
389 const char *type = argv[1];
390 struct ofpbuf *echo;
391
392 echo = ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, OFP10_VERSION,
393 htonl(0x12345678), 0);
394 test_send_hello(type, echo->data, echo->size, EPROTO);
395 ofpbuf_delete(echo);
396 }
397
398 /* Try connecting and sending a hello packet that has its length field as 0,
399 * which should fail with EPROTO. */
400 static void
401 test_send_short_hello(int argc OVS_UNUSED, char *argv[])
402 {
403 const char *type = argv[1];
404 struct ofp_header hello;
405
406 memset(&hello, 0, sizeof hello);
407 test_send_hello(type, &hello, sizeof hello, EPROTO);
408 }
409
410 /* Try connecting and sending a hello packet that has a bad version, which
411 * should fail with EPROTO. */
412 static void
413 test_send_invalid_version_hello(int argc OVS_UNUSED, char *argv[])
414 {
415 const char *type = argv[1];
416 struct ofpbuf *hello;
417
418 hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP10_VERSION,
419 htonl(0x12345678), 0);
420 ((struct ofp_header *) hello->data)->version = 0;
421 test_send_hello(type, hello->data, hello->size, EPROTO);
422 ofpbuf_delete(hello);
423 }
424
425 static const struct command commands[] = {
426 {"refuse-connection", 1, 1, test_refuse_connection},
427 {"accept-then-close", 1, 1, test_accept_then_close},
428 {"read-hello", 1, 1, test_read_hello},
429 {"send-plain-hello", 1, 1, test_send_plain_hello},
430 {"send-long-hello", 1, 1, test_send_long_hello},
431 {"send-echo-hello", 1, 1, test_send_echo_hello},
432 {"send-short-hello", 1, 1, test_send_short_hello},
433 {"send-invalid-version-hello", 1, 1, test_send_invalid_version_hello},
434 {NULL, 0, 0, NULL},
435 };
436
437 int
438 main(int argc, char *argv[])
439 {
440 set_program_name(argv[0]);
441 vlog_set_levels(NULL, VLF_ANY_FACILITY, VLL_EMER);
442 vlog_set_levels(NULL, VLF_CONSOLE, VLL_DBG);
443 signal(SIGPIPE, SIG_IGN);
444
445 time_alarm(10);
446
447 run_command(argc - 1, argv + 1, commands);
448
449 return 0;
450 }