test_iov.cpp 5.2 KB

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