Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ee/agent-smith/pkg/classifier/signature_test.go
2501 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package classifier
6
7
import (
8
"bytes"
9
"os"
10
"testing"
11
12
"github.com/h2non/filetype"
13
"github.com/h2non/filetype/matchers"
14
)
15
16
func TestMatchELF(t *testing.T) {
17
exec := "/bin/bash"
18
input, err := os.OpenFile(exec, os.O_RDONLY, 0600)
19
if err != nil {
20
t.Errorf("cannot open test executable: %v", err)
21
return
22
}
23
defer input.Close()
24
25
sfc := SignatureReadCache{
26
Reader: input,
27
}
28
29
sig := Signature{
30
Kind: ObjectELFSymbols,
31
Pattern: []byte("bash_groupname_completion_function"),
32
Regexp: false,
33
}
34
if err := sig.Validate(); err != nil {
35
t.Errorf("invalid signature: %v", err)
36
return
37
}
38
39
matches, err := sig.Matches(&sfc)
40
if err != nil {
41
t.Errorf("cannot match signature: %v", err)
42
return
43
}
44
if !matches {
45
t.Errorf("%s does not match signature", exec)
46
}
47
}
48
49
func TestMatchAny(t *testing.T) {
50
tests := []struct {
51
Input []byte
52
Signature Signature
53
Matches bool
54
Err error
55
}{
56
{[]byte("hello world!!"), Signature{Kind: ObjectAny, Pattern: []byte("o w")}, true, nil},
57
{[]byte("abab dede"), Signature{Kind: ObjectAny, Pattern: []byte("ab"), Slice: Slice{5, 0}}, false, nil},
58
{[]byte("abab dede"), Signature{Kind: ObjectAny, Pattern: []byte("de"), Slice: Slice{5, 0}}, true, nil},
59
}
60
61
for i, test := range tests {
62
if err := test.Signature.Validate(); err != nil {
63
t.Errorf("[%03d] invalid signature: %v", i, err)
64
return
65
}
66
67
sfc := SignatureReadCache{
68
Reader: bytes.NewReader(test.Input),
69
}
70
71
matches, err := test.Signature.matchAny(&sfc)
72
if err != nil {
73
t.Errorf("[%03d] cannot match signature: %v", i, err)
74
return
75
}
76
if matches != test.Matches {
77
t.Errorf("[%03d] expected match == %v, actual is %v", i, test.Matches, matches)
78
}
79
}
80
}
81
82
func TestIsELF(t *testing.T) {
83
tests := []struct {
84
Name string
85
Input []byte
86
}{
87
{"ELF", []byte{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
88
{"JPEG2000", []byte{0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A}},
89
{"too short", []byte{}},
90
}
91
92
for _, test := range tests {
93
// test our input against a known library to make sure we actually have an ELF binary on our hands
94
isActuallyELF := filetype.IsType(test.Input, matchers.TypeElf)
95
weThinkItsELF := isELF(test.Input)
96
97
if weThinkItsELF != isActuallyELF {
98
t.Errorf("%s: input does not test as same ELF-ness as gold standard. %v expected, %v actual", test.Name, isActuallyELF, weThinkItsELF)
99
}
100
}
101
}
102
103