parent
9e49dd1270
commit
cee44fcc93
17 changed files with 479 additions and 532 deletions
|
@ -1,43 +0,0 @@
|
|||
/*
|
||||
* Copyright 2023 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.signal.core.util.logging;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public final class LogTest {
|
||||
|
||||
@Test
|
||||
public void tag_short_class_name() {
|
||||
assertEquals("MyClass", Log.tag(MyClass.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tag_23_character_class_name() {
|
||||
String tag = Log.tag(TwentyThreeCharacters23.class);
|
||||
Assert.assertEquals("TwentyThreeCharacters23", tag);
|
||||
Assert.assertEquals(23, tag.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tag_24_character_class_name() {
|
||||
Assert.assertEquals(24, TwentyFour24Characters24.class.getSimpleName().length());
|
||||
String tag = Log.tag(TwentyFour24Characters24.class);
|
||||
Assert.assertEquals("TwentyFour24Characters2", tag);
|
||||
assertEquals(23, Log.tag(TwentyThreeCharacters23.class).length());
|
||||
}
|
||||
|
||||
private class MyClass {
|
||||
}
|
||||
|
||||
private class TwentyThreeCharacters23 {
|
||||
}
|
||||
|
||||
private class TwentyFour24Characters24 {
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Copyright 2023 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
package org.signal.core.util.logging
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class LogTest {
|
||||
@Test
|
||||
fun tag_short_class_name() {
|
||||
assertEquals("MyClass", Log.tag(MyClass::class))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun tag_23_character_class_name() {
|
||||
val tag = Log.tag(TwentyThreeCharacters23::class)
|
||||
assertEquals("TwentyThreeCharacters23", tag)
|
||||
assertEquals(23, tag.length)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun tag_24_character_class_name() {
|
||||
assertEquals(24, TwentyFour24Characters24::class.simpleName!!.length)
|
||||
val tag = Log.tag(TwentyFour24Characters24::class)
|
||||
assertEquals("TwentyFour24Characters2", tag)
|
||||
assertEquals(23, Log.tag(TwentyThreeCharacters23::class).length)
|
||||
}
|
||||
|
||||
private inner class MyClass
|
||||
|
||||
private inner class TwentyThreeCharacters23
|
||||
|
||||
private inner class TwentyFour24Characters24
|
||||
}
|
|
@ -1,104 +0,0 @@
|
|||
package org.signal.core.util;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public final class BreakIteratorCompatTest {
|
||||
|
||||
@Test
|
||||
public void empty() {
|
||||
BreakIteratorCompat breakIterator = BreakIteratorCompat.getInstance();
|
||||
breakIterator.setText("");
|
||||
|
||||
assertEquals(BreakIteratorCompat.DONE, breakIterator.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void single() {
|
||||
BreakIteratorCompat breakIterator = BreakIteratorCompat.getInstance();
|
||||
breakIterator.setText("a");
|
||||
|
||||
assertEquals(1, breakIterator.next());
|
||||
assertEquals(BreakIteratorCompat.DONE, breakIterator.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void count_empty() {
|
||||
BreakIteratorCompat breakIterator = BreakIteratorCompat.getInstance();
|
||||
breakIterator.setText("");
|
||||
|
||||
assertEquals(0, breakIterator.countBreaks());
|
||||
assertEquals(BreakIteratorCompat.DONE, breakIterator.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void count_simple_text() {
|
||||
BreakIteratorCompat breakIterator = BreakIteratorCompat.getInstance();
|
||||
breakIterator.setText("abc");
|
||||
|
||||
assertEquals(3, breakIterator.countBreaks());
|
||||
assertEquals(BreakIteratorCompat.DONE, breakIterator.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void two_counts() {
|
||||
BreakIteratorCompat breakIterator = BreakIteratorCompat.getInstance();
|
||||
breakIterator.setText("abc");
|
||||
|
||||
assertEquals(3, breakIterator.countBreaks());
|
||||
assertEquals(BreakIteratorCompat.DONE, breakIterator.next());
|
||||
assertEquals(3, breakIterator.countBreaks());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void count_multi_character_graphemes() {
|
||||
String hindi = "समाजो गयेग";
|
||||
|
||||
BreakIteratorCompat breakIterator = BreakIteratorCompat.getInstance();
|
||||
breakIterator.setText(hindi);
|
||||
|
||||
assertEquals(7, breakIterator.countBreaks());
|
||||
assertEquals(BreakIteratorCompat.DONE, breakIterator.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void iterate_multi_character_graphemes() {
|
||||
String hindi = "समाजो गयेग";
|
||||
|
||||
BreakIteratorCompat breakIterator = BreakIteratorCompat.getInstance();
|
||||
breakIterator.setText(hindi);
|
||||
|
||||
assertEquals(asList("स", "मा", "जो", " ", "ग", "ये", "ग"), toList(breakIterator));
|
||||
assertEquals(BreakIteratorCompat.DONE, breakIterator.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void split_multi_character_graphemes() {
|
||||
String hindi = "समाजो गयेग";
|
||||
|
||||
BreakIteratorCompat breakIterator = BreakIteratorCompat.getInstance();
|
||||
breakIterator.setText(hindi);
|
||||
|
||||
assertEquals("समाजो गयेग", breakIterator.take(8));
|
||||
assertEquals("समाजो गयेग", breakIterator.take(7));
|
||||
assertEquals("समाजो गये", breakIterator.take(6));
|
||||
assertEquals("समाजो ग", breakIterator.take(5));
|
||||
assertEquals("समाजो ", breakIterator.take(4));
|
||||
assertEquals("समाजो", breakIterator.take(3));
|
||||
assertEquals("समा", breakIterator.take(2));
|
||||
assertEquals("स", breakIterator.take(1));
|
||||
assertEquals("", breakIterator.take(0));
|
||||
assertEquals("", breakIterator.take(-1));
|
||||
}
|
||||
|
||||
private List<CharSequence> toList(BreakIteratorCompat breakIterator) {
|
||||
List<CharSequence> list = new ArrayList<>();
|
||||
breakIterator.forEach(list::add);
|
||||
return list;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
package org.signal.core.util
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class BreakIteratorCompatTest {
|
||||
@Test
|
||||
fun empty() {
|
||||
val breakIterator = BreakIteratorCompat.getInstance()
|
||||
breakIterator.setText("")
|
||||
|
||||
assertEquals(BreakIteratorCompat.DONE, breakIterator.next())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun single() {
|
||||
val breakIterator = BreakIteratorCompat.getInstance()
|
||||
breakIterator.setText("a")
|
||||
|
||||
assertEquals(1, breakIterator.next())
|
||||
assertEquals(BreakIteratorCompat.DONE, breakIterator.next())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun count_empty() {
|
||||
val breakIterator = BreakIteratorCompat.getInstance()
|
||||
breakIterator.setText("")
|
||||
|
||||
assertEquals(0, breakIterator.countBreaks())
|
||||
assertEquals(BreakIteratorCompat.DONE, breakIterator.next())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun count_simple_text() {
|
||||
val breakIterator = BreakIteratorCompat.getInstance()
|
||||
breakIterator.setText("abc")
|
||||
|
||||
assertEquals(3, breakIterator.countBreaks())
|
||||
assertEquals(BreakIteratorCompat.DONE, breakIterator.next())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun two_counts() {
|
||||
val breakIterator = BreakIteratorCompat.getInstance()
|
||||
breakIterator.setText("abc")
|
||||
|
||||
assertEquals(3, breakIterator.countBreaks())
|
||||
assertEquals(BreakIteratorCompat.DONE, breakIterator.next())
|
||||
assertEquals(3, breakIterator.countBreaks())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun count_multi_character_graphemes() {
|
||||
val hindi = "समाजो गयेग"
|
||||
|
||||
val breakIterator = BreakIteratorCompat.getInstance()
|
||||
breakIterator.setText(hindi)
|
||||
|
||||
assertEquals(7, breakIterator.countBreaks())
|
||||
assertEquals(BreakIteratorCompat.DONE, breakIterator.next())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun iterate_multi_character_graphemes() {
|
||||
val hindi = "समाजो गयेग"
|
||||
|
||||
val breakIterator = BreakIteratorCompat.getInstance()
|
||||
breakIterator.setText(hindi)
|
||||
|
||||
assertEquals(listOf("स", "मा", "जो", " ", "ग", "ये", "ग"), breakIterator.toList())
|
||||
assertEquals(BreakIteratorCompat.DONE, breakIterator.next())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun split_multi_character_graphemes() {
|
||||
val hindi = "समाजो गयेग"
|
||||
|
||||
val breakIterator = BreakIteratorCompat.getInstance()
|
||||
breakIterator.setText(hindi)
|
||||
|
||||
assertEquals("समाजो गयेग", breakIterator.take(8))
|
||||
assertEquals("समाजो गयेग", breakIterator.take(7))
|
||||
assertEquals("समाजो गये", breakIterator.take(6))
|
||||
assertEquals("समाजो ग", breakIterator.take(5))
|
||||
assertEquals("समाजो ", breakIterator.take(4))
|
||||
assertEquals("समाजो", breakIterator.take(3))
|
||||
assertEquals("समा", breakIterator.take(2))
|
||||
assertEquals("स", breakIterator.take(1))
|
||||
assertEquals("", breakIterator.take(0))
|
||||
assertEquals("", breakIterator.take(-1))
|
||||
}
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
package org.signal.core.util;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ListUtilTest {
|
||||
|
||||
@Test
|
||||
public void chunk_oneChunk() {
|
||||
List<String> input = Arrays.asList("A", "B", "C");
|
||||
|
||||
List<List<String>> output = ListUtil.chunk(input, 3);
|
||||
assertEquals(1, output.size());
|
||||
assertEquals(input, output.get(0));
|
||||
|
||||
output = ListUtil.chunk(input, 4);
|
||||
assertEquals(1, output.size());
|
||||
assertEquals(input, output.get(0));
|
||||
|
||||
output = ListUtil.chunk(input, 100);
|
||||
assertEquals(1, output.size());
|
||||
assertEquals(input, output.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void chunk_multipleChunks() {
|
||||
List<String> input = Arrays.asList("A", "B", "C", "D", "E");
|
||||
|
||||
List<List<String>> output = ListUtil.chunk(input, 4);
|
||||
assertEquals(2, output.size());
|
||||
assertEquals(Arrays.asList("A", "B", "C", "D"), output.get(0));
|
||||
assertEquals(Arrays.asList("E"), output.get(1));
|
||||
|
||||
output = ListUtil.chunk(input, 2);
|
||||
assertEquals(3, output.size());
|
||||
assertEquals(Arrays.asList("A", "B"), output.get(0));
|
||||
assertEquals(Arrays.asList("C", "D"), output.get(1));
|
||||
assertEquals(Arrays.asList("E"), output.get(2));
|
||||
|
||||
output = ListUtil.chunk(input, 1);
|
||||
assertEquals(5, output.size());
|
||||
assertEquals(Arrays.asList("A"), output.get(0));
|
||||
assertEquals(Arrays.asList("B"), output.get(1));
|
||||
assertEquals(Arrays.asList("C"), output.get(2));
|
||||
assertEquals(Arrays.asList("D"), output.get(3));
|
||||
assertEquals(Arrays.asList("E"), output.get(4));
|
||||
}
|
||||
}
|
47
core-util/src/test/java/org/signal/core/util/ListUtilTest.kt
Normal file
47
core-util/src/test/java/org/signal/core/util/ListUtilTest.kt
Normal file
|
@ -0,0 +1,47 @@
|
|||
package org.signal.core.util
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class ListUtilTest {
|
||||
@Test
|
||||
fun chunk_oneChunk() {
|
||||
val input = listOf("A", "B", "C")
|
||||
|
||||
var output = ListUtil.chunk(input, 3)
|
||||
assertEquals(1, output.size)
|
||||
assertEquals(input, output[0])
|
||||
|
||||
output = ListUtil.chunk(input, 4)
|
||||
assertEquals(1, output.size)
|
||||
assertEquals(input, output[0])
|
||||
|
||||
output = ListUtil.chunk(input, 100)
|
||||
assertEquals(1, output.size)
|
||||
assertEquals(input, output[0])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun chunk_multipleChunks() {
|
||||
val input: List<String> = listOf("A", "B", "C", "D", "E")
|
||||
|
||||
var output = ListUtil.chunk(input, 4)
|
||||
assertEquals(2, output.size)
|
||||
assertEquals(listOf("A", "B", "C", "D"), output[0])
|
||||
assertEquals(listOf("E"), output[1])
|
||||
|
||||
output = ListUtil.chunk(input, 2)
|
||||
assertEquals(3, output.size)
|
||||
assertEquals(listOf("A", "B"), output[0])
|
||||
assertEquals(listOf("C", "D"), output[1])
|
||||
assertEquals(listOf("E"), output[2])
|
||||
|
||||
output = ListUtil.chunk(input, 1)
|
||||
assertEquals(5, output.size)
|
||||
assertEquals(listOf("A"), output[0])
|
||||
assertEquals(listOf("B"), output[1])
|
||||
assertEquals(listOf("C"), output[2])
|
||||
assertEquals(listOf("D"), output[3])
|
||||
assertEquals(listOf("E"), output[4])
|
||||
}
|
||||
}
|
|
@ -1,107 +0,0 @@
|
|||
package org.signal.core.util.money;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Currency;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class FiatMoneyTest {
|
||||
|
||||
@Test
|
||||
public void given100USD_whenIGetDefaultPrecisionString_thenIExpect100dot00() {
|
||||
// GIVEN
|
||||
FiatMoney fiatMoney = new FiatMoney(BigDecimal.valueOf(100), Currency.getInstance("USD"));
|
||||
|
||||
// WHEN
|
||||
String result = fiatMoney.getDefaultPrecisionString();
|
||||
|
||||
// THEN
|
||||
assertEquals("100.00", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given100USD_whenIGetMinimumUnitPrecisionString_thenIExpect10000() {
|
||||
// GIVEN
|
||||
FiatMoney fiatMoney = new FiatMoney(BigDecimal.valueOf(100), Currency.getInstance("USD"));
|
||||
|
||||
// WHEN
|
||||
String result = fiatMoney.getMinimumUnitPrecisionString();
|
||||
|
||||
// THEN
|
||||
assertEquals("10000", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given100JPY_whenIGetDefaultPrecisionString_thenIExpect100() {
|
||||
// GIVEN
|
||||
FiatMoney fiatMoney = new FiatMoney(BigDecimal.valueOf(100), Currency.getInstance("JPY"));
|
||||
|
||||
// WHEN
|
||||
String result = fiatMoney.getDefaultPrecisionString();
|
||||
|
||||
// THEN
|
||||
assertEquals("100", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given100JPY_whenIGetMinimumUnitPrecisionString_thenIExpect100() {
|
||||
// GIVEN
|
||||
FiatMoney fiatMoney = new FiatMoney(BigDecimal.valueOf(100), Currency.getInstance("JPY"));
|
||||
|
||||
// WHEN
|
||||
String result = fiatMoney.getMinimumUnitPrecisionString();
|
||||
|
||||
// THEN
|
||||
assertEquals("100", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given100UGX_whenIGetDefaultPrecisionString_thenIExpect100() {
|
||||
// GIVEN
|
||||
FiatMoney fiatMoney = new FiatMoney(BigDecimal.valueOf(100), Currency.getInstance("UGX"));
|
||||
|
||||
// WHEN
|
||||
String result = fiatMoney.getDefaultPrecisionString();
|
||||
|
||||
// THEN
|
||||
assertEquals("100", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given100UGX_whenIGetMinimumUnitPrecisionString_thenIExpect10000() {
|
||||
// GIVEN
|
||||
FiatMoney fiatMoney = new FiatMoney(BigDecimal.valueOf(100), Currency.getInstance("UGX"));
|
||||
|
||||
// WHEN
|
||||
String result = fiatMoney.getMinimumUnitPrecisionString();
|
||||
|
||||
// THEN
|
||||
assertEquals("10000", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given100ISK_whenIGetDefaultPrecisionString_thenIExpect100() {
|
||||
// GIVEN
|
||||
FiatMoney fiatMoney = new FiatMoney(BigDecimal.valueOf(100), Currency.getInstance("ISK"));
|
||||
|
||||
// WHEN
|
||||
String result = fiatMoney.getDefaultPrecisionString();
|
||||
|
||||
// THEN
|
||||
assertEquals("100", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given100ISK_whenIGetMinimumUnitPrecisionString_thenIExpect10000() {
|
||||
// GIVEN
|
||||
FiatMoney fiatMoney = new FiatMoney(BigDecimal.valueOf(100), Currency.getInstance("ISK"));
|
||||
|
||||
// WHEN
|
||||
String result = fiatMoney.getMinimumUnitPrecisionString();
|
||||
|
||||
// THEN
|
||||
assertEquals("10000", result);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
package org.signal.core.util.money
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import java.math.BigDecimal
|
||||
import java.util.Currency
|
||||
|
||||
class FiatMoneyTest {
|
||||
@Test
|
||||
fun given100USD_whenIGetDefaultPrecisionString_thenIExpect100dot00() {
|
||||
// GIVEN
|
||||
val fiatMoney = FiatMoney(BigDecimal.valueOf(100), Currency.getInstance("USD"))
|
||||
|
||||
// WHEN
|
||||
val result = fiatMoney.defaultPrecisionString
|
||||
|
||||
// THEN
|
||||
assertEquals("100.00", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun given100USD_whenIGetMinimumUnitPrecisionString_thenIExpect10000() {
|
||||
// GIVEN
|
||||
val fiatMoney = FiatMoney(BigDecimal.valueOf(100), Currency.getInstance("USD"))
|
||||
|
||||
// WHEN
|
||||
val result = fiatMoney.minimumUnitPrecisionString
|
||||
|
||||
// THEN
|
||||
assertEquals("10000", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun given100JPY_whenIGetDefaultPrecisionString_thenIExpect100() {
|
||||
// GIVEN
|
||||
val fiatMoney = FiatMoney(BigDecimal.valueOf(100), Currency.getInstance("JPY"))
|
||||
|
||||
// WHEN
|
||||
val result = fiatMoney.defaultPrecisionString
|
||||
|
||||
// THEN
|
||||
assertEquals("100", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun given100JPY_whenIGetMinimumUnitPrecisionString_thenIExpect100() {
|
||||
// GIVEN
|
||||
val fiatMoney = FiatMoney(BigDecimal.valueOf(100), Currency.getInstance("JPY"))
|
||||
|
||||
// WHEN
|
||||
val result = fiatMoney.minimumUnitPrecisionString
|
||||
|
||||
// THEN
|
||||
assertEquals("100", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun given100UGX_whenIGetDefaultPrecisionString_thenIExpect100() {
|
||||
// GIVEN
|
||||
val fiatMoney = FiatMoney(BigDecimal.valueOf(100), Currency.getInstance("UGX"))
|
||||
|
||||
// WHEN
|
||||
val result = fiatMoney.defaultPrecisionString
|
||||
|
||||
// THEN
|
||||
assertEquals("100", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun given100UGX_whenIGetMinimumUnitPrecisionString_thenIExpect10000() {
|
||||
// GIVEN
|
||||
val fiatMoney = FiatMoney(BigDecimal.valueOf(100), Currency.getInstance("UGX"))
|
||||
|
||||
// WHEN
|
||||
val result = fiatMoney.minimumUnitPrecisionString
|
||||
|
||||
// THEN
|
||||
assertEquals("10000", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun given100ISK_whenIGetDefaultPrecisionString_thenIExpect100() {
|
||||
// GIVEN
|
||||
val fiatMoney = FiatMoney(BigDecimal.valueOf(100), Currency.getInstance("ISK"))
|
||||
|
||||
// WHEN
|
||||
val result = fiatMoney.defaultPrecisionString
|
||||
|
||||
// THEN
|
||||
assertEquals("100", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun given100ISK_whenIGetMinimumUnitPrecisionString_thenIExpect10000() {
|
||||
// GIVEN
|
||||
val fiatMoney = FiatMoney(BigDecimal.valueOf(100), Currency.getInstance("ISK"))
|
||||
|
||||
// WHEN
|
||||
val result = fiatMoney.minimumUnitPrecisionString
|
||||
|
||||
// THEN
|
||||
assertEquals("10000", result)
|
||||
}
|
||||
}
|
|
@ -1,89 +0,0 @@
|
|||
package org.signal.devicetransfer;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.signal.devicetransfer.DeviceTransferAuthentication.Client;
|
||||
import org.signal.devicetransfer.DeviceTransferAuthentication.DeviceTransferAuthenticationException;
|
||||
import org.signal.devicetransfer.DeviceTransferAuthentication.Server;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.whispersystems.signalservice.test.LibSignalLibraryUtil.assumeLibSignalSupportedOnOS;
|
||||
|
||||
public class DeviceTransferAuthenticationTest {
|
||||
|
||||
private byte[] certificate;
|
||||
|
||||
@Before
|
||||
public void ensureNativeSupported() throws KeyGenerationFailedException {
|
||||
assumeLibSignalSupportedOnOS();
|
||||
|
||||
certificate = SelfSignedIdentity.create().getX509Encoded();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompute_withNoChanges() throws DeviceTransferAuthenticationException {
|
||||
Client client = new Client(certificate);
|
||||
Server server = new Server(certificate, client.getCommitment());
|
||||
|
||||
byte[] clientRandom = client.setServerRandomAndGetClientRandom(server.getRandom());
|
||||
|
||||
server.setClientRandom(clientRandom);
|
||||
assertEquals(client.computeShortAuthenticationCode(), server.computeShortAuthenticationCode());
|
||||
}
|
||||
|
||||
@Test(expected = DeviceTransferAuthenticationException.class)
|
||||
public void testServerCompute_withChangedClientCertificate() throws DeviceTransferAuthenticationException, KeyGenerationFailedException {
|
||||
byte[] badCertificate = SelfSignedIdentity.create().getX509Encoded();
|
||||
Client client = new Client(badCertificate);
|
||||
Server server = new Server(certificate, client.getCommitment());
|
||||
|
||||
byte[] clientRandom = client.setServerRandomAndGetClientRandom(server.getRandom());
|
||||
|
||||
server.setClientRandom(clientRandom);
|
||||
server.computeShortAuthenticationCode();
|
||||
}
|
||||
|
||||
@Test(expected = DeviceTransferAuthenticationException.class)
|
||||
public void testServerCompute_withChangedClientCommitment() throws DeviceTransferAuthenticationException {
|
||||
Client client = new Client(certificate);
|
||||
Server server = new Server(certificate, randomBytes());
|
||||
|
||||
byte[] clientRandom = client.setServerRandomAndGetClientRandom(server.getRandom());
|
||||
|
||||
server.setClientRandom(clientRandom);
|
||||
server.computeShortAuthenticationCode();
|
||||
}
|
||||
|
||||
@Test(expected = DeviceTransferAuthenticationException.class)
|
||||
public void testServerCompute_withChangedClientRandom() throws DeviceTransferAuthenticationException {
|
||||
Client client = new Client(certificate);
|
||||
Server server = new Server(certificate, client.getCommitment());
|
||||
|
||||
client.setServerRandomAndGetClientRandom(server.getRandom());
|
||||
|
||||
server.setClientRandom(randomBytes());
|
||||
server.computeShortAuthenticationCode();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClientCompute_withChangedServerSecret() throws DeviceTransferAuthenticationException {
|
||||
Client client = new Client(certificate);
|
||||
Server server = new Server(certificate, client.getCommitment());
|
||||
|
||||
byte[] clientRandom = client.setServerRandomAndGetClientRandom(randomBytes());
|
||||
|
||||
server.setClientRandom(clientRandom);
|
||||
assertNotEquals(client.computeShortAuthenticationCode(), server.computeShortAuthenticationCode());
|
||||
}
|
||||
|
||||
private @NonNull byte[] randomBytes() {
|
||||
byte[] bytes = new byte[32];
|
||||
new Random().nextBytes(bytes);
|
||||
return bytes;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
package org.signal.devicetransfer
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotEquals
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.signal.devicetransfer.DeviceTransferAuthentication.DeviceTransferAuthenticationException
|
||||
import org.whispersystems.signalservice.test.LibSignalLibraryUtil
|
||||
import kotlin.random.Random
|
||||
|
||||
class DeviceTransferAuthenticationTest {
|
||||
private lateinit var certificate: ByteArray
|
||||
|
||||
@Before
|
||||
fun ensureNativeSupported() {
|
||||
LibSignalLibraryUtil.assumeLibSignalSupportedOnOS()
|
||||
|
||||
certificate = SelfSignedIdentity.create().x509Encoded
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCompute_withNoChanges() {
|
||||
val client = DeviceTransferAuthentication.Client(certificate)
|
||||
val server = DeviceTransferAuthentication.Server(certificate, client.commitment)
|
||||
|
||||
val clientRandom = client.setServerRandomAndGetClientRandom(server.random)
|
||||
|
||||
server.setClientRandom(clientRandom)
|
||||
assertEquals(client.computeShortAuthenticationCode(), server.computeShortAuthenticationCode())
|
||||
}
|
||||
|
||||
@Test(expected = DeviceTransferAuthenticationException::class)
|
||||
fun testServerCompute_withChangedClientCertificate() {
|
||||
val badCertificate = SelfSignedIdentity.create().x509Encoded
|
||||
val client = DeviceTransferAuthentication.Client(badCertificate)
|
||||
val server = DeviceTransferAuthentication.Server(certificate, client.commitment)
|
||||
|
||||
val clientRandom = client.setServerRandomAndGetClientRandom(server.random)
|
||||
|
||||
server.setClientRandom(clientRandom)
|
||||
server.computeShortAuthenticationCode()
|
||||
}
|
||||
|
||||
@Test(expected = DeviceTransferAuthenticationException::class)
|
||||
fun testServerCompute_withChangedClientCommitment() {
|
||||
val client = DeviceTransferAuthentication.Client(certificate)
|
||||
val server = DeviceTransferAuthentication.Server(certificate, randomBytes())
|
||||
|
||||
val clientRandom = client.setServerRandomAndGetClientRandom(server.random)
|
||||
|
||||
server.setClientRandom(clientRandom)
|
||||
server.computeShortAuthenticationCode()
|
||||
}
|
||||
|
||||
@Test(expected = DeviceTransferAuthenticationException::class)
|
||||
fun testServerCompute_withChangedClientRandom() {
|
||||
val client = DeviceTransferAuthentication.Client(certificate)
|
||||
val server = DeviceTransferAuthentication.Server(certificate, client.commitment)
|
||||
|
||||
client.setServerRandomAndGetClientRandom(server.random)
|
||||
|
||||
server.setClientRandom(randomBytes())
|
||||
server.computeShortAuthenticationCode()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testClientCompute_withChangedServerSecret() {
|
||||
val client = DeviceTransferAuthentication.Client(certificate)
|
||||
val server = DeviceTransferAuthentication.Server(certificate, client.commitment)
|
||||
|
||||
val clientRandom = client.setServerRandomAndGetClientRandom(randomBytes())
|
||||
|
||||
server.setClientRandom(clientRandom)
|
||||
assertNotEquals(client.computeShortAuthenticationCode(), server.computeShortAuthenticationCode())
|
||||
}
|
||||
|
||||
private fun randomBytes(): ByteArray {
|
||||
val bytes = ByteArray(32)
|
||||
Random.nextBytes(bytes)
|
||||
return bytes
|
||||
}
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
package org.signal.devicetransfer;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(manifest = Config.NONE, application = Application.class)
|
||||
public class WifiDirectTest {
|
||||
|
||||
@Test
|
||||
public void instanceName_withExtraInfo() {
|
||||
String instanceName = WifiDirect.buildServiceInstanceName("knownothing");
|
||||
|
||||
assertEquals("_devicetransfer._knownothing._signal.org", instanceName);
|
||||
|
||||
String extractedExtraInfo = WifiDirect.isInstanceNameMatching(instanceName);
|
||||
assertEquals(extractedExtraInfo, "knownothing");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void instanceName_matchingWithoutExtraInfo() {
|
||||
String instanceName = WifiDirect.buildServiceInstanceName("");
|
||||
|
||||
assertEquals("_devicetransfer._signal.org", instanceName);
|
||||
|
||||
String extractedExtraInfo = WifiDirect.isInstanceNameMatching(instanceName);
|
||||
assertEquals(extractedExtraInfo, "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void instanceName_notMatching() {
|
||||
String extractedExtraInfo = WifiDirect.isInstanceNameMatching("_whoknows._what.org");
|
||||
assertNull(extractedExtraInfo);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package org.signal.devicetransfer
|
||||
|
||||
import android.app.Application
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(manifest = Config.NONE, application = Application::class)
|
||||
class WifiDirectTest {
|
||||
@Test
|
||||
fun instanceName_withExtraInfo() {
|
||||
val instanceName = WifiDirect.buildServiceInstanceName("knownothing")
|
||||
|
||||
assertEquals("_devicetransfer._knownothing._signal.org", instanceName)
|
||||
|
||||
val extractedExtraInfo = WifiDirect.isInstanceNameMatching(instanceName)
|
||||
assertEquals(extractedExtraInfo, "knownothing")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun instanceName_matchingWithoutExtraInfo() {
|
||||
val instanceName = WifiDirect.buildServiceInstanceName("")
|
||||
|
||||
assertEquals("_devicetransfer._signal.org", instanceName)
|
||||
|
||||
val extractedExtraInfo = WifiDirect.isInstanceNameMatching(instanceName)
|
||||
assertEquals(extractedExtraInfo, "")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun instanceName_notMatching() {
|
||||
val extractedExtraInfo = WifiDirect.isInstanceNameMatching("_whoknows._what.org")
|
||||
assertNull(extractedExtraInfo)
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
package org.whispersystems.util;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FlagUtilTest {
|
||||
|
||||
@Test
|
||||
public void given1_whenIConvertToBinaryFlag_thenIExpect1() {
|
||||
int expected = 1;
|
||||
|
||||
int actual = FlagUtil.toBinaryFlag(1);
|
||||
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void given2_whenIConvertToBinaryFlag_thenIExpect2() {
|
||||
int expected = 2;
|
||||
|
||||
int actual = FlagUtil.toBinaryFlag(2);
|
||||
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given3_whenIConvertToBinaryFlag_thenIExpect4() {
|
||||
int expected = 4;
|
||||
|
||||
int actual = FlagUtil.toBinaryFlag(3);
|
||||
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package org.whispersystems.util
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class FlagUtilTest {
|
||||
@Test
|
||||
fun given1_whenIConvertToBinaryFlag_thenIExpect1() {
|
||||
assertEquals(1, FlagUtil.toBinaryFlag(1))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun given2_whenIConvertToBinaryFlag_thenIExpect2() {
|
||||
assertEquals(2, FlagUtil.toBinaryFlag(2))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun given3_whenIConvertToBinaryFlag_thenIExpect4() {
|
||||
assertEquals(4, FlagUtil.toBinaryFlag(3))
|
||||
}
|
||||
}
|
|
@ -1,58 +0,0 @@
|
|||
package org.signal.paging;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class DataStatusTest {
|
||||
|
||||
@Test
|
||||
public void insertState_initiallyEmpty_InsertAtZero() {
|
||||
DataStatus subject = DataStatus.obtain(0);
|
||||
subject.insertState(0, true);
|
||||
|
||||
assertEquals(1, subject.size());
|
||||
assertTrue(subject.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insertState_someData_InsertAtZero() {
|
||||
DataStatus subject = DataStatus.obtain(2);
|
||||
subject.mark(1);
|
||||
|
||||
subject.insertState(0, true);
|
||||
|
||||
assertEquals(3, subject.size());
|
||||
assertTrue(subject.get(0));
|
||||
assertFalse(subject.get(1));
|
||||
assertTrue(subject.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insertState_someData_InsertAtOne() {
|
||||
DataStatus subject = DataStatus.obtain(3);
|
||||
subject.mark(1);
|
||||
|
||||
subject.insertState(1, true);
|
||||
|
||||
assertEquals(4, subject.size());
|
||||
assertFalse(subject.get(0));
|
||||
assertTrue(subject.get(1));
|
||||
assertTrue(subject.get(2));
|
||||
assertFalse(subject.get(3));
|
||||
}
|
||||
|
||||
@Test(expected = IndexOutOfBoundsException.class)
|
||||
public void insertState_negativeThrows() {
|
||||
DataStatus subject = DataStatus.obtain(0);
|
||||
subject.insertState(-1, true);
|
||||
}
|
||||
|
||||
@Test(expected = IndexOutOfBoundsException.class)
|
||||
public void insertState_largerThanSizePlusOneThrows() {
|
||||
DataStatus subject = DataStatus.obtain(0);
|
||||
subject.insertState(2, true);
|
||||
}
|
||||
}
|
56
paging/lib/src/test/java/org/signal/paging/DataStatusTest.kt
Normal file
56
paging/lib/src/test/java/org/signal/paging/DataStatusTest.kt
Normal file
|
@ -0,0 +1,56 @@
|
|||
package org.signal.paging
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class DataStatusTest {
|
||||
@Test
|
||||
fun insertState_initiallyEmpty_InsertAtZero() {
|
||||
val subject = DataStatus.obtain(0)
|
||||
subject.insertState(0, true)
|
||||
|
||||
assertEquals(1, subject.size())
|
||||
assertTrue(subject[0])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun insertState_someData_InsertAtZero() {
|
||||
val subject = DataStatus.obtain(2)
|
||||
subject.mark(1)
|
||||
|
||||
subject.insertState(0, true)
|
||||
|
||||
assertEquals(3, subject.size())
|
||||
assertTrue(subject[0])
|
||||
assertFalse(subject[1])
|
||||
assertTrue(subject[2])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun insertState_someData_InsertAtOne() {
|
||||
val subject = DataStatus.obtain(3)
|
||||
subject.mark(1)
|
||||
|
||||
subject.insertState(1, true)
|
||||
|
||||
assertEquals(4, subject.size())
|
||||
assertFalse(subject[0])
|
||||
assertTrue(subject[1])
|
||||
assertTrue(subject[2])
|
||||
assertFalse(subject[3])
|
||||
}
|
||||
|
||||
@Test(expected = IndexOutOfBoundsException::class)
|
||||
fun insertState_negativeThrows() {
|
||||
val subject = DataStatus.obtain(0)
|
||||
subject.insertState(-1, true)
|
||||
}
|
||||
|
||||
@Test(expected = IndexOutOfBoundsException::class)
|
||||
fun insertState_largerThanSizePlusOneThrows() {
|
||||
val subject = DataStatus.obtain(0)
|
||||
subject.insertState(2, true)
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue