-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathschema_test.go
108 lines (92 loc) · 2.67 KB
/
schema_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
// Copyright 2020 Joakim Kennedy. All rights reserved. Use of
// this source code is governed by the included BSD license.
//+build long_test
package stix2_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/TcM1911/stix2"
"github.com/stretchr/testify/assert"
"github.com/xeipuuv/gojsonschema"
)
func TestCollectionToBundle(t *testing.T) {
sloader := gojsonschema.NewReferenceLoader("http://raw.githubusercontent.com/oasis-open/cti-stix2-json-schemas/stix2.1/schemas/common/bundle.json")
schema, err := gojsonschema.NewSchema(sloader)
if err != nil {
t.Fatalf("Failed to load schema: %s\n", err)
}
t.Run("create_from_collection", func(t *testing.T) {
assert := assert.New(t)
c := &stix2.Collection{}
ip, err := stix2.NewIPv4Address("10.0.0.1")
assert.NoError(err)
c.Add(ip)
ip, err = stix2.NewIPv4Address("10.0.0.2")
assert.NoError(err)
c.Add(ip)
b, err := c.ToBundle()
assert.NoError(err)
assert.NotNil(b)
data, err := json.Marshal(b)
assert.NoError(err)
assert.NotNil(data)
docloader := gojsonschema.NewBytesLoader(data)
result, err := schema.Validate(docloader)
assert.NoError(err)
assert.True(result.Valid())
})
t.Run("examples", func(t *testing.T) {
runFolder(t, schema, "examples")
})
t.Run("threat-reports", func(t *testing.T) {
runFolder(t, schema, filepath.Join("examples", "threat-reports"))
})
}
func runFolder(t *testing.T, schema *gojsonschema.Schema, path string) {
assert := assert.New(t)
pth, err := filepath.Abs(filepath.Join("testresources", path))
if err != nil {
t.Fatalf("Error when resolving abs path to resource files: %s\n", err)
}
info, err := ioutil.ReadDir(pth)
if err != nil {
t.Fatalf("Error when loading resource files: %s\n", err)
}
for _, f := range info {
if f.IsDir() || !strings.HasSuffix(f.Name(), ".json") {
continue
}
fr, err := os.OpenFile(filepath.Join(pth, f.Name()), os.O_RDONLY, 0600)
if err != nil {
t.Fatalf("Error when opening the file: %s\n", err)
}
inData, err := ioutil.ReadAll(fr)
assert.NoError(err)
fr.Close()
collection, err := stix2.FromJSON(inData)
assert.NoError(err)
assert.NotNil(collection)
bundle, err := collection.ToBundle()
assert.NoError(err)
assert.NotNil(bundle)
outData, err := json.Marshal(bundle)
assert.NoError(err)
assert.NotNil(outData)
docloader := gojsonschema.NewBytesLoader(outData)
result, err := schema.Validate(docloader)
assert.NoError(err)
assert.True(result.Valid(), f.Name()+" failed to validate")
if !result.Valid() {
for _, desc := range result.Errors() {
fmt.Printf("- %s\n", desc)
}
// Dump the output data
fmt.Println(string(outData))
}
}
}