forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatted_reader_test.go
307 lines (243 loc) · 12.4 KB
/
formatted_reader_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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package sbom_test
import (
"bytes"
"encoding/json"
"io"
"os"
"testing"
"time"
"github.com/anchore/syft/syft"
"github.com/paketo-buildpacks/packit/v2/sbom"
"github.com/paketo-buildpacks/packit/v2/sbom/internal/formats/syft2"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testFormattedReader(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
bom sbom.SBOM
)
it.Before(func() {
var err error
bom, err = sbom.Generate("testdata/")
Expect(err).NotTo(HaveOccurred())
})
it("writes the SBOM in the default CycloneDX format", func() {
buffer := bytes.NewBuffer(nil)
_, err := io.Copy(buffer, sbom.NewFormattedReader(bom, sbom.CycloneDXFormat))
Expect(err).NotTo(HaveOccurred())
format := syft.IdentifyFormat(buffer.Bytes())
Expect(format.ID()).To(Equal(syft.CycloneDxJSONFormatID))
// Ensures pretty printing
Expect(buffer.String()).To(ContainSubstring(`{
"bomFormat": "CycloneDX",
"components": [
{`))
var cdxOutput cdxOutput
err = json.Unmarshal(buffer.Bytes(), &cdxOutput)
Expect(err).NotTo(HaveOccurred(), buffer.String())
Expect(cdxOutput.BOMFormat).To(Equal("CycloneDX"), buffer.String())
Expect(cdxOutput.SpecVersion).To(Equal("1.3"), buffer.String())
Expect(cdxOutput.SerialNumber).To(Equal(""), buffer.String())
Expect(cdxOutput.Metadata.Timestamp).To(Equal(""), buffer.String())
Expect(cdxOutput.Metadata.Component.Type).To(Equal("file"), buffer.String())
Expect(cdxOutput.Metadata.Component.Type).To(Equal("file"), buffer.String())
Expect(cdxOutput.Metadata.Component.Name).To(Equal("testdata/"), buffer.String())
Expect(cdxOutput.Components[0].Name).To(Equal("collapse-white-space"), buffer.String())
Expect(cdxOutput.Components[1].Name).To(Equal("end-of-stream"), buffer.String())
Expect(cdxOutput.Components[2].Name).To(Equal("insert-css"), buffer.String())
Expect(cdxOutput.Components[3].Name).To(Equal("once"), buffer.String())
Expect(cdxOutput.Components[4].Name).To(Equal("pump"), buffer.String())
Expect(cdxOutput.Components[5].Name).To(Equal("wrappy"), buffer.String())
rerunBuffer := bytes.NewBuffer(nil)
_, err = io.Copy(rerunBuffer, sbom.NewFormattedReader(bom, sbom.CycloneDXFormat))
Expect(err).NotTo(HaveOccurred())
Expect(rerunBuffer.String()).To(Equal(buffer.String()))
})
it("writes the SBOM in the latest CycloneDX format (1.4)", func() {
buffer := bytes.NewBuffer(nil)
_, err := io.Copy(buffer, sbom.NewFormattedReader(bom, sbom.Format(syft.CycloneDxJSONFormatID)))
Expect(err).NotTo(HaveOccurred())
format := syft.IdentifyFormat(buffer.Bytes())
Expect(format.ID()).To(Equal(syft.CycloneDxJSONFormatID))
var cdxOutput cdxOutput
err = json.Unmarshal(buffer.Bytes(), &cdxOutput)
Expect(err).NotTo(HaveOccurred(), buffer.String())
Expect(cdxOutput.BOMFormat).To(Equal("CycloneDX"), buffer.String())
Expect(cdxOutput.SpecVersion).To(Equal("1.4"), buffer.String())
Expect(cdxOutput.SerialNumber).To(Equal(""), buffer.String())
Expect(cdxOutput.Metadata.Timestamp).To(Equal(""), buffer.String())
Expect(cdxOutput.Metadata.Component.Type).To(Equal("file"), buffer.String())
Expect(cdxOutput.Metadata.Component.Name).To(Equal("testdata/"), buffer.String())
Expect(cdxOutput.Components[0].Name).To(Equal("collapse-white-space"), buffer.String())
Expect(cdxOutput.Components[1].Name).To(Equal("end-of-stream"), buffer.String())
Expect(cdxOutput.Components[2].Name).To(Equal("insert-css"), buffer.String())
Expect(cdxOutput.Components[3].Name).To(Equal("once"), buffer.String())
Expect(cdxOutput.Components[4].Name).To(Equal("pump"), buffer.String())
Expect(cdxOutput.Components[5].Name).To(Equal("wrappy"), buffer.String())
rerunBuffer := bytes.NewBuffer(nil)
_, err = io.Copy(rerunBuffer, sbom.NewFormattedReader(bom, sbom.Format(syft.CycloneDxJSONFormatID)))
Expect(err).NotTo(HaveOccurred())
Expect(rerunBuffer.String()).To(Equal(buffer.String()))
})
context("writes the SBOM in SPDX format, with fields replaced for reproducibility", func() {
it("produces an SBOM", func() {
buffer := bytes.NewBuffer(nil)
_, err := io.Copy(buffer, sbom.NewFormattedReader(bom, sbom.SPDXFormat))
Expect(err).NotTo(HaveOccurred())
format := syft.IdentifyFormat(buffer.Bytes())
Expect(format.ID()).To(Equal(syft.SPDXJSONFormatID))
// Ensures pretty printing
Expect(buffer.String()).To(ContainSubstring(`{
"SPDXID": "SPDXRef-DOCUMENT",
"creationInfo": {`))
var spdxOutput spdxOutput
err = json.Unmarshal(buffer.Bytes(), &spdxOutput)
Expect(err).NotTo(HaveOccurred(), buffer.String())
Expect(spdxOutput.SPDXVersion).To(Equal("SPDX-2.2"), buffer.String())
Expect(spdxOutput.Packages[0].Name).To(Equal("collapse-white-space"), buffer.String())
Expect(spdxOutput.Packages[1].Name).To(Equal("end-of-stream"), buffer.String())
Expect(spdxOutput.Packages[2].Name).To(Equal("insert-css"), buffer.String())
Expect(spdxOutput.Packages[3].Name).To(Equal("once"), buffer.String())
Expect(spdxOutput.Packages[4].Name).To(Equal("pump"), buffer.String())
Expect(spdxOutput.Packages[5].Name).To(Equal("wrappy"), buffer.String())
// Ensure documentNamespace and creationInfo.created have reproducible values
Expect(spdxOutput.DocumentNamespace).To(Equal("https://paketo.io/packit/dir/testdata-e5ba1162-56a7-57ac-8372-3aff3f15e036"), buffer.String())
Expect(spdxOutput.CreationInfo.Created).To(BeZero(), buffer.String())
rerunBuffer := bytes.NewBuffer(nil)
_, err = io.Copy(rerunBuffer, sbom.NewFormattedReader(bom, sbom.SPDXFormat))
Expect(err).NotTo(HaveOccurred())
Expect(rerunBuffer.String()).To(Equal(buffer.String()))
})
context("when SOURCE_DATE_EPOCH is set", func() {
var original string
it.Before(func() {
original = os.Getenv("SOURCE_DATE_EPOCH")
Expect(os.Setenv("SOURCE_DATE_EPOCH", "1659551872")).To(Succeed())
})
it.After(func() {
Expect(os.Setenv("SOURCE_DATE_EPOCH", original)).To(Succeed())
})
context("when the timestamp is valid", func() {
it.Before(func() {
Expect(os.Setenv("SOURCE_DATE_EPOCH", "1659551872")).To(Succeed())
})
it("produces an SBOM with the given timestamp", func() {
buffer := bytes.NewBuffer(nil)
_, err := io.Copy(buffer, sbom.NewFormattedReader(bom, sbom.SPDXFormat))
Expect(err).NotTo(HaveOccurred())
var spdxOutput spdxOutput
err = json.Unmarshal(buffer.Bytes(), &spdxOutput)
Expect(err).NotTo(HaveOccurred(), buffer.String())
format := syft.IdentifyFormat(buffer.Bytes())
Expect(format.ID()).To(Equal(syft.SPDXJSONFormatID))
Expect(spdxOutput.SPDXVersion).To(Equal("SPDX-2.2"), buffer.String())
Expect(spdxOutput.Packages[0].Name).To(Equal("collapse-white-space"), buffer.String())
Expect(spdxOutput.Packages[1].Name).To(Equal("end-of-stream"), buffer.String())
Expect(spdxOutput.Packages[2].Name).To(Equal("insert-css"), buffer.String())
Expect(spdxOutput.Packages[3].Name).To(Equal("once"), buffer.String())
Expect(spdxOutput.Packages[4].Name).To(Equal("pump"), buffer.String())
Expect(spdxOutput.Packages[5].Name).To(Equal("wrappy"), buffer.String())
// Ensure documentNamespace and creationInfo.created have reproducible values
Expect(spdxOutput.DocumentNamespace).To(Equal("https://paketo.io/packit/dir/testdata-ef57d584-3f15-5c91-be8c-0f7c011883a8"), buffer.String())
Expect(spdxOutput.CreationInfo.Created).To(Equal(time.Unix(1659551872, 0).UTC()), buffer.String())
rerunBuffer := bytes.NewBuffer(nil)
_, err = io.Copy(rerunBuffer, sbom.NewFormattedReader(bom, sbom.SPDXFormat))
Expect(err).NotTo(HaveOccurred())
Expect(rerunBuffer.String()).To(Equal(buffer.String()))
})
context("failure cases", func() {
context("when the timestamp is not valid", func() {
it.Before(func() {
Expect(os.Setenv("SOURCE_DATE_EPOCH", "not-a-valid-timestamp")).To(Succeed())
})
it("returns an error", func() {
buffer := bytes.NewBuffer(nil)
_, err := io.Copy(buffer, sbom.NewFormattedReader(bom, sbom.SPDXFormat))
Expect(err).To(MatchError(ContainSubstring("failed to parse SOURCE_DATE_EPOCH")))
})
})
})
})
})
}, spec.Sequential())
it("writes the SBOM in the default syft format", func() {
buffer := bytes.NewBuffer(nil)
_, err := io.Copy(buffer, sbom.NewFormattedReader(bom, sbom.SyftFormat))
Expect(err).NotTo(HaveOccurred())
var syftOutput syftOutput
err = json.Unmarshal(buffer.Bytes(), &syftOutput)
Expect(err).NotTo(HaveOccurred(), buffer.String())
Expect(syftOutput.Schema.Version).To(Equal(`3.0.1`), buffer.String())
Expect(syftOutput.Source.Type).To(Equal("directory"), buffer.String())
Expect(syftOutput.Source.Target).To(Equal("testdata/"), buffer.String())
Expect(syftOutput.Artifacts[0].Name).To(Equal("collapse-white-space"), buffer.String())
Expect(syftOutput.Artifacts[1].Name).To(Equal("end-of-stream"), buffer.String())
Expect(syftOutput.Artifacts[2].Name).To(Equal("insert-css"), buffer.String())
Expect(syftOutput.Artifacts[3].Name).To(Equal("once"), buffer.String())
Expect(syftOutput.Artifacts[4].Name).To(Equal("pump"), buffer.String())
Expect(syftOutput.Artifacts[5].Name).To(Equal("wrappy"), buffer.String())
rerunBuffer := bytes.NewBuffer(nil)
_, err = io.Copy(rerunBuffer, sbom.NewFormattedReader(bom, sbom.SyftFormat))
Expect(err).NotTo(HaveOccurred())
Expect(rerunBuffer.String()).To(Equal(buffer.String()))
})
it("writes the SBOM in Syft 2.0.2 format", func() {
buffer := bytes.NewBuffer(nil)
_, err := io.Copy(buffer, sbom.NewFormattedReader(bom, sbom.Format(syft2.ID)))
Expect(err).NotTo(HaveOccurred())
// Ensures pretty printing
Expect(buffer.String()).To(ContainSubstring(`{
"artifacts": [
{
"id":`))
var syftOutput syftOutput
err = json.Unmarshal(buffer.Bytes(), &syftOutput)
Expect(err).NotTo(HaveOccurred(), buffer.String())
Expect(syftOutput.Schema.Version).To(Equal("2.0.2"), buffer.String())
Expect(syftOutput.Source.Type).To(Equal("directory"), buffer.String())
Expect(syftOutput.Source.Target).To(Equal("testdata/"), buffer.String())
Expect(syftOutput.Artifacts[0].Name).To(Equal("collapse-white-space"), buffer.String())
Expect(syftOutput.Artifacts[1].Name).To(Equal("end-of-stream"), buffer.String())
Expect(syftOutput.Artifacts[2].Name).To(Equal("insert-css"), buffer.String())
Expect(syftOutput.Artifacts[3].Name).To(Equal("once"), buffer.String())
Expect(syftOutput.Artifacts[4].Name).To(Equal("pump"), buffer.String())
Expect(syftOutput.Artifacts[5].Name).To(Equal("wrappy"), buffer.String())
rerunBuffer := bytes.NewBuffer(nil)
_, err = io.Copy(rerunBuffer, sbom.NewFormattedReader(bom, sbom.Format(syft2.ID)))
Expect(err).NotTo(HaveOccurred())
Expect(rerunBuffer.String()).To(Equal(buffer.String()))
})
it("writes the SBOM in the latest Syft format (7.*)", func() {
buffer := bytes.NewBuffer(nil)
_, err := io.Copy(buffer, sbom.NewFormattedReader(bom, sbom.Format(syft.JSONFormatID)))
Expect(err).NotTo(HaveOccurred())
var syftOutput syftOutput
err = json.Unmarshal(buffer.Bytes(), &syftOutput)
Expect(err).NotTo(HaveOccurred(), buffer.String())
Expect(syftOutput.Schema.Version).To(MatchRegexp(`7\.\d+\.\d+`), buffer.String())
Expect(syftOutput.Source.Type).To(Equal("directory"), buffer.String())
Expect(syftOutput.Source.Target).To(Equal("testdata/"), buffer.String())
Expect(syftOutput.Artifacts[0].Name).To(Equal("collapse-white-space"), buffer.String())
Expect(syftOutput.Artifacts[1].Name).To(Equal("end-of-stream"), buffer.String())
Expect(syftOutput.Artifacts[2].Name).To(Equal("insert-css"), buffer.String())
Expect(syftOutput.Artifacts[3].Name).To(Equal("once"), buffer.String())
Expect(syftOutput.Artifacts[4].Name).To(Equal("pump"), buffer.String())
Expect(syftOutput.Artifacts[5].Name).To(Equal("wrappy"), buffer.String())
rerunBuffer := bytes.NewBuffer(nil)
_, err = io.Copy(rerunBuffer, sbom.NewFormattedReader(bom, sbom.Format(syft.JSONFormatID)))
Expect(err).NotTo(HaveOccurred())
Expect(rerunBuffer.String()).To(Equal(buffer.String()))
})
context("Read", func() {
context("failure cases", func() {
context("when the SBOM cannot be encoded to the given format", func() {
it("returns an error", func() {
formatter := sbom.NewFormattedReader(sbom.SBOM{}, sbom.Format("unknown-format"))
_, err := formatter.Read(make([]byte, 10))
Expect(err).To(MatchError("failed to format sbom: 'unknown-format' is not a valid SBOM format identifier"))
})
})
})
})
}