test_security_null.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 #if defined(ZMQ_HAVE_WINDOWS)
7 #include <winsock2.h>
8 #include <ws2tcpip.h>
9 #include <stdexcept>
10 #define close closesocket
11 #else
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <unistd.h>
16 #endif
17 
18 #include <stdlib.h>
19 
20 static void zap_handler (void *handler_)
21 {
22  // Process ZAP requests forever
23  while (true) {
24  char *version = s_recv (handler_);
25  if (!version)
26  break; // Terminating
27 
28  char *sequence = s_recv (handler_);
29  char *domain = s_recv (handler_);
30  char *address = s_recv (handler_);
31  char *routing_id = s_recv (handler_);
32  char *mechanism = s_recv (handler_);
33 
35  TEST_ASSERT_EQUAL_STRING ("NULL", mechanism);
36 
38  send_string_expect_success (handler_, sequence, ZMQ_SNDMORE);
39  if (streq (domain, "TEST")) {
40  send_string_expect_success (handler_, "200", ZMQ_SNDMORE);
41  send_string_expect_success (handler_, "OK", ZMQ_SNDMORE);
42  send_string_expect_success (handler_, "anonymous", ZMQ_SNDMORE);
43  send_string_expect_success (handler_, "", 0);
44  } else {
45  send_string_expect_success (handler_, "400", ZMQ_SNDMORE);
46  send_string_expect_success (handler_, "BAD DOMAIN", ZMQ_SNDMORE);
48  send_string_expect_success (handler_, "", 0);
49  }
50  free (version);
51  free (sequence);
52  free (domain);
53  free (address);
54  free (routing_id);
55  free (mechanism);
56  }
57  close_zero_linger (handler_);
58 }
59 
60 void *zap_thread;
61 
62 static void setup_zap_handler ()
63 {
64  // Spawn ZAP handler
65  // We create and bind ZAP socket in main thread to avoid case
66  // where child thread does not start up fast enough.
68  TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (handler, "inproc://zeromq.zap.01"));
70 }
71 
72 static void teardown_zap_handler ()
73 {
74  // Wait until ZAP handler terminates
76 }
77 
78 void setUp ()
79 {
82 }
83 
84 void tearDown ()
85 {
88 }
89 
91 {
92  // We first test client/server with no ZAP domain
93  // Libzmq does not call our ZAP handler, the connect must succeed
99  bounce (server, client);
102 }
103 
105 {
106  // Now define a ZAP domain for the server; this enables
107  // authentication. We're using the wrong domain so this test
108  // must fail.
112  zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "WRONG", 5));
119 }
120 
122 {
123  // Now use the right domain, the test must pass
127  zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "TEST", 4));
131  bounce (server, client);
134 }
135 
137 {
138  // Unauthenticated messages from a vanilla socket shouldn't be received
141  zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "WRONG", 5));
144 
146 
147  // send anonymous ZMTP/1.0 greeting
148  send (s, "\x01\x00", 2, 0);
149  // send sneaky message that shouldn't be received
150  send (s, "\x08\x00sneaky\0", 9, 0);
151  int timeout = 250;
153  char *buf = s_recv (server);
154  if (buf != NULL) {
155  printf ("Received unauthenticated message: %s\n", buf);
157  }
158  close (s);
160 }
161 
162 int main ()
163 {
165 
166  UNITY_BEGIN ();
171  return UNITY_END ();
172 }
bounce
static void bounce(void *socket_)
Definition: test_req_relaxed.cpp:50
TEST_ASSERT_EQUAL_STRING
#define TEST_ASSERT_EQUAL_STRING(expected, actual)
Definition: unity.h:235
domain
const char domain[]
Definition: test_security_plain.cpp:74
setup_zap_handler
static void setup_zap_handler()
Definition: test_security_null.cpp:62
NULL
NULL
Definition: test_security_zap.cpp:405
UNITY_END
return UNITY_END()
zmq_threadstart
ZMQ_EXPORT void * zmq_threadstart(zmq_thread_fn *func_, void *arg_)
Definition: zmq_utils.cpp:54
test_no_domain
void test_no_domain()
Definition: test_security_null.cpp:90
main
int main()
Definition: test_security_null.cpp:162
s
XmlRpcServer s
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
setup_test_context
void setup_test_context()
Definition: testutil_unity.cpp:179
bind_loopback_ipv4
void bind_loopback_ipv4(void *socket_, char *my_endpoint_, size_t len_)
Definition: testutil_unity.cpp:246
teardown_test_context
void teardown_test_context()
Definition: testutil_unity.cpp:189
get_test_context
void * get_test_context()
Definition: testutil_unity.cpp:184
client
void client(int num)
Definition: test_multithread.cpp:134
address
const char * address
Definition: builds/zos/test_fork.cpp:6
send
void send(fd_t fd_, const char(&data_)[N])
Definition: test_security_curve.cpp:209
test_context_socket_close_zero_linger
void * test_context_socket_close_zero_linger(void *socket_)
Definition: testutil_unity.cpp:215
close_zero_linger
void close_zero_linger(void *socket_)
Definition: testutil.cpp:193
s_recv
char * s_recv(void *socket_)
Definition: testutil.cpp:123
zmq_connect
ZMQ_EXPORT int zmq_connect(void *s_, const char *addr_)
Definition: zmq.cpp:307
test_vanilla_socket
void test_vanilla_socket()
Definition: test_security_null.cpp:136
test_wrong_domain_fails
void test_wrong_domain_fails()
Definition: test_security_null.cpp:104
testutil_unity.hpp
ZMQ_DEALER
#define ZMQ_DEALER
Definition: zmq.h:263
zmq_setsockopt
ZMQ_EXPORT int zmq_setsockopt(void *s_, int option_, const void *optval_, size_t optvallen_)
Definition: zmq.cpp:250
zmq_threadclose
ZMQ_EXPORT void zmq_threadclose(void *thread_)
Definition: zmq_utils.cpp:62
testutil.hpp
ZMQ_REP
#define ZMQ_REP
Definition: zmq.h:262
connect_socket
fd_t connect_socket(const char *endpoint_, const int af_, const int protocol_)
Definition: testutil.cpp:353
my_endpoint
char my_endpoint[MAX_SOCKET_STRING]
Definition: test_security_curve.cpp:31
MAX_SOCKET_STRING
#define MAX_SOCKET_STRING
Definition: libzmq/tests/testutil.hpp:35
zmq_bind
ZMQ_EXPORT int zmq_bind(void *s_, const char *addr_)
Definition: zmq.cpp:299
timeout
GLbitfield GLuint64 timeout
Definition: glcorearb.h:3588
zmq_socket
ZMQ_EXPORT void * zmq_socket(void *, int type_)
Definition: zmq.cpp:230
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
server
void * server
Definition: test_security_curve.cpp:29
buf
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:4175
zap_handler
static void zap_handler(void *handler_)
Definition: test_security_null.cpp:20
streq
bool streq(const char *lhs_, const char *rhs_)
Definition: testutil.cpp:508
ZMQ_ZAP_DOMAIN
#define ZMQ_ZAP_DOMAIN
Definition: zmq.h:320
setUp
void setUp()
Definition: test_security_null.cpp:78
expect_bounce_fail
void expect_bounce_fail(void *server_, void *client_)
Definition: testutil.cpp:107
send_string_expect_success
void send_string_expect_success(void *socket_, const char *str_, int flags_)
Definition: testutil_unity.cpp:94
zap_thread
void * zap_thread
Definition: test_security_null.cpp:60
ZMQ_RCVTIMEO
#define ZMQ_RCVTIMEO
Definition: zmq.h:296
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
fd_t
zmq_fd_t fd_t
Definition: libzmq/tests/testutil.hpp:98
handler
void * handler
Definition: test_security_curve.cpp:27
test_success
void test_success()
Definition: test_security_null.cpp:121
tearDown
void tearDown()
Definition: test_security_null.cpp:84
version
static struct @0 version
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: proxy_thr.cpp:47
teardown_zap_handler
static void teardown_zap_handler()
Definition: test_security_null.cpp:72
TEST_ASSERT_NULL
#define TEST_ASSERT_NULL(pointer)
Definition: unity.h:124


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