From c4e60994a2cf955908f236b4945ea7288950ec45 Mon Sep 17 00:00:00 2001 From: Niels Provos Date: Mon, 22 Aug 2005 01:34:34 +0000 Subject: [PATCH] including the tagging code that is required by event_rpcgen.py; test the new functionality. svn:r172 --- Makefile.am | 8 +- event.h | 40 ++++++ event_rpcgen.py | 59 ++++++--- event_tagging.c | 338 +++++++++++++++++++++++++++++++++++++++++++++++ test/Makefile.am | 21 ++- test/regress.c | 146 ++++++++++++++++++++ 6 files changed, 584 insertions(+), 28 deletions(-) create mode 100644 event_tagging.c diff --git a/Makefile.am b/Makefile.am index e08bc03f..b861df4b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,9 +2,11 @@ AUTOMAKE_OPTIONS = foreign no-dependencies SUBDIRS = . sample test +bin_SCRIPTS = event_rpcgen.py + EXTRA_DIST = acconfig.h event.h event-internal.h log.h evsignal.h event.3 \ kqueue.c epoll_sub.c epoll.c select.c rtsig.c poll.c signal.c \ - devpoll.c \ + devpoll.c event_rpcgen.py \ sample/Makefile.am sample/Makefile.in sample/event-test.c \ sample/signal-test.c sample/time-test.c \ test/Makefile.am test/Makefile.in test/bench.c test/regress.c \ @@ -21,9 +23,9 @@ EXTRA_DIST = acconfig.h event.h event-internal.h log.h evsignal.h event.3 \ lib_LTLIBRARIES = libevent.la -libevent_la_SOURCES = event.c buffer.c evbuffer.c log.c +libevent_la_SOURCES = event.c buffer.c evbuffer.c log.c event_tagging.c libevent_la_LIBADD = @LTLIBOBJS@ -libevent_la_LDFLAGS = -release @VERSION@ -version-info 1:2:0 +libevent_la_LDFLAGS = -release @VERSION@ -version-info 1:3:0 include_HEADERS = event.h diff --git a/event.h b/event.h index 857dd376..18e8d9da 100644 --- a/event.h +++ b/event.h @@ -270,6 +270,46 @@ int evbuffer_read(struct evbuffer *, int, int); u_char *evbuffer_find(struct evbuffer *, u_char *, size_t); void evbuffer_setcb(struct evbuffer *, void (*)(struct evbuffer *, size_t, size_t, void *), void *); +/* + * Marshaling tagged data - We assume that all tags are inserted in their + * numeric order - so that unknown tags will always be higher than the + * known ones - and we can just ignore the end of an event buffer. + */ + +void evtag_init(void); + +void evtag_marshal(struct evbuffer *evbuf, uint8_t tag, const void *data, + uint16_t len); + +void encode_int(struct evbuffer *evbuf, uint32_t number); + +void evtag_marshal_int(struct evbuffer *evbuf, uint8_t tag, uint32_t integer); + +void evtag_marshal_string(struct evbuffer *buf, uint8_t tag, + const char *string); + +void evtag_marshal_timeval(struct evbuffer *evbuf, uint8_t tag, + struct timeval *tv); + +void evtag_test(void); + +int evtag_unmarshal(struct evbuffer *src, uint8_t *ptag, struct evbuffer *dst); +int evtag_peek(struct evbuffer *evbuf, uint8_t *ptag); +int evtag_peek_length(struct evbuffer *evbuf, uint32_t *plength); +int evtag_consume(struct evbuffer *evbuf); + +int evtag_unmarshal_int(struct evbuffer *evbuf, uint8_t need_tag, + uint32_t *pinteger); + +int evtag_unmarshal_fixed(struct evbuffer *src, uint8_t need_tag, void *data, + size_t len); + +int evtag_unmarshal_string(struct evbuffer *evbuf, uint8_t need_tag, + char **pstring); + +int evtag_unmarshal_timeval(struct evbuffer *evbuf, uint8_t need_tag, + struct timeval *ptv); + #ifdef __cplusplus } #endif diff --git a/event_rpcgen.py b/event_rpcgen.py index 7cba2b9f..f2cc3da5 100755 --- a/event_rpcgen.py +++ b/event_rpcgen.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# # Copyright (c) 2005 Niels Provos # All rights reserved. # @@ -89,9 +90,9 @@ class Struct: self._name, self._name) + 'int %s_complete(struct %s *);' % (self._name, self._name) ) - print >>file, ('void tag_marshal_%s(struct evbuffer *, uint8_t, ' + print >>file, ('void evtag_marshal_%s(struct evbuffer *, uint8_t, ' 'struct %s *);') % ( self._name, self._name) - print >>file, ('int tag_unmarshal_%s(struct evbuffer *, uint8_t, ' + print >>file, ('int evtag_unmarshal_%s(struct evbuffer *, uint8_t, ' 'struct %s *);') % ( self._name, self._name) # Write a setting function of every variable @@ -185,7 +186,7 @@ class Struct: '{\n' ' uint8_t tag;\n' ' while (EVBUFFER_LENGTH(evbuf) > 0) {\n' - ' if (tag_peek(evbuf, &tag) == -1)\n' + ' if (evtag_peek(evbuf, &tag) == -1)\n' ' return (-1);\n' ' switch (tag) {\n' ) @@ -229,7 +230,7 @@ class Struct: # Complete message unmarshaling print >>file, ( 'int\n' - 'tag_unmarshal_%s(struct evbuffer *evbuf, uint8_t need_tag, ' + 'evtag_unmarshal_%s(struct evbuffer *evbuf, uint8_t need_tag, ' ' struct %s *msg)' ) % (self._name, self._name) print >>file, ( @@ -239,7 +240,8 @@ class Struct: '\n' ' struct evbuffer *tmp = evbuffer_new();\n' '\n' - ' if (tag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)\n' + ' if (evtag_unmarshal(evbuf, &tag, tmp) == -1' + ' || tag != need_tag)\n' ' goto error;\n' '\n' ' if (%s_unmarshal(msg, tmp) == -1)\n' @@ -255,14 +257,14 @@ class Struct: # Complete message marshaling print >>file, ( 'void\n' - 'tag_marshal_%s(struct evbuffer *evbuf, uint8_t tag, ' + 'evtag_marshal_%s(struct evbuffer *evbuf, uint8_t tag, ' 'struct %s *msg)\n' % (self._name, self._name) + '{\n' ' if (_buf == NULL)\n' ' _buf = evbuffer_new();\n' ' evbuffer_drain(_buf, -1);\n' ' %s_marshal(_buf, msg);\n' % self._name + - ' tag_marshal(evbuf, tag, EVBUFFER_DATA(_buf), ' + ' evtag_marshal(evbuf, tag, EVBUFFER_DATA(_buf), ' 'EVBUFFER_LENGTH(_buf));\n' '}\n' ) @@ -435,7 +437,7 @@ class EntryBytes(Entry): return code def CodeUnmarshal(self, buf, tag_name, var_name): - code = [ 'if (tag_unmarshal_fixed(%s, %s, ' % (buf, tag_name) + + code = [ 'if (evtag_unmarshal_fixed(%s, %s, ' % (buf, tag_name) + '%s->%s_data, ' % (var_name, self._name) + 'sizeof(%s->%s_data)) == -1)' % ( var_name, self._name), @@ -443,7 +445,7 @@ class EntryBytes(Entry): return code def CodeMarshal(self, buf, tag_name, var_name): - code = ['tag_marshal(%s, %s, %s->%s_data, sizeof(%s->%s_data));' % ( + code = ['evtag_marshal(%s, %s, %s->%s_data, sizeof(%s->%s_data));' % ( buf, tag_name, var_name, self._name, var_name, self._name )] return code @@ -476,13 +478,13 @@ class EntryInt(Entry): self._ctype = 'uint32_t' def CodeUnmarshal(self, buf, tag_name, var_name): - code = ['if (tag_unmarshal_int(%s, %s, &%s->%s_data) == -1)' % ( + code = ['if (evtag_unmarshal_int(%s, %s, &%s->%s_data) == -1)' % ( buf, tag_name, var_name, self._name), ' return (-1);'] return code def CodeMarshal(self, buf, tag_name, var_name): - code = ['tag_marshal_int(%s, %s, %s->%s_data);' % ( + code = ['evtag_marshal_int(%s, %s, %s->%s_data);' % ( buf, tag_name, var_name, self._name)] return code @@ -515,13 +517,13 @@ class EntryString(Entry): return code def CodeUnmarshal(self, buf, tag_name, var_name): - code = ['if (tag_unmarshal_string(%s, %s, &%s->%s_data) == -1)' % ( + code = ['if (evtag_unmarshal_string(%s, %s, &%s->%s_data) == -1)' % ( buf, tag_name, var_name, self._name), ' return (-1);'] return code def CodeMarshal(self, buf, tag_name, var_name): - code = ['tag_marshal_string(%s, %s, %s->%s_data);' % ( + code = ['evtag_marshal_string(%s, %s, %s->%s_data);' % ( buf, tag_name, var_name, self._name)] return code @@ -643,13 +645,17 @@ class EntryStruct(Entry): return code def CodeUnmarshal(self, buf, tag_name, var_name): - code = ['if (tag_unmarshal_%s(%s, %s, %s->%s_data) == -1)' % ( + code = ['%s->%s_data = %s_new();' % ( + var_name, self._name, self._refname), + 'if (%s->%s_data == NULL)' % (var_name, self._name), + ' return (-1);', + 'if (evtag_unmarshal_%s(%s, %s, %s->%s_data) == -1)' % ( self._refname, buf, tag_name, var_name, self._name), ' return (-1);'] return code def CodeMarshal(self, buf, tag_name, var_name): - code = ['tag_marshal_%s(%s, %s, %s->%s_data);' % ( + code = ['evtag_marshal_%s(%s, %s, %s->%s_data);' % ( self._refname, buf, tag_name, var_name, self._name)] return code @@ -713,20 +719,20 @@ class EntryVarBytes(Entry): return code def CodeUnmarshal(self, buf, tag_name, var_name): - code = ['if (tag_peek_length(%s, &%s->%s_length) == -1)' % ( + code = ['if (evtag_peek_length(%s, &%s->%s_length) == -1)' % ( buf, var_name, self._name), ' return (-1);', 'if ((%s->%s_data = malloc(%s->%s_length)) == NULL)' % ( var_name, self._name, var_name, self._name), ' return (-1);', - 'if (tag_unmarshal_fixed(%s, %s, %s->%s_data, ' + 'if (evtag_unmarshal_fixed(%s, %s, %s->%s_data, ' '%s->%s_length) == -1)' % ( buf, tag_name, var_name, self._name, var_name, self._name), ' return (-1);'] return code def CodeMarshal(self, buf, tag_name, var_name): - code = ['tag_marshal(%s, %s, %s->%s_data, %s->%s_length);' % ( + code = ['evtag_marshal(%s, %s, %s->%s_data, %s->%s_length);' % ( buf, tag_name, var_name, self._name, var_name, self._name)] return code @@ -890,10 +896,27 @@ def GetNextStruct(file): got_struct = 0 + processed_lines = [] + + have_c_comment = 0 data = '' for line in file: line_count += 1 line = line[:-1] + + if not have_c_comment and re.search(r'/\*', line): + if re.search(r'/\*.*\*/', line): + line = re.sub(r'/\*.*\*/', '', line) + else: + line = re.sub(r'/\*.*$', '', line) + have_c_comment = 1 + + if have_c_comment: + if not re.search(r'\*/', line): + continue + have_c_comment = 0 + line = re.sub(r'^.*\*/', '', line) + line = NormalizeLine(line) if not line: diff --git a/event_tagging.c b/event_tagging.c new file mode 100644 index 00000000..89fd4131 --- /dev/null +++ b/event_tagging.c @@ -0,0 +1,338 @@ +/* + * Copyright (c) 2003, 2004 Niels Provos + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#ifdef HAVE_SYS_TIME_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include + +#include "event.h" + +int decode_int(uint32_t *pnumber, struct evbuffer *evbuf); + +static struct evbuffer *_buf; + +void +evtag_init() +{ + if ((_buf = evbuffer_new()) == NULL) + err(1, "%s: malloc", __func__); +} + +/* + * We encode integer's by nibbles; the first nibble contains the number + * of significant nibbles - 1; this allows us to encode up to 64-bit + * integers. This function is byte-order independent. + */ + +void +encode_int(struct evbuffer *evbuf, uint32_t number) +{ + int off = 1, nibbles = 0; + uint8_t data[5]; + + memset(data, 0, sizeof(data)); + while (number) { + if (off & 0x1) + data[off/2] = (data[off/2] & 0xf0) | (number & 0x0f); + else + data[off/2] = (data[off/2] & 0x0f) | + ((number & 0x0f) << 4); + number >>= 4; + off++; + } + + if (off > 2) + nibbles = off - 2; + + /* Off - 1 is the number of encoded nibbles */ + data[0] = (data[0] & 0x0f) | ((nibbles & 0x0f) << 4); + + evbuffer_add(evbuf, data, (off + 1) / 2); +} + +/* + * Marshal a data type, the general format is as follows: + * + * tag number: one byte; length: var bytes; payload: var bytes + */ + +void +evtag_marshal(struct evbuffer *evbuf, uint8_t tag, + const void *data, uint16_t len) +{ + evbuffer_add(evbuf, &tag, sizeof(tag)); + encode_int(evbuf, len); + evbuffer_add(evbuf, (void *)data, len); +} + +/* Marshaling for integers */ +void +evtag_marshal_int(struct evbuffer *evbuf, uint8_t tag, uint32_t integer) +{ + evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf)); + encode_int(_buf, integer); + + evbuffer_add(evbuf, &tag, sizeof(tag)); + encode_int(evbuf, EVBUFFER_LENGTH(_buf)); + evbuffer_add_buffer(evbuf, _buf); +} + +void +evtag_marshal_string(struct evbuffer *buf, uint8_t tag, const char *string) +{ + evtag_marshal(buf, tag, string, strlen(string)); +} + +void +evtag_marshal_timeval(struct evbuffer *evbuf, uint8_t tag, struct timeval *tv) +{ + evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf)); + + encode_int(_buf, tv->tv_sec); + encode_int(_buf, tv->tv_usec); + + evtag_marshal(evbuf, tag, EVBUFFER_DATA(_buf), + EVBUFFER_LENGTH(_buf)); +} + +static int __inline +decode_int_internal(uint32_t *pnumber, struct evbuffer *evbuf, int dodrain) +{ + uint32_t number = 0; + uint8_t *data = EVBUFFER_DATA(evbuf); + int len = EVBUFFER_LENGTH(evbuf); + int nibbles = 0, off; + + if (!len) + return (-1); + + nibbles = ((data[0] & 0xf0) >> 4) + 1; + if (nibbles > 8 || (nibbles >> 1) > len - 1) + return (-1); + + off = nibbles; + while (off > 0) { + number <<= 4; + if (off & 0x1) + number |= data[off >> 1] & 0x0f; + else + number |= (data[off >> 1] & 0xf0) >> 4; + off--; + } + + len = (nibbles >> 1) + 1; + if (dodrain) + evbuffer_drain(evbuf, len); + + *pnumber = number; + + return (len); +} + +int +decode_int(uint32_t *pnumber, struct evbuffer *evbuf) +{ + return (decode_int_internal(pnumber, evbuf, 1) == -1 ? -1 : 0); +} + +int +evtag_peek(struct evbuffer *evbuf, uint8_t *ptag) +{ + if (EVBUFFER_LENGTH(evbuf) < 2) + return (-1); + *ptag = EVBUFFER_DATA(evbuf)[0]; + + return (0); +} + +int +evtag_peek_length(struct evbuffer *evbuf, uint32_t *plength) +{ + struct evbuffer tmp; + int res; + + if (EVBUFFER_LENGTH(evbuf) < 2) + return (-1); + + tmp = *evbuf; + tmp.buffer += 1; + tmp.off -= 1; + + res = decode_int_internal(plength, &tmp, 0); + if (res == -1) + return (-1); + + *plength += res + 1; + + return (0); +} + +int +evtag_consume(struct evbuffer *evbuf) +{ + uint32_t len; + evbuffer_drain(evbuf, 1); + if (decode_int(&len, evbuf) == -1) + return (-1); + evbuffer_drain(evbuf, len); + + return (0); +} + +/* Reads the data type from an event buffer */ + +int +evtag_unmarshal(struct evbuffer *src, uint8_t *ptag, struct evbuffer *dst) +{ + uint8_t tag; + uint16_t len; + uint32_t integer; + + if (evbuffer_remove(src, &tag, sizeof(tag)) != sizeof(tag)) + return (-1); + if (decode_int(&integer, src) == -1) + return (-1); + len = integer; + + if (EVBUFFER_LENGTH(src) < len) + return (-1); + + if (evbuffer_add(dst, EVBUFFER_DATA(src), len) == -1) + return (-1); + + evbuffer_drain(src, len); + + *ptag = tag; + return (len); +} + +/* Marshaling for integers */ + +int +evtag_unmarshal_int(struct evbuffer *evbuf, uint8_t need_tag, uint32_t *pinteger) +{ + uint8_t tag; + uint16_t len; + uint32_t integer; + + if (evbuffer_remove(evbuf, &tag, sizeof(tag)) != sizeof(tag) || + tag != need_tag) + return (-1); + if (decode_int(&integer, evbuf) == -1) + return (-1); + len = integer; + + if (EVBUFFER_LENGTH(evbuf) < len) + return (-1); + + evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf)); + if (evbuffer_add(_buf, EVBUFFER_DATA(evbuf), len) == -1) + return (-1); + + evbuffer_drain(evbuf, len); + + return (decode_int(pinteger, _buf)); +} + +/* Unmarshal a fixed length tag */ + +int +evtag_unmarshal_fixed(struct evbuffer *src, uint8_t need_tag, void *data, + size_t len) +{ + uint8_t tag; + + /* Initialize this event buffer so that we can read into it */ + evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf)); + + /* Now unmarshal a tag and check that it matches the tag we want */ + if (evtag_unmarshal(src, &tag, _buf) == -1 || tag != need_tag) + return (-1); + + if (EVBUFFER_LENGTH(_buf) != len) + return (-1); + + memcpy(data, EVBUFFER_DATA(_buf), len); + return (0); +} + +int +evtag_unmarshal_string(struct evbuffer *evbuf, uint8_t need_tag, char **pstring) +{ + uint8_t tag; + + evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf)); + + if (evtag_unmarshal(evbuf, &tag, _buf) == -1 || tag != need_tag) + return (-1); + + *pstring = calloc(EVBUFFER_LENGTH(_buf) + 1, 1); + if (*pstring == NULL) + err(1, "%s: calloc", __func__); + evbuffer_remove(_buf, *pstring, EVBUFFER_LENGTH(_buf)); + + return (0); +} + +int +evtag_unmarshal_timeval(struct evbuffer *evbuf, uint8_t need_tag, + struct timeval *ptv) +{ + uint8_t tag; + uint32_t integer; + + evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf)); + if (evtag_unmarshal(evbuf, &tag, _buf) == -1 || tag != need_tag) + return (-1); + + if (decode_int(&integer, _buf) == -1) + return (-1); + ptv->tv_sec = integer; + if (decode_int(&integer, _buf) == -1) + return (-1); + ptv->tv_usec = integer; + + return (0); +} diff --git a/test/Makefile.am b/test/Makefile.am index 0efdd40c..11a20d92 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -4,20 +4,27 @@ LDADD = ../libevent.la CPPFPLAGS = -I.. CFLAGS = -I../compat @CFLAGS@ +EXTRA_DIST = regress.rpc + noinst_PROGRAMS = test-init test-eof test-weof test-time regress bench -test_init_sources = test-init.c -test_eof_sources = test-eof.c -test_weof_sources = test-weof.c -test_time_sources = test-time.c -regress_sources = regress.c -bench_sources = bench.c +BUILT_SOURCES = regress.gen.c regress.gen.h +test_init_SOURCES = test-init.c +test_eof_SOURCES = test-eof.c +test_weof_SOURCES = test-weof.c +test_time_SOURCES = test-time.c +regress_SOURCES = regress.c regress.gen.c regress.gen.h +bench_SOURCES = bench.c + +regress.gen.c regress.gen.h: regress.rpc + ../event_rpcgen.py regress.rpc DISTCLEANFILES = *~ +CLEANFILES = regress.gen.h regress.gen.c test: test-init test-eof test-weof test-time regress verify: test @./test.sh -bench test-init test-eof test-weof test-time regress: ../libevent.la +bench test-init test-eof test-weof test-time: ../libevent.la diff --git a/test/regress.c b/test/regress.c index ce47d225..52b7e6f3 100644 --- a/test/regress.c +++ b/test/regress.c @@ -52,6 +52,8 @@ #include +#include "regress.gen.h" + static int pair[2]; static int test_ok; static int called; @@ -637,6 +639,146 @@ test_multiple_events_for_same_fd(void) cleanup_test(); } +int decode_int(uint32_t *pnumber, struct evbuffer *evbuf); + +#define TEST_MAX_INT 6 + +void +evtag_int_test(void) +{ + struct evbuffer *tmp = evbuffer_new(); + uint32_t integers[TEST_MAX_INT] = { + 0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000 + }; + uint32_t integer; + int i; + + for (i = 0; i < TEST_MAX_INT; i++) { + int oldlen, newlen; + oldlen = EVBUFFER_LENGTH(tmp); + encode_int(tmp, integers[i]); + newlen = EVBUFFER_LENGTH(tmp); + fprintf(stderr, "\t\tencoded 0x%08x with %d bytes\n", + integers[i], newlen - oldlen); + } + + for (i = 0; i < TEST_MAX_INT; i++) { + if (decode_int(&integer, tmp) == -1) { + fprintf(stderr, "decode %d failed", i); + exit(1); + } + if (integer != integers[i]) { + fprintf(stderr, "got %x, wanted %x", + integer, integers[i]); + exit(1); + } + } + + if (EVBUFFER_LENGTH(tmp) != 0) { + fprintf(stderr, "trailing data"); + exit(1); + } + evbuffer_free(tmp); + + fprintf(stderr, "\t%s: OK\n", __func__); +} + +void +evtag_fuzz() +{ + u_char buffer[4096]; + struct evbuffer *tmp = evbuffer_new(); + struct timeval tv; + int i, j; + + for (j = 0; j < 100; j++) { + for (i = 0; i < sizeof(buffer); i++) + buffer[i] = rand(); + evbuffer_drain(tmp, -1); + evbuffer_add(tmp, buffer, sizeof(buffer)); + + if (evtag_unmarshal_timeval(tmp, 0, &tv) != -1) { + fprintf(stderr, "evtag_unmarshal should have failed"); + exit(1); + } + } + + /* Now insert some corruption into the tag length field */ + evbuffer_drain(tmp, -1); + timerclear(&tv); + tv.tv_sec = 1; + evtag_marshal_timeval(tmp, 0, &tv); + evbuffer_add(tmp, buffer, sizeof(buffer)); + + EVBUFFER_DATA(tmp)[1] = 0xff; + if (evtag_unmarshal_timeval(tmp, 0, &tv) != -1) { + fprintf(stderr, "evtag_unmarshal_timeval should have failed"); + exit(1); + } + + evbuffer_free(tmp); + + fprintf(stderr, "\t%s: OK\n", __func__); +} + +void +evtag_test(void) +{ + fprintf(stdout, "Testing Tagging:\n"); + + evtag_init(); + evtag_int_test(); + evtag_fuzz(); + + fprintf(stdout, "OK\n"); +} + +void +rpc_test(void) +{ + struct msg *msg, *msg2; + struct kill *kill; + struct evbuffer *tmp = evbuffer_new(); + + fprintf(stdout, "Testing RPC: "); + + msg = msg_new(); + EVTAG_ASSIGN(msg, from_name, "niels"); + EVTAG_ASSIGN(msg, to_name, "phoenix"); + + if (EVTAG_GET(msg, kill, &kill) == -1) { + fprintf(stderr, "Failed to set kill message.\n"); + exit(1); + } + + EVTAG_ASSIGN(kill, weapon, "feather"); + EVTAG_ASSIGN(kill, action, "tickle"); + + if (msg_complete(msg) == -1) { + fprintf(stderr, "Failed to make complete message.\n"); + exit(1); + } + + evtag_marshal_msg(tmp, 0, msg); + + msg2 = msg_new(); + if (evtag_unmarshal_msg(tmp, 0, msg2) == -1) { + fprintf(stderr, "Failed to unmarshal message.\n"); + exit(1); + } + + if (!EVTAG_HAS(msg2, from_name) || + !EVTAG_HAS(msg2, to_name) || + !EVTAG_HAS(msg2, kill)) { + fprintf(stderr, "Missing data structures.\n"); + exit(1); + } + + msg_free(msg); + msg_free(msg2); + + fprintf(stdout, "OK\n"); +} int main (int argc, char **argv) @@ -680,6 +822,10 @@ main (int argc, char **argv) test_multiple_events_for_same_fd(); + evtag_test(); + + rpc_test(); + return (0); }