summaryrefslogtreecommitdiff
path: root/tests/compat_msg.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-08-07 18:16:54 -0700
committerGarrett D'Amore <garrett@damore.org>2017-08-07 18:16:54 -0700
commit491f1c1a5a209545287f9cc79f59ee816f5b9a41 (patch)
treeeecce44404886f4b85c3d45d867424d62a6b592d /tests/compat_msg.c
parent705d39a6daf99b44c820631f04a116cc09b134b5 (diff)
downloadnng-491f1c1a5a209545287f9cc79f59ee816f5b9a41.tar.gz
nng-491f1c1a5a209545287f9cc79f59ee816f5b9a41.tar.bz2
nng-491f1c1a5a209545287f9cc79f59ee816f5b9a41.zip
Fix crash when using legacy send API with NN_MSG.
We introduced the compat_msg.c from the old msg.c in the nanomsg repo. While here, we found that the handling for send() was badly wrong, by a level of indirection. We simplified the code to so that nn_send() and nn_recv() are simple wrappers around the nn_sendmsg() and nn_recvmsg() APIs (as in old nanomsg). This may not be quite as fast, but it's more likely to be correct and reduces complexity.
Diffstat (limited to 'tests/compat_msg.c')
-rw-r--r--tests/compat_msg.c129
1 files changed, 129 insertions, 0 deletions
diff --git a/tests/compat_msg.c b/tests/compat_msg.c
new file mode 100644
index 00000000..683739d3
--- /dev/null
+++ b/tests/compat_msg.c
@@ -0,0 +1,129 @@
+/*
+ Copyright (c) 2013 Martin Sustrik All rights reserved.
+ Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
+ Copyright 2017 Garrett D'Amore <garrett@damore.org>
+ Copyright 2017 Capitar IT Group BV <info@capitar.com>
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"),
+ to deal in the Software without restriction, including without limitation
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ and/or sell copies of the Software, and to permit persons to whom
+ the Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ IN THE SOFTWARE.
+*/
+
+#include "nng_compat.h"
+#include "compat_testutil.h"
+
+#include <string.h>
+
+#define SOCKET_ADDRESS "inproc://a"
+
+char longdata[1 << 20];
+
+int main (int argc, const char *argv[])
+{
+ int rc;
+ int sb;
+ int sc;
+ unsigned char *buf1, *buf2;
+ int i;
+ struct nn_iovec iov;
+ struct nn_msghdr hdr;
+ char socket_address_tcp[128];
+
+ test_addr_from(socket_address_tcp, "tcp", "127.0.0.1",
+ get_test_port(argc, argv));
+
+ sb = test_socket (AF_SP, NN_PAIR);
+ test_bind (sb, SOCKET_ADDRESS);
+ sc = test_socket (AF_SP, NN_PAIR);
+ test_connect (sc, SOCKET_ADDRESS);
+
+ buf1 = nn_allocmsg (256, 0);
+ alloc_assert (buf1);
+ for (i = 0; i != 256; ++i)
+ buf1 [i] = (unsigned char) i;
+ rc = nn_send (sc, &buf1, NN_MSG, 0);
+ errno_assert (rc >= 0);
+ nn_assert (rc == 256);
+
+ buf2 = NULL;
+ rc = nn_recv (sb, &buf2, NN_MSG, 0);
+ errno_assert (rc >= 0);
+ nn_assert (rc == 256);
+ nn_assert (buf2);
+ for (i = 0; i != 256; ++i)
+ nn_assert (buf2 [i] == (unsigned char) i);
+ rc = nn_freemsg (buf2);
+ errno_assert (rc == 0);
+
+ buf1 = nn_allocmsg (256, 0);
+ alloc_assert (buf1);
+ for (i = 0; i != 256; ++i)
+ buf1 [i] = (unsigned char) i;
+ iov.iov_base = &buf1;
+ iov.iov_len = NN_MSG;
+ memset (&hdr, 0, sizeof (hdr));
+ hdr.msg_iov = &iov;
+ hdr.msg_iovlen = 1;
+ rc = nn_sendmsg (sc, &hdr, 0);
+ errno_assert (rc >= 0);
+ nn_assert (rc == 256);
+
+ buf2 = NULL;
+ iov.iov_base = &buf2;
+ iov.iov_len = NN_MSG;
+ memset (&hdr, 0, sizeof (hdr));
+ hdr.msg_iov = &iov;
+ hdr.msg_iovlen = 1;
+ rc = nn_recvmsg (sb, &hdr, 0);
+ errno_assert (rc >= 0);
+ nn_assert (rc == 256);
+ nn_assert (buf2);
+ for (i = 0; i != 256; ++i)
+ nn_assert (buf2 [i] == (unsigned char) i);
+ rc = nn_freemsg (buf2);
+ errno_assert (rc == 0);
+
+ test_close (sc);
+ test_close (sb);
+
+ /* Test receiving of large message */
+
+ sb = test_socket (AF_SP, NN_PAIR);
+ test_bind (sb, socket_address_tcp);
+ sc = test_socket (AF_SP, NN_PAIR);
+ test_connect (sc, socket_address_tcp);
+
+ for (i = 0; i < (int) sizeof (longdata); ++i)
+ longdata[i] = '0' + (i % 10);
+ longdata [sizeof (longdata) - 1] = 0;
+ test_send (sb, longdata);
+
+ rc = nn_recv (sc, &buf2, NN_MSG, 0);
+ errno_assert (rc >= 0);
+ nn_assert (rc == sizeof (longdata) - 1);
+ nn_assert (buf2);
+ for (i = 0; i < (int) sizeof (longdata) - 1; ++i)
+ nn_assert (buf2 [i] == longdata [i]);
+ rc = nn_freemsg (buf2);
+ errno_assert (rc == 0);
+
+ test_close (sc);
+ test_close (sb);
+
+ return 0;
+}
+