use of org.zeromq.ZMonitor.Event in project jeromq by zeromq.
the class ZMonitorTest method testPlainKo.
@Test(timeout = 5000)
public void testPlainKo() throws IOException, InterruptedException {
final List<ZEvent> receivedEventsClient = new ArrayList<>();
final List<ZEvent> receivedEventsServer = new ArrayList<>();
ZAuth.Auth plain = new ZAuth.Auth() {
@Override
public boolean authorize(ZapRequest request, boolean verbose) {
return false;
}
@Override
public boolean configure(ZMsg msg, boolean verbose) {
return true;
}
};
try (ZContext ctx = new ZContext();
ZAuth auth = new ZAuth(ctx, "authenticator", Collections.singletonMap("PLAIN", plain));
Socket server = ctx.createSocket(SocketType.PUSH);
ZMonitor serverMonitor = new ZMonitor(ctx, server);
Socket client = ctx.createSocket(SocketType.PULL);
ZMonitor clientMonitor = new ZMonitor(ctx, client)) {
clientMonitor.verbose(true);
clientMonitor.add(Event.ALL);
clientMonitor.start();
serverMonitor.verbose(true);
serverMonitor.add(Event.ALL);
serverMonitor.start();
server.setPlainServer(true);
// Start an authentication engine for this context. This engine
// allows or denies incoming connections (talking to the libzmq
// core over a protocol called ZAP).
// Get some indication of what the authenticator is deciding
auth.setVerbose(true);
// auth send the replies
auth.replies(true);
// Create and bind server socket
server.setPlainServer(true);
server.setZapDomain("global".getBytes());
final int port = server.bindToRandomPort("tcp://*");
// Create and connect client socket
client.setPlainUsername("admin".getBytes());
client.setPlainPassword("wrong".getBytes());
boolean rc = client.connect("tcp://127.0.0.1:" + port);
Assert.assertTrue(rc);
// Send a single message from server to client
rc = server.send("Hello");
Assert.assertTrue(rc);
ZAuth.ZapReply reply = auth.nextReply();
Assert.assertEquals(400, reply.statusCode);
ZEvent received;
while ((received = clientMonitor.nextEvent(100)) != null) {
receivedEventsClient.add(received);
}
while ((received = serverMonitor.nextEvent(100)) != null) {
receivedEventsServer.add(received);
}
final Event[] expectedEventsClient = new Event[] { Event.CONNECT_DELAYED, Event.CONNECTED, Event.HANDSHAKE_PROTOCOL, Event.HANDSHAKE_FAILED_AUTH, Event.DISCONNECTED };
check(receivedEventsClient, expectedEventsClient);
final Event[] expectedEventsServer = new Event[] { Event.LISTENING, Event.ACCEPTED, Event.HANDSHAKE_PROTOCOL, Event.DISCONNECTED };
check(receivedEventsServer, expectedEventsServer);
}
}
use of org.zeromq.ZMonitor.Event in project jeromq by zeromq.
the class ZMonitorTest method testZMonitorCurveKo.
@Test(timeout = 5000)
public void testZMonitorCurveKo() throws IOException, InterruptedException {
final List<ZEvent> receivedEventsClient = new ArrayList<>();
final List<ZEvent> receivedEventsServer = new ArrayList<>();
final byte[][] serverKeyPair = new Curve().keypair();
final byte[] serverPublicKey = serverKeyPair[0];
final byte[] serverSecretKey = serverKeyPair[1];
final byte[][] clientKeyPair = new Curve().keypair();
final byte[] clientPublicKey = clientKeyPair[0];
final byte[] clientSecretKey = clientKeyPair[1];
final ZContext ctx = new ZContext();
final Socket client = ctx.createSocket(SocketType.PUSH);
client.setCurveServerKey(serverPublicKey);
client.setCurvePublicKey(clientPublicKey);
client.setCurveSecretKey(serverSecretKey);
final Socket server = ctx.createSocket(SocketType.PULL);
server.setCurveServer(true);
server.setCurveSecretKey(clientSecretKey);
final ZMonitor clientMonitor = new ZMonitor(ctx, client);
clientMonitor.verbose(true);
clientMonitor.add(Event.ALL);
clientMonitor.start();
final ZMonitor serverMonitor = new ZMonitor(ctx, server);
serverMonitor.verbose(true);
serverMonitor.add(Event.ALL);
serverMonitor.start();
// Check server is now listening
server.bind("tcp://127.0.0.1:34782");
// Check server connected to client
boolean rc = client.connect("tcp://127.0.0.1:" + 34782);
Assert.assertTrue(rc);
Thread.sleep(100);
client.send("hello");
Thread.sleep(100);
client.close();
ZEvent received;
while ((received = clientMonitor.nextEvent(100)) != null) {
receivedEventsClient.add(received);
}
clientMonitor.close();
server.close();
while ((received = serverMonitor.nextEvent(100)) != null) {
receivedEventsServer.add(received);
}
serverMonitor.close();
ctx.close();
final Event[] expectedEventsClient = new Event[] { Event.CONNECT_DELAYED, Event.CONNECTED, Event.HANDSHAKE_PROTOCOL, Event.DISCONNECTED, Event.MONITOR_STOPPED };
check(receivedEventsClient, expectedEventsClient);
final Event[] expectedEventsServer = new Event[] { Event.LISTENING, Event.ACCEPTED, Event.HANDSHAKE_PROTOCOL, Event.HANDSHAKE_FAILED_PROTOCOL, Event.DISCONNECTED, Event.CLOSED, Event.MONITOR_STOPPED };
check(receivedEventsServer, expectedEventsServer);
}
use of org.zeromq.ZMonitor.Event in project jeromq by zeromq.
the class ZMonitorTest method testZMonitorCurveOK.
@Test(timeout = 5000)
public void testZMonitorCurveOK() throws IOException {
final List<ZEvent> receivedEventsClient = new ArrayList<>();
final List<ZEvent> receivedEventsServer = new ArrayList<>();
final byte[][] serverKeyPair = new Curve().keypair();
final byte[] serverPublicKey = serverKeyPair[0];
final byte[] serverSecretKey = serverKeyPair[1];
final byte[][] clientKeyPair = new Curve().keypair();
final byte[] clientPublicKey = clientKeyPair[0];
final byte[] clientSecretKey = clientKeyPair[1];
final ZContext ctx = new ZContext();
final Socket client = ctx.createSocket(SocketType.PUSH);
client.setCurveServerKey(serverPublicKey);
client.setCurvePublicKey(clientPublicKey);
client.setCurveSecretKey(clientSecretKey);
final Socket server = ctx.createSocket(SocketType.PULL);
server.setCurveServer(true);
server.setCurveSecretKey(serverSecretKey);
final ZMonitor clientMonitor = new ZMonitor(ctx, client);
clientMonitor.verbose(true);
clientMonitor.add(Event.ALL);
clientMonitor.start();
final ZMonitor serverMonitor = new ZMonitor(ctx, server);
serverMonitor.verbose(true);
serverMonitor.add(Event.ALL);
serverMonitor.start();
// Check server is now listening
int port = server.bindToRandomPort("tcp://127.0.0.1");
// Check server connected to client
boolean rc = client.connect("tcp://127.0.0.1:" + port);
client.send("hello");
server.recvStr();
Assert.assertTrue(rc);
client.close();
ZEvent received;
while ((received = clientMonitor.nextEvent(100)) != null) {
receivedEventsClient.add(received);
}
clientMonitor.close();
server.close();
while ((received = serverMonitor.nextEvent(100)) != null) {
receivedEventsServer.add(received);
}
serverMonitor.close();
ctx.close();
// [ZEvent [_PROTOCOL, code=32768, address=tcp://127.0.0.1:53682, value=3], ZEvent [type=DISCONNECTED, code=512, address=tcp://127.0.0.1:53682, value=null]]
final Event[] expectedEventsClient = new Event[] { Event.CONNECT_DELAYED, Event.CONNECTED, Event.HANDSHAKE_PROTOCOL, Event.MONITOR_STOPPED };
check(receivedEventsClient, expectedEventsClient);
final Event[] expectedEventsServer = new Event[] { Event.LISTENING, Event.ACCEPTED, Event.HANDSHAKE_PROTOCOL, Event.DISCONNECTED, Event.CLOSED, Event.MONITOR_STOPPED };
check(receivedEventsServer, expectedEventsServer);
}