Ejemplo n.º 1
0
  @Test
  public void exceptionInProtocolClosesClientSession() throws IOException {
    i = 0;
    final Protocol protocol =
        new Protocol() {
          public void init(ClientSession session) {}

          public void parseFirst(InputStream message) throws IOException {}

          public void parsePacket(InputStream message) throws IOException {
            i++;
            throw new IOException("Test");
          }
        };

    context.checking(
        new Expectations() {
          {
            oneOf(mockedClient).close();
          }
        });

    prot.setCurrentProtocol(protocol);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CData.writeU16(out, 2);
    CData.writeU16(out, 1337);
    prot.messageReceived(mockedClient, new ByteArrayInputStream(out.toByteArray()));

    assertEquals(1, i);

    context.assertIsSatisfied();
  }
Ejemplo n.º 2
0
  @Test
  public void receivePacketsSkipsUnusedData() throws IOException {
    i = 0;
    final Protocol protocol =
        new Protocol() {
          public void init(ClientSession session) {}

          public void parseFirst(InputStream message) throws IOException {}

          public void parsePacket(InputStream message) throws IOException {
            assertEquals(2, message.available());
            if (i == 1) {
              assertEquals(1338, CData.readU16(message));
            }
            i++;
          }
        };
    prot.setCurrentProtocol(protocol);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CData.writeU16(out, 2);
    CData.writeU16(out, 1337);
    CData.writeU16(out, 2);
    CData.writeU16(out, 1338);
    prot.messageReceived(mockedClient, new ByteArrayInputStream(out.toByteArray()));

    assertEquals(2, i);
  }
Ejemplo n.º 3
0
 @Test
 public void parseValidHeader() throws IOException {
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   CData.writeU16(out, 2);
   CData.writeU16(out, 1337);
   InputStream in = prot.parseHeader(new ByteArrayInputStream(out.toByteArray()));
   assertEquals(2, in.available());
   assertEquals(1337, CData.readU16(in));
   assertTrue(in instanceof MessageInputStream);
 }
Ejemplo n.º 4
0
 @Test
 public void parseInvalidHeader() throws IOException {
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   CData.writeU16(out, 2);
   InputStream in = prot.parseHeader(new ByteArrayInputStream(out.toByteArray()));
   assertNull(in);
 }
Ejemplo n.º 5
0
  @Test
  public void disconnectsClientUponUnknownProtocol() throws IOException {
    context.checking(
        new Expectations() {
          {
            oneOf(mockedClient).close();
          }
        });

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CData.writeU16(out, 1);
    CData.writeByte(out, 123);
    prot.messageReceived(mockedClient, new ByteArrayInputStream(out.toByteArray()));

    context.assertIsSatisfied();
  }
Ejemplo n.º 6
0
  @Test
  public void changesToCorrectProtocol() throws IOException {
    final Protocol protocol = context.mock(Protocol.class);
    final ProtocolProvider provider = context.mock(ProtocolProvider.class);
    context.checking(
        new Expectations() {
          {
            allowing(provider).getProtocol(123);
            will(returnValue(protocol));
            oneOf(protocol).parseFirst(with(any(InputStream.class)));
            oneOf(protocol).init(mockedClient);
          }
        });
    prot.addProtocolProvider(provider);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CData.writeU16(out, 1);
    CData.writeByte(out, 123);
    prot.messageReceived(mockedClient, new ByteArrayInputStream(out.toByteArray()));

    assertSame(protocol, prot.getCurrentProtocol());
    context.assertIsSatisfied();
  }