Skip to content

Commit

Permalink
broken test for microtype...
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaruth committed Sep 22, 2024
1 parent 8a7931f commit 918287f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 63 deletions.
1 change: 0 additions & 1 deletion client/src/main/java/util/AbstractDesktopClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ public abstract class AbstractDesktopClient extends AbstractClient
public void init()
{
EncryptionUtil.setBase64Interface(new Base64Desktop());
MessageUtil.generatePublicKey();
}
}
20 changes: 9 additions & 11 deletions client/src/main/kotlin/http/SessionApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,18 @@ class SessionApi(private val httpClient: HttpClient) {
DialogUtilNew.showErrorLater(
"Error communicating with server.\n\n${response.unirestException.message}"
)
is SuccessResponse<BeginSessionResponse> -> {
// 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<BeginSessionResponse> -> 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) {
Expand Down
21 changes: 1 addition & 20 deletions core/src/main/java/util/EncryptionUtil.java
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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;
Expand Down
32 changes: 1 addition & 31 deletions core/src/main/java/util/MessageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions core/src/test/kotlin/types/StringMicrotypeTest.kt
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 918287f

Please sign in to comment.