Skip to content

Commit 1e7e672

Browse files
committed
add tests for maven license packagename
Signed-off-by: Rafi <refaei.shikho@hotmail.com>
1 parent 01597e2 commit 1e7e672

2 files changed

Lines changed: 117 additions & 0 deletions

File tree

internal/core/vulndb/deps_dev_service_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,108 @@ func TestGetVersion(t *testing.T) {
5858
t.Fatalf("expected no error, got %v", err)
5959
}
6060
})
61+
62+
t.Run("should replace slashes with colons for maven packages", func(t *testing.T) {
63+
// Mock server to simulate the deps.dev API
64+
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
65+
// Verify that slashes are replaced with colons for Maven packages
66+
// The package name should be "com.fasterxml.jackson.core:jackson-core" (colons replacing slashes)
67+
expectedPath := "/systems/maven/packages/com.fasterxml.jackson.core:jackson-core/versions/2.13.0"
68+
if r.URL.Path != expectedPath {
69+
t.Errorf("expected path %s, got %s", expectedPath, r.URL.Path)
70+
http.Error(w, "Not Found", http.StatusNotFound)
71+
return
72+
}
73+
w.WriteHeader(http.StatusOK)
74+
w.Write([]byte(`{"versionKey": {"system": "maven", "name": "com.fasterxml.jackson.core:jackson-core", "version": "2.13.0"}}`)) // nolint
75+
}))
76+
defer mockServer.Close()
77+
78+
// Override the depsDevAPIURL to point to the mock server
79+
depsDevAPIURL = mockServer.URL
80+
81+
service := NewDepsDevService()
82+
ctx := context.Background()
83+
84+
// Test with a Maven package that has slashes in the name
85+
_, err := service.GetVersion(ctx, "maven", "com.fasterxml.jackson.core/jackson-core", "2.13.0")
86+
if err != nil {
87+
t.Fatalf("expected no error, got %v", err)
88+
}
89+
})
90+
91+
t.Run("should not modify package names for non-maven ecosystems", func(t *testing.T) {
92+
testCases := []struct {
93+
ecosystem string
94+
packageName string
95+
expectedSystem string
96+
expectedPackage string
97+
}{
98+
{"npm", "react/dom", "npm", "react%2Fdom"}, // npm should keep slashes (URL encoded)
99+
{"golang", "github.com/test/pkg", "go", "github.com%2Ftest%2Fpkg"}, // golang -> go, keep slashes (URL encoded)
100+
{"pypi", "django/contrib", "pypi", "django%2Fcontrib"}, // pypi should keep slashes (URL encoded)
101+
}
102+
103+
for _, tc := range testCases {
104+
t.Run(tc.ecosystem+"_"+tc.packageName, func(t *testing.T) {
105+
// Mock server to simulate the deps.dev API
106+
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
107+
expectedPath := "/systems/" + tc.expectedSystem + "/packages/" + tc.expectedPackage + "/versions/1.0.0"
108+
actualPath := r.URL.Path
109+
// Check RawPath for encoded values if Path is unescaped
110+
if r.URL.RawPath != "" {
111+
actualPath = r.URL.RawPath
112+
}
113+
if actualPath != expectedPath {
114+
t.Errorf("expected path %s, got %s", expectedPath, actualPath)
115+
http.Error(w, "Not Found", http.StatusNotFound)
116+
return
117+
}
118+
w.WriteHeader(http.StatusOK)
119+
w.Write([]byte(`{"versionKey": {"system": "` + tc.expectedSystem + `", "name": "` + tc.packageName + `", "version": "1.0.0"}}`)) // nolint
120+
}))
121+
defer mockServer.Close()
122+
123+
// Override the depsDevAPIURL to point to the mock server
124+
depsDevAPIURL = mockServer.URL
125+
126+
service := NewDepsDevService()
127+
ctx := context.Background()
128+
129+
_, err := service.GetVersion(ctx, tc.ecosystem, tc.packageName, "1.0.0")
130+
if err != nil {
131+
t.Fatalf("expected no error for %s/%s, got %v", tc.ecosystem, tc.packageName, err)
132+
}
133+
})
134+
}
135+
})
136+
137+
t.Run("should handle multiple slashes in maven package names", func(t *testing.T) {
138+
// Mock server to simulate the deps.dev API
139+
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
140+
// Verify that multiple slashes are all replaced with colons
141+
// The package name should be "org.springframework:spring-web:core" (all slashes replaced with colons)
142+
expectedPath := "/systems/maven/packages/org.springframework:spring-web:core/versions/5.3.21"
143+
if r.URL.Path != expectedPath {
144+
t.Errorf("expected path %s, got %s", expectedPath, r.URL.Path)
145+
http.Error(w, "Not Found", http.StatusNotFound)
146+
return
147+
}
148+
w.WriteHeader(http.StatusOK)
149+
w.Write([]byte(`{"versionKey": {"system": "maven", "name": "org.springframework:spring-web:core", "version": "5.3.21"}}`)) // nolint
150+
}))
151+
defer mockServer.Close()
152+
153+
// Override the depsDevAPIURL to point to the mock server
154+
depsDevAPIURL = mockServer.URL
155+
156+
service := NewDepsDevService()
157+
ctx := context.Background()
158+
159+
// Test with a Maven package that has multiple slashes
160+
_, err := service.GetVersion(ctx, "maven", "org.springframework/spring-web/core", "5.3.21")
161+
if err != nil {
162+
t.Fatalf("expected no error, got %v", err)
163+
}
164+
})
61165
}

test_url_escape.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/url"
6+
)
7+
8+
func main() {
9+
fmt.Println("Testing url.PathEscape:")
10+
fmt.Printf("react/dom -> %s\n", url.PathEscape("react/dom"))
11+
fmt.Printf("github.com/test/pkg -> %s\n", url.PathEscape("github.com/test/pkg"))
12+
fmt.Printf("django/contrib -> %s\n", url.PathEscape("django/contrib"))
13+
}

0 commit comments

Comments
 (0)