From 9ed061d29453ade1ad7e70e1e8b7b6a8477a8272 Mon Sep 17 00:00:00 2001 From: DenBond7 Date: Thu, 12 Aug 2021 16:40:00 +0300 Subject: [PATCH] Added more tests. Refactored code.| #1201 --- .../keys/wkd_advanced_pub@localhost_pub.asc | Bin 0 -> 380 bytes .../pgp/keys/wkd_direct_pub@localhost_pub.asc | Bin 0 -> 378 bytes .../activity/CreateMessageActivityWkdTest.kt | 180 +++++++++++++++++- .../email/util/BetterEmailAddress.kt | 74 +++---- 4 files changed, 212 insertions(+), 42 deletions(-) create mode 100644 FlowCrypt/src/androidTest/assets/pgp/keys/wkd_advanced_pub@localhost_pub.asc create mode 100644 FlowCrypt/src/androidTest/assets/pgp/keys/wkd_direct_pub@localhost_pub.asc diff --git a/FlowCrypt/src/androidTest/assets/pgp/keys/wkd_advanced_pub@localhost_pub.asc b/FlowCrypt/src/androidTest/assets/pgp/keys/wkd_advanced_pub@localhost_pub.asc new file mode 100644 index 0000000000000000000000000000000000000000..099147a8b9483ea7f50c704b15e8c3a7e5ed433a GIT binary patch literal 380 zcmbPX%#tX|y-ti%n~jl$@s>M3BO|+mV=I$z(ULdcrZ?oC_dCQUyX36?che~eiGdq~ z=A1hxlC?#uJUb;mF{LaqFF7?OzMwS8AtygMF()IxxTK?kMOciBL4lPCWG<65GpiUA zGa~~FHzx->t0)&I2RD-(Baj^i`+#B?}~+uqkEu@8YbxH+1!<*HWK&4*V9` z8?x}Xk!skrkI%%aRtwam+t9vFuCG_R@ptq5 zX@~iDSb)7E#0B=s^iW1tkYDslW~MG+7v3}DTjjN!&s}ML?8jZ!`xLl}27li(&Ez{5 zGb0CkM=6U0(9NXCqxnlEni$9;wx1dCQR;mB?>`#)% u_HLi^Glb!P`PYd8YfX6X+C3M$yi$CpAm6N#!nJ|#9nIEP^Sn^`!vg>%z=~o3 literal 0 HcmV?d00001 diff --git a/FlowCrypt/src/androidTest/assets/pgp/keys/wkd_direct_pub@localhost_pub.asc b/FlowCrypt/src/androidTest/assets/pgp/keys/wkd_direct_pub@localhost_pub.asc new file mode 100644 index 0000000000000000000000000000000000000000..6e98aa6da90e1a64fa4d55b127087c528d4b477b GIT binary patch literal 378 zcmbPX%#tXowoQyvn~jl$@s>M3BO|-RH_y}e8<-kaEHypmB%@by@aOW%-17Rjb~|m> zMLwQ=W%(9~^6ZrOl+2>ki?&Ta%HYYnp`IgOpW~*Tu43&uspE z@~YO(n}0q38YTT*``=SgwT0oo!=!-d_NddE%{8j;KG*Ncaj9_sB5ZZLZKv(>gb7n` zaqqAId1Si~7uX}yLm62?KAA5VerkhO-v03O(;L^6>uilo^VE>L)n&_K_NexiR9y}; zBL{m&DT@Tq&9X2z^I&r`vV*m_8U86VoxkRPgTpk_LV2ujB8JPGE~0y;eFG! rhTv*bMuz_Wwh4xoB{m`AJq{dCl^bFXEUbNSFFow& { + MockResponse().setResponseCode(HttpURLConnection.HTTP_OK) + } + + "testWkdAdvancedTimeOutWkdDirectAvailable", + "testWkdAdvancedTimeOutWkdDirectTimeOut" -> { + Thread.sleep(5000) + MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND) + } + + else -> MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND) + } } private fun handleAdvancedWkdRequest(): MockResponse { return when (testNameRule.methodName) { - "testWkdNoResult" -> { + "testWkdAdvancedNoResult" -> { MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND) } + "testWkdAdvancedPub" -> { + MockResponse() + .setResponseCode(HttpURLConnection.HTTP_OK) + .setBody( + Buffer().write( + TestGeneralUtil.readFileFromAssetsAsByteArray( + "pgp/keys/wkd_advanced_pub@localhost_pub.asc" + ) + ) + ) + } + else -> MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND) } } private fun handleDirectPolicyRequest(): MockResponse { - return MockResponse().setResponseCode(HttpURLConnection.HTTP_OK) + return when (testNameRule.methodName) { + "testWkdAdvancedSkippedWkdDirectNoResult", + "testWkdAdvancedSkippedWkdDirectPub", + "testWkdAdvancedTimeOutWkdDirectAvailable" -> { + MockResponse().setResponseCode(HttpURLConnection.HTTP_OK) + } + + "testWkdAdvancedTimeOutWkdDirectTimeOut" -> { + Thread.sleep(5000) + MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND) + } + + else -> MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND) + } } private fun handleDirectWkdRequest(): MockResponse { - return MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_ACCEPTABLE) + return when (testNameRule.methodName) { + "testWkdAdvancedSkippedWkdDirectNoResult" -> { + MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND) + } + + "testWkdAdvancedSkippedWkdDirectPub", + "testWkdAdvancedTimeOutWkdDirectAvailable" -> { + MockResponse() + .setResponseCode(HttpURLConnection.HTTP_OK) + .setBody( + Buffer().write( + TestGeneralUtil.readFileFromAssetsAsByteArray( + "pgp/keys/wkd_direct_pub@localhost_pub.asc" + ) + ) + ) + } + + else -> MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND) + } } } diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/util/BetterEmailAddress.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/util/BetterEmailAddress.kt index 83264ee9ee..e0284d5c5e 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/util/BetterEmailAddress.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/util/BetterEmailAddress.kt @@ -10,46 +10,12 @@ import com.flowcrypt.email.extensions.kotlin.isValidEmail // https://en.wikipedia.org/wiki/Email_address#Internationalization_examples class BetterInternetAddress(str: String, verifySpecialCharacters: Boolean = true) { - - companion object { - const val alphanum = "\\p{L}\\u0900-\\u097F0-9" - const val validEmail = "(?:[${alphanum}!#\$%&'*+/=?^_`{|}~-]+(?:\\.[${alphanum}!#\$%&'*+/=?^" + - "_`{|}~-]+)*|\"(?:[\\x01-\\x08" + - "\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f" + - "])*\")@(?:(?:[${alphanum}](?:[${alphanum}-]*[${alphanum}])?\\.)+[${alphanum}](?:[" + - "${alphanum}-]*[${alphanum}])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:" + - "25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[${alphanum}-]*[${alphanum}]:(?:[\\x01-\\x08\\x0b" + - "\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)])" - private const val validPersonalNameWithEmail = - "([$alphanum\\p{Punct}\\p{Space}]*)<($validEmail)>" - - private val validEmailRegex = validEmail.toRegex() - private val validPersonalNameWithEmailRegex = validPersonalNameWithEmail.toRegex() - // if these appear in the display-name they must be double quoted - private val containsSpecialCharacterRegex = ".*[()<>\\[\\]:;@\\\\,.\"].*".toRegex() - // double quotes at ends only - private val doubleQuotedTextRegex = "\"[^\"]*\"".toRegex() - private val validLocalhostEmailRegex = Regex("([a-zA-z])([a-zA-z0-9])+@localhost") - - fun isValidEmail(email: String): Boolean { - return validEmailRegex.matchEntire(email) != null - } - - fun isValidLocalhostEmail(email: String): Boolean { - return validLocalhostEmailRegex.matchEntire(email) != null - } - - fun areValidEmails(emails: Iterable): Boolean { - return emails.all { it.isValidEmail() } - } - } - val personalName: String? val emailAddress: String init { val personalNameWithEmailMatch = validPersonalNameWithEmailRegex.find(str) - val emailMatch = str.matches(validEmailRegex) + val emailMatch = str.matches(validEmailRegex) || str.matches(validLocalhostEmailRegex) when { personalNameWithEmailMatch != null -> { val group = personalNameWithEmailMatch.groupValues @@ -65,11 +31,49 @@ class BetterInternetAddress(str: String, verifySpecialCharacters: Boolean = true ) } } + emailMatch -> { personalName = null emailAddress = str } + else -> throw IllegalArgumentException("Invalid email $str") } } + + companion object { + private const val ALPHANUM = "\\p{L}\\u0900-\\u097F0-9" + private const val VALID_EMAIL = + "(?:[${ALPHANUM}!#\$%&'*+/=?^_`{|}~-]+(?:\\.[${ALPHANUM}!#\$%&'*+/=?^" + + "_`{|}~-]+)*|\"(?:[\\x01-\\x08" + + "\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f" + + "])*\")@(?:(?:[${ALPHANUM}](?:[${ALPHANUM}-]*[${ALPHANUM}])?\\.)+[${ALPHANUM}](?:[" + + "${ALPHANUM}-]*[${ALPHANUM}])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:" + + "25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[${ALPHANUM}-]*[${ALPHANUM}]:(?:[\\x01-\\x08\\x0b" + + "\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)])" + private const val VALID_PERSONAL_NAME_WITH_EMAIL = + "([$ALPHANUM\\p{Punct}\\p{Space}]*)<($VALID_EMAIL)>" + + private val validEmailRegex = VALID_EMAIL.toRegex() + private val validPersonalNameWithEmailRegex = VALID_PERSONAL_NAME_WITH_EMAIL.toRegex() + + // if these appear in the display-name they must be double quoted + private val containsSpecialCharacterRegex = ".*[()<>\\[\\]:;@\\\\,.\"].*".toRegex() + + // double quotes at ends only + private val doubleQuotedTextRegex = "\"[^\"]*\"".toRegex() + private val validLocalhostEmailRegex = Regex("([a-zA-z])([a-zA-z0-9])+@localhost") + + fun isValidEmail(email: String): Boolean { + return validEmailRegex.matchEntire(email) != null + } + + fun isValidLocalhostEmail(email: String): Boolean { + return validLocalhostEmailRegex.matchEntire(email) != null + } + + fun areValidEmails(emails: Iterable): Boolean { + return emails.all { it.isValidEmail() } + } + } }