mirror of
https://github.com/bleackstorm/satiea.git
synced 2026-12-03 07:41:56 +01:00
159 lines
5.9 KiB
Kotlin
159 lines
5.9 KiB
Kotlin
package com.example
|
|
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import android.graphics.Bitmap
|
|
import android.graphics.Color
|
|
import android.provider.MediaStore
|
|
import androidx.test.core.app.ApplicationProvider
|
|
import com.example.util.ImageUtils
|
|
import kotlinx.coroutines.test.runTest
|
|
import org.junit.Assert.assertEquals
|
|
import org.junit.Assert.assertFalse
|
|
import org.junit.Assert.assertNotNull
|
|
import org.junit.Assert.assertNull
|
|
import org.junit.Assert.assertTrue
|
|
import org.junit.Before
|
|
import org.junit.Test
|
|
import org.junit.runner.RunWith
|
|
import org.robolectric.RobolectricTestRunner
|
|
import org.robolectric.annotation.Config
|
|
import java.io.File
|
|
import java.io.FileOutputStream
|
|
|
|
@RunWith(RobolectricTestRunner::class)
|
|
@Config(sdk = [36])
|
|
class ImageUtilsTest {
|
|
|
|
private lateinit var context: Context
|
|
|
|
@Before
|
|
fun setUp() {
|
|
context = ApplicationProvider.getApplicationContext()
|
|
// Robolectric creates a fresh cacheDir per test, while FileProvider caches PathStrategy statically.
|
|
runCatching {
|
|
val cacheField = androidx.core.content.FileProvider::class.java.getDeclaredField("sCache")
|
|
cacheField.isAccessible = true
|
|
(cacheField.get(null) as? MutableMap<*, *>)?.clear()
|
|
}
|
|
}
|
|
|
|
@Test
|
|
fun deletePhotoFile_validPhotoInsideAllowedDirectory_deletesSuccessfully() {
|
|
val photosDir = ImageUtils.getPhotosDirectory(context)
|
|
val testFile = File(photosDir, "test_photo.jpg").apply {
|
|
writeText("dummy content")
|
|
}
|
|
assertTrue(testFile.exists())
|
|
|
|
val deleted = ImageUtils.deletePhotoFile(context, "file://${testFile.absolutePath}")
|
|
assertTrue(deleted)
|
|
assertFalse(testFile.exists())
|
|
}
|
|
|
|
@Test
|
|
fun deletePhotoFile_pathTraversalAttempt_isBlockedAndFilePreserved() {
|
|
val sensitiveDir = File(context.filesDir, "databases").apply { mkdirs() }
|
|
val sensitiveFile = File(sensitiveDir, "app_database.db").apply {
|
|
writeText("sensitive data")
|
|
}
|
|
assertTrue(sensitiveFile.exists())
|
|
|
|
val maliciousUri = "file://${context.filesDir.absolutePath}/meal_photos/../databases/app_database.db"
|
|
val deleted = ImageUtils.deletePhotoFile(context, maliciousUri)
|
|
|
|
assertFalse("Path traversal attempt should be rejected", deleted)
|
|
assertTrue("Sensitive file outside allowed folder must not be deleted", sensitiveFile.exists())
|
|
}
|
|
|
|
@Test
|
|
fun deletePhotoFile_externalAbsoluteFile_isBlocked() {
|
|
val otherFile = File(context.cacheDir, "cached_secret.txt").apply {
|
|
writeText("secret")
|
|
}
|
|
assertTrue(otherFile.exists())
|
|
|
|
val deleted = ImageUtils.deletePhotoFile(context, "file://${otherFile.absolutePath}")
|
|
assertFalse(deleted)
|
|
assertTrue(otherFile.exists())
|
|
}
|
|
|
|
@Test
|
|
fun deletePhotoFile_uncontextualMethod_blocksTraversal() {
|
|
assertFalse(ImageUtils.deletePhotoFile("/data/data/com.example/databases/secret.db"))
|
|
assertFalse(ImageUtils.deletePhotoFile("../meal_photos/secret.db"))
|
|
}
|
|
|
|
@Test
|
|
fun createReceiptCaptureUri_preCreatesFileAndReturnsFileProviderUri() {
|
|
val uri = ImageUtils.createReceiptCaptureUri(context)
|
|
assertEquals("content", uri.scheme)
|
|
assertTrue(uri.authority.orEmpty().endsWith(".fileprovider"))
|
|
assertTrue(uri.toString().contains("receipt_scans"))
|
|
|
|
val scanDir = File(context.cacheDir, "receipt_scans")
|
|
assertTrue(scanDir.exists())
|
|
assertTrue(scanDir.listFiles()?.isNotEmpty() == true)
|
|
}
|
|
|
|
@Test
|
|
fun buildCameraCaptureIntent_setsExtraOutputClipDataAndUriGrantFlags() {
|
|
val uri = ImageUtils.createReceiptCaptureUri(context)
|
|
val intent = ImageUtils.buildCameraCaptureIntent(context, uri)
|
|
|
|
assertEquals(MediaStore.ACTION_IMAGE_CAPTURE, intent.action)
|
|
@Suppress("DEPRECATION")
|
|
assertEquals(uri, intent.getParcelableExtra(MediaStore.EXTRA_OUTPUT))
|
|
assertNotNull(intent.clipData)
|
|
assertEquals(uri, intent.clipData?.getItemAt(0)?.uri)
|
|
assertTrue((intent.flags and Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0)
|
|
assertTrue((intent.flags and Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0)
|
|
}
|
|
|
|
@Test
|
|
fun loadResizedBitmapForAi_decodesValidFileProviderJpeg() = runTest {
|
|
val uri = ImageUtils.createReceiptCaptureUri(context)
|
|
val bmp = Bitmap.createBitmap(400, 300, Bitmap.Config.ARGB_8888).apply {
|
|
eraseColor(Color.WHITE)
|
|
}
|
|
context.contentResolver.openOutputStream(uri)?.use { out ->
|
|
bmp.compress(Bitmap.CompressFormat.JPEG, 90, out)
|
|
}
|
|
bmp.recycle()
|
|
|
|
val loaded = ImageUtils.loadResizedBitmapForAi(context, uri, maxDimension = 200)
|
|
assertNotNull(loaded)
|
|
assertTrue(loaded!!.width <= 200 && loaded.height <= 200)
|
|
loaded.recycle()
|
|
}
|
|
|
|
@Test
|
|
fun loadResizedBitmapForAi_emptyFileReturnsNull() = runTest {
|
|
val uri = ImageUtils.createReceiptCaptureUri(context)
|
|
val loaded = ImageUtils.loadResizedBitmapForAi(context, uri, maxDimension = 200)
|
|
assertNull(loaded)
|
|
}
|
|
|
|
@Test
|
|
fun resolveCapturedReceiptUri_fallsBackToInlineBitmapWhenExpectedFileIsEmpty() = runTest {
|
|
val emptyExpectedUri = ImageUtils.createReceiptCaptureUri(context)
|
|
val inlineBmp = Bitmap.createBitmap(120, 160, Bitmap.Config.ARGB_8888).apply {
|
|
eraseColor(Color.LTGRAY)
|
|
}
|
|
val resultIntent = Intent().putExtra("data", inlineBmp)
|
|
|
|
val resolved = ImageUtils.resolveCapturedReceiptUri(
|
|
context = context,
|
|
expectedUri = emptyExpectedUri,
|
|
resultData = resultIntent
|
|
)
|
|
inlineBmp.recycle()
|
|
|
|
assertNotNull(resolved)
|
|
assertFalse("Should fall back away from empty expected URI", resolved == emptyExpectedUri)
|
|
val loaded = ImageUtils.loadResizedBitmapForAi(context, resolved!!, maxDimension = 200)
|
|
assertNotNull(loaded)
|
|
loaded!!.recycle()
|
|
}
|
|
}
|