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 <string.h>
34
35#ifndef _WIN32
36#include <netinet/in.h>
37#include <arpa/inet.h>
38#include <unistd.h>
39#endif
40
41SETUP_TEARDOWN_TESTCONTEXT
42
43void test_stream_exceeds_buffer ()
44{
45 const int msgsize = 8193;
46 char sndbuf[msgsize] = "\xde\xad\xbe\xef";
47 unsigned char rcvbuf[msgsize];
48 char my_endpoint[MAX_SOCKET_STRING];
49
50 int server_sock =
51 TEST_ASSERT_SUCCESS_RAW_ERRNO (socket (AF_INET, SOCK_STREAM, 0));
52 int enable = 1;
53 TEST_ASSERT_SUCCESS_RAW_ERRNO (setsockopt (server_sock, SOL_SOCKET,
54 SO_REUSEADDR, (char *) &enable,
55 sizeof (enable)));
56 struct sockaddr_in saddr = bind_bsd_socket (server_sock);
57 TEST_ASSERT_SUCCESS_RAW_ERRNO (listen (server_sock, 1));
58
59 sprintf (my_endpoint, "tcp://127.0.0.1:%d", ntohs (saddr.sin_port));
60
61 void *zsock = test_context_socket (ZMQ_STREAM);
62 TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (zsock, my_endpoint));
63
64 int client_sock =
65 TEST_ASSERT_SUCCESS_RAW_ERRNO (accept (server_sock, NULL, NULL));
66
67 TEST_ASSERT_SUCCESS_RAW_ERRNO (close (server_sock));
68
69 TEST_ASSERT_EQUAL_INT (msgsize, send (client_sock, sndbuf, msgsize, 0));
70
71 zmq_msg_t msg;
72 zmq_msg_init (&msg);
73
74 int rcvbytes = 0;
75 while (rcvbytes == 0) // skip connection notification, if any
76 {
77 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, zsock, 0)); // peerid
78 TEST_ASSERT_TRUE (zmq_msg_more (&msg));
79 rcvbytes = TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, zsock, 0));
80 TEST_ASSERT_FALSE (zmq_msg_more (&msg));
81 }
82
83 // for this test, we only collect the first chunk
84 // since the corruption already occurs in the first chunk
85 memcpy (rcvbuf, zmq_msg_data (&msg), zmq_msg_size (&msg));
86
87 zmq_msg_close (&msg);
88 test_context_socket_close (zsock);
89 close (client_sock);
90
91 TEST_ASSERT_GREATER_OR_EQUAL (4, rcvbytes);
92
93 // notice that only the 1st byte gets corrupted
94 TEST_ASSERT_EQUAL_UINT (0xef, rcvbuf[3]);
95 TEST_ASSERT_EQUAL_UINT (0xbe, rcvbuf[2]);
96 TEST_ASSERT_EQUAL_UINT (0xad, rcvbuf[1]);
97 TEST_ASSERT_EQUAL_UINT (0xde, rcvbuf[0]);
98}
99
100int main ()
101{
102 UNITY_BEGIN ();
103 RUN_TEST (test_stream_exceeds_buffer);
104 return UNITY_END ();
105}
106