Skip to content

Commit

Permalink
Temporarily set MAX_PROTO=2
Browse files Browse the repository at this point in the history
  • Loading branch information
thevindu-w committed Apr 13, 2024
1 parent e06b160 commit 9e818d9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.tw.clipshare.netConnection.MockConnection;
import com.tw.clipshare.protocol.Proto;
import com.tw.clipshare.protocol.Proto_v3;
import com.tw.clipshare.protocol.*;
import com.tw.clipshare.protocol.ProtocolSelector;
import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand All @@ -19,22 +18,46 @@ public class ProtocolSelectorTest {
static final byte PROTOCOL_SUPPORTED = 1;
static final byte PROTOCOL_OBSOLETE = 2;
static final byte PROTOCOL_UNKNOWN = 3;
static final byte MAX_PROTO = 3;
static final byte MAX_PROTO = ProtocolSelector.PROTO_MAX;

@SuppressWarnings("ConstantConditions")
@Test
public void testNullConnection() throws IOException {
Proto proto = ProtocolSelector.getProto(null, null, null);
assertNull(proto);
}

@SuppressWarnings("ConstantConditions")
@Test
public void testProtoOk() throws IOException {
BAOStreamBuilder builder = new BAOStreamBuilder();
builder.addByte(PROTOCOL_SUPPORTED);
ByteArrayInputStream istream = builder.getStream();
MockConnection connection = new MockConnection(istream);
Proto proto = ProtocolSelector.getProto(connection, null, null);
assertTrue(proto instanceof Proto_v3);
Class<?> protoClass;
switch (MAX_PROTO) {
case 1:
{
protoClass = Proto_v1.class;
break;
}
case 2:
{
protoClass = Proto_v2.class;
break;
}
case 3:
{
protoClass = Proto_v3.class;
break;
}
default:
{
throw new ProtocolException("Unknown protocol version");
}
}
assertTrue(protoClass.isInstance(proto));
byte[] received = connection.getOutputBytes();
assertArrayEquals(new byte[] {MAX_PROTO}, received);
proto.close();
Expand All @@ -51,8 +74,10 @@ public void testProtoObsolete() {
assertArrayEquals(new byte[] {MAX_PROTO}, received);
}

@SuppressWarnings("ConstantConditions")
@Test
public void testProtoNegotiateV1() throws ProtocolException {
if (MAX_PROTO <= 1) return;
BAOStreamBuilder builder = new BAOStreamBuilder();
builder.addByte(PROTOCOL_UNKNOWN);
builder.addByte(1);
Expand All @@ -64,8 +89,10 @@ public void testProtoNegotiateV1() throws ProtocolException {
proto.close();
}

@SuppressWarnings("ConstantConditions")
@Test
public void testProtoNegotiateV2() throws ProtocolException {
if (MAX_PROTO <= 2) return;
BAOStreamBuilder builder = new BAOStreamBuilder();
builder.addByte(PROTOCOL_UNKNOWN);
builder.addByte(2);
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/com/tw/clipshare/ClipShareActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,6 @@ protected void onCreate(Bundle savedInstanceState) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
Expand Down
25 changes: 13 additions & 12 deletions app/src/main/java/com/tw/clipshare/protocol/ProtocolSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ProtocolSelector {
static final byte PROTOCOL_OBSOLETE = 2;
static final byte PROTOCOL_UNKNOWN = 3;
private static final byte PROTO_MIN = 1;
private static final byte PROTO_MAX = 3;
public static final byte PROTO_MAX = 2;

private ProtocolSelector() {}

Expand All @@ -53,6 +53,7 @@ public static Proto getProto(
if (connection.receive(proto_v)) {
return null;
}
int selectedProto = PROTO_MAX;
if (proto_v[0] == ProtocolSelector.PROTOCOL_OBSOLETE) {
throw new ProtocolException("Obsolete client");
} else if (proto_v[0] == ProtocolSelector.PROTOCOL_UNKNOWN) {
Expand All @@ -69,20 +70,20 @@ public static Proto getProto(
if (!acceptProto(connection, serverMaxProto)) {
return null;
}
switch (serverMaxProto) {
case 1:
return new Proto_v1(connection, utils, notifier);
case 2:
return new Proto_v2(connection, utils, notifier);
default:
{
throw new ProtocolException("Unknown protocol");
}
}
selectedProto = serverMaxProto;
} else if (proto_v[0] != ProtocolSelector.PROTOCOL_SUPPORTED) {
return null;
}
return new Proto_v3(connection, utils, notifier);
switch (selectedProto) {
case 1:
return new Proto_v1(connection, utils, notifier);
case 2:
return new Proto_v2(connection, utils, notifier);
case 3:
return new Proto_v3(connection, utils, notifier);
default:
throw new ProtocolException("Unknown protocol");
}
}

private static boolean acceptProto(@NotNull ServerConnection connection, byte proto) {
Expand Down

0 comments on commit 9e818d9

Please sign in to comment.