From 5b3fb5bfa50a23d2a3d973a66b1a0d76195a3cca Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 29 Oct 2009 16:35:20 +0000 Subject: [PATCH] More documentation and unit tests for event_tagging. svn:r1476 --- event_tagging.c | 24 ++++++++++++++++++++++++ test/regress.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/event_tagging.c b/event_tagging.c index c5654d30..f1d7a992 100644 --- a/event_tagging.c +++ b/event_tagging.c @@ -67,6 +67,30 @@ #include "mm-internal.h" #include "util-internal.h" +/* + Here's our wire format: + + Stream = TaggedData* + + TaggedData = Tag Length Data + where the integer value of 'Length' is the length of 'data'. + + Tag = HByte* LByte + where HByte is a byte with the high bit set, and LByte is a byte + with the high bit clear. The integer value of the tag is taken + by concatenating the lower 7 bits from all the tags. So for example, + the tag 0x66 is encoded as [66], whereas the tag 0x166 is encoded as + [82 66] + + Length = Integer + + Integer = NNibbles Nibble* Padding? + where NNibbles is a 4-bit value encoding the number of nibbles-1, + and each Nibble is 4 bits worth of encoded integer, in big-endian + order. If the total encoded integer size is an odd number of nibbles, + a final padding nibble with value 0 is appended. +*/ + int evtag_decode_int(ev_uint32_t *pnumber, struct evbuffer *evbuf); int evtag_decode_int64(ev_uint64_t *pnumber, struct evbuffer *evbuf); int evtag_encode_tag(struct evbuffer *evbuf, ev_uint32_t tag); diff --git a/test/regress.c b/test/regress.c index e1929580..b34bb744 100644 --- a/test/regress.c +++ b/test/regress.c @@ -1464,6 +1464,33 @@ end: evbuffer_free(tmp); } +static void +evtag_test_peek(void *ptr) +{ + struct evbuffer *tmp = evbuffer_new(); + ev_uint32_t u32; + + evtag_marshal_int(tmp, 30, 0); + evtag_marshal_string(tmp, 40, "Hello world"); + + tt_int_op(evtag_peek(tmp, &u32), ==, 1); + tt_int_op(u32, ==, 30); + tt_int_op(evtag_peek_length(tmp, &u32), ==, 0); + tt_int_op(u32, ==, 1+1+1); + tt_int_op(evtag_consume(tmp), ==, 0); + + tt_int_op(evtag_peek(tmp, &u32), ==, 1); + tt_int_op(u32, ==, 40); + tt_int_op(evtag_peek_length(tmp, &u32), ==, 0); + tt_int_op(u32, ==, 1+1+11); + tt_int_op(evtag_payload_length(tmp, &u32), ==, 0); + tt_int_op(u32, ==, 11); + +end: + evbuffer_free(tmp); +} + + static void test_methods(void *ptr) @@ -1832,6 +1859,7 @@ struct testcase_t evtag_testcases[] = { { "int", evtag_int_test, TT_FORK, NULL, NULL }, { "fuzz", evtag_fuzz, TT_FORK, NULL, NULL }, { "encoding", evtag_tag_encoding, TT_FORK, NULL, NULL }, + { "peek", evtag_test_peek, 0, NULL, NULL }, END_OF_TESTCASES }; -- 2.40.0