From 918287f93b7d917e792673237a789a22e738ae7d Mon Sep 17 00:00:00 2001 From: Alyssa Date: Sun, 22 Sep 2024 18:26:52 +0100 Subject: [PATCH] broken test for microtype... --- .../main/java/util/AbstractDesktopClient.java | 1 - client/src/main/kotlin/http/SessionApi.kt | 20 ++++++------ core/src/main/java/util/EncryptionUtil.java | 21 +----------- core/src/main/java/util/MessageUtil.java | 32 +------------------ .../test/kotlin/types/StringMicrotypeTest.kt | 21 ++++++++++++ 5 files changed, 32 insertions(+), 63 deletions(-) create mode 100644 core/src/test/kotlin/types/StringMicrotypeTest.kt diff --git a/client/src/main/java/util/AbstractDesktopClient.java b/client/src/main/java/util/AbstractDesktopClient.java index 467ef61..5631a5b 100644 --- a/client/src/main/java/util/AbstractDesktopClient.java +++ b/client/src/main/java/util/AbstractDesktopClient.java @@ -7,6 +7,5 @@ public abstract class AbstractDesktopClient extends AbstractClient public void init() { EncryptionUtil.setBase64Interface(new Base64Desktop()); - MessageUtil.generatePublicKey(); } } diff --git a/client/src/main/kotlin/http/SessionApi.kt b/client/src/main/kotlin/http/SessionApi.kt index d4ba316..e4bce97 100644 --- a/client/src/main/kotlin/http/SessionApi.kt +++ b/client/src/main/kotlin/http/SessionApi.kt @@ -25,20 +25,18 @@ class SessionApi(private val httpClient: HttpClient) { DialogUtilNew.showErrorLater( "Error communicating with server.\n\n${response.unirestException.message}" ) - is SuccessResponse -> { - // Errr, store the sessionId someplace, then hook back into legacy code somehow to - // launch the lobby etc - val sessionResponse = response.body - - val lobby = ScreenCache.getEntropyLobby() - lobby.username = sessionResponse.name - lobby.setLocationRelativeTo(null) - lobby.isVisible = true - lobby.init() - } + is SuccessResponse -> handleConnectSuccess(response.body) } } + private fun handleConnectSuccess(response: BeginSessionResponse) { + val lobby = ScreenCache.getEntropyLobby() + lobby.username = response.name + lobby.setLocationRelativeTo(null) + lobby.isVisible = true + lobby.init() + } + private fun handleBeginSessionFailure(response: FailureResponse<*>) = SwingUtilities.invokeLater { when (response.errorCode) { diff --git a/core/src/main/java/util/EncryptionUtil.java b/core/src/main/java/util/EncryptionUtil.java index 9532dae..ad1ace4 100644 --- a/core/src/main/java/util/EncryptionUtil.java +++ b/core/src/main/java/util/EncryptionUtil.java @@ -1,11 +1,9 @@ package util; -import java.security.Key; -import java.security.MessageDigest; - import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; +import java.security.Key; public class EncryptionUtil { @@ -112,23 +110,6 @@ public static String decrypt(String encryptedMessage, Key key, boolean asymmetri return messageString; } - public static String sha256Hash(String input) - { - try - { - MessageDigest md = MessageDigest.getInstance("SHA-256"); - md.update(input.getBytes("UTF-8")); - byte[] digest = md.digest(); - - return EncryptionUtil.base64Interface.encode(digest); - } - catch (Throwable t) - { - Debug.stackTrace(t); - return ""; - } - } - public static void setBase64Interface(Base64Interface base64Interface) { EncryptionUtil.base64Interface = base64Interface; diff --git a/core/src/main/java/util/MessageUtil.java b/core/src/main/java/util/MessageUtil.java index db458b5..fffc2fe 100644 --- a/core/src/main/java/util/MessageUtil.java +++ b/core/src/main/java/util/MessageUtil.java @@ -6,15 +6,8 @@ import org.w3c.dom.Document; import javax.crypto.SecretKey; -import java.io.BufferedInputStream; -import java.io.InputStream; -import java.io.ObjectInputStream; -import java.math.BigInteger; import java.net.InetAddress; import java.net.UnknownHostException; -import java.security.KeyFactory; -import java.security.PublicKey; -import java.security.spec.RSAPublicKeySpec; import static utils.InjectedThings.logger; @@ -27,30 +20,7 @@ public class MessageUtil implements OnlineConstants public static final String SERVER_IP = LOCAL_IP; public static int millisDelay = 0; - public static PublicKey publicKey = null; public static SecretKey symmetricKey = EncryptionUtil.reconstructKeyFromString(LegacyConstants.SYMMETRIC_KEY_STR); - - private static int cachedPortNumber = SERVER_PORT_NUMBER; - - public static void generatePublicKey() - { - Debug.append("Reading in public key..."); - - try (InputStream in = MessageUtil.class.getResourceAsStream("/public.key"); - ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in))) - { - BigInteger m = (BigInteger) oin.readObject(); - BigInteger e = (BigInteger) oin.readObject(); - RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e); - KeyFactory fact = KeyFactory.getInstance("RSA"); - MessageUtil.publicKey = fact.generatePublic(keySpec); - Debug.append("Key read successfully"); - } - catch (Throwable t) - { - logger.error("keyError", "Unable to read public key - won't be able to communicate with Server.", t); - } - } public static void sendMessage(Document message, int millis) { @@ -86,7 +56,7 @@ public static void sendMessage(MessageSenderParams message, boolean encrypt) public static int getRandomPortNumber() { - return cachedPortNumber; + return SERVER_PORT_NUMBER; } public static InetAddress factoryInetAddress(String ipAddress) diff --git a/core/src/test/kotlin/types/StringMicrotypeTest.kt b/core/src/test/kotlin/types/StringMicrotypeTest.kt new file mode 100644 index 0000000..f8062db --- /dev/null +++ b/core/src/test/kotlin/types/StringMicrotypeTest.kt @@ -0,0 +1,21 @@ +package types + +import com.fasterxml.jackson.databind.ObjectMapper +import io.kotest.matchers.shouldBe +import org.junit.jupiter.api.Test +import testCore.AbstractTest + +class StringMicrotypeTest : AbstractTest() { + @Test + fun `Should serialize and deserialize correctly`() { + val microtype = TestMicrotype("testValue") + + val serialized = ObjectMapper().writeValueAsString(microtype) + serialized shouldBe "testValue" + + val deserialized = ObjectMapper().readValue(serialized, TestMicrotype::class.java) + deserialized shouldBe microtype + } + + private class TestMicrotype(value: String) : StringMicrotype(value) +}