2023-06-05 00:16:05 +01:00
|
|
|
/* SPDX-License-Identifier: MPL-2.0 */
|
2015-07-20 22:22:13 +02:00
|
|
|
|
2016-02-06 23:19:10 +11:00
|
|
|
#include "testutil.hpp"
|
2018-12-12 05:27:50 -05:00
|
|
|
#include "testutil_unity.hpp"
|
|
|
|
|
2019-03-23 08:04:57 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2019-03-24 12:51:28 -04:00
|
|
|
SETUP_TEARDOWN_TESTCONTEXT
|
2016-02-12 15:46:55 +01:00
|
|
|
|
2018-12-12 05:27:50 -05:00
|
|
|
void test_stream_exceeds_buffer ()
|
2015-07-20 22:22:13 +02:00
|
|
|
{
|
|
|
|
const int msgsize = 8193;
|
|
|
|
char sndbuf[msgsize] = "\xde\xad\xbe\xef";
|
|
|
|
unsigned char rcvbuf[msgsize];
|
2017-11-09 23:37:09 +00:00
|
|
|
char my_endpoint[MAX_SOCKET_STRING];
|
2015-07-20 22:22:13 +02:00
|
|
|
|
2020-04-25 19:53:44 +01:00
|
|
|
int server_sock = bind_socket_resolve_port ("127.0.0.1", "0", my_endpoint);
|
2017-11-09 23:37:09 +00:00
|
|
|
|
2018-12-12 05:27:50 -05:00
|
|
|
void *zsock = test_context_socket (ZMQ_STREAM);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (zsock, my_endpoint));
|
2015-07-20 22:22:13 +02:00
|
|
|
|
2018-12-12 05:27:50 -05:00
|
|
|
int client_sock =
|
|
|
|
TEST_ASSERT_SUCCESS_RAW_ERRNO (accept (server_sock, NULL, NULL));
|
2015-07-20 22:22:13 +02:00
|
|
|
|
2018-12-12 05:27:50 -05:00
|
|
|
TEST_ASSERT_SUCCESS_RAW_ERRNO (close (server_sock));
|
2015-07-20 22:22:13 +02:00
|
|
|
|
2018-12-12 05:27:50 -05:00
|
|
|
TEST_ASSERT_EQUAL_INT (msgsize, send (client_sock, sndbuf, msgsize, 0));
|
2015-07-20 22:22:13 +02:00
|
|
|
|
|
|
|
zmq_msg_t msg;
|
|
|
|
zmq_msg_init (&msg);
|
|
|
|
|
|
|
|
int rcvbytes = 0;
|
|
|
|
while (rcvbytes == 0) // skip connection notification, if any
|
|
|
|
{
|
2018-12-12 05:27:50 -05:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, zsock, 0)); // peerid
|
|
|
|
TEST_ASSERT_TRUE (zmq_msg_more (&msg));
|
|
|
|
rcvbytes = TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, zsock, 0));
|
|
|
|
TEST_ASSERT_FALSE (zmq_msg_more (&msg));
|
2015-07-20 22:22:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// for this test, we only collect the first chunk
|
|
|
|
// since the corruption already occurs in the first chunk
|
|
|
|
memcpy (rcvbuf, zmq_msg_data (&msg), zmq_msg_size (&msg));
|
|
|
|
|
|
|
|
zmq_msg_close (&msg);
|
2018-12-12 05:27:50 -05:00
|
|
|
test_context_socket_close (zsock);
|
2015-07-20 22:22:13 +02:00
|
|
|
close (client_sock);
|
|
|
|
|
2018-12-12 05:27:50 -05:00
|
|
|
TEST_ASSERT_GREATER_OR_EQUAL (4, rcvbytes);
|
2015-07-20 22:22:13 +02:00
|
|
|
|
|
|
|
// notice that only the 1st byte gets corrupted
|
2018-12-12 05:27:50 -05:00
|
|
|
TEST_ASSERT_EQUAL_UINT (0xef, rcvbuf[3]);
|
|
|
|
TEST_ASSERT_EQUAL_UINT (0xbe, rcvbuf[2]);
|
|
|
|
TEST_ASSERT_EQUAL_UINT (0xad, rcvbuf[1]);
|
|
|
|
TEST_ASSERT_EQUAL_UINT (0xde, rcvbuf[0]);
|
|
|
|
}
|
2016-01-30 02:44:09 -05:00
|
|
|
|
2018-12-12 05:27:50 -05:00
|
|
|
int main ()
|
|
|
|
{
|
|
|
|
UNITY_BEGIN ();
|
|
|
|
RUN_TEST (test_stream_exceeds_buffer);
|
|
|
|
return UNITY_END ();
|
2015-07-20 22:22:13 +02:00
|
|
|
}
|