82 log_debug("eof: %p", sock); |
82 log_debug("eof: %p", sock); |
83 |
83 |
84 assert_err(-sock_stream_read(sock, &buf, 1), ERR_READ_EOF); |
84 assert_err(-sock_stream_read(sock, &buf, 1), ERR_READ_EOF); |
85 } |
85 } |
86 |
86 |
|
87 void assert_sock_send_data (struct sock_test *sock, const char *data) |
|
88 { |
|
89 // get the data out |
|
90 char *buf; |
|
91 size_t len; |
|
92 |
|
93 sock_test_get_send_data(sock, &buf, &len); |
|
94 |
|
95 log_debug("get_send_data: '%*s'", (int) len, buf); |
|
96 |
|
97 // should be the same |
|
98 assert_strncmp(buf, data, len); |
|
99 assert_strlen(data, len); |
|
100 |
|
101 // cleanup |
|
102 free(buf); |
|
103 } |
|
104 |
87 void test_sock_test (void) |
105 void test_sock_test (void) |
88 { |
106 { |
89 struct sock_test *sock = sock_test_create(); |
107 struct sock_test *sock = sock_test_create(); |
90 struct io_vec _read_data[] = { |
108 struct io_vec _read_data[] = { |
91 { "foo", 3 }, |
109 { "foo", 3 }, |
108 // write the data in |
126 // write the data in |
109 log_info("test sock_test_write"); |
127 log_info("test sock_test_write"); |
110 |
128 |
111 assert_sock_write(SOCK_TEST_BASE(sock), "test "); |
129 assert_sock_write(SOCK_TEST_BASE(sock), "test "); |
112 assert_sock_write(SOCK_TEST_BASE(sock), "data"); |
130 assert_sock_write(SOCK_TEST_BASE(sock), "data"); |
113 |
131 |
114 // get the data out |
132 // check output |
115 char *data; |
133 assert_sock_send_data(sock, _write_data); |
116 size_t len; |
134 |
117 |
135 // check output |
118 log_info("test get_send_data"); |
136 assert_sock_send_data(sock, ""); |
119 |
|
120 sock_test_get_send_data(sock, &data, &len); |
|
121 |
|
122 // should be the same |
|
123 assert_strlen(_write_data, len); |
|
124 assert_strncmp(data, _write_data, len); |
|
125 |
137 |
126 // cleanup |
138 // cleanup |
127 free(data); |
|
128 sock_test_destroy(sock); |
139 sock_test_destroy(sock); |
129 } |
140 } |
130 |
141 |
131 void assert_read_line (struct line_proto *lp, const char *line_str) |
142 void assert_read_line (struct line_proto *lp, const char *line_str) |
132 { |
143 { |
214 |
225 |
215 ctx.line = "fragment"; |
226 ctx.line = "fragment"; |
216 sock_test_add_recv_vec(sock, _trailing_data); |
227 sock_test_add_recv_vec(sock, _trailing_data); |
217 assert_strnull(ctx.line); |
228 assert_strnull(ctx.line); |
218 |
229 |
|
230 // test writing |
|
231 log_info("test line_proto_send"); |
|
232 assert_success(-line_proto_send(lp, "foobar\r\n")); |
|
233 assert_success(-line_proto_send(lp, "quux\r\n")); |
|
234 assert_sock_send_data(sock, "foobar\r\nquux\r\n"); |
|
235 |
|
236 // XXX: test partial writes |
|
237 |
219 // cleanup |
238 // cleanup |
220 line_proto_release(lp); |
239 line_proto_release(lp); |
221 } |
240 } |
222 |
241 |
|
242 struct _test_irc_conn_ctx { |
|
243 bool on_registered, on_TEST; |
|
244 }; |
|
245 |
|
246 static void _conn_on_registered (struct irc_conn *conn, void *arg) |
|
247 { |
|
248 struct _test_irc_conn_ctx *ctx = arg; |
|
249 |
|
250 (void) conn; |
|
251 |
|
252 ctx->on_registered = true; |
|
253 |
|
254 log_debug("registered"); |
|
255 } |
|
256 |
|
257 static void _conn_on_TEST (const struct irc_line *line, void *arg) |
|
258 { |
|
259 struct _test_irc_conn_ctx *ctx = arg; |
|
260 |
|
261 assert_strcmp(line->prefix, "foobar-prefix"); |
|
262 assert_strcmp(line->command, "TEST"); |
|
263 assert_strcmp(line->args[0], "arg0"); |
|
264 assert_strnull(line->args[1]); |
|
265 |
|
266 ctx->on_TEST = true; |
|
267 |
|
268 log_debug("on_TEST"); |
|
269 } |
|
270 |
223 static struct irc_conn_callbacks _conn_callbacks = { |
271 static struct irc_conn_callbacks _conn_callbacks = { |
224 .on_registered = NULL, |
272 .on_registered = &_conn_on_registered, |
|
273 }; |
|
274 |
|
275 static struct irc_cmd_handler _conn_handlers[] = { |
|
276 { "TEST", &_conn_on_TEST }, |
|
277 { NULL, NULL } |
225 }; |
278 }; |
226 |
279 |
227 void test_irc_conn (void) |
280 void test_irc_conn (void) |
228 { |
281 { |
229 struct sock_test *sock; |
282 struct sock_test *sock; |
230 struct irc_conn *conn; |
283 struct irc_conn *conn; |
231 struct error_info err; |
284 struct error_info err; |
|
285 struct irc_conn_register_info register_info = { |
|
286 "nick", "user", "realname" |
|
287 }; |
|
288 struct _test_irc_conn_ctx ctx = { false, false }; |
232 |
289 |
233 // create the test socket |
290 // create the test socket |
234 assert((sock = sock_test_create())); |
291 assert((sock = sock_test_create())); |
235 |
292 |
236 // create the irc_conn |
293 // create the irc_conn |
237 assert_success(irc_conn_create(&conn, SOCK_TEST_BASE(sock), &_conn_callbacks, NULL, &err)); |
294 assert_success(irc_conn_create(&conn, SOCK_TEST_BASE(sock), &_conn_callbacks, &ctx, &err)); |
|
295 assert_success(irc_conn_add_cmd_handlers(conn, _conn_handlers, &ctx)); |
|
296 |
|
297 // test register |
|
298 log_info("test irc_conn_register"); |
|
299 assert_success(irc_conn_register(conn, ®ister_info)); |
|
300 assert_sock_send_data(sock, "NICK nick\r\nUSER user 0 * realname\r\n"); |
|
301 |
|
302 // test on_register callback |
|
303 log_info("test irc_conn_callbacks.on_register"); |
|
304 sock_test_add_recv_str(sock, "001 mynick :Blaa blaa blaa\r\n"); |
|
305 assert(ctx.on_registered); |
|
306 assert_strcmp(conn->nickname, "mynick"); |
|
307 |
|
308 // test on_TEST handler |
|
309 log_info("test irc_conn.handlers"); |
|
310 sock_test_add_recv_str(sock, ":foobar-prefix TEST arg0\r\n"); |
|
311 assert(ctx.on_TEST); |
238 |
312 |
239 // destroy it |
313 // destroy it |
240 irc_conn_destroy(conn); |
314 irc_conn_destroy(conn); |
241 } |
315 } |
242 |
316 |