diff options
author | Nick Mathewson <nickm@torproject.org> | 2017-09-28 08:35:30 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2017-09-28 08:35:30 -0400 |
commit | 99e52711bb46123b49b02386db0464d351822e63 (patch) | |
tree | 1d1d73194810dd81d3e748fe67561139672ae8dd | |
parent | 01d67a90711f084c6ec1971315f7486af2797faf (diff) | |
download | tor-99e52711bb46123b49b02386db0464d351822e63.tar.gz tor-99e52711bb46123b49b02386db0464d351822e63.zip |
Add unit test for buf_get_line().
-rw-r--r-- | src/test/test_proto_misc.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/test/test_proto_misc.c b/src/test/test_proto_misc.c index b7eebaf0a0..263ca47447 100644 --- a/src/test/test_proto_misc.c +++ b/src/test/test_proto_misc.c @@ -207,10 +207,56 @@ test_proto_ext_or_cmd(void *arg) tor_free(tmp); } +static void +test_proto_line(void *arg) +{ + (void)arg; + char tmp[60]; + buf_t *buf = buf_new(); +#define S(str) str, sizeof(str)-1 + const struct { + const char *input; + size_t input_len; + size_t line_len; + const char *output; + int returnval; + } cases[] = { + { S("Hello world"), 0, NULL, 0 }, + { S("Hello world\n"), 12, "Hello world\n", 1 }, + { S("Hello world\nMore"), 12, "Hello world\n", 1 }, + { S("\n oh hello world\nMore"), 1, "\n", 1 }, + { S("Hello worpd\n\nMore"), 12, "Hello worpd\n", 1 }, + { S("------------------------------------------------------------\n"), 0, + NULL, -1 }, + }; + unsigned i; + for (i = 0; i < ARRAY_LENGTH(cases); ++i) { + buf_add(buf, cases[i].input, cases[i].input_len); + memset(tmp, 0xfe, sizeof(tmp)); + size_t sz = sizeof(tmp); + int rv = buf_get_line(buf, tmp, &sz); + tt_int_op(rv, OP_EQ, cases[i].returnval); + if (rv == 1) { + tt_int_op(sz, OP_LT, sizeof(tmp)); + tt_mem_op(cases[i].output, OP_EQ, tmp, sz+1); + tt_int_op(buf_datalen(buf), OP_EQ, cases[i].input_len - strlen(tmp)); + tt_int_op(sz, OP_EQ, cases[i].line_len); + } else { + tt_int_op(buf_datalen(buf), OP_EQ, cases[i].input_len); + // tt_int_op(sz, OP_EQ, sizeof(tmp)); + } + buf_clear(buf); + } + + done: + buf_free(buf); +} + struct testcase_t proto_misc_tests[] = { { "var_cell", test_proto_var_cell, 0, NULL, NULL }, { "control0", test_proto_control0, 0, NULL, NULL }, { "ext_or_cmd", test_proto_ext_or_cmd, TT_FORK, NULL, NULL }, + { "line", test_proto_line, 0, NULL, NULL }, END_OF_TESTCASES }; |