-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbomkit-theia_test.go
178 lines (156 loc) · 5.49 KB
/
cbomkit-theia_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
// Copyright 2024 IBM
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package main
import (
"bytes"
"encoding/hex"
"io"
"os"
"path/filepath"
"testing"
"github.com/IBM/cbomkit-theia/provider/cyclonedx"
"github.com/IBM/cbomkit-theia/provider/docker"
"github.com/IBM/cbomkit-theia/provider/filesystem"
"github.com/IBM/cbomkit-theia/scanner"
"github.com/IBM/cbomkit-theia/scanner/hash"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
"go.uber.org/dig"
cdx "github.com/CycloneDX/cyclonedx-go"
)
var testfileFolder string = "./testdata"
var outputExtension string = "/out/bom.json"
var bomFolderExtension string = "/in/bom.json"
var dockerfileExtension string = "/image/Dockerfile"
var dirExtension string = "/dir"
type testType int
const (
testTypeDir testType = iota + 1
testTypeImageBuild
testTypeImageGet
)
var tests = []struct {
testType testType
additionalInfo string
in string
err bool
}{
{testTypeImageBuild, "", "/0_empty", false},
{testTypeImageGet, "busybox", "/0_empty", false},
{testTypeImageBuild, "", "/1_exclude_single_algorithm", false},
{testTypeImageBuild, "", "/2_tomcat", false},
{testTypeImageBuild, "", "/3_certificates", false},
{testTypeDir, "", "/4_unknown_keySize", false},
{testTypeDir, "", "/5_single_certificate", false},
{testTypeDir, "", "/6_malformed_java_security", false},
{testTypeDir, "", "/7_private_key", false},
{testTypeDir, "", "/8_secrets", false},
}
func TestScan(t *testing.T) {
schemaPath := filepath.Join("provider", "cyclonedx", "bom-1.6.schema.json")
for _, test := range tests {
t.Run(test.in+", BOM: "+test.in, func(t *testing.T) {
tempTarget := new(bytes.Buffer)
var runErr error
container := dig.New()
if err := container.Provide(func() string {
return testfileFolder + test.in + bomFolderExtension
}, dig.Name("bomFilePath")); err != nil {
panic(err)
}
if err := container.Provide(func() string {
return schemaPath
}, dig.Name("bomSchemaPath")); err != nil {
panic(err)
}
if err := container.Provide(func() io.Writer {
return tempTarget
}); err != nil {
panic(err)
}
for _, pluginConstructor := range scanner.GetAllPluginConstructors() {
if err := container.Provide(pluginConstructor, dig.Group("plugins")); err != nil {
panic(err)
}
}
switch test.testType {
case testTypeImageBuild:
dockerfilePath := filepath.Join(testfileFolder, test.in, dockerfileExtension)
image, err := docker.BuildNewImage(dockerfilePath)
assert.NoError(t, err)
defer image.TearDown()
err = container.Provide(func() filesystem.Filesystem {
return docker.GetSquashedFilesystem(image)
})
assert.NoError(t, err)
runErr = container.Invoke(scanner.ReadFilesAndRunScan)
case testTypeImageGet:
image, err := docker.GetPrebuiltImage(test.additionalInfo)
assert.NoError(t, err)
defer image.TearDown()
err = container.Provide(func() filesystem.Filesystem {
return docker.GetSquashedFilesystem(image)
})
assert.NoError(t, err)
runErr = container.Invoke(scanner.ReadFilesAndRunScan)
case testTypeDir:
err := container.Provide(func() filesystem.Filesystem {
return filesystem.NewPlainFilesystem(filepath.Join(testfileFolder, test.in, dirExtension))
})
assert.NoError(t, err)
runErr = container.Invoke(scanner.ReadFilesAndRunScan)
}
if test.err {
assert.Error(t, runErr, "scan did not fail although it should")
} else {
assert.NoError(t, runErr, "scan did fail although it should not")
}
bomReaderTrue, _ := os.Open(filepath.Join(testfileFolder, test.in, outputExtension))
schemaReader, _ := os.Open(schemaPath)
bomTrue, err := cyclonedx.ParseBOM(bomReaderTrue, schemaReader)
assert.NoError(t, err)
schemaReader.Seek(0, 0)
bomCurrent, err := cyclonedx.ParseBOM(tempTarget, schemaReader)
assert.NoError(t, err)
assert.Empty(t, cmp.Diff(*bomTrue, *bomCurrent,
cmpopts.SortSlices(func(a cdx.Service, b cdx.Service) bool {
return a.Name < b.Name
}),
cmpopts.SortSlices(func(a cdx.Component, b cdx.Component) bool {
aHash := hash.HashCDXComponentWithoutRefs(a)
bHash := hash.HashCDXComponentWithoutRefs(b)
return hex.EncodeToString(aHash[:]) < hex.EncodeToString(bHash[:])
}),
cmpopts.SortSlices(func(a cdx.EvidenceOccurrence, b cdx.EvidenceOccurrence) bool {
if a.Location != b.Location {
return a.Location < b.Location
} else {
return *a.Line < *b.Line
}
}),
cmpopts.IgnoreTypes(cdx.Dependency{}),
cmpopts.IgnoreFields(cdx.Component{},
"BOMRef",
"CryptoProperties.CertificateProperties.SignatureAlgorithmRef",
"CryptoProperties.CertificateProperties.SubjectPublicKeyRef",
"CryptoProperties.RelatedCryptoMaterialProperties.AlgorithmRef",
"CryptoProperties.RelatedCryptoMaterialProperties.SecuredBy.AlgorithmRef",
"CryptoProperties.ProtocolProperties.CryptoRefArray"),
))
})
}
}