168
|
1 |
/**
|
|
2 |
* @file
|
|
3 |
*
|
|
4 |
* Test functions for the line_proto module
|
|
5 |
*/
|
|
6 |
#include "test.h"
|
|
7 |
#include "transport.h"
|
|
8 |
|
|
9 |
#include "../line_proto.h"
|
|
10 |
|
|
11 |
void assert_read_line (struct line_proto *lp, const char *line_str)
|
|
12 |
{
|
|
13 |
char *line_buf;
|
|
14 |
|
|
15 |
log_debug("expect: %s", dump_str(line_str));
|
|
16 |
|
|
17 |
assert_success(line_proto_recv(lp, &line_buf));
|
|
18 |
|
|
19 |
if (line_str) {
|
|
20 |
assert_strcmp(line_buf, line_str);
|
|
21 |
|
|
22 |
} else {
|
|
23 |
assert_strnull(line_buf);
|
|
24 |
|
|
25 |
}
|
|
26 |
}
|
|
27 |
|
|
28 |
/**
|
|
29 |
* Context info for test_line_proto callbacks
|
|
30 |
*/
|
|
31 |
struct _lp_test_ctx {
|
|
32 |
/** Expected line */
|
|
33 |
const char *line;
|
|
34 |
|
|
35 |
/** Expected error */
|
|
36 |
struct error_info err;
|
|
37 |
};
|
|
38 |
|
|
39 |
static void _lp_on_line (char *line, void *arg)
|
|
40 |
{
|
|
41 |
struct _lp_test_ctx *ctx = arg;
|
|
42 |
|
|
43 |
log_debug("%s", dump_str(line));
|
|
44 |
|
|
45 |
assert_strcmp(line, ctx->line);
|
|
46 |
|
|
47 |
ctx->line = NULL;
|
|
48 |
}
|
|
49 |
|
|
50 |
static void _lp_on_error (struct error_info *err, void *arg)
|
|
51 |
{
|
|
52 |
struct _lp_test_ctx *ctx = arg;
|
|
53 |
|
|
54 |
assert_error(err, &ctx->err);
|
|
55 |
}
|
|
56 |
|
|
57 |
static struct line_proto_callbacks _lp_callbacks = {
|
|
58 |
.on_line = &_lp_on_line,
|
|
59 |
.on_error = &_lp_on_error,
|
|
60 |
};
|
|
61 |
|
|
62 |
void test_line_proto (void)
|
|
63 |
{
|
|
64 |
struct transport_test *tp = transport_test_create(NULL);
|
|
65 |
transport_t *transport = transport_test_cast(tp);
|
|
66 |
struct line_proto *lp;
|
|
67 |
struct _lp_test_ctx ctx;
|
|
68 |
struct error_info err;
|
|
69 |
|
|
70 |
// put the read data
|
|
71 |
log_debug("transport_test_push_*");
|
|
72 |
transport_test_push_str(tp, "hello\r\n");
|
|
73 |
transport_test_push_str(tp, "world\n");
|
|
74 |
transport_test_push_str(tp, "this ");
|
|
75 |
transport_test_push_str(tp, "is a line\r");
|
|
76 |
transport_test_push_str(tp, "\nfragment");
|
|
77 |
|
|
78 |
// create the lp
|
|
79 |
assert_success(line_proto_create(&lp, transport, 128, &_lp_callbacks, &ctx, &err));
|
|
80 |
|
|
81 |
log_info("test line_proto_recv");
|
|
82 |
|
|
83 |
// then read some lines from it
|
|
84 |
assert_read_line(lp, "hello");
|
|
85 |
assert_read_line(lp, "world");
|
|
86 |
assert_read_line(lp, "this is a line");
|
|
87 |
assert_read_line(lp, NULL);
|
|
88 |
|
|
89 |
// then add a final bit to trigger on_line
|
|
90 |
log_info("test on_line");
|
|
91 |
|
|
92 |
ctx.line = "fragment";
|
|
93 |
transport_test_push_str(tp, "\r\n");
|
|
94 |
assert_strnull(ctx.line);
|
|
95 |
|
|
96 |
// test writing
|
|
97 |
log_info("test line_proto_send");
|
|
98 |
assert_success(-line_proto_send(lp, "foobar\r\n"));
|
|
99 |
assert_success(-line_proto_send(lp, "quux\r\n"));
|
|
100 |
assert_transport_data(tp, "foobar\r\nquux\r\n");
|
|
101 |
|
|
102 |
// XXX: test partial writes
|
|
103 |
|
|
104 |
// cleanup
|
|
105 |
line_proto_destroy(lp);
|
|
106 |
}
|
|
107 |
|