use of org.apache.zookeeper.server.quorum.QuorumCnxManager.InitialMessage in project zookeeper by apache.
the class CnxManagerTest method testInitialMessage.
@Test
public void testInitialMessage() throws Exception {
InitialMessage msg;
ByteArrayOutputStream bos;
DataInputStream din;
DataOutputStream dout;
String hostport;
// message with bad protocol version
try {
// the initial message (without the protocol version)
hostport = "10.0.0.2:3888";
bos = new ByteArrayOutputStream();
dout = new DataOutputStream(bos);
// sid
dout.writeLong(5L);
dout.writeInt(hostport.getBytes().length);
dout.writeBytes(hostport);
// now parse it
din = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
msg = InitialMessage.parse(-65530L, din);
Assert.fail("bad protocol version accepted");
} catch (InitialMessage.InitialMessageException ex) {
}
// message too long
try {
hostport = createLongString(1048576);
bos = new ByteArrayOutputStream();
dout = new DataOutputStream(bos);
// sid
dout.writeLong(5L);
dout.writeInt(hostport.getBytes().length);
dout.writeBytes(hostport);
din = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
msg = InitialMessage.parse(QuorumCnxManager.PROTOCOL_VERSION, din);
Assert.fail("long message accepted");
} catch (InitialMessage.InitialMessageException ex) {
}
// bad hostport string
try {
hostport = "what's going on here?";
bos = new ByteArrayOutputStream();
dout = new DataOutputStream(bos);
// sid
dout.writeLong(5L);
dout.writeInt(hostport.getBytes().length);
dout.writeBytes(hostport);
din = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
msg = InitialMessage.parse(QuorumCnxManager.PROTOCOL_VERSION, din);
Assert.fail("bad hostport accepted");
} catch (InitialMessage.InitialMessageException ex) {
}
// good message
try {
hostport = "10.0.0.2:3888";
bos = new ByteArrayOutputStream();
dout = new DataOutputStream(bos);
// sid
dout.writeLong(5L);
dout.writeInt(hostport.getBytes().length);
dout.writeBytes(hostport);
// now parse it
din = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
msg = InitialMessage.parse(QuorumCnxManager.PROTOCOL_VERSION, din);
} catch (InitialMessage.InitialMessageException ex) {
Assert.fail(ex.toString());
}
}
Aggregations