vconn: Test SSL vconns too.
[sliver-openvswitch.git] / tests / test-vconn.c
1 /*
2  * Copyright (c) 2009, 2010 Nicira Networks.
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 "poll-loop.h"
25 #include "socket-util.h"
26 #include "stream.h"
27 #include "stream-ssl.h"
28 #include "timeval.h"
29 #include "util.h"
30 #include "vlog.h"
31
32 #undef NDEBUG
33 #include <assert.h>
34
35 struct fake_pvconn {
36     const char *type;
37     char *pvconn_name;
38     char *vconn_name;
39     struct pstream *pstream;
40 };
41
42 static void
43 check(int a, int b, const char *as, const char *file, int line)
44 {
45     if (a != b) {
46         ovs_fatal(0, "%s:%d: %s is %d but should be %d", file, line, as, a, b);
47     }
48 }
49
50
51 #define CHECK(A, B) check(A, B, #A, __FILE__, __LINE__)
52
53 static void
54 check_errno(int a, int b, const char *as, const char *file, int line)
55 {
56     if (a != b) {
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)));
59     }
60 }
61
62 #define CHECK_ERRNO(A, B) check_errno(A, B, #A, __FILE__, __LINE__)
63
64 static void
65 fpv_create(const char *type, struct fake_pvconn *fpv)
66 {
67     fpv->type = type;
68     if (!strcmp(type, "unix")) {
69         static int unix_count = 0;
70         char *bind_path;
71
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);
76         free(bind_path);
77     } else if (!strcmp(type, "tcp") || !strcmp(type, "ssl")) {
78         char *s, *method, *port, *save_ptr = NULL;
79         char *open_name;
80
81         open_name = xasprintf("p%s:0:127.0.0.1", type);
82         CHECK_ERRNO(pstream_open(open_name, &fpv->pstream), 0);
83
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);
88
89         /* Save info. */
90         fpv->pvconn_name = xstrdup(pstream_get_name(fpv->pstream));
91         fpv->vconn_name = xasprintf("%s:127.0.0.1:%s", type, port);
92
93         free(open_name);
94         free(s);
95     } else {
96         abort();
97     }
98 }
99
100 static struct stream *
101 fpv_accept(struct fake_pvconn *fpv)
102 {
103     struct stream *stream;
104
105     CHECK_ERRNO(pstream_accept_block(fpv->pstream, &stream), 0);
106
107     return stream;
108 }
109
110 static void
111 fpv_close(struct fake_pvconn *fpv)
112 {
113     pstream_close(fpv->pstream);
114     fpv->pstream = NULL;
115 }
116
117 static void
118 fpv_destroy(struct fake_pvconn *fpv)
119 {
120     fpv_close(fpv);
121     free(fpv->pvconn_name);
122     free(fpv->vconn_name);
123 }
124
125 /* Connects to a fake_pvconn with vconn_open(), then closes the listener and
126  * verifies that vconn_connect() reports 'expected_error'. */
127 static void
128 test_refuse_connection(const char *type, int expected_error)
129 {
130     struct fake_pvconn fpv;
131     struct vconn *vconn;
132
133     fpv_create(type, &fpv);
134     CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP_VERSION, &vconn), 0);
135     fpv_close(&fpv);
136     vconn_run(vconn);
137     CHECK_ERRNO(vconn_connect(vconn), expected_error);
138     vconn_close(vconn);
139     fpv_destroy(&fpv);
140 }
141
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'. */
145 static void
146 test_accept_then_close(const char *type, int expected_error)
147 {
148     struct fake_pvconn fpv;
149     struct vconn *vconn;
150
151     fpv_create(type, &fpv);
152     CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP_VERSION, &vconn), 0);
153     vconn_run(vconn);
154     stream_close(fpv_accept(&fpv));
155     fpv_close(&fpv);
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  * reads the hello message from it, then closes the connection and verifies
163  * that vconn_connect() reports 'expected_error'. */
164 static void
165 test_read_hello(const char *type, int expected_error)
166 {
167     struct fake_pvconn fpv;
168     struct vconn *vconn;
169     struct stream *stream;
170
171     fpv_create(type, &fpv);
172     CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP_VERSION, &vconn), 0);
173     vconn_run(vconn);
174     stream = fpv_accept(&fpv);
175     fpv_destroy(&fpv);
176     for (;;) {
177        struct ofp_header hello;
178        int retval;
179
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));
185            break;
186        } else {
187            CHECK_ERRNO(retval, -EAGAIN);
188        }
189
190        vconn_run(vconn);
191        CHECK_ERRNO(vconn_connect(vconn), EAGAIN);
192        vconn_run_wait(vconn);
193        vconn_connect_wait(vconn);
194        stream_recv_wait(stream);
195        poll_block();
196     }
197     stream_close(stream);
198     CHECK_ERRNO(vconn_connect(vconn), expected_error);
199     vconn_close(vconn);
200 }
201
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'. */
206 static void
207 test_send_hello(const char *type, const void *out, size_t out_size,
208                 int expect_connect_error)
209 {
210     struct fake_pvconn fpv;
211     struct vconn *vconn;
212     bool read_hello, connected;
213     struct ofpbuf *msg;
214     struct stream *stream;
215     size_t n_sent;
216
217     fpv_create(type, &fpv);
218     CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP_VERSION, &vconn), 0);
219     vconn_run(vconn);
220     stream = fpv_accept(&fpv);
221     fpv_destroy(&fpv);
222
223     n_sent = 0;
224     while (n_sent < out_size) {
225         int retval;
226
227         retval = stream_send(stream, (char *) out + n_sent, out_size - n_sent);
228         if (retval > 0) {
229             n_sent += retval;
230         } else if (retval == -EAGAIN) {
231             stream_run(stream);
232             vconn_run(vconn);
233             stream_recv_wait(stream);
234             vconn_connect_wait(vconn);
235             vconn_run_wait(vconn);
236             poll_block();
237         } else {
238             ovs_fatal(0, "stream_send returned unexpected value %d", retval);
239         }
240     }
241
242     read_hello = connected = false;
243     for (;;) {
244        if (!read_hello) {
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));
251                read_hello = true;
252            } else {
253                CHECK_ERRNO(retval, -EAGAIN);
254            }
255        }
256
257        vconn_run(vconn);
258        if (!connected) {
259            int error = vconn_connect(vconn);
260            if (error == expect_connect_error) {
261                if (!error) {
262                    connected = true;
263                } else {
264                    stream_close(stream);
265                    vconn_close(vconn);
266                    return;
267                }
268            } else {
269                CHECK_ERRNO(error, EAGAIN);
270            }
271        }
272
273        if (read_hello && connected) {
274            break;
275        }
276
277        vconn_run_wait(vconn);
278        if (!connected) {
279            vconn_connect_wait(vconn);
280        }
281        if (!read_hello) {
282            stream_recv_wait(stream);
283        }
284        poll_block();
285     }
286     stream_close(stream);
287     CHECK_ERRNO(vconn_recv(vconn, &msg), EOF);
288     vconn_close(vconn);
289 }
290
291 /* Try connecting and sending a normal hello, which should succeed. */
292 static void
293 test_send_plain_hello(const char *type)
294 {
295     struct ofp_header hello;
296
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);
302 }
303
304 /* Try connecting and sending an extra-long hello, which should succeed (since
305  * the specification says that implementations must accept and ignore extra
306  * data). */
307 static void
308 test_send_long_hello(const char *type)
309 {
310     struct ofp_header hello;
311     char buffer[sizeof hello * 2];
312
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);
320 }
321
322 /* Try connecting and sending an echo request instead of a hello, which should
323  * fail with EPROTO. */
324 static void
325 test_send_echo_hello(const char *type)
326 {
327     struct ofp_header echo;
328
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);
334 }
335
336 /* Try connecting and sending a hello packet that has its length field as 0,
337  * which should fail with EPROTO. */
338 static void
339 test_send_short_hello(const char *type)
340 {
341     struct ofp_header hello;
342
343     memset(&hello, 0, sizeof hello);
344     test_send_hello(type, &hello, sizeof hello, EPROTO);
345 }
346
347 /* Try connecting and sending a hello packet that has a bad version, which
348  * should fail with EPROTO. */
349 static void
350 test_send_invalid_version_hello(const char *type)
351 {
352     struct ofp_header hello;
353
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);
359 }
360
361 int
362 main(int argc UNUSED, char *argv[])
363 {
364     set_program_name(argv[0]);
365     time_init();
366     vlog_init();
367     signal(SIGPIPE, SIG_IGN);
368
369     time_alarm(10);
370
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");
379
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");
388
389 #ifdef HAVE_OPENSSL
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);
393
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 */
403
404     return 0;
405 }