-
Notifications
You must be signed in to change notification settings - Fork 1
/
fprot_test.go
485 lines (464 loc) · 12.3 KB
/
fprot_test.go
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// Copyright (C) 2018-2021 Andrew Colin Kissa <andrew@datopdog.io>
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
/*
Package fprot Golang F-Prot client
Fprot - Golang F-Prot client
*/
package fprot
import (
"bytes"
"context"
"go/build"
"io/ioutil"
"os"
"path"
"strings"
"testing"
"time"
)
const (
eicarVirus = `X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*`
)
type CommandTestKey struct {
in Command
out string
}
type StatusCodeTestKey struct {
in StatusCode
out string
}
var TestCommands = []CommandTestKey{
{Help, "HELP"},
{ScanFile, "SCAN FILE"},
{ScanStream, "SCAN STREAM"},
{Queue, "QUEUE"},
{ScanQueue, "SCAN"},
{Quit, "QUIT"},
{Command(100), ""},
}
var TestStatusCodes = []StatusCodeTestKey{
{NoMatch, "No signature was matched"},
{Infected, "Atleast one virus-infected object was found"},
{HeuristicMatch, "Atleast one suspicious (heuristic match) object was found"},
{UserError, "Scanning interrupted by user"},
{RestrictionError, "Scan restriction caused scan to skip files"},
{SystemError, "Platform error"},
{InternalError, "Internal Engine error"},
{SkipError, "Atleast one object was not scanned"},
{DisinfectError, "Atleast one object was disinfected"},
{StatusCode(100), ""},
}
func TestCommand(t *testing.T) {
for _, tt := range TestCommands {
if s := tt.in.String(); s != tt.out {
t.Errorf("%q.String() = %q, want %q", tt.in, s, tt.out)
}
}
}
func TestStatusCode(t *testing.T) {
for _, tt := range TestStatusCodes {
if s := tt.in.String(); s != tt.out {
t.Errorf("%q.String() = %q, want %q", tt.in, s, tt.out)
}
}
}
func TestBasics(t *testing.T) {
c, e := NewClient("")
if e != nil {
t.Fatalf("An error should not be returned")
}
if c.address != "127.0.0.1:10200" {
t.Errorf("Got %q want %q", c.address, "127.0.0.1:10200")
}
if c.connTimeout != defaultTimeout {
t.Errorf("The default conn timeout should be set")
}
if c.connSleep != defaultSleep {
t.Errorf("The default conn sleep should be set")
}
if c.connRetries != 0 {
t.Errorf("The default conn retries should be set")
}
expected := 2 * time.Second
c.SetConnTimeout(expected)
if c.connTimeout != expected {
t.Errorf("Calling c.SetConnTimeout(%q) failed", expected)
}
c.SetCmdTimeout(expected)
if c.cmdTimeout != expected {
t.Errorf("Calling c.SetCmdTimeout(%q) failed", expected)
}
c.SetConnSleep(expected)
if c.connSleep != expected {
t.Errorf("Calling c.SetConnSleep(%q) failed", expected)
}
c.SetConnRetries(2)
if c.connRetries != 2 {
t.Errorf("Calling c.SetConnRetries(%q) failed", 2)
}
c.SetConnRetries(-2)
if c.connRetries != 0 {
t.Errorf("Preventing negative values in c.SetConnRetries(%q) failed", -2)
}
if _, e = NewClient("/var/lib/ms/ms.sock"); e == nil {
t.Errorf("An error should be returned")
}
if _, e = NewClient("fe80::879:d85f:f836:1b56%en1"); e == nil {
t.Errorf("An error should be returned")
} else {
expect := "The supplied address is invalid"
if e.Error() != expect {
t.Errorf("Got %q want %q", e, expect)
}
}
}
func TestGetFiles(t *testing.T) {
dir, e := ioutil.TempDir("", "")
if e != nil {
t.Fatalf("Temp directory creation failed")
}
defer os.RemoveAll(dir)
if e = os.Chmod(dir, 0755); e != nil {
t.Errorf("Temp directory chmod failed")
}
// cm := map[string]bool{}
pts := []string{path.Join(dir, "file1.txt"), path.Join(dir, "file2.txt")}
content := []byte("temporary file's content")
for _, fn := range pts {
e = ioutil.WriteFile(fn, content, 0640)
if e != nil {
t.Errorf("Temp directory chmod failed")
continue
}
// cm[fn] = true
defer os.Remove(fn)
}
fls, e := getFiles(dir)
found := len(fls)
if found != 2 {
t.Errorf("Calling getFiles(%q) should return %q got %q", dir, 2, found)
}
if fls[0] != pts[0] && fls[1] != pts[1] {
t.Errorf("Files returned do not match created")
}
_, e = getFiles("/tmxts/hylsgxut.2s.sas")
if e == nil {
t.Errorf("An error should be returned")
}
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = build.Default.GOPATH
}
fn := path.Join(gopath, "src/github.com/baruwa-enterprise/fprot/README.md")
_, e = getFiles(fn)
if e == nil {
t.Errorf("An error should be returned")
}
}
func TestScan(t *testing.T) {
address := os.Getenv("FPROT_ADDRESS")
if address != "" {
c, e := NewClient(address)
if e != nil {
t.Fatalf("Error should not be returned: %s", e)
}
ctx := context.Background()
defer c.Close(ctx)
fn := "/var/spool/testfiles/install.log"
s, e := c.ScanFile(ctx, fn)
if e != nil {
t.Fatalf("Error should not be returned: %s", e)
}
if len(s) != 1 {
t.Fatalf("Expected 1 got %d", len(s))
}
if s[0].Filename != fn {
t.Fatalf("Filename expected %s got %s", fn, s[0].Filename)
}
if s[0].Infected {
t.Fatalf("Infected expected %t got %t", false, s[0].Infected)
}
if s[0].Signature != "" {
t.Fatalf("Filename expected %s got %s", "", s[0].Signature)
}
fn = "/var/spool/testfiles/eicar.txt"
s, e = c.ScanFile(ctx, fn)
if e != nil {
t.Fatalf("Error should not be returned: %s", e)
}
if len(s) != 1 {
t.Fatalf("Expected 1 got %d", len(s))
}
if s[0].Filename != fn {
t.Fatalf("Filename expected %s got %s", fn, s[0].Filename)
}
if !s[0].Infected {
t.Fatalf("Infected expected %t got %t", true, s[0].Infected)
}
if s[0].Signature != "EICAR_Test_File" {
t.Fatalf("Filename expected %s got %s", "EICAR_Test_File", s[0].Signature)
}
} else {
t.Skip("skipping test; $FPROT_ADDRESS not set")
}
}
func TestScanFiles(t *testing.T) {
address := os.Getenv("FPROT_ADDRESS")
if address != "" {
c, e := NewClient(address)
if e != nil {
t.Fatalf("Error should not be returned: %s", e)
}
ctx := context.Background()
defer c.Close(ctx)
fns := []string{
"/var/spool/testfiles/eicar.txt",
"/var/spool/testfiles/eicar.tar.bz2",
}
s, e := c.ScanFiles(ctx, fns[0], fns[1])
if e != nil {
t.Fatalf("Error should not be returned: %s", e)
}
if len(s) != len(fns) {
t.Fatalf("Expected %d got %d", len(fns), len(s))
}
for _, r := range s {
if r.Filename != fns[0] && r.Filename != fns[1] {
t.Fatalf("Filename expected %s or %s got %s", fns[0], fns[1], r.Filename)
}
if !r.Infected {
t.Fatalf("Infected expected %t got %t", true, r.Infected)
}
if r.Signature != "EICAR_Test_File" {
t.Fatalf("Filename expected %s got %s", "EICAR_Test_File", r.Signature)
}
}
} else {
t.Skip("skipping test; $FPROT_ADDRESS not set")
}
}
// func TestScanDir(t *testing.T) {
// address := os.Getenv("FPROT_ADDRESS")
// if address != "" {
// c, e := NewClient(address)
// if e != nil {
// t.Fatalf("Error should not be returned: %s", e)
// }
// defer c.Close()
// s, e := c.ScanDir("/var/spool/testfiles")
// if e != nil {
// t.Fatalf("Error should not be returned: %s", e)
// }
// if len(s) == 0 {
// t.Fatalf("Expected > 1 got %d", len(s))
// }
// }
// }
func TestScanDirStream(t *testing.T) {
address := os.Getenv("FPROT_ADDRESS")
if address != "" {
c, e := NewClient(address)
if e != nil {
t.Fatalf("Error should not be returned: %s", e)
}
ctx := context.Background()
defer c.Close(ctx)
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = build.Default.GOPATH
}
dn := path.Join(gopath, "src/github.com/baruwa-enterprise/fprot/examples/data")
s, e := c.ScanDirStream(ctx, dn)
if e != nil {
t.Fatalf("Error should not be returned: %s", e)
}
for _, r := range s {
if !r.Infected {
t.Fatalf("Infected expected %t got %t", true, r.Infected)
}
if r.Signature != "EICAR_Test_File" {
t.Fatalf("Filename expected %s got %s", "EICAR_Test_File", r.Signature)
}
}
} else {
t.Skip("skipping test; $FPROT_ADDRESS not set")
}
}
func TestScanStream(t *testing.T) {
address := os.Getenv("FPROT_ADDRESS")
if address != "" {
c, e := NewClient(address)
if e != nil {
t.Fatalf("Error should not be returned: %s", e)
}
ctx := context.Background()
defer c.Close(ctx)
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = build.Default.GOPATH
}
fn := path.Join(gopath, "src/github.com/baruwa-enterprise/fprot/examples/data/eicar.tar.bz2")
s, e := c.ScanStream(ctx, fn)
if e != nil {
t.Fatalf("Error should not be returned: %s", e)
}
for _, r := range s {
if !r.Infected {
t.Fatalf("Infected expected %t got %t", true, r.Infected)
}
if r.Signature != "EICAR_Test_File" {
t.Fatalf("Filename expected %s got %s", "EICAR_Test_File", r.Signature)
}
}
} else {
t.Skip("skipping test; $FPROT_ADDRESS not set")
}
}
func TestScanReader(t *testing.T) {
address := os.Getenv("FPROT_ADDRESS")
if address != "" {
c, e := NewClient(address)
if e != nil {
t.Fatalf("Error should not be returned: %s", e)
}
ctx := context.Background()
defer c.Close(ctx)
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = build.Default.GOPATH
}
fn := path.Join(gopath, "src/github.com/baruwa-enterprise/fprot/examples/data/eicar.tar.bz2")
f, e := os.Open(fn)
if e != nil {
t.Fatalf("Failed to open file: %s", fn)
}
defer f.Close()
s, e := c.ScanReader(ctx, f)
if e != nil {
t.Fatalf("Error should not be returned: %s", e)
}
for _, r := range s {
if !r.Infected {
t.Fatalf("Infected expected %t got %t", true, r.Infected)
}
if r.Signature != "EICAR_Test_File" {
t.Fatalf("Filename expected %s got %s", "EICAR_Test_File", r.Signature)
}
}
} else {
t.Skip("skipping test; $FPROT_ADDRESS not set")
}
}
func TestScanReaderBytes(t *testing.T) {
address := os.Getenv("FPROT_ADDRESS")
if address != "" {
c, e := NewClient(address)
if e != nil {
t.Fatalf("Error should not be returned: %s", e)
}
ctx := context.Background()
defer c.Close(ctx)
m := []byte(eicarVirus)
f := bytes.NewReader(m)
s, e := c.ScanReader(ctx, f)
if e != nil {
t.Fatalf("Error should not be returned: %s", e)
}
for _, r := range s {
if !r.Infected {
t.Fatalf("Infected expected %t got %t", true, r.Infected)
}
if r.Signature != "EICAR_Test_File" {
t.Fatalf("Filename expected %s got %s", "EICAR_Test_File", r.Signature)
}
}
} else {
t.Skip("skipping test; $FPROT_ADDRESS not set")
}
}
func TestScanReaderBuffer(t *testing.T) {
address := os.Getenv("FPROT_ADDRESS")
if address != "" {
c, e := NewClient(address)
if e != nil {
t.Fatalf("Error should not be returned: %s", e)
}
ctx := context.Background()
defer c.Close(ctx)
f := bytes.NewBufferString(eicarVirus)
s, e := c.ScanReader(ctx, f)
if e != nil {
t.Fatalf("Error should not be returned: %s", e)
}
for _, r := range s {
if !r.Infected {
t.Fatalf("Infected expected %t got %t", true, r.Infected)
}
if r.Signature != "EICAR_Test_File" {
t.Fatalf("Filename expected %s got %s", "EICAR_Test_File", r.Signature)
}
}
} else {
t.Skip("skipping test; $FPROT_ADDRESS not set")
}
}
func TestScanReaderString(t *testing.T) {
address := os.Getenv("FPROT_ADDRESS")
if address != "" {
c, e := NewClient(address)
if e != nil {
t.Fatalf("Error should not be returned: %s", e)
}
ctx := context.Background()
defer c.Close(ctx)
f := strings.NewReader(eicarVirus)
s, e := c.ScanReader(ctx, f)
if e != nil {
t.Fatalf("Error should not be returned: %s", e)
}
for _, r := range s {
if !r.Infected {
t.Fatalf("Infected expected %t got %t", true, r.Infected)
}
if r.Signature != "EICAR_Test_File" {
t.Fatalf("Filename expected %s got %s", "EICAR_Test_File", r.Signature)
}
}
} else {
t.Skip("skipping test; $FPROT_ADDRESS not set")
}
}
func TestInfo(t *testing.T) {
address := os.Getenv("FPROT_ADDRESS")
if address != "" {
c, e := NewClient(address)
if e != nil {
t.Fatalf("Error should not be returned: %s", e)
}
ctx := context.Background()
defer c.Close(ctx)
i, e := c.Info(ctx)
if e != nil {
t.Fatalf("Error should not be returned: %s", e)
}
if i.Engine == "" {
t.Errorf("i.Engine should be none empty string")
}
if i.Version == "" {
t.Errorf("i.Version should be none empty string")
}
if i.Protocol == "" {
t.Errorf("i.Protocol should be none empty string")
}
if i.Signature == "" {
t.Errorf("i.Signature should be none empty string")
}
if i.Uptime == "" {
t.Errorf("i.Uptime should be none empty string")
}
} else {
t.Skip("skipping test; $FPROT_ADDRESS not set")
}
}