Search in sources :

Example 96 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class V2EncoderTest method testReaderLong.

@Test
public void testReaderLong() {
    Msg msg = readLongMessage1();
    ValueReference<ByteBuffer> ref = new ValueReference<>();
    int outsize = encoder.encode(ref, 0);
    assertThat(outsize, is(0));
    ByteBuffer out = ref.get();
    assertThat(out, nullValue());
    encoder.loadMsg(msg);
    outsize = encoder.encode(ref, 64);
    assertThat(outsize, is(64));
    out = ref.get();
    int position = out.position();
    int limit = out.limit();
    assertThat(limit, is(64));
    assertThat(position, is(64));
    ref.set(null);
    outsize = encoder.encode(ref, 64);
    assertThat(outsize, is(138));
    out = ref.get();
    position = out.position();
    limit = out.limit();
    assertThat(position, is(62));
    assertThat(limit, is(200));
}
Also used : Msg(zmq.Msg) ByteBuffer(java.nio.ByteBuffer) ValueReference(zmq.util.ValueReference) Test(org.junit.Test)

Example 97 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class SecurityNullTest method testNullMechanismSecurity.

@Test
public void testNullMechanismSecurity() throws IOException, InterruptedException {
    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));
    thread.start();
    // We bounce between a binding server and a connecting client
    // We first test client/server with no ZAP domain
    // Libzmq does not call our ZAP handler, the connect must succeed
    System.out.println("Test NO ZAP domain");
    SocketBase server = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(server, notNullValue());
    SocketBase client = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(client, notNullValue());
    rc = ZMQ.bind(server, host);
    assertThat(rc, is(true));
    String endpoint = (String) ZMQ.getSocketOptionExt(server, ZMQ.ZMQ_LAST_ENDPOINT);
    assertThat(endpoint, notNullValue());
    rc = ZMQ.connect(client, endpoint);
    assertThat(rc, is(true));
    Helper.bounce(server, client);
    ZMQ.closeZeroLinger(server);
    ZMQ.closeZeroLinger(client);
    // Now define a ZAP domain for the server; this enables
    // authentication. We're using the wrong domain so this test
    // must fail.
    System.out.println("Test WRONG ZAP domain");
    server = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(server, notNullValue());
    client = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(client, notNullValue());
    ZMQ.setSocketOption(server, ZMQ.ZMQ_ZAP_DOMAIN, "WRONG");
    rc = ZMQ.bind(server, host);
    assertThat(rc, is(true));
    endpoint = (String) ZMQ.getSocketOptionExt(server, ZMQ.ZMQ_LAST_ENDPOINT);
    assertThat(endpoint, notNullValue());
    rc = ZMQ.connect(client, endpoint);
    assertThat(rc, is(true));
    Helper.expectBounceFail(server, client);
    ZMQ.closeZeroLinger(server);
    ZMQ.closeZeroLinger(client);
    // Now use the right domain, the test must pass
    System.out.println("Test RIGHT ZAP domain");
    server = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(server, notNullValue());
    client = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(client, notNullValue());
    ZMQ.setSocketOption(server, ZMQ.ZMQ_ZAP_DOMAIN, "TEST");
    rc = ZMQ.bind(server, host);
    assertThat(rc, is(true));
    endpoint = (String) ZMQ.getSocketOptionExt(server, ZMQ.ZMQ_LAST_ENDPOINT);
    assertThat(endpoint, notNullValue());
    rc = ZMQ.connect(client, endpoint);
    assertThat(rc, is(true));
    Helper.bounce(server, client);
    ZMQ.closeZeroLinger(server);
    ZMQ.closeZeroLinger(client);
    // Unauthenticated messages from a vanilla socket shouldn't be received
    server = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(server, notNullValue());
    ZMQ.setSocketOption(server, ZMQ.ZMQ_ZAP_DOMAIN, "WRONG");
    rc = ZMQ.bind(server, host);
    assertThat(rc, is(true));
    endpoint = (String) ZMQ.getSocketOptionExt(server, ZMQ.ZMQ_LAST_ENDPOINT);
    assertThat(endpoint, notNullValue());
    Socket sock = new Socket("127.0.0.1", TestUtils.port(endpoint));
    // 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 98 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class LocalLat method main.

public static void main(String[] args) {
    String bindTo;
    int roundtripCount;
    int messageSize;
    Ctx ctx;
    SocketBase s;
    boolean rc;
    int n;
    int i;
    Msg msg;
    if (args.length != 3) {
        printf("usage: local_lat <bind-to> <message-size> " + "<roundtrip-count>\n");
        return;
    }
    bindTo = args[0];
    messageSize = atoi(args[1]);
    roundtripCount = atoi(args[2]);
    ctx = ZMQ.init(1);
    if (ctx == null) {
        printf("error in init: %s\n");
        return;
    }
    s = ZMQ.socket(ctx, ZMQ.ZMQ_REP);
    if (s == null) {
        printf("error in socket: %s\n", ZMQ.strerror(ctx.errno().get()));
        return;
    }
    rc = ZMQ.bind(s, bindTo);
    if (!rc) {
        printf("error in bind: %s\n", ZMQ.strerror(s.errno()));
        return;
    }
    for (i = 0; i != roundtripCount; i++) {
        msg = ZMQ.recvMsg(s, 0);
        if (msg == null) {
            printf("error in recvmsg: %s\n", ZMQ.strerror(s.errno()));
            return;
        }
        if (ZMQ.msgSize(msg) != messageSize) {
            printf("message of incorrect size received\n");
            return;
        }
        n = ZMQ.sendMsg(s, msg, 0);
        if (n < 0) {
            printf("error in sendmsg: %s\n", ZMQ.strerror(s.errno()));
            return;
        }
    }
    ZMQ.sleep(1000);
    ZMQ.close(s);
    ZMQ.term(ctx);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx)

Example 99 with Msg

use of zmq.Msg 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 100 with Msg

use of zmq.Msg 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)

Aggregations

Msg (zmq.Msg)124 Test (org.junit.Test)45 SocketBase (zmq.SocketBase)37 Ctx (zmq.Ctx)32 ByteBuffer (java.nio.ByteBuffer)16 ValueReference (zmq.util.ValueReference)16 Pipe (zmq.pipe.Pipe)13 Blob (zmq.util.Blob)7 ArrayList (java.util.ArrayList)5 OutputStream (java.io.OutputStream)4 Socket (java.net.Socket)4 HashSet (java.util.HashSet)3 ExecutorService (java.util.concurrent.ExecutorService)2 Metadata (zmq.io.Metadata)2 InputStream (java.io.InputStream)1 List (java.util.List)1 Event (zmq.ZMQ.Event)1 ZObject (zmq.ZObject)1 Step (zmq.io.coder.IDecoder.Step)1 RawDecoder (zmq.io.coder.raw.RawDecoder)1