Skip to content

Commit

Permalink
Added some tests.| #1768
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Jun 22, 2022
1 parent 564f9bd commit c459517
Show file tree
Hide file tree
Showing 5 changed files with 462 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,32 @@ import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.RootMatchers.isDialog
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.rules.activityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.MediumTest
import com.flowcrypt.email.R
import com.flowcrypt.email.TestConstants
import com.flowcrypt.email.api.retrofit.ApiHelper
import com.flowcrypt.email.api.retrofit.response.api.EkmPrivateKeysResponse
import com.flowcrypt.email.api.retrofit.response.model.Key
import com.flowcrypt.email.api.retrofit.response.model.OrgRules
import com.flowcrypt.email.base.BaseTest
import com.flowcrypt.email.database.entity.KeyEntity
import com.flowcrypt.email.extensions.org.pgpainless.util.asString
import com.flowcrypt.email.model.KeyImportDetails
import com.flowcrypt.email.rules.AddAccountToDatabaseRule
import com.flowcrypt.email.rules.AddPrivateKeyToDatabaseRule
import com.flowcrypt.email.rules.ClearAppSettingsRule
import com.flowcrypt.email.rules.FlowCryptMockWebServerRule
import com.flowcrypt.email.rules.RetryRule
import com.flowcrypt.email.rules.ScreenshotTestRule
import com.flowcrypt.email.security.KeysStorageImpl
import com.flowcrypt.email.security.model.PgpKeyDetails
import com.flowcrypt.email.security.pgp.PgpKey
import com.flowcrypt.email.ui.activity.MainActivity
import com.flowcrypt.email.util.AccountDaoManager
import com.flowcrypt.email.ui.base.BaseRefreshKeysFromEkmFlowTest
import com.flowcrypt.email.util.PrivateKeysManager
import com.flowcrypt.email.util.exception.ApiException
import com.google.gson.Gson
import okhttp3.mockwebserver.Dispatcher
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.RecordedRequest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.rules.TestName
import org.junit.rules.TestRule
import org.junit.runner.RunWith
import org.pgpainless.util.Passphrase
Expand All @@ -61,46 +51,14 @@ import java.net.HttpURLConnection
*/
@MediumTest
@RunWith(AndroidJUnit4::class)
class RefreshKeysFromEkmFlowTest : BaseTest() {
private val userWithOrgRules = AccountDaoManager.getUserWithOrgRules(
OrgRules(
flags = listOf(
OrgRules.DomainRule.NO_PRV_CREATE,
OrgRules.DomainRule.NO_PRV_BACKUP,
OrgRules.DomainRule.FORBID_STORING_PASS_PHRASE
),
customKeyserverUrl = null,
keyManagerUrl = EKM_URL,
enforceKeygenAlgo = null,
enforceKeygenExpireMonths = null
)
).copy(email = "ekm@localhost:1212")

private val addAccountToDatabaseRule = AddAccountToDatabaseRule(userWithOrgRules)
class RefreshKeysFromEkmFlowTest : BaseRefreshKeysFromEkmFlowTest() {
private val addPrivateKeyToDatabaseRule = AddPrivateKeyToDatabaseRule(
accountEntity = addAccountToDatabaseRule.account,
keyPath = "pgp/[email protected]_prv_default.asc",
passphrase = TestConstants.DEFAULT_PASSWORD,
sourceType = KeyImportDetails.SourceType.EMAIL,
passphraseType = KeyEntity.PassphraseType.RAM
)
private val mockWebServerRule = FlowCryptMockWebServerRule(TestConstants.MOCK_WEB_SERVER_PORT,
object : Dispatcher() {
override fun dispatch(request: RecordedRequest): MockResponse {
val gson = ApiHelper.getInstance(getTargetContext()).gson

if (request.path?.startsWith("/ekm") == true) {
return handleEkmAPI(gson)
}

return MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND)
}
})

override val activityScenarioRule = activityScenarioRule<MainActivity>()

@get:Rule
val testNameRule = TestName()

@get:Rule
var ruleChain: TestRule = RuleChain
Expand All @@ -115,7 +73,10 @@ class RefreshKeysFromEkmFlowTest : BaseTest() {
@Test
fun testUpdatePrvKeyFromEkmSuccessSilent() {
val keysStorage = KeysStorageImpl.getInstance(getTargetContext())
addPassphraseToRamCache(keysStorage)
addPassphraseToRamCache(
keysStorage = keysStorage,
fingerprint = addPrivateKeyToDatabaseRule.pgpKeyDetails.fingerprint
)

//check existing key before updating
val existingPgpKeyDetailsBeforeUpdating = checkExistingKeyBeforeUpdating(keysStorage)
Expand Down Expand Up @@ -169,7 +130,10 @@ class RefreshKeysFromEkmFlowTest : BaseTest() {
@Test
fun testUpdatePrvKeyFromEkmShowApiError() {
val keysStorage = KeysStorageImpl.getInstance(getTargetContext())
addPassphraseToRamCache(keysStorage)
addPassphraseToRamCache(
keysStorage = keysStorage,
fingerprint = addPrivateKeyToDatabaseRule.pgpKeyDetails.fingerprint
)

//check existing key before updating
val existingPgpKeyDetailsBeforeUpdating = checkExistingKeyBeforeUpdating(keysStorage)
Expand Down Expand Up @@ -200,19 +164,7 @@ class RefreshKeysFromEkmFlowTest : BaseTest() {
return existingPgpKeyDetailsBeforeUpdating
}

private fun addPassphraseToRamCache(keysStorage: KeysStorageImpl) {
keysStorage.putPassphraseToCache(
fingerprint = addPrivateKeyToDatabaseRule.pgpKeyDetails.fingerprint,
passphrase = Passphrase.fromPassword(TestConstants.DEFAULT_PASSWORD),
validUntil = KeysStorageImpl.calculateLifeTimeForPassphrase(),
passphraseType = KeyEntity.PassphraseType.RAM
)
}

private fun handleEkmAPI(gson: Gson): MockResponse {
//simulate network operation to prevent too fast response
Thread.sleep(500)

override fun handleEkmAPI(gson: Gson): MockResponse {
return when (testNameRule.methodName) {
"testUpdatePrvKeyFromEkmSuccessSilent", "testUpdatePrvKeyFromEkmShowFixMissingPassphrase" ->
MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
Expand All @@ -227,7 +179,6 @@ class RefreshKeysFromEkmFlowTest : BaseTest() {
}

companion object {
private const val EKM_URL = "https://localhost:1212/ekm/"
private val EKM_KEY_WITH_EXTENDED_EXPIRATION = PrivateKeysManager.getPgpKeyDetailsFromAssets(
"pgp/[email protected]_prv_default.asc"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* © 2016-present FlowCrypt a.s. Limitations apply. Contact [email protected]
* Contributors: DenBond7
*/

package com.flowcrypt.email.ui

import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.MediumTest
import com.flowcrypt.email.R
import com.flowcrypt.email.TestConstants
import com.flowcrypt.email.api.retrofit.response.api.EkmPrivateKeysResponse
import com.flowcrypt.email.api.retrofit.response.model.Key
import com.flowcrypt.email.database.entity.KeyEntity
import com.flowcrypt.email.model.KeyImportDetails
import com.flowcrypt.email.rules.AddPrivateKeyToDatabaseRule
import com.flowcrypt.email.rules.ClearAppSettingsRule
import com.flowcrypt.email.rules.RetryRule
import com.flowcrypt.email.rules.ScreenshotTestRule
import com.flowcrypt.email.security.KeysStorageImpl
import com.flowcrypt.email.security.pgp.PgpKey
import com.flowcrypt.email.ui.base.BaseRefreshKeysFromEkmFlowTest
import com.flowcrypt.email.util.PrivateKeysManager
import com.google.gson.Gson
import okhttp3.mockwebserver.MockResponse
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.rules.TestRule
import org.junit.runner.RunWith
import org.pgpainless.util.Passphrase
import java.net.HttpURLConnection

/**
* @author Denis Bondarenko
* Date: 6/22/22
* Time: 6:39 PM
* E-mail: [email protected]
*/
@MediumTest
@RunWith(AndroidJUnit4::class)
class RefreshRevokedKeysFromEkmDeleteNotMatchingFlowTest : BaseRefreshKeysFromEkmFlowTest() {
private val addPrivateKeyToDatabaseRule = AddPrivateKeyToDatabaseRule(
accountEntity = addAccountToDatabaseRule.account,
keyPath = "pgp/[email protected]_fisrtKey_prv_default_revoked.asc",
passphrase = TestConstants.DEFAULT_PASSWORD,
sourceType = KeyImportDetails.SourceType.EMAIL,
passphraseType = KeyEntity.PassphraseType.RAM
)
private val addSecondPrivateKeyToDatabaseRule = AddPrivateKeyToDatabaseRule(
accountEntity = addAccountToDatabaseRule.account,
keyPath = TestConstants.DEFAULT_SECOND_KEY_PRV_STRONG,
passphrase = TestConstants.DEFAULT_STRONG_PASSWORD,
sourceType = KeyImportDetails.SourceType.EMAIL,
passphraseType = KeyEntity.PassphraseType.RAM
)

@get:Rule
var ruleChain: TestRule = RuleChain
.outerRule(RetryRule.DEFAULT)
.around(ClearAppSettingsRule())
.around(mockWebServerRule)
.around(addAccountToDatabaseRule)
.around(addPrivateKeyToDatabaseRule)
.around(addSecondPrivateKeyToDatabaseRule)
.around(activityScenarioRule)
.around(ScreenshotTestRule())

override fun handleEkmAPI(gson: Gson): MockResponse {
return when (testNameRule.methodName) {
"testDeleteNotMatchingKeys" ->
MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(gson.toJson(EKM_RESPONSE_SUCCESS_WITH_KEY_WHERE_MODIFICATION_DATE_AFTER_REVOKED))

else -> MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND)
}
}

@Test
fun testDeleteNotMatchingKeys() {
val keysStorage = KeysStorageImpl.getInstance(getTargetContext())
addPassphraseToRamCache(
keysStorage = keysStorage,
fingerprint = addPrivateKeyToDatabaseRule.pgpKeyDetails.fingerprint
)

//check existing keys before updating
val firstPgpKeyDetailsBeforeUpdating = keysStorage.getPgpKeyDetailsList()
.first { it.fingerprint == addPrivateKeyToDatabaseRule.pgpKeyDetails.fingerprint }
assertTrue(firstPgpKeyDetailsBeforeUpdating.isRevoked)

assertNotNull(keysStorage.getPgpKeyDetailsList()
.firstOrNull { it.fingerprint == addSecondPrivateKeyToDatabaseRule.pgpKeyDetails.fingerprint })

//we need to make a delay to wait while [KeysStorageImpl] will update internal data
Thread.sleep(2000)

//check existing keys after updating. We should have only the first key.
assertEquals(keysStorage.getPgpKeyDetailsList().size, 1)
assertNull(keysStorage.getPgpKeyDetailsList()
.firstOrNull { it.fingerprint == addSecondPrivateKeyToDatabaseRule.pgpKeyDetails.fingerprint })

val firstPgpKeyDetailsAfterUpdating = keysStorage.getPgpKeyDetailsList()
.first { it.fingerprint == firstPgpKeyDetailsBeforeUpdating.fingerprint }
assertTrue(firstPgpKeyDetailsAfterUpdating.isRevoked)
assertEquals(
firstPgpKeyDetailsBeforeUpdating.fingerprint,
firstPgpKeyDetailsAfterUpdating.fingerprint
)
assertEquals(
firstPgpKeyDetailsBeforeUpdating.lastModified,
firstPgpKeyDetailsAfterUpdating.lastModified
)

onView(withId(R.id.toolbar))
.check(matches(isDisplayed()))
}

companion object {
private val EKM_KEY_WITH_MODIFICATION_DATE_AFTER_REVOKED =
PrivateKeysManager.getPgpKeyDetailsFromAssets(
"pgp/[email protected]_fisrtKey_prv_default_mod_06_17_2022.asc"
)
private val EKM_RESPONSE_SUCCESS_WITH_KEY_WHERE_MODIFICATION_DATE_AFTER_REVOKED =
EkmPrivateKeysResponse(
privateKeys = listOf(
Key(
PgpKey.decryptKey(
requireNotNull(EKM_KEY_WITH_MODIFICATION_DATE_AFTER_REVOKED.privateKey),
Passphrase.fromPassword(TestConstants.DEFAULT_PASSWORD)
)
)
)
)
}
}
Loading

0 comments on commit c459517

Please sign in to comment.