Rewrites EncryptedPrefsFormatTest with matchers

Issue #2745
This commit is contained in:
Ryan Haining 2023-09-24 22:47:32 -07:00
parent e1e72bf4a9
commit 202040b9ec

View file

@ -9,8 +9,8 @@ import app.aaps.core.main.utils.CryptoUtil
import app.aaps.core.interfaces.maintenance.PrefMetadata import app.aaps.core.interfaces.maintenance.PrefMetadata
import app.aaps.core.interfaces.resources.ResourceHelper import app.aaps.core.interfaces.resources.ResourceHelper
import app.aaps.shared.tests.TestBase import app.aaps.shared.tests.TestBase
import com.google.common.truth.Truth.assertThat
import com.google.common.truth.TruthJUnit.assume import com.google.common.truth.TruthJUnit.assume
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.mockito.ArgumentMatchers import org.mockito.ArgumentMatchers
@ -63,13 +63,16 @@ open class EncryptedPrefsFormatTest : TestBase() {
assumeAES256isSupported(cryptoUtil) assumeAES256isSupported(cryptoUtil)
Assertions.assertEquals(prefs.values.size, 2) assertThat(prefs.values).containsExactlyEntriesIn(
Assertions.assertEquals(prefs.values["key1"], "A") mapOf(
Assertions.assertEquals(prefs.values["keyB"], "2") "key1" to "A",
"keyB" to "2",
)
)
Assertions.assertEquals(prefs.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]?.status, PrefsStatusImpl.OK) assertThat(prefs.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]!!.status).isEqualTo(PrefsStatusImpl.OK)
Assertions.assertEquals(prefs.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]?.value, PrefsFormat.FORMAT_KEY_ENC) assertThat(prefs.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]!!.value).isEqualTo(PrefsFormat.FORMAT_KEY_ENC)
Assertions.assertEquals(prefs.metadata[PrefsMetadataKeyImpl.ENCRYPTION]?.status, PrefsStatusImpl.OK) assertThat(prefs.metadata[PrefsMetadataKeyImpl.ENCRYPTION]!!.status).isEqualTo(PrefsStatusImpl.OK)
} }
@Test @Test
@ -107,13 +110,15 @@ open class EncryptedPrefsFormatTest : TestBase() {
assumeAES256isSupported(cryptoUtil) assumeAES256isSupported(cryptoUtil)
Assertions.assertEquals(prefsOut.values.size, 2) assertThat(prefsOut.values).containsExactlyEntriesIn(
Assertions.assertEquals(prefsOut.values["testpref1"], "--1--") mapOf(
Assertions.assertEquals(prefsOut.values["testpref2"], "another") "testpref1" to "--1--",
"testpref2" to "another",
Assertions.assertEquals(prefsOut.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]?.status, PrefsStatusImpl.OK) )
Assertions.assertEquals(prefsOut.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]?.value, PrefsFormat.FORMAT_KEY_ENC) )
Assertions.assertEquals(prefsOut.metadata[PrefsMetadataKeyImpl.ENCRYPTION]?.status, PrefsStatusImpl.OK) assertThat(prefsOut.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]!!.status).isEqualTo(PrefsStatusImpl.OK)
assertThat(prefsOut.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]!!.value).isEqualTo(PrefsFormat.FORMAT_KEY_ENC)
assertThat(prefsOut.metadata[PrefsMetadataKeyImpl.ENCRYPTION]!!.status).isEqualTo(PrefsStatusImpl.OK)
} }
@Test @Test
@ -134,11 +139,11 @@ open class EncryptedPrefsFormatTest : TestBase() {
val encryptedFormat = EncryptedPrefsFormat(rh, cryptoUtil, storage) val encryptedFormat = EncryptedPrefsFormat(rh, cryptoUtil, storage)
val prefs = encryptedFormat.loadPreferences(getMockedFile(), "it-is-NOT-right-secret") val prefs = encryptedFormat.loadPreferences(getMockedFile(), "it-is-NOT-right-secret")
Assertions.assertEquals(prefs.values.size, 0) assertThat(prefs.values).isEmpty()
Assertions.assertEquals(prefs.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]?.status, PrefsStatusImpl.OK) assertThat(prefs.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]!!.status).isEqualTo(PrefsStatusImpl.OK)
Assertions.assertEquals(prefs.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]?.value, PrefsFormat.FORMAT_KEY_ENC) assertThat(prefs.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]!!.value).isEqualTo(PrefsFormat.FORMAT_KEY_ENC)
Assertions.assertEquals(prefs.metadata[PrefsMetadataKeyImpl.ENCRYPTION]?.status, PrefsStatusImpl.ERROR) assertThat(prefs.metadata[PrefsMetadataKeyImpl.ENCRYPTION]!!.status).isEqualTo(PrefsStatusImpl.ERROR)
} }
@Test @Test
@ -164,10 +169,10 @@ open class EncryptedPrefsFormatTest : TestBase() {
assumeAES256isSupported(cryptoUtil) assumeAES256isSupported(cryptoUtil)
// contents were not tampered and we can decrypt them // contents were not tampered and we can decrypt them
Assertions.assertEquals(prefs.values.size, 2) assertThat(prefs.values).hasSize(2)
// but checksum fails on metadata, so overall security fails // but checksum fails on metadata, so overall security fails
Assertions.assertEquals(prefs.metadata[PrefsMetadataKeyImpl.ENCRYPTION]?.status, PrefsStatusImpl.ERROR) assertThat(prefs.metadata[PrefsMetadataKeyImpl.ENCRYPTION]!!.status).isEqualTo(PrefsStatusImpl.ERROR)
} }
@Test @Test
@ -188,8 +193,8 @@ open class EncryptedPrefsFormatTest : TestBase() {
val encryptedFormat = EncryptedPrefsFormat(rh, cryptoUtil, storage) val encryptedFormat = EncryptedPrefsFormat(rh, cryptoUtil, storage)
val prefs = encryptedFormat.loadPreferences(getMockedFile(), "sikret") val prefs = encryptedFormat.loadPreferences(getMockedFile(), "sikret")
Assertions.assertEquals(prefs.values.size, 0) assertThat(prefs.values).isEmpty()
Assertions.assertEquals(prefs.metadata[PrefsMetadataKeyImpl.ENCRYPTION]?.status, PrefsStatusImpl.ERROR) assertThat(prefs.metadata[PrefsMetadataKeyImpl.ENCRYPTION]!!.status).isEqualTo(PrefsStatusImpl.ERROR)
} }
@Test @Test
@ -203,8 +208,8 @@ open class EncryptedPrefsFormatTest : TestBase() {
val encryptedFormat = EncryptedPrefsFormat(rh, cryptoUtil, storage) val encryptedFormat = EncryptedPrefsFormat(rh, cryptoUtil, storage)
val prefs = encryptedFormat.loadPreferences(getMockedFile(), "sikret") val prefs = encryptedFormat.loadPreferences(getMockedFile(), "sikret")
Assertions.assertEquals(prefs.values.size, 0) assertThat(prefs.values).isEmpty()
Assertions.assertEquals(prefs.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]?.status, PrefsStatusImpl.ERROR) assertThat(prefs.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]!!.status).isEqualTo(PrefsStatusImpl.ERROR)
} }
@Test @Test