-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathcyclonedx_test.go
More file actions
137 lines (109 loc) · 4.19 KB
/
cyclonedx_test.go
File metadata and controls
137 lines (109 loc) · 4.19 KB
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
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1
package sbom_test
import (
"bytes"
"os"
"testing"
"github.com/CycloneDX/cyclonedx-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mondoo.com/cnquery/v12/sbom"
"go.mondoo.com/cnquery/v12/sbom/generator"
)
func TestCycloneDxOutput(t *testing.T) {
report, err := generator.LoadReport("./testdata/alpine.json")
require.NoError(t, err)
sboms := generator.GenerateBom(report)
// store bom in different formats
selectedBom := sboms[0]
exporter := sbom.New(sbom.FormatCycloneDxJSON)
exporter.ApplyOptions(sbom.WithCPE(), sbom.WithEvidence())
output := bytes.Buffer{}
err = exporter.Render(&output, selectedBom)
require.NoError(t, err)
data := output.String()
// os.WriteFile("./testdata/bom_cyclone.json", output.Bytes(), 0700)
assert.Contains(t, data, "cyclonedx")
// ensure os package is included
assert.Contains(t, data, "alpine-baselayout")
assert.Contains(t, data, "cpe:2.3:a:alpine-baselayout:alpine-baselayout:1695795276:aarch64:*:*:*:*:*:*")
// check that package files are included
assert.Contains(t, data, "etc/profile.d/color_prompt.sh.disabled")
// ensure python package is included
assert.Contains(t, data, "pip")
assert.Contains(t, data, "cpe:2.3:a:pip_project:pip:21.2.4:*:*:*:*:*:*:*")
assert.Contains(t, data, "pkg:pypi/pip@21.2.4")
// ensure npm package is included
assert.Contains(t, data, "npm")
assert.Contains(t, data, "cpe:2.3:a:npm:npm:10.2.4:*:*:*:*:*:*:*")
assert.Contains(t, data, "pkg:npm/npm@10.2.4")
}
func TestCycloneDxJsonDecoding(t *testing.T) {
t.Run("alpine 3.19", func(t *testing.T) {
f, err := os.Open("./testdata/alpine-319.cyclone.json")
require.NoError(t, err)
formatHandler := &sbom.CycloneDX{
Format: cyclonedx.BOMFileFormatJSON,
}
bom, err := formatHandler.Parse(f)
require.NoError(t, err)
assert.NotNil(t, bom)
})
t.Run("ubuntu 20.04 container", func(t *testing.T) {
f, err := os.Open("./testdata/ubuntu-20.04-cyclonedx.json")
require.NoError(t, err)
formatHandler := &sbom.CycloneDX{
Format: cyclonedx.BOMFileFormatJSON,
}
bom, err := formatHandler.Parse(f)
require.NoError(t, err)
assert.NotNil(t, bom)
// verify we have the right asset and platform information.
assert.Equal(t, "ubuntu", bom.Asset.Platform.Name)
assert.Equal(t, "20.04", bom.Asset.Platform.Version)
assert.Equal(t, []string{"linux", "unix", "os"}, bom.Asset.Platform.Family)
assert.Equal(t, "Ubuntu 20.04.6 LTS", bom.Asset.Platform.Title)
// this is the bom-ref
assert.Equal(t, []string{"//platformid.api.mondoo.app/runtime/docker/images/e3cf4bf83104fade"}, bom.Asset.PlatformIds)
// 1 library components + 1 os component
assert.Len(t, bom.Packages, 2)
// verify the generator is correct
assert.Equal(t, "syft", bom.Generator.Name)
assert.Equal(t, "1.38.2", bom.Generator.Version)
assert.Equal(t, "anchore", bom.Generator.Vendor)
})
t.Run("ubuntu 22.04 container", func(t *testing.T) {
f, err := os.Open("./testdata/ubuntu-22.04-cyclonedx.json")
require.NoError(t, err)
formatHandler := &sbom.CycloneDX{
Format: cyclonedx.BOMFileFormatJSON,
}
bom, err := formatHandler.Parse(f)
require.NoError(t, err)
assert.NotNil(t, bom)
// verify we have the right asset and platform information.
assert.Equal(t, "ubuntu", bom.Asset.Platform.Name)
assert.Equal(t, "22.04", bom.Asset.Platform.Version)
assert.Equal(t, []string{"linux", "unix", "os"}, bom.Asset.Platform.Family)
assert.Equal(t, "Ubuntu 22.04.5 LTS", bom.Asset.Platform.Title)
// this is the bom-ref
assert.Equal(t, []string{"//platformid.api.mondoo.app/runtime/docker/images/2e194621f3c81dfe"}, bom.Asset.PlatformIds)
// 1 library components + 1 os component
assert.Len(t, bom.Packages, 2)
// verify the generator is correct
assert.Equal(t, "syft", bom.Generator.Name)
assert.Equal(t, "1.38.2", bom.Generator.Version)
assert.Equal(t, "anchore", bom.Generator.Vendor)
})
}
func TestCycloneDxXmlDecoding(t *testing.T) {
f, err := os.Open("./testdata/alpine-319.cyclone.xml")
require.NoError(t, err)
formatHandler := &sbom.CycloneDX{
Format: cyclonedx.BOMFileFormatXML,
}
bom, err := formatHandler.Parse(f)
require.NoError(t, err)
assert.NotNil(t, bom)
}