Create new api module

Creates a new API module with stable api interfaces. Adds some unit
tests to the vote test, updates unit testing framework across the board.
This commit is contained in:
Joe Hirschfeld committed 2019-10-11 21:35:58 -07:00
1 parent 2ad0fa118f
commit 1db78a5bea
11 files changed
+93 -64

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
# nuvotifier-api
Files in this directory have a strong API promise - they really shouldn't
change over time. Feel free to depend against it!
+5
View File
@@ -0,0 +1,5 @@
dependencies {
compile 'com.google.code.gson:gson:2.8.0'
testCompile "junit:junit:4.11"
testCompile "org.mockito:mockito-core:2.+"
}
@@ -0,0 +1,31 @@
package com.vexsoftware.votifier.model;
import com.google.gson.JsonObject;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
public class VoteTest {
@Test
public void testVoteConstructFromJsonObject() {
String serviceName = "serviceNameA";
String username = "usernameA";
String address = "home";
String timestamp = "1234";
Long localTimestamp = 1233L;
JsonObject o = new JsonObject();
o.addProperty("serviceName", serviceName);
o.addProperty("username", username);
o.addProperty("address", address);
o.addProperty("timestamp", timestamp);
o.addProperty("localTimestamp", localTimestamp);
Vote v = new Vote(o);
Assertions.assertEquals(serviceName, v.getServiceName());
Assertions.assertEquals(username, v.getUsername());
Assertions.assertEquals(address, v.getAddress());
Assertions.assertEquals(timestamp, v.getTimeStamp());
Assertions.assertEquals(localTimestamp, v.getLocalTimestamp());
}
}
+10 -3
View File
@@ -26,11 +26,18 @@ subprojects {
url "https://repo.velocitypowered.com/snapshots/"
}
}
dependencies {
testImplementation "org.junit.jupiter:junit-jupiter-api:5.4.2"
testRuntime "org.junit.jupiter:junit-jupiter-engine:5.4.2"
testCompile "org.mockito:mockito-core:2.+"
}
processResources {
filter(ReplaceTokens, tokens: ["app.version": this.project.version])
}
}
if (System.env.PUBLISH) {
apply from: 'publish.gradle'
test {
useJUnitPlatform()
}
}
+1 -1
View File
@@ -1,7 +1,7 @@
dependencies {
compile project(":nuvotifier-api")
compile group: "io.netty", name: "netty-handler", version: "4.1.32.Final"
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.google.guava:guava:24.1-jre'
testCompile group: "junit", name: "junit", version: "4.12"
testCompile group: "org.json", name: "json", version: "20180130" // retain this for testing reasons
}
@@ -8,14 +8,14 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.CorruptedFrameException;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
public class VotifierProtocol1DecoderTest {
private static final VotifierSession SESSION = new VotifierSession();
@@ -50,24 +50,21 @@ public class VotifierProtocol1DecoderTest {
byte[] encrypted = RSA.encrypt(bad.getBytes(StandardCharsets.UTF_8), TestVotifierPlugin.getI().getProtocolV1Key().getPublic());
ByteBuf encryptedByteBuf = Unpooled.wrappedBuffer(encrypted);
try {
channel.writeInbound(encryptedByteBuf);
} finally {
channel.close();
}
assertThrows(CorruptedFrameException.class, () -> channel.writeInbound(encryptedByteBuf));
channel.close();
}
@Test(expected = CorruptedFrameException.class)
@Test
public void testFailureDecodeMissingField() throws Exception {
verifyFailure("VOTE\nTest\ntest\ntest"); // missing field
}
@Test(expected = CorruptedFrameException.class)
@Test
public void testFailureDecodeBadOpcode() throws Exception {
verifyFailure("TEST\nTest\ntest\ntest\ntest\n");
}
@Test(expected = CorruptedFrameException.class)
@Test
public void testFailureDecodeBadRsa() throws Exception {
// Decode our bad RSA key
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
@@ -80,10 +77,7 @@ public class VotifierProtocol1DecoderTest {
byte[] encrypted = VoteUtil.encodePOJOv1(new Vote("Test", "test", "test", "test"), badPublicKey);
ByteBuf encryptedByteBuf = Unpooled.wrappedBuffer(encrypted);
try {
channel.writeInbound(encryptedByteBuf);
} finally {
channel.close();
}
assertThrows(CorruptedFrameException.class, ()->channel.writeInbound(encryptedByteBuf));
channel.close();
}
}
@@ -10,14 +10,15 @@ import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.CorruptedFrameException;
import io.netty.handler.codec.DecoderException;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import javax.crypto.Mac;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.Base64;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
public class VotifierProtocol2DecoderTest {
private static final VotifierSession SESSION = new VotifierSession();
@@ -61,7 +62,7 @@ public class VotifierProtocol2DecoderTest {
sendVote(new Vote("Test", "test", "test", "0"), TestVotifierPlugin.getI().getTokens().get("default"), true);
}
@Test(expected = DecoderException.class)
@Test
public void testFailureDecodeBadPacket() {
// Create a well-formed request
EmbeddedChannel channel = createChannel();
@@ -73,14 +74,11 @@ public class VotifierProtocol2DecoderTest {
object.put("payload", GsonInst.gson.toJson(payload));
// We "forget" the signature.
try {
channel.writeInbound(object.toString());
} finally {
channel.close();
}
assertThrows(DecoderException.class, () -> channel.writeInbound(object.toString()));
channel.close();
}
@Test(expected = DecoderException.class)
@Test
public void testFailureDecodeBadVoteField() throws Exception {
// Create a well-formed request
EmbeddedChannel channel = createChannel();
@@ -96,14 +94,11 @@ public class VotifierProtocol2DecoderTest {
object.put("signature",
Base64.getEncoder().encodeToString(mac.doFinal(payloadEncoded.getBytes(StandardCharsets.UTF_8))));
try {
channel.writeInbound(object.toString());
} finally {
channel.close();
}
assertThrows(DecoderException.class, () -> channel.writeInbound(object.toString()));
channel.close();
}
@Test(expected = DecoderException.class)
@Test
public void testFailureDecodeBadChallenge() throws Exception {
// Create a well-formed request
EmbeddedChannel channel = createChannel();
@@ -120,29 +115,24 @@ public class VotifierProtocol2DecoderTest {
object.put("signature",
Base64.getEncoder().encode(mac.doFinal(payloadEncoded.getBytes(StandardCharsets.UTF_8))));
try {
channel.writeInbound(object.toString());
} finally {
channel.close();
}
assertThrows(DecoderException.class, () -> channel.writeInbound(object.toString()));
channel.close();
}
@Test(expected = DecoderException.class)
@Test
public void testFailureDecodeNonExistentKey() throws Exception {
TestVotifierPlugin.getI().specificKeysOnly();
Vote vote = new Vote("Bad Service", "test", "test", "0");
try {
sendVote(vote, TestVotifierPlugin.getI().getTokens().get("Test"), false);
} finally {
TestVotifierPlugin.getI().restoreDefault();
}
assertThrows(DecoderException.class, () -> sendVote(vote, TestVotifierPlugin.getI().getTokens().get("Test"), false));
TestVotifierPlugin.getI().restoreDefault();
}
@Test(expected = CorruptedFrameException.class)
public void testFailureDecodeBadSignature() throws Exception {
@Test
public void testFailureDecodeBadSignature() {
Vote vote = new Vote("Bad Service", "test", "test", "0");
sendVote(vote, KeyCreator.createKeyFrom("BadKey"), false);
assertThrows(CorruptedFrameException.class, () -> sendVote(vote, KeyCreator.createKeyFrom("BadKey"), false));
}
}
@@ -5,10 +5,12 @@ import com.vexsoftware.votifier.net.VotifierSession;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.CorruptedFrameException;
import io.netty.handler.codec.DecoderException;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class VotifierProtocolDifferentiatorTest {
@Test
@@ -43,7 +45,7 @@ public class VotifierProtocolDifferentiatorTest {
channel.close();
}
@Test(expected = DecoderException.class)
@Test
public void failIfv1NotSupported() {
EmbeddedChannel channel = new EmbeddedChannel(new VotifierProtocolDifferentiator(true, false));
@@ -54,13 +56,11 @@ public class VotifierProtocolDifferentiatorTest {
for (int i = 0; i < 256; i++) {
test.writeByte(0);
}
channel.writeInbound(test);
assertEquals(VotifierSession.ProtocolVersion.ONE, session.getVersion());
assertThrows(CorruptedFrameException.class, () -> channel.writeInbound(test));
channel.close();
}
@Test(expected = DecoderException.class)
@Test
public void failOnBadPacketTest() {
EmbeddedChannel channel = new EmbeddedChannel(new VotifierProtocolDifferentiator(true, false));
@@ -69,11 +69,8 @@ public class VotifierProtocolDifferentiatorTest {
buf.writeByte(0);
}
try {
channel.writeInbound(buf);
} finally {
channel.close();
}
assertThrows(DecoderException.class, () -> channel.writeInbound(buf));
channel.close();
}
@Test
@@ -2,13 +2,11 @@ package com.vexsoftware.votifier.support.forwarding;
import com.google.common.collect.Lists;
import com.vexsoftware.votifier.model.Vote;
import com.vexsoftware.votifier.support.forwarding.ForwardedVoteListener;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class PMForwardingSinkTest {
+3
View File
@@ -1,5 +1,8 @@
rootProject.name = 'nuvotifier'
include 'api'
findProject(':api')?.name = 'nuvotifier-api'
include 'common'
findProject(':common')?.name = 'nuvotifier-common'