-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverification_test.go
More file actions
112 lines (97 loc) · 2.99 KB
/
verification_test.go
File metadata and controls
112 lines (97 loc) · 2.99 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
// TestGhostPackCore is a simple integration test
// It mocks a server that serves a package.json and a yarn.lock
// And checks if the scanner finds them and correctly identifies packages.
// Since the actual main() reads from stdin and prints to stdout,
// we will test the core functions: ProbeDomain, ParsePackageJSON, CheckPackage.
func TestGhostPackCore(t *testing.T) {
// 1. Mock NPM Registry
mockRegistry := func(req *http.Request) (*http.Response, error) {
if req.URL.Path == "/@internal/secret-pkg" {
return &http.Response{
StatusCode: 404,
Body: http.NoBody,
}, nil
}
if req.URL.Path == "/react" {
return &http.Response{
StatusCode: 200,
Body: http.NoBody,
}, nil
}
return &http.Response{StatusCode: 404, Body: http.NoBody}, nil
}
client := &http.Client{
Transport: &MockTransport{RoundTripFunc: mockRegistry},
}
// 2. Test Analysis
res1 := CheckPackage(client, "@internal/secret-pkg")
if res1.Status != "VULNERABLE" {
t.Errorf("Expected @internal/secret-pkg to be VULNERABLE, got %s", res1.Status)
}
res2 := CheckPackage(client, "react")
if res2.Status != "SAFE" {
t.Errorf("Expected react to be SAFE, got %s", res2.Status)
}
// 3. Test Parsing
pkgJSON := `{"dependencies": {"react": "^17.0.0"}, "devDependencies": {"@internal/secret-pkg": "1.0.0"}}`
pkgs, err := ParsePackageJSON([]byte(pkgJSON))
if err != nil {
t.Fatalf("Parse error: %v", err)
}
if len(pkgs) != 2 {
t.Errorf("Expected 2 packages, found %d", len(pkgs))
}
// 4. Test Discovery via Mock Server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/package.json" {
w.WriteHeader(200)
w.Write([]byte(pkgJSON))
} else {
w.WriteHeader(404)
}
}))
defer ts.Close()
// Use a real client for local server probing
probeClient := &http.Client{}
found, err := ProbeDomain(ts.URL, probeClient, nil)
if err != nil {
t.Fatalf("ProbeDomain failed: %v", err)
}
if len(found) == 0 {
t.Fatal("Expected to find package.json, found nothing")
}
if found[0].Type != "json" {
t.Errorf("Expected type json, got %s", found[0].Type)
}
}
func TestProtocolDefault(t *testing.T) {
// Check if ProbeDomain adds http:// to bare domains
// We mock the transport to capture the URL scheme
var params []string
mockTransport := func(req *http.Request) (*http.Response, error) {
params = append(params, req.URL.Scheme)
return &http.Response{StatusCode: 404, Body: http.NoBody}, nil
}
client := &http.Client{
Transport: &MockTransport{RoundTripFunc: mockTransport},
}
ProbeDomain("example.com", client, nil)
if len(params) == 0 {
t.Fatal("No requests made")
}
if params[0] != "http" {
t.Errorf("Expected scheme http, got %s", params[0])
}
}
type MockTransport struct {
RoundTripFunc func(req *http.Request) (*http.Response, error)
}
func (m *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return m.RoundTripFunc(req)
}