Search in sources :

Example 1 with Event

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);
    }
}
Also used : ArrayList(java.util.ArrayList) ZapRequest(org.zeromq.ZAuth.ZapRequest) ZEvent(org.zeromq.ZMonitor.ZEvent) ZEvent(org.zeromq.ZMonitor.ZEvent) Event(org.zeromq.ZMonitor.Event) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 2 with Event

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);
}
Also used : ZEvent(org.zeromq.ZMonitor.ZEvent) ArrayList(java.util.ArrayList) Curve(zmq.io.mechanism.curve.Curve) ZEvent(org.zeromq.ZMonitor.ZEvent) Event(org.zeromq.ZMonitor.Event) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 3 with Event

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);
}
Also used : ArrayList(java.util.ArrayList) Curve(zmq.io.mechanism.curve.Curve) ZEvent(org.zeromq.ZMonitor.ZEvent) ZEvent(org.zeromq.ZMonitor.ZEvent) Event(org.zeromq.ZMonitor.Event) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 Socket (org.zeromq.ZMQ.Socket)3 Event (org.zeromq.ZMonitor.Event)3 ZEvent (org.zeromq.ZMonitor.ZEvent)3 Curve (zmq.io.mechanism.curve.Curve)2 ZapRequest (org.zeromq.ZAuth.ZapRequest)1