aboutsummaryrefslogtreecommitdiff
path: root/src/compat/nanomsg/compat_msg_test.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2023-11-25 22:43:34 -0800
committerGarrett D'Amore <garrett@damore.org>2023-11-25 22:52:31 -0800
commit003f0556a61f3e7225f4f0d0087bdf08af5b632a (patch)
tree49287eae1d85f80329dbca439f2e8b02bf67829b /src/compat/nanomsg/compat_msg_test.c
parentfe692071ed2121ef693eaaaeacc1feb596c3e23b (diff)
downloadnng-003f0556a61f3e7225f4f0d0087bdf08af5b632a.tar.gz
nng-003f0556a61f3e7225f4f0d0087bdf08af5b632a.tar.bz2
nng-003f0556a61f3e7225f4f0d0087bdf08af5b632a.zip
fixes #1701 compat: nn_reallocmsg is incorrect
Diffstat (limited to 'src/compat/nanomsg/compat_msg_test.c')
-rw-r--r--src/compat/nanomsg/compat_msg_test.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/compat/nanomsg/compat_msg_test.c b/src/compat/nanomsg/compat_msg_test.c
new file mode 100644
index 00000000..d037a0b4
--- /dev/null
+++ b/src/compat/nanomsg/compat_msg_test.c
@@ -0,0 +1,92 @@
+//
+// Copyright 2023 Staysail Systems, Inc. <info@staysail.tech>
+//
+// 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 <nng/compat/nanomsg/nn.h>
+#include <nng/compat/nanomsg/pair.h>
+#include <nng/compat/nanomsg/tcp.h>
+
+#include "nuts_compat.h"
+
+#include <nuts.h>
+
+void
+test_msg_alloc(void)
+{
+ char *msg;
+ msg = nn_allocmsg(1, 0);
+ NUTS_TRUE(msg != NULL);
+ NUTS_NN_PASS(nn_freemsg(msg));
+}
+
+void
+test_msg_zero_length(void)
+{
+ char *msg;
+ msg = nn_allocmsg(0, 0); // empty message is invalid
+ NUTS_TRUE(msg == NULL);
+ NUTS_TRUE(nn_errno() == EINVAL);
+}
+
+void
+test_msg_overflow(void)
+{
+ char *msg;
+ msg = nn_allocmsg((size_t)-1, 0); // this will overflow
+ NUTS_TRUE(msg == NULL);
+ NUTS_TRUE(nn_errno() == EINVAL);
+}
+
+void
+test_msg_bad_type(void)
+{
+ char *msg;
+ msg = nn_allocmsg(0, 1); // we only support message type 0
+ NUTS_TRUE(msg == NULL);
+ NUTS_TRUE(nn_errno() == EINVAL);
+}
+
+void
+test_msg_realloc(void)
+{
+ char *msg0;
+ char *msg1;
+ char *msg2;
+ char *msg3;
+
+ msg0 = nn_allocmsg(5, 0);
+ NUTS_TRUE(msg0 != NULL);
+
+ memcpy(msg0, "this", 5);
+
+ msg1 = nn_reallocmsg(msg0, 65536);
+ NUTS_TRUE(msg1 != NULL);
+ NUTS_TRUE(msg1 != msg0);
+ NUTS_MATCH(msg1, "this");
+
+ msg1[65000] = 'A';
+
+ msg2 = nn_reallocmsg(msg1, 5);
+ NUTS_TRUE(msg2 == msg1);
+
+ // test for overflow
+ msg3 = nn_reallocmsg(msg2, (size_t)-1);
+ NUTS_TRUE(msg3 == NULL);
+ NUTS_TRUE(nn_errno() == EINVAL);
+
+ nn_freemsg(msg2);
+}
+
+TEST_LIST = {
+ { "alloc msg", test_msg_alloc },
+ { "zero length", test_msg_zero_length },
+ { "invalid type", test_msg_bad_type },
+ { "overflow", test_msg_overflow },
+ { "reallocate msg", test_msg_realloc },
+ { NULL, NULL },
+};