diff --git a/FlowCrypt/src/androidTest/assets/pgp/old_key_on_wkd@flowcrypt.test_pub_0.asc b/FlowCrypt/src/androidTest/assets/pgp/old_key_on_wkd@flowcrypt.test_pub_0.asc new file mode 100644 index 0000000000..257eff4fc6 --- /dev/null +++ b/FlowCrypt/src/androidTest/assets/pgp/old_key_on_wkd@flowcrypt.test_pub_0.asc @@ -0,0 +1,13 @@ +-----BEGIN PGP PUBLIC KEY BLOCK----- + +mDMEZcoGXBYJKwYBBAHaRw8BAQdAiDbSgVEDXdBM/4WvpATVLxC4nSU3Vtift9Fr +Xlqu7tS0HW9sZF9rZXlfb25fd2tkQGZsb3djcnlwdC50ZXN0iI8EExYKAEEFAmXK +Bl0JECSmnjYlYOSiFiEEtax14ezyQTN8ub6qJKaeNiVg5KICngECmwMFFgIDAQAE +CwkIBwUVCgkICwKZAQAAtsUA/2wRnYiYkymQOgWGvwX/VWhg4JpGE2TiMugwNcp6 +wMLLAQDg55aPjHOnSypr4kdPqsY81191smgis3BCs32O2W3DCLg4BGXKBl0SCisG +AQQBl1UBBQEBB0AgLWAgSCNa3OU/zuTVIHcw9HJJPZ4p5Hx3d1Tt9ZG3JQMBCAeI +dQQYFgoAHQUCZcoGXQKeAQKbDAUWAgMBAAQLCQgHBRUKCQgLAAoJECSmnjYlYOSi +fXIA/1RuhAU1/d1CmzY5LYOpxlVys0Dcu7J5LyhHog4+nKuAAQDmWEtLZsLsINtC +QXZq2s52ZuNxJsOwo5AGlnQbQnUICA== +=zgwx +-----END PGP PUBLIC KEY BLOCK----- diff --git a/FlowCrypt/src/androidTest/assets/pgp/old_key_on_wkd@flowcrypt.test_pub_1.asc b/FlowCrypt/src/androidTest/assets/pgp/old_key_on_wkd@flowcrypt.test_pub_1.asc new file mode 100644 index 0000000000..73c5227b24 --- /dev/null +++ b/FlowCrypt/src/androidTest/assets/pgp/old_key_on_wkd@flowcrypt.test_pub_1.asc @@ -0,0 +1,17 @@ +-----BEGIN PGP PUBLIC KEY BLOCK----- + +mDMEZcoGXBYJKwYBBAHaRw8BAQdAiDbSgVEDXdBM/4WvpATVLxC4nSU3Vtift9Fr +Xlqu7tS0HW9sZF9rZXlfb25fd2tkQGZsb3djcnlwdC50ZXN0iI8EExYKAEEFAmXK +Bl0JECSmnjYlYOSiFiEEtax14ezyQTN8ub6qJKaeNiVg5KICngECmwMFFgIDAQAE +CwkIBwUVCgkICwKZAQAAtsUA/2wRnYiYkymQOgWGvwX/VWhg4JpGE2TiMugwNcp6 +wMLLAQDg55aPjHOnSypr4kdPqsY81191smgis3BCs32O2W3DCLQfb2xkX2tleV9v +bl93a2RfMUBmbG93Y3J5cHQudGVzdIiQBBMWCAA4FiEEtax14ezyQTN8ub6qJKae +NiVg5KIFAmXKB4gCGwMFCwkIBwIGFQoJCAsCBBYCAwECHgECF4AACgkQJKaeNiVg +5KLnyQEAk7JJpvJtB/JrK0cZVXZYtJNLdwqisLrzcWP/93fsysgA/2ZhoeRTEvop +Uq7ZhKZE4oEJEJkxoKzHpzXSPaiGJsoPuDgEZcoGXRIKKwYBBAGXVQEFAQEHQCAt +YCBII1rc5T/O5NUgdzD0ckk9ninkfHd3VO31kbclAwEIB4h1BBgWCgAdBQJlygZd +Ap4BApsMBRYCAwEABAsJCAcFFQoJCAsACgkQJKaeNiVg5KJ9cgD/VG6EBTX93UKb +Njktg6nGVXKzQNy7snkvKEeiDj6cq4ABAOZYS0tmwuwg20JBdmraznZm43Emw7Cj +kAaWdBtCdQgI +=ILwT +-----END PGP PUBLIC KEY BLOCK----- diff --git a/FlowCrypt/src/androidTest/assets/pgp/user_with_few_keys_from_wkd@flowcrypt.test_pub.asc b/FlowCrypt/src/androidTest/assets/pgp/user_with_few_keys_from_wkd@flowcrypt.test_pub.asc new file mode 100644 index 0000000000..c7a99f7f6e --- /dev/null +++ b/FlowCrypt/src/androidTest/assets/pgp/user_with_few_keys_from_wkd@flowcrypt.test_pub.asc @@ -0,0 +1,29 @@ +-----BEGIN PGP PUBLIC KEY BLOCK----- +Version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AB0FAmXKC3UCngECmwwFFgIDAQAECwkIBwUVCgkI +CwAKCRAixavp2+3aeXhkAP4p1RFLm4T2JMjHv7/uotuCmBsbvn7tiNjYd+IS7np5 +tgEA6T3uLgGM5F0RK78O7gZG7S5jrESMxj4vpBu5qlYRbwc= +=4RFZ +-----END PGP PUBLIC KEY BLOCK----- diff --git a/FlowCrypt/src/androidTest/java/com/flowcrypt/email/ui/ComposeScreenEnterpriseFlowTest.kt b/FlowCrypt/src/androidTest/java/com/flowcrypt/email/ui/ComposeScreenEnterpriseFlowTest.kt new file mode 100644 index 0000000000..fa7dcacb5b --- /dev/null +++ b/FlowCrypt/src/androidTest/java/com/flowcrypt/email/ui/ComposeScreenEnterpriseFlowTest.kt @@ -0,0 +1,104 @@ +/* + * © 2016-present FlowCrypt a.s. Limitations apply. Contact human@flowcrypt.com + * Contributors: DenBond7 + */ + +package com.flowcrypt.email.ui + +import androidx.recyclerview.widget.RecyclerView +import androidx.test.espresso.Espresso.onView +import androidx.test.espresso.action.ViewActions.click +import androidx.test.espresso.assertion.ViewAssertions.matches +import androidx.test.espresso.contrib.RecyclerViewActions.scrollTo +import androidx.test.espresso.matcher.ViewMatchers.isDisplayed +import androidx.test.espresso.matcher.ViewMatchers.withId +import androidx.test.espresso.matcher.ViewMatchers.withText +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.database.entity.PublicKeyEntity +import com.flowcrypt.email.database.entity.RecipientEntity +import com.flowcrypt.email.extensions.kotlin.asInternetAddress +import com.flowcrypt.email.junit.annotations.EnterpriseTest +import com.flowcrypt.email.matchers.CustomMatchers.Companion.withChipsBackgroundColor +import com.flowcrypt.email.rules.AddPrivateKeyToDatabaseRule +import com.flowcrypt.email.rules.ClearAppSettingsRule +import com.flowcrypt.email.rules.FlowCryptMockWebServerRule +import com.flowcrypt.email.rules.GrantPermissionRuleChooser +import com.flowcrypt.email.rules.RetryRule +import com.flowcrypt.email.rules.ScreenshotTestRule +import com.flowcrypt.email.ui.adapter.RecipientChipRecyclerViewAdapter +import com.flowcrypt.email.ui.base.BaseComposeScreenTest +import com.flowcrypt.email.util.TestGeneralUtil +import okhttp3.mockwebserver.Dispatcher +import okhttp3.mockwebserver.MockResponse +import okhttp3.mockwebserver.RecordedRequest +import org.hamcrest.Matchers.allOf +import org.junit.ClassRule +import org.junit.Rule +import org.junit.Test +import org.junit.rules.RuleChain +import org.junit.rules.TestRule +import org.junit.runner.RunWith +import java.net.HttpURLConnection + +/** + * @author Denys Bondarenko + */ +@MediumTest +@RunWith(AndroidJUnit4::class) +@EnterpriseTest +class ComposeScreenEnterpriseFlowTest : BaseComposeScreenTest() { + + @get:Rule + var ruleChain: TestRule = RuleChain + .outerRule(RetryRule.DEFAULT) + .around(ClearAppSettingsRule()) + .around(GrantPermissionRuleChooser.grant(android.Manifest.permission.POST_NOTIFICATIONS)) + .around(addAccountToDatabaseRule) + .around(AddPrivateKeyToDatabaseRule()) + .around(activeActivityRule) + .around(ScreenshotTestRule()) + + @Test + fun testShowWarningIfFoundNotUsableKeySHA1() { + val keyWithSHA1Algo = + TestGeneralUtil.readFileFromAssetsAsByteArray("pgp/sha1@flowcrypt.test_pub.asc") + val email = "sha1@flowcrypt.test" + roomDatabase.recipientDao().insert(RecipientEntity(email = email)) + roomDatabase.pubKeyDao().insert( + PublicKeyEntity( + recipient = email, + fingerprint = "5DE92AB364B3100D89FBF460241512660BDDC426", + publicKey = keyWithSHA1Algo + ) + ) + + activeActivityRule?.launch(intent) + registerAllIdlingResources() + + fillInAllFields(to = setOf(requireNotNull(email.asInternetAddress()))) + + onView(withId(R.id.recyclerViewChipsTo)) + .perform( + scrollTo( + allOf( + withText(email), + withChipsBackgroundColor( + getTargetContext(), + RecipientChipRecyclerViewAdapter.CHIP_COLOR_RES_ID_NO_USABLE_PUB_KEY + ) + ) + ) + ) + + onView(withId(R.id.menuActionSend)) + .check(matches(isDisplayed())) + .perform(click()) + + onView(withText(R.string.warning_one_of_recipients_has_not_usable_pub_key)) + .check(matches(isDisplayed())) + .perform(click()) + } +} diff --git a/FlowCrypt/src/androidTest/java/com/flowcrypt/email/ui/ComposeScreenFlowTest.kt b/FlowCrypt/src/androidTest/java/com/flowcrypt/email/ui/ComposeScreenFlowTest.kt index 9621386d7b..b5e39cf0f8 100644 --- a/FlowCrypt/src/androidTest/java/com/flowcrypt/email/ui/ComposeScreenFlowTest.kt +++ b/FlowCrypt/src/androidTest/java/com/flowcrypt/email/ui/ComposeScreenFlowTest.kt @@ -73,6 +73,7 @@ import org.hamcrest.Matchers.allOf import org.hamcrest.Matchers.emptyString import org.hamcrest.Matchers.`is` import org.hamcrest.Matchers.not +import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.BeforeClass import org.junit.ClassRule @@ -82,6 +83,7 @@ import org.junit.rules.RuleChain import org.junit.rules.TemporaryFolder import org.junit.rules.TestRule import org.junit.runner.RunWith +import org.pgpainless.key.info.KeyRingInfo import java.io.File import java.net.HttpURLConnection import java.time.Instant @@ -625,7 +627,6 @@ class ComposeScreenFlowTest : BaseComposeScreenTest() { } @Test - //fun testShowWarningIfFoundNotUsableKeySHA1() { fun testAcceptIfFoundKeySHA1() { val keyWithSHA1Algo = TestGeneralUtil.readFileFromAssetsAsByteArray("pgp/sha1@flowcrypt.test_pub.asc") @@ -656,14 +657,6 @@ class ComposeScreenFlowTest : BaseComposeScreenTest() { ) ) ) - - /*temporary disabled due too https://github.com/FlowCrypt/flowcrypt-android/issues/1478 - onView(withId(R.id.menuActionSend)) - .check(matches(isDisplayed())) - .perform(click()) - onView(withText(R.string.warning_one_of_pub_keys_is_not_usable)) - .check(matches(isDisplayed())) - .perform(click())*/ } @Test @@ -704,6 +697,95 @@ class ComposeScreenFlowTest : BaseComposeScreenTest() { ) ) ) + + val existingRecipientAfterUpdate = + roomDatabase.recipientDao().getRecipientWithPubKeysByEmail(internetAddress.address) + ?: throw IllegalArgumentException("Contact not found") + + val existingKeyExpirationAfterUpdate = + PgpKey.parseKeys(String(existingRecipientAfterUpdate.publicKeys.first().publicKey)) + .pgpKeyRingCollection.pgpPublicKeyRingCollection.first().expiration + ?: throw IllegalArgumentException("No expiration date") + + assertTrue(existingKeyExpirationAfterUpdate.isAfter(Instant.now())) + } + + @Test + fun testKeepPublicKeysFreshDoNotUpdateIfReceivedOlderKey() { + val keyDetailsFromAssets = + PrivateKeysManager.getPgpKeyDetailsFromAssets("pgp/old_key_on_wkd@flowcrypt.test_pub_1.asc") + val internetAddress = requireNotNull(keyDetailsFromAssets.getPrimaryInternetAddress()) + val recipientEntity = keyDetailsFromAssets.toRecipientEntity() + roomDatabase.recipientDao().insert(requireNotNull(recipientEntity)) + roomDatabase.pubKeyDao().insert( + requireNotNull(keyDetailsFromAssets.toPublicKeyEntity(recipientEntity.email)) + ) + val existingRecipient = + roomDatabase.recipientDao().getRecipientWithPubKeysByEmail(internetAddress.address) + ?: throw IllegalArgumentException("Contact not found") + + val existingKeyBeforeUpdate = + PgpKey.parseKeys(String(existingRecipient.publicKeys.first().publicKey)) + .pgpKeyRingCollection.pgpPublicKeyRingCollection.first() + val keyRingInfoBeforeUpdate = KeyRingInfo(existingKeyBeforeUpdate) + + assertEquals(2, keyRingInfoBeforeUpdate.userIds.size) + + activeActivityRule?.launch(intent) + registerAllIdlingResources() + + fillInAllFields(to = setOf(internetAddress)) + + onView(withId(R.id.recyclerViewChipsTo)) + .perform( + RecyclerViewActions.scrollTo( + allOf( + withText(internetAddress.address), + withChipsBackgroundColor( + getTargetContext(), + R.color.colorPrimary + ) + ) + ) + ) + + val existingRecipientAfterUpdate = + roomDatabase.recipientDao().getRecipientWithPubKeysByEmail(internetAddress.address) + ?: throw IllegalArgumentException("Contact not found") + + val existingKeyBeforeUpdateUpdate = + PgpKey.parseKeys(String(existingRecipientAfterUpdate.publicKeys.first().publicKey)) + .pgpKeyRingCollection.pgpPublicKeyRingCollection.first() + val keyRingInfoAfterUpdate = KeyRingInfo(existingKeyBeforeUpdateUpdate) + + assertEquals(2, keyRingInfoAfterUpdate.userIds.size) + } + + @Test + fun testKeepPublicKeysFreshFewKeysFromServer() { + activeActivityRule?.launch(intent) + registerAllIdlingResources() + + fillInAllFields(to = setOf(requireNotNull(USER_WITH_FEW_KEYS_FROM_WKD.asInternetAddress()))) + + onView(withId(R.id.recyclerViewChipsTo)) + .perform( + RecyclerViewActions.scrollTo( + allOf( + withText(USER_WITH_FEW_KEYS_FROM_WKD), + withChipsBackgroundColor( + getTargetContext(), + R.color.colorPrimary + ) + ) + ) + ) + + val existingRecipientAfterUpdate = + roomDatabase.recipientDao().getRecipientWithPubKeysByEmail(USER_WITH_FEW_KEYS_FROM_WKD) + ?: throw IllegalArgumentException("Contact not found") + + assertEquals(2, existingRecipientAfterUpdate.publicKeys.size) } @Test @@ -792,6 +874,7 @@ class ComposeScreenFlowTest : BaseComposeScreenTest() { private const val ATTACHMENTS_COUNT = 3 private const val EMAIL_SUBJECT = "Test subject" + private const val USER_WITH_FEW_KEYS_FROM_WKD = "user_with_few_keys_from_wkd@flowcrypt.test" private var atts: MutableList = mutableListOf() @@ -833,6 +916,26 @@ class ComposeScreenFlowTest : BaseComposeScreenTest() { ) ) } + + "old_key_on_wkd@flowcrypt.test".equals(lastSegment, true) -> { + return MockResponse() + .setResponseCode(HttpURLConnection.HTTP_OK) + .setBody( + TestGeneralUtil.readFileFromAssetsAsString( + "pgp/old_key_on_wkd@flowcrypt.test_pub_0.asc" + ) + ) + } + + USER_WITH_FEW_KEYS_FROM_WKD.equals(lastSegment, true) -> { + return MockResponse() + .setResponseCode(HttpURLConnection.HTTP_OK) + .setBody( + TestGeneralUtil.readFileFromAssetsAsString( + "pgp/user_with_few_keys_from_wkd@flowcrypt.test_pub.asc" + ) + ) + } } } diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/CreateMessageFragment.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/CreateMessageFragment.kt index 5cafbae179..756d2db6ce 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/CreateMessageFragment.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/CreateMessageFragment.kt @@ -746,6 +746,11 @@ class CreateMessageFragment : BaseFragment(), } else continue } + if (!recipientWithPubKeys.hasUsablePubKey()) { + showInfoDialog(dialogMsg = getString(R.string.warning_one_of_recipients_has_not_usable_pub_key)) + return true + } + if (!recipientWithPubKeys.hasNotExpiredPubKey()) { showInfoDialog(dialogMsg = getString(R.string.warning_one_of_recipients_has_expired_pub_key)) return true @@ -755,11 +760,6 @@ class CreateMessageFragment : BaseFragment(), showInfoDialog(dialogMsg = getString(R.string.warning_one_of_recipients_has_revoked_pub_key)) return true } - - if (!recipientWithPubKeys.hasUsablePubKey()) { - showInfoDialog(dialogMsg = getString(R.string.warning_one_of_recipients_has_not_usable_pub_key)) - return true - } } return false