Cotton code
34 lines · 1.8 KB · KotlinRaw
package com.cottondb.android.ui.traffic.messagesimport android.net.Uriimport android.os.CancellationSignalimport androidx.test.ext.junit.runners.AndroidJUnit4import androidx.test.platform.app.InstrumentationRegistryimport java.io.ByteArrayInputStreamimport java.io.Fileimport java.io.InputStreamimport kotlinx.coroutines.runBlockingimport org.junit.Assert.assertFalseimport org.junit.Assert.assertTrueimport org.junit.Testimport org.junit.runner.RunWith@RunWith(AndroidJUnit4::class)class WhatsAppSafAdapterTest {    @Test fun wrongExtensionIsRefusedBeforeProviderBytesOpenAndSamePrefixWrongLeaseIsNotDiscarded(): Unit = runBlocking {        val root = File(InstrumentationRegistry.getInstrumentation().targetContext.noBackupFilesDir, "whatsapp-adapter-test").also { it.deleteRecursively(); check(it.mkdirs()) }        val source = CountingSource("chat.pdf")        val stager = AndroidWhatsAppExportStager(root, source)        try { stager.stage(Uri.parse("content://synthetic/pdf")); throw AssertionError("expected refusal") }        catch (_: WhatsAppExportStageException) { assertFalse(source.opened) }        val wrongLease = File(root, "whatsapp-synthetic.pdf").apply { writeBytes(byteArrayOf(1)) }        try { stager.discard(wrongLease.absolutePath); throw AssertionError("expected refusal") }        catch (_: WhatsAppExportStageException) { assertTrue(wrongLease.exists()) }        wrongLease.delete(); root.deleteRecursively()    }}private class CountingSource(private val name: String) : WhatsAppExportContentSource {    var opened = false    override fun displayName(uri: Uri, signal: CancellationSignal) = name    override fun open(uri: Uri, signal: CancellationSignal): InputStream { opened = true; return ByteArrayInputStream(byteArrayOf()) }}