forked from Velocidex/go-yara
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem_blocks_test.go
72 lines (65 loc) · 1.55 KB
/
mem_blocks_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
package yara
import (
"testing"
)
type block struct {
base uint64
data []byte
}
type testIter struct {
data []block
current int
}
func (it *testIter) First() *MemoryBlock {
it.current = 0
return it.Next()
}
func (it *testIter) Next() *MemoryBlock {
if it.current >= len(it.data) {
return nil
}
data := it.data[it.current].data
base := it.data[it.current].base
it.current += 1
return &MemoryBlock{
Base: base,
Size: uint64(len(data)),
FetchData: func(buf []byte) { copy(buf, data) },
}
}
func TestIterator(t *testing.T) {
rs := MustCompile(`
rule t1 { condition: true }
//rule a { strings: $a = "aaaa" condition: all of them }
//rule b { strings: $b = "bbbb" condition: all of them }
rule t2 {
strings: $a = "aaaa" $b = "bbbb"
condition: $a at 0 and $b at 32
}
`, nil)
var mrs MatchRules
if err := rs.ScanMemBlocksWithCallback(&testIter{}, 0, 0, &mrs); err != nil {
t.Errorf("simple iterator scan (no data): %v", err)
} else {
t.Logf("simple iterator scan (no data): %v", mrs)
}
mrs = nil
if err := rs.ScanMemBlocksWithCallback(&testIter{
data: []block{{0, nil}},
}, 0, 0, &mrs); err != nil {
t.Errorf("simple iterator scan (empty block): %v", err)
} else {
t.Logf("simple iterator scan (empty block): %+v", mrs)
}
mrs = nil
if err := rs.ScanMemBlocksWithCallback(&testIter{
data: []block{
{0, []byte("aaaaaaaaaaaaaaaa")},
{32, []byte("bbbbbbbbbbbbbbbb")},
},
}, 0, 0, &mrs); err != nil {
t.Errorf("simple iterator scan (aaa..bbbb): %v", err)
} else {
t.Logf("simple iterator scan (aaa..bbb): %+v", mrs)
}
}