-
Notifications
You must be signed in to change notification settings - Fork 6
/
option_test.go
250 lines (198 loc) · 5.37 KB
/
option_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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package stix2
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestCyberObservableOptions(t *testing.T) {
a := assert.New(t)
type testObject struct {
STIXCyberObservableObject
}
t.Run("happy_path", func(t *testing.T) {
o := &testObject{}
err := OptionSpecVersion("2.1")(o)
a.NoError(err)
a.Equal("2.1", o.SpecVersion)
err = OptionDefanged(true)(o)
a.NoError(err)
a.True(o.Defanged)
ids := []Identifier{Identifier("test-id")}
err = OptionObjectMarking(ids)(o)
a.NoError(err)
a.Equal(ids, o.ObjectMarking)
gms := []*GranularMarking{{Lang: "test"}}
err = OptionGranularMarking(gms)(o)
a.NoError(err)
a.Equal(gms, o.GranularMarking)
ext := map[string]string{}
ext["TestField"] = "test value"
err = OptionExtension("TestExtension", ext)(o)
a.NoError(err)
a.Len(o.Extensions, 1)
a.Contains(o.Extensions["TestExtension"].(map[string]string)["TestField"], "test value")
})
t.Run("nil_object", func(t *testing.T) {
err := OptionDefanged(true)(nil)
a.Error(err)
a.Equal("object must be a pointer type", err.Error())
})
t.Run("empty_object", func(t *testing.T) {
obj := &emptryTestStruct{}
err := OptionDefanged(true)(obj)
a.Error(err)
a.Equal("failed to apply Defanged option: Defanged not found", err.Error())
})
t.Run("not_pinter_to_struct", func(t *testing.T) {
obj := badType("Test")
err := OptionDefanged(true)(&obj)
a.Error(err)
a.Equal("object has to be a pointer to struct", err.Error())
})
t.Run("field_in_main_object", func(t *testing.T) {
obj := &testStructWithDefanged{}
err := OptionDefanged(true)(obj)
a.NoError(err)
a.True(obj.Defanged)
})
t.Run("wrong_field_kind_1", func(t *testing.T) {
obj := &structWithWrongKind{}
err := OptionDefanged(true)(obj)
a.Error(err)
a.Equal("failed to apply Defanged option: STIXCyberObservableObject is not of struct type", err.Error())
})
t.Run("wrong_field_kind_2", func(t *testing.T) {
obj := &structWithWrongFieldKind{}
err := OptionDefanged(true)(obj)
a.Error(err)
a.Equal("bool field is not a Defanged type", err.Error())
})
t.Run("extensions_wrong_field_kind_1", func(t *testing.T) {
obj := &structWithWrongKind{}
err := OptionExtension("tesst", "")(obj)
a.Error(err)
a.Equal("object can not have extensions", err.Error())
})
t.Run("extensions_wrong_extension_kind", func(t *testing.T) {
obj := &struct {
emptryTestStruct
STIXCyberObservableObject struct {
Extensions string
}
}{}
err := OptionExtension("test", "")(obj)
a.Error(err)
a.Equal("object can not have extensions", err.Error())
})
t.Run("extensions_no_field", func(t *testing.T) {
obj := &struct {
emptryTestStruct
STIXCyberObservableObject struct {
Something string
}
}{}
err := OptionExtension("test", "")(obj)
a.Error(err)
a.Equal("object can not have extensions", err.Error())
})
t.Run("extenstion_not_pinter_to_struct", func(t *testing.T) {
// obj := badType("Test")
err := OptionExtension("test", "")(nil)
a.Error(err)
a.Equal("object must be a pointer type", err.Error())
})
t.Run("applyOptions_error", func(t *testing.T) {
err := applyOptions(nil, []STIXOption{OptionDefanged(true)})
a.Error(err)
a.Equal("object must be a pointer type", err.Error())
})
}
type emptryTestStruct struct{}
func (s *emptryTestStruct) GetExtendedTopLevelProperties() *CustomObject {
panic("Failed")
}
func (s *emptryTestStruct) GetCreated() *time.Time {
panic("Failed")
}
func (s *emptryTestStruct) GetModified() *time.Time {
panic("Failed")
}
func (s *emptryTestStruct) GetType() STIXType {
panic("Failed")
}
func (s *emptryTestStruct) GetID() Identifier {
panic("Failed")
}
type testStructWithDefanged struct {
emptryTestStruct
Defanged bool
}
type structWithWrongKind struct {
emptryTestStruct
STIXCyberObservableObject bool
}
type structWithWrongFieldKind struct {
emptryTestStruct
Defanged string
}
type badType string
func (s *badType) GetExtendedTopLevelProperties() *CustomObject {
panic("Failed")
}
func (s *badType) GetCreated() *time.Time {
panic("Failed")
}
func (s *badType) GetModified() *time.Time {
panic("Failed")
}
func (s *badType) GetType() STIXType {
panic("Failed")
}
func (s *badType) GetID() Identifier {
panic("Failed")
}
func TestDomainObjectOptions(t *testing.T) {
a := assert.New(t)
type testObject struct {
STIXDomainObject
}
o := &testObject{}
ts := &Timestamp{time.Now()}
id := Identifier("test-id")
sarray := []string{"test slice"}
refs := []*ExternalReference{{Name: "Test ref"}}
gms := []*GranularMarking{{Lang: "Test marking"}}
err := OptionSpecVersion("2.1")(o)
a.NoError(err)
a.Equal("2.1", o.SpecVersion)
err = OptionCreatedBy(id)(o)
a.NoError(err)
a.Equal(id, o.CreatedBy)
err = OptionCreated(ts)(o)
a.NoError(err)
a.Equal(ts, o.Created)
err = OptionModified(ts)(o)
a.NoError(err)
a.Equal(ts, o.Modified)
err = OptionRevoked(true)(o)
a.NoError(err)
a.True(o.Revoked)
err = OptionLabels(sarray)(o)
a.NoError(err)
a.Equal(sarray, o.Labels)
err = OptionConfidence(100)(o)
a.NoError(err)
a.Equal(100, o.Confidence)
err = OptionLang("en")(o)
a.NoError(err)
a.Equal("en", o.Lang)
err = OptionExternalReferences(refs)(o)
a.NoError(err)
a.Equal(refs, o.ExternalReferences)
err = OptionObjectMarking([]Identifier{id})(o)
a.NoError(err)
a.Contains(o.ObjectMarking, id)
err = OptionGranularMarking(gms)(o)
a.NoError(err)
a.Equal(gms, o.GranularMarking)
}