From 202040b9ec166fdbe04dd1a459cd81fbff226023 Mon Sep 17 00:00:00 2001 From: Ryan Haining Date: Sun, 24 Sep 2023 22:47:32 -0700 Subject: [PATCH] Rewrites EncryptedPrefsFormatTest with matchers Issue #2745 --- .../formats/EncryptedPrefsFormatTest.kt | 53 ++++++++++--------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/plugins/configuration/src/test/kotlin/app/aaps/configuration/maintenance/formats/EncryptedPrefsFormatTest.kt b/plugins/configuration/src/test/kotlin/app/aaps/configuration/maintenance/formats/EncryptedPrefsFormatTest.kt index 023bbe21a9..c23b43bd89 100644 --- a/plugins/configuration/src/test/kotlin/app/aaps/configuration/maintenance/formats/EncryptedPrefsFormatTest.kt +++ b/plugins/configuration/src/test/kotlin/app/aaps/configuration/maintenance/formats/EncryptedPrefsFormatTest.kt @@ -9,8 +9,8 @@ import app.aaps.core.main.utils.CryptoUtil import app.aaps.core.interfaces.maintenance.PrefMetadata import app.aaps.core.interfaces.resources.ResourceHelper import app.aaps.shared.tests.TestBase +import com.google.common.truth.Truth.assertThat import com.google.common.truth.TruthJUnit.assume -import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.mockito.ArgumentMatchers @@ -63,13 +63,16 @@ open class EncryptedPrefsFormatTest : TestBase() { assumeAES256isSupported(cryptoUtil) - Assertions.assertEquals(prefs.values.size, 2) - Assertions.assertEquals(prefs.values["key1"], "A") - Assertions.assertEquals(prefs.values["keyB"], "2") + assertThat(prefs.values).containsExactlyEntriesIn( + mapOf( + "key1" to "A", + "keyB" to "2", + ) + ) - Assertions.assertEquals(prefs.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]?.status, PrefsStatusImpl.OK) - Assertions.assertEquals(prefs.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]?.value, PrefsFormat.FORMAT_KEY_ENC) - Assertions.assertEquals(prefs.metadata[PrefsMetadataKeyImpl.ENCRYPTION]?.status, PrefsStatusImpl.OK) + assertThat(prefs.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]!!.status).isEqualTo(PrefsStatusImpl.OK) + assertThat(prefs.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]!!.value).isEqualTo(PrefsFormat.FORMAT_KEY_ENC) + assertThat(prefs.metadata[PrefsMetadataKeyImpl.ENCRYPTION]!!.status).isEqualTo(PrefsStatusImpl.OK) } @Test @@ -107,13 +110,15 @@ open class EncryptedPrefsFormatTest : TestBase() { assumeAES256isSupported(cryptoUtil) - Assertions.assertEquals(prefsOut.values.size, 2) - Assertions.assertEquals(prefsOut.values["testpref1"], "--1--") - Assertions.assertEquals(prefsOut.values["testpref2"], "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.values).containsExactlyEntriesIn( + mapOf( + "testpref1" to "--1--", + "testpref2" to "another", + ) + ) + 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 @@ -134,11 +139,11 @@ open class EncryptedPrefsFormatTest : TestBase() { val encryptedFormat = EncryptedPrefsFormat(rh, cryptoUtil, storage) 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) - Assertions.assertEquals(prefs.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]?.value, PrefsFormat.FORMAT_KEY_ENC) - Assertions.assertEquals(prefs.metadata[PrefsMetadataKeyImpl.ENCRYPTION]?.status, PrefsStatusImpl.ERROR) + assertThat(prefs.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]!!.status).isEqualTo(PrefsStatusImpl.OK) + assertThat(prefs.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]!!.value).isEqualTo(PrefsFormat.FORMAT_KEY_ENC) + assertThat(prefs.metadata[PrefsMetadataKeyImpl.ENCRYPTION]!!.status).isEqualTo(PrefsStatusImpl.ERROR) } @Test @@ -164,10 +169,10 @@ open class EncryptedPrefsFormatTest : TestBase() { assumeAES256isSupported(cryptoUtil) // 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 - Assertions.assertEquals(prefs.metadata[PrefsMetadataKeyImpl.ENCRYPTION]?.status, PrefsStatusImpl.ERROR) + assertThat(prefs.metadata[PrefsMetadataKeyImpl.ENCRYPTION]!!.status).isEqualTo(PrefsStatusImpl.ERROR) } @Test @@ -188,8 +193,8 @@ open class EncryptedPrefsFormatTest : TestBase() { val encryptedFormat = EncryptedPrefsFormat(rh, cryptoUtil, storage) val prefs = encryptedFormat.loadPreferences(getMockedFile(), "sikret") - Assertions.assertEquals(prefs.values.size, 0) - Assertions.assertEquals(prefs.metadata[PrefsMetadataKeyImpl.ENCRYPTION]?.status, PrefsStatusImpl.ERROR) + assertThat(prefs.values).isEmpty() + assertThat(prefs.metadata[PrefsMetadataKeyImpl.ENCRYPTION]!!.status).isEqualTo(PrefsStatusImpl.ERROR) } @Test @@ -203,8 +208,8 @@ open class EncryptedPrefsFormatTest : TestBase() { val encryptedFormat = EncryptedPrefsFormat(rh, cryptoUtil, storage) val prefs = encryptedFormat.loadPreferences(getMockedFile(), "sikret") - Assertions.assertEquals(prefs.values.size, 0) - Assertions.assertEquals(prefs.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]?.status, PrefsStatusImpl.ERROR) + assertThat(prefs.values).isEmpty() + assertThat(prefs.metadata[PrefsMetadataKeyImpl.FILE_FORMAT]!!.status).isEqualTo(PrefsStatusImpl.ERROR) } @Test