mirror of
https://github.com/bleackstorm/satiea.git
synced 2026-12-03 07:41:56 +01:00
refactor: optimize Firestore rules and meal cleanup
- Tighten Firestore validation rules (data size and limits) - Pass context to `deletePhotoFile` for safer file management - Cleanup unused dependencies in build configuration - Update Robolectric SDK target for tests
This commit is contained in:
1 parent
87bbb448c2
commit
79e04f64d8
28 files changed
+1606
-1230
No files matched your search
@@ -0,0 +1,71 @@
|
||||
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"))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user