1package tools
2
3import (
4 "runtime"
5 "testing"
6)
7
8func TestGetSafeReadOnlyCommands(t *testing.T) {
9 commands := getSafeReadOnlyCommands()
10
11 // Check that we have some commands
12 if len(commands) == 0 {
13 t.Fatal("Expected some safe commands, got none")
14 }
15
16 // Check for cross-platform commands that should always be present
17 crossPlatformCommands := []string{"echo", "hostname", "whoami", "git status", "go version"}
18 for _, cmd := range crossPlatformCommands {
19 found := false
20 for _, safeCmd := range commands {
21 if safeCmd == cmd {
22 found = true
23 break
24 }
25 }
26 if !found {
27 t.Errorf("Expected cross-platform command %q to be in safe commands", cmd)
28 }
29 }
30
31 if runtime.GOOS == "windows" {
32 // Check for Windows-specific commands
33 windowsCommands := []string{"dir", "type", "Get-Process"}
34 for _, cmd := range windowsCommands {
35 found := false
36 for _, safeCmd := range commands {
37 if safeCmd == cmd {
38 found = true
39 break
40 }
41 }
42 if !found {
43 t.Errorf("Expected Windows command %q to be in safe commands on Windows", cmd)
44 }
45 }
46
47 // Check that Unix commands are NOT present on Windows
48 unixCommands := []string{"ls", "pwd", "ps"}
49 for _, cmd := range unixCommands {
50 found := false
51 for _, safeCmd := range commands {
52 if safeCmd == cmd {
53 found = true
54 break
55 }
56 }
57 if found {
58 t.Errorf("Unix command %q should not be in safe commands on Windows", cmd)
59 }
60 }
61 } else {
62 // Check for Unix-specific commands
63 unixCommands := []string{"ls", "pwd", "ps"}
64 for _, cmd := range unixCommands {
65 found := false
66 for _, safeCmd := range commands {
67 if safeCmd == cmd {
68 found = true
69 break
70 }
71 }
72 if !found {
73 t.Errorf("Expected Unix command %q to be in safe commands on Unix", cmd)
74 }
75 }
76
77 // Check that Windows-specific commands are NOT present on Unix
78 windowsOnlyCommands := []string{"dir", "Get-Process", "systeminfo"}
79 for _, cmd := range windowsOnlyCommands {
80 found := false
81 for _, safeCmd := range commands {
82 if safeCmd == cmd {
83 found = true
84 break
85 }
86 }
87 if found {
88 t.Errorf("Windows-only command %q should not be in safe commands on Unix", cmd)
89 }
90 }
91 }
92}
93
94func TestPlatformSpecificSafeCommands(t *testing.T) {
95 // Test that the function returns different results on different platforms
96 commands := getSafeReadOnlyCommands()
97
98 hasWindowsCommands := false
99 hasUnixCommands := false
100
101 for _, cmd := range commands {
102 if cmd == "dir" || cmd == "Get-Process" || cmd == "systeminfo" {
103 hasWindowsCommands = true
104 }
105 if cmd == "ls" || cmd == "ps" || cmd == "df" {
106 hasUnixCommands = true
107 }
108 }
109
110 if runtime.GOOS == "windows" {
111 if !hasWindowsCommands {
112 t.Error("Expected Windows commands on Windows platform")
113 }
114 if hasUnixCommands {
115 t.Error("Did not expect Unix commands on Windows platform")
116 }
117 } else {
118 if hasWindowsCommands {
119 t.Error("Did not expect Windows-only commands on Unix platform")
120 }
121 if !hasUnixCommands {
122 t.Error("Expected Unix commands on Unix platform")
123 }
124 }
125}