-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
170 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package osv_generator | ||
|
||
import ( | ||
"os" | ||
"regexp" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/jarcoal/httpmock" | ||
) | ||
|
||
var sampleCSV = []byte(`2023/rhsa-2023_6919.json,2024-12-02T08:22:15+00:00 | ||
2023/rhba-2023_6330.json,2024-12-02T08:22:03+00:00 | ||
2022/rhba-2022_5476.json,2024-12-02T07:52:26+00:00 | ||
2022/rhsa-2022_5249.json,2024-12-02T07:52:17+00:00 | ||
2024/rhsa-2024_5439.json,2024-12-02T07:52:10+00:00`) | ||
|
||
var sampleOSVResult = `{"schema_version":"1.6.0","id":"CVE","database_specific":{"severity":"Moderate","cwe_ids":["CWE"]},"published":"2024-08-20T10:54:54Z","summary":"summary","details":"description","affected":[{"package":{"ecosystem":"Red Hat","name":"redhat/openstack","purl":"pkg:rpm/redhat/openstack@2.0.0"},"ranges":[{"type":"ECOSYSTEM","events":[{"introduced":"0.0.0"},{"fixed":"2.0.0"}]}]}],"references":[{"type":"WEB","url":"fake-url"}]}` | ||
|
||
func TestGetAdvisoryListByModified(t *testing.T) { | ||
// Create a mock | ||
httpmock.Activate() | ||
t.Cleanup(httpmock.DeactivateAndReset) | ||
httpmock.RegisterResponder("GET", "https://security.access.redhat.com/data/csaf/v2/advisories/changes.csv", | ||
httpmock.NewBytesResponder(200, sampleCSV)) | ||
|
||
advisories, err := getAdvisoryListByModified(10) | ||
if err != nil { | ||
t.Fatalf("Expected no error, got %v", err) | ||
} | ||
|
||
expected := []string{"2024/rhsa-2024_5439.json", "2023/rhsa-2023_6919.json", "2022/rhsa-2022_5249.json"} | ||
if !cmp.Equal(advisories, expected) { | ||
t.Fatalf("Different result: %v", cmp.Diff(expected, advisories)) | ||
} | ||
} | ||
|
||
// E2E test of the whole functionality of OSV module | ||
func TestGenerateOSV(t *testing.T) { | ||
// Create a mock | ||
httpmock.Activate() | ||
t.Cleanup(httpmock.DeactivateAndReset) | ||
httpmock.RegisterResponder("GET", "https://security.access.redhat.com/data/csaf/v2/advisories/changes.csv", | ||
httpmock.NewBytesResponder(200, []byte(`test-rhsa-advisory.json,2024-12-02T07:52:10+00:00`))) | ||
// Set up a fake advisory | ||
httpmock.RegisterResponder("GET", "https://security.access.redhat.com/data/csaf/v2/advisories/test-rhsa-advisory.json", | ||
httpmock.NewBytesResponder(200, []byte(`{ | ||
"document": { | ||
"aggregate_severity": { | ||
"text": "Moderate" | ||
} | ||
}, | ||
"vulnerabilities": [ | ||
{ | ||
"cve": "CVE", | ||
"discovery_date": "2024-08-20T10:54:54.042000+00:00", | ||
"cwe": { | ||
"id": "CWE" | ||
}, | ||
"references": [ | ||
{ | ||
"category": "REPORT", | ||
"url": "fake-url" | ||
} | ||
], | ||
"notes": [ | ||
{ | ||
"category": "summary", | ||
"text": "summary" | ||
}, | ||
{ | ||
"category": "description", | ||
"text": "description" | ||
} | ||
] | ||
} | ||
], | ||
"product_tree": { | ||
"branches": [ | ||
{ | ||
"branches": [ | ||
{ | ||
"category": "architecture", | ||
"branches": [ | ||
{ | ||
"product": { | ||
"product_identification_helper": { | ||
"purl": "pkg:rpm/redhat/openstack@2.0.0?arch=src" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
}`))) | ||
|
||
// Create a test file | ||
err := GenerateOSV("testfile") | ||
defer os.Remove("testfile") | ||
if err != nil { | ||
t.Fatalf("Expected no error, got %v", err) | ||
} | ||
createdFile, err := os.ReadFile("testfile") | ||
if err != nil { | ||
t.Fatalf("Expected no error reading the file, got %v", err) | ||
} | ||
|
||
// Remove the modified field since it is set to current time | ||
re := regexp.MustCompile(`"modified":"[^"]*",`) | ||
result := re.ReplaceAllString(string(createdFile), "") | ||
// Remove newlines | ||
result = strings.ReplaceAll(result, "\n", "") | ||
|
||
// Compare the results | ||
if result != sampleOSVResult { | ||
t.Fatalf("Different result: %v", cmp.Diff(string(sampleOSVResult), string(result))) | ||
} | ||
|
||
} |