/**
* @file
*
* Test functions for the line_proto module
*/
#include "test.h"
#include "transport.h"
#include "../line_proto.h"
void assert_read_line (struct line_proto *lp, const char *line_str)
{
char *line_buf;
log_debug("expect: %s", dump_str(line_str));
assert_success(line_proto_recv(lp, &line_buf));
if (line_str) {
assert_strcmp(line_buf, line_str);
} else {
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", dump_str(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(err, &ctx->err);
}
static struct line_proto_callbacks _lp_callbacks = {
.on_line = &_lp_on_line,
.on_error = &_lp_on_error,
};
void test_line_proto (void)
{
struct transport_test *tp = transport_test_create(NULL);
transport_t *transport = transport_test_cast(tp);
struct line_proto *lp;
struct _lp_test_ctx ctx;
struct error_info err;
// put the read data
log_debug("transport_test_push_*");
transport_test_push_str(tp, "hello\r\n");
transport_test_push_str(tp, "world\n");
transport_test_push_str(tp, "this ");
transport_test_push_str(tp, "is a line\r");
transport_test_push_str(tp, "\nfragment");
// create the lp
assert_success(line_proto_create(&lp, transport, 128, &_lp_callbacks, &ctx, &err));
log_info("test line_proto_recv");
// then read some lines from it
assert_read_line(lp, "hello");
assert_read_line(lp, "world");
assert_read_line(lp, "this is a line");
assert_read_line(lp, NULL);
// then add a final bit to trigger on_line
log_info("test on_line");
ctx.line = "fragment";
transport_test_push_str(tp, "\r\n");
assert_strnull(ctx.line);
// test writing
log_info("test line_proto_send");
assert_success(-line_proto_send(lp, "foobar\r\n"));
assert_success(-line_proto_send(lp, "quux\r\n"));
assert_transport_data(tp, "foobar\r\nquux\r\n");
// XXX: test partial writes
// cleanup
line_proto_destroy(lp);
}