Skip to content

Commit

Permalink
Merge branch 'master' of github.com:assaabloy-ppi/salt-channel
Browse files Browse the repository at this point in the history
  • Loading branch information
franslundberg committed Oct 24, 2017
2 parents 5762751 + b2ff5f7 commit 3cb440b
Showing 1 changed file with 125 additions and 15 deletions.
140 changes: 125 additions & 15 deletions src/saltchannel/dev/TcpEchoTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.io.IOException;
import java.net.Socket;
import java.util.Arrays;

/**
* Tests a salt-channel v2 host that runs the simplest case of the echo protocol.
*
Expand Down Expand Up @@ -102,7 +101,7 @@ public void handshake(boolean bufferm4, byte[] expectedPub) {
}
}

private void go(String[] args) throws Exception {
private void go(String[] args) throws Throwable {

if (args.length > 0) {
hostAddr = args[0];
Expand All @@ -112,7 +111,7 @@ private void go(String[] args) throws Exception {
}

/* Step 1 */
a1RequestTest("A2 Server should response \"SC2-------\",\"ECHO------\" without public key included in A1", null, false);
a1RequestTest("A2 Server should response \"SCv2------\",\"ECHO------\" without public key included in A1", null, false);

/* Step 2 */
byte[] hostPub = handshakeTest("Normal handshake without public key included should work", null, null);
Expand All @@ -121,7 +120,7 @@ private void go(String[] args) throws Exception {
dummyPub[4] += 1;

/* Step 3 */
a1RequestTest("A2 Server should response \"SC2-------\",\"ECHO------\" when public key is included in A1", hostPub, false);
a1RequestTest("A2 Server should response \"SCv2------\",\"ECHO------\" when public key is included in A1", hostPub, false);

/* Step 4 */
a1RequestTest("A2 Server should response NO_SUCH_SERVER when an invalid public key is included in A1", dummyPub, true);
Expand All @@ -133,15 +132,48 @@ private void go(String[] args) throws Exception {
handshakeTest("Handshake should should response NO_SUCH_SERVER when unexpected public key is included in M1", dummyPub, new NoSuchServer());

/* Step 7 */
singlePackageEchoTest(null);
singlePackageEchoTest();
singlePackageEchoTest(hostPub);
singlePackageEchoTest(hostPub, true);

/* Step 8 */
multiPackageEchoTest();
multiPackageEchoTest(hostPub);
multiPackageEchoTest(hostPub, true);

/* Step 9 */
versionTest();

/* Step 10 */
sendDataTest();

/* Step 11 */
closeTest();

}

public static void main(String[] args) throws Exception {
new TcpEchoTester().go(args);
public static void main(final String[] args) throws Throwable {

int n = 1;
if (args.length > 2) {
n = Integer.parseInt(args[2]);
}

for (int i = 0; i < n; i++) {
new Thread(new Runnable() {

@Override
public void run() {
try {
new TcpEchoTester().go(args);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}).start();
}


}

private abstract class Test {
Expand Down Expand Up @@ -177,11 +209,11 @@ private void teardown() throws IOException {
environment.socket.close();
}

public void go() throws IOException {
public void go() throws Throwable {
System.out.println("Running test: " + doc);
environment = setup();
boolean passed = false;
Exception e = null;
Throwable e = null;
try {
run();
teardown();
Expand All @@ -190,18 +222,20 @@ public void go() throws IOException {
if (expectException != null && ex.getClass().equals(expectException.getClass())) {
passed = true;
}
e = ex;
}

if (!passed && e != null) {
System.out.println("Result: Failed");
throw new Throwable(e);
}

System.out.println("Result: Passed");

}
}

public void a1RequestTest(String doc, final byte[] hostPub, final boolean expectNoSuchServer) throws IOException {
public void a1RequestTest(String doc, final byte[] hostPub, final boolean expectNoSuchServer) throws Throwable {
new Test(doc) {
@Override
public void run() {
Expand Down Expand Up @@ -241,7 +275,7 @@ public void run() {
}.go();
}

public byte[] handshakeTest(String doc, final byte[] expectedPub, final Exception expectedException) throws IOException {
public byte[] handshakeTest(String doc, final byte[] expectedPub, final Exception expectedException) throws Throwable {

final byte[][] hostPub = {null};

Expand All @@ -260,12 +294,18 @@ public void run() {
return hostPub[0];
}

public void singlePackageEchoTest(final byte[] expectedPub) throws IOException {
public void singlePackageEchoTest() throws Throwable {
singlePackageEchoTest(null, false);
}
public void singlePackageEchoTest(byte[] expectedPub) throws Throwable {
singlePackageEchoTest(expectedPub, false);
}
public void singlePackageEchoTest(final byte[] expectedPub, final boolean bufferM4) throws Throwable {
new Test("Echo request in single encrypted package should work") {

@Override
public void run() throws IOException {
environment.handshake(false, expectedPub);
environment.handshake(bufferM4, expectedPub);

byte[] request = new byte[]{0x01, 0x02, 0x03};

Expand All @@ -280,12 +320,18 @@ public void run() throws IOException {
}.go();
}

public void multiPackageEchoTest() throws IOException {
public void multiPackageEchoTest() throws Throwable {
multiPackageEchoTest(null, false);
}
public void multiPackageEchoTest(final byte[] expectedPub) throws Throwable {
multiPackageEchoTest(expectedPub, false);
}
public void multiPackageEchoTest(final byte[] expectedPub, final boolean bufferM4) throws Throwable {
new Test("Multiple Echo requests in multi encrypted package should work") {

@Override
public void run() throws IOException {
environment.handshake(false, null);
environment.handshake(bufferM4, expectedPub);

byte[] request1 = new byte[]{0x01, 0x02, 0x03};
byte[] request2 = new byte[]{0x01, 0x02, 0x03};
Expand All @@ -306,4 +352,68 @@ public void run() throws IOException {
}.go();
}

public void versionTest() throws Throwable {
new Test("ECHO protocol version should work") {

@Override
public void run() throws IOException {
environment.handshake(false, null);

byte[] request = new byte[]{0x00};

environment.saltChannel.write(false, request);
byte[] response = environment.saltChannel.read();

if (!Arrays.equals(response, new byte[]{0x00, 0x01})) {
throw new AssertionError("Response differs from request");
}


}
}.go();
}

public void sendDataTest() throws Throwable {
new Test("ECHO send data request should work") {

@Override
public void run() throws IOException {
environment.handshake(false, null);

byte[] request = new byte[]{0x02, 0x05, 0x00, 0x00, 0x00, 0x7f};

environment.saltChannel.write(false, request);
byte[] response = environment.saltChannel.read();

byte[] expectedResponse = new byte[]{0x02, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f};

if (!Arrays.equals(response, expectedResponse)) {
throw new AssertionError("Response 1 differs from request");
}


}
}.go();
}

public void closeTest() throws Throwable {
new Test("ECHO close should work") {

@Override
public void run() throws IOException {
environment.handshake(false, null);

byte[] request = new byte[]{0x03};

environment.saltChannel.write(false, request);
byte[] response = environment.saltChannel.read();

if (!Arrays.equals(response, request)) {
throw new AssertionError("Response 1 differs from request");
}

}
}.go();
}

}

0 comments on commit 3cb440b

Please sign in to comment.