From dfe784581fc69af8fa36108ba3d320567496840f Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 16 Nov 2018 10:58:48 +0000 Subject: [PATCH] Allow zero in length validation. return boolean for checks. --- .../quorum/tessera/node/PartyInfoParser.java | 16 ++--- .../node/PartyInfoParserException.java | 13 ---- .../node/PartyInfoParserExceptionTest.java | 19 ------ .../tessera/node/PartyInfoParserTest.java | 63 ++++++++++++++----- 4 files changed, 55 insertions(+), 56 deletions(-) delete mode 100644 tessera-core/src/main/java/com/quorum/tessera/node/PartyInfoParserException.java delete mode 100644 tessera-core/src/test/java/com/quorum/tessera/node/PartyInfoParserExceptionTest.java diff --git a/tessera-core/src/main/java/com/quorum/tessera/node/PartyInfoParser.java b/tessera-core/src/main/java/com/quorum/tessera/node/PartyInfoParser.java index 5da1db78cf..e5970276e4 100644 --- a/tessera-core/src/main/java/com/quorum/tessera/node/PartyInfoParser.java +++ b/tessera-core/src/main/java/com/quorum/tessera/node/PartyInfoParser.java @@ -144,18 +144,18 @@ static PartyInfoParser create() { } - static void checkLength(long value) { - Optional.of(value) - .filter(v -> v > 0) + static boolean checkLength(long value) { + return Optional.of(value) + .filter(v -> v >= 0) .filter(v -> v < Long.MAX_VALUE - 1) - .orElseThrow(() -> new PartyInfoParserException("Invalid length "+ value)); + .isPresent(); } - static void checkLength(int value) { - Optional.of(value) - .filter(v -> v > 0) + static boolean checkLength(int value) { + return Optional.of(value) + .filter(v -> v >= 0) .filter(v -> v < Integer.MAX_VALUE - 1) - .orElseThrow(() -> new PartyInfoParserException("Invalid length "+ value)); + .isPresent(); } } diff --git a/tessera-core/src/main/java/com/quorum/tessera/node/PartyInfoParserException.java b/tessera-core/src/main/java/com/quorum/tessera/node/PartyInfoParserException.java deleted file mode 100644 index 1c2b28b28c..0000000000 --- a/tessera-core/src/main/java/com/quorum/tessera/node/PartyInfoParserException.java +++ /dev/null @@ -1,13 +0,0 @@ - -package com.quorum.tessera.node; - -import com.quorum.tessera.exception.TesseraException; - - -public class PartyInfoParserException extends TesseraException { - - public PartyInfoParserException(String message) { - super(message); - } - -} diff --git a/tessera-core/src/test/java/com/quorum/tessera/node/PartyInfoParserExceptionTest.java b/tessera-core/src/test/java/com/quorum/tessera/node/PartyInfoParserExceptionTest.java deleted file mode 100644 index 5884fb73d0..0000000000 --- a/tessera-core/src/test/java/com/quorum/tessera/node/PartyInfoParserExceptionTest.java +++ /dev/null @@ -1,19 +0,0 @@ - -package com.quorum.tessera.node; - -import static org.assertj.core.api.Assertions.assertThat; -import org.junit.Test; - - -public class PartyInfoParserExceptionTest { - - @Test - public void createWithMessage() { - - PartyInfoParserException result = new PartyInfoParserException("OUCH"); - - assertThat(result).hasMessage("OUCH"); - - } - -} diff --git a/tessera-core/src/test/java/com/quorum/tessera/node/PartyInfoParserTest.java b/tessera-core/src/test/java/com/quorum/tessera/node/PartyInfoParserTest.java index c6a21cf06d..cc3c7e924b 100644 --- a/tessera-core/src/test/java/com/quorum/tessera/node/PartyInfoParserTest.java +++ b/tessera-core/src/test/java/com/quorum/tessera/node/PartyInfoParserTest.java @@ -9,6 +9,7 @@ import java.nio.ByteBuffer; import java.util.Base64; +import java.util.Random; import static org.assertj.core.api.Assertions.assertThat; @@ -164,34 +165,64 @@ public void toUsingSameInfoFromFixture() { } - @Test(expected = PartyInfoParserException.class) - public void checkLengthZero() { - PartyInfoParser.checkLength(0); + @Test + public void zeroAllows() { + assertThat(PartyInfoParser.checkLength(0)).isTrue(); } - @Test(expected = PartyInfoParserException.class) - public void checkLengthMinusValue() { - PartyInfoParser.checkLength(-1); + @Test + public void negativeValuesNotAllowed() { + + new Random() + .ints(10) + .forEach(n -> { + int input = Integer.signum(n) == 1 ? Math.negateExact(n) : n; + boolean result = PartyInfoParser.checkLength(input); + assertThat(result).isFalse(); + + }); } - @Test(expected = PartyInfoParserException.class) - public void checkLengthZeroLong() { - PartyInfoParser.checkLength((long) 0); + @Test + public void positiveValuesAllowed() { + + new Random() + .ints(10) + .map(Math::abs) + .forEach(n -> { + + assertThat(PartyInfoParser.checkLength(n)) + .isTrue(); + + }); + } - @Test(expected = PartyInfoParserException.class) + @Test public void checkLengthMaxValue() { - PartyInfoParser.checkLength(Integer.MAX_VALUE); + assertThat(PartyInfoParser.checkLength(Integer.MAX_VALUE)).isFalse(); + } - @Test(expected = PartyInfoParserException.class) - public void checkLengthMinusValueLong() { - PartyInfoParser.checkLength((long) -1); + @Test + public void negativeLOngValuesNotAllows() { + + new Random() + .ints(10) + .map(Math::negateExact) + .asLongStream() + .forEach(n -> { + + long input = Long.signum(n) == 1 ? Math.negateExact(n) : n; + boolean result = PartyInfoParser.checkLength(input); + assertThat(result).isFalse(); + }); } - @Test(expected = PartyInfoParserException.class) + @Test public void checkLengthMaxValueLong() { - PartyInfoParser.checkLength(Long.MAX_VALUE); + assertThat(PartyInfoParser.checkLength(Long.MAX_VALUE)).isFalse(); + } }