Skip to content

Commit

Permalink
Add test to negotiate for proto v2
Browse files Browse the repository at this point in the history
  • Loading branch information
thevindu-w committed Apr 12, 2024
1 parent e36a82b commit e06b160
Showing 1 changed file with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

@RunWith(AndroidJUnit4.class)
public class ProtocolSelectorTest {
static final byte PROTOCOL_REJECT = 0;
static final byte PROTOCOL_SUPPORTED = 1;
static final byte PROTOCOL_OBSOLETE = 2;
static final byte PROTOCOL_UNKNOWN = 3;
static final byte MAX_PROTO = 3;

@Test
Expand All @@ -26,7 +30,7 @@ public void testNullConnection() throws IOException {
@Test
public void testProtoOk() throws IOException {
BAOStreamBuilder builder = new BAOStreamBuilder();
builder.addByte(1);
builder.addByte(PROTOCOL_SUPPORTED);
ByteArrayInputStream istream = builder.getStream();
MockConnection connection = new MockConnection(istream);
Proto proto = ProtocolSelector.getProto(connection, null, null);
Expand All @@ -39,7 +43,7 @@ public void testProtoOk() throws IOException {
@Test
public void testProtoObsolete() {
BAOStreamBuilder builder = new BAOStreamBuilder();
builder.addByte(2);
builder.addByte(PROTOCOL_OBSOLETE);
ByteArrayInputStream istream = builder.getStream();
MockConnection connection = new MockConnection(istream);
assertThrows(ProtocolException.class, () -> ProtocolSelector.getProto(connection, null, null));
Expand All @@ -50,7 +54,7 @@ public void testProtoObsolete() {
@Test
public void testProtoNegotiateV1() throws ProtocolException {
BAOStreamBuilder builder = new BAOStreamBuilder();
builder.addByte(3);
builder.addByte(PROTOCOL_UNKNOWN);
builder.addByte(1);
ByteArrayInputStream istream = builder.getStream();
MockConnection connection = new MockConnection(istream);
Expand All @@ -60,11 +64,24 @@ public void testProtoNegotiateV1() throws ProtocolException {
proto.close();
}

@Test
public void testProtoNegotiateV2() throws ProtocolException {
BAOStreamBuilder builder = new BAOStreamBuilder();
builder.addByte(PROTOCOL_UNKNOWN);
builder.addByte(2);
ByteArrayInputStream istream = builder.getStream();
MockConnection connection = new MockConnection(istream);
Proto proto = ProtocolSelector.getProto(connection, null, null);
byte[] received = connection.getOutputBytes();
assertArrayEquals(new byte[] {MAX_PROTO, 2}, received);
proto.close();
}

@Test
public void testProtoNegotiateFail() {
BAOStreamBuilder builder = new BAOStreamBuilder();
builder.addByte(3);
builder.addByte(0);
builder.addByte(PROTOCOL_UNKNOWN);
builder.addByte(PROTOCOL_REJECT);
ByteArrayInputStream istream = builder.getStream();
MockConnection connection = new MockConnection(istream);
assertThrows(ProtocolException.class, () -> ProtocolSelector.getProto(connection, null, null));
Expand All @@ -75,7 +92,7 @@ public void testProtoNegotiateFail() {
@Test
public void testInvalidStatus() throws ProtocolException {
BAOStreamBuilder builder = new BAOStreamBuilder();
builder.addByte(4);
builder.addByte(4); // 4 is invalid
ByteArrayInputStream istream = builder.getStream();
MockConnection connection = new MockConnection(istream);
Proto proto = ProtocolSelector.getProto(connection, null, null);
Expand All @@ -94,7 +111,7 @@ public void testReceiveFail1() throws ProtocolException {
@Test
public void testReceiveFail2() throws ProtocolException {
BAOStreamBuilder builder = new BAOStreamBuilder();
builder.addByte(3);
builder.addByte(PROTOCOL_UNKNOWN);
ByteArrayInputStream istream = builder.getStream();
MockConnection connection = new MockConnection(istream);
Proto proto = ProtocolSelector.getProto(connection, null, null);
Expand Down

0 comments on commit e06b160

Please sign in to comment.