]>
git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libstrongswan/tests/suites/test_watcher.c
2 * Copyright (C) 2013 Martin Willi
3 * Copyright (C) 2013 revosec AG
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>.
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
16 #include "test_suite.h"
23 static char testbuf
[1] = "";
25 static bool readcb(void *data
, int fd
, watcher_event_t event
)
27 ck_assert_int_eq(*(int*)data
, fd
);
28 ck_assert_int_eq(event
, WATCHER_READ
);
30 if (recv(fd
, testbuf
, 1, MSG_DONTWAIT
) != 1)
32 ck_assert(errno
== EAGAIN
|| errno
== EWOULDBLOCK
);
42 lib
->processor
->set_threads(lib
->processor
, 8);
44 ck_assert(socketpair(AF_UNIX
, SOCK_STREAM
, 0, fd
) != -1);
46 lib
->watcher
->add(lib
->watcher
, fd
[0], WATCHER_READ
, readcb
, &fd
[0]);
48 for (c
= 'a'; c
<= 'z'; c
++)
50 ck_assert_int_eq(send(fd
[1], &c
, 1, 0), 1);
51 while (testbuf
[0] != c
)
57 lib
->watcher
->remove(lib
->watcher
, fd
[0]);
61 lib
->processor
->cancel(lib
->processor
);
65 static bool writecb(void *data
, int fd
, watcher_event_t event
)
67 ck_assert_int_eq(event
, WATCHER_WRITE
);
68 if (send(fd
, data
, 1, MSG_DONTWAIT
) != 1)
70 ck_assert(errno
== EAGAIN
|| errno
== EWOULDBLOCK
);
75 START_TEST(test_write
)
80 lib
->processor
->set_threads(lib
->processor
, 8);
82 ck_assert(socketpair(AF_UNIX
, SOCK_STREAM
, 0, fd
) != -1);
84 lib
->watcher
->add(lib
->watcher
, fd
[1], WATCHER_WRITE
, writecb
, &in
);
86 ck_assert_int_eq(recv(fd
[0], &out
, 1, 0), 1);
87 ck_assert_int_eq(out
, in
);
89 lib
->watcher
->remove(lib
->watcher
, fd
[1]);
93 lib
->processor
->cancel(lib
->processor
);
97 static bool multiread(void *data
, int fd
, watcher_event_t event
)
99 ck_assert_int_eq(event
, WATCHER_READ
);
100 if (recv(fd
, data
, 1, MSG_DONTWAIT
) != 1)
102 ck_assert(errno
== EAGAIN
|| errno
== EWOULDBLOCK
);
107 START_TEST(test_multiread
)
110 char in
, out
[countof(fd
)];
112 lib
->processor
->set_threads(lib
->processor
, 8);
114 for (i
= 0; i
< countof(fd
); i
++)
116 ck_assert(socketpair(AF_UNIX
, SOCK_STREAM
, 0, fd
[i
]) != -1);
117 lib
->watcher
->add(lib
->watcher
, fd
[i
][0],
118 WATCHER_READ
, multiread
, &out
[i
]);
121 for (i
= 0; i
< countof(fd
); i
++)
123 for (in
= 'a'; in
<= 'z'; in
++)
125 ck_assert_int_eq(send(fd
[i
][1], &in
, 1, 0), 1);
133 for (i
= 0; i
< countof(fd
); i
++)
135 lib
->watcher
->remove(lib
->watcher
, fd
[i
][0]);
140 lib
->processor
->cancel(lib
->processor
);
144 static bool multiwrite(void *data
, int fd
, watcher_event_t event
)
146 ck_assert_int_eq(event
, WATCHER_WRITE
);
147 if (send(fd
, data
, 1, MSG_DONTWAIT
) != 1)
149 ck_assert(errno
== EAGAIN
|| errno
== EWOULDBLOCK
);
154 START_TEST(test_multiwrite
)
157 u_char out
, in
[countof(fd
)];
159 lib
->processor
->set_threads(lib
->processor
, 8);
161 for (i
= 0; i
< countof(fd
); i
++)
163 ck_assert(socketpair(AF_UNIX
, SOCK_STREAM
, 0, fd
[i
]) != -1);
165 lib
->watcher
->add(lib
->watcher
, fd
[i
][1],
166 WATCHER_WRITE
, multiwrite
, &in
[i
]);
169 for (j
= 0; j
< 10; j
++)
171 for (i
= 0; i
< countof(fd
); i
++)
173 ck_assert_int_eq(recv(fd
[i
][0], &out
, 1, 0), 1);
174 ck_assert_int_eq(out
, i
);
178 for (i
= 0; i
< countof(fd
); i
++)
180 lib
->watcher
->remove(lib
->watcher
, fd
[i
][1]);
185 lib
->processor
->cancel(lib
->processor
);
189 Suite
*watcher_suite_create()
194 s
= suite_create("watcher");
196 tc
= tcase_create("read");
197 tcase_add_test(tc
, test_read
);
198 suite_add_tcase(s
, tc
);
200 tc
= tcase_create("write");
201 tcase_add_test(tc
, test_write
);
202 suite_add_tcase(s
, tc
);
204 tc
= tcase_create("multiread");
205 tcase_add_test(tc
, test_multiread
);
206 suite_add_tcase(s
, tc
);
208 tc
= tcase_create("multiwrite");
209 tcase_add_test(tc
, test_multiwrite
);
210 suite_add_tcase(s
, tc
);