-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathclair_test.go
87 lines (80 loc) · 2.44 KB
/
clair_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
package main
import (
"bytes"
"errors"
"io"
"net/http"
"testing"
)
// TestParseReportID tests the parseReportID function
func TestParseReportID(t *testing.T) {
data := []byte(`{"manifest_hash": "dummy-report-id"}`)
reportID, err := parseReportID(data)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if reportID != "dummy-report-id" {
t.Errorf("Expected report ID to be 'dummy-report-id', got '%s'", reportID)
}
}
// TestParseVulnerabilityReport tests the parseVulnerabilityReport function
func TestParseVulnerabilityReport(t *testing.T) {
data := []byte(`{
"vulnerabilities": {
"CVE-1234": {
"name": "CVE-1234",
"severity": "High",
"description": "Test vulnerability",
"package": {"name": "test-package", "version": "1.0"},
"links": "http://example.com"
}
}
}`)
vulnerabilities, err := parseVulnerabilityReport(data)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if len(vulnerabilities) != 1 {
t.Fatalf("Expected 1 vulnerability, got %d", len(vulnerabilities))
}
if vulnerabilities[0].FeatureName != "test-package" {
t.Errorf("Expected FeatureName to be 'test-package', got '%s'", vulnerabilities[0].FeatureName)
}
}
// TestFetchVulnerabilities tests the fetchVulnerabilities function
func TestFetchVulnerabilities(t *testing.T) {
mockClient := &MockHTTPClient{
DoFunc: func(req *http.Request) (*http.Response, error) {
if req.URL.Path == "/matcher/api/v1/vulnerability_report/dummy-report-id" {
return &http.Response{
StatusCode: 200,
Body: io.NopCloser(bytes.NewReader([]byte(`{
"vulnerabilities": {
"CVE-1234": {
"name": "CVE-1234",
"severity": "High",
"description": "Test vulnerability",
"package": {"name": "test-package", "version": "1.0"},
"links": "http://example.com"
}
}
}`))),
}, nil
}
return nil, errors.New("unexpected request")
},
}
headers := map[string]string{"Authorization": "Bearer dummy-token"}
clairURL := "http://example.com"
reportID := "dummy-report-id"
vulnerabilities, err := fetchVulnerabilities(mockClient, headers, clairURL, reportID)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if len(vulnerabilities) != 1 {
t.Fatalf("Expected 1 vulnerability, got %d", len(vulnerabilities))
}
if vulnerabilities[0].FeatureName != "test-package" {
t.Errorf("Expected FeatureName to be 'test-package', got '%s'", vulnerabilities[0].FeatureName)
}
}