@@ -11,7 +11,6 @@ import (
1111 "testing"
1212
1313 "github.com/chainguard-dev/clog"
14- "github.com/chainguard-dev/clog/slogtest"
1514 "github.com/chainguard-dev/malcontent/pkg/archive"
1615 "github.com/chainguard-dev/malcontent/pkg/malcontent"
1716 "github.com/chainguard-dev/malcontent/pkg/programkind"
@@ -215,7 +214,7 @@ func TestExtractNestedArchive(t *testing.T) {
215214
216215func TestScanArchive (t * testing.T ) {
217216 t .Parallel ()
218- ctx := slogtest . Context ( t )
217+ ctx := context . Background ( )
219218 clog .FromContext (ctx ).With ("test" , "scan_archive" )
220219
221220 var out bytes.Buffer
@@ -260,6 +259,95 @@ func TestScanArchive(t *testing.T) {
260259 }
261260}
262261
262+ func extractError (e error ) error {
263+ if strings .Contains (e .Error (), "not a valid gzip archive" ) || strings .Contains (e .Error (), "not a valid zip file" ) {
264+ return nil
265+ }
266+ return e
267+ }
268+
269+ func TestScanInvalidArchive (t * testing.T ) {
270+ t .Parallel ()
271+ ctx := context .Background ()
272+ clog .FromContext (ctx ).With ("test" , "scan_invalid_archive" )
273+
274+ var out bytes.Buffer
275+ r , err := render .New ("json" , & out )
276+ if err != nil {
277+ t .Fatalf ("render: %v" , err )
278+ }
279+
280+ rfs := []fs.FS {rules .FS , thirdparty .FS }
281+ yrs , err := CachedRules (ctx , rfs )
282+ if err != nil {
283+ t .Fatalf ("rules: %v" , err )
284+ }
285+
286+ mc := malcontent.Config {
287+ Concurrency : runtime .NumCPU (),
288+ ExitExtraction : true ,
289+ IgnoreSelf : false ,
290+ MinFileRisk : 0 ,
291+ MinRisk : 0 ,
292+ Renderer : r ,
293+ Rules : yrs ,
294+ ScanPaths : []string {
295+ "testdata/17419.zip" ,
296+ "testdata/joblib_0.9.4.dev0_compressed_cache_size_pickle_py35_np19.gz" ,
297+ },
298+ }
299+ _ , err = Scan (ctx , mc )
300+ err = extractError (err )
301+ if err != nil {
302+ t .Fatal (err )
303+ }
304+ }
305+
306+ func TestScanInvalidArchiveIgnore (t * testing.T ) {
307+ t .Parallel ()
308+ ctx := context .Background ()
309+ clog .FromContext (ctx ).With ("test" , "scan_invalid_archive_ignore" )
310+
311+ var out bytes.Buffer
312+ r , err := render .New ("json" , & out )
313+ if err != nil {
314+ t .Fatalf ("render: %v" , err )
315+ }
316+
317+ rfs := []fs.FS {rules .FS , thirdparty .FS }
318+ yrs , err := CachedRules (ctx , rfs )
319+ if err != nil {
320+ t .Fatalf ("rules: %v" , err )
321+ }
322+
323+ mc := malcontent.Config {
324+ Concurrency : runtime .NumCPU (),
325+ ExitExtraction : false ,
326+ IgnoreSelf : false ,
327+ MinFileRisk : 0 ,
328+ MinRisk : 0 ,
329+ Renderer : r ,
330+ Rules : yrs ,
331+ ScanPaths : []string {
332+ "testdata/17419.zip" ,
333+ "testdata/joblib_0.9.4.dev0_compressed_cache_size_pickle_py35_np19.gz" ,
334+ },
335+ }
336+ res , err := Scan (ctx , mc )
337+ if err != nil {
338+ t .Fatal (err )
339+ }
340+ if err := r .Full (ctx , nil , res ); err != nil {
341+ t .Fatalf ("full: %v" , err )
342+ }
343+
344+ got := out .String ()
345+ want := "{}\n "
346+ if diff := cmp .Diff (want , got ); diff != "" {
347+ t .Errorf ("output mismatch: (-want +got):\n %s" , diff )
348+ }
349+ }
350+
263351func TestGetExt (t * testing.T ) {
264352 tests := []struct {
265353 path string
0 commit comments