package com.cottondb.android.ui.traffic.messages import android.net.Uri import android.os.CancellationSignal import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry import java.io.ByteArrayInputStream import java.io.File import java.io.InputStream import kotlinx.coroutines.runBlocking import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Test import 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()) } }