-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathas_test.go
92 lines (80 loc) · 2.25 KB
/
as_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
// Copyright 2020 Joakim Kennedy. All rights reserved. Use of
// this source code is governed by the included BSD license.
package stix2
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestAS(t *testing.T) {
assert := assert.New(t)
num := int64(42)
name := "System 12"
rir := "RIPE"
t.Run("missing_property", func(t *testing.T) {
obj, err := NewAutonomousSystem(0)
assert.Nil(obj)
assert.Equal(ErrInvalidParameter, err)
})
t.Run("with_property", func(t *testing.T) {
obj, err := NewAutonomousSystem(num, 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),
//
OptionName(name),
OptionRIR(rir),
}
obj, err := NewAutonomousSystem(num, 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(num, obj.Number)
assert.Equal(name, obj.Name)
assert.Equal(rir, obj.RIR)
})
t.Run("id-generation", func(t *testing.T) {
tests := []struct {
number int64
id string
}{
{int64(100), "autonomous-system--0a68995b-d4b2-5f3e-810d-1aeeeb0d4b88"},
}
for _, test := range tests {
obj, err := NewAutonomousSystem(test.number)
assert.NoError(err)
assert.Equal(Identifier(test.id), obj.ID)
}
})
t.Run("parse_json", func(t *testing.T) {
data := []byte(`{
"type": "autonomous-system",
"spec_version": "2.1",
"id": "autonomous-system--f720c34b-98ae-597f-ade5-27dc241e8c74",
"number": 15139,
"name": "Slime Industries",
"rir": "ARIN"
}`)
var obj *AutonomousSystem
err := json.Unmarshal(data, &obj)
assert.NoError(err)
assert.Equal(Identifier("autonomous-system--f720c34b-98ae-597f-ade5-27dc241e8c74"), obj.ID)
assert.Equal("2.1", obj.SpecVersion)
assert.Equal(TypeAutonomousSystem, obj.Type)
assert.Equal(int64(15139), obj.Number)
assert.Equal("Slime Industries", obj.Name)
assert.Equal("ARIN", obj.RIR)
})
}