-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
stress_test.go
136 lines (131 loc) · 3.57 KB
/
stress_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
// Copyright © 2015-2020 Hilko Bengen <[email protected]>
// All rights reserved.
//
// Use of this source code is governed by the license that can be
// found in the LICENSE file.
package yara
import (
"fmt"
"os"
"os/user"
"path"
"path/filepath"
"sync"
"testing"
)
func TestFileWalk(t *testing.T) {
if os.ExpandEnv("${TEST_WALK}") == "" {
t.Skip("Set TEST_WALK to enable scanning files from user's HOME with a dummy ruleset.\n" +
"(Setting -test.timeout may be a good idea for this.)")
}
initialPath := os.ExpandEnv("${TEST_WALK_START}")
if initialPath == "" {
if u, err := user.Current(); err != nil {
t.Skip("Could get user's homedir. You can use TEST_WALK_START " +
"to set an initial path for filepath.Walk()")
} else {
initialPath = u.HomeDir
}
}
r, err := Compile("rule test: tag1 tag2 tag3 { meta: foo = 1 bar = \"xxx\" quux = false condition: true }", nil)
if err != nil {
t.Fatal(err)
}
wg := sync.WaitGroup{}
for i := 0; i < 32; i++ {
wg.Add(1)
go func(i int) {
filepath.Walk(initialPath, func(name string, info os.FileInfo, inErr error) error {
fmt.Printf("[%02d] %s\n", i, name)
if inErr != nil {
fmt.Printf("[%02d] Walk to \"%s\": %s\n", i, name, inErr)
return nil
}
if info.IsDir() && info.Mode()&os.ModeSymlink != 0 {
fmt.Printf("[%02d] Walk to \"%s\": Skipping symlink\n", i, name)
return filepath.SkipDir
}
if !info.Mode().IsRegular() || info.Size() >= 2000000 {
return nil
}
// r.DefineVariable("filename", info.Name())
// r.DefineVariable("filepath", name)
var m MatchRules
if err := r.ScanFile(name, 0, 0, &m); err == nil {
fmt.Printf("[%02d] Scan \"%s\": %d\n", i, path.Base(name), len(m))
} else {
fmt.Printf("[%02d] Scan \"%s\": %s\n", i, path.Base(name), err)
}
return nil
})
wg.Done()
}(i)
}
wg.Wait()
}
func TestScannerFileWalk(t *testing.T) {
if os.ExpandEnv("${TEST_WALK}") == "" {
t.Skip("Set TEST_WALK to enable scanning files from user's HOME with a dummy ruleset.\n" +
"(Setting -test.timeout may be a good idea for this.)")
}
initialPath := os.ExpandEnv("${TEST_WALK_START}")
if initialPath == "" {
if u, err := user.Current(); err != nil {
t.Skip("Could get user's homedir. You can use TEST_WALK_START " +
"to set an initial path for filepath.Walk()")
} else {
initialPath = u.HomeDir
}
}
r, err := Compile(`
rule test: tag1 tag2 tag3 {
meta:
foo = 1
bar = "xxx"
quux = false
condition:
true
}
`, map[string]interface{}{
"filename": "",
"filepath": "",
})
if err != nil {
t.Fatal(err)
}
wg := sync.WaitGroup{}
for i := 0; i < 32; i++ {
wg.Add(1)
go func(i int) {
s, err := NewScanner(r)
if err != nil {
t.Fatal(err)
}
filepath.Walk(initialPath, func(name string, info os.FileInfo, inErr error) error {
fmt.Printf("[%02d] %s\n", i, name)
if inErr != nil {
fmt.Printf("[%02d] Walk to \"%s\": %s\n", i, name, inErr)
return nil
}
if info.IsDir() && info.Mode()&os.ModeSymlink != 0 {
fmt.Printf("[%02d] Walk to \"%s\": Skipping symlink\n", i, name)
return filepath.SkipDir
}
if !info.Mode().IsRegular() || info.Size() >= 2000000 {
return nil
}
s.DefineVariable("filepath", name)
s.DefineVariable("filename", info.Name())
var m MatchRules
if err := s.SetCallback(&m).ScanFile(name); err == nil {
fmt.Printf("[%02d] Scan \"%s\": %d\n", i, path.Base(name), len(m))
} else {
fmt.Printf("[%02d] Scan \"%s\": %s\n", i, path.Base(name), err)
}
return nil
})
wg.Done()
}(i)
}
wg.Wait()
}