mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
Added reasonable testing for StreamReciever, further testing would probably need StreamReciever to be rewritten #story[817]
This commit is contained in:
@@ -19,6 +19,7 @@ public class StreamReceiver {
|
|||||||
public StreamReceiver(String hostAddress, int hostPort, PriorityBlockingQueue packetBuffer) {
|
public StreamReceiver(String hostAddress, int hostPort, PriorityBlockingQueue packetBuffer) {
|
||||||
try {
|
try {
|
||||||
host = new Socket(hostAddress, hostPort);
|
host = new Socket(hostAddress, hostPort);
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
@@ -26,6 +27,12 @@ public class StreamReceiver {
|
|||||||
this.packetBuffer = packetBuffer;
|
this.packetBuffer = packetBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public StreamReceiver(Socket host, PriorityBlockingQueue packetBuffer){
|
||||||
|
this.host=host;
|
||||||
|
this.packetBuffer = packetBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void connect(){
|
public void connect(){
|
||||||
try {
|
try {
|
||||||
stream = host.getInputStream();
|
stream = host.getInputStream();
|
||||||
@@ -66,7 +73,6 @@ public class StreamReceiver {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
moreBytes = false;
|
moreBytes = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,8 +106,8 @@ public class StreamReceiver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* takes an array of up to 7 bytes and returns a positive
|
* takes an array of up to 7 bytes in little endian format and
|
||||||
* long constructed from the input bytes
|
* returns a positive long constructed from the input bytes
|
||||||
*
|
*
|
||||||
* @return a positive long if there is less than 7 bytes -1 otherwise
|
* @return a positive long if there is less than 7 bytes -1 otherwise
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -0,0 +1,105 @@
|
|||||||
|
package seng302.models.parsers;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.concurrent.PriorityBlockingQueue;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by ptg19 on 26/04/17.
|
||||||
|
*/
|
||||||
|
public class StreamReceiverTest {
|
||||||
|
|
||||||
|
private PriorityBlockingQueue pq;
|
||||||
|
private byte[] brokenPacket = {0x47, (byte) 0x83, 37, // sync1 sync2 and message type
|
||||||
|
0b00000000, 0b01000000, 0b00100010, 0b00100100, 0b00011000, 0b00000000, //timestamp
|
||||||
|
0b00000000, 0b00010000, 0b01000000, 0b00000000, //source id
|
||||||
|
0b00100010, 0b00101000, // message length
|
||||||
|
0b00010010, 0b00010010, 0b00010010}; //random start of payload
|
||||||
|
|
||||||
|
private byte[] workingPacket = {0x47, (byte) 0x83, 37, // sync1 sync2 and message type
|
||||||
|
0b00000000, 0b01000000, 0b00100010, 0b00100100, 0b00011000, 0b00000000, //timestamp
|
||||||
|
0b00000000, 0b00010000, 0b01000000, 0b00000000, //source id
|
||||||
|
0b00000010, 0b00000000, // message length
|
||||||
|
0b00010010, 0b00010010, // payload
|
||||||
|
0b00100110, (byte)0b10000111, 0b00110101, 0b01111000}; //crc
|
||||||
|
|
||||||
|
private byte[] crcMismatchPacket = {0x47, (byte) 0x83, 37, // sync1 sync2 and message type
|
||||||
|
0b00000000, 0b01000000, 0b00100010, 0b00100100, 0b00011000, 0b00000000, //timestamp
|
||||||
|
0b00000000, 0b00000000, 0b01000000, 0b00000000, //source id
|
||||||
|
0b00000010, 0b00000000, // message length
|
||||||
|
0b00010010, 0b00010010, // payload
|
||||||
|
0b00100110, (byte)0b10000111, 0b00110101, 0b01111000}; //crc
|
||||||
|
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup(){
|
||||||
|
pq = new PriorityBlockingQueue<>(256, new Comparator<StreamPacket>() {
|
||||||
|
@Override
|
||||||
|
public int compare(StreamPacket s1, StreamPacket s2) {
|
||||||
|
return (int) (s1.getTimeStamp() - s2.getTimeStamp());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void connectExitsOnUnexpectedStreamEnd() throws Exception {
|
||||||
|
Socket host=mock(Socket.class);
|
||||||
|
InputStream stream = new ByteArrayInputStream(brokenPacket);
|
||||||
|
when(host.getInputStream()).thenReturn(stream);
|
||||||
|
StreamReceiver streamReceiver = new StreamReceiver(host, pq);
|
||||||
|
|
||||||
|
streamReceiver.connect();
|
||||||
|
assert pq.size() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void connectReadsAPacket() throws Exception {
|
||||||
|
Socket host=mock(Socket.class);
|
||||||
|
InputStream stream = new ByteArrayInputStream(workingPacket);
|
||||||
|
when(host.getInputStream()).thenReturn(stream);
|
||||||
|
StreamReceiver streamReceiver = new StreamReceiver(host, pq);
|
||||||
|
|
||||||
|
streamReceiver.connect();
|
||||||
|
assert pq.size() == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void connectDropsAMismatchedCrc() throws Exception {
|
||||||
|
Socket host=mock(Socket.class);
|
||||||
|
InputStream stream = new ByteArrayInputStream(crcMismatchPacket);
|
||||||
|
when(host.getInputStream()).thenReturn(stream);
|
||||||
|
StreamReceiver streamReceiver = new StreamReceiver(host, pq);
|
||||||
|
|
||||||
|
streamReceiver.connect();
|
||||||
|
assert pq.size() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void bytestoLongTest() {
|
||||||
|
Socket host=mock(Socket.class);
|
||||||
|
StreamReceiver streamReceiver = new StreamReceiver(host, pq);
|
||||||
|
try {
|
||||||
|
Class[] args = new Class[1];
|
||||||
|
args[0] = byte[].class;
|
||||||
|
Method bytesToLong = streamReceiver.getClass().getDeclaredMethod("bytesToLong", args);
|
||||||
|
bytesToLong.setAccessible(true);
|
||||||
|
byte[] sevenBtyeNumber = {0b01100100, 0b00110100, 0b00010100, 0b00000000, 0b00000000, 0b00000000, (byte)0b10000000};
|
||||||
|
assert bytesToLong.invoke(streamReceiver, sevenBtyeNumber).equals(36028797020288100L);
|
||||||
|
byte[] eightByteNumber = {0b01100100, 0b00110100, 0b00010100, 0b00000000, 0b00000000, 0b00000000, (byte)0b10000000, 0b00100101};
|
||||||
|
assert bytesToLong.invoke(streamReceiver, eightByteNumber).equals(-1L);
|
||||||
|
byte[] emptyArray = {};
|
||||||
|
assert bytesToLong.invoke(streamReceiver, emptyArray).equals(0L);
|
||||||
|
} catch (Exception e){
|
||||||
|
System.out.println("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user