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
@@ -100,4 +100,27 @@ class CsvImporterTest {
|
||||
assertEquals(1, result.importedMeals[0].parsedFoodItems().size)
|
||||
assertEquals("Pavé de saumon", result.importedMeals[0].parsedFoodItems()[0].name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testFormulaInjectionSanitization() {
|
||||
val dangerousInputs = listOf(
|
||||
"=SUM(A1:A10)",
|
||||
"+cmd|' /C calc'!A0",
|
||||
"-2+3*cmd|' /C calc'!A0",
|
||||
"@SUM(1,2)",
|
||||
"\tTAB_FORMULA"
|
||||
)
|
||||
|
||||
for (input in dangerousInputs) {
|
||||
val escaped = com.example.util.CsvExporter.escapeCsvField(input)
|
||||
// Either escaped starts with "'=" or "\"\'="
|
||||
val isPrefixedWithQuote = escaped.startsWith("'") || escaped.startsWith("\"'")
|
||||
org.junit.Assert.assertTrue("Input '$input' should be prefixed with single quote: $escaped", isPrefixedWithQuote)
|
||||
}
|
||||
|
||||
val safeInput = "Repas classique sans formule"
|
||||
val safeEscaped = com.example.util.CsvExporter.escapeCsvField(safeInput)
|
||||
assertEquals("Repas classique sans formule", safeEscaped)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.example
|
||||
|
||||
import android.content.Context
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34])
|
||||
class ExampleRobolectricTest {
|
||||
|
||||
@Test
|
||||
fun `read string from context`() {
|
||||
val context = ApplicationProvider.getApplicationContext<Context>()
|
||||
val appName = context.getString(R.string.app_name)
|
||||
assertEquals("Satiéa", appName)
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.example
|
||||
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* See [testing documentation](http://d.android.com/tools/testing).
|
||||
*/
|
||||
class ExampleUnitTest {
|
||||
@Test
|
||||
fun addition_isCorrect() {
|
||||
assertEquals(4, 2 + 2)
|
||||
}
|
||||
}
|
||||
@@ -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"))
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34])
|
||||
@Config(sdk = [36])
|
||||
class LocalDatabaseTest {
|
||||
|
||||
private lateinit var db: AppDatabase
|
||||
|
||||
@@ -15,7 +15,7 @@ import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34])
|
||||
@Config(sdk = [36])
|
||||
class ReminderPreferencesTest {
|
||||
|
||||
private lateinit var context: Context
|
||||
|
||||
@@ -22,7 +22,7 @@ import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.LooperMode
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(manifest = Config.NONE, sdk = [34])
|
||||
@Config(manifest = Config.NONE, sdk = [36])
|
||||
@LooperMode(LooperMode.Mode.INSTRUMENTATION_TEST)
|
||||
abstract class FirestoreEmulatorTestBase {
|
||||
|
||||
|
||||
Reference in new issue
Block a user