forked from hillu/go-yara
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler_test.go
104 lines (94 loc) · 2.73 KB
/
compiler_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
// 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 (
"testing"
)
func TestCompiler(t *testing.T) {
c, _ := NewCompiler()
if err := c.AddString(
"rule test : tag1 { meta: author = \"Hilko Bengen\" strings: $a = \"abc\" fullword condition: $a }", "",
); err != nil {
t.Errorf("error: %s", err)
}
if err := c.AddString("xxx", ""); err == nil {
t.Error("did not recognize error")
} else {
t.Logf("expected error: %s", err)
}
}
func TestPanic(t *testing.T) {
defer func() {
err := recover()
if err == nil {
t.Error("MustCompile with broken data did not panic")
} else {
t.Logf("Everything ok, MustCompile panicked: %v", err)
}
}()
_ = MustCompile("asflkjkl", nil)
}
func TestWarnings(t *testing.T) {
c, _ := NewCompiler()
c.AddString("rule foo { bar }", "")
if len(c.Errors) == 0 {
t.Error()
}
t.Logf("Recorded Errors=%#v, Warnings=%#v", c.Errors, c.Warnings)
}
func TestErrors(t *testing.T) {
c, _ := NewCompiler()
c.AddString("rule foo { bar }", "")
if len(c.Errors) == 0 {
t.Error("did not recognize error")
}
expectedError := "rule \"foo\": syntax error, unexpected identifier, expecting <condition>"
if c.Errors[0].Text != expectedError {
t.Errorf("expected error: %#v, got %#v", expectedError, c.Errors[0].Text)
}
}
func TestErrorNoPanic(t *testing.T) {
c, _ := NewCompiler()
c.AddString("rule test { condition: true }", "")
if _, err := c.GetRules(); err != nil {
t.Errorf("did not expect error: %v", err)
}
if err := c.AddString("rule test { }", ""); err == nil {
t.Error("expected AddString after GetRules to fail")
} else {
t.Logf("got error as expected: %v", err)
}
}
func setupCompiler(t *testing.T) *Compiler {
c, err := NewCompiler()
if err != nil {
t.Fatal(err)
}
c.SetIncludeCallback(func(name, rulefile, namespace string) []byte {
t.Logf(`Processing include "%s" (from ns="%s", file="%s")`, name, namespace, rulefile)
if name == "existing" {
return []byte(`rule ext { condition: true }`)
}
return nil
})
return c
}
func TestCompilerIncludeCallback(t *testing.T) {
c := setupCompiler(t)
var err error
if err = c.AddString(`include "existing"`, ""); err != nil {
t.Fatalf(`Failed to include "existing" rule "file": %s`, err)
}
if err = c.AddString(`rule int { condition: ext }`, ""); err != nil {
t.Fatalf(`Failed to define rule referring to included rule: %s`, err)
}
c = setupCompiler(t)
if err = c.AddString(`include "non-existing"`, ""); err != nil {
t.Logf("Compiler returned error on attempt to include non-existing rule: %s", err)
} else {
t.Fatal(`Compiler did not return error on non-existing include rule`)
}
}