Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
L
libzmq
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
libzmq
Commits
3a50aa44
Commit
3a50aa44
authored
Sep 02, 2013
by
Ian Barber
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #641 from hintjens/master
Fixed regression tests
parents
c128fac7
fba56120
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
404 additions
and
489 deletions
+404
-489
.gitignore
.gitignore
+5
-0
zmq_utils.h
include/zmq_utils.h
+2
-2
curve_server.cpp
src/curve_server.cpp
+1
-1
null_mechanism.cpp
src/null_mechanism.cpp
+8
-0
plain_mechanism.cpp
src/plain_mechanism.cpp
+1
-1
Makefile.am
tests/Makefile.am
+6
-3
test_fork.cpp
tests/test_fork.cpp
+43
-72
test_linger.cpp
tests/test_linger.cpp
+47
-52
test_security_curve.cpp
tests/test_security_curve.cpp
+103
-139
test_security_null.cpp
tests/test_security_null.cpp
+109
-0
test_security_plain.cpp
tests/test_security_plain.cpp
+75
-213
testutil.hpp
tests/testutil.hpp
+4
-6
No files found.
.gitignore
View file @
3a50aa44
...
@@ -61,6 +61,11 @@ tests/test_spec_router
...
@@ -61,6 +61,11 @@ tests/test_spec_router
tests/test_req_request_ids
tests/test_req_request_ids
tests/test_req_strict
tests/test_req_strict
tests/test_fork
tests/test_fork
tests/test_conflate
tests/test_linger
tests/test_security_null
tests/test_security_null.opp
tests/test_security_plain
src/platform.hpp*
src/platform.hpp*
src/stamp-h1
src/stamp-h1
perf/local_lat
perf/local_lat
...
...
include/zmq_utils.h
View file @
3a50aa44
...
@@ -59,10 +59,10 @@ ZMQ_EXPORT unsigned long zmq_stopwatch_stop (void *watch_);
...
@@ -59,10 +59,10 @@ ZMQ_EXPORT unsigned long zmq_stopwatch_stop (void *watch_);
ZMQ_EXPORT
void
zmq_sleep
(
int
seconds_
);
ZMQ_EXPORT
void
zmq_sleep
(
int
seconds_
);
/* Start a thread. Returns a handle to the thread. */
/* Start a thread. Returns a handle to the thread. */
ZMQ_EXPORT
void
*
zmq_threadstart
(
zmq_thread_fn
*
func
,
void
*
arg
);
ZMQ_EXPORT
void
*
zmq_threadstart
(
zmq_thread_fn
*
func
,
void
*
arg
);
/* Wait for thread to complete then free up resources. */
/* Wait for thread to complete then free up resources. */
ZMQ_EXPORT
void
zmq_threadclose
(
void
*
thread
);
ZMQ_EXPORT
void
zmq_threadclose
(
void
*
thread
);
#undef ZMQ_EXPORT
#undef ZMQ_EXPORT
...
...
src/curve_server.cpp
View file @
3a50aa44
...
@@ -537,7 +537,7 @@ void zmq::curve_server_t::send_zap_request (const uint8_t *key)
...
@@ -537,7 +537,7 @@ void zmq::curve_server_t::send_zap_request (const uint8_t *key)
rc
=
session
->
write_zap_msg
(
&
msg
);
rc
=
session
->
write_zap_msg
(
&
msg
);
errno_assert
(
rc
==
0
);
errno_assert
(
rc
==
0
);
//
i
dentity frame
//
I
dentity frame
rc
=
msg
.
init_size
(
options
.
identity_size
);
rc
=
msg
.
init_size
(
options
.
identity_size
);
errno_assert
(
rc
==
0
);
errno_assert
(
rc
==
0
);
memcpy
(
msg
.
data
(),
options
.
identity
,
options
.
identity_size
);
memcpy
(
msg
.
data
(),
options
.
identity
,
options
.
identity_size
);
...
...
src/null_mechanism.cpp
View file @
3a50aa44
...
@@ -196,6 +196,14 @@ void zmq::null_mechanism_t::send_zap_request ()
...
@@ -196,6 +196,14 @@ void zmq::null_mechanism_t::send_zap_request ()
rc
=
session
->
write_zap_msg
(
&
msg
);
rc
=
session
->
write_zap_msg
(
&
msg
);
errno_assert
(
rc
==
0
);
errno_assert
(
rc
==
0
);
// Identity frame
rc
=
msg
.
init_size
(
options
.
identity_size
);
errno_assert
(
rc
==
0
);
memcpy
(
msg
.
data
(),
options
.
identity
,
options
.
identity_size
);
msg
.
set_flags
(
msg_t
::
more
);
rc
=
session
->
write_zap_msg
(
&
msg
);
errno_assert
(
rc
==
0
);
// Mechanism frame
// Mechanism frame
rc
=
msg
.
init_size
(
5
);
rc
=
msg
.
init_size
(
5
);
errno_assert
(
rc
==
0
);
errno_assert
(
rc
==
0
);
...
...
src/plain_mechanism.cpp
View file @
3a50aa44
...
@@ -380,7 +380,7 @@ void zmq::plain_mechanism_t::send_zap_request (const std::string &username,
...
@@ -380,7 +380,7 @@ void zmq::plain_mechanism_t::send_zap_request (const std::string &username,
rc
=
session
->
write_zap_msg
(
&
msg
);
rc
=
session
->
write_zap_msg
(
&
msg
);
errno_assert
(
rc
==
0
);
errno_assert
(
rc
==
0
);
//
i
dentity frame
//
I
dentity frame
rc
=
msg
.
init_size
(
options
.
identity_size
);
rc
=
msg
.
init_size
(
options
.
identity_size
);
errno_assert
(
rc
==
0
);
errno_assert
(
rc
==
0
);
memcpy
(
msg
.
data
(),
options
.
identity
,
options
.
identity_size
);
memcpy
(
msg
.
data
(),
options
.
identity
,
options
.
identity_size
);
...
...
tests/Makefile.am
View file @
3a50aa44
...
@@ -23,7 +23,8 @@ noinst_PROGRAMS = test_pair_inproc \
...
@@ -23,7 +23,8 @@ noinst_PROGRAMS = test_pair_inproc \
test_stream
\
test_stream
\
test_disconnect_inproc
\
test_disconnect_inproc
\
test_ctx_options
\
test_ctx_options
\
test_security
\
test_security_null
\
test_security_plain
\
test_security_curve
\
test_security_curve
\
test_iov
\
test_iov
\
test_spec_req
\
test_spec_req
\
...
@@ -64,7 +65,8 @@ test_stream_SOURCES = test_stream.cpp
...
@@ -64,7 +65,8 @@ test_stream_SOURCES = test_stream.cpp
test_disconnect_inproc_SOURCES
=
test_disconnect_inproc.cpp
test_disconnect_inproc_SOURCES
=
test_disconnect_inproc.cpp
test_ctx_options_SOURCES
=
test_ctx_options.cpp
test_ctx_options_SOURCES
=
test_ctx_options.cpp
test_iov_SOURCES
=
test_iov.cpp
test_iov_SOURCES
=
test_iov.cpp
test_security_SOURCES
=
test_security.cpp
test_security_null_SOURCES
=
test_security_null.cpp
test_security_plain_SOURCES
=
test_security_plain.cpp
test_security_curve_SOURCES
=
test_security_curve.cpp
test_security_curve_SOURCES
=
test_security_curve.cpp
test_spec_req_SOURCES
=
test_spec_req.cpp
test_spec_req_SOURCES
=
test_spec_req.cpp
test_spec_rep_SOURCES
=
test_spec_rep.cpp
test_spec_rep_SOURCES
=
test_spec_rep.cpp
...
@@ -87,4 +89,4 @@ noinst_PROGRAMS += test_raw_sock
...
@@ -87,4 +89,4 @@ noinst_PROGRAMS += test_raw_sock
test_raw_sock_SOURCES
=
test_raw_sock.cpp
test_raw_sock_SOURCES
=
test_raw_sock.cpp
# Run the test cases
# Run the test cases
TESTS
=
$(noinst_PROGRAMS)
TESTS
=
$(noinst_PROGRAMS)
\ No newline at end of file
tests/test_fork.cpp
View file @
3a50aa44
...
@@ -33,86 +33,57 @@ const char* address = "tcp://127.0.0.1:6571";
...
@@ -33,86 +33,57 @@ const char* address = "tcp://127.0.0.1:6571";
int
main
(
void
)
int
main
(
void
)
{
{
int
rc
;
setup_test_environment
();
setup_test_environment
();
printf
(
"parent: process id = %d
\n
"
,
(
int
)
getpid
());
void
*
ctx
=
zmq_ctx_new
();
void
*
ctx
=
zmq_ctx_new
();
assert
(
ctx
);
assert
(
ctx
);
void
*
sock
=
zmq_socket
(
ctx
,
ZMQ_PULL
);
// Create and bind pull socket to receive messages
assert
(
sock
);
void
*
pull
=
zmq_socket
(
ctx
,
ZMQ_PULL
);
assert
(
pull
);
rc
=
zmq_bind
(
sock
,
address
);
int
rc
=
zmq_bind
(
pull
,
address
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
// wait for io threads to be running
int
pid
=
fork
();
usleep
(
100000
);
printf
(
"about to fork()
\n
"
);
int
pid
=
fork
();
if
(
pid
==
0
)
{
if
(
pid
==
0
)
{
// this is the child process
// Child process
// Immediately close parent sockets and context
usleep
(
100000
);
zmq_close
(
pull
);
printf
(
"child: process id = %d
\n
"
,
(
int
)
getpid
());
zmq_term
(
ctx
);
printf
(
"child: terminating inherited context...
\n
"
);
// close the socket, or the context gets stuck indefinitely
// Create new context, socket, connect and send some messages
// zmq_close(sock);
void
*
child_ctx
=
zmq_ctx_new
();
zmq_term
(
ctx
);
assert
(
child_ctx
);
void
*
push
=
zmq_socket
(
child_ctx
,
ZMQ_PUSH
);
// create new context, socket, connect and send some messages
assert
(
push
);
void
*
ctx2
=
zmq_ctx_new
();
rc
=
zmq_connect
(
push
,
address
);
assert
(
ctx2
);
assert
(
rc
==
0
);
int
count
;
void
*
push
=
zmq_socket
(
ctx2
,
ZMQ_PUSH
);
for
(
count
=
0
;
count
<
NUM_MESSAGES
;
count
++
)
assert
(
push
);
zmq_send
(
push
,
"Hello"
,
5
,
0
);
rc
=
zmq_connect
(
push
,
address
);
zmq_close
(
push
);
assert
(
rc
==
0
);
zmq_ctx_destroy
(
child_ctx
);
exit
(
0
);
const
char
*
message
=
"hello"
;
}
else
{
int
i
;
// Parent process
for
(
i
=
0
;
i
<
NUM_MESSAGES
;
i
+=
1
)
{
int
count
;
printf
(
"child: send message #%d
\n
"
,
i
);
for
(
count
=
0
;
count
<
NUM_MESSAGES
;
count
++
)
{
zmq_send
(
push
,
message
,
strlen
(
message
),
0
);
char
buffer
[
5
];
usleep
(
100000
);
int
num_bytes
=
zmq_recv
(
pull
,
buffer
,
5
,
0
);
}
assert
(
num_bytes
==
5
);
printf
(
"child: closing push socket
\n
"
);
zmq_close
(
push
);
printf
(
"child: destroying child context
\n
"
);
zmq_ctx_destroy
(
ctx2
);
printf
(
"child: done
\n
"
);
exit
(
0
);
}
else
{
// this is the parent process
printf
(
"parent: waiting for %d messages
\n
"
,
NUM_MESSAGES
);
char
buffer
[
1024
];
int
i
;
for
(
i
=
0
;
i
<
NUM_MESSAGES
;
i
+=
1
)
{
int
num_bytes
=
zmq_recv
(
sock
,
buffer
,
1024
,
0
);
assert
(
num_bytes
>
0
);
buffer
[
num_bytes
]
=
0
;
printf
(
"parent: received #%d: %s
\n
"
,
i
,
buffer
);
}
}
int
child_status
;
int
child_status
;
while
(
true
)
{
while
(
true
)
{
rc
=
waitpid
(
pid
,
&
child_status
,
0
);
rc
=
waitpid
(
pid
,
&
child_status
,
0
);
if
(
rc
==
-
1
&&
errno
==
EINTR
)
continue
;
if
(
rc
==
-
1
&&
errno
==
EINTR
)
printf
(
"parent: child exit code = %d, rc = %d, errno = %d
\n
"
,
WEXITSTATUS
(
child_status
),
rc
,
errno
)
;
continue
;
assert
(
rc
>
0
);
assert
(
rc
>
0
);
//
verify the status code of the child was zero.
//
Verify the status code of the child was zero
assert
(
WEXITSTATUS
(
child_status
)
==
0
);
assert
(
WEXITSTATUS
(
child_status
)
==
0
);
break
;
break
;
}
}
printf
(
"parent: done.
\n
"
);
exit
(
0
);
exit
(
0
);
}
}
return
0
;
return
0
;
}
}
tests/test_linger.cpp
View file @
3a50aa44
...
@@ -17,6 +17,10 @@
...
@@ -17,6 +17,10 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
*/
// This test case should be moved or added to issues repository, perhaps,
// or alternatively we can change policy that issue test cases should be
// added to the main build when they're useful. -- PH 2013/09/02
#include "../include/zmq_utils.h"
#include "../include/zmq_utils.h"
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string.h>
...
@@ -24,80 +28,72 @@
...
@@ -24,80 +28,72 @@
#define URL "tcp://127.0.0.1:5560"
#define URL "tcp://127.0.0.1:5560"
// sender connects a PUSH socket
// Sender connects a PUSH socket and sends a message of a given size,
// and sends a message of a given size,
// closing the socket and terminating the context immediately.
// closing the socket and terminating the context immediately.
// LINGER should prevent this from dropping messages.
// LINGER should prevent this from dropping messages.
static
void
sender
(
void
*
vsize
)
static
void
sender
(
void
*
vsize
)
{
{
void
*
context
=
zmq_ctx_new
();
void
*
context
=
zmq_ctx_new
();
void
*
push
=
zmq_socket
(
context
,
ZMQ_PUSH
);
void
*
push
=
zmq_socket
(
context
,
ZMQ_PUSH
);
zmq_msg_t
msg
;
size_t
size
=
((
size_t
*
)
vsize
)
[
0
];
size_t
size
=
((
size_t
*
)
vsize
)[
0
];
int
rc
=
zmq_connect
(
push
,
URL
);
int
rc
=
zmq_connect
(
push
,
URL
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_msg_init_size
(
&
msg
,
size
);
zmq_msg_t
msg
;
rc
=
zmq_msg_init_size
(
&
msg
,
size
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
char
*
buf
=
(
char
*
)
zmq_msg_data
(
&
msg
);
char
*
buf
=
(
char
*
)
zmq_msg_data
(
&
msg
);
assert
(
buf
!=
NULL
);
assert
(
buf
!=
NULL
);
for
(
size_t
i
=
0
;
i
<
size
;
++
i
)
{
memset
(
buf
,
'x'
,
size
);
buf
[
i
]
=
'x'
;
}
printf
(
"Sending %lu B ... "
,
zmq_msg_size
(
&
msg
));
rc
=
zmq_msg_send
(
&
msg
,
push
,
0
);
rc
=
zmq_msg_send
(
&
msg
,
push
,
0
);
assert
((
size_t
)
rc
==
size
);
assert
((
size_t
)
rc
==
size
);
rc
=
zmq_msg_close
(
&
msg
);
rc
=
zmq_msg_close
(
&
msg
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_close
(
push
);
rc
=
zmq_close
(
push
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_ctx_term
(
context
);
rc
=
zmq_ctx_term
(
context
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
}
}
int
main
(
void
)
int
main
(
void
)
{
{
void
*
context
=
zmq_ctx_new
();
void
*
context
=
zmq_ctx_new
();
void
*
pull
=
zmq_socket
(
context
,
ZMQ_PULL
);
void
*
pull
=
zmq_socket
(
context
,
ZMQ_PULL
);
zmq_msg_t
msg
;
zmq_msg_t
msg
;
int
rc
=
zmq_bind
(
pull
,
URL
);
int
rc
=
zmq_bind
(
pull
,
URL
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
// Symptom of test failure is message never received
// Symptom of test failure is message never received
int
rcvtimeo
=
100
;
int
rcvtimeo
=
1000
;
rc
=
zmq_setsockopt
(
pull
,
ZMQ_RCVTIMEO
,
&
rcvtimeo
,
sizeof
(
int
));
rc
=
zmq_setsockopt
(
pull
,
ZMQ_RCVTIMEO
,
&
rcvtimeo
,
sizeof
(
int
));
assert
(
rc
==
0
);
assert
(
rc
==
0
);
for
(
int
p
=
0
;
p
<
25
;
p
+=
4
)
// Single test case that currently provokes failure in libzmq
{
// Note that socket will wait as long as receive timeout, before
size_t
size
=
1
<<
p
;
// returning a "resource unavailable" or an assertion failure in
// poller_base.cpp:31.
// Start the sender thread
size_t
size
=
4000000
;
void
*
send_thread
=
zmq_threadstart
(
&
sender
,
(
void
*
)
&
size
);
// TODO: Allow make check to pass, until this issue is resolved
zmq_msg_init
(
&
msg
);
size
=
40000
;
rc
=
zmq_msg_recv
(
&
msg
,
pull
,
0
);
if
(
rc
==
-
1
)
{
// Start the sender thread and get message
printf
(
"%s
\n
"
,
zmq_strerror
(
zmq_errno
()));
void
*
send_thread
=
zmq_threadstart
(
&
sender
,
(
void
*
)
&
size
);
}
else
{
zmq_msg_init
(
&
msg
);
printf
(
"Received.
\n
"
);
rc
=
zmq_msg_recv
(
&
msg
,
pull
,
0
);
}
assert
((
size_t
)
rc
==
size
);
assert
((
size_t
)
rc
==
size
);
zmq_msg_close
(
&
msg
);
zmq_msg_close
(
&
msg
);
zmq_threadclose
(
send_thread
);
zmq_threadclose
(
send_thread
);
}
zmq_close
(
pull
);
zmq_close
(
pull
);
zmq_ctx_term
(
context
);
zmq_ctx_term
(
context
);
return
0
;
}
return
0
;
}
\ No newline at end of file
tests/test_security_curve.cpp
View file @
3a50aa44
...
@@ -23,45 +23,46 @@
...
@@ -23,45 +23,46 @@
#include <stdlib.h>
#include <stdlib.h>
#include "testutil.hpp"
#include "testutil.hpp"
static
void
zap_handler
(
void
*
zap
)
static
void
zap_handler
(
void
*
ctx
)
{
{
int
timeout
=
250
;
// Create and bind ZAP socket
int
rc
;
void
*
zap
=
zmq_socket
(
ctx
,
ZMQ_REP
);
rc
=
zmq_setsockopt
(
zap
,
ZMQ_RCVTIMEO
,
&
timeout
,
sizeof
(
int
));
assert
(
zap
);
assert
(
rc
==
0
);
int
rc
=
zmq_bind
(
zap
,
"inproc://zeromq.zap.01"
);
char
*
version
=
s_recv
(
zap
);
assert
(
rc
==
0
);
if
(
version
==
NULL
)
{
printf
(
"ZAP timeout
\n
"
);
// Process ZAP requests forever
rc
=
zmq_close
(
zap
);
while
(
true
)
{
assert
(
rc
==
0
);
char
*
version
=
s_recv
(
zap
);
return
;
if
(
!
version
)
break
;
// Terminating
char
*
sequence
=
s_recv
(
zap
);
char
*
domain
=
s_recv
(
zap
);
char
*
address
=
s_recv
(
zap
);
char
*
identity
=
s_recv
(
zap
);
char
*
mechanism
=
s_recv
(
zap
);
char
*
client_key
=
s_recv
(
zap
);
assert
(
streq
(
version
,
"1.0"
));
assert
(
streq
(
mechanism
,
"CURVE"
));
assert
(
streq
(
identity
,
"IDENT"
));
s_sendmore
(
zap
,
version
);
s_sendmore
(
zap
,
sequence
);
s_sendmore
(
zap
,
"200"
);
s_sendmore
(
zap
,
"OK"
);
s_sendmore
(
zap
,
"anonymous"
);
s_send
(
zap
,
""
);
free
(
version
);
free
(
sequence
);
free
(
domain
);
free
(
address
);
free
(
identity
);
free
(
mechanism
);
free
(
client_key
);
}
}
char
*
sequence
=
s_recv
(
zap
);
char
*
domain
=
s_recv
(
zap
);
char
*
address
=
s_recv
(
zap
);
char
*
identity
=
s_recv
(
zap
);
char
*
mechanism
=
s_recv
(
zap
);
char
*
client_key
=
s_recv
(
zap
);
assert
(
streq
(
version
,
"1.0"
));
assert
(
streq
(
mechanism
,
"CURVE"
));
assert
(
streq
(
identity
,
"IDENT"
));
s_sendmore
(
zap
,
version
);
s_sendmore
(
zap
,
sequence
);
s_sendmore
(
zap
,
"200"
);
s_sendmore
(
zap
,
"OK"
);
s_sendmore
(
zap
,
"anonymous"
);
s_send
(
zap
,
""
);
free
(
version
);
free
(
sequence
);
free
(
domain
);
free
(
address
);
free
(
identity
);
free
(
mechanism
);
free
(
client_key
);
rc
=
zmq_close
(
zap
);
rc
=
zmq_close
(
zap
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
}
}
...
@@ -73,22 +74,12 @@ int main (void)
...
@@ -73,22 +74,12 @@ int main (void)
printf
(
"libsodium not installed, skipping CURVE test
\n
"
);
printf
(
"libsodium not installed, skipping CURVE test
\n
"
);
return
0
;
return
0
;
#endif
#endif
setup_test_environment
();
setup_test_environment
();
int
rc
;
size_t
optsize
;
int
mechanism
;
int
as_server
;
void
*
ctx
=
zmq_ctx_new
();
void
*
ctx
=
zmq_ctx_new
();
assert
(
ctx
);
assert
(
ctx
);
// Server socket will accept connections
// Spawn ZAP handler
void
*
server
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
void
*
zap_thread
=
zmq_threadstart
(
&
zap_handler
,
ctx
);
assert
(
server
);
// Client socket that will try to connect to server
void
*
client
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
assert
(
client
);
// Test keys from the zmq_curve man page
// Test keys from the zmq_curve man page
char
client_public
[]
=
"Yne@$w-vo<fVvi]a<NY6T1ed:M$fCG*[IaLV{hID"
;
char
client_public
[]
=
"Yne@$w-vo<fVvi]a<NY6T1ed:M$fCG*[IaLV{hID"
;
...
@@ -96,134 +87,107 @@ int main (void)
...
@@ -96,134 +87,107 @@ int main (void)
char
server_public
[]
=
"rq:rM>}U?@Lns47E1%kR.o@n%FcmmsL/@{H8]yf7"
;
char
server_public
[]
=
"rq:rM>}U?@Lns47E1%kR.o@n%FcmmsL/@{H8]yf7"
;
char
server_secret
[]
=
"JTKVSB%%)wK0E.X)V>+}o?pNmC{O&4W4b!Ni{Lh6"
;
char
server_secret
[]
=
"JTKVSB%%)wK0E.X)V>+}o?pNmC{O&4W4b!Ni{Lh6"
;
as_server
=
1
;
// Server socket will accept connections
rc
=
zmq_setsockopt
(
server
,
ZMQ_CURVE_SERVER
,
&
as_server
,
sizeof
(
int
));
void
*
server
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
assert
(
server
);
int
as_server
=
1
;
int
rc
=
zmq_setsockopt
(
server
,
ZMQ_CURVE_SERVER
,
&
as_server
,
sizeof
(
int
));
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_setsockopt
(
server
,
ZMQ_CURVE_SECRETKEY
,
server_secret
,
40
);
rc
=
zmq_setsockopt
(
server
,
ZMQ_CURVE_SECRETKEY
,
server_secret
,
40
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_setsockopt
(
server
,
ZMQ_IDENTITY
,
"IDENT"
,
6
);
rc
=
zmq_setsockopt
(
server
,
ZMQ_IDENTITY
,
"IDENT"
,
6
);
assert
(
rc
==
0
);
rc
=
zmq_bind
(
server
,
"tcp://*:9998"
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
// Check CURVE security with valid credentials
void
*
client
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
assert
(
client
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_CURVE_SERVERKEY
,
server_public
,
40
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_CURVE_SERVERKEY
,
server_public
,
40
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_CURVE_PUBLICKEY
,
client_public
,
40
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_CURVE_PUBLICKEY
,
client_public
,
40
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_CURVE_SECRETKEY
,
client_secret
,
40
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_CURVE_SECRETKEY
,
client_secret
,
40
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
// Test the client and server both have the right mechanism.
optsize
=
sizeof
(
int
);
rc
=
zmq_getsockopt
(
client
,
ZMQ_MECHANISM
,
&
mechanism
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
mechanism
==
ZMQ_CURVE
);
rc
=
zmq_getsockopt
(
server
,
ZMQ_MECHANISM
,
&
mechanism
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
mechanism
==
ZMQ_CURVE
);
// Test the server bit on both client and server.
rc
=
zmq_getsockopt
(
client
,
ZMQ_CURVE_SERVER
,
&
as_server
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
as_server
==
0
);
rc
=
zmq_getsockopt
(
server
,
ZMQ_CURVE_SERVER
,
&
as_server
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
as_server
==
1
);
// Create and bind ZAP socket
void
*
zap
=
zmq_socket
(
ctx
,
ZMQ_REP
);
assert
(
zap
);
rc
=
zmq_bind
(
zap
,
"inproc://zeromq.zap.01"
);
assert
(
rc
==
0
);
// Spawn ZAP handler
void
*
zap_thread
=
zmq_threadstart
(
&
zap_handler
,
zap
);
rc
=
zmq_bind
(
server
,
"tcp://*:9998"
);
assert
(
rc
==
0
);
rc
=
zmq_connect
(
client
,
"tcp://localhost:9998"
);
rc
=
zmq_connect
(
client
,
"tcp://localhost:9998"
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
bounce
(
server
,
client
);
bounce
(
server
,
client
);
rc
=
zmq_close
(
client
);
rc
=
zmq_close
(
client
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_close
(
server
);
assert
(
rc
==
0
);
// Wait until ZAP handler terminates.
zmq_threadclose
(
zap_thread
);
// Test that Curve rejects inauthenticated connections
// Check CURVE security with a garbage server key
// This will be caught by the curve_server class, not passed to ZAP
// Use the wrong client key
char
garbage_key
[]
=
"0000111122223333444455556666777788889999"
;
strcpy
(
client_public
,
"1111222233334444555566667777888899990000"
);
// Server socket will accept connections
server
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
assert
(
server
);
// Client socket that will try to connect to server
client
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
client
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
assert
(
client
);
assert
(
client
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_CURVE_SERVERKEY
,
garbage_key
,
40
);
as_server
=
1
;
rc
=
zmq_setsockopt
(
server
,
ZMQ_CURVE_SERVER
,
&
as_server
,
sizeof
(
int
));
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_setsockopt
(
server
,
ZMQ_CURVE_SECRETKEY
,
server_secret
,
40
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_CURVE_PUBLICKEY
,
client_public
,
40
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_setsockopt
(
server
,
ZMQ_IDENTITY
,
"IDENT"
,
6
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_CURVE_SECRETKEY
,
client_secret
,
40
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_connect
(
client
,
"tcp://localhost:9998"
);
assert
(
rc
==
0
);
expect_bounce_fail
(
server
,
client
);
close_zero_linger
(
client
);
// Check CURVE security with a garbage client public key
// This will be caught by the curve_server class, not passed to ZAP
client
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
assert
(
client
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_CURVE_SERVERKEY
,
server_public
,
40
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_CURVE_SERVERKEY
,
server_public
,
40
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_CURVE_PUBLICKEY
,
client_public
,
40
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_CURVE_PUBLICKEY
,
garbage_key
,
40
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_CURVE_SECRETKEY
,
client_secret
,
40
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_CURVE_SECRETKEY
,
client_secret
,
40
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_connect
(
client
,
"tcp://localhost:9998"
);
// Test the client and server both have the right mechanism.
optsize
=
sizeof
(
int
);
rc
=
zmq_getsockopt
(
client
,
ZMQ_MECHANISM
,
&
mechanism
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
assert
(
mechanism
==
ZMQ_CURVE
);
expect_bounce_fail
(
server
,
client
);
rc
=
zmq_getsockopt
(
server
,
ZMQ_MECHANISM
,
&
mechanism
,
&
optsize
);
close_zero_linger
(
client
);
// Check CURVE security with a garbage client secret key
// This will be caught by the curve_server class, not passed to ZAP
client
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
assert
(
client
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_CURVE_SERVERKEY
,
server_public
,
40
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
assert
(
mechanism
==
ZMQ_CURVE
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_CURVE_PUBLICKEY
,
client_public
,
40
);
// Test the server bit on both client and server.
rc
=
zmq_getsockopt
(
client
,
ZMQ_CURVE_SERVER
,
&
as_server
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
assert
(
as_server
==
0
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_CURVE_SECRETKEY
,
garbage_key
,
40
);
rc
=
zmq_getsockopt
(
server
,
ZMQ_CURVE_SERVER
,
&
as_server
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
assert
(
as_server
==
1
);
rc
=
zmq_connect
(
client
,
"tcp://localhost:9998"
);
// Create and bind ZAP socket
zap
=
zmq_socket
(
ctx
,
ZMQ_REP
);
assert
(
zap
);
rc
=
zmq_bind
(
zap
,
"inproc://zeromq.zap.01"
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
expect_bounce_fail
(
server
,
client
);
zap_thread
=
zmq_threadstart
(
&
zap_handler
,
zap
);
close_zero_linger
(
client
);
rc
=
zmq_bind
(
server
,
"tcp://*:9997"
);
// Check CURVE security with bogus client credentials
// This must be caught by the ZAP handler
char
bogus_public
[]
=
"8)<]6{NT{}=MZBsH)i%l0k}y*^i#80n-Yf{I8Z+P"
;
char
bogus_secret
[]
=
"[m9E0TW2Mf?Ke3K>fuBGCrkBpc6aJbj4jv4451Nx"
;
client
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
assert
(
client
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_CURVE_SERVERKEY
,
server_public
,
40
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_
connect
(
client
,
"tcp://localhost:9997"
);
rc
=
zmq_
setsockopt
(
client
,
ZMQ_CURVE_PUBLICKEY
,
bogus_public
,
40
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_CURVE_SECRETKEY
,
bogus_secret
,
40
);
expect_bounce_fail
(
server
,
client
);
close_zero_linger
(
client
);
rc
=
zmq_close
(
server
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_connect
(
client
,
"tcp://localhost:9998"
);
// Wait until ZAP handler terminates.
assert
(
rc
==
0
);
zmq_threadclose
(
zap_thread
);
// TODO: does not handle ZAP failures properly
// expect_bounce_fail (server, client);
close_zero_linger
(
client
);
// Shutdown
// Shutdown
rc
=
zmq_close
(
server
);
assert
(
rc
==
0
);
rc
=
zmq_ctx_term
(
ctx
);
rc
=
zmq_ctx_term
(
ctx
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
// Wait until ZAP handler terminates
zmq_threadclose
(
zap_thread
);
return
0
;
return
0
;
}
}
tests/test_security_null.cpp
0 → 100644
View file @
3a50aa44
/*
Copyright (c) 2007-2013 Contributors as noted in the AUTHORS file
This file is part of 0MQ.
0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
0MQ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../include/zmq_utils.h"
#include <string.h>
#include <stdlib.h>
#include "testutil.hpp"
static
void
zap_handler
(
void
*
ctx
)
{
// Create and bind ZAP socket
void
*
zap
=
zmq_socket
(
ctx
,
ZMQ_REP
);
assert
(
zap
);
int
rc
=
zmq_bind
(
zap
,
"inproc://zeromq.zap.01"
);
assert
(
rc
==
0
);
// Process ZAP requests forever
while
(
true
)
{
char
*
version
=
s_recv
(
zap
);
if
(
!
version
)
break
;
// Terminating
char
*
sequence
=
s_recv
(
zap
);
char
*
domain
=
s_recv
(
zap
);
char
*
address
=
s_recv
(
zap
);
char
*
identity
=
s_recv
(
zap
);
char
*
mechanism
=
s_recv
(
zap
);
assert
(
streq
(
version
,
"1.0"
));
assert
(
streq
(
mechanism
,
"NULL"
));
// TODO: null_mechanism.cpp issues ZAP requests for connections other
// than the expected one. In these cases identity is not set, and the
// test fails. We'd expect one ZAP request per real client connection.
// assert (streq (identity, "IDENT"));
s_sendmore
(
zap
,
version
);
s_sendmore
(
zap
,
sequence
);
s_sendmore
(
zap
,
"200"
);
s_sendmore
(
zap
,
"OK"
);
s_sendmore
(
zap
,
"anonymous"
);
s_send
(
zap
,
""
);
free
(
version
);
free
(
sequence
);
free
(
domain
);
free
(
address
);
free
(
identity
);
free
(
mechanism
);
}
rc
=
zmq_close
(
zap
);
assert
(
rc
==
0
);
}
int
main
(
void
)
{
setup_test_environment
();
void
*
ctx
=
zmq_ctx_new
();
assert
(
ctx
);
// Spawn ZAP handler
void
*
zap_thread
=
zmq_threadstart
(
&
zap_handler
,
ctx
);
// Server socket will accept connections
void
*
server
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
assert
(
server
);
int
rc
=
zmq_setsockopt
(
server
,
ZMQ_IDENTITY
,
"IDENT"
,
6
);
assert
(
rc
==
0
);
rc
=
zmq_bind
(
server
,
"tcp://*:9999"
);
assert
(
rc
==
0
);
// Client socket that will try to connect to server
void
*
client
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
assert
(
client
);
rc
=
zmq_connect
(
client
,
"tcp://localhost:9999"
);
assert
(
rc
==
0
);
bounce
(
server
,
client
);
rc
=
zmq_close
(
client
);
assert
(
rc
==
0
);
rc
=
zmq_close
(
server
);
assert
(
rc
==
0
);
// Shutdown
rc
=
zmq_ctx_term
(
ctx
);
assert
(
rc
==
0
);
// Wait until ZAP handler terminates.
zmq_threadclose
(
zap_thread
);
return
0
;
}
tests/test_security.cpp
→
tests/test_security
_plain
.cpp
View file @
3a50aa44
...
@@ -22,48 +22,57 @@
...
@@ -22,48 +22,57 @@
#include <stdlib.h>
#include <stdlib.h>
#include "testutil.hpp"
#include "testutil.hpp"
static
void
zap_handler
(
void
*
zap
)
static
void
zap_handler
(
void
*
ctx
)
{
{
char
*
version
=
s_recv
(
zap
);
// Create and bind ZAP socket
char
*
sequence
=
s_recv
(
zap
);
void
*
zap
=
zmq_socket
(
ctx
,
ZMQ_REP
);
char
*
domain
=
s_recv
(
zap
);
assert
(
zap
);
char
*
address
=
s_recv
(
zap
);
int
rc
=
zmq_bind
(
zap
,
"inproc://zeromq.zap.01"
);
char
*
identity
=
s_recv
(
zap
);
assert
(
rc
==
0
);
char
*
mechanism
=
s_recv
(
zap
);
char
*
username
=
s_recv
(
zap
);
// Process ZAP requests forever
char
*
password
=
s_recv
(
zap
);
while
(
true
)
{
char
*
version
=
s_recv
(
zap
);
printf
(
"identity: %s
\n
"
,
identity
);
if
(
!
version
)
assert
(
streq
(
version
,
"1.0"
));
break
;
// Terminating
assert
(
streq
(
mechanism
,
"PLAIN"
));
char
*
sequence
=
s_recv
(
zap
);
assert
(
streq
(
identity
,
"IDENT"
));
char
*
domain
=
s_recv
(
zap
);
char
*
address
=
s_recv
(
zap
);
s_sendmore
(
zap
,
version
);
char
*
identity
=
s_recv
(
zap
);
s_sendmore
(
zap
,
sequence
);
char
*
mechanism
=
s_recv
(
zap
);
if
(
streq
(
username
,
"admin"
)
char
*
username
=
s_recv
(
zap
);
&&
streq
(
password
,
"password"
))
{
char
*
password
=
s_recv
(
zap
);
s_sendmore
(
zap
,
"200"
);
s_sendmore
(
zap
,
"OK"
);
assert
(
streq
(
version
,
"1.0"
));
s_sendmore
(
zap
,
"anonymous"
);
assert
(
streq
(
mechanism
,
"PLAIN"
));
s_send
(
zap
,
""
);
assert
(
streq
(
identity
,
"IDENT"
));
}
else
{
s_sendmore
(
zap
,
version
);
s_sendmore
(
zap
,
"400"
);
s_sendmore
(
zap
,
sequence
);
s_sendmore
(
zap
,
"Invalid username or password"
);
if
(
streq
(
username
,
"admin"
)
s_sendmore
(
zap
,
""
);
&&
streq
(
password
,
"password"
))
{
s_send
(
zap
,
""
);
s_sendmore
(
zap
,
"200"
);
s_sendmore
(
zap
,
"OK"
);
s_sendmore
(
zap
,
"anonymous"
);
s_send
(
zap
,
""
);
}
else
{
s_sendmore
(
zap
,
"400"
);
s_sendmore
(
zap
,
"Invalid username or password"
);
s_sendmore
(
zap
,
""
);
s_send
(
zap
,
""
);
}
free
(
version
);
free
(
sequence
);
free
(
domain
);
free
(
address
);
free
(
identity
);
free
(
mechanism
);
free
(
username
);
free
(
password
);
}
}
rc
=
zmq_close
(
zap
);
free
(
version
);
free
(
sequence
);
free
(
domain
);
free
(
address
);
free
(
identity
);
free
(
mechanism
);
free
(
username
);
free
(
password
);
int
rc
=
zmq_close
(
zap
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
}
}
...
@@ -72,221 +81,74 @@ int main (void)
...
@@ -72,221 +81,74 @@ int main (void)
setup_test_environment
();
setup_test_environment
();
void
*
ctx
=
zmq_ctx_new
();
void
*
ctx
=
zmq_ctx_new
();
assert
(
ctx
);
assert
(
ctx
);
// Spawn ZAP handler
void
*
zap_thread
=
zmq_threadstart
(
&
zap_handler
,
ctx
);
// Server socket will accept connections
// Server socket will accept connections
void
*
server
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
void
*
server
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
assert
(
server
);
assert
(
server
);
int
rc
=
zmq_setsockopt
(
server
,
ZMQ_IDENTITY
,
"IDENT"
,
6
);
// Client socket that will try to connect to server
void
*
client
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
assert
(
client
);
// Check NULL security configuration
int
rc
;
size_t
optsize
;
int
mechanism
;
int
as_server
;
optsize
=
sizeof
(
int
);
rc
=
zmq_getsockopt
(
client
,
ZMQ_MECHANISM
,
&
mechanism
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
assert
(
mechanism
==
ZMQ_NULL
);
int
as_server
=
1
;
rc
=
zmq_setsockopt
(
server
,
ZMQ_PLAIN_SERVER
,
&
as_server
,
sizeof
(
int
));
rc
=
zmq_getsockopt
(
server
,
ZMQ_MECHANISM
,
&
mechanism
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
mechanism
==
ZMQ_NULL
);
rc
=
zmq_getsockopt
(
client
,
ZMQ_PLAIN_SERVER
,
&
as_server
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
as_server
==
0
);
rc
=
zmq_getsockopt
(
server
,
ZMQ_PLAIN_SERVER
,
&
as_server
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
as_server
==
0
);
rc
=
zmq_bind
(
server
,
"tcp://*:9999"
);
assert
(
rc
==
0
);
rc
=
zmq_connect
(
client
,
"tcp://localhost:9999"
);
assert
(
rc
==
0
);
bounce
(
server
,
client
);
rc
=
zmq_close
(
client
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_
close
(
server
);
rc
=
zmq_
bind
(
server
,
"tcp://*:9998"
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
// Check PLAIN security
server
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
assert
(
server
);
rc
=
zmq_setsockopt
(
server
,
ZMQ_IDENTITY
,
"IDENT"
,
6
);
client
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
assert
(
client
);
char
username
[
256
];
char
username
[
256
];
optsize
=
256
;
rc
=
zmq_getsockopt
(
client
,
ZMQ_PLAIN_USERNAME
,
username
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
optsize
==
1
);
// Null string is one byte long
char
password
[
256
];
char
password
[
256
];
optsize
=
256
;
rc
=
zmq_getsockopt
(
client
,
ZMQ_PLAIN_PASSWORD
,
password
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
optsize
==
1
);
// Null string is one byte long
// Check PLAIN security with correct username/password
void
*
client
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
assert
(
client
);
strcpy
(
username
,
"admin"
);
strcpy
(
username
,
"admin"
);
strcpy
(
password
,
"password"
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_PLAIN_USERNAME
,
username
,
strlen
(
username
));
rc
=
zmq_setsockopt
(
client
,
ZMQ_PLAIN_USERNAME
,
username
,
strlen
(
username
));
assert
(
rc
==
0
);
assert
(
rc
==
0
);
strcpy
(
password
,
"password"
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_PLAIN_PASSWORD
,
password
,
strlen
(
password
));
rc
=
zmq_setsockopt
(
client
,
ZMQ_PLAIN_PASSWORD
,
password
,
strlen
(
password
));
assert
(
rc
==
0
);
assert
(
rc
==
0
);
optsize
=
256
;
rc
=
zmq_getsockopt
(
client
,
ZMQ_PLAIN_USERNAME
,
username
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
optsize
==
5
+
1
);
optsize
=
256
;
rc
=
zmq_getsockopt
(
client
,
ZMQ_PLAIN_PASSWORD
,
password
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
optsize
==
8
+
1
);
as_server
=
1
;
rc
=
zmq_setsockopt
(
server
,
ZMQ_PLAIN_SERVER
,
&
as_server
,
sizeof
(
int
));
assert
(
rc
==
0
);
optsize
=
sizeof
(
int
);
rc
=
zmq_getsockopt
(
client
,
ZMQ_MECHANISM
,
&
mechanism
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
mechanism
==
ZMQ_PLAIN
);
rc
=
zmq_getsockopt
(
server
,
ZMQ_MECHANISM
,
&
mechanism
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
mechanism
==
ZMQ_PLAIN
);
rc
=
zmq_getsockopt
(
client
,
ZMQ_PLAIN_SERVER
,
&
as_server
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
as_server
==
0
);
rc
=
zmq_getsockopt
(
server
,
ZMQ_PLAIN_SERVER
,
&
as_server
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
as_server
==
1
);
// Create and bind ZAP socket
void
*
zap
=
zmq_socket
(
ctx
,
ZMQ_REP
);
assert
(
zap
);
rc
=
zmq_bind
(
zap
,
"inproc://zeromq.zap.01"
);
assert
(
rc
==
0
);
// Spawn ZAP handler
void
*
zap_thread
=
zmq_threadstart
(
&
zap_handler
,
zap
);
rc
=
zmq_bind
(
server
,
"tcp://*:9998"
);
assert
(
rc
==
0
);
rc
=
zmq_connect
(
client
,
"tcp://localhost:9998"
);
rc
=
zmq_connect
(
client
,
"tcp://localhost:9998"
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
bounce
(
server
,
client
);
bounce
(
server
,
client
);
rc
=
zmq_close
(
client
);
rc
=
zmq_close
(
client
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_close
(
server
);
assert
(
rc
==
0
);
// Wait until ZAP handler terminates.
zmq_threadclose
(
zap_thread
);
// Check PLAIN security -- failed authentication
// Check PLAIN security -- failed authentication
server
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
assert
(
server
);
rc
=
zmq_setsockopt
(
server
,
ZMQ_IDENTITY
,
"IDENT"
,
6
);
client
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
client
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
assert
(
client
);
assert
(
client
);
strcpy
(
username
,
"wronguser"
);
strcpy
(
username
,
"wronguser"
);
strcpy
(
password
,
"wrongpass"
);
strcpy
(
password
,
"wrongpass"
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_PLAIN_USERNAME
,
username
,
strlen
(
username
));
rc
=
zmq_setsockopt
(
client
,
ZMQ_PLAIN_USERNAME
,
username
,
strlen
(
username
));
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_PLAIN_PASSWORD
,
password
,
strlen
(
password
));
rc
=
zmq_setsockopt
(
client
,
ZMQ_PLAIN_PASSWORD
,
password
,
strlen
(
password
));
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_connect
(
client
,
"tcp://localhost:9998"
);
as_server
=
1
;
rc
=
zmq_setsockopt
(
server
,
ZMQ_PLAIN_SERVER
,
&
as_server
,
sizeof
(
int
));
assert
(
rc
==
0
);
optsize
=
sizeof
(
int
);
rc
=
zmq_getsockopt
(
client
,
ZMQ_MECHANISM
,
&
mechanism
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
mechanism
==
ZMQ_PLAIN
);
rc
=
zmq_getsockopt
(
server
,
ZMQ_MECHANISM
,
&
mechanism
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
mechanism
==
ZMQ_PLAIN
);
rc
=
zmq_getsockopt
(
client
,
ZMQ_PLAIN_SERVER
,
&
as_server
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
as_server
==
0
);
rc
=
zmq_getsockopt
(
server
,
ZMQ_PLAIN_SERVER
,
&
as_server
,
&
optsize
);
assert
(
rc
==
0
);
assert
(
as_server
==
1
);
// Create and bind ZAP socket
zap
=
zmq_socket
(
ctx
,
ZMQ_REP
);
assert
(
zap
);
rc
=
zmq_bind
(
zap
,
"inproc://zeromq.zap.01"
);
assert
(
rc
==
0
);
// Spawn ZAP handler
zap_thread
=
zmq_threadstart
(
&
zap_handler
,
zap
);
rc
=
zmq_bind
(
server
,
"tcp://*:9996"
);
assert
(
rc
==
0
);
rc
=
zmq_connect
(
client
,
"tcp://localhost:9996"
);
assert
(
rc
==
0
);
// Send message from inauthenticated client to server
expect_bounce_fail
(
server
,
client
);
rc
=
zmq_close
(
client
);
assert
(
rc
==
0
);
rc
=
zmq_close
(
server
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
// TODO: this does not fail as it should
// expect_bounce_fail (server, client);
close_zero_linger
(
client
);
// Wait until ZAP handler terminates.
// Check PLAIN security with badly configured client (as_server)
zmq_threadclose
(
zap_thread
);
// Check PLAIN security -- two servers trying to talk to each other
server
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
assert
(
server
);
client
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
client
=
zmq_socket
(
ctx
,
ZMQ_DEALER
);
assert
(
client
);
assert
(
client
);
as_server
=
1
;
rc
=
zmq_setsockopt
(
server
,
ZMQ_PLAIN_SERVER
,
&
as_server
,
sizeof
(
int
));
assert
(
rc
==
0
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_PLAIN_SERVER
,
&
as_server
,
sizeof
(
int
));
rc
=
zmq_setsockopt
(
client
,
ZMQ_PLAIN_SERVER
,
&
as_server
,
sizeof
(
int
));
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_connect
(
client
,
"tcp://localhost:9998"
);
rc
=
zmq_bind
(
server
,
"tcp://*:9997"
);
assert
(
rc
==
0
);
rc
=
zmq_connect
(
client
,
"tcp://localhost:9997"
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
// TODO: this does not fail as it should
//
TODO: this test fails without any error
//
expect_bounce_fail (server, client);
// bounce (server,
client);
close_zero_linger
(
client
);
rc
=
zmq_close
(
client
);
// Shutdown
assert
(
rc
==
0
);
rc
=
zmq_close
(
server
);
rc
=
zmq_close
(
server
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
// Shutdown
rc
=
zmq_ctx_term
(
ctx
);
rc
=
zmq_ctx_term
(
ctx
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
// Wait until ZAP handler terminates
zmq_threadclose
(
zap_thread
);
return
0
;
return
0
;
}
}
tests/testutil.hpp
View file @
3a50aa44
...
@@ -99,17 +99,16 @@ expect_bounce_fail (void *server, void *client)
...
@@ -99,17 +99,16 @@ expect_bounce_fail (void *server, void *client)
assert
(
rc
==
32
);
assert
(
rc
==
32
);
// Receive message at server side (should not succeed)
// Receive message at server side (should not succeed)
int
timeout
=
2
50
;
int
timeout
=
1
50
;
rc
=
zmq_setsockopt
(
server
,
ZMQ_RCVTIMEO
,
&
timeout
,
sizeof
(
int
));
rc
=
zmq_setsockopt
(
server
,
ZMQ_RCVTIMEO
,
&
timeout
,
sizeof
(
int
));
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_setsockopt
(
client
,
ZMQ_RCVTIMEO
,
&
timeout
,
sizeof
(
int
));
rc
=
zmq_setsockopt
(
client
,
ZMQ_RCVTIMEO
,
&
timeout
,
sizeof
(
int
));
assert
(
rc
==
0
);
assert
(
rc
==
0
);
rc
=
zmq_recv
(
server
,
buffer
,
32
,
0
);
rc
=
zmq_recv
(
server
,
buffer
,
32
,
0
);
assert
(
rc
==
-
1
);
assert
(
rc
==
-
1
);
assert
(
zmq_errno
()
==
EAGAIN
);
assert
(
zmq_errno
()
==
EAGAIN
);
rc
=
zmq_send
(
server
,
content
,
32
,
ZMQ_SNDMORE
);
rc
=
zmq_send
(
server
,
content
,
32
,
ZMQ_SNDMORE
);
assert
(
rc
==
32
);
assert
(
rc
==
32
);
rc
=
zmq_send
(
server
,
content
,
32
,
0
);
rc
=
zmq_send
(
server
,
content
,
32
,
0
);
...
@@ -152,7 +151,6 @@ s_sendmore (void *socket, const char *string) {
...
@@ -152,7 +151,6 @@ s_sendmore (void *socket, const char *string) {
#define streq(s1,s2) (!strcmp ((s1), (s2)))
#define streq(s1,s2) (!strcmp ((s1), (s2)))
#define strneq(s1,s2) (strcmp ((s1), (s2)))
#define strneq(s1,s2) (strcmp ((s1), (s2)))
const
char
*
SEQ_END
=
(
const
char
*
)
1
;
const
char
*
SEQ_END
=
(
const
char
*
)
1
;
// Sends a message composed of frames that are C strings or null frames.
// Sends a message composed of frames that are C strings or null frames.
...
@@ -229,7 +227,7 @@ void close_zero_linger (void *socket)
...
@@ -229,7 +227,7 @@ void close_zero_linger (void *socket)
{
{
int
linger
=
0
;
int
linger
=
0
;
int
rc
=
zmq_setsockopt
(
socket
,
ZMQ_LINGER
,
&
linger
,
sizeof
(
linger
));
int
rc
=
zmq_setsockopt
(
socket
,
ZMQ_LINGER
,
&
linger
,
sizeof
(
linger
));
assert
(
rc
==
0
);
assert
(
rc
==
0
||
errno
==
ETERM
);
rc
=
zmq_close
(
socket
);
rc
=
zmq_close
(
socket
);
assert
(
rc
==
0
);
assert
(
rc
==
0
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment