0
0
mirror of https://github.com/markusfisch/BinaryEye.git synced 2024-09-20 03:52:16 +02:00

Add tests for MATMSG format

This commit is contained in:
Markus Fisch 2022-06-07 20:43:15 +02:00
parent 75a59bc3e9
commit 2bb413e3e8
4 changed files with 61 additions and 14 deletions

View File

@ -19,31 +19,37 @@ object MatMsgAction : IntentAction() {
context: Context,
data: ByteArray
): Intent? {
val encoded = String(data)
// Allow arbitrary order.
val to = encoded.extractFirst("""[:;]TO:([\w.%@+-]+);""")
val sub = encoded.extractFirst("""[:;]SUB:([^;]+);""")
val body = encoded.extractFirst("""[:;]BODY:([^;]+);""")
val mm = MatMsg(String(data))
// Allow incomplete but not completely missing data.
if (to == null && sub == null && body == null) {
if (mm.isEmpty()) {
return null
}
return Intent(
Intent.ACTION_SENDTO,
Uri.parse("mailto:")
).apply {
to?.let {
putExtra(Intent.EXTRA_EMAIL, arrayOf(to))
mm.to?.let {
putExtra(Intent.EXTRA_EMAIL, arrayOf(mm.to))
}
sub?.let {
putExtra(Intent.EXTRA_SUBJECT, sub)
mm.sub?.let {
putExtra(Intent.EXTRA_SUBJECT, mm.sub)
}
body?.let {
putExtra(Intent.EXTRA_TEXT, body)
mm.body?.let {
putExtra(Intent.EXTRA_TEXT, mm.body)
}
}
}
}
// Public so this can be tested.
class MatMsg(encoded: String) {
// Allow arbitrary order.
val to = encoded.extractFirst("""[:;]TO:([\w.%@+-]+);""")
val sub = encoded.extractFirst("""[:;]SUB:([^;]+);""")
val body = encoded.extractFirst("""[:;]BODY:([^;]+);""")
fun isEmpty() = to == null && sub == null && body == null
}
private fun String.extractFirst(regex: String) =
regex.toRegex().find(this)?.groupValues?.get(1)

View File

@ -0,0 +1,43 @@
package de.markusfisch.android.binaryeye.actions.mail
import junit.framework.TestCase.assertEquals
import org.junit.Test
class MatMsgTest {
@Test
fun to() {
val mm = MatMsg(
"MATMSG:TO:someone@example.org;;"
)
assertEquals(mm.to, "someone@example.org")
}
@Test
fun toSub() {
val mm = MatMsg(
"MATMSG:TO:someone@example.org;SUB:Stuff;;"
)
assertEquals(mm.to, "someone@example.org")
assertEquals(mm.sub, "Stuff")
}
@Test
fun toSubBody() {
val mm = MatMsg(
"MATMSG:TO:someone@example.org;SUB:Stuff;BODY:This is some text;;"
)
assertEquals(mm.to, "someone@example.org")
assertEquals(mm.sub, "Stuff")
assertEquals(mm.body, "This is some text")
}
@Test
fun differentOrder() {
val mm = MatMsg(
"MATMSG:SUB:Stuff;BODY:This is some text;TO:someone@example.org;;"
)
assertEquals(mm.to, "someone@example.org")
assertEquals(mm.sub, "Stuff")
assertEquals(mm.body, "This is some text")
}
}

View File

@ -1,6 +1,5 @@
package de.markusfisch.android.binaryeye.actions.vtype
import de.markusfisch.android.binaryeye.simpleFail
import junit.framework.TestCase.*
import org.junit.Test

View File

@ -1,6 +1,5 @@
package de.markusfisch.android.binaryeye.actions.web
import de.markusfisch.android.binaryeye.simpleFail
import junit.framework.TestCase.*
import org.junit.Test