-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner_test.go
More file actions
455 lines (391 loc) · 11.1 KB
/
scanner_test.go
File metadata and controls
455 lines (391 loc) · 11.1 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package main
import (
"archive/zip"
"os"
"path/filepath"
"testing"
"time"
)
func TestParseClamAVOutput(t *testing.T) {
// Use OS-specific path separator for test paths
sep := string(os.PathSeparator)
baseDir := sep + "tmp" + sep + "scan"
tests := []struct {
name string
output string
baseDir string
wantCount int
wantThreats []Threat
}{
{
name: "empty output",
output: "",
baseDir: baseDir,
wantCount: 0,
},
{
name: "clean scan (no output with --infected)",
output: "\n\n",
baseDir: baseDir,
wantCount: 0,
},
{
name: "single threat",
output: baseDir + sep + "malware.exe: Win.Trojan.Test FOUND\n",
baseDir: baseDir,
wantCount: 1,
wantThreats: []Threat{
{Name: "Win.Trojan.Test", File: "malware.exe", Severity: "critical"},
},
},
{
name: "multiple threats",
output: baseDir + sep + "file1.exe: Virus.A FOUND\n" + baseDir + sep + "subdir" + sep + "file2.dll: Virus.B FOUND\n",
baseDir: baseDir,
wantCount: 2,
wantThreats: []Threat{
{Name: "Virus.A", File: "file1.exe", Severity: "critical"},
{Name: "Virus.B", File: "subdir" + sep + "file2.dll", Severity: "critical"},
},
},
{
name: "EICAR test signature",
output: baseDir + sep + "eicar.txt: Win.Test.EICAR_HDB-1 FOUND\n",
baseDir: baseDir,
wantCount: 1,
wantThreats: []Threat{
{Name: "Win.Test.EICAR_HDB-1", File: "eicar.txt", Severity: "critical"},
},
},
{
name: "skips summary lines",
output: "----------- SCAN SUMMARY -----------\nKnown viruses: 123456\nEngine version: 1.0.0\nScanned files: 10\n" + baseDir + sep + "bad.exe: Malware FOUND\n",
baseDir: baseDir,
wantCount: 1,
wantThreats: []Threat{
{Name: "Malware", File: "bad.exe", Severity: "critical"},
},
},
{
name: "handles trailing separator in baseDir",
output: baseDir + sep + "file.exe: Virus FOUND\n",
baseDir: baseDir + sep,
wantCount: 1,
wantThreats: []Threat{
{Name: "Virus", File: "file.exe", Severity: "critical"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
threats := parseClamAVOutput(tt.output, tt.baseDir)
if len(threats) != tt.wantCount {
t.Errorf("got %d threats, want %d", len(threats), tt.wantCount)
}
for i, want := range tt.wantThreats {
if i >= len(threats) {
break
}
got := threats[i]
if got.Name != want.Name {
t.Errorf("threat[%d].Name = %q, want %q", i, got.Name, want.Name)
}
if got.File != want.File {
t.Errorf("threat[%d].File = %q, want %q", i, got.File, want.File)
}
if got.Severity != want.Severity {
t.Errorf("threat[%d].Severity = %q, want %q", i, got.Severity, want.Severity)
}
}
})
}
}
func TestComputeFileHash(t *testing.T) {
// Create temp file with known content
tmpFile, err := os.CreateTemp("", "hash-test-*")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
defer os.Remove(tmpFile.Name())
content := []byte("test content for hashing")
if _, err := tmpFile.Write(content); err != nil {
t.Fatalf("failed to write temp file: %v", err)
}
tmpFile.Close()
hash, err := computeFileHash(tmpFile.Name())
if err != nil {
t.Fatalf("computeFileHash() error: %v", err)
}
if len(hash) != 64 {
t.Errorf("hash length = %d, want 64", len(hash))
}
// Verify it's a valid hex string
for _, c := range hash {
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) {
t.Errorf("hash contains invalid character: %c", c)
}
}
}
func TestComputeFileHashNotFound(t *testing.T) {
_, err := computeFileHash("/nonexistent/file/path")
if err == nil {
t.Error("expected error for nonexistent file")
}
}
func TestExtractZipSafe(t *testing.T) {
cfg := &Config{
MaxExtractedSize: 10 << 20, // 10MB
MaxFileCount: 100,
MaxSingleFileSize: 5 << 20, // 5MB
}
s := NewScanner(cfg)
t.Run("extracts valid zip", func(t *testing.T) {
zipPath := createTestZip(t, map[string]string{
"file1.txt": "content1",
"file2.txt": "content2",
})
defer os.Remove(zipPath)
targetDir, err := os.MkdirTemp("", "extract-test-*")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(targetDir)
count, err := s.extractZipSafe(zipPath, targetDir)
if err != nil {
t.Fatalf("extractZipSafe() error: %v", err)
}
if count != 2 {
t.Errorf("file count = %d, want 2", count)
}
if _, err := os.Stat(filepath.Join(targetDir, "file1.txt")); os.IsNotExist(err) {
t.Error("file1.txt not extracted")
}
if _, err := os.Stat(filepath.Join(targetDir, "file2.txt")); os.IsNotExist(err) {
t.Error("file2.txt not extracted")
}
})
t.Run("extracts zip with directories", func(t *testing.T) {
zipPath := createTestZipWithDirs(t, map[string]string{
"subdir/file1.txt": "content1",
"subdir/file2.txt": "content2",
})
defer os.Remove(zipPath)
targetDir, _ := os.MkdirTemp("", "extract-test-*")
defer os.RemoveAll(targetDir)
count, err := s.extractZipSafe(zipPath, targetDir)
if err != nil {
t.Fatalf("extractZipSafe() error: %v", err)
}
// Count includes directory entry + 2 files = 3
if count != 3 {
t.Errorf("file count = %d, want 3", count)
}
})
t.Run("rejects too many files", func(t *testing.T) {
cfg := &Config{
MaxExtractedSize: 10 << 20,
MaxFileCount: 2,
MaxSingleFileSize: 5 << 20,
}
s := NewScanner(cfg)
zipPath := createTestZip(t, map[string]string{
"file1.txt": "a",
"file2.txt": "b",
"file3.txt": "c",
})
defer os.Remove(zipPath)
targetDir, _ := os.MkdirTemp("", "extract-test-*")
defer os.RemoveAll(targetDir)
_, err := s.extractZipSafe(zipPath, targetDir)
if err == nil {
t.Error("expected error for too many files")
}
})
t.Run("rejects file exceeding size limit", func(t *testing.T) {
cfg := &Config{
MaxExtractedSize: 10 << 20,
MaxFileCount: 100,
MaxSingleFileSize: 10, // 10 bytes limit
}
s := NewScanner(cfg)
zipPath := createTestZip(t, map[string]string{
"big.txt": "this content is longer than 10 bytes",
})
defer os.Remove(zipPath)
targetDir, _ := os.MkdirTemp("", "extract-test-*")
defer os.RemoveAll(targetDir)
_, err := s.extractZipSafe(zipPath, targetDir)
if err == nil {
t.Error("expected error for file exceeding size limit")
}
})
t.Run("rejects archive exceeding total size limit", func(t *testing.T) {
cfg := &Config{
MaxExtractedSize: 20, // 20 bytes total
MaxFileCount: 100,
MaxSingleFileSize: 50,
}
s := NewScanner(cfg)
zipPath := createTestZip(t, map[string]string{
"file1.txt": "content1content1",
"file2.txt": "content2content2",
})
defer os.Remove(zipPath)
targetDir, _ := os.MkdirTemp("", "extract-test-*")
defer os.RemoveAll(targetDir)
_, err := s.extractZipSafe(zipPath, targetDir)
if err == nil {
t.Error("expected error for archive exceeding total size limit")
}
})
t.Run("returns error for non-zip file", func(t *testing.T) {
tmpFile, _ := os.CreateTemp("", "not-a-zip-*")
tmpFile.WriteString("this is not a zip file")
tmpFile.Close()
defer os.Remove(tmpFile.Name())
targetDir, _ := os.MkdirTemp("", "extract-test-*")
defer os.RemoveAll(targetDir)
_, err := s.extractZipSafe(tmpFile.Name(), targetDir)
if err == nil {
t.Error("expected error for non-zip file")
}
})
}
func TestCopySingleFile(t *testing.T) {
cfg := &Config{
MaxSingleFileSize: 1024, // 1KB limit
}
s := NewScanner(cfg)
t.Run("copies file within limit", func(t *testing.T) {
srcFile, _ := os.CreateTemp("", "src-*")
srcFile.WriteString("small content")
srcFile.Close()
defer os.Remove(srcFile.Name())
targetDir, _ := os.MkdirTemp("", "target-*")
defer os.RemoveAll(targetDir)
count, err := s.copySingleFile(srcFile.Name(), targetDir)
if err != nil {
t.Fatalf("copySingleFile() error: %v", err)
}
if count != 1 {
t.Errorf("count = %d, want 1", count)
}
// Verify file exists
if _, err := os.Stat(filepath.Join(targetDir, "file")); os.IsNotExist(err) {
t.Error("file not copied")
}
})
t.Run("rejects file exceeding limit", func(t *testing.T) {
srcFile, _ := os.CreateTemp("", "src-*")
// Write more than 1KB
for i := 0; i < 200; i++ {
srcFile.WriteString("0123456789")
}
srcFile.Close()
defer os.Remove(srcFile.Name())
targetDir, _ := os.MkdirTemp("", "target-*")
defer os.RemoveAll(targetDir)
_, err := s.copySingleFile(srcFile.Name(), targetDir)
if err == nil {
t.Error("expected error for file exceeding limit")
}
})
t.Run("returns error for nonexistent file", func(t *testing.T) {
targetDir, _ := os.MkdirTemp("", "target-*")
defer os.RemoveAll(targetDir)
_, err := s.copySingleFile("/nonexistent/file", targetDir)
if err == nil {
t.Error("expected error for nonexistent file")
}
})
}
func TestNewScanner(t *testing.T) {
cfg := &Config{
Port: "9000",
ScanTimeout: 5 * time.Minute,
}
s := NewScanner(cfg)
if s == nil {
t.Fatal("NewScanner() returned nil")
}
if s.config != cfg {
t.Error("scanner config not set correctly")
}
}
// Helper function to create a test zip file
func createTestZip(t *testing.T, files map[string]string) string {
t.Helper()
tmpFile, err := os.CreateTemp("", "test-*.zip")
if err != nil {
t.Fatalf("failed to create temp zip: %v", err)
}
w := zip.NewWriter(tmpFile)
for name, content := range files {
f, err := w.Create(name)
if err != nil {
t.Fatalf("failed to create file in zip: %v", err)
}
f.Write([]byte(content))
}
w.Close()
tmpFile.Close()
return tmpFile.Name()
}
// Helper function to create a test zip file with directory structure
func createTestZipWithDirs(t *testing.T, files map[string]string) string {
t.Helper()
tmpFile, err := os.CreateTemp("", "test-*.zip")
if err != nil {
t.Fatalf("failed to create temp zip: %v", err)
}
w := zip.NewWriter(tmpFile)
// Create directories first
dirs := make(map[string]bool)
for name := range files {
dir := filepath.Dir(name)
if dir != "." && !dirs[dir] {
_, err := w.Create(dir + "/")
if err != nil {
t.Fatalf("failed to create dir in zip: %v", err)
}
dirs[dir] = true
}
}
// Create files
for name, content := range files {
f, err := w.Create(name)
if err != nil {
t.Fatalf("failed to create file in zip: %v", err)
}
f.Write([]byte(content))
}
w.Close()
tmpFile.Close()
return tmpFile.Name()
}
func TestExtractFileSafe(t *testing.T) {
cfg := &Config{
MaxSingleFileSize: 100,
}
s := NewScanner(cfg)
t.Run("extracts file within limit", func(t *testing.T) {
zipPath := createTestZip(t, map[string]string{
"test.txt": "small content",
})
defer os.Remove(zipPath)
reader, _ := zip.OpenReader(zipPath)
defer reader.Close()
targetDir, _ := os.MkdirTemp("", "extract-test-*")
defer os.RemoveAll(targetDir)
targetPath := filepath.Join(targetDir, "test.txt")
err := s.extractFileSafe(reader.File[0], targetPath)
if err != nil {
t.Errorf("extractFileSafe() error: %v", err)
}
// Verify content
content, _ := os.ReadFile(targetPath)
if string(content) != "small content" {
t.Errorf("content = %q, want 'small content'", string(content))
}
})
}