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