Skip to content

Commit 7840eb9

Browse files
committed
add CycloneDX, SARIF skip upload tests
Signed-off-by: Jeff Rescignano <jeffr@defenseunicorns.com>
1 parent 1d9689a commit 7840eb9

2 files changed

Lines changed: 130 additions & 0 deletions

File tree

pkg/attestation/crafter/materials/cyclonedxjson_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,70 @@ func TestCyclonedxJSONCraft(t *testing.T) {
224224
})
225225
}
226226
}
227+
228+
func TestCycloneDXJSONCraft_SkipUpload(t *testing.T) {
229+
testCases := []struct {
230+
name string
231+
skipUpload bool
232+
wantUpload bool
233+
}{
234+
{
235+
name: "upload enabled (default)",
236+
skipUpload: false,
237+
wantUpload: true,
238+
},
239+
{
240+
name: "upload skipped via contract",
241+
skipUpload: true,
242+
wantUpload: false,
243+
},
244+
}
245+
246+
ast := assert.New(t)
247+
l := zerolog.Nop()
248+
filePath := "./testdata/sbom.cyclonedx.json"
249+
250+
for _, tc := range testCases {
251+
t.Run(tc.name, func(t *testing.T) {
252+
schema := &contractAPI.CraftingSchema_Material{
253+
Name: "test",
254+
Type: contractAPI.CraftingSchema_Material_SBOM_CYCLONEDX_JSON,
255+
SkipUpload: tc.skipUpload,
256+
}
257+
258+
// Mock uploader - only expect upload call if not skipped
259+
uploader := mUploader.NewUploader(t)
260+
if tc.wantUpload {
261+
uploader.On("UploadFile", context.TODO(), filePath).
262+
Return(&casclient.UpDownStatus{
263+
Digest: "deadbeef",
264+
Filename: "sbom.cyclonedx.json",
265+
}, nil)
266+
}
267+
268+
backend := &casclient.CASBackend{Uploader: uploader}
269+
crafter, err := materials.NewCyclonedxJSONCrafter(schema, backend, &l)
270+
require.NoError(t, err)
271+
272+
got, err := crafter.Craft(context.TODO(), filePath)
273+
require.NoError(t, err)
274+
275+
ast.Equal(contractAPI.CraftingSchema_Material_SBOM_CYCLONEDX_JSON.String(), got.MaterialType.String())
276+
277+
// Verify upload behavior matches expectation
278+
if tc.wantUpload {
279+
ast.True(got.UploadedToCas, "material should be uploaded when skip_upload is false")
280+
} else {
281+
ast.False(got.UploadedToCas, "material should not be uploaded when skip_upload is true")
282+
}
283+
284+
// Verify digest is always computed regardless of upload
285+
ast.Equal("sha256:16159bb881eb4ab7eb5d8afc5350b0feeed1e31c0a268e355e74f9ccbe885e0c", got.GetSbomArtifact().Artifact.Digest)
286+
ast.Equal("sbom.cyclonedx.json", got.GetSbomArtifact().Artifact.Name)
287+
288+
// Verify main component extraction still works
289+
ast.Equal(".", got.GetSbomArtifact().MainComponent.Name)
290+
ast.Equal("file", got.GetSbomArtifact().MainComponent.Kind)
291+
})
292+
}
293+
}

pkg/attestation/crafter/materials/sarif_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,66 @@ func TestSARIFCraft(t *testing.T) {
130130
})
131131
}
132132
}
133+
134+
func TestSARIFCraft_SkipUpload(t *testing.T) {
135+
testCases := []struct {
136+
name string
137+
skipUpload bool
138+
wantUpload bool
139+
}{
140+
{
141+
name: "upload enabled (default)",
142+
skipUpload: false,
143+
wantUpload: true,
144+
},
145+
{
146+
name: "upload skipped via contract",
147+
skipUpload: true,
148+
wantUpload: false,
149+
},
150+
}
151+
152+
assert := assert.New(t)
153+
l := zerolog.Nop()
154+
filePath := "./testdata/report.sarif"
155+
156+
for _, tc := range testCases {
157+
t.Run(tc.name, func(t *testing.T) {
158+
schema := &contractAPI.CraftingSchema_Material{
159+
Name: "test",
160+
Type: contractAPI.CraftingSchema_Material_SARIF,
161+
SkipUpload: tc.skipUpload,
162+
}
163+
164+
// Mock uploader - only expect upload call if not skipped
165+
uploader := mUploader.NewUploader(t)
166+
if tc.wantUpload {
167+
uploader.On("UploadFile", context.TODO(), filePath).
168+
Return(&casclient.UpDownStatus{
169+
Digest: "deadbeef",
170+
Filename: "report.sarif",
171+
}, nil)
172+
}
173+
174+
backend := &casclient.CASBackend{Uploader: uploader}
175+
crafter, err := materials.NewSARIFCrafter(schema, backend, &l)
176+
require.NoError(t, err)
177+
178+
got, err := crafter.Craft(context.TODO(), filePath)
179+
require.NoError(t, err)
180+
181+
assert.Equal(contractAPI.CraftingSchema_Material_SARIF.String(), got.MaterialType.String())
182+
183+
// Verify upload behavior matches expectation
184+
if tc.wantUpload {
185+
assert.True(got.UploadedToCas, "material should be uploaded when skip_upload is false")
186+
} else {
187+
assert.False(got.UploadedToCas, "material should not be uploaded when skip_upload is true")
188+
}
189+
190+
// Verify digest is always computed regardless of upload
191+
assert.Equal("sha256:c4a63494f9289dd9fd44f841efb4f5b52765c2de6332f2d86e5f6c0340b40a95", got.GetArtifact().Digest)
192+
assert.Equal("report.sarif", got.GetArtifact().Name)
193+
})
194+
}
195+
}

0 commit comments

Comments
 (0)