Annotation of embedaddon/strongswan/src/libcharon/plugins/vici/suites/test_socket.c, revision 1.1.1.1
1.1 misho 1: /*
2: * Copyright (C) 2014 Martin Willi
3: * Copyright (C) 2014 revosec AG
4: *
5: * This program is free software; you can redistribute it and/or modify it
6: * under the terms of the GNU General Public License as published by the
7: * Free Software Foundation; either version 2 of the License, or (at your
8: * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9: *
10: * This program is distributed in the hope that it will be useful, but
11: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13: * for more details.
14: */
15:
16: #include <test_suite.h>
17:
18: #include "../vici_socket.h"
19:
20: #include <unistd.h>
21:
22: typedef struct {
23: vici_socket_t *s;
24: int disconnect;
25: int bytes;
26: u_int id;
27: } test_data_t;
28:
29: static void echo_inbound(void *user, u_int id, chunk_t buf)
30: {
31: test_data_t *data = user;
32:
33: ck_assert_int_eq(data->id, id);
34: /* count number of bytes, including the header */
35: data->bytes += buf.len + sizeof(uint32_t);
36: /* echo back data chunk */
37: data->s->send(data->s, id, chunk_clone(buf));
38: }
39:
40: static void echo_connect(void *user, u_int id)
41: {
42: test_data_t *data = user;
43:
44: data->id = id;
45: }
46:
47: static void echo_disconnect(void *user, u_int id)
48: {
49: test_data_t *data = user;
50:
51: ck_assert(id == data->id);
52: data->disconnect++;
53: }
54:
55: static struct {
56: char *uri;
57: u_int chunksize;
58: } echo_tests[] = {
59: { "tcp://127.0.0.1:6543", ~0 },
60: { "tcp://127.0.0.1:6543", 1 },
61: { "tcp://127.0.0.1:6543", 2 },
62: { "tcp://127.0.0.1:6543", 3 },
63: { "tcp://127.0.0.1:6543", 7 },
64: #ifndef WIN32
65: { "unix:///tmp/strongswan-tests-vici-socket", ~0 },
66: { "unix:///tmp/strongswan-tests-vici-socket", 1 },
67: { "unix:///tmp/strongswan-tests-vici-socket", 2 },
68: { "unix:///tmp/strongswan-tests-vici-socket", 3 },
69: { "unix:///tmp/strongswan-tests-vici-socket", 7 },
70: #endif /* !WIN32 */
71: };
72:
73: START_TEST(test_echo)
74: {
75: stream_t *c;
76: test_data_t data = {};
77: chunk_t x, m = chunk_from_chars(
78: 0x00,0x00,0x00,0x00,
79: 0x00,0x00,0x00,0x01, 0x01,
80: 0x00,0x00,0x00,0x05, 0x11,0x12,0x13,0x14,0x15,
81: 0x00,0x00,0x00,0x0A, 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x02A,
82: );
83: char buf[m.len];
84: uint32_t len;
85:
86: lib->processor->set_threads(lib->processor, 4);
87:
88: /* create socket, connect with stream */
89: data.s = vici_socket_create(echo_tests[_i].uri, echo_inbound, echo_connect,
90: echo_disconnect, &data);
91: ck_assert(data.s != NULL);
92: c = lib->streams->connect(lib->streams, echo_tests[_i].uri);
93: ck_assert(c != NULL);
94:
95: /* write arbitrary chunks of messages blob depending on test */
96: x = m;
97: while (x.len)
98: {
99: len = min(x.len, echo_tests[_i].chunksize);
100: ck_assert(c->write_all(c, x.ptr, len));
101: x = chunk_skip(x, len);
102: }
103:
104: /* verify echo */
105: ck_assert(c->read_all(c, buf, sizeof(buf)));
106: ck_assert(chunk_equals(m, chunk_from_thing(buf)));
107:
108: /* wait for completion */
109: c->destroy(c);
110: while (data.disconnect != 1)
111: {
112: usleep(1000);
113: }
114: /* check that we got correct number of bytes/invocations */
115: ck_assert_int_eq(data.bytes, m.len);
116:
117: data.s->destroy(data.s);
118: }
119: END_TEST
120:
121: Suite *socket_suite_create()
122: {
123: Suite *s;
124: TCase *tc;
125:
126: s = suite_create("vici socket");
127:
128: tc = tcase_create("echo");
129: tcase_add_loop_test(tc, test_echo, 0, countof(echo_tests));
130: suite_add_tcase(s, tc);
131:
132: return s;
133: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>