test_security_plain.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 #include <stdlib.h>
7 #include <string.h>
8 
9 static void zap_handler (void *zap_)
10 {
11  // Process ZAP requests forever
12  while (true) {
13  char *version = s_recv (zap_);
14  if (!version)
15  break; // Terminating
16  char *sequence = s_recv (zap_);
17  char *domain = s_recv (zap_);
18  char *address = s_recv (zap_);
19  char *routing_id = s_recv (zap_);
20  char *mechanism = s_recv (zap_);
21  char *username = s_recv (zap_);
22  char *password = s_recv (zap_);
23 
25  TEST_ASSERT_EQUAL_STRING ("PLAIN", mechanism);
26  TEST_ASSERT_EQUAL_STRING ("IDENT", routing_id);
27 
29  send_string_expect_success (zap_, sequence, ZMQ_SNDMORE);
30  if (streq (username, "admin") && streq (password, "password")) {
33  send_string_expect_success (zap_, "anonymous", ZMQ_SNDMORE);
34  send_string_expect_success (zap_, "", 0);
35  } else {
37  send_string_expect_success (zap_, "Invalid username or password",
38  ZMQ_SNDMORE);
40  send_string_expect_success (zap_, "", 0);
41  }
42  free (version);
43  free (sequence);
44  free (domain);
45  free (address);
46  free (routing_id);
47  free (mechanism);
48  free (username);
49  free (password);
50  }
52 }
53 
54 void *zap_thread;
55 
57 
58 static void setup_zap_handler ()
59 {
60  // Spawn ZAP handler
61  // We create and bind ZAP socket in main thread to avoid case
62  // where child thread does not start up fast enough.
64  TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (handler, "inproc://zeromq.zap.01"));
66 }
67 
68 static void teardown_zap_handler ()
69 {
70  // Wait until ZAP handler terminates
72 }
73 
74 const char domain[] = "test";
75 
76 void *server;
77 
78 static void setup_server ()
79 {
80  // Server socket will accept connections
83  zmq_setsockopt (server, ZMQ_ROUTING_ID, "IDENT", 6));
86  const int as_server = 1;
88  zmq_setsockopt (server, ZMQ_PLAIN_SERVER, &as_server, sizeof (int)));
90 }
91 
92 static void teardown_server ()
93 {
95 }
96 
97 void setUp ()
98 {
101  setup_server ();
102 }
103 
104 void tearDown ()
105 {
106  teardown_server ();
109 }
110 
112 {
113  // Check PLAIN security with correct username/password
115  const char username[] = "admin";
117  zmq_setsockopt (client, ZMQ_PLAIN_USERNAME, username, strlen (username)));
118  const char password[] = "password";
120  zmq_setsockopt (client, ZMQ_PLAIN_PASSWORD, password, strlen (password)));
122  bounce (server, client);
124 }
125 
127 {
128  // Check PLAIN security with badly configured client (as_server)
129  // This will be caught by the plain_server class, not passed to ZAP
133  const int as_server = 1;
135  zmq_setsockopt (client, ZMQ_PLAIN_SERVER, &as_server, sizeof (int)));
139 }
140 
142 {
143  // Check PLAIN security -- failed authentication
145  const char username[] = "wronguser";
146  const char password[] = "wrongpass";
148  zmq_setsockopt (client, ZMQ_PLAIN_USERNAME, username, strlen (username)));
150  zmq_setsockopt (client, ZMQ_PLAIN_PASSWORD, password, strlen (password)));
154 }
155 
157 {
158  // Unauthenticated messages from a vanilla socket shouldn't be received
160  // send anonymous ZMTP/1.0 greeting
161  send (s, "\x01\x00", 2, 0);
162  // send sneaky message that shouldn't be received
163  send (s, "\x08\x00sneaky\0", 9, 0);
164  int timeout = 250;
166  char *buf = s_recv (server);
167  if (buf != NULL) {
168  printf ("Received unauthenticated message: %s\n", buf);
170  }
171  close (s);
172 }
173 
174 int main (void)
175 {
177 
178  UNITY_BEGIN ();
183  return UNITY_END ();
184 }
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
ZMQ_PLAIN_USERNAME
#define ZMQ_PLAIN_USERNAME
Definition: zmq.h:310
NULL
NULL
Definition: test_security_zap.cpp:405
UNITY_END
return UNITY_END()
setUp
void setUp()
Definition: test_security_plain.cpp:97
teardown_server
static void teardown_server()
Definition: test_security_plain.cpp:92
zmq_threadstart
ZMQ_EXPORT void * zmq_threadstart(zmq_thread_fn *func_, void *arg_)
Definition: zmq_utils.cpp:54
tearDown
void tearDown()
Definition: test_security_plain.cpp:104
s
XmlRpcServer s
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
ZMQ_PLAIN_SERVER
#define ZMQ_PLAIN_SERVER
Definition: zmq.h:309
setup_server
static void setup_server()
Definition: test_security_plain.cpp:78
ZMQ_PLAIN_PASSWORD
#define ZMQ_PLAIN_PASSWORD
Definition: zmq.h:311
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_plain_wrong_credentials_fails
void test_plain_wrong_credentials_fails()
Definition: test_security_plain.cpp:141
test_context_socket_close_zero_linger
void * test_context_socket_close_zero_linger(void *socket_)
Definition: testutil_unity.cpp:215
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_plain_client_as_server_fails
void test_plain_client_as_server_fails()
Definition: test_security_plain.cpp:126
zap_thread
void * zap_thread
Definition: test_security_plain.cpp:54
testutil_unity.hpp
my_endpoint
char my_endpoint[MAX_SOCKET_STRING]
Definition: test_security_plain.cpp:56
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
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
zap_handler
static void zap_handler(void *zap_)
Definition: test_security_plain.cpp:9
timeout
GLbitfield GLuint64 timeout
Definition: glcorearb.h:3588
zmq_socket
ZMQ_EXPORT void * zmq_socket(void *, int type_)
Definition: zmq.cpp:230
setup_zap_handler
static void setup_zap_handler()
Definition: test_security_plain.cpp:58
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
buf
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:4175
streq
bool streq(const char *lhs_, const char *rhs_)
Definition: testutil.cpp:508
zmq_close
ZMQ_EXPORT int zmq_close(void *s_)
Definition: zmq.cpp:241
ZMQ_ZAP_DOMAIN
#define ZMQ_ZAP_DOMAIN
Definition: zmq.h:320
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
ZMQ_RCVTIMEO
#define ZMQ_RCVTIMEO
Definition: zmq.h:296
main
int main(void)
Definition: test_security_plain.cpp:174
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
teardown_zap_handler
static void teardown_zap_handler()
Definition: test_security_plain.cpp:68
ZMQ_ROUTING_ID
#define ZMQ_ROUTING_ID
Definition: zmq.h:277
handler
void * handler
Definition: test_security_curve.cpp:27
server
void * server
Definition: test_security_plain.cpp:76
version
static struct @0 version
test_context_socket_close
void * test_context_socket_close(void *socket_)
Definition: testutil_unity.cpp:208
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: proxy_thr.cpp:47
test_plain_vanilla_socket
void test_plain_vanilla_socket()
Definition: test_security_plain.cpp:156
test_plain_success
void test_plain_success()
Definition: test_security_plain.cpp:111
TEST_ASSERT_NULL
#define TEST_ASSERT_NULL(pointer)
Definition: unity.h:124


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