This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodule_bom_test.go
289 lines (260 loc) · 7.48 KB
/
module_bom_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
package nodemodulebom_test
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"testing"
nodemodulebom "github.com/paketo-buildpacks/node-module-bom"
"github.com/paketo-buildpacks/node-module-bom/fakes"
"github.com/paketo-buildpacks/packit/v2"
//nolint Ignore SA1019, informed usage of deprecated package
"github.com/paketo-buildpacks/packit/v2/paketosbom"
"github.com/paketo-buildpacks/packit/v2/pexec"
"github.com/paketo-buildpacks/packit/v2/scribe"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testModuleBOM(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
workingDir string
executable *fakes.Executable
buffer *bytes.Buffer
commandOutput *bytes.Buffer
moduleBOM nodemodulebom.ModuleBOM
)
it.Before(func() {
var err error
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())
executable = &fakes.Executable{}
executable.ExecuteCall.Stub = func(execution pexec.Execution) error {
Expect(os.WriteFile(filepath.Join(workingDir, "bom.json"), []byte(`{
"bomFormat": "CycloneDX",
"specVersion": "1.3",
"serialNumber": "urn:uuid:a717bde3-8a77-4ec6-a530-5d0d9007ecbe",
"version": 1,
"metadata": {
"timestamp": "2021-08-16T19:35:52.107Z",
"tools": [
{
"vendor": "CycloneDX",
"name": "Node.js module",
"version": "3.0.3"
}
],
"component": {
"type": "library"
}
},
"components": [
{
"type": "library",
"name": "leftpad",
"version": "0.0.1",
"description": "left pad numbers",
"hashes": [
{
"alg": "SHA-1",
"content": "86b1a4de4face180ac545a83f1503523d8fed115"
}
],
"licenses": [
{
"license": {
"id": "BSD-3-Clause"
}
}
],
"purl": "pkg:npm/leftpad@0.0.1"
},
{
"type": "library",
"name": "rightpad",
"version": "1.0.0",
"description": "right pad numbers",
"hashes": [
{
"alg": "SHA-256",
"content": "123456789"
}
],
"licenses": [
{
"license": {
"id": "Apache"
}
}
],
"purl": "pkg:npm/rightpad@1.0.0"
}
]
}
`), 0600)).To(Succeed())
return nil
}
buffer = bytes.NewBuffer(nil)
commandOutput = bytes.NewBuffer(nil)
moduleBOM = nodemodulebom.NewModuleBOM(executable, scribe.NewEmitter(buffer))
})
it.After(func() {
Expect(os.RemoveAll(workingDir)).To(Succeed())
})
context("Generate", func() {
it("succeeds in installing the BOM generation tool and creating the BOM", func() {
bomEntries, err := moduleBOM.Generate(workingDir)
Expect(err).ToNot(HaveOccurred())
Expect(executable.ExecuteCall.Receives.Execution).To(Equal(pexec.Execution{
Args: []string{"-o", "bom.json"},
Dir: workingDir,
Stdout: commandOutput,
Stderr: commandOutput,
}))
algorithm, err := paketosbom.GetBOMChecksumAlgorithm("SHA-1")
Expect(err).ToNot(HaveOccurred())
Expect(bomEntries).To(Equal([]packit.BOMEntry{
{
Name: "leftpad",
Metadata: paketosbom.BOMMetadata{
Checksum: paketosbom.BOMChecksum{
Algorithm: algorithm,
Hash: "86b1a4de4face180ac545a83f1503523d8fed115",
},
PURL: "pkg:npm/leftpad@0.0.1",
Licenses: []string{"BSD-3-Clause"},
Version: "0.0.1",
},
},
{
Name: "rightpad",
Metadata: paketosbom.BOMMetadata{
PURL: "pkg:npm/rightpad@1.0.0",
Licenses: []string{"Apache"},
Version: "1.0.0",
Checksum: paketosbom.BOMChecksum{
Algorithm: paketosbom.SHA256,
Hash: "123456789",
},
},
},
}))
Expect(filepath.Join(workingDir, "bom.json")).ToNot(BeAnExistingFile())
})
context("the bom.json has no hashes", func() {
it.Before(func() {
executable.ExecuteCall.Stub = func(execution pexec.Execution) error {
Expect(os.WriteFile(filepath.Join(workingDir, "bom.json"), []byte(`{
"components": [
{
"type": "library",
"name": "leftpad",
"version": "0.0.1",
"description": "left pad numbers",
"licenses": [
{
"license": {
"id": "BSD-3-Clause"
}
}
],
"purl": "pkg:npm/leftpad@0.0.1"
}
]
}`), 0600)).To(Succeed())
return nil
}
})
it("the output BOM does not contain hashes", func() {
bomEntries, err := moduleBOM.Generate(workingDir)
Expect(err).ToNot(HaveOccurred())
Expect(bomEntries).To(Equal([]packit.BOMEntry{
{
Name: "leftpad",
Metadata: paketosbom.BOMMetadata{
PURL: "pkg:npm/leftpad@0.0.1",
Licenses: []string{"BSD-3-Clause"},
Version: "0.0.1",
},
},
}))
})
})
context("failure cases", func() {
context("the cyclonedx-bom executable call fails", func() {
it.Before(func() {
executable.ExecuteCall.Stub = func(execution pexec.Execution) error {
fmt.Fprintln(execution.Stdout, "build error stdout")
fmt.Fprintln(execution.Stderr, "build error stderr")
return errors.New("error")
}
})
it("returns an error", func() {
_, err := moduleBOM.Generate(workingDir)
Expect(err).To(MatchError("failed to run cyclonedx-bom: error"))
Expect(buffer.String()).To(ContainSubstring(" build error stdout"))
Expect(buffer.String()).To(ContainSubstring(" build error stderr"))
})
})
context("cannot open the bom.json file", func() {
it.Before(func() {
executable.ExecuteCall.Stub = func(execution pexec.Execution) error {
Expect(os.WriteFile(filepath.Join(workingDir, "bom.json"), []byte(``), 0000)).To(Succeed())
return nil
}
})
it("returns an error", func() {
_, err := moduleBOM.Generate(workingDir)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(ContainSubstring("failed to open bom.json")))
})
})
context("cannot decode the bom.json into a struct", func() {
it.Before(func() {
executable.ExecuteCall.Stub = func(execution pexec.Execution) error {
Expect(os.WriteFile(filepath.Join(workingDir, "bom.json"), []byte(``), 0600)).To(Succeed())
return nil
}
})
it("returns an error", func() {
_, err := moduleBOM.Generate(workingDir)
Expect(err).To(MatchError(ContainSubstring("failed to decode bom.json")))
})
})
context("the BOM entry contains unsupported checksum algorithm", func() {
it.Before(func() {
executable.ExecuteCall.Stub = func(execution pexec.Execution) error {
Expect(os.WriteFile(filepath.Join(workingDir, "bom.json"), []byte(`{
"components": [
{
"type": "library",
"name": "leftpad",
"version": "0.0.1",
"description": "left pad numbers",
"hashes": [
{
"alg": "randomAlgorithm",
"content": "86b1a4de4face180ac545a83f1503523d8fed115"
}
]
}
]
}`), 0600)).To(Succeed())
return nil
}
buffer = bytes.NewBuffer(nil)
commandOutput = bytes.NewBuffer(nil)
moduleBOM = nodemodulebom.NewModuleBOM(executable, scribe.NewEmitter(buffer))
})
it.After(func() {
Expect(os.RemoveAll(workingDir)).To(Succeed())
})
it("returns an error", func() {
_, err := moduleBOM.Generate(workingDir)
Expect(err).To(MatchError("failed to get supported BOM checksum algorithm: randomAlgorithm is not valid"))
})
})
})
})
}