From 4a3226d8c20eaead0a8498a2fa13117bc2ffab03 Mon Sep 17 00:00:00 2001 From: jhugman Date: Thu, 22 Aug 2019 14:01:49 +0100 Subject: [PATCH] 867 - Fix obvious NPE sync crash (#884) --- .../main/java/mozilla/lockbox/store/DataStore.kt | 16 +++++++++++++--- .../java/mozilla/lockbox/store/DataStoreTest.kt | 9 +++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/mozilla/lockbox/store/DataStore.kt b/app/src/main/java/mozilla/lockbox/store/DataStore.kt index 142b198e1..13b197d92 100644 --- a/app/src/main/java/mozilla/lockbox/store/DataStore.kt +++ b/app/src/main/java/mozilla/lockbox/store/DataStore.kt @@ -4,6 +4,7 @@ package mozilla.lockbox.store +import androidx.annotation.VisibleForTesting import com.jakewharton.rxrelay2.BehaviorRelay import com.jakewharton.rxrelay2.ReplayRelay import io.reactivex.Observable @@ -60,7 +61,8 @@ open class DataStore( internal val compositeDisposable = CompositeDisposable() private val stateSubject = ReplayRelay.createWithSize(1) - private val syncStateSubject = BehaviorRelay.createDefault(SyncState.NotSyncing) + @VisibleForTesting + val syncStateSubject: BehaviorRelay = BehaviorRelay.createDefault(SyncState.NotSyncing) private val listSubject: BehaviorRelay> = BehaviorRelay.createDefault(emptyList()) private val deletedItemSubject = ReplayRelay.create>() @@ -235,13 +237,21 @@ open class DataStore( } } - private fun sync() { + @VisibleForTesting( + otherwise = VisibleForTesting.PRIVATE + ) + fun sync() { resetSupport(support) + val syncConfig = support.syncConfig ?: run { + log.error("syncConfig is null in sync. This is likely a bug.") + return + } + // ideally, we don't sync unless we are connected to the network syncStateSubject.accept(SyncState.Syncing) - backend.sync(support.syncConfig!!) + backend.sync(syncConfig) .asSingle(coroutineContext) .map { log.debug("Hashed UID: $it") diff --git a/app/src/test/java/mozilla/lockbox/store/DataStoreTest.kt b/app/src/test/java/mozilla/lockbox/store/DataStoreTest.kt index 613dd8fec..fc32c2ec1 100644 --- a/app/src/test/java/mozilla/lockbox/store/DataStoreTest.kt +++ b/app/src/test/java/mozilla/lockbox/store/DataStoreTest.kt @@ -56,6 +56,15 @@ class DataStoreTest : DisposingTest() { subject = DataStore(dispatcher, support, timingSupport, lifecycleStore) } + @Test + fun `test issue 867 sync crash`() { + support.syncConfig = null + Assert.assertNull(support.syncConfig) + subject.sync() + Assert.assertNull(support.syncConfig) + Assert.assertEquals(subject.syncStateSubject.value, DataStore.SyncState.NotSyncing) + } + @Test fun testLockUnlock_shouldSync() { val stateIterator = this.subject.state.blockingIterable().iterator()