Skip to content

Commit 9bdb442

Browse files
committed
refactor: remove rollback actions and align command validation tests
1 parent 2bfa93c commit 9bdb442

4 files changed

Lines changed: 62 additions & 59 deletions

File tree

app/src/main/java/com/appcontrolx/ui/screens/settings/SettingsScreen.kt

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import androidx.hilt.navigation.compose.hiltViewModel
2020
import androidx.lifecycle.compose.collectAsStateWithLifecycle
2121
import com.appcontrolx.data.ThemeMode
2222
import com.appcontrolx.model.ActionHistoryItem
23-
import com.appcontrolx.model.AppAction
2423
import com.appcontrolx.model.ExecutionMode
2524

2625
@OptIn(ExperimentalMaterial3Api::class)
@@ -430,7 +429,6 @@ fun SettingsScreen(
430429
if (showActionHistoryDialog) {
431430
ActionHistoryDialog(
432431
history = actionHistory,
433-
onRollback = { viewModel.rollbackAction(it) },
434432
onClear = { viewModel.clearHistory() },
435433
onDismiss = { showActionHistoryDialog = false }
436434
)
@@ -674,7 +672,6 @@ private fun ThemeOption(
674672
@Composable
675673
private fun ActionHistoryDialog(
676674
history: List<ActionHistoryItem>,
677-
onRollback: (ActionHistoryItem) -> Unit,
678675
onClear: () -> Unit,
679676
onDismiss: () -> Unit
680677
) {
@@ -736,10 +733,7 @@ private fun ActionHistoryDialog(
736733
verticalArrangement = Arrangement.spacedBy(8.dp)
737734
) {
738735
items(history, key = { it.timestamp }) { item ->
739-
ActionHistoryItem(
740-
item = item,
741-
onRollback = { onRollback(item) }
742-
)
736+
ActionHistoryItem(item = item)
743737
}
744738
}
745739
}
@@ -749,8 +743,7 @@ private fun ActionHistoryDialog(
749743

750744
@Composable
751745
private fun ActionHistoryItem(
752-
item: ActionHistoryItem,
753-
onRollback: () -> Unit
746+
item: ActionHistoryItem
754747
) {
755748
Surface(
756749
modifier = Modifier.fillMaxWidth(),

app/src/main/java/com/appcontrolx/ui/screens/settings/SettingsViewModel.kt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import com.appcontrolx.core.ShellManager
88
import com.appcontrolx.data.ActionHistoryStore
99
import com.appcontrolx.data.ThemeMode
1010
import com.appcontrolx.data.UserPreferences
11-
import com.appcontrolx.domain.AppManager
1211
import com.appcontrolx.model.ActionHistoryItem
13-
import com.appcontrolx.model.AppAction
1412
import com.appcontrolx.model.ExecutionMode
1513
import dagger.hilt.android.lifecycle.HiltViewModel
1614
import dagger.hilt.android.qualifiers.ApplicationContext
@@ -35,7 +33,6 @@ class SettingsViewModel @Inject constructor(
3533
@ApplicationContext private val context: Context,
3634
private val userPreferences: UserPreferences,
3735
private val shellManager: ShellManager,
38-
private val appManager: AppManager,
3936
private val actionHistoryStore: ActionHistoryStore
4037
) : ViewModel() {
4138

@@ -103,17 +100,6 @@ class SettingsViewModel @Inject constructor(
103100
}
104101
}
105102

106-
fun rollbackAction(item: ActionHistoryItem) {
107-
viewModelScope.launch {
108-
val rollbackAction = when (item.action) {
109-
AppAction.FREEZE -> AppAction.UNFREEZE
110-
AppAction.UNFREEZE -> AppAction.FREEZE
111-
else -> return@launch
112-
}
113-
appManager.executeAction(item.packageName, rollbackAction)
114-
}
115-
}
116-
117103
fun clearHistory() {
118104
viewModelScope.launch {
119105
actionHistoryStore.clearHistory()
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.appcontrolx.core
2+
3+
import org.junit.Assert.assertTrue
4+
import org.junit.Test
5+
6+
class CommandValidatorTest {
7+
8+
@Test
9+
fun allowsSupportedCommandFamilies() {
10+
val commands = listOf(
11+
"pm disable-user --user 0 com.example.app",
12+
"am force-stop com.example.app",
13+
"appops set com.example.app RUN_IN_BACKGROUND ignore",
14+
"cmd appops query-op RUN_IN_BACKGROUND ignore",
15+
"dumpsys activity processes"
16+
)
17+
18+
commands.forEach { command ->
19+
val result = CommandValidator.validate(command)
20+
assertTrue("Expected command to be allowed: $command", result.isSuccess)
21+
}
22+
}
23+
24+
@Test
25+
fun rejectsDisallowedCommandPrefixes() {
26+
val commands = listOf(
27+
"sh -c pm clear com.example.app",
28+
"rm -rf /",
29+
"ls /data"
30+
)
31+
32+
commands.forEach { command ->
33+
val result = CommandValidator.validate(command)
34+
assertTrue("Expected command to be rejected: $command", result.isFailure)
35+
}
36+
}
37+
38+
@Test
39+
fun rejectsDangerousShellPatterns() {
40+
val commands = listOf(
41+
"pm clear com.example.app && pm enable com.example.app",
42+
"pm clear com.example.app$(id)",
43+
"pm clear com.example.app > /sdcard/out.txt"
44+
)
45+
46+
commands.forEach { command ->
47+
val result = CommandValidator.validate(command)
48+
assertTrue("Expected dangerous command to be rejected: $command", result.isFailure)
49+
}
50+
}
51+
52+
@Test
53+
fun rejectsBlankAndInvalidCommands() {
54+
val blank = CommandValidator.validate(" ")
55+
val invalidStructure = CommandValidator.validate("pm clear")
56+
57+
assertTrue("Expected blank command to be rejected", blank.isFailure)
58+
assertTrue("Expected invalid command structure to be rejected", invalidStructure.isFailure)
59+
}
60+
}

app/src/test/java/com/appcontrolx/core/ShellCommandPolicyTest.kt

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)