test_iov.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #include "testutil.hpp"
4 #include "testutil_unity.hpp"
5 
6 #include <stdlib.h>
7 #include <string.h>
8 
10 
11 // XSI vector I/O
12 #if defined ZMQ_HAVE_UIO
13 #include <sys/uio.h>
14 #else
15 struct iovec
16 {
17  void *iov_base;
18  size_t iov_len;
19 };
20 #endif
21 
22 static void do_check (void *sb_, void *sc_, size_t msg_size_)
23 {
26  TEST_ASSERT_GREATER_THAN (0, msg_size_);
27 
28  const char msg_val = '1';
29  const int num_messages = 10;
30  size_t send_count, recv_count;
31 
32  send_count = recv_count = num_messages;
33 
34  char *ref_msg = static_cast<char *> (malloc (msg_size_));
35  TEST_ASSERT_NOT_NULL (ref_msg);
36  memset (ref_msg, msg_val, msg_size_);
37 
38  // zmq_sendiov(3) as a single multi-part send
39  struct iovec send_iov[num_messages];
40  char *buf = static_cast<char *> (malloc (msg_size_ * num_messages));
41 
42  for (int i = 0; i < num_messages; i++) {
43  send_iov[i].iov_base = &buf[i * msg_size_];
44  send_iov[i].iov_len = msg_size_;
45  memcpy (send_iov[i].iov_base, ref_msg, msg_size_);
46 
47  // TODO: this assertion only checks if memcpy behaves as expected... remove this or assert something else?
48  TEST_ASSERT_EQUAL_HEX8_ARRAY (ref_msg, send_iov[i].iov_base, msg_size_);
49  }
50 
51  // Test errors - zmq_recviov - null socket
53  ENOTSOCK, zmq_sendiov (NULL, send_iov, send_count, ZMQ_SNDMORE));
54  // Test errors - zmq_recviov - invalid send count
55  TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_sendiov (sc_, send_iov, 0, 0));
56  // Test errors - zmq_recviov - null iovec
57  TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_sendiov (sc_, NULL, send_count, 0));
58 
59  // Test success
60 
61  // The zmq_sendiov(3) API method does not follow the same semantics as
62  // zmq_recviov(3); the latter returns the count of messages sent, rightly
63  // so, whilst the former sends the number of bytes successfully sent from
64  // the last message, which does not hold much sense from a batch send
65  // perspective; hence the assert checks if the result is same as msg_size.
67  (int) msg_size_, TEST_ASSERT_SUCCESS_ERRNO (
68  zmq_sendiov (sc_, send_iov, send_count, ZMQ_SNDMORE)));
69 
70  // zmq_recviov(3) single-shot
71  struct iovec recv_iov[num_messages];
72 
73  // Test errors - zmq_recviov - null socket
75  zmq_recviov (NULL, recv_iov, &recv_count, 0));
76  // Test error - zmq_recviov - invalid receive count
77  TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_recviov (sb_, recv_iov, NULL, 0));
78  size_t invalid_recv_count = 0;
80  EINVAL, zmq_recviov (sb_, recv_iov, &invalid_recv_count, 0));
81  // Test error - zmq_recviov - null iovec
82  TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_recviov (sb_, NULL, &recv_count, 0));
83 
84  // Test success
86  num_messages,
87  TEST_ASSERT_SUCCESS_ERRNO (zmq_recviov (sb_, recv_iov, &recv_count, 0)));
88 
89  for (int i = 0; i < num_messages; i++) {
90  TEST_ASSERT_NOT_NULL (recv_iov[i].iov_base);
91  TEST_ASSERT_EQUAL_STRING_LEN (ref_msg, recv_iov[i].iov_base, msg_size_);
92  free (recv_iov[i].iov_base);
93  }
94 
95  TEST_ASSERT_EQUAL_INT (send_count, recv_count);
96  free (ref_msg);
97  free (buf);
98 }
99 
100 void test_iov ()
101 {
102  void *sb = test_context_socket (ZMQ_PULL);
103  TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "inproc://a"));
104 
106 
107  void *sc = test_context_socket (ZMQ_PUSH);
108  TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, "inproc://a"));
109 
110  // message bigger than VSM max
111  do_check (sb, sc, 100);
112 
113  // message smaller than VSM max
114  do_check (sb, sc, 10);
115 
118 }
119 
120 int main ()
121 {
123 
124  UNITY_BEGIN ();
125  RUN_TEST (test_iov);
126  return UNITY_END ();
127 }
TEST_ASSERT_EQUAL_STRING_LEN
#define TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len)
Definition: unity.h:236
do_check
static void do_check(void *sb_, void *sc_, size_t msg_size_)
Definition: test_iov.cpp:22
NULL
NULL
Definition: test_security_zap.cpp:405
UNITY_END
return UNITY_END()
EINVAL
#define EINVAL
Definition: errno.hpp:25
msleep
void msleep(int milliseconds_)
Definition: testutil.cpp:227
main
int main()
Definition: test_iov.cpp:120
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
TEST_ASSERT_GREATER_THAN
#define TEST_ASSERT_GREATER_THAN(threshold, actual)
Definition: unity.h:152
SETUP_TEARDOWN_TESTCONTEXT
#define SETUP_TEARDOWN_TESTCONTEXT
Definition: testutil_unity.hpp:172
zmq_connect
ZMQ_EXPORT int zmq_connect(void *s_, const char *addr_)
Definition: zmq.cpp:307
TEST_ASSERT_EQUAL_HEX8_ARRAY
#define TEST_ASSERT_EQUAL_HEX8_ARRAY(expected, actual, num_elements)
Definition: unity.h:251
testutil_unity.hpp
ZMQ_PUSH
#define ZMQ_PUSH
Definition: zmq.h:266
sb
void * sb
Definition: test_channel.cpp:8
testutil.hpp
zmq_sendiov
ZMQ_EXPORT int zmq_sendiov(void *s_, struct iovec *iov_, size_t count_, int flags_)
Definition: zmq.cpp:432
zmq_bind
ZMQ_EXPORT int zmq_bind(void *s_, const char *addr_)
Definition: zmq.cpp:299
ENOTSOCK
#define ENOTSOCK
Definition: zmq.h:128
sc
void * sc
Definition: test_channel.cpp:9
test_iov
void test_iov()
Definition: test_iov.cpp:100
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
SETTLE_TIME
#define SETTLE_TIME
Definition: libzmq/tests/testutil.hpp:31
buf
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:4175
TEST_ASSERT_EQUAL_INT
#define TEST_ASSERT_EQUAL_INT(expected, actual)
Definition: unity.h:128
i
int i
Definition: gmock-matchers_test.cc:764
iovec
Definition: zmq.cpp:45
iovec::iov_len
size_t iov_len
Definition: zmq.cpp:48
zmq_recviov
ZMQ_EXPORT int zmq_recviov(void *s_, struct iovec *iov_, size_t *count_, int flags_)
Definition: zmq.cpp:535
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
UNITY_BEGIN
UNITY_BEGIN()
ZMQ_SNDMORE
#define ZMQ_SNDMORE
Definition: zmq.h:359
TEST_ASSERT_FAILURE_ERRNO
#define TEST_ASSERT_FAILURE_ERRNO(error_code, expr)
Definition: testutil_unity.hpp:95
iovec::iov_base
void * iov_base
Definition: zmq.cpp:47
ZMQ_PULL
#define ZMQ_PULL
Definition: zmq.h:265
test_context_socket_close
void * test_context_socket_close(void *socket_)
Definition: testutil_unity.cpp:208
TEST_ASSERT_NOT_NULL
#define TEST_ASSERT_NOT_NULL(pointer)
Definition: unity.h:125
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: proxy_thr.cpp:47


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:59