libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_stream_exceeds_buffer.cpp
Go to the documentation of this file.
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 
32 #if defined (ZMQ_HAVE_WINDOWS)
33 # include <winsock2.h>
34 # include <ws2tcpip.h>
35 # include <stdexcept>
36 # define close closesocket
37 #else
38 # include <sys/socket.h>
39 # include <netinet/in.h>
40 # include <arpa/inet.h>
41 # include <unistd.h>
42 #endif
43 
44 #include <zmq.h>
45 
46 int main()
47 {
48  const int msgsize = 8193;
49  char sndbuf[msgsize] = "\xde\xad\xbe\xef";
50  unsigned char rcvbuf[msgsize];
51 
52  int server_sock = socket(AF_INET, SOCK_STREAM, 0);
53  assert(server_sock!=-1);
54  int enable = 1;
55  int rc = setsockopt (server_sock, SOL_SOCKET, SO_REUSEADDR, (char *) &enable, sizeof(enable));
56  assert(rc!=-1);
57 
58  struct sockaddr_in saddr;
59  memset(&saddr, 0, sizeof(saddr));
60  saddr.sin_family = AF_INET;
61  saddr.sin_addr.s_addr = INADDR_ANY;
62  saddr.sin_port = htons(12345);
63 
64  rc = bind(server_sock, (struct sockaddr *)&saddr, sizeof(saddr));
65  assert(rc!=-1);
66  rc = listen(server_sock, 1);
67  assert(rc!=-1);
68 
69  void *zctx = zmq_ctx_new();
70  assert(zctx);
71  void *zsock = zmq_socket(zctx, ZMQ_STREAM);
72  assert(zsock);
73  rc = zmq_connect(zsock, "tcp://127.0.0.1:12345");
74  assert(rc!=-1);
75 
76  int client_sock = accept(server_sock, NULL, NULL);
77  assert(client_sock!=-1);
78 
79  rc = close(server_sock);
80  assert(rc!=-1);
81 
82  rc = send(client_sock, sndbuf, msgsize, 0);
83  assert(rc==msgsize);
84 
85  zmq_msg_t msg;
86  zmq_msg_init(&msg);
87 
88  int rcvbytes = 0;
89  while (rcvbytes==0) // skip connection notification, if any
90  {
91  rc = zmq_msg_recv(&msg, zsock, 0); // peerid
92  assert(rc!=-1);
93  assert(zmq_msg_more(&msg));
94  rcvbytes = zmq_msg_recv(&msg, zsock, 0);
95  assert(rcvbytes!=-1);
96  assert(!zmq_msg_more(&msg));
97  }
98 
99  // for this test, we only collect the first chunk
100  // since the corruption already occurs in the first chunk
101  memcpy(rcvbuf, zmq_msg_data(&msg), zmq_msg_size(&msg));
102 
103  zmq_msg_close(&msg);
104  zmq_close(zsock);
105  close(client_sock);
106 
107  zmq_ctx_destroy(zctx);
108 
109  assert(rcvbytes >= 4);
110 
111  // notice that only the 1st byte gets corrupted
112  assert(rcvbuf[3]==0xef);
113  assert(rcvbuf[2]==0xbe);
114  assert(rcvbuf[1]==0xad);
115  assert(rcvbuf[0]==0xde);
116 
117  (void)(rc); // avoid -Wunused-but-set-variable warning in release build
118 }
119 
#define ZMQ_STREAM
Definition: zmq.h:257
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
Definition: command.hpp:84
ZMQ_EXPORT void * zmq_msg_data(zmq_msg_t *msg)
Definition: zmq.cpp:666
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
ZMQ_EXPORT int zmq_connect(void *s, const char *addr)
Definition: zmq.cpp:332
ZMQ_EXPORT int zmq_close(void *s)
Definition: zmq.cpp:255
ZMQ_EXPORT int zmq_msg_recv(zmq_msg_t *msg, void *s, int flags)
Definition: zmq.cpp:640
ZMQ_EXPORT int zmq_msg_more(zmq_msg_t *msg)
Definition: zmq.cpp:676
ZMQ_EXPORT int zmq_ctx_destroy(void *context)
Definition: zmq.cpp:236
Definition: zmq.h:221
ZMQ_EXPORT int zmq_msg_close(zmq_msg_t *msg)
Definition: zmq.cpp:651
ZMQ_EXPORT size_t zmq_msg_size(zmq_msg_t *msg)
Definition: zmq.cpp:671
ZMQ_EXPORT int zmq_msg_init(zmq_msg_t *msg)
Definition: zmq.cpp:613