2014-11-11 19:57:53 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
2015-11-18 18:09:39 -08:00
|
|
|
import org.junit.Test;
|
|
|
|
import org.thoughtcrime.securesms.BaseUnitTest;
|
2014-11-11 19:57:53 -08:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.dependencies.AxolotlStorageModule;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.libsignal.ecc.Curve;
|
|
|
|
import org.whispersystems.libsignal.state.SignedPreKeyRecord;
|
|
|
|
import org.whispersystems.libsignal.state.SignedPreKeyStore;
|
|
|
|
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
|
|
|
|
import org.whispersystems.signalservice.api.push.SignedPreKeyEntity;
|
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
|
2014-11-11 19:57:53 -08:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import dagger.Module;
|
|
|
|
import dagger.ObjectGraph;
|
|
|
|
import dagger.Provides;
|
|
|
|
|
2015-11-18 18:09:39 -08:00
|
|
|
import static org.junit.Assert.assertTrue;
|
2014-11-11 19:57:53 -08:00
|
|
|
import static org.mockito.Matchers.anyInt;
|
|
|
|
import static org.mockito.Matchers.eq;
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.never;
|
|
|
|
import static org.mockito.Mockito.times;
|
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
|
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
2015-11-18 18:09:39 -08:00
|
|
|
public class CleanPreKeysJobTest extends BaseUnitTest {
|
|
|
|
@Test
|
2014-11-11 19:57:53 -08:00
|
|
|
public void testSignedPreKeyRotationNotRegistered() throws IOException, MasterSecretJob.RequirementNotMetException {
|
2016-03-23 10:34:41 -07:00
|
|
|
SignalServiceAccountManager accountManager = mock(SignalServiceAccountManager.class);
|
|
|
|
SignedPreKeyStore signedPreKeyStore = mock(SignedPreKeyStore.class);
|
|
|
|
MasterSecret masterSecret = mock(MasterSecret.class);
|
2014-11-11 19:57:53 -08:00
|
|
|
when(accountManager.getSignedPreKey()).thenReturn(null);
|
|
|
|
|
2015-11-18 18:09:39 -08:00
|
|
|
CleanPreKeysJob cleanPreKeysJob = new CleanPreKeysJob(context);
|
2014-11-11 19:57:53 -08:00
|
|
|
|
|
|
|
ObjectGraph objectGraph = ObjectGraph.create(new TestModule(accountManager, signedPreKeyStore));
|
|
|
|
objectGraph.inject(cleanPreKeysJob);
|
|
|
|
|
|
|
|
cleanPreKeysJob.onRun(masterSecret);
|
|
|
|
|
|
|
|
verify(accountManager).getSignedPreKey();
|
|
|
|
verifyNoMoreInteractions(signedPreKeyStore);
|
|
|
|
}
|
|
|
|
|
2015-11-18 18:09:39 -08:00
|
|
|
@Test
|
2014-11-11 19:57:53 -08:00
|
|
|
public void testSignedPreKeyEviction() throws Exception {
|
2016-03-23 10:34:41 -07:00
|
|
|
SignedPreKeyStore signedPreKeyStore = mock(SignedPreKeyStore.class);
|
|
|
|
SignalServiceAccountManager accountManager = mock(SignalServiceAccountManager.class);
|
|
|
|
SignedPreKeyEntity currentSignedPreKeyEntity = mock(SignedPreKeyEntity.class);
|
|
|
|
MasterSecret masterSecret = mock(MasterSecret.class);
|
2014-11-11 19:57:53 -08:00
|
|
|
|
|
|
|
when(currentSignedPreKeyEntity.getKeyId()).thenReturn(3133);
|
|
|
|
when(accountManager.getSignedPreKey()).thenReturn(currentSignedPreKeyEntity);
|
|
|
|
|
|
|
|
final SignedPreKeyRecord currentRecord = new SignedPreKeyRecord(3133, System.currentTimeMillis(), Curve.generateKeyPair(), new byte[64]);
|
|
|
|
|
|
|
|
List<SignedPreKeyRecord> records = new LinkedList<SignedPreKeyRecord>() {{
|
|
|
|
add(new SignedPreKeyRecord(2, 11, Curve.generateKeyPair(), new byte[32]));
|
|
|
|
add(new SignedPreKeyRecord(4, System.currentTimeMillis() - 100, Curve.generateKeyPair(), new byte[64]));
|
|
|
|
add(currentRecord);
|
|
|
|
add(new SignedPreKeyRecord(3, System.currentTimeMillis() - 90, Curve.generateKeyPair(), new byte[64]));
|
|
|
|
add(new SignedPreKeyRecord(1, 10, Curve.generateKeyPair(), new byte[32]));
|
|
|
|
}};
|
|
|
|
|
|
|
|
when(signedPreKeyStore.loadSignedPreKeys()).thenReturn(records);
|
|
|
|
when(signedPreKeyStore.loadSignedPreKey(eq(3133))).thenReturn(currentRecord);
|
|
|
|
|
2015-11-18 18:09:39 -08:00
|
|
|
CleanPreKeysJob cleanPreKeysJob = new CleanPreKeysJob(context);
|
2014-11-11 19:57:53 -08:00
|
|
|
|
|
|
|
ObjectGraph objectGraph = ObjectGraph.create(new TestModule(accountManager, signedPreKeyStore));
|
|
|
|
objectGraph.inject(cleanPreKeysJob);
|
|
|
|
|
|
|
|
cleanPreKeysJob.onRun(masterSecret);
|
|
|
|
|
|
|
|
verify(signedPreKeyStore).removeSignedPreKey(eq(1));
|
|
|
|
verify(signedPreKeyStore, times(1)).removeSignedPreKey(anyInt());
|
|
|
|
}
|
|
|
|
|
2015-11-18 18:09:39 -08:00
|
|
|
@Test
|
2014-11-11 19:57:53 -08:00
|
|
|
public void testSignedPreKeyNoEviction() throws Exception {
|
2016-03-23 10:34:41 -07:00
|
|
|
SignedPreKeyStore signedPreKeyStore = mock(SignedPreKeyStore.class);
|
|
|
|
SignalServiceAccountManager accountManager = mock(SignalServiceAccountManager.class);
|
|
|
|
SignedPreKeyEntity currentSignedPreKeyEntity = mock(SignedPreKeyEntity.class);
|
2014-11-11 19:57:53 -08:00
|
|
|
|
|
|
|
when(currentSignedPreKeyEntity.getKeyId()).thenReturn(3133);
|
|
|
|
when(accountManager.getSignedPreKey()).thenReturn(currentSignedPreKeyEntity);
|
|
|
|
|
|
|
|
final SignedPreKeyRecord currentRecord = new SignedPreKeyRecord(3133, System.currentTimeMillis(), Curve.generateKeyPair(), new byte[64]);
|
|
|
|
|
|
|
|
List<SignedPreKeyRecord> records = new LinkedList<SignedPreKeyRecord>() {{
|
|
|
|
add(currentRecord);
|
|
|
|
}};
|
|
|
|
|
|
|
|
when(signedPreKeyStore.loadSignedPreKeys()).thenReturn(records);
|
|
|
|
when(signedPreKeyStore.loadSignedPreKey(eq(3133))).thenReturn(currentRecord);
|
|
|
|
|
2015-11-18 18:09:39 -08:00
|
|
|
CleanPreKeysJob cleanPreKeysJob = new CleanPreKeysJob(context);
|
2014-11-11 19:57:53 -08:00
|
|
|
|
|
|
|
ObjectGraph objectGraph = ObjectGraph.create(new TestModule(accountManager, signedPreKeyStore));
|
|
|
|
objectGraph.inject(cleanPreKeysJob);
|
|
|
|
|
|
|
|
verify(signedPreKeyStore, never()).removeSignedPreKey(anyInt());
|
|
|
|
}
|
|
|
|
|
2015-11-18 18:09:39 -08:00
|
|
|
@Test
|
2014-11-11 19:57:53 -08:00
|
|
|
public void testConnectionError() throws Exception {
|
|
|
|
SignedPreKeyStore signedPreKeyStore = mock(SignedPreKeyStore.class);
|
2016-03-23 10:34:41 -07:00
|
|
|
SignalServiceAccountManager accountManager = mock(SignalServiceAccountManager.class);
|
2014-11-11 19:57:53 -08:00
|
|
|
MasterSecret masterSecret = mock(MasterSecret.class);
|
|
|
|
|
|
|
|
when(accountManager.getSignedPreKey()).thenThrow(new PushNetworkException("Connectivity error!"));
|
|
|
|
|
2015-11-18 18:09:39 -08:00
|
|
|
CleanPreKeysJob cleanPreKeysJob = new CleanPreKeysJob(context);
|
2014-11-11 19:57:53 -08:00
|
|
|
|
|
|
|
ObjectGraph objectGraph = ObjectGraph.create(new TestModule(accountManager, signedPreKeyStore));
|
|
|
|
objectGraph.inject(cleanPreKeysJob);
|
|
|
|
|
|
|
|
try {
|
|
|
|
cleanPreKeysJob.onRun(masterSecret);
|
|
|
|
throw new AssertionError("should have failed!");
|
|
|
|
} catch (IOException e) {
|
|
|
|
assertTrue(cleanPreKeysJob.onShouldRetry(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Module(injects = {CleanPreKeysJob.class})
|
|
|
|
public static class TestModule {
|
2016-03-23 10:34:41 -07:00
|
|
|
private final SignalServiceAccountManager accountManager;
|
2014-11-11 19:57:53 -08:00
|
|
|
private final SignedPreKeyStore signedPreKeyStore;
|
|
|
|
|
2016-03-23 10:34:41 -07:00
|
|
|
private TestModule(SignalServiceAccountManager accountManager, SignedPreKeyStore signedPreKeyStore) {
|
2014-11-11 19:57:53 -08:00
|
|
|
this.accountManager = accountManager;
|
|
|
|
this.signedPreKeyStore = signedPreKeyStore;
|
|
|
|
}
|
|
|
|
|
2016-03-23 10:34:41 -07:00
|
|
|
@Provides SignalServiceAccountManager provideTextSecureAccountManager() {
|
2014-11-11 19:57:53 -08:00
|
|
|
return accountManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Provides
|
|
|
|
AxolotlStorageModule.SignedPreKeyStoreFactory provideSignedPreKeyStore() {
|
|
|
|
return new AxolotlStorageModule.SignedPreKeyStoreFactory() {
|
|
|
|
@Override
|
2015-07-30 13:03:44 -07:00
|
|
|
public SignedPreKeyStore create() {
|
2014-11-11 19:57:53 -08:00
|
|
|
return signedPreKeyStore;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|