-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstore_test.go
More file actions
306 lines (274 loc) · 7.94 KB
/
store_test.go
File metadata and controls
306 lines (274 loc) · 7.94 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package gitstore
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/codeGROOVE-dev/gitMDM/internal/gitmdm"
)
func TestSanitizeID(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"normal-id", "normal-id"},
{"../../../etc/passwd", "etc-passwd"},
{"id/with/slashes", "id-with-slashes"},
{"id\\with\\backslashes", "id-with-backslashes"},
{"id:with:colons", "id-with-colons"},
{"id with spaces", "id-with-spaces"},
{"id<with>special*chars?", "id-with-special-chars"},
{"", "id-e3b0c44298fc1c14"}, // hash of empty string
{"..", "id-5ec1f7e700f37c3d"}, // hash of ".."
{"./", "id-c14cecec97312ad1"}, // hash of "./"
{strings.Repeat("a", 300), strings.Repeat("a", 100)}, // max length is now 100
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := sanitizeID(tt.input)
if result != tt.expected {
t.Errorf("sanitizeID(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
func TestNewStore(t *testing.T) {
ctx := context.Background()
tempDir := t.TempDir()
localPath := filepath.Join(tempDir, "test-repo")
// Initialize git repo first
if err := os.MkdirAll(localPath, 0o755); err != nil {
t.Fatalf("Failed to create directory: %v", err)
}
cmd := exec.Command("git", "init")
cmd.Dir = localPath
if err := cmd.Run(); err != nil {
t.Fatalf("Failed to init git repo: %v", err)
}
// Test local repository creation
store, err := NewLocal(ctx, localPath)
if err != nil {
t.Fatalf("Failed to create local store: %v", err)
}
if store == nil {
t.Fatal("Store is nil")
}
// Verify repository was initialized
gitDir := filepath.Join(localPath, ".git")
if _, err := os.Stat(gitDir); os.IsNotExist(err) {
t.Error("Git repository was not initialized")
}
// Verify devices directory was created
devicesDir := filepath.Join(localPath, "devices")
if _, err := os.Stat(devicesDir); os.IsNotExist(err) {
t.Error("Devices directory was not created")
}
}
func TestSaveAndLoadDevice(t *testing.T) {
ctx := context.Background()
tempDir := t.TempDir()
localPath := filepath.Join(tempDir, "test-repo")
// Initialize git repo first
if err := os.MkdirAll(localPath, 0o755); err != nil {
t.Fatalf("Failed to create directory: %v", err)
}
cmd := exec.Command("git", "init")
cmd.Dir = localPath
if err := cmd.Run(); err != nil {
t.Fatalf("Failed to init git repo: %v", err)
}
store, err := NewLocal(ctx, localPath)
if err != nil {
t.Fatalf("Failed to create store: %v", err)
}
// Create test device
device := &gitmdm.Device{
HardwareID: "test-device-123",
Hostname: "test-host",
User: "test-user",
LastSeen: time.Now(),
Checks: map[string]gitmdm.Check{
"hostname": {
Outputs: []gitmdm.CommandOutput{
{
Command: "hostname",
Stdout: "test-host.local",
Stderr: "",
ExitCode: 0,
},
},
Status: "n/a",
Reason: "Informational",
},
"uname": {
Outputs: []gitmdm.CommandOutput{
{
Command: "uname -a",
Stdout: "Linux test-host 5.10.0",
Stderr: "",
ExitCode: 0,
},
},
Status: "n/a",
Reason: "Informational",
},
},
}
// Save device
if err := store.SaveDevice(ctx, device); err != nil {
t.Fatalf("Failed to save device: %v", err)
}
// Load devices
devices, err := store.LoadDevices(ctx)
if err != nil {
t.Fatalf("Failed to list devices: %v", err)
}
if len(devices) != 1 {
t.Fatalf("Expected 1 device, got %d", len(devices))
}
// Verify loaded device
loaded := devices[0]
if loaded.HardwareID != device.HardwareID {
t.Errorf("HardwareID mismatch: got %s, want %s", loaded.HardwareID, device.HardwareID)
}
if loaded.Hostname != device.Hostname {
t.Errorf("Hostname mismatch: got %s, want %s", loaded.Hostname, device.Hostname)
}
if loaded.User != device.User {
t.Errorf("User mismatch: got %s, want %s", loaded.User, device.User)
}
if len(loaded.Checks) != len(device.Checks) {
t.Errorf("Checks count mismatch: got %d, want %d", len(loaded.Checks), len(device.Checks))
}
// Verify checks content
for name, check := range device.Checks {
loadedCheck, exists := loaded.Checks[name]
if !exists {
t.Errorf("Check %s not found in loaded device", name)
continue
}
if len(loadedCheck.Outputs) != len(check.Outputs) {
t.Errorf("Check %s outputs count mismatch: got %d, want %d", name, len(loadedCheck.Outputs), len(check.Outputs))
continue
}
for i, output := range check.Outputs {
if i >= len(loadedCheck.Outputs) {
break
}
loadedOutput := loadedCheck.Outputs[i]
if loadedOutput.Command != output.Command {
t.Errorf("Check %s output %d command mismatch: got %s, want %s", name, i, loadedOutput.Command, output.Command)
}
if loadedOutput.Stdout != output.Stdout {
t.Errorf("Check %s output %d stdout mismatch: got %s, want %s", name, i, loadedOutput.Stdout, output.Stdout)
}
if loadedOutput.ExitCode != output.ExitCode {
t.Errorf("Check %s output %d exit code mismatch: got %d, want %d", name, i, loadedOutput.ExitCode, output.ExitCode)
}
}
}
}
func TestPathTraversalPrevention(t *testing.T) {
ctx := context.Background()
tempDir := t.TempDir()
localPath := filepath.Join(tempDir, "test-repo")
// Initialize git repo first
if err := os.MkdirAll(localPath, 0o755); err != nil {
t.Fatalf("Failed to create directory: %v", err)
}
cmd := exec.Command("git", "init")
cmd.Dir = localPath
if err := cmd.Run(); err != nil {
t.Fatalf("Failed to init git repo: %v", err)
}
store, err := NewLocal(ctx, localPath)
if err != nil {
t.Fatalf("Failed to create store: %v", err)
}
// Try to save device with path traversal attempt
device := &gitmdm.Device{
HardwareID: "../../../etc/passwd",
Hostname: "evil-host",
User: "evil-user",
LastSeen: time.Now(),
Checks: map[string]gitmdm.Check{
"../../../evil": {
Outputs: []gitmdm.CommandOutput{
{
Command: "evil command",
Stdout: "evil output",
Stderr: "",
ExitCode: 0,
},
},
Status: "fail",
Reason: "Evil detected",
},
},
}
// This should succeed but sanitize the paths
if err := store.SaveDevice(ctx, device); err != nil {
t.Fatalf("Failed to save device: %v", err)
}
// Verify the file was saved in the correct location (sanitized)
expectedDir := filepath.Join(localPath, "devices", "etc-passwd")
if _, err := os.Stat(expectedDir); os.IsNotExist(err) {
t.Error("Device directory was not created in expected location")
}
// Verify no files were created outside the repo
etcPasswd := "/etc/passwd.md"
if _, err := os.Stat(etcPasswd); err == nil {
t.Fatal("Path traversal was not prevented!")
}
}
func TestConcurrentSaves(t *testing.T) {
ctx := context.Background()
tempDir := t.TempDir()
localPath := filepath.Join(tempDir, "test-repo")
// Initialize git repo first
if err := os.MkdirAll(localPath, 0o755); err != nil {
t.Fatalf("Failed to create directory: %v", err)
}
cmd := exec.Command("git", "init")
cmd.Dir = localPath
if err := cmd.Run(); err != nil {
t.Fatalf("Failed to init git repo: %v", err)
}
store, err := NewLocal(ctx, localPath)
if err != nil {
t.Fatalf("Failed to create store: %v", err)
}
// Run concurrent saves
done := make(chan bool, 10)
for i := range 10 {
go func(id int) {
device := &gitmdm.Device{
HardwareID: fmt.Sprintf("device-%d", id),
Hostname: fmt.Sprintf("host-%d", id),
User: "test-user",
LastSeen: time.Now(),
Checks: map[string]gitmdm.Check{},
}
if err := store.SaveDevice(ctx, device); err != nil {
t.Errorf("Failed to save device %d: %v", id, err)
}
done <- true
}(i)
}
// Wait for all saves to complete
for range 10 {
<-done
}
// Verify all devices were saved
devices, err := store.LoadDevices(ctx)
if err != nil {
t.Fatalf("Failed to list devices: %v", err)
}
if len(devices) != 10 {
t.Errorf("Expected 10 devices, got %d", len(devices))
}
}