-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_test.go
More file actions
180 lines (141 loc) · 4.4 KB
/
exec_test.go
File metadata and controls
180 lines (141 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package hostlib
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPerformExecCommand_Success(t *testing.T) {
req := ExecCommandRequest{
Command: "echo",
Args: []string{"hello", "world"},
}
resp := PerformExecCommand(context.Background(), req)
assert.Nil(t, resp.Error)
assert.Equal(t, 0, resp.ExitCode)
assert.Equal(t, "hello world\n", resp.Stdout)
assert.Empty(t, resp.Stderr)
assert.False(t, resp.IsTimeout)
assert.GreaterOrEqual(t, resp.DurationMs, int64(0))
}
func TestPerformExecCommand_InvalidCommand(t *testing.T) {
req := ExecCommandRequest{
Command: "nonexistentcommand12345",
}
resp := PerformExecCommand(context.Background(), req)
require.NotNil(t, resp.Error)
assert.Equal(t, "EXECUTION_FAILED", resp.Error.Code)
}
func TestPerformExecCommand_Timeout(t *testing.T) {
if testing.Short() {
t.Skip("skipping timeout test in short mode")
}
req := ExecCommandRequest{
Command: "sleep",
Args: []string{"2"},
Timeout: 100, // 100ms
}
resp := PerformExecCommand(context.Background(), req)
assert.True(t, resp.IsTimeout)
assert.Nil(t, resp.Error) // Timeout is a valid result state, not an execution error
}
func TestPerformExecCommand_ExitCode(t *testing.T) {
// false command returns exit code 1
req := ExecCommandRequest{
Command: "false",
}
resp := PerformExecCommand(context.Background(), req)
assert.Nil(t, resp.Error)
assert.Equal(t, 1, resp.ExitCode)
}
func TestPerformExecCommand_Env(t *testing.T) {
req := ExecCommandRequest{
Command: "env",
Env: []string{"TEST_VAR=test_value"},
}
resp := PerformExecCommand(context.Background(), req)
assert.Nil(t, resp.Error)
assert.Contains(t, resp.Stdout, "TEST_VAR=test_value")
}
func TestPerformExecCommand_EmptyCommand(t *testing.T) {
req := ExecCommandRequest{
Command: "",
}
resp := PerformExecCommand(context.Background(), req)
require.NotNil(t, resp.Error)
assert.Equal(t, "INVALID_REQUEST", resp.Error.Code)
}
func TestPerformExecCommand_WithEnvSanitization(t *testing.T) {
capGetter := func(plugin, cap string) bool {
// Allow nothing
return false
}
req := ExecCommandRequest{
Command: "env",
Env: []string{"SAFE_VAR=value", "LD_PRELOAD=/evil.so", "PATH=/usr/bin"},
}
resp := PerformExecCommand(context.Background(), req,
WithEnvSanitization("test-plugin", capGetter),
)
assert.Nil(t, resp.Error)
// SAFE_VAR should be present
assert.Contains(t, resp.Stdout, "SAFE_VAR=value")
// LD_PRELOAD should be blocked (always blocked)
assert.NotContains(t, resp.Stdout, "LD_PRELOAD")
// PATH should be blocked (capability-gated and no capability granted)
assert.NotContains(t, resp.Stdout, "PATH=")
}
func TestPerformExecCommand_WithEnvSanitization_AllowsCapability(t *testing.T) {
capGetter := func(plugin, cap string) bool {
// Allow PATH
return cap == "env:PATH"
}
req := ExecCommandRequest{
Command: "env",
Env: []string{"PATH=/custom/path"},
}
resp := PerformExecCommand(context.Background(), req,
WithEnvSanitization("test-plugin", capGetter),
)
assert.Nil(t, resp.Error)
// PATH should be present since capability is granted
assert.Contains(t, resp.Stdout, "PATH=/custom/path")
}
func TestPerformExecCommand_WithIsolatedEnv(t *testing.T) {
req := ExecCommandRequest{
Command: "env",
// No Env provided
}
resp := PerformExecCommand(context.Background(), req, WithIsolatedEnv())
assert.Nil(t, resp.Error)
// With isolated env and no provided env, output should be empty
assert.Empty(t, resp.Stdout)
}
func TestPerformExecCommand_WithMaxOutputSize(t *testing.T) {
req := ExecCommandRequest{
Command: "yes",
Args: []string{"hello"},
Timeout: 100, // 100ms to limit output
}
resp := PerformExecCommand(context.Background(), req,
WithMaxOutputSize(100),
)
// Command will timeout, but we care about truncation
assert.True(t, resp.StdoutTruncated)
assert.LessOrEqual(t, len(resp.Stdout), 100)
}
func TestPerformSecureExecCommand(t *testing.T) {
capGetter := func(plugin, cap string) bool {
return false
}
req := ExecCommandRequest{
Command: "env",
Env: []string{"SAFE_VAR=value", "LD_PRELOAD=/evil.so"},
}
resp := PerformSecureExecCommand(context.Background(), req, "test-plugin", capGetter)
assert.Nil(t, resp.Error)
// SAFE_VAR should be present
assert.Contains(t, resp.Stdout, "SAFE_VAR=value")
// LD_PRELOAD should be blocked
assert.NotContains(t, resp.Stdout, "LD_PRELOAD")
}