From 0794c90333499876b9e5ce7ee7fc0cbb71990560 Mon Sep 17 00:00:00 2001 From: AndrewQuijano Date: Tue, 7 May 2024 10:59:50 -0400 Subject: [PATCH] Use validating stream --- src/main/java/BigIntPoint.java | 3 +-- src/main/java/EncryptedPathsComparison.java | 4 ++-- src/main/java/PathsAlice.java | 18 ++++++++++++++++-- src/test/java/IntersectTest.java | 21 +++++++++++++-------- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/main/java/BigIntPoint.java b/src/main/java/BigIntPoint.java index 3639a3c..13c38fc 100644 --- a/src/main/java/BigIntPoint.java +++ b/src/main/java/BigIntPoint.java @@ -6,8 +6,7 @@ public class BigIntPoint implements Serializable BigInteger x; BigInteger y; - public BigIntPoint(BigInteger x, BigInteger y) - { + public BigIntPoint(BigInteger x, BigInteger y) { this.x = x; this.y = y; } diff --git a/src/main/java/EncryptedPathsComparison.java b/src/main/java/EncryptedPathsComparison.java index 5cc7b97..2e9072f 100644 --- a/src/main/java/EncryptedPathsComparison.java +++ b/src/main/java/EncryptedPathsComparison.java @@ -127,10 +127,10 @@ public boolean encryptedOnSegment (BigIntPoint p, BigIntPoint q, BigIntPoint r) z = a && b && c && d; - } catch (HomomorphicException | IOException | ClassNotFoundException e){ + } + catch (HomomorphicException | IOException | ClassNotFoundException e){ System.err.println("Exception in OnSegment" + e.getMessage()); } - return z; } diff --git a/src/main/java/PathsAlice.java b/src/main/java/PathsAlice.java index dffec96..53249ec 100644 --- a/src/main/java/PathsAlice.java +++ b/src/main/java/PathsAlice.java @@ -1,3 +1,4 @@ +import org.apache.commons.io.serialization.ValidatingObjectInputStream; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import security.misc.HomomorphicException; @@ -6,7 +7,6 @@ import java.io.File; import java.io.IOException; -import java.io.ObjectInputStream; import java.math.BigInteger; import java.net.Socket; import java.util.ArrayList; @@ -51,7 +51,7 @@ public static void main(String[] args) { Socket socket = new Socket (ip_address, pathsalice.port); alice.set_socket(socket); alice.receivePublicKeys(); - ObjectInputStream input = new ObjectInputStream(socket.getInputStream()); + ValidatingObjectInputStream input = get_ois(socket); Object object = input.readObject(); if (object instanceof List) { @@ -80,4 +80,18 @@ public static void main(String[] args) { logger.fatal(e.getStackTrace()); } } + + public static ValidatingObjectInputStream get_ois(Socket socket) throws IOException { + ValidatingObjectInputStream ois = new ValidatingObjectInputStream(socket.getInputStream()); + ois.accept( + java.util.List.class, + BigIntPoint.class, + java.lang.Number.class, + java.math.BigInteger.class + ); + ois.accept("[B"); + ois.accept("[L*"); + return ois; + } + } diff --git a/src/test/java/IntersectTest.java b/src/test/java/IntersectTest.java index 6f97d90..b688eac 100644 --- a/src/test/java/IntersectTest.java +++ b/src/test/java/IntersectTest.java @@ -1,3 +1,5 @@ +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.junit.Test; import org.junit.Before; import static org.junit.Assert.assertEquals; @@ -21,6 +23,7 @@ import java.util.List; public class IntersectTest { + private static final Logger logger = LogManager.getLogger(IntersectTest.class); private static KeyPair dgk = null; private static KeyPair paillier = null; @@ -69,11 +72,11 @@ public void encrypted_test_intersections() { System.out.println("Reading the file: " + ownroute); boolean will_collide = Boolean.parseBoolean(expected_result); - //Parsing routes + // Parsing routes List ownroute_list = CleartextPathsComparison.read_all_paths(ownroute); List cryptroute_list = CleartextPathsComparison.read_all_paths(cryptroute); - //encrypt routes + // Encrypt routes List encryptedownroute_list = new ArrayList<>(); List encryptedcryptroute_list = new ArrayList<>(); @@ -111,7 +114,7 @@ public void encrypted_test_intersections() { } } catch (IOException | ClassNotFoundException | HomomorphicException | InterruptedException e) { - System.err.println("Error reading files" + e.getMessage()); + logger.fatal(e.getStackTrace()); } } @@ -129,12 +132,14 @@ public static Socket connectWithRetry(String host, int port) { // Connection refused, print a message and retry after a delay System.out.println("Connection refused. Retrying in 1 second..."); try { - Thread.sleep(1000); // Wait for 5 seconds before retrying - } catch (InterruptedException ex) { - ex.printStackTrace(); + Thread.sleep(1000); // Wait for 1 second before retrying + } + catch (InterruptedException ex) { + logger.info(ex.getStackTrace()); } - } catch (Exception e) { - e.printStackTrace(); + } + catch (Exception e) { + logger.fatal(e.getStackTrace()); } } }