1/*
2 Copyright (c) 2007-2017 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 <stdlib.h>
34#include <unity.h>
35
36void setUp ()
37{
38}
39
40void tearDown ()
41{
42}
43
44static void zap_handler (void *handler_)
45{
46 uint8_t metadata[] = {5, 'H', 'e', 'l', 'l', 'o', 0, 0,
47 0, 5, 'W', 'o', 'r', 'l', 'd'};
48
49 // Process ZAP requests forever
50 while (true) {
51 char *version = s_recv (handler_);
52 if (!version)
53 break; // Terminating
54
55 char *sequence = s_recv (handler_);
56 char *domain = s_recv (handler_);
57 char *address = s_recv (handler_);
58 char *routing_id = s_recv (handler_);
59 char *mechanism = s_recv (handler_);
60
61 TEST_ASSERT_EQUAL_STRING ("1.0", version);
62 TEST_ASSERT_EQUAL_STRING ("NULL", mechanism);
63
64 send_string_expect_success (handler_, version, ZMQ_SNDMORE);
65 send_string_expect_success (handler_, sequence, ZMQ_SNDMORE);
66 if (streq (domain, "DOMAIN")) {
67 send_string_expect_success (handler_, "200", ZMQ_SNDMORE);
68 send_string_expect_success (handler_, "OK", ZMQ_SNDMORE);
69 send_string_expect_success (handler_, "anonymous", ZMQ_SNDMORE);
70 zmq_send (handler_, metadata, sizeof (metadata), 0);
71 } else {
72 send_string_expect_success (handler_, "400", ZMQ_SNDMORE);
73 send_string_expect_success (handler_, "BAD DOMAIN", ZMQ_SNDMORE);
74 send_string_expect_success (handler_, "", ZMQ_SNDMORE);
75 send_string_expect_success (handler_, "", 0);
76 }
77 free (version);
78 free (sequence);
79 free (domain);
80 free (address);
81 free (routing_id);
82 free (mechanism);
83 }
84 close_zero_linger (handler_);
85}
86
87void test_metadata ()
88{
89 char my_endpoint[MAX_SOCKET_STRING];
90 setup_test_context ();
91
92 // Spawn ZAP handler
93 // We create and bind ZAP socket in main thread to avoid case
94 // where child thread does not start up fast enough.
95 void *handler = zmq_socket (get_test_context (), ZMQ_REP);
96 TEST_ASSERT_NOT_NULL (handler);
97 TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (handler, "inproc://zeromq.zap.01"));
98 void *zap_thread = zmq_threadstart (&zap_handler, handler);
99
100 void *server = test_context_socket (ZMQ_DEALER);
101 void *client = test_context_socket (ZMQ_DEALER);
102 TEST_ASSERT_SUCCESS_ERRNO (
103 zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "DOMAIN", 6));
104 bind_loopback_ipv4 (server, my_endpoint, sizeof (my_endpoint));
105 TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
106
107 send_string_expect_success (client, "This is a message", 0);
108 zmq_msg_t msg;
109 zmq_msg_init (&msg);
110 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, server, 0));
111 TEST_ASSERT_EQUAL_STRING ("World", zmq_msg_gets (&msg, "Hello"));
112 TEST_ASSERT_EQUAL_STRING ("DEALER", zmq_msg_gets (&msg, "Socket-Type"));
113 TEST_ASSERT_EQUAL_STRING ("anonymous", zmq_msg_gets (&msg, "User-Id"));
114 TEST_ASSERT_EQUAL_STRING ("127.0.0.1", zmq_msg_gets (&msg, "Peer-Address"));
115
116 TEST_ASSERT_NULL (zmq_msg_gets (&msg, "No Such"));
117 TEST_ASSERT_EQUAL_INT (EINVAL, zmq_errno ());
118 zmq_msg_close (&msg);
119
120 test_context_socket_close_zero_linger (client);
121 test_context_socket_close_zero_linger (server);
122
123 // Shutdown
124 teardown_test_context ();
125
126 // Wait until ZAP handler terminates
127 zmq_threadclose (zap_thread);
128}
129
130int main ()
131{
132 setup_test_environment ();
133 UNITY_BEGIN ();
134 RUN_TEST (test_metadata);
135 return UNITY_END ();
136}
137