libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_security_null.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 #if defined (ZMQ_HAVE_WINDOWS)
32 # include <winsock2.h>
33 # include <ws2tcpip.h>
34 # include <stdexcept>
35 # define close closesocket
36 #else
37 # include <sys/socket.h>
38 # include <netinet/in.h>
39 # include <arpa/inet.h>
40 # include <unistd.h>
41 #endif
42 
43 static void
45 {
46  // Process ZAP requests forever
47  while (true) {
48  char *version = s_recv (handler);
49  if (!version)
50  break; // Terminating
51 
52  char *sequence = s_recv (handler);
53  char *domain = s_recv (handler);
54  char *address = s_recv (handler);
55  char *identity = s_recv (handler);
56  char *mechanism = s_recv (handler);
57 
58  assert (streq (version, "1.0"));
59  assert (streq (mechanism, "NULL"));
60 
61  s_sendmore (handler, version);
62  s_sendmore (handler, sequence);
63  if (streq (domain, "TEST")) {
64  s_sendmore (handler, "200");
65  s_sendmore (handler, "OK");
66  s_sendmore (handler, "anonymous");
67  s_send (handler, "");
68  }
69  else {
70  s_sendmore (handler, "400");
71  s_sendmore (handler, "BAD DOMAIN");
72  s_sendmore (handler, "");
73  s_send (handler, "");
74  }
75  free (version);
76  free (sequence);
77  free (domain);
78  free (address);
79  free (identity);
80  free (mechanism);
81  }
82  close_zero_linger (handler);
83 }
84 
85 int main (void)
86 {
88  void *ctx = zmq_ctx_new ();
89  assert (ctx);
90 
91  // Spawn ZAP handler
92  // We create and bind ZAP socket in main thread to avoid case
93  // where child thread does not start up fast enough.
94  void *handler = zmq_socket (ctx, ZMQ_REP);
95  assert (handler);
96  int rc = zmq_bind (handler, "inproc://zeromq.zap.01");
97  assert (rc == 0);
98  void *zap_thread = zmq_threadstart (&zap_handler, handler);
99 
100  // We bounce between a binding server and a connecting client
101 
102  // We first test client/server with no ZAP domain
103  // Libzmq does not call our ZAP handler, the connect must succeed
104  void *server = zmq_socket (ctx, ZMQ_DEALER);
105  assert (server);
106  void *client = zmq_socket (ctx, ZMQ_DEALER);
107  assert (client);
108  rc = zmq_bind (server, "tcp://127.0.0.1:9000");
109  assert (rc == 0);
110  rc = zmq_connect (client, "tcp://127.0.0.1:9000");
111  assert (rc == 0);
112  bounce (server, client);
113  close_zero_linger (client);
114  close_zero_linger (server);
115 
116  // Now define a ZAP domain for the server; this enables
117  // authentication. We're using the wrong domain so this test
118  // must fail.
119  server = zmq_socket (ctx, ZMQ_DEALER);
120  assert (server);
121  client = zmq_socket (ctx, ZMQ_DEALER);
122  assert (client);
123  rc = zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "WRONG", 5);
124  assert (rc == 0);
125  rc = zmq_bind (server, "tcp://127.0.0.1:9001");
126  assert (rc == 0);
127  rc = zmq_connect (client, "tcp://127.0.0.1:9001");
128  assert (rc == 0);
129  expect_bounce_fail (server, client);
130  close_zero_linger (client);
131  close_zero_linger (server);
132 
133  // Now use the right domain, the test must pass
134  server = zmq_socket (ctx, ZMQ_DEALER);
135  assert (server);
136  client = zmq_socket (ctx, ZMQ_DEALER);
137  assert (client);
138  rc = zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "TEST", 4);
139  assert (rc == 0);
140  rc = zmq_bind (server, "tcp://127.0.0.1:9002");
141  assert (rc == 0);
142  rc = zmq_connect (client, "tcp://127.0.0.1:9002");
143  assert (rc == 0);
144  bounce (server, client);
145  close_zero_linger (client);
146  close_zero_linger (server);
147 
148  // Unauthenticated messages from a vanilla socket shouldn't be received
149  server = zmq_socket (ctx, ZMQ_DEALER);
150  assert (server);
151  rc = zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "WRONG", 5);
152  assert (rc == 0);
153  rc = zmq_bind (server, "tcp://127.0.0.1:9003");
154  assert (rc == 0);
155 
156  struct sockaddr_in ip4addr;
157  int s;
158 
159  ip4addr.sin_family = AF_INET;
160  ip4addr.sin_port = htons(9003);
161 #if defined (ZMQ_HAVE_WINDOWS) && (_WIN32_WINNT < 0x0600)
162  ip4addr.sin_addr.s_addr = inet_addr ("127.0.0.1");
163 #else
164  inet_pton(AF_INET, "127.0.0.1", &ip4addr.sin_addr);
165 #endif
166 
167  s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
168  rc = connect (s, (struct sockaddr*) &ip4addr, sizeof ip4addr);
169  assert (rc > -1);
170  // send anonymous ZMTP/1.0 greeting
171  send (s, "\x01\x00", 2, 0);
172  // send sneaky message that shouldn't be received
173  send (s, "\x08\x00sneaky\0", 9, 0);
174  int timeout = 250;
175  zmq_setsockopt (server, ZMQ_RCVTIMEO, &timeout, sizeof (timeout));
176  char *buf = s_recv (server);
177  if (buf != NULL) {
178  printf ("Received unauthenticated message: %s\n", buf);
179  assert (buf == NULL);
180  }
181  close (s);
182  close_zero_linger (server);
183 
184  // Shutdown
185  rc = zmq_ctx_term (ctx);
186  assert (rc == 0);
187  // Wait until ZAP handler terminates
188  zmq_threadclose (zap_thread);
189 
190  return 0;
191 }
ZMQ_EXPORT int zmq_setsockopt(void *s, int option, const void *optval, size_t optvallen)
Definition: zmq.cpp:265
int main(void)
int s_sendmore(void *socket, const char *string)
Definition: testutil.hpp:190
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
static void zap_handler(void *handler)
#define ZMQ_DEALER
Definition: zmq.h:251
#define ZMQ_REP
Definition: zmq.h:250
void setup_test_environment(void)
Definition: testutil.hpp:285
void expect_bounce_fail(void *server, void *client)
Definition: testutil.hpp:128
void handler(int timer_id, void *arg)
Definition: test_timers.cpp:43
#define ZMQ_ZAP_DOMAIN
Definition: zmq.h:308
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
ZMQ_EXPORT void zmq_threadclose(void *thread)
Definition: zmq_utils.cpp:85
#define streq(s1, s2)
Definition: testutil.hpp:195
ZMQ_EXPORT int zmq_connect(void *s, const char *addr)
Definition: zmq.cpp:332
char * s_recv(void *socket)
Definition: testutil.hpp:170
void close_zero_linger(void *socket)
Definition: testutil.hpp:275
void bounce(void *server, void *client)
Definition: testutil.hpp:73
ZMQ_EXPORT int zmq_bind(void *s, const char *addr)
Definition: zmq.cpp:321
ZMQ_EXPORT int zmq_ctx_term(void *context)
Definition: zmq.cpp:162
int s_send(void *socket, const char *string)
Definition: testutil.hpp:183
ZMQ_EXPORT void * zmq_threadstart(zmq_thread_fn *func, void *arg)
Definition: zmq_utils.cpp:78
#define ZMQ_RCVTIMEO
Definition: zmq.h:284
const char * address
Definition: test_fork.cpp:32