-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsoftware_test.go
129 lines (117 loc) · 3.15 KB
/
software_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
// Copyright 2020 Joakim Kennedy. All rights reserved. Use of
// this source code is governed by the included BSD license.
package stix2
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSoftware(t *testing.T) {
assert := assert.New(t)
val := "Software name"
testStr := "Test string"
t.Run("missing_property", func(t *testing.T) {
obj, err := NewSoftware("", nil)
assert.Nil(obj)
assert.Equal(ErrInvalidParameter, err)
})
t.Run("with_property", func(t *testing.T) {
obj, err := NewSoftware(val, nil)
assert.NotNil(obj)
assert.NoError(err)
})
t.Run("payload_with_options", func(t *testing.T) {
marking := make([]*GranularMarking, 0)
objmark := []Identifier{Identifier("id")}
specVer := "2.0"
opts := []STIXOption{
OptionGranularMarking(marking),
OptionObjectMarking(objmark),
OptionSpecVersion(specVer),
OptionDefanged(true),
//
OptionCPE(testStr),
OptionSWID(testStr),
OptionLanguages([]string{testStr}),
OptionVendor(testStr),
OptionVersion(testStr),
}
obj, err := NewSoftware(val, opts...)
assert.NotNil(obj)
assert.NoError(err)
assert.Equal(marking, obj.GranularMarking)
assert.Equal(objmark, obj.ObjectMarking)
assert.Equal(specVer, obj.SpecVersion)
assert.True(obj.Defanged)
assert.Equal(val, obj.Name)
assert.Equal(testStr, obj.CPE)
assert.Equal(testStr, obj.SWID)
assert.Equal([]string{testStr}, obj.Languages)
assert.Equal(testStr, obj.Vendor)
assert.Equal(testStr, obj.Version)
})
t.Run("id-generation", func(t *testing.T) {
tests := []struct {
name string
cpe string
vendor string
version string
id string
}{
{
"Word",
"",
"",
"",
"software--b2ad0d37-3ded-5b98-96f6-8fbb994ba540",
},
{
"Word",
"cpe:2.3:a:microsoft:word:2000:*:*:*:*:*:*:*",
"",
"",
"software--2a04f5b2-6a03-5762-bb36-5fd126e20d6c",
},
{
"Word",
"cpe:2.3:a:microsoft:word:2000:*:*:*:*:*:*:*",
"Microsoft",
"",
"software--cfdedf86-ec9c-5769-ae75-2062f68c3313",
},
{
"Word",
"cpe:2.3:a:microsoft:word:2000:*:*:*:*:*:*:*",
"Microsoft",
"2002",
"software--c9ce663b-b8f8-5256-8cd9-3c0db09808dd",
},
}
for _, test := range tests {
opts := make([]STIXOption, 0, 5)
if test.cpe != "" {
opts = append(opts, OptionCPE(test.cpe))
}
if test.vendor != "" {
opts = append(opts, OptionVendor(test.vendor))
}
if test.version != "" {
opts = append(opts, OptionVersion(test.version))
}
obj, err := NewSoftware(test.name, opts...)
assert.NoError(err)
assert.Equal(Identifier(test.id), obj.ID)
}
})
// t.Run("parse_json", func(t *testing.T) {
// data := []byte(``)
// var obj *Software
// err := json.Unmarshal(data, &obj)
// assert.NoError(err)
// assert.Equal(Identifier("network-traffic--2568d22a-8998-58eb-99ec-3c8ca74f527d"), obj.ID)
// assert.Equal("2.1", obj.SpecVersion)
// assert.Equal(TypeSoftware, obj.Type)
// assert.Equal(Identifier("ipv4-addr--4d22aae0-2bf9-5427-8819-e4f6abf20a53"), obj.Src)
// assert.Equal(Identifier("ipv4-addr--ff26c055-6336-5bc5-b98d-13d6226742dd"), obj.Dst)
// assert.Equal("tcp", obj.Protocols[0])
// })
}