| 1 | /* |
| 2 | Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file |
| 3 | |
| 4 | This file is part of libzmq, the ZeroMQ core engine in C++. |
| 5 | |
| 6 | libzmq is free software; you can redistribute it and/or modify it under |
| 7 | the terms of the GNU Lesser General Public License (LGPL) as published |
| 8 | by the Free Software Foundation; either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | As a special exception, the Contributors give you permission to link |
| 12 | this library with independent modules to produce an executable, |
| 13 | regardless of the license terms of these independent modules, and to |
| 14 | copy and distribute the resulting executable under terms of your choice, |
| 15 | provided that you also meet, for each linked independent module, the |
| 16 | terms and conditions of the license of that module. An independent |
| 17 | module is a module which is not derived from or based on this library. |
| 18 | If you modify this library, you must extend this exception to your |
| 19 | version of the library. |
| 20 | |
| 21 | libzmq is distributed in the hope that it will be useful, but WITHOUT |
| 22 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 23 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 24 | License for more details. |
| 25 | |
| 26 | You should have received a copy of the GNU Lesser General Public License |
| 27 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 | */ |
| 29 | |
| 30 | #include "testutil.hpp" |
| 31 | #include "testutil_unity.hpp" |
| 32 | |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | |
| 36 | SETUP_TEARDOWN_TESTCONTEXT |
| 37 | |
| 38 | // XSI vector I/O |
| 39 | #if defined ZMQ_HAVE_UIO |
| 40 | #include <sys/uio.h> |
| 41 | #else |
| 42 | struct iovec |
| 43 | { |
| 44 | void *iov_base; |
| 45 | size_t iov_len; |
| 46 | }; |
| 47 | #endif |
| 48 | |
| 49 | static void do_check (void *sb_, void *sc_, size_t msg_size_) |
| 50 | { |
| 51 | TEST_ASSERT_NOT_NULL (sb_); |
| 52 | TEST_ASSERT_NOT_NULL (sc_); |
| 53 | TEST_ASSERT_GREATER_THAN (0, msg_size_); |
| 54 | |
| 55 | const char msg_val = '1'; |
| 56 | const int num_messages = 10; |
| 57 | size_t send_count, recv_count; |
| 58 | |
| 59 | send_count = recv_count = num_messages; |
| 60 | |
| 61 | char *ref_msg = static_cast<char *> (malloc (msg_size_)); |
| 62 | TEST_ASSERT_NOT_NULL (ref_msg); |
| 63 | memset (ref_msg, msg_val, msg_size_); |
| 64 | |
| 65 | // zmq_sendiov(3) as a single multi-part send |
| 66 | struct iovec send_iov[num_messages]; |
| 67 | char *buf = static_cast<char *> (malloc (msg_size_ * num_messages)); |
| 68 | |
| 69 | for (int i = 0; i < num_messages; i++) { |
| 70 | send_iov[i].iov_base = &buf[i * msg_size_]; |
| 71 | send_iov[i].iov_len = msg_size_; |
| 72 | memcpy (send_iov[i].iov_base, ref_msg, msg_size_); |
| 73 | |
| 74 | // TODO: this assertion only checks if memcpy behaves as expected... remove this or assert something else? |
| 75 | TEST_ASSERT_EQUAL_HEX8_ARRAY (ref_msg, send_iov[i].iov_base, msg_size_); |
| 76 | } |
| 77 | |
| 78 | // Test errors - zmq_recviov - null socket |
| 79 | TEST_ASSERT_FAILURE_ERRNO ( |
| 80 | ENOTSOCK, zmq_sendiov (NULL, send_iov, send_count, ZMQ_SNDMORE)); |
| 81 | // Test errors - zmq_recviov - invalid send count |
| 82 | TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_sendiov (sc_, send_iov, 0, 0)); |
| 83 | // Test errors - zmq_recviov - null iovec |
| 84 | TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_sendiov (sc_, NULL, send_count, 0)); |
| 85 | |
| 86 | // Test success |
| 87 | |
| 88 | // The zmq_sendiov(3) API method does not follow the same semantics as |
| 89 | // zmq_recviov(3); the latter returns the count of messages sent, rightly |
| 90 | // so, whilst the former sends the number of bytes successfully sent from |
| 91 | // the last message, which does not hold much sense from a batch send |
| 92 | // perspective; hence the assert checks if the result is same as msg_size. |
| 93 | TEST_ASSERT_EQUAL_INT ( |
| 94 | (int) msg_size_, TEST_ASSERT_SUCCESS_ERRNO ( |
| 95 | zmq_sendiov (sc_, send_iov, send_count, ZMQ_SNDMORE))); |
| 96 | |
| 97 | // zmq_recviov(3) single-shot |
| 98 | struct iovec recv_iov[num_messages]; |
| 99 | |
| 100 | // Test errors - zmq_recviov - null socket |
| 101 | TEST_ASSERT_FAILURE_ERRNO (ENOTSOCK, |
| 102 | zmq_recviov (NULL, recv_iov, &recv_count, 0)); |
| 103 | // Test error - zmq_recviov - invalid receive count |
| 104 | TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_recviov (sb_, recv_iov, NULL, 0)); |
| 105 | size_t invalid_recv_count = 0; |
| 106 | TEST_ASSERT_FAILURE_ERRNO ( |
| 107 | EINVAL, zmq_recviov (sb_, recv_iov, &invalid_recv_count, 0)); |
| 108 | // Test error - zmq_recviov - null iovec |
| 109 | TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_recviov (sb_, NULL, &recv_count, 0)); |
| 110 | |
| 111 | // Test success |
| 112 | TEST_ASSERT_EQUAL_INT ( |
| 113 | num_messages, |
| 114 | TEST_ASSERT_SUCCESS_ERRNO (zmq_recviov (sb_, recv_iov, &recv_count, 0))); |
| 115 | |
| 116 | for (int i = 0; i < num_messages; i++) { |
| 117 | TEST_ASSERT_NOT_NULL (recv_iov[i].iov_base); |
| 118 | TEST_ASSERT_EQUAL_STRING_LEN (ref_msg, recv_iov[i].iov_base, msg_size_); |
| 119 | free (recv_iov[i].iov_base); |
| 120 | } |
| 121 | |
| 122 | TEST_ASSERT_EQUAL_INT (send_count, recv_count); |
| 123 | free (ref_msg); |
| 124 | free (buf); |
| 125 | } |
| 126 | |
| 127 | void test_iov () |
| 128 | { |
| 129 | void *sb = test_context_socket (ZMQ_PULL); |
| 130 | TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "inproc://a" )); |
| 131 | |
| 132 | msleep (SETTLE_TIME); |
| 133 | |
| 134 | void *sc = test_context_socket (ZMQ_PUSH); |
| 135 | TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, "inproc://a" )); |
| 136 | |
| 137 | // message bigger than VSM max |
| 138 | do_check (sb, sc, 100); |
| 139 | |
| 140 | // message smaller than VSM max |
| 141 | do_check (sb, sc, 10); |
| 142 | |
| 143 | test_context_socket_close (sc); |
| 144 | test_context_socket_close (sb); |
| 145 | } |
| 146 | |
| 147 | int main () |
| 148 | { |
| 149 | setup_test_environment (); |
| 150 | |
| 151 | UNITY_BEGIN (); |
| 152 | RUN_TEST (test_iov); |
| 153 | return UNITY_END (); |
| 154 | } |
| 155 | |