Add tests

This commit is contained in:
Flare Microsystems
2024-11-10 20:15:43 -08:00
parent dd47f1e242
commit 75eb55d341
21 changed files with 583 additions and 32 deletions

View File

@@ -4,5 +4,8 @@
<classpathentry kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="/tool/junit4/mockito-core-2.28.2.jar"/>
<classpathentry kind="lib" path="/tool/junit4/byte-buddy-1.9.16.jar"/>
<classpathentry kind="lib" path="/tool/junit4/objenesis-2.6.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

27
bin/test.xml Normal file
View File

@@ -0,0 +1,27 @@
<execution>
<environment>
<variable name="ENV_VAR">Variable Contents</variable>
</environment>
<!--Empty clone clones the whole workspace?-->
<clone />
<bat>
echo this could be a whole batch script or just one command
</bat>
<py>
print("this could be a whole python script or just one call")
</py>
<sh which="bash">
echo "this could be a whole sh script or just one command"
</sh>
<ps>
echo "this could be a whole powershell script or just one command"
</ps>
<artifacts>
<!--Mode could be raw copy or zip?-->
<artifact mode="copy">
<from>dist/*</from>
<!--Empty clone clones the whole workspace?-->
<to>dist/</to>
</artifact>
</artifacts>
</execution>

View File

@@ -52,7 +52,7 @@
</classpath>
<batchtest fork="yes" todir="${reports.dir}">
<fileset dir="${test.dir}">
<include name="**/*.class" />
<include name="**/*Test.class" />
</fileset>
</batchtest>
<formatter type="xml" />

View File

@@ -9,8 +9,8 @@ import com.flaremicro.crossjeeves.net.packet.Packet127KeepAlive;
import com.flaremicro.crossjeeves.net.packet.Packet1Status;
import com.flaremicro.crossjeeves.net.packet.Packet2Script;
import com.flaremicro.crossjeeves.net.packet.Packet3Clone;
import com.flaremicro.crossjeeves.net.packet.Packet4FileData;
import com.flaremicro.crossjeeves.net.packet.Packet5Artifact;
import com.flaremicro.crossjeeves.net.packet.test.Packet4FileDataTest;
import com.flaremicro.util.ZipUtils;
public class ClientHandler extends NetworkHandler {
@@ -61,7 +61,7 @@ public class ClientHandler extends NetworkHandler {
}
@Override
public void handlePacket(Packet4FileData packet) {
public void handlePacket(Packet4FileDataTest packet) {
// TODO Auto-generated method stub
}

View File

@@ -6,8 +6,8 @@ import com.flaremicro.crossjeeves.net.packet.Packet127KeepAlive;
import com.flaremicro.crossjeeves.net.packet.Packet1Status;
import com.flaremicro.crossjeeves.net.packet.Packet2Script;
import com.flaremicro.crossjeeves.net.packet.Packet3Clone;
import com.flaremicro.crossjeeves.net.packet.Packet4FileData;
import com.flaremicro.crossjeeves.net.packet.Packet5Artifact;
import com.flaremicro.crossjeeves.net.packet.test.Packet4FileDataTest;
public abstract class NetworkHandler {
@@ -17,7 +17,7 @@ public abstract class NetworkHandler {
public abstract void handlePacket(Packet1Status packet);
public abstract void handlePacket(Packet2Script packet);
public abstract void handlePacket(Packet3Clone packet);
public abstract void handlePacket(Packet4FileData packet);
public abstract void handlePacket(Packet4FileDataTest packet);
public abstract void handlePacket(Packet5Artifact packet);
public abstract void handlePacket(Packet127KeepAlive packet);

View File

@@ -6,8 +6,8 @@ import com.flaremicro.crossjeeves.net.packet.Packet127KeepAlive;
import com.flaremicro.crossjeeves.net.packet.Packet1Status;
import com.flaremicro.crossjeeves.net.packet.Packet2Script;
import com.flaremicro.crossjeeves.net.packet.Packet3Clone;
import com.flaremicro.crossjeeves.net.packet.Packet4FileData;
import com.flaremicro.crossjeeves.net.packet.Packet5Artifact;
import com.flaremicro.crossjeeves.net.packet.test.Packet4FileDataTest;
public class ServerHandler extends NetworkHandler {
@@ -39,7 +39,7 @@ public class ServerHandler extends NetworkHandler {
}
@Override
public void handlePacket(Packet4FileData packet) {
public void handlePacket(Packet4FileDataTest packet) {
// TODO Auto-generated method stub
}

View File

@@ -7,8 +7,8 @@ import java.io.IOException;
import com.flaremicro.crossjeeves.net.NetworkHandler;
public class Packet0Identify extends Packet{
public int protocolVersion;
int flags;
private int protocolVersion;
private int flags;
public Packet0Identify(){
}
@@ -33,6 +33,16 @@ public class Packet0Identify extends Packet{
networkHandler.handlePacket(this);
}
public int getProtocolVersion()
{
return protocolVersion;
}
public int getFlags()
{
return flags;
}
@Override
public Packet cloneTypeOnly() {
return new Packet0Identify();

View File

@@ -7,8 +7,7 @@ import java.io.IOException;
import com.flaremicro.crossjeeves.net.NetworkHandler;
public class Packet1Status extends Packet{
public int protocolVersion;
int flags;
private int flags;
public static final int BUSY = 0x00000001;
@@ -32,6 +31,11 @@ public class Packet1Status extends Packet{
networkHandler.handlePacket(this);
}
public int getFlags()
{
return flags;
}
@Override
public Packet cloneTypeOnly() {
return new Packet1Status();

View File

@@ -7,7 +7,7 @@ import java.io.IOException;
import com.flaremicro.crossjeeves.net.NetworkHandler;
public class Packet2Script extends Packet{
String script;
public String script;
public Packet2Script(){
}

View File

@@ -7,8 +7,8 @@ import java.io.IOException;
import com.flaremicro.crossjeeves.net.NetworkHandler;
public class Packet3Clone extends Packet{
long fileId;
long fileSize;
private long fileId;
private long fileSize;
public Packet3Clone(){
}

View File

@@ -7,8 +7,8 @@ import java.io.IOException;
import com.flaremicro.crossjeeves.net.NetworkHandler;
public class Packet4FileData extends Packet{
long fileId;
byte[] fileChunk;
private long fileId;
private byte[] fileChunk;
public Packet4FileData(){
}
@@ -36,6 +36,16 @@ public class Packet4FileData extends Packet{
networkHandler.handlePacket(this);
}
public long getFileId()
{
return fileId;
}
public byte[] getFileChunk()
{
return fileChunk;
}
@Override
public Packet cloneTypeOnly() {
return new Packet4FileData();

View File

@@ -7,14 +7,15 @@ import java.io.IOException;
import com.flaremicro.crossjeeves.net.NetworkHandler;
public class Packet5Artifact extends Packet{
long fileId;
String relativeFile;
private long fileId;
private String relativeFile;
public Packet5Artifact(){
}
public Packet5Artifact(long fileId, String relativeFile){
this.fileId = fileId;
this.relativeFile = relativeFile;
}
public void recievePacket(DataInputStream in) throws IOException {
@@ -32,6 +33,15 @@ public class Packet5Artifact extends Packet{
networkHandler.handlePacket(this);
}
public long getFileId()
{
return fileId;
}
public String getRelativeFile(){
return this.relativeFile;
}
@Override
public Packet cloneTypeOnly() {
return new Packet5Artifact();

View File

@@ -1,14 +0,0 @@
package com.flaremicro.crossjeeves;
import static org.junit.Assert.*;
import org.junit.Test;
public class MainTest {
@Test
public void test() {
assertTrue(true);
}
}

View File

@@ -0,0 +1,68 @@
package com.flaremicro.crossjeeves.net.packet.test;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import com.flaremicro.crossjeeves.net.NetworkHandler;
import com.flaremicro.crossjeeves.net.packet.Packet;
import com.flaremicro.crossjeeves.net.packet.Packet0Identify;
@RunWith(MockitoJUnitRunner.class)
public class Packet0IdentifyTest extends PacketTestBase {
@Mock
NetworkHandler handler;
@Test
public void testWrite() throws IOException
{
Packet0Identify packet = new Packet0Identify(1337);
assertEquals(1337, packet.getFlags());
assertEquals(Packet.PROTOCOL_VERSION, packet.getProtocolVersion());
packet.sendPacket(output());
assertEquals(0, input().readByte());
assertEquals(Packet.PROTOCOL_VERSION, input().readInt());
assertEquals(1337, input().readInt());
assertBufferEmpty();
}
@Test
public void testRead() throws IOException
{
Packet0Identify packet = new Packet0Identify();
output().writeInt(Packet.PROTOCOL_VERSION);
output().writeInt(1337);
packet.recievePacket(input());
assertEquals(Packet.PROTOCOL_VERSION, packet.getProtocolVersion());
assertEquals(1337, packet.getFlags());
assertBufferEmpty();
}
@Test
public void testProcess() throws IOException
{
Packet0Identify packet = new Packet0Identify(1337);
packet.processPacket(handler);
Mockito.verify(handler, Mockito.times(1)).handlePacket(packet);
}
@Test
public void testTypeCopy()
{
Packet0Identify packet = new Packet0Identify();
Packet clonedPacket = packet.cloneTypeOnly();
assertEquals(packet.getClass(), clonedPacket.getClass());
}
}

View File

@@ -0,0 +1,64 @@
package com.flaremicro.crossjeeves.net.packet.test;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import com.flaremicro.crossjeeves.net.NetworkHandler;
import com.flaremicro.crossjeeves.net.packet.Packet;
import com.flaremicro.crossjeeves.net.packet.Packet1Status;
@RunWith(MockitoJUnitRunner.class)
public class Packet1StatusTest extends PacketTestBase {
@Mock
NetworkHandler handler;
@Test
public void testWrite() throws IOException
{
Packet1Status packet = new Packet1Status(1337);
assertEquals(1337, packet.getFlags());
packet.sendPacket(output());
assertEquals(1, input().readByte());
assertEquals(1337, input().readInt());
assertBufferEmpty();
}
@Test
public void testRead() throws IOException
{
Packet1Status packet = new Packet1Status();
output().writeInt(1337);
packet.recievePacket(input());
assertEquals(1337, packet.getFlags());
assertBufferEmpty();
}
@Test
public void testProcess() throws IOException
{
Packet1Status packet = new Packet1Status(1337);
packet.processPacket(handler);
Mockito.verify(handler, Mockito.times(1)).handlePacket(packet);
}
@Test
public void testTypeCopy()
{
Packet1Status packet = new Packet1Status();
Packet clonedPacket = packet.cloneTypeOnly();
assertEquals(packet.getClass(), clonedPacket.getClass());
}
}

View File

@@ -0,0 +1,66 @@
package com.flaremicro.crossjeeves.net.packet.test;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import com.flaremicro.crossjeeves.net.NetworkHandler;
import com.flaremicro.crossjeeves.net.packet.Packet;
import com.flaremicro.crossjeeves.net.packet.Packet2Script;
@RunWith(MockitoJUnitRunner.class)
public class Packet2ScriptTest extends PacketTestBase {
@Mock
NetworkHandler handler;
@Test
public void testWrite() throws IOException
{
Packet2Script packet = new Packet2Script("1337");
assertEquals("1337", packet.script);
packet.sendPacket(output());
assertEquals(2, input().readByte());
assertEquals("1337", input().readUTF());
assertBufferEmpty();
}
@Test
public void testRead() throws IOException
{
Packet2Script packet = new Packet2Script();
packet.script = "";
output().writeUTF("1337");
packet.recievePacket(input());
assertEquals("1337", packet.script);
assertBufferEmpty();
}
@Test
public void testProcess() throws IOException
{
Packet2Script packet = new Packet2Script("1337");
packet.processPacket(handler);
Mockito.verify(handler, Mockito.times(1)).handlePacket(packet);
}
@Test
public void testTypeCopy()
{
Packet2Script packet = new Packet2Script();
Packet clonedPacket = packet.cloneTypeOnly();
assertEquals(packet.getClass(), clonedPacket.getClass());
}
}

View File

@@ -0,0 +1,68 @@
package com.flaremicro.crossjeeves.net.packet.test;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import com.flaremicro.crossjeeves.net.NetworkHandler;
import com.flaremicro.crossjeeves.net.packet.Packet;
import com.flaremicro.crossjeeves.net.packet.Packet3Clone;
@RunWith(MockitoJUnitRunner.class)
public class Packet3CloneTest extends PacketTestBase {
@Mock
NetworkHandler handler;
@Test
public void testWrite() throws IOException
{
Packet3Clone packet = new Packet3Clone(1, 2);
assertEquals(packet.getFileID(), 1);
assertEquals(packet.getFileSize(), 2);
packet.sendPacket(output());
assertEquals(3, input().readByte());
assertEquals(1, input().readLong());
assertEquals(2, input().readLong());
assertBufferEmpty();
}
@Test
public void testRead() throws IOException
{
Packet3Clone packet = new Packet3Clone();
output().writeLong(1);
output().writeLong(2);
packet.recievePacket(input());
assertEquals(1, packet.getFileID());
assertEquals(2, packet.getFileSize());
assertBufferEmpty();
}
@Test
public void testProcess() throws IOException
{
Packet3Clone packet = new Packet3Clone(1, 2);
packet.processPacket(handler);
Mockito.verify(handler, Mockito.times(1)).handlePacket(packet);
}
@Test
public void testTypeCopy()
{
Packet3Clone packet = new Packet3Clone();
Packet clonedPacket = packet.cloneTypeOnly();
assertEquals(packet.getClass(), clonedPacket.getClass());
}
}

View File

@@ -0,0 +1,67 @@
package com.flaremicro.crossjeeves.net.packet.test;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import com.flaremicro.crossjeeves.net.NetworkHandler;
import com.flaremicro.crossjeeves.net.packet.Packet;
import com.flaremicro.crossjeeves.net.packet.Packet5Artifact;
@RunWith(MockitoJUnitRunner.class)
public class Packet4FileDataTest extends PacketTestBase {
@Mock
NetworkHandler handler;
@Test
public void testWrite() throws IOException
{
Packet5Artifact packet = new Packet5Artifact(1, "file.txt");
assertEquals(packet.getFileId(), 1);
assertEquals("file.txt", packet.getRelativeFile());
packet.sendPacket(output());
assertEquals(5, input().readByte());
assertEquals(1, input().readLong());
assertEquals("file.txt", input().readUTF());
assertBufferEmpty();
}
@Test
public void testRead() throws IOException
{
Packet5Artifact packet = new Packet5Artifact();
output().writeLong(1);
output().writeUTF("file.txt");
packet.recievePacket(input());
assertEquals(1, packet.getFileId());
assertEquals("file.txt", packet.getRelativeFile());
assertBufferEmpty();
}
@Test
public void testProcess() throws IOException
{
Packet5Artifact packet = new Packet5Artifact(1, "file.txt");
packet.processPacket(handler);
Mockito.verify(handler, Mockito.times(1)).handlePacket(packet);
}
@Test
public void testTypeCopy()
{
Packet5Artifact packet = new Packet5Artifact();
Packet clonedPacket = packet.cloneTypeOnly();
assertEquals(packet.getClass(), clonedPacket.getClass());
}
}

View File

@@ -0,0 +1,74 @@
package com.flaremicro.crossjeeves.net.packet.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import com.flaremicro.crossjeeves.net.NetworkHandler;
import com.flaremicro.crossjeeves.net.packet.Packet;
import com.flaremicro.crossjeeves.net.packet.Packet4FileData;
@RunWith(MockitoJUnitRunner.class)
public class Packet5ArtifactTest extends PacketTestBase {
@Mock
NetworkHandler handler;
@Test
public void testWrite() throws IOException
{
Packet4FileData packet = new Packet4FileData(1, new byte[]{1,2,3,4});
assertEquals(packet.getFileId(), 1);
assertArrayEquals(new byte[]{1,2,3,4}, packet.getFileChunk());
packet.sendPacket(output());
byte[] buffer = new byte[4];
assertEquals(4, input().readByte());
assertEquals(1, input().readLong());
assertEquals(4, input().readShort());
input().readFully(buffer);
assertArrayEquals(new byte[]{1,2,3,4}, buffer);
assertBufferEmpty();
}
@Test
public void testRead() throws IOException
{
Packet4FileData packet = new Packet4FileData();
output().writeLong(1);
output().writeShort(4);
output().write(new byte[]{1,2,3,4});
packet.recievePacket(input());
assertEquals(1, packet.getFileId());
assertArrayEquals(new byte[]{1,2,3,4}, packet.getFileChunk());
assertBufferEmpty();
}
@Test
public void testProcess() throws IOException
{
Packet4FileData packet = new Packet4FileData(1, new byte[]{1,2,3,4});
packet.processPacket(handler);
Mockito.verify(handler, Mockito.times(1)).handlePacket(packet);
}
@Test
public void testTypeCopy()
{
Packet4FileData packet = new Packet4FileData();
Packet clonedPacket = packet.cloneTypeOnly();
assertEquals(packet.getClass(), clonedPacket.getClass());
}
}

View File

@@ -0,0 +1,44 @@
package com.flaremicro.crossjeeves.net.packet.test;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
import org.junit.AfterClass;
import org.junit.Before;
import com.flaremicro.test.util.InOutBuffer;
public abstract class PacketTestBase {
private static InOutBuffer buffer = new InOutBuffer();
private static DataInputStream input = new DataInputStream(buffer.getInputStream());
private static DataOutputStream output = new DataOutputStream(buffer.getOutputStream());
@AfterClass
public static void cleanUp() throws IOException {
buffer.empty();
input.close();
output.close();
}
@Before
public void clearBuffer()
{
buffer.empty();
}
public void assertBufferEmpty() {
assertEquals(0, buffer.size());
}
protected DataInputStream input() {
return input;
}
protected DataOutputStream output() {
return output;
}
}

View File

@@ -0,0 +1,50 @@
package com.flaremicro.test.util;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
public class InOutBuffer {
private final ArrayList<Byte> buffer = new ArrayList<Byte>();
public final InputStream getInputStream() {
return new BufferInputStream();
}
public final OutputStream getOutputStream() {
return new BufferOutputStream();
}
public void empty() {
buffer.clear();
}
class BufferInputStream extends InputStream {
@Override
public int read() throws IOException {
synchronized (buffer)
{
if (buffer.size() <= 0)
throw new EOFException("End of file");
return buffer.remove(0) & 0xFF;
}
}
}
class BufferOutputStream extends OutputStream {
@Override
public void write(int arg0) throws IOException {
synchronized (buffer)
{
buffer.add((byte) arg0);
}
}
}
public int size() {
return buffer.size();
}
}