use of zmq.Ctx 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();
}
use of zmq.Ctx in project jeromq by zeromq.
the class BindSrcAddressTest method test.
@Test
public void test() throws IOException {
Ctx ctx = ZMQ.createContext();
assert (ctx != null);
SocketBase socket = ZMQ.socket(ctx, ZMQ.ZMQ_PUB);
assert (socket != null);
int port1 = Utils.findOpenPort();
int port2 = Utils.findOpenPort();
int port3 = Utils.findOpenPort();
boolean rc = ZMQ.connect(socket, "tcp://127.0.0.1:0;localhost:" + port1);
assert (rc);
rc = ZMQ.connect(socket, "tcp://localhost:" + port3 + ";localhost:" + port2);
assert (rc);
// rc = ZMQ.connect(socket, "tcp://lo:" + port3 + ";localhost:" + port2);
// assert (rc);
ZMQ.close(socket);
ZMQ.term(ctx);
}
use of zmq.Ctx 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);
}
use of zmq.Ctx in project jeromq by zeromq.
the class StreamEngineTest method testEncoderFlipIssue520.
@Test
public void testEncoderFlipIssue520() throws IOException {
Ctx ctx = ZMQ.createContext();
assertThat(ctx, notNullValue());
SocketBase sender = ZMQ.socket(ctx, ZMQ.ZMQ_PUSH);
assertThat(sender, notNullValue());
boolean rc = ZMQ.setSocketOption(sender, ZMQ.ZMQ_IMMEDIATE, false);
assertThat(rc, is(true));
SocketBase receiver = ZMQ.socket(ctx, ZMQ.ZMQ_PULL);
assertThat(receiver, notNullValue());
String addr = "tcp://localhost:*";
rc = ZMQ.bind(receiver, addr);
assertThat(rc, is(true));
addr = (String) ZMQ.getSocketOptionExt(receiver, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(addr, notNullValue());
rc = ZMQ.connect(sender, addr);
assertThat(rc, is(true));
final int headerSize = 8 + 1;
// first message + second message header fill the buffer
byte[] msg1 = msg(Config.OUT_BATCH_SIZE.getValue() - 2 * headerSize);
// second message will go in zero-copy mode
byte[] msg2 = msg(Config.OUT_BATCH_SIZE.getValue());
exchange(sender, receiver, msg1, msg2, msg1, msg2);
ZMQ.close(receiver);
ZMQ.close(sender);
ZMQ.term(ctx);
}
use of zmq.Ctx in project jeromq by zeromq.
the class CustomDecoderTest method testAssignCustomDecoder.
@SuppressWarnings("deprecation")
@Test
public void testAssignCustomDecoder() {
Ctx ctx = ZMQ.createContext();
SocketBase socket = ctx.createSocket(ZMQ.ZMQ_PAIR);
boolean rc = socket.setSocketOpt(ZMQ.ZMQ_DECODER, CustomDecoder.class);
assertThat(rc, is(true));
ZMQ.close(socket);
ZMQ.term(ctx);
}
Aggregations