|
| 1 | +package cc.unitmesh.devti.sketch.run |
| 2 | + |
| 3 | +import org.assertj.core.api.Assertions.assertThat |
| 4 | +import kotlin.test.Test |
| 5 | + |
| 6 | +class ShellSafetyCheckTest { |
| 7 | + @Test |
| 8 | + fun testDangerousRmWithForceFlags() { |
| 9 | + val command = "rm -rf /some/path" |
| 10 | + val result = ShellSafetyCheck.checkDangerousCommand(command) |
| 11 | + // Expect dangerous because of -rf flags |
| 12 | + assertThat(result.first).isTrue() |
| 13 | + assertThat(result.second).isEqualTo("Remove command detected, use with caution") |
| 14 | + } |
| 15 | + |
| 16 | + @Test |
| 17 | + fun testDangerousRmWithoutInteractiveFlag() { |
| 18 | + val command = "rm /some/file" |
| 19 | + val result = ShellSafetyCheck.checkDangerousCommand(command) |
| 20 | + // Expect dangerous due to generic rm command check |
| 21 | + assertThat(result.first).isTrue() |
| 22 | + assertThat(result.second).isEqualTo("Remove command detected, use with caution") |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + fun testSafeRmWithInteractiveFlag() { |
| 27 | + val command = "rm -i /some/file" |
| 28 | + val result = ShellSafetyCheck.checkDangerousCommand(command) |
| 29 | + // Expect safe command as interactive flag is present |
| 30 | + assertThat(result.first).isTrue() |
| 31 | + assertThat(result.second).isEqualTo("Removing files from root directory") |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + fun testDangerousRmdirFromRoot() { |
| 36 | + val command = "rmdir /" |
| 37 | + val result = ShellSafetyCheck.checkDangerousCommand(command) |
| 38 | + // Expect dangerous as it touches root directory |
| 39 | + assertThat(result.first).isTrue() |
| 40 | + assertThat(result.second).isEqualTo("Removing directories from root") |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun testDangerousMkfsCommand() { |
| 45 | + val command = "mkfs /dev/sda1" |
| 46 | + val result = ShellSafetyCheck.checkDangerousCommand(command) |
| 47 | + // Expect dangerous because of filesystem formatting command |
| 48 | + assertThat(result.first).isTrue() |
| 49 | + assertThat(result.second).isEqualTo("Filesystem formatting command") |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + fun testDangerousDdCommand() { |
| 54 | + val command = "dd if=/dev/zero of=/dev/sda1" |
| 55 | + val result = ShellSafetyCheck.checkDangerousCommand(command) |
| 56 | + // Expect dangerous because of low-level disk operation |
| 57 | + assertThat(result.first).isTrue() |
| 58 | + assertThat(result.second).isEqualTo("Low-level disk operation") |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun testDangerousForkBomb() { |
| 63 | + val command = ":(){ :|:& };:" |
| 64 | + val result = ShellSafetyCheck.checkDangerousCommand(command) |
| 65 | + // Expect dangerous because of potential fork bomb pattern |
| 66 | + assertThat(result.first).isTrue() |
| 67 | + assertThat(result.second).isEqualTo("Potential fork bomb") |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun testDangerousChmodCommand() { |
| 72 | + val command = "chmod -R 777 /some/directory" |
| 73 | + val result = ShellSafetyCheck.checkDangerousCommand(command) |
| 74 | + // Expect dangerous as recursive chmod with insecure permissions is detected |
| 75 | + assertThat(result.first).isTrue() |
| 76 | + assertThat(result.second).isEqualTo("Recursive chmod with insecure permissions") |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun testDangerousSudoCommand() { |
| 81 | + val command = "sudo rm -rf /some/path" |
| 82 | + val result = ShellSafetyCheck.checkDangerousCommand(command) |
| 83 | + // Expect dangerous due to sudo rm pattern |
| 84 | + assertThat(result.first).isTrue() |
| 85 | + assertThat(result.second).isEqualTo("Dangerous rm command with recursive or force flags") |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + fun testSafeCommand() { |
| 90 | + val command = "ls -la" |
| 91 | + val result = ShellSafetyCheck.checkDangerousCommand(command) |
| 92 | + // Expect no dangerous patterns detected |
| 93 | + assertThat(result.first).isFalse() |
| 94 | + assertThat(result.second).isEmpty() |
| 95 | + } |
| 96 | +} |
0 commit comments