diff --git a/grype/matcher/java/matcher_test.go b/grype/matcher/java/matcher_test.go index 649c3df229a..2052400bedb 100644 --- a/grype/matcher/java/matcher_test.go +++ b/grype/matcher/java/matcher_test.go @@ -1,7 +1,11 @@ package java import ( + "context" + "errors" + "strings" "testing" + "time" "github.com/google/uuid" "github.com/stretchr/testify/assert" @@ -39,7 +43,8 @@ func TestMatcherJava_matchUpstreamMavenPackage(t *testing.T) { MavenSearcher: newMockSearcher(p), } store := newMockProvider() - actual, _ := matcher.matchUpstreamMavenPackages(store, nil, p) + context := context.Background() + actual, _ := matcher.matchUpstreamMavenPackages(context, store, nil, p) assert.Len(t, actual, 2, "unexpected matches count") @@ -64,3 +69,85 @@ func TestMatcherJava_matchUpstreamMavenPackage(t *testing.T) { t.Logf("discovered CVES: %+v", foundCVEs) } } +func TestMatcherJava_TestMatchUpstreamMavenPackagesTimeout(t *testing.T) { + p := pkg.Package{ + ID: pkg.ID(uuid.NewString()), + Name: "org.springframework.spring-webmvc", + Version: "5.1.5.RELEASE", + Language: syftPkg.Java, + Type: syftPkg.JavaPkg, + Metadata: pkg.JavaMetadata{ + ArchiveDigests: []pkg.Digest{ + { + Algorithm: "sha1", + Value: "236e3bfdbdc6c86629237a74f0f11414adb4e211", + }, + }, + }, + } + matcher := Matcher{ + cfg: MatcherConfig{ + ExternalSearchConfig: ExternalSearchConfig{ + SearchMavenUpstream: true, + }, + UseCPEs: false, + }, + MavenSearcher: newMockSearcher(p), + } + store := newMockProvider() + + // Create a context with a very short timeout + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) + defer cancel() + + _, err := matcher.matchUpstreamMavenPackages(ctx, store, nil, p) + // Check if the error is a context deadline exceeded error + if err == nil { + t.Errorf("expected an error but got none") + } else if !errors.Is(err, context.DeadlineExceeded) { + t.Errorf("expected a context deadline exceeded error but got a different error: %v", err) + } +} + +func TestMatcherJava_TestMatchTimeoutExtraction(t *testing.T) { + // Create a Matcher with an invalid AbortAfter value + matcher := Matcher{ + cfg: MatcherConfig{ + ExternalSearchConfig: ExternalSearchConfig{ + SearchMavenUpstream: true, + AbortAfter: "invalid_duration", // This cannot be parsed into a duration + }, + UseCPEs: false, + }, + MavenSearcher: newMockSearcher(pkg.Package{}), + } + + // Call Match + _, err := matcher.Match(nil, nil, pkg.Package{}) + if err == nil { + t.Error("expected an error but got none") + } else if !strings.Contains(err.Error(), "failed to parse timeout duration") { + t.Errorf("expected a 'failed to parse timeout duration' error but got %v", err) + } +} + +func TestMatcherJava_TestMatchWithCorrectAbortAfterVal(t *testing.T) { + // Create a Matcher with a valid AbortAfter value + matcher := Matcher{ + cfg: MatcherConfig{ + ExternalSearchConfig: ExternalSearchConfig{ + SearchMavenUpstream: true, + AbortAfter: "1ns", + }, + UseCPEs: false, + }, + MavenSearcher: newMockSearcher(pkg.Package{}), + } + + // Call Match + store := newMockProvider() + _, err := matcher.Match(store, nil, pkg.Package{}) + if err != nil { + t.Errorf("expected no error but got %v", err) + } +}