aboutsummaryrefslogtreecommitdiff
path: root/tests/message.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-08-10 18:14:27 -0700
committerGarrett D'Amore <garrett@damore.org>2017-08-10 22:47:43 -0700
commitdbbee1a3ea7fbd26c528eb96ebe0dbfd7075e292 (patch)
treec5b94f14fd95725077d2b4e9cd0d3d4d166aa7e2 /tests/message.c
parent34ceda3c2dd4990d15e0341e86861dd291003f63 (diff)
downloadnng-dbbee1a3ea7fbd26c528eb96ebe0dbfd7075e292.tar.gz
nng-dbbee1a3ea7fbd26c528eb96ebe0dbfd7075e292.tar.bz2
nng-dbbee1a3ea7fbd26c528eb96ebe0dbfd7075e292.zip
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.
Diffstat (limited to 'tests/message.c')
-rw-r--r--tests/message.c186
1 files changed, 186 insertions, 0 deletions
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 <garrett@damore.org>
+// Copyright 2017 Capitar IT Group BV <info@capitar.com>
+//
+// 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 <string.h>
+
+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);
+ });
+ });
+});