Annotation of embedaddon/strongswan/src/libstrongswan/tests/suites/test_stream.c, revision 1.1.1.1
1.1 misho 1: /*
2: * Copyright (C) 2013 Martin Willi
3: * Copyright (C) 2013 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 <unistd.h>
19:
20: static char* services[] = {
21: #ifndef WIN32
22: "unix:///tmp/strongswan-test-service.sck",
23: #endif
24: "tcp://127.0.0.1:7766",
25: "tcp://[::1]:7766",
26: };
27:
28: static char msg[] = "testmessage";
29: static int msglen = 12;
30:
31: static bool servicing(void *data, stream_t *stream)
32: {
33: char buf[64];
34: ssize_t len, total;
35:
36: ck_assert(streq((char*)data, "test"));
37:
38: for (total = 0; total < msglen;)
39: {
40: len = stream->read(stream, buf, sizeof(buf), TRUE);
41: ck_assert(len > 0);
42: total += len;
43: }
44: for (total = 0; total < msglen;)
45: {
46: len = stream->write(stream, buf, len, TRUE);
47: ck_assert(len > 0);
48: total += len;
49: }
50:
51: return FALSE;
52: }
53:
54: START_TEST(test_sync)
55: {
56: char buf[64];
57: stream_service_t *service;
58: stream_t *stream;
59: ssize_t len, total;
60:
61: lib->processor->set_threads(lib->processor, 8);
62:
63: service = lib->streams->create_service(lib->streams, services[_i], 1);
64: ck_assert(service != NULL);
65: service->on_accept(service, servicing, "test", JOB_PRIO_HIGH, 1);
66:
67: stream = lib->streams->connect(lib->streams, services[_i]);
68: ck_assert(stream != NULL);
69: for (total = 0; total < msglen;)
70: {
71: len = stream->write(stream, msg, msglen, TRUE);
72: ck_assert(len > 0);
73: total += len;
74: }
75: for (total = 0; total < msglen;)
76: {
77: len = stream->read(stream, buf, sizeof(buf), TRUE);
78: ck_assert(len > 0);
79: total += len;
80: }
81: ck_assert(streq(buf, msg));
82: stream->destroy(stream);
83:
84: service->destroy(service);
85: }
86: END_TEST
87:
88: static bool on_write(void *data, stream_t *stream)
89: {
90: ssize_t len, total;
91:
92: ck_assert(streq((char*)data, "test-write"));
93: for (total = 0; total < msglen;)
94: {
95: len = stream->write(stream, msg, msglen, TRUE);
96: ck_assert(len > 0);
97: total += len;
98: }
99: return FALSE;
100: }
101:
102: static bool read_done = FALSE;
103:
104: static bool on_read(void *data, stream_t *stream)
105: {
106: ssize_t len, total;
107: char buf[64];
108:
109: ck_assert(streq((char*)data, "test-read"));
110: for (total = 0; total < msglen;)
111: {
112: len = stream->read(stream, buf, sizeof(buf), TRUE);
113: ck_assert(len > 0);
114: total += len;
115: }
116: ck_assert(streq(buf, msg));
117: read_done = TRUE;
118: return FALSE;
119: }
120:
121: START_TEST(test_async)
122: {
123: stream_service_t *service;
124: stream_t *stream;
125:
126: lib->processor->set_threads(lib->processor, 8);
127:
128: service = lib->streams->create_service(lib->streams, services[_i], 1);
129: ck_assert(service != NULL);
130: service->on_accept(service, servicing, "test", JOB_PRIO_HIGH, 0);
131:
132: stream = lib->streams->connect(lib->streams, services[_i]);
133: ck_assert(stream != NULL);
134: read_done = FALSE;
135: stream->on_write(stream, (stream_cb_t)on_write, "test-write");
136: stream->on_read(stream, (stream_cb_t)on_read, "test-read");
137:
138: while (!read_done)
139: {
140: usleep(1000);
141: }
142: stream->destroy(stream);
143:
144: service->destroy(service);
145: }
146: END_TEST
147:
148: static bool all(void *data, stream_t *stream)
149: {
150: char buf[64], *pos;
151: ssize_t len;
152: int i;
153:
154: pos = buf;
155: for (i = 0; i < msglen; i++)
156: {
157: len = stream->read(stream, pos, 1, TRUE);
158: ck_assert_int_eq(len, 1);
159: pos += len;
160: }
161: pos = buf;
162: for (i = 0; i < msglen; i++)
163: {
164: len = stream->write(stream, pos, 1, TRUE);
165: ck_assert_int_eq(len, 1);
166: pos += len;
167: }
168:
169: return FALSE;
170: }
171:
172: START_TEST(test_all)
173: {
174: char buf[64];
175: stream_service_t *service;
176: stream_t *stream;
177:
178: lib->processor->set_threads(lib->processor, 8);
179:
180: service = lib->streams->create_service(lib->streams, services[_i], 1);
181: ck_assert(service != NULL);
182: service->on_accept(service, all, NULL, JOB_PRIO_HIGH, 1);
183:
184: stream = lib->streams->connect(lib->streams, services[_i]);
185: ck_assert(stream != NULL);
186: ck_assert(stream->write_all(stream, msg, msglen));
187: ck_assert(stream->read_all(stream, buf, msglen));
188: ck_assert(streq(buf, msg));
189: stream->destroy(stream);
190:
191: service->destroy(service);
192: }
193: END_TEST
194:
195: static bool concurrency(void *data, stream_t *stream)
196: {
197: static refcount_t refs = 0;
198: u_int current;
199: ssize_t len;
200:
201: current = ref_get(&refs);
202: ck_assert(current <= 3);
203: len = stream->write(stream, "x", 1, TRUE);
204: ck_assert_int_eq(len, 1);
205: usleep(1000);
206: ignore_result(ref_put(&refs));
207:
208: return FALSE;
209: }
210:
211: START_TEST(test_concurrency)
212: {
213: stream_service_t *service;
214: stream_t *streams[10];
215: ssize_t len;
216: char x;
217: int i;
218:
219: lib->processor->set_threads(lib->processor, 8);
220:
221: service = lib->streams->create_service(lib->streams, services[_i], 10);
222: ck_assert(service != NULL);
223: service->on_accept(service, concurrency, NULL, JOB_PRIO_HIGH, 3);
224:
225: for (i = 0; i < countof(streams); i++)
226: {
227: streams[i] = lib->streams->connect(lib->streams, services[_i]);
228: ck_assert(streams[i] != NULL);
229: }
230: for (i = 0; i < countof(streams); i++)
231: {
232: len = streams[i]->read(streams[i], &x, 1, TRUE);
233: ck_assert_int_eq(len, 1);
234: ck_assert_int_eq(x, 'x');
235: }
236: for (i = 0; i < countof(streams); i++)
237: {
238: streams[i]->destroy(streams[i]);
239: }
240: service->destroy(service);
241: }
242: END_TEST
243:
244: Suite *stream_suite_create()
245: {
246: Suite *s;
247: TCase *tc;
248:
249: s = suite_create("stream");
250:
251: tc = tcase_create("sync");
252: tcase_add_loop_test(tc, test_sync, 0, countof(services));
253: suite_add_tcase(s, tc);
254:
255: tc = tcase_create("async");
256: tcase_add_loop_test(tc, test_async, 0, countof(services));
257: suite_add_tcase(s, tc);
258:
259: tc = tcase_create("all");
260: tcase_add_loop_test(tc, test_all, 0, countof(services));
261: suite_add_tcase(s, tc);
262:
263: tc = tcase_create("concurrency");
264: tcase_add_loop_test(tc, test_concurrency, 0, countof(services));
265: suite_add_tcase(s, tc);
266:
267: return s;
268: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>