From dbbee1a3ea7fbd26c528eb96ebe0dbfd7075e292 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Thu, 10 Aug 2017 18:14:27 -0700 Subject: Unify the msg API. This makes the operations that work on headers start with nni_msg_header or nng_msg_header. It also renames _trunc to _chop (same strlen as _trim), and renames prepend to insert. We add a shorthand for clearing message content, and make better use of the endian safe 32-bit accessors too. This also fixes a bug in inserting large headers into messages. A test suite for message handling is included. --- tests/CMakeLists.txt | 2 +- tests/message.c | 186 +++++++++++++++++++++++++++++++++++++++++++++++++++ tests/survey.c | 2 +- 3 files changed, 188 insertions(+), 2 deletions(-) create mode 100644 tests/message.c (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 416bcb1f..f0c09ca6 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -87,7 +87,7 @@ add_nng_test(sock 5) add_nng_test(survey 5) add_nng_test(tcp 5) add_nng_test(scalability 20) -add_nng_test(device 5) +add_nng_test(message 5) # compatbility tests add_nng_compat_test(compat_block 5) diff --git a/tests/message.c b/tests/message.c new file mode 100644 index 00000000..2fc119f5 --- /dev/null +++ b/tests/message.c @@ -0,0 +1,186 @@ +// +// Copyright 2017 Garrett D'Amore +// Copyright 2017 Capitar IT Group BV +// +// This software is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + +#include "convey.h" +#include "nng.h" + +#include + +TestMain("Message Tests", { + int rv; + nng_msg *msg; + + Convey("Given an empty message", { + + So(nng_msg_alloc(&msg, 0) == 0); + + Reset({ nng_msg_free(msg); }); + + Convey("Lengths are empty", { + So(nng_msg_len(msg) == 0); + So(nng_msg_header_len(msg) == 0); + }); + + Convey("We can append to the header", { + So(nng_msg_header_append(msg, "pad", 4) == 0); + So(nng_msg_header_len(msg) == 4); + So(strcmp(nng_msg_header(msg), "pad") == 0); + }); + + Convey("We can append to the body", { + So(nng_msg_append(msg, "123", 4) == 0); + So(nng_msg_len(msg) == 4); + So(strcmp(nng_msg_body(msg), "123") == 0); + }); + + Convey("We can insert to the header", { + So(nng_msg_header_append(msg, "def", 4) == 0); + So(nng_msg_header_insert(msg, "abc", 3) == 0); + So(nng_msg_header_len(msg) == 7); + So(strcmp(nng_msg_header(msg), "abcdef") == 0); + + Convey("We can delete from the front", { + So(nng_msg_header_trim(msg, 2) == 0); + So(nng_msg_header_len(msg) == 5); + So(strcmp(nng_msg_header(msg), "cdef") == 0); + }); + + Convey("We can delete from the back", { + So(nng_msg_header_chop(msg, 5) == 0); + So(nng_msg_header_len(msg) == 2); + So(memcmp(nng_msg_header(msg), "ab", 2) == 0); + }); + }); + + Convey("We can insert to the body", { + So(nng_msg_append(msg, "xyz", 4) == 0); + So(nng_msg_insert(msg, "uvw", 3) == 0); + So(nng_msg_len(msg) == 7); + So(strcmp(nng_msg_body(msg), "uvwxyz") == 0); + + Convey("We can delete from the front", { + So(nng_msg_trim(msg, 2) == 0); + So(nng_msg_len(msg) == 5); + So(strcmp(nng_msg_body(msg), "wxyz") == 0); + }); + + Convey("We can delete from the back", { + So(nng_msg_chop(msg, 5) == 0); + So(nng_msg_len(msg) == 2); + So(memcmp(nng_msg_body(msg), "uv", 2) == 0); + }); + }); + + Convey("Clearing the header works", { + So(nng_msg_header_append(msg, "bogus", 6) == 0); + So(nng_msg_header_len(msg) == 6); + nng_msg_header_clear(msg); + So(nng_msg_header_len(msg) == 0); + }); + + Convey("Clearing the body works", { + So(nng_msg_append(msg, "bogus", 6) == 0); + So(nng_msg_len(msg) == 6); + nng_msg_clear(msg); + So(nng_msg_len(msg) == 0); + }); + + Convey("We cannot delete more header than exists", { + So(nng_msg_header_append( + msg, "short", strlen("short") + 1) == 0); + So(nng_msg_header_trim(msg, 16) == NNG_EINVAL); + So(nng_msg_header_len(msg) == strlen("short") + 1); + So(nng_msg_header_chop(msg, 16) == NNG_EINVAL); + So(nng_msg_header_len(msg) == strlen("short") + 1); + So(strcmp(nng_msg_header(msg), "short") == 0); + }); + + Convey("We cannot delete more body than exists", { + So(nng_msg_append(msg, "short", strlen("short") + 1) == + 0); + So(nng_msg_trim(msg, 16) == NNG_EINVAL); + So(nng_msg_len(msg) == strlen("short") + 1); + So(nng_msg_chop(msg, 16) == NNG_EINVAL); + So(nng_msg_len(msg) == strlen("short") + 1); + So(strcmp(nng_msg_body(msg), "short") == 0); + }); + + Convey("Pipe retrievals work", { + So(nng_msg_get_pipe(msg) == 0); + nng_msg_set_pipe(msg, (nng_pipe) 45); + So(nng_msg_get_pipe(msg) == (nng_pipe) 45); + }); + + Convey("Message realloc works", { + So(nng_msg_append(msg, "abc", 4) == 0); + So(nng_msg_realloc(msg, 1500) == 0); + So(nng_msg_len(msg) == 1500); + So(strcmp(nng_msg_body(msg), "abc") == 0); + So(nng_msg_realloc(msg, 2) == 0); + So(nng_msg_len(msg) == 2); + So(memcmp(nng_msg_body(msg), "abc", 2) == 0); + So(nng_msg_append(msg, "CDEF", strlen("CDEF") + 1) == + 0); + So(nng_msg_len(msg) == strlen("abCDEF") + 1); + So(strcmp(nng_msg_body(msg), "abCDEF") == 0); + }); + + Convey("Inserting a lot of data works", { + char chunk[1024]; + memset(chunk, '+', sizeof(chunk)); + So(nng_msg_append(msg, "abc", strlen("abc") + 1) == 0); + So(nng_msg_len(msg) == strlen("abc") + 1); + So(nng_msg_insert(msg, chunk, sizeof(chunk)) == 0); + So(nng_msg_len(msg) == + strlen("abc") + 1 + sizeof(chunk)); + So(memcmp(chunk, nng_msg_body(msg), sizeof(chunk)) == + 0); + So(strcmp((char *) nng_msg_body(msg) + sizeof(chunk), + "abc") == 0); + So(nng_msg_trim(msg, sizeof(chunk) - 2) == 0); + So(strcmp(nng_msg_body(msg), "++abc") == 0); + }); + + Convey("Message dup works", { + nng_msg *m2; + + So(nng_msg_header_append( + msg, "front", strlen("front") + 1) == 0); + So(nng_msg_append(msg, "back", strlen("back") + 1) == + 0); + + So(nng_msg_dup(&m2, msg) == 0); + Reset({ nng_msg_free(m2); }); + + So(nng_msg_len(msg) == strlen("front")); + So(nng_msg_len(m2) == strlen("front")); + So(nng_msg_header_len(msg) == nng_msg_header_len(m2)); + + So(nng_msg_insert(msg, "way", 3) == 0); + So(nng_msg_len(msg) == strlen("wayback") + 1); + So(nng_msg_len(m2) == strlen("back") + 1); + So(strcmp(nng_msg_body(msg), "wayback") == 0); + So(strcmp(nng_msg_body(m2), "back") == 0); + So(nng_msg_chop(m2, 1) == 0); + So(nng_msg_append( + m2, "2basics", strlen("2basics") + 1) == 0); + So(nng_msg_len(msg) == strlen("wayback") + 1); + So(strcmp(nng_msg_body(msg), "wayback") == 0); + So(nng_msg_len(m2) == strlen("back2basics") + 1); + So(strcmp(nng_msg_body(m2), "back2basics") == 0); + }); + + Convey("Missing option fails properly", { + char buf[128]; + size_t sz = sizeof(buf); + So(nng_msg_getopt(msg, 4545, buf, &sz) == NNG_ENOENT); + }); + }); +}); diff --git a/tests/survey.c b/tests/survey.c index 6f85850f..2a757a98 100644 --- a/tests/survey.c +++ b/tests/survey.c @@ -123,7 +123,7 @@ Main({ msg = NULL; So(nng_recvmsg(resp, &msg, 0) == 0); CHECKSTR(msg, "abc"); - nng_msg_trunc(msg, 3); + nng_msg_chop(msg, 3); APPENDSTR(msg, "def"); So(nng_sendmsg(resp, msg, 0) == 0); msg = NULL; -- cgit v1.2.3-70-g09d2