forked from package-url/packageurl-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackageurl_test.go
207 lines (190 loc) · 6.36 KB
/
packageurl_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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
Copyright (c) the purl authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package packageurl_test
import (
"encoding/json"
"os"
"reflect"
"testing"
"github.com/package-url/packageurl-go"
)
type TestFixture struct {
Description string `json:"description"`
Purl string `json:"purl"`
CanonicalPurl string `json:"canonical_purl"`
PackageType string `json:"type"`
Namespace string `json:"namespace"`
Name string `json:"name"`
Version string `json:"version"`
QualifierMap map[string]string `json:"qualifiers"`
Subpath string `json:"subpath"`
IsInvalid bool `json:"is_invalid"`
}
func (t TestFixture) Qualifiers() packageurl.Qualifiers {
q := packageurl.Qualifiers{}
for k, v := range t.QualifierMap {
q.Add(k, v)
}
return q
}
// TestFromStringExamples verifies that parsing example strings produce expected
// results.
func TestFromStringExamples(t *testing.T) {
// Read the json file
data, err := os.ReadFile("testdata/test-suite-data.json")
if err != nil {
t.Fatal(err)
}
// Load the json file contents into a structure
testData := []TestFixture{}
err = json.Unmarshal(data, &testData)
if err != nil {
t.Fatal(err)
}
// Use FromString on each item in the test set
for _, tc := range testData {
// Should parse without issue
p, err := packageurl.FromString(tc.Purl)
if tc.IsInvalid == false {
if err != nil {
t.Logf("%s failed: %s", tc.Description, err)
t.Fail()
}
// verify parsing
if p.Type != tc.PackageType {
t.Logf("%s: incorrect package type: wanted: '%s', got '%s'", tc.Description, tc.PackageType, p.Type)
t.Fail()
}
if p.Namespace != tc.Namespace {
t.Logf("%s: incorrect namespace: wanted: '%s', got '%s'", tc.Description, tc.Namespace, p.Namespace)
t.Fail()
}
if p.Name != tc.Name {
t.Logf("%s: incorrect name: wanted: '%s', got '%s'", tc.Description, tc.Name, p.Name)
t.Fail()
}
if p.Version != tc.Version {
t.Logf("%s: incorrect version: wanted: '%s', got '%s'", tc.Description, tc.Version, p.Version)
t.Fail()
}
if !reflect.DeepEqual(p.Qualifiers, tc.Qualifiers()) {
t.Logf("%s: incorrect qualifiers: wanted: '%#v', got '%#v'", tc.Description, tc.Qualifiers(), p.Qualifiers)
t.Fail()
}
if p.Subpath != tc.Subpath {
t.Logf("%s: incorrect subpath: wanted: '%s', got '%s'", tc.Description, tc.Subpath, p.Subpath)
t.Fail()
}
} else {
// Invalid cases
if err == nil {
t.Logf("%s did not fail and returned %#v", tc.Description, p)
t.Fail()
}
}
}
}
// TestToStringExamples verifies that the resulting package urls created match
// the expected format.
func TestToStringExamples(t *testing.T) {
// Read the json file
data, err := os.ReadFile("testdata/test-suite-data.json")
if err != nil {
t.Fatal(err)
}
// Load the json file contents into a structure
var testData []TestFixture
err = json.Unmarshal(data, &testData)
if err != nil {
t.Fatal(err)
}
// Use ToString on each item
for _, tc := range testData {
// Skip invalid items
if tc.IsInvalid == true {
continue
}
instance := packageurl.NewPackageURL(
tc.PackageType, tc.Namespace, tc.Name, tc.Version,
tc.Qualifiers(), tc.Subpath)
result := instance.ToString()
// NOTE: We create a purl with ToString and then load into a PackageURL
// because qualifiers may not be in any order. By reparsing back
// we can ensure the data transfers between string and instance form.
canonical, _ := packageurl.FromString(tc.CanonicalPurl)
toTest, _ := packageurl.FromString(result)
// If the two results don't equal then the ToString failed
if !reflect.DeepEqual(toTest, canonical) {
t.Logf("%s failed: %s != %s", tc.Description, result, tc.CanonicalPurl)
t.Fail()
}
}
}
// TestStringer verifies that the Stringer implementation produces results
// equivalent with the ToString method.
func TestStringer(t *testing.T) {
// Read the json file
data, err := os.ReadFile("testdata/test-suite-data.json")
if err != nil {
t.Fatal(err)
}
// Load the json file contents into a structure
var testData []TestFixture
err = json.Unmarshal(data, &testData)
if err != nil {
t.Fatal(err)
}
// Use ToString on each item
for _, tc := range testData {
// Skip invalid items
if tc.IsInvalid == true {
continue
}
purlPtr := packageurl.NewPackageURL(
tc.PackageType, tc.Namespace, tc.Name,
tc.Version, tc.Qualifiers(), tc.Subpath)
purlValue := *purlPtr
// Verify that the Stringer implementation returns a result
// equivalent to ToString().
if purlPtr.ToString() != purlPtr.String() {
t.Logf("%s failed: Stringer implementation differs from ToString: %s != %s", tc.Description, purlPtr.String(), purlPtr.ToString())
t.Fail()
}
// Verify that the %s format modifier works for values.
fmtStr := purlValue.String()
if fmtStr != purlPtr.String() {
t.Logf("%s failed: %%s format modifier does not work on values: %s != %s", tc.Description, fmtStr, purlPtr.ToString())
t.Fail()
}
}
}
func TestNameEscaping(t *testing.T) {
testCases := map[string]string{
"abc": "pkg:deb/abc",
"ab/c": "pkg:deb/ab%2Fc",
}
for name, output := range testCases {
t.Run(name, func(t *testing.T) {
p := &packageurl.PackageURL{Type: "deb", Name: name}
if s := p.ToString(); s != output {
t.Fatalf("wrong escape. expected=%q, got=%q", output, s)
}
})
}
}