Search in sources :

Example 1 with ZapRequest

use of org.zeromq.ZAuth.ZapRequest 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)

Aggregations

ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 ZapRequest (org.zeromq.ZAuth.ZapRequest)1 Socket (org.zeromq.ZMQ.Socket)1 Event (org.zeromq.ZMonitor.Event)1 ZEvent (org.zeromq.ZMonitor.ZEvent)1