|
| 1 | +package org.thoughtcrime.securesms.service; |
| 2 | + |
| 3 | +import android.test.AndroidTestCase; |
| 4 | + |
| 5 | +import org.whispersystems.libaxolotl.ecc.Curve; |
| 6 | +import org.whispersystems.libaxolotl.state.SignedPreKeyRecord; |
| 7 | +import org.whispersystems.libaxolotl.state.SignedPreKeyStore; |
| 8 | +import org.whispersystems.textsecure.push.PushServiceSocket; |
| 9 | +import org.whispersystems.textsecure.push.SignedPreKeyEntity; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.LinkedList; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import static org.mockito.Matchers.anyInt; |
| 16 | +import static org.mockito.Matchers.eq; |
| 17 | +import static org.mockito.Mockito.mock; |
| 18 | +import static org.mockito.Mockito.never; |
| 19 | +import static org.mockito.Mockito.times; |
| 20 | +import static org.mockito.Mockito.verify; |
| 21 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 22 | +import static org.mockito.Mockito.when; |
| 23 | + |
| 24 | +public class PreKeyServiceTest extends AndroidTestCase { |
| 25 | + |
| 26 | + public void testSignedPreKeyRotationNotRegistered() throws IOException { |
| 27 | + SignedPreKeyStore signedPreKeyStore = mock(SignedPreKeyStore.class); |
| 28 | + PushServiceSocket pushServiceSocket = mock(PushServiceSocket.class); |
| 29 | + |
| 30 | + when(pushServiceSocket.getCurrentSignedPreKey()).thenReturn(null); |
| 31 | + |
| 32 | + PreKeyService.CleanSignedPreKeysTask cleanTask = new PreKeyService.CleanSignedPreKeysTask(signedPreKeyStore, |
| 33 | + pushServiceSocket); |
| 34 | + |
| 35 | + cleanTask.run(); |
| 36 | + |
| 37 | + verify(pushServiceSocket).getCurrentSignedPreKey(); |
| 38 | + verifyNoMoreInteractions(signedPreKeyStore); |
| 39 | + } |
| 40 | + |
| 41 | + public void testSignedPreKeyEviction() throws Exception { |
| 42 | + SignedPreKeyStore signedPreKeyStore = mock(SignedPreKeyStore.class); |
| 43 | + PushServiceSocket pushServiceSocket = mock(PushServiceSocket.class); |
| 44 | + SignedPreKeyEntity currentSignedPreKeyEntity = mock(SignedPreKeyEntity.class); |
| 45 | + |
| 46 | + when(currentSignedPreKeyEntity.getKeyId()).thenReturn(3133); |
| 47 | + when(pushServiceSocket.getCurrentSignedPreKey()).thenReturn(currentSignedPreKeyEntity); |
| 48 | + |
| 49 | + final SignedPreKeyRecord currentRecord = new SignedPreKeyRecord(3133, System.currentTimeMillis(), Curve.generateKeyPair(true), new byte[64]); |
| 50 | + |
| 51 | + List<SignedPreKeyRecord> records = new LinkedList<SignedPreKeyRecord>() {{ |
| 52 | + add(new SignedPreKeyRecord(1, 10, Curve.generateKeyPair(true), new byte[32])); |
| 53 | + add(new SignedPreKeyRecord(2, 11, Curve.generateKeyPair(true), new byte[32])); |
| 54 | + add(new SignedPreKeyRecord(3, System.currentTimeMillis() - 90, Curve.generateKeyPair(true), new byte[64])); |
| 55 | + add(new SignedPreKeyRecord(4, System.currentTimeMillis() - 100, Curve.generateKeyPair(true), new byte[64])); |
| 56 | + add(currentRecord); |
| 57 | + }}; |
| 58 | + |
| 59 | + when(signedPreKeyStore.loadSignedPreKeys()).thenReturn(records); |
| 60 | + when(signedPreKeyStore.loadSignedPreKey(eq(3133))).thenReturn(currentRecord); |
| 61 | + |
| 62 | + PreKeyService.CleanSignedPreKeysTask cleanTask = new PreKeyService.CleanSignedPreKeysTask(signedPreKeyStore, pushServiceSocket); |
| 63 | + cleanTask.run(); |
| 64 | + |
| 65 | + verify(signedPreKeyStore).removeSignedPreKey(eq(1)); |
| 66 | + verify(signedPreKeyStore).removeSignedPreKey(eq(2)); |
| 67 | + verify(signedPreKeyStore, times(2)).removeSignedPreKey(anyInt()); |
| 68 | + } |
| 69 | + |
| 70 | + public void testSignedPreKeyNoEviction() throws Exception { |
| 71 | + SignedPreKeyStore signedPreKeyStore = mock(SignedPreKeyStore.class); |
| 72 | + PushServiceSocket pushServiceSocket = mock(PushServiceSocket.class); |
| 73 | + SignedPreKeyEntity currentSignedPreKeyEntity = mock(SignedPreKeyEntity.class); |
| 74 | + |
| 75 | + when(currentSignedPreKeyEntity.getKeyId()).thenReturn(3133); |
| 76 | + when(pushServiceSocket.getCurrentSignedPreKey()).thenReturn(currentSignedPreKeyEntity); |
| 77 | + |
| 78 | + final SignedPreKeyRecord currentRecord = new SignedPreKeyRecord(3133, System.currentTimeMillis(), Curve.generateKeyPair(true), new byte[64]); |
| 79 | + |
| 80 | + List<SignedPreKeyRecord> records = new LinkedList<SignedPreKeyRecord>() {{ |
| 81 | + add(currentRecord); |
| 82 | + }}; |
| 83 | + |
| 84 | + when(signedPreKeyStore.loadSignedPreKeys()).thenReturn(records); |
| 85 | + when(signedPreKeyStore.loadSignedPreKey(eq(3133))).thenReturn(currentRecord); |
| 86 | + |
| 87 | + PreKeyService.CleanSignedPreKeysTask cleanTask = new PreKeyService.CleanSignedPreKeysTask(signedPreKeyStore, pushServiceSocket); |
| 88 | + cleanTask.run(); |
| 89 | + |
| 90 | + verify(signedPreKeyStore, never()).removeSignedPreKey(anyInt()); |
| 91 | + } |
| 92 | +} |
0 commit comments