76
88
void *zap_thread = zmq_threadstart (&zap_handler, handler);
78
90
// We bounce between a binding server and a connecting client
92
// We first test client/server with no ZAP domain
93
// Libzmq does not call our ZAP handler, the connect must succeed
79
94
void *server = zmq_socket (ctx, ZMQ_DEALER);
81
96
void *client = zmq_socket (ctx, ZMQ_DEALER);
84
// We first test client/server with no ZAP domain
85
// Libzmq does not call our ZAP handler, the connect must succeed
86
98
rc = zmq_bind (server, "tcp://127.0.0.1:9000");
88
rc = zmq_connect (client, "tcp://localhost:9000");
100
rc = zmq_connect (client, "tcp://127.0.0.1:9000");
90
102
bounce (server, client);
91
zmq_unbind (server, "tcp://127.0.0.1:9000");
92
zmq_disconnect (client, "tcp://localhost:9000");
103
close_zero_linger (client);
104
close_zero_linger (server);
94
106
// Now define a ZAP domain for the server; this enables
95
107
// authentication. We're using the wrong domain so this test
97
// **************************************************************
98
// PH: the following causes libzmq to get confused, so that the
99
// next step fails. To reproduce, uncomment this block. Note that
100
// even creating a new client/server socket pair, the behaviour
102
// **************************************************************
103
// Destroying the old sockets and creating new ones isn't needed,
104
// but it shows that the problem isn't related to specific sockets.
105
//close_zero_linger (client);
106
//close_zero_linger (server);
107
//server = zmq_socket (ctx, ZMQ_DEALER);
109
//client = zmq_socket (ctx, ZMQ_DEALER);
111
//// The above code should not be required
112
//rc = zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "WRONG", 5);
114
//rc = zmq_bind (server, "tcp://127.0.0.1:9001");
116
//rc = zmq_connect (client, "tcp://localhost:9001");
118
//expect_bounce_fail (server, client);
119
//zmq_unbind (server, "tcp://127.0.0.1:9001");
120
//zmq_disconnect (client, "tcp://localhost:9001");
109
server = zmq_socket (ctx, ZMQ_DEALER);
111
client = zmq_socket (ctx, ZMQ_DEALER);
113
rc = zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "WRONG", 5);
115
rc = zmq_bind (server, "tcp://127.0.0.1:9001");
117
rc = zmq_connect (client, "tcp://127.0.0.1:9001");
119
expect_bounce_fail (server, client);
120
close_zero_linger (client);
121
close_zero_linger (server);
122
123
// Now use the right domain, the test must pass
124
server = zmq_socket (ctx, ZMQ_DEALER);
126
client = zmq_socket (ctx, ZMQ_DEALER);
123
128
rc = zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "TEST", 4);
124
129
assert (rc == 0);
125
130
rc = zmq_bind (server, "tcp://127.0.0.1:9002");
126
131
assert (rc == 0);
127
rc = zmq_connect (client, "tcp://localhost:9002");
132
rc = zmq_connect (client, "tcp://127.0.0.1:9002");
128
133
assert (rc == 0);
129
// **************************************************************
130
// PH: it fails here; though the ZAP reply is 200 OK, and
131
// null_mechanism.cpp correctly parses that, the connection
132
// never succeeds and the test hangs.
133
// **************************************************************
134
134
bounce (server, client);
135
zmq_unbind (server, "tcp://127.0.0.1:9002");
136
zmq_disconnect (client, "tcp://localhost:9002");
135
close_zero_linger (client);
136
close_zero_linger (server);
138
// Unauthenticated messages from a vanilla socket shouldn't be received
139
server = zmq_socket (ctx, ZMQ_DEALER);
141
rc = zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "WRONG", 5);
143
rc = zmq_bind (server, "tcp://127.0.0.1:9003");
146
struct sockaddr_in ip4addr;
149
ip4addr.sin_family = AF_INET;
150
ip4addr.sin_port = htons(9003);
151
inet_pton(AF_INET, "127.0.0.1", &ip4addr.sin_addr);
153
s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
154
rc = connect (s, (struct sockaddr*) &ip4addr, sizeof ip4addr);
156
// send anonymous ZMTP/1.0 greeting
157
send (s, "\x01\x00", 2, 0);
158
// send sneaky message that shouldn't be received
159
send (s, "\x08\x00sneaky\0", 9, 0);
161
zmq_setsockopt (server, ZMQ_RCVTIMEO, &timeout, sizeof (timeout));
162
char *buf = s_recv (server);
164
printf ("Received unauthenticated message: %s\n", buf);
165
assert (buf == NULL);
168
close_zero_linger (server);
139
close_zero_linger (client);
140
close_zero_linger (server);
141
171
rc = zmq_ctx_term (ctx);
142
172
assert (rc == 0);
144
// Wait until ZAP handler terminates.
173
// Wait until ZAP handler terminates
145
174
zmq_threadclose (zap_thread);