From 2cd6b648b72f3a89b6f9c4231fa64b824a313464 Mon Sep 17 00:00:00 2001 From: Shivanand Yadav Date: Sun, 13 Apr 2025 20:40:12 +0530 Subject: [PATCH] Convert OptionalUtilTest, Uint64UtilTest and UuidUtilTest classes to Kotlin --- .../api/util/OptionalUtilTest.java | 73 -------------- .../api/util/OptionalUtilTest.kt | 74 +++++++++++++++ .../api/util/Uint64UtilTest.java | 95 ------------------- .../signalservice/api/util/Uint64UtilTest.kt | 84 ++++++++++++++++ .../signalservice/api/util/UuidUtilTest.java | 60 ------------ .../signalservice/api/util/UuidUtilTest.kt | 51 ++++++++++ 6 files changed, 209 insertions(+), 228 deletions(-) delete mode 100644 libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/OptionalUtilTest.java create mode 100644 libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/OptionalUtilTest.kt delete mode 100644 libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/Uint64UtilTest.java create mode 100644 libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/Uint64UtilTest.kt delete mode 100644 libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/UuidUtilTest.java create mode 100644 libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/UuidUtilTest.kt diff --git a/libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/OptionalUtilTest.java b/libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/OptionalUtilTest.java deleted file mode 100644 index f858a76517d..00000000000 --- a/libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/OptionalUtilTest.java +++ /dev/null @@ -1,73 +0,0 @@ -package org.whispersystems.signalservice.api.util; - -import org.junit.Test; - -import java.util.Optional; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; - -public final class OptionalUtilTest { - - @Test - public void absent_are_equal() { - assertTrue(OptionalUtil.byteArrayEquals(Optional.empty(), Optional.empty())); - } - - @Test - public void first_non_absent_not_equal() { - assertFalse(OptionalUtil.byteArrayEquals(Optional.of(new byte[1]), Optional.empty())); - } - - @Test - public void second_non_absent_not_equal() { - assertFalse(OptionalUtil.byteArrayEquals(Optional.empty(), Optional.of(new byte[1]))); - } - - @Test - public void equal_contents() { - byte[] contentsA = new byte[]{1, 2, 3}; - byte[] contentsB = contentsA.clone(); - Optional a = Optional.of(contentsA); - Optional b = Optional.of(contentsB); - assertTrue(OptionalUtil.byteArrayEquals(a, b)); - assertEquals(OptionalUtil.byteArrayHashCode(a), OptionalUtil.byteArrayHashCode(b)); - } - - @Test - public void in_equal_contents() { - byte[] contentsA = new byte[]{1, 2, 3}; - byte[] contentsB = new byte[]{4, 5, 6}; - Optional a = Optional.of(contentsA); - Optional b = Optional.of(contentsB); - assertFalse(OptionalUtil.byteArrayEquals(a, b)); - assertNotEquals(OptionalUtil.byteArrayHashCode(a), OptionalUtil.byteArrayHashCode(b)); - } - - @Test - public void hash_code_absent() { - assertEquals(0, OptionalUtil.byteArrayHashCode(Optional.empty())); - } - - @Test - public void or_singleAbsent() { - assertFalse(OptionalUtil.or(Optional.empty()).isPresent()); - } - - @Test - public void or_multipleAbsent() { - assertFalse(OptionalUtil.or(Optional.empty(), Optional.empty()).isPresent()); - } - - @Test - public void or_firstHasValue() { - assertEquals(5, OptionalUtil.or(Optional.of(5), Optional.empty()).get().longValue()); - } - - @Test - public void or_secondHasValue() { - assertEquals(5, OptionalUtil.or(Optional.empty(), Optional.of(5)).get().longValue()); - } -} \ No newline at end of file diff --git a/libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/OptionalUtilTest.kt b/libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/OptionalUtilTest.kt new file mode 100644 index 00000000000..ac225c5c746 --- /dev/null +++ b/libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/OptionalUtilTest.kt @@ -0,0 +1,74 @@ +package org.whispersystems.signalservice.api.util + +import junit.framework.TestCase.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNotEquals +import org.junit.Assert.assertTrue +import org.junit.Test +import org.whispersystems.signalservice.api.util.OptionalUtil.byteArrayEquals +import org.whispersystems.signalservice.api.util.OptionalUtil.byteArrayHashCode +import org.whispersystems.signalservice.api.util.OptionalUtil.or +import java.util.Optional + +class OptionalUtilTest { + + @Test + fun absent_are_equal() { + assertTrue(byteArrayEquals(Optional.empty(), Optional.empty())) + } + + @Test + fun first_non_absent_not_equal() { + assertFalse(byteArrayEquals(Optional.of(ByteArray(1)), Optional.empty())) + } + + @Test + fun second_non_absent_not_equal() { + assertFalse(byteArrayEquals(Optional.empty(), Optional.of(ByteArray(1)))) + } + + @Test + fun equal_contents() { + val contentsA = byteArrayOf(1, 2, 3) + val contentsB = contentsA.copyOf() + val a: Optional = Optional.of(contentsA) + val b: Optional = Optional.of(contentsB) + assertTrue(byteArrayEquals(a, b)) + assertEquals(byteArrayHashCode(a), byteArrayHashCode(b)) + } + + @Test + fun in_equal_contents() { + val contentsA = byteArrayOf(1, 2, 3) + val contentsB = byteArrayOf(4, 5, 6) + val a: Optional = Optional.of(contentsA) + val b: Optional = Optional.of(contentsB) + assertFalse(byteArrayEquals(a, b)) + assertNotEquals(byteArrayHashCode(a), byteArrayHashCode(b)) + } + + @Test + fun hash_code_absent() { + assertEquals(0, byteArrayHashCode(Optional.empty())) + } + + @Test + fun or_singleAbsent() { + assertFalse(or(Optional.empty()).isPresent) + } + + @Test + fun or_multipleAbsent() { + assertFalse(or(Optional.empty(), Optional.empty()).isPresent) + } + + @Test + fun or_firstHasValue() { + assertEquals(5, or(Optional.of(5), Optional.empty()).get()) + } + + @Test + fun or_secondHasValue() { + assertEquals(5, or(Optional.empty(), Optional.of(5)).get()) + } +} diff --git a/libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/Uint64UtilTest.java b/libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/Uint64UtilTest.java deleted file mode 100644 index 7976651ad92..00000000000 --- a/libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/Uint64UtilTest.java +++ /dev/null @@ -1,95 +0,0 @@ -package org.whispersystems.signalservice.api.util; - -import org.junit.Test; - -import java.math.BigInteger; - -import static org.junit.Assert.assertEquals; -import static org.whispersystems.signalservice.api.util.Uint64Util.bigIntegerToUInt64; -import static org.whispersystems.signalservice.api.util.Uint64Util.uint64ToBigInteger; - -public final class Uint64UtilTest { - - @Test - public void long_zero_to_bigInteger() { - BigInteger bigInteger = uint64ToBigInteger(0); - - assertEquals("0", bigInteger.toString()); - } - - @Test - public void long_to_bigInteger() { - BigInteger bigInteger = uint64ToBigInteger(12345L); - - assertEquals("12345", bigInteger.toString()); - } - - @Test - public void bigInteger_zero_to_long() throws Uint64RangeException { - long uint64 = bigIntegerToUInt64(BigInteger.ZERO); - - assertEquals(0, uint64); - } - - @Test - public void first_uint64_value_to_bigInteger() { - BigInteger bigInteger = uint64ToBigInteger(0x8000000000000000L); - - assertEquals("9223372036854775808", bigInteger.toString()); - } - - @Test - public void bigInteger_to_first_uint64_value() throws Uint64RangeException { - long uint64 = bigIntegerToUInt64(new BigInteger("9223372036854775808")); - - assertEquals(0x8000000000000000L, uint64); - } - - @Test - public void large_uint64_value_to_bigInteger() { - BigInteger bigInteger = uint64ToBigInteger(0xa523f21e412c14d2L); - - assertEquals("11899620852199331026", bigInteger.toString()); - } - - @Test - public void bigInteger_to_large_uint64_value() throws Uint64RangeException { - long uint64 = bigIntegerToUInt64(new BigInteger("11899620852199331026")); - - assertEquals(0xa523f21e412c14d2L, uint64); - } - - @Test - public void largest_uint64_value_to_bigInteger() { - BigInteger bigInteger = uint64ToBigInteger(0xffffffffffffffffL); - - assertEquals("18446744073709551615", bigInteger.toString()); - } - - @Test - public void bigInteger_to_largest_uint64_value() throws Uint64RangeException { - long uint64 = bigIntegerToUInt64(new BigInteger("18446744073709551615")); - - assertEquals(0xffffffffffffffffL, uint64); - } - - @Test(expected = Uint64RangeException.class) - public void too_big_by_one() throws Uint64RangeException { - bigIntegerToUInt64(new BigInteger("18446744073709551616")); - } - - @Test(expected = Uint64RangeException.class) - public void too_small_by_one() throws Uint64RangeException { - bigIntegerToUInt64(new BigInteger("-1")); - } - - @Test(expected = Uint64RangeException.class) - public void too_big_by_a_lot() throws Uint64RangeException { - bigIntegerToUInt64(new BigInteger("1844674407370955161623")); - } - - @Test(expected = Uint64RangeException.class) - public void too_small_by_a_lot() throws Uint64RangeException { - bigIntegerToUInt64(new BigInteger("-1844674407370955161623")); - } -} diff --git a/libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/Uint64UtilTest.kt b/libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/Uint64UtilTest.kt new file mode 100644 index 00000000000..345ceaa14ac --- /dev/null +++ b/libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/Uint64UtilTest.kt @@ -0,0 +1,84 @@ +package org.whispersystems.signalservice.api.util + +import junit.framework.TestCase.assertEquals +import org.junit.Test +import org.whispersystems.signalservice.api.util.Uint64Util.bigIntegerToUInt64 +import org.whispersystems.signalservice.api.util.Uint64Util.uint64ToBigInteger +import java.math.BigInteger + +class Uint64UtilTest { + + @Test + fun long_zero_to_bigInteger() { + val bigInteger = uint64ToBigInteger(0) + assertEquals("0", bigInteger.toString()) + } + + @Test + fun long_to_bigInteger() { + val bigInteger = uint64ToBigInteger(12345L) + assertEquals("12345", bigInteger.toString()) + } + + @Test + fun bigInteger_zero_to_long() { + val uint64 = bigIntegerToUInt64(BigInteger.ZERO) + assertEquals(0L, uint64) + } + + @Test + fun first_uint64_value_to_bigInteger() { + val bigInteger = uint64ToBigInteger(0x8000000000000000UL.toLong()) + assertEquals("9223372036854775808", bigInteger.toString()) + } + + @Test + fun bigInteger_to_first_uint64_value() { + val uint64 = bigIntegerToUInt64(BigInteger("9223372036854775808")) + assertEquals(0x8000000000000000UL.toLong(), uint64) + } + + @Test + fun large_uint64_value_to_bigInteger() { + val bigInteger = uint64ToBigInteger(0xa523f21e412c14d2UL.toLong()) + assertEquals("11899620852199331026", bigInteger.toString()) + } + + @Test + fun bigInteger_to_large_uint64_value() { + val uint64 = bigIntegerToUInt64(BigInteger("11899620852199331026")) + assertEquals(0xa523f21e412c14d2UL.toLong(), uint64) + } + + @Test + fun largest_uint64_value_to_bigInteger() { + val bigInteger = uint64ToBigInteger(0xffffffffffffffffUL.toLong()) + assertEquals("18446744073709551615", bigInteger.toString()) + } + + @Test + fun bigInteger_to_largest_uint64_value() { + val uint64 = bigIntegerToUInt64(BigInteger("18446744073709551615")) + assertEquals(0xffffffffffffffffUL.toLong(), uint64) + } + + @Test(expected = Uint64RangeException::class) + fun too_big_by_one() { + bigIntegerToUInt64(BigInteger("18446744073709551616")) + } + + @Test(expected = Uint64RangeException::class) + fun too_small_by_one() { + bigIntegerToUInt64(BigInteger("-1")) + } + + @Test(expected = Uint64RangeException::class) + fun too_big_by_a_lot() { + bigIntegerToUInt64(BigInteger("1844674407370955161623")) + } + + @Test(expected = Uint64RangeException::class) + fun too_small_by_a_lot() { + bigIntegerToUInt64(BigInteger("-1844674407370955161623")) + } +} diff --git a/libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/UuidUtilTest.java b/libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/UuidUtilTest.java deleted file mode 100644 index 624e3f4521e..00000000000 --- a/libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/UuidUtilTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package org.whispersystems.signalservice.api.util; - -import org.junit.Test; -import org.signal.libsignal.protocol.util.Hex; - -import java.io.IOException; -import java.util.UUID; - -import okio.ByteString; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; - -public final class UuidUtilTest { - - @Test - public void toByteArray() throws IOException { - UUID uuid = UUID.fromString("67dfd496-ea02-4720-b13d-83a462168b1d"); - - byte[] serialized = UuidUtil.toByteArray(uuid); - - assertArrayEquals(Hex.fromStringCondensed("67dfd496ea024720b13d83a462168b1d"), serialized); - } - - @Test - public void toByteArray_alternativeValues() throws IOException { - UUID uuid = UUID.fromString("b70df6ac-3b21-4b39-a514-613561f51e2a"); - - byte[] serialized = UuidUtil.toByteArray(uuid); - - assertArrayEquals(Hex.fromStringCondensed("b70df6ac3b214b39a514613561f51e2a"), serialized); - } - - @Test - public void parseOrThrow_from_byteArray() throws IOException { - byte[] bytes = Hex.fromStringCondensed("3dc48790568b49c19bd6ab6604a5bc32"); - - UUID uuid = UuidUtil.parseOrThrow(bytes); - - assertEquals("3dc48790-568b-49c1-9bd6-ab6604a5bc32", uuid.toString()); - } - - @Test - public void parseOrThrow_from_byteArray_alternativeValues() throws IOException { - byte[] bytes = Hex.fromStringCondensed("b83dfb0b67f141aa992e030c167cd011"); - - UUID uuid = UuidUtil.parseOrThrow(bytes); - - assertEquals("b83dfb0b-67f1-41aa-992e-030c167cd011", uuid.toString()); - } - - @Test - public void byte_string_round_trip() { - UUID uuid = UUID.fromString("67dfd496-ea02-4720-b13d-83a462168b1d"); - - UUID result = UuidUtil.fromByteString(ByteString.of(UuidUtil.toByteArray(uuid))); - - assertEquals(uuid, result); - } -} diff --git a/libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/UuidUtilTest.kt b/libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/UuidUtilTest.kt new file mode 100644 index 00000000000..a4187b4fb86 --- /dev/null +++ b/libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/UuidUtilTest.kt @@ -0,0 +1,51 @@ +package org.whispersystems.signalservice.api.util + +import okio.ByteString +import org.junit.Assert.assertArrayEquals +import org.junit.Assert.assertEquals +import org.junit.Test +import org.signal.libsignal.protocol.util.Hex +import java.io.IOException +import java.util.UUID + +class UuidUtilTest { + + @Test + @Throws(IOException::class) + fun toByteArray() { + val uuid = UUID.fromString("67dfd496-ea02-4720-b13d-83a462168b1d") + val serialized = UuidUtil.toByteArray(uuid) + assertArrayEquals(Hex.fromStringCondensed("67dfd496ea024720b13d83a462168b1d"), serialized) + } + + @Test + @Throws(IOException::class) + fun toByteArray_alternativeValues() { + val uuid = UUID.fromString("b70df6ac-3b21-4b39-a514-613561f51e2a") + val serialized = UuidUtil.toByteArray(uuid) + assertArrayEquals(Hex.fromStringCondensed("b70df6ac3b214b39a514613561f51e2a"), serialized) + } + + @Test + @Throws(IOException::class) + fun parseOrThrow_from_byteArray() { + val bytes = Hex.fromStringCondensed("3dc48790568b49c19bd6ab6604a5bc32") + val uuid = UuidUtil.parseOrThrow(bytes) + assertEquals("3dc48790-568b-49c1-9bd6-ab6604a5bc32", uuid.toString()) + } + + @Test + @Throws(IOException::class) + fun parseOrThrow_from_byteArray_alternativeValues() { + val bytes = Hex.fromStringCondensed("b83dfb0b67f141aa992e030c167cd011") + val uuid = UuidUtil.parseOrThrow(bytes) + assertEquals("b83dfb0b-67f1-41aa-992e-030c167cd011", uuid.toString()) + } + + @Test + fun byte_string_round_trip() { + val uuid = UUID.fromString("67dfd496-ea02-4720-b13d-83a462168b1d") + val result = UuidUtil.fromByteString(ByteString.of(*UuidUtil.toByteArray(uuid))) + assertEquals(uuid, result) + } +}