Search in sources :

Example 71 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class CustomDecoderTest method testAssignWrongCustomDecoder.

@SuppressWarnings("deprecation")
@Test(expected = ZError.InstantiationException.class)
public void testAssignWrongCustomDecoder() {
    Ctx ctx = ZMQ.createContext();
    SocketBase socket = ctx.createSocket(ZMQ.ZMQ_PAIR);
    try {
        socket.setSocketOpt(ZMQ.ZMQ_DECODER, WrongDecoder.class);
    } finally {
        ZMQ.close(socket);
        ZMQ.term(ctx);
    }
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 72 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class SecurityCurveTest method testCurveMechanismSecurity.

@Test
public void testCurveMechanismSecurity() throws IOException, InterruptedException {
    Curve cryptoBox = new Curve();
    // Generate new keypairs for this test
    // We'll generate random test keys at startup
    String[] clientKeys = cryptoBox.keypairZ85();
    String clientPublic = clientKeys[0];
    String clientSecret = clientKeys[1];
    String[] serverKeys = cryptoBox.keypairZ85();
    String serverPublic = serverKeys[0];
    String serverSecret = serverKeys[1];
    String host = "tcp://127.0.0.1:*";
    Ctx ctx = ZMQ.createContext();
    // Spawn ZAP handler
    // We create and bind ZAP socket in main thread to avoid case
    // where child thread does not start up fast enough.
    SocketBase handler = ZMQ.socket(ctx, ZMQ.ZMQ_REP);
    assertThat(handler, notNullValue());
    boolean rc = ZMQ.bind(handler, "inproc://zeromq.zap.01");
    assertThat(rc, is(true));
    Thread thread = new Thread(new ZapHandler(handler, clientPublic));
    thread.start();
    // Server socket will accept connections
    SocketBase server = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(server, notNullValue());
    ZMQ.setSocketOption(server, ZMQ.ZMQ_CURVE_SERVER, true);
    ZMQ.setSocketOption(server, ZMQ.ZMQ_CURVE_SECRETKEY, serverSecret);
    ZMQ.setSocketOption(server, ZMQ.ZMQ_IDENTITY, "IDENT");
    rc = ZMQ.bind(server, host);
    assertThat(rc, is(true));
    host = (String) ZMQ.getSocketOptionExt(server, ZMQ.ZMQ_LAST_ENDPOINT);
    int port = TestUtils.port(host);
    // Check CURVE security with valid credentials
    System.out.println("Test Correct CURVE security");
    SocketBase client = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(client, notNullValue());
    ZMQ.setSocketOption(client, ZMQ.ZMQ_CURVE_SERVERKEY, serverPublic);
    ZMQ.setSocketOption(client, ZMQ.ZMQ_CURVE_PUBLICKEY, clientPublic);
    ZMQ.setSocketOption(client, ZMQ.ZMQ_CURVE_SECRETKEY, clientSecret);
    rc = ZMQ.connect(client, host);
    assertThat(rc, is(true));
    Helper.bounce(server, client);
    ZMQ.close(client);
    // Check CURVE security with a garbage server key
    // This will be caught by the curve_server class, not passed to ZAP
    System.out.println("Test bad server key CURVE security");
    String garbageKey = "0000000000000000000000000000000000000000";
    client = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(client, notNullValue());
    ZMQ.setSocketOption(client, ZMQ.ZMQ_CURVE_SERVERKEY, garbageKey);
    ZMQ.setSocketOption(client, ZMQ.ZMQ_CURVE_PUBLICKEY, clientPublic);
    ZMQ.setSocketOption(client, ZMQ.ZMQ_CURVE_SECRETKEY, clientSecret);
    rc = ZMQ.connect(client, host);
    assertThat(rc, is(true));
    Helper.expectBounceFail(server, client);
    ZMQ.closeZeroLinger(client);
    // Check CURVE security with a garbage client public key
    // This will be caught by the curve_server class, not passed to ZAP
    System.out.println("Test bad client public key CURVE security");
    client = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(client, notNullValue());
    ZMQ.setSocketOption(client, ZMQ.ZMQ_CURVE_SERVERKEY, serverPublic);
    ZMQ.setSocketOption(client, ZMQ.ZMQ_CURVE_PUBLICKEY, garbageKey);
    ZMQ.setSocketOption(client, ZMQ.ZMQ_CURVE_SECRETKEY, clientSecret);
    rc = ZMQ.connect(client, host);
    assertThat(rc, is(true));
    Helper.expectBounceFail(server, client);
    ZMQ.closeZeroLinger(client);
    // Check CURVE security with a garbage client secret key
    // This will be caught by the curve_server class, not passed to ZAP
    System.out.println("Test bad client public key CURVE security");
    client = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(client, notNullValue());
    ZMQ.setSocketOption(client, ZMQ.ZMQ_CURVE_SERVERKEY, serverPublic);
    ZMQ.setSocketOption(client, ZMQ.ZMQ_CURVE_PUBLICKEY, clientPublic);
    ZMQ.setSocketOption(client, ZMQ.ZMQ_CURVE_SECRETKEY, garbageKey);
    rc = ZMQ.connect(client, host);
    assertThat(rc, is(true));
    Helper.expectBounceFail(server, client);
    ZMQ.closeZeroLinger(client);
    // Check CURVE security with bogus client credentials
    // This must be caught by the ZAP handler
    String[] bogus = cryptoBox.keypairZ85();
    String bogusPublic = bogus[0];
    String bogusSecret = bogus[1];
    System.out.println("Test bad client credentials CURVE security");
    client = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(client, notNullValue());
    ZMQ.setSocketOption(client, ZMQ.ZMQ_CURVE_SERVERKEY, serverPublic);
    ZMQ.setSocketOption(client, ZMQ.ZMQ_CURVE_PUBLICKEY, bogusPublic);
    ZMQ.setSocketOption(client, ZMQ.ZMQ_CURVE_SECRETKEY, bogusSecret);
    rc = ZMQ.connect(client, host);
    assertThat(rc, is(true));
    Helper.expectBounceFail(server, client);
    ZMQ.closeZeroLinger(client);
    // Check CURVE security with NULL client credentials
    // This must be caught by the curve_server class, not passed to ZAP
    System.out.println("Test NULL client with CURVE security");
    client = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(client, notNullValue());
    rc = ZMQ.connect(client, host);
    assertThat(rc, is(true));
    Helper.expectBounceFail(server, client);
    ZMQ.closeZeroLinger(client);
    // Check CURVE security with PLAIN client credentials
    // This must be caught by the curve_server class, not passed to ZAP
    System.out.println("Test PLAIN client with CURVE security");
    client = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(client, notNullValue());
    ZMQ.setSocketOption(client, ZMQ.ZMQ_PLAIN_USERNAME, "user");
    ZMQ.setSocketOption(client, ZMQ.ZMQ_PLAIN_PASSWORD, "pass");
    rc = ZMQ.connect(client, host);
    assertThat(rc, is(true));
    Helper.expectBounceFail(server, client);
    ZMQ.closeZeroLinger(client);
    // Unauthenticated messages from a vanilla socket shouldn't be received
    System.out.println("Test unauthenticated from vanilla socket CURVE security");
    Socket sock = new Socket("127.0.0.1", port);
    // send anonymous ZMTP/1.0 greeting
    OutputStream out = sock.getOutputStream();
    out.write(new StringBuilder().append(0x01).append(0x00).toString().getBytes(ZMQ.CHARSET));
    // send sneaky message that shouldn't be received
    out.write(new StringBuilder().append(0x08).append(0x00).append("sneaky").append(0x00).toString().getBytes(ZMQ.CHARSET));
    int timeout = 250;
    ZMQ.setSocketOption(server, ZMQ.ZMQ_RCVTIMEO, timeout);
    Msg msg = ZMQ.recv(server, 0);
    assertThat(msg, nullValue());
    sock.close();
    // Check return codes for invalid buffer sizes
    // TODO
    System.out.println("Test return codes for invalid buffer sizes with CURVE security");
    client = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(client, notNullValue());
    rc = ZMQ.setSocketOption(client, ZMQ.ZMQ_CURVE_SERVERKEY, new byte[123]);
    assertThat(rc, is(false));
    assertThat(client.errno.get(), is(ZError.EINVAL));
    rc = ZMQ.setSocketOption(client, ZMQ.ZMQ_CURVE_PUBLICKEY, new byte[123]);
    assertThat(rc, is(false));
    assertThat(client.errno.get(), is(ZError.EINVAL));
    rc = ZMQ.setSocketOption(client, ZMQ.ZMQ_CURVE_SECRETKEY, new byte[123]);
    assertThat(rc, is(false));
    assertThat(client.errno.get(), is(ZError.EINVAL));
    ZMQ.closeZeroLinger(client);
    ZMQ.closeZeroLinger(server);
    // Shutdown
    ZMQ.term(ctx);
    // Wait until ZAP handler terminates
    thread.join();
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) OutputStream(java.io.OutputStream) Curve(zmq.io.mechanism.curve.Curve) Socket(java.net.Socket) Test(org.junit.Test)

Example 73 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class SecurityPlainTest method testPlainMechanismSecurity.

@Test
public void testPlainMechanismSecurity() throws IOException, InterruptedException {
    int port = Utils.findOpenPort();
    String host = "tcp://127.0.0.1:" + port;
    Ctx ctx = ZMQ.createContext();
    // Spawn ZAP handler
    // We create and bind ZAP socket in main thread to avoid case
    // where child thread does not start up fast enough.
    SocketBase handler = ZMQ.socket(ctx, ZMQ.ZMQ_REP);
    assertThat(handler, notNullValue());
    boolean rc = ZMQ.bind(handler, "inproc://zeromq.zap.01");
    assertThat(rc, is(true));
    Thread thread = new Thread(new ZapHandler(handler));
    thread.start();
    // Server socket will accept connections
    SocketBase server = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(server, notNullValue());
    ZMQ.setSocketOption(server, ZMQ.ZMQ_IDENTITY, "IDENT");
    ZMQ.setSocketOption(server, ZMQ.ZMQ_PLAIN_SERVER, true);
    rc = ZMQ.bind(server, host);
    assertThat(rc, is(true));
    String username = "admin";
    String password = "password";
    // Check PLAIN security with correct username/password
    System.out.println("Test Correct PLAIN security");
    SocketBase client = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(client, notNullValue());
    ZMQ.setSocketOption(client, ZMQ.ZMQ_PLAIN_USERNAME, username);
    ZMQ.setSocketOption(client, ZMQ.ZMQ_PLAIN_PASSWORD, password);
    rc = ZMQ.connect(client, host);
    assertThat(rc, is(true));
    Helper.bounce(server, client);
    ZMQ.close(client);
    // Check PLAIN security with badly configured client (as_server)
    // This will be caught by the plain_server class, not passed to ZAP
    System.out.println("Test badly configured PLAIN security");
    client = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(client, notNullValue());
    ZMQ.setSocketOption(client, ZMQ.ZMQ_PLAIN_SERVER, true);
    rc = ZMQ.connect(client, host);
    assertThat(rc, is(true));
    Helper.expectBounceFail(server, client);
    ZMQ.closeZeroLinger(client);
    // Check PLAIN security -- failed authentication
    System.out.println("Test wrong authentication PLAIN security");
    client = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(client, notNullValue());
    ZMQ.setSocketOption(client, ZMQ.ZMQ_PLAIN_USERNAME, "wronguser");
    ZMQ.setSocketOption(client, ZMQ.ZMQ_PLAIN_PASSWORD, "wrongpass");
    rc = ZMQ.connect(client, host);
    assertThat(rc, is(true));
    Helper.expectBounceFail(server, client);
    ZMQ.closeZeroLinger(client);
    // Unauthenticated messages from a vanilla socket shouldn't be received
    System.out.println("Test unauthenticated from vanilla socket PLAIN security");
    Socket sock = new Socket("127.0.0.1", port);
    // send anonymous ZMTP/1.0 greeting
    OutputStream out = sock.getOutputStream();
    out.write(new StringBuilder().append(0x01).append(0x00).toString().getBytes(ZMQ.CHARSET));
    // send sneaky message that shouldn't be received
    out.write(new StringBuilder().append(0x08).append(0x00).append("sneaky").append(0x00).toString().getBytes(ZMQ.CHARSET));
    int timeout = 250;
    ZMQ.setSocketOption(server, ZMQ.ZMQ_RCVTIMEO, timeout);
    Msg msg = ZMQ.recv(server, 0);
    assertThat(msg, nullValue());
    sock.close();
    ZMQ.closeZeroLinger(server);
    // Shutdown
    ZMQ.term(ctx);
    // Wait until ZAP handler terminates
    thread.join();
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) OutputStream(java.io.OutputStream) Socket(java.net.Socket) Test(org.junit.Test)

Example 74 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class TimerEventTest method testHandshakeTimeout.

@Test
public void testHandshakeTimeout() throws IOException, InterruptedException {
    int handshakeInterval = 10;
    Ctx ctx = ZMQ.createContext();
    assertThat(ctx, notNullValue());
    SocketBase socket = ctx.createSocket(ZMQ.ZMQ_REP);
    assertThat(socket, notNullValue());
    boolean rc = ZMQ.setSocketOption(socket, ZMQ.ZMQ_HANDSHAKE_IVL, handshakeInterval);
    assertThat(rc, is(true));
    rc = ZMQ.monitorSocket(socket, "inproc://monitor", ZMQ.ZMQ_EVENT_DISCONNECTED);
    assertThat(rc, is(true));
    SocketMonitor monitor = new SocketMonitor(ctx, "inproc://monitor");
    monitor.start();
    rc = ZMQ.bind(socket, "tcp://127.0.0.1:*");
    assertThat(rc, is(true));
    String endpoint = (String) ZMQ.getSocketOptionExt(socket, ZMQ.ZMQ_LAST_ENDPOINT);
    assertThat(endpoint, notNullValue());
    Socket sender = new Socket("127.0.0.1", TestUtils.port(endpoint));
    OutputStream out = sender.getOutputStream();
    out.write(incompleteHandshake());
    out.flush();
    monitor.join();
    // there shall be a disconnected event because of the handshake timeout
    final ZMQ.Event event = monitor.event.get();
    assertThat(event, notNullValue());
    assertThat(event.event, is(ZMQ.ZMQ_EVENT_DISCONNECTED));
    out.close();
    sender.close();
    ZMQ.close(socket);
    ZMQ.term(ctx);
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) OutputStream(java.io.OutputStream) Socket(java.net.Socket) ZMQ(zmq.ZMQ) Test(org.junit.Test)

Example 75 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class TestPairInproc method testPairInproc.

// Create REQ/ROUTER wiring.
@Test
public void testPairInproc() {
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase sb = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
    assertThat(sb, notNullValue());
    boolean brc = ZMQ.bind(sb, "inproc://a");
    assertThat(brc, is(true));
    SocketBase sc = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
    assertThat(sc, notNullValue());
    brc = ZMQ.connect(sc, "inproc://a");
    assertThat(brc, is(true));
    Helper.bounce(sb, sc);
    // Tear down the wiring.
    ZMQ.close(sb);
    ZMQ.close(sc);
    ZMQ.term(ctx);
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Aggregations

Ctx (zmq.Ctx)81 Test (org.junit.Test)73 SocketBase (zmq.SocketBase)63 Msg (zmq.Msg)31 AbstractSpecTest (zmq.socket.AbstractSpecTest)16 OutputStream (java.io.OutputStream)5 Socket (java.net.Socket)5 Ignore (org.junit.Ignore)4 ExecutorService (java.util.concurrent.ExecutorService)3 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 Curve (zmq.io.mechanism.curve.Curve)2 InputStream (java.io.InputStream)1 UncaughtExceptionHandler (java.lang.Thread.UncaughtExceptionHandler)1 ClosedSelectorException (java.nio.channels.ClosedSelectorException)1 Selector (java.nio.channels.Selector)1 UUID (java.util.UUID)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1