-
Notifications
You must be signed in to change notification settings - Fork 2
/
artifact_test.go
153 lines (136 loc) · 4.06 KB
/
artifact_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
package stix2
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestArtifact(t *testing.T) {
assert := assert.New(t)
payload := Binary([]byte("Hello World"))
mime := "mime/type"
url := "url://"
hashes := Hashes{"SHA-256": "2cbb138d4097f05fffeb968b34a4e62884fc4755d7d043e2d3760950f1e1a9ee", "MD5": "0da609bd9ec46237557373f1c5cfcae9"}
key := "key"
t.Run("missing_property", func(t *testing.T) {
obj, err := NewArtifact(nil)
assert.Nil(obj)
assert.Equal(ErrPropertyMissing, err)
})
t.Run("url_and_payload", func(t *testing.T) {
obj, err := NewArtifact(OptionURL(url), OptionPayload(payload))
assert.Nil(obj)
assert.Equal(err, ErrInvalidParameter)
})
t.Run("url_and_no_hash", func(t *testing.T) {
obj, err := NewArtifact(OptionURL(url))
assert.Nil(obj)
assert.Equal(err, ErrPropertyMissing)
})
t.Run("url_and_hash", func(t *testing.T) {
obj, err := NewArtifact(OptionURL(url), OptionHashes(hashes))
assert.NoError(err)
assert.Equal(url, obj.URL)
})
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),
//
OptionMimeType(mime),
OptionPayload(payload),
OptionHashes(hashes),
OptionEncryption(EncryptionAlgorithmAES256GCM),
OptionKey(key),
}
obj, err := NewArtifact(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(mime, obj.MimeType)
assert.Equal(payload, obj.Payload)
assert.Equal(hashes, obj.Hashes)
assert.Equal(EncryptionAlgorithmAES256GCM, obj.Encryption)
assert.Equal(key, obj.Key)
})
t.Run("id-generation", func(t *testing.T) {
tests := []struct {
payload Binary
hashes Hashes
id string
}{
{payload, nil, "artifact--2640de4e-9baf-5a92-8f14-dcc7628ec983"},
{payload, hashes, "artifact--ead91fe9-8a76-5413-9feb-94292e1622ea"},
{nil, hashes, "artifact--e9ad4fc1-2f44-538d-98c5-5e226ea95501"},
}
for _, test := range tests {
var arg STIXOption
if test.payload == nil {
arg = OptionURL(url)
} else {
arg = OptionPayload(test.payload)
}
obj, err := NewArtifact(
arg,
OptionHashes(test.hashes),
)
assert.NoError(err)
assert.Equal(Identifier(test.id), obj.ID)
}
})
t.Run("parse_json", func(t *testing.T) {
data := []byte(`{
"type": "artifact",
"spec_version": "2.1",
"id": "artifact--6f437177-6e48-5cf8-9d9e-872a2bddd641",
"mime_type": "application/zip",
"payload_bin": "SGVsbG8gV29ybGQ=",
"encryption_algorithm": "mime-type-indicated",
"decryption_key": "My voice is my passport"
}`)
var obj *Artifact
err := json.Unmarshal(data, &obj)
assert.NoError(err)
assert.Equal(Identifier("artifact--6f437177-6e48-5cf8-9d9e-872a2bddd641"), obj.ID)
assert.Equal("2.1", obj.SpecVersion)
assert.Equal(TypeArtifact, obj.Type)
assert.Equal("application/zip", obj.MimeType)
assert.Equal("SGVsbG8gV29ybGQ=", obj.Payload.String())
assert.Equal(EncryptionAlgorithmMimeTypeIndicated, obj.Encryption)
assert.Equal("My voice is my passport", obj.Key)
})
}
func TestEncryptionAlgorithm(t *testing.T) {
assert := assert.New(t)
t.Run("stringer", func(t *testing.T) {
tests := []struct {
expected string
alg EncryptionAlgorithm
}{
{"AES-256-GCM", EncryptionAlgorithmAES256GCM},
{"ChaCha20-Poly1305", EncryptionAlgorithmChaCha20Poly1305},
{"mime-type-indicated", EncryptionAlgorithmMimeTypeIndicated},
}
for _, test := range tests {
assert.Equal(test.expected, test.alg.String())
}
})
t.Run("unmarshalJSON", func(t *testing.T) {
data := []byte(`{
"type": "artifact",
"spec_version": "2.1",
"encryption_algorithm": "something not accepted"
}`)
var obj *Artifact
err := json.Unmarshal(data, &obj)
assert.NoError(err)
assert.Equal(EncryptionAlgorithmNone, obj.Encryption)
})
}