mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-26 19:56:02 +01:00
Remove E164s most places and prefer ServiceId more places.\
This commit is contained in:
committed by
Alex Hart
parent
d6b6884c69
commit
935dd7de45
@@ -9,6 +9,7 @@ import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.testutil.LogRecorder;
|
||||
import org.whispersystems.libsignal.util.guava.Optional;
|
||||
import org.whispersystems.signalservice.api.push.ACI;
|
||||
import org.whispersystems.signalservice.api.push.ServiceId;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -40,7 +41,7 @@ public final class RecipientIdCacheTest {
|
||||
|
||||
@Test
|
||||
public void empty_access_by_uuid() {
|
||||
RecipientId recipientId = recipientIdCache.get(ACI.from(UUID.randomUUID()), null);
|
||||
RecipientId recipientId = recipientIdCache.get(ServiceId.from(UUID.randomUUID()), null);
|
||||
|
||||
assertNull(recipientId);
|
||||
}
|
||||
@@ -55,11 +56,11 @@ public final class RecipientIdCacheTest {
|
||||
@Test
|
||||
public void cache_hit_by_uuid() {
|
||||
RecipientId recipientId1 = recipientId();
|
||||
ACI aci1 = ACI.from(UUID.randomUUID());
|
||||
ServiceId sid1 = ServiceId.from(UUID.randomUUID());
|
||||
|
||||
recipientIdCache.put(recipient(recipientId1, aci1, null));
|
||||
recipientIdCache.put(recipient(recipientId1, sid1, null));
|
||||
|
||||
RecipientId recipientId = recipientIdCache.get(aci1, null);
|
||||
RecipientId recipientId = recipientIdCache.get(sid1, null);
|
||||
|
||||
assertEquals(recipientId1, recipientId);
|
||||
}
|
||||
@@ -67,12 +68,12 @@ public final class RecipientIdCacheTest {
|
||||
@Test
|
||||
public void cache_miss_by_uuid() {
|
||||
RecipientId recipientId1 = recipientId();
|
||||
ACI aci1 = ACI.from(UUID.randomUUID());
|
||||
ACI aci2 = ACI.from(UUID.randomUUID());
|
||||
ServiceId sid1 = ServiceId.from(UUID.randomUUID());
|
||||
ServiceId sid2 = ServiceId.from(UUID.randomUUID());
|
||||
|
||||
recipientIdCache.put(recipient(recipientId1, aci1, null));
|
||||
recipientIdCache.put(recipient(recipientId1, sid1, null));
|
||||
|
||||
RecipientId recipientId = recipientIdCache.get(aci2, null);
|
||||
RecipientId recipientId = recipientIdCache.get(sid2, null);
|
||||
|
||||
assertNull(recipientId);
|
||||
}
|
||||
@@ -80,11 +81,11 @@ public final class RecipientIdCacheTest {
|
||||
@Test
|
||||
public void cache_hit_by_uuid_e164_not_supplied_on_get() {
|
||||
RecipientId recipientId1 = recipientId();
|
||||
ACI aci1 = ACI.from(UUID.randomUUID());
|
||||
ServiceId sid1 = ServiceId.from(UUID.randomUUID());
|
||||
|
||||
recipientIdCache.put(recipient(recipientId1, aci1, "+15551234567"));
|
||||
recipientIdCache.put(recipient(recipientId1, sid1, "+15551234567"));
|
||||
|
||||
RecipientId recipientId = recipientIdCache.get(aci1, null);
|
||||
RecipientId recipientId = recipientIdCache.get(sid1, null);
|
||||
|
||||
assertEquals(recipientId1, recipientId);
|
||||
}
|
||||
@@ -92,11 +93,11 @@ public final class RecipientIdCacheTest {
|
||||
@Test
|
||||
public void cache_miss_by_uuid_e164_not_supplied_on_put() {
|
||||
RecipientId recipientId1 = recipientId();
|
||||
ACI aci1 = ACI.from(UUID.randomUUID());
|
||||
ServiceId sid1 = ServiceId.from(UUID.randomUUID());
|
||||
|
||||
recipientIdCache.put(recipient(recipientId1, aci1, null));
|
||||
recipientIdCache.put(recipient(recipientId1, sid1, null));
|
||||
|
||||
RecipientId recipientId = recipientIdCache.get(aci1, "+15551234567");
|
||||
RecipientId recipientId = recipientIdCache.get(sid1, "+15551234567");
|
||||
|
||||
assertNull(recipientId);
|
||||
}
|
||||
@@ -129,9 +130,9 @@ public final class RecipientIdCacheTest {
|
||||
@Test
|
||||
public void cache_hit_by_e164_uuid_not_supplied_on_get() {
|
||||
RecipientId recipientId1 = recipientId();
|
||||
ACI aci1 = ACI.from(UUID.randomUUID());
|
||||
ServiceId sid1 = ServiceId.from(UUID.randomUUID());
|
||||
|
||||
recipientIdCache.put(recipient(recipientId1, aci1, "+15551234567"));
|
||||
recipientIdCache.put(recipient(recipientId1, sid1, "+15551234567"));
|
||||
|
||||
RecipientId recipientId = recipientIdCache.get(null, "+15551234567");
|
||||
|
||||
@@ -141,12 +142,12 @@ public final class RecipientIdCacheTest {
|
||||
@Test
|
||||
public void cache_miss_by_e164_uuid_not_supplied_on_put() {
|
||||
RecipientId recipientId1 = recipientId();
|
||||
ACI aci1 = ACI.from(UUID.randomUUID());
|
||||
ServiceId sid1 = ServiceId.from(UUID.randomUUID());
|
||||
String e164 = "+1555123456";
|
||||
|
||||
recipientIdCache.put(recipient(recipientId1, null, e164));
|
||||
|
||||
RecipientId recipientId = recipientIdCache.get(aci1, e164);
|
||||
RecipientId recipientId = recipientIdCache.get(sid1, e164);
|
||||
|
||||
assertNull(recipientId);
|
||||
}
|
||||
@@ -154,12 +155,12 @@ public final class RecipientIdCacheTest {
|
||||
@Test
|
||||
public void cache_hit_by_both() {
|
||||
RecipientId recipientId1 = recipientId();
|
||||
ACI aci1 = ACI.from(UUID.randomUUID());
|
||||
ServiceId sid1 = ServiceId.from(UUID.randomUUID());
|
||||
String e164 = "+1555123456";
|
||||
|
||||
recipientIdCache.put(recipient(recipientId1, aci1, e164));
|
||||
recipientIdCache.put(recipient(recipientId1, sid1, e164));
|
||||
|
||||
RecipientId recipientId = recipientIdCache.get(aci1, e164);
|
||||
RecipientId recipientId = recipientIdCache.get(sid1, e164);
|
||||
|
||||
assertEquals(recipientId1, recipientId);
|
||||
}
|
||||
@@ -167,13 +168,13 @@ public final class RecipientIdCacheTest {
|
||||
@Test
|
||||
public void full_recipient_id_learned_by_two_puts() {
|
||||
RecipientId recipientId1 = recipientId();
|
||||
ACI aci1 = ACI.from(UUID.randomUUID());
|
||||
ServiceId sid1 = ServiceId.from(UUID.randomUUID());
|
||||
String e164 = "+1555123456";
|
||||
|
||||
recipientIdCache.put(recipient(recipientId1, aci1, null));
|
||||
recipientIdCache.put(recipient(recipientId1, sid1, null));
|
||||
recipientIdCache.put(recipient(recipientId1, null, e164));
|
||||
|
||||
RecipientId recipientId = recipientIdCache.get(aci1, e164);
|
||||
RecipientId recipientId = recipientIdCache.get(sid1, e164);
|
||||
|
||||
assertEquals(recipientId1, recipientId);
|
||||
}
|
||||
@@ -182,13 +183,13 @@ public final class RecipientIdCacheTest {
|
||||
public void if_cache_state_disagrees_returns_null() {
|
||||
RecipientId recipientId1 = recipientId();
|
||||
RecipientId recipientId2 = recipientId();
|
||||
ACI aci = ACI.from(UUID.randomUUID());
|
||||
ServiceId sid = ServiceId.from(UUID.randomUUID());
|
||||
String e164 = "+1555123456";
|
||||
|
||||
recipientIdCache.put(recipient(recipientId1, null, e164));
|
||||
recipientIdCache.put(recipient(recipientId2, aci, null));
|
||||
recipientIdCache.put(recipient(recipientId2, sid, null));
|
||||
|
||||
RecipientId recipientId = recipientIdCache.get(aci, e164);
|
||||
RecipientId recipientId = recipientIdCache.get(sid, e164);
|
||||
|
||||
assertNull(recipientId);
|
||||
|
||||
@@ -200,15 +201,15 @@ public final class RecipientIdCacheTest {
|
||||
public void after_invalid_cache_hit_entries_are_cleared_up() {
|
||||
RecipientId recipientId1 = recipientId();
|
||||
RecipientId recipientId2 = recipientId();
|
||||
ACI aci = ACI.from(UUID.randomUUID());
|
||||
ServiceId sid = ServiceId.from(UUID.randomUUID());
|
||||
String e164 = "+1555123456";
|
||||
|
||||
recipientIdCache.put(recipient(recipientId1, null, e164));
|
||||
recipientIdCache.put(recipient(recipientId2, aci, null));
|
||||
recipientIdCache.put(recipient(recipientId2, sid, null));
|
||||
|
||||
recipientIdCache.get(aci, e164);
|
||||
recipientIdCache.get(sid, e164);
|
||||
|
||||
assertNull(recipientIdCache.get(aci, null));
|
||||
assertNull(recipientIdCache.get(sid, null));
|
||||
assertNull(recipientIdCache.get(null, e164));
|
||||
}
|
||||
|
||||
@@ -216,57 +217,57 @@ public final class RecipientIdCacheTest {
|
||||
public void multiple_entries() {
|
||||
RecipientId recipientId1 = recipientId();
|
||||
RecipientId recipientId2 = recipientId();
|
||||
ACI aci1 = ACI.from(UUID.randomUUID());
|
||||
ACI aci2 = ACI.from(UUID.randomUUID());
|
||||
ServiceId sid1 = ServiceId.from(UUID.randomUUID());
|
||||
ServiceId sid2 = ServiceId.from(UUID.randomUUID());
|
||||
|
||||
recipientIdCache.put(recipient(recipientId1, aci1, null));
|
||||
recipientIdCache.put(recipient(recipientId2, aci2, null));
|
||||
recipientIdCache.put(recipient(recipientId1, sid1, null));
|
||||
recipientIdCache.put(recipient(recipientId2, sid2, null));
|
||||
|
||||
assertEquals(recipientId1, recipientIdCache.get(aci1, null));
|
||||
assertEquals(recipientId2, recipientIdCache.get(aci2, null));
|
||||
assertEquals(recipientId1, recipientIdCache.get(sid1, null));
|
||||
assertEquals(recipientId2, recipientIdCache.get(sid2, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void drops_oldest_when_reaches_cache_limit() {
|
||||
RecipientId recipientId1 = recipientId();
|
||||
ACI aci1 = ACI.from(UUID.randomUUID());
|
||||
ServiceId sid1 = ServiceId.from(UUID.randomUUID());
|
||||
|
||||
recipientIdCache.put(recipient(recipientId1, aci1, null));
|
||||
recipientIdCache.put(recipient(recipientId1, sid1, null));
|
||||
|
||||
for (int i = 0; i < TEST_CACHE_LIMIT; i++) {
|
||||
recipientIdCache.put(recipient(recipientId(), ACI.from(UUID.randomUUID()), null));
|
||||
recipientIdCache.put(recipient(recipientId(), ServiceId.from(UUID.randomUUID()), null));
|
||||
}
|
||||
|
||||
assertNull(recipientIdCache.get(aci1, null));
|
||||
assertNull(recipientIdCache.get(sid1, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void remains_in_cache_when_used_before_reaching_cache_limit() {
|
||||
RecipientId recipientId1 = recipientId();
|
||||
ACI aci1 = ACI.from(UUID.randomUUID());
|
||||
ServiceId sid1 = ServiceId.from(UUID.randomUUID());
|
||||
|
||||
recipientIdCache.put(recipient(recipientId1, aci1, null));
|
||||
recipientIdCache.put(recipient(recipientId1, sid1, null));
|
||||
|
||||
for (int i = 0; i < TEST_CACHE_LIMIT - 1; i++) {
|
||||
recipientIdCache.put(recipient(recipientId(), ACI.from(UUID.randomUUID()), null));
|
||||
recipientIdCache.put(recipient(recipientId(), ServiceId.from(UUID.randomUUID()), null));
|
||||
}
|
||||
|
||||
assertEquals(recipientId1, recipientIdCache.get(aci1, null));
|
||||
assertEquals(recipientId1, recipientIdCache.get(sid1, null));
|
||||
|
||||
recipientIdCache.put(recipient(recipientId(), ACI.from(UUID.randomUUID()), null));
|
||||
recipientIdCache.put(recipient(recipientId(), ServiceId.from(UUID.randomUUID()), null));
|
||||
|
||||
assertEquals(recipientId1, recipientIdCache.get(aci1, null));
|
||||
assertEquals(recipientId1, recipientIdCache.get(sid1, null));
|
||||
}
|
||||
|
||||
private static @NonNull RecipientId recipientId() {
|
||||
return mock(RecipientId.class);
|
||||
}
|
||||
|
||||
private static @NonNull Recipient recipient(RecipientId recipientId, @Nullable ACI aci, @Nullable String e164) {
|
||||
private static @NonNull Recipient recipient(RecipientId recipientId, @Nullable ServiceId serviceId, @Nullable String e164) {
|
||||
Recipient mock = mock(Recipient.class);
|
||||
|
||||
when(mock.getId()).thenReturn(recipientId);
|
||||
when(mock.getServiceId()).thenReturn(Optional.fromNullable(aci));
|
||||
when(mock.getServiceId()).thenReturn(Optional.fromNullable(serviceId));
|
||||
when(mock.getE164()).thenReturn(Optional.fromNullable(e164));
|
||||
|
||||
return mock;
|
||||
|
||||
Reference in New Issue
Block a user