Search in sources :

Example 1 with InitialMessage

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());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) InitialMessage(org.apache.zookeeper.server.quorum.QuorumCnxManager.InitialMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 InitialMessage (org.apache.zookeeper.server.quorum.QuorumCnxManager.InitialMessage)1 Test (org.junit.Test)1