package com.example import android.content.Context import androidx.test.core.app.ApplicationProvider import com.example.util.ImageUtils import org.junit.Assert.assertFalse 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 @RunWith(RobolectricTestRunner::class) @Config(sdk = [36]) class ImageUtilsTest { private lateinit var context: Context @Before fun setUp() { context = ApplicationProvider.getApplicationContext() } @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")) } }