diff -r 40f7aa051acb -r 13cfc41f76a7 src/test.c --- a/src/test.c Thu Mar 12 21:12:48 2009 +0200 +++ b/src/test.c Thu Mar 12 21:23:33 2009 +0200 @@ -11,16 +11,16 @@ #include #include -void assert_strcmp (const char *a, const char *b) +void assert_strcmp (const char *is, const char *should_be) { - if (strcmp(a, b)) - FATAL("'%s' != '%s'", a, b); + if (strcmp(is, should_be)) + FATAL("'%s' != '%s'", is, should_be); } -void assert_strncmp (const char *a, const char *b, size_t n) +void assert_strncmp (const char *is, const char *should_be, size_t n) { - if (strncmp(a, b, n)) - FATAL("'%s':%d != '%s'", a, n, b); + if (strncmp(is, should_be, n)) + FATAL("'%s':%d != '%s'", is, n, should_be); } void assert_strlen (const char *str, size_t n) @@ -29,7 +29,7 @@ FATAL("strlen('%s') != %u", str, n); } -void assert_strnul (const char *str) +void assert_strnull (const char *str) { if (str != NULL) FATAL("'%s' != NULL", str); @@ -45,7 +45,13 @@ void assert_err (err_t err, err_t target) { if (err != target) - FATAL("error: <%s> != target <%s>", error_name(err), error_name(target)); + FATAL("error: <%s> != <%s>", error_name(err), error_name(target)); +} + +void assert_error_info (struct error_info *is, struct error_info *should_be) +{ + if (ERROR_CODE(is) != ERROR_CODE(should_be) || ERROR_EXTRA(is) != ERROR_EXTRA(should_be)) + FATAL("error: <%s> != <%s>", error_msg(is), error_msg(should_be)); } void assert_sock_read (struct sock_stream *sock, const char *str) @@ -135,14 +141,43 @@ assert_strcmp(line_buf, line_str); } else { - assert_strnul(line_buf); + assert_strnull(line_buf); } } +/** + * Context info for test_line_proto callbacks + */ +struct _lp_test_ctx { + /** Expected line */ + const char *line; + + /** Expected error */ + struct error_info err; +}; + +static void _lp_on_line (char *line, void *arg) +{ + struct _lp_test_ctx *ctx = arg; + + log_debug("'%s'", line); + + assert_strcmp(line, ctx->line); + + ctx->line = NULL; +} + +static void _lp_on_error (struct error_info *err, void *arg) +{ + struct _lp_test_ctx *ctx = arg; + + assert_error_info(err, &ctx->err); +} + static struct line_proto_callbacks _lp_callbacks = { - .on_line = NULL, - .on_error = NULL, + .on_line = &_lp_on_line, + .on_error = &_lp_on_error, }; void test_line_proto (void) @@ -156,6 +191,7 @@ { "\nfragment", 9 }, }, _trailing_data = { "\r\n", 2 }; struct line_proto *lp; + struct _lp_test_ctx ctx; struct error_info err; // put the read data @@ -163,7 +199,7 @@ sock_test_set_recv_buffer(sock, _read_data, 5, false); // create the lp - assert_success(line_proto_create(&lp, SOCK_TEST_BASE(sock), 128, &_lp_callbacks, NULL, &err)); + assert_success(line_proto_create(&lp, SOCK_TEST_BASE(sock), 128, &_lp_callbacks, &ctx, &err)); log_info("test line_proto_recv"); @@ -173,11 +209,12 @@ assert_read_line(lp, "this is a line"); assert_read_line(lp, NULL); - // then add a final bit + // then add a final bit to trigger on_line + log_info("test on_line"); + + ctx.line = "fragment"; sock_test_add_recv_vec(sock, _trailing_data); - - // read the final bit - assert_read_line(lp, "fragment"); + assert_strnull(ctx.line); // cleanup line_proto_release(lp);