-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathPackageURLTest.java
More file actions
315 lines (290 loc) · 13.8 KB
/
PackageURLTest.java
File metadata and controls
315 lines (290 loc) · 13.8 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* MIT License
*
* 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 com.github.packageurl;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
/**
* Test cases for PackageURL parsing
* <p>
* Original test cases retrieved from:
* <a href="https://raw.githubusercontent.com/package-url/purl-spec/master/test-suite-data.json">https://raw.githubusercontent.com/package-url/purl-spec/master/test-suite-data.json</a>
*
* @author Steve Springett
*/
class PackageURLTest {
private static final Locale DEFAULT_LOCALE = Locale.getDefault();
@BeforeAll
static void setup() {
Locale.setDefault(new Locale("tr"));
}
@AfterAll
static void resetLocale() {
Locale.setDefault(DEFAULT_LOCALE);
}
@Test
void validPercentEncoding() throws MalformedPackageURLException {
PackageURL purl = new PackageURL("maven", "com.google.summit", "summit-ast", "2.2.0\n", null, null);
assertEquals("pkg:maven/com.google.summit/summit-ast@2.2.0%0A", purl.toString());
PackageURL purl2 =
new PackageURL("pkg:nuget/%D0%9Cicros%D0%BEft.%D0%95ntit%D1%83Fram%D0%B5work%D0%A1%D0%BEr%D0%B5");
assertEquals("Мicrosоft.ЕntitуFramеworkСоrе", purl2.getName());
assertEquals(
"pkg:nuget/%D0%9Cicros%D0%BEft.%D0%95ntit%D1%83Fram%D0%B5work%D0%A1%D0%BEr%D0%B5", purl2.toString());
}
@Test
void invalidPercentEncoding() {
assertThrowsExactly(
MalformedPackageURLException.class,
() -> new PackageURL("pkg:maven/com.google.summit/summit-ast@2.2.0%"));
assertThrowsExactly(
MalformedPackageURLException.class,
() -> new PackageURL("pkg:maven/com.google.summit/summit-ast@2.2.0%0"));
}
static Stream<Arguments> constructorParsing() throws IOException {
return PurlParameters.getTestDataFromFiles(
"test-suite-data.json", "custom-suite.json", "string-constructor-only.json");
}
@DisplayName("Test constructor parsing")
@ParameterizedTest(name = "{0}: ''{1}''")
@MethodSource
void constructorParsing(
String description,
@Nullable String purlString,
PurlParameters parameters,
@Nullable String canonicalPurl,
boolean invalid)
throws Exception {
if (invalid) {
assertThrows(
getExpectedException(purlString),
() -> new PackageURL(purlString),
"Build should fail due to " + description);
} else {
PackageURL purl = new PackageURL(purlString);
assertPurlEquals(parameters, purl);
assertEquals(canonicalPurl, purl.canonicalize(), "canonical PURL");
}
}
static Stream<Arguments> constructorParameters() throws IOException {
return PurlParameters.getTestDataFromFiles(
"test-suite-data.json", "custom-suite.json", "components-constructor-only.json");
}
@DisplayName("Test constructor parameters")
@ParameterizedTest(name = "{0}: {2}")
@MethodSource
void constructorParameters(
String description,
@Nullable String purlString,
PurlParameters parameters,
@Nullable String canonicalPurl,
boolean invalid)
throws Exception {
if (invalid) {
assertThrows(
getExpectedException(parameters),
() -> new PackageURL(
parameters.getType(),
parameters.getNamespace(),
parameters.getName(),
parameters.getVersion(),
parameters.getQualifiers(),
parameters.getSubpath()),
"Build should fail due to " + description);
} else {
PackageURL purl = new PackageURL(
parameters.getType(),
parameters.getNamespace(),
parameters.getName(),
parameters.getVersion(),
parameters.getQualifiers(),
parameters.getSubpath());
assertPurlEquals(parameters, purl);
assertEquals(canonicalPurl, purl.canonicalize(), "canonical PURL");
}
}
static Stream<Arguments> constructorTypeNameSpace() throws IOException {
return PurlParameters.getTestDataFromFiles("type-namespace-constructor-only.json");
}
@ParameterizedTest
@MethodSource
void constructorTypeNameSpace(
String description,
@Nullable String purlString,
PurlParameters parameters,
@Nullable String canonicalPurl,
boolean invalid)
throws Exception {
if (invalid) {
assertThrows(
getExpectedException(parameters), () -> new PackageURL(parameters.getType(), parameters.getName()));
} else {
PackageURL purl = new PackageURL(parameters.getType(), parameters.getName());
assertPurlEquals(parameters, purl);
assertEquals(canonicalPurl, purl.canonicalize(), "canonical PURL");
}
}
private static void assertPurlEquals(PurlParameters expected, PackageURL actual) {
assertEquals("pkg", actual.getScheme(), "scheme");
assertEquals(expected.getType(), actual.getType(), "type");
assertEquals(emptyToNull(expected.getNamespace()), actual.getNamespace(), "namespace");
assertEquals(expected.getName(), actual.getName(), "name");
assertEquals(emptyToNull(expected.getVersion()), actual.getVersion(), "version");
assertEquals(emptyToNull(expected.getSubpath()), actual.getSubpath(), "subpath");
assertNotNull(actual.getQualifiers(), "qualifiers");
assertEquals(actual.getQualifiers(), expected.getQualifiers(), "qualifiers");
}
private static Class<? extends Exception> getExpectedException(PurlParameters json) {
return json.getType() == null || json.getName() == null
? NullPointerException.class
: MalformedPackageURLException.class;
}
private static Class<? extends Exception> getExpectedException(@Nullable String purl) {
return purl == null ? NullPointerException.class : MalformedPackageURLException.class;
}
private static @Nullable String emptyToNull(@Nullable String value) {
return value == null || value.isEmpty() ? null : value;
}
@Test
void standardTypes() {
assertEquals("alpm", PackageURL.StandardTypes.ALPM);
assertEquals("apk", PackageURL.StandardTypes.APK);
assertEquals("bitbucket", PackageURL.StandardTypes.BITBUCKET);
assertEquals("bitnami", PackageURL.StandardTypes.BITNAMI);
assertEquals("cocoapods", PackageURL.StandardTypes.COCOAPODS);
assertEquals("cargo", PackageURL.StandardTypes.CARGO);
assertEquals("composer", PackageURL.StandardTypes.COMPOSER);
assertEquals("conan", PackageURL.StandardTypes.CONAN);
assertEquals("conda", PackageURL.StandardTypes.CONDA);
assertEquals("cpan", PackageURL.StandardTypes.CPAN);
assertEquals("cran", PackageURL.StandardTypes.CRAN);
assertEquals("deb", PackageURL.StandardTypes.DEB);
assertEquals("docker", PackageURL.StandardTypes.DOCKER);
assertEquals("gem", PackageURL.StandardTypes.GEM);
assertEquals("generic", PackageURL.StandardTypes.GENERIC);
assertEquals("github", PackageURL.StandardTypes.GITHUB);
assertEquals("golang", PackageURL.StandardTypes.GOLANG);
assertEquals("hackage", PackageURL.StandardTypes.HACKAGE);
assertEquals("hex", PackageURL.StandardTypes.HEX);
assertEquals("huggingface", PackageURL.StandardTypes.HUGGINGFACE);
assertEquals("luarocks", PackageURL.StandardTypes.LUAROCKS);
assertEquals("maven", PackageURL.StandardTypes.MAVEN);
assertEquals("mlflow", PackageURL.StandardTypes.MLFLOW);
assertEquals("npm", PackageURL.StandardTypes.NPM);
assertEquals("nuget", PackageURL.StandardTypes.NUGET);
assertEquals("qpkg", PackageURL.StandardTypes.QPKG);
assertEquals("oci", PackageURL.StandardTypes.OCI);
assertEquals("pub", PackageURL.StandardTypes.PUB);
assertEquals("pypi", PackageURL.StandardTypes.PYPI);
assertEquals("rpm", PackageURL.StandardTypes.RPM);
assertEquals("swid", PackageURL.StandardTypes.SWID);
assertEquals("swift", PackageURL.StandardTypes.SWIFT);
}
@Test
void coordinatesEquals() throws Exception {
PackageURL p1 = new PackageURL("pkg:generic/acme/example-component@1.0.0?key1=value1&key2=value2");
PackageURL p2 = new PackageURL("pkg:generic/acme/example-component@1.0.0");
assertTrue(p1.isCoordinatesEquals(p2));
}
@Test
void canonicalEquals() throws Exception {
PackageURL p1 = new PackageURL("pkg:generic/acme/example-component@1.0.0?key1=value1&key2=value2");
PackageURL p2 = new PackageURL("pkg:generic/acme/example-component@1.0.0?key2=value2&key1=value1");
assertTrue(p1.isCanonicalEquals(p2));
}
@Test
void getCoordinates() throws Exception {
PackageURL purl = new PackageURL("pkg:generic/acme/example-component@1.0.0?key1=value1&key2=value2");
assertEquals("pkg:generic/acme/example-component@1.0.0", purl.getCoordinates());
}
@Test
void getCoordinatesNoCacheIssue89() throws Exception {
PackageURL purl = new PackageURL("pkg:generic/acme/example-component@1.0.0?key1=value1&key2=value2");
purl.canonicalize();
assertEquals("pkg:generic/acme/example-component@1.0.0", purl.getCoordinates());
}
@Test
void npmCaseSensitive() throws Exception {
// e.g. https://www.npmjs.com/package/base64/v/1.0.0
PackageURL base64Lowercase = new PackageURL("pkg:npm/base64@1.0.0");
assertEquals("npm", base64Lowercase.getType());
assertEquals("base64", base64Lowercase.getName());
assertEquals("1.0.0", base64Lowercase.getVersion());
// e.g. https://www.npmjs.com/package/Base64/v/1.0.0
PackageURL base64Uppercase = new PackageURL("pkg:npm/Base64@1.0.0");
assertEquals("npm", base64Uppercase.getType());
assertEquals("Base64", base64Uppercase.getName());
assertEquals("1.0.0", base64Uppercase.getVersion());
}
@Test
void uriEncode() throws URISyntaxException, MalformedPackageURLException {
String genDelims = "?#[]@"; // /
String subDelims = "!$&'()*+,;=";
String pchar = "/" + genDelims + subDelims + ":";
String query = "key=" + pchar.replace("=", "%3D").replace("&", "%26") + "/?";
String fragment = pchar + "/?";
String scheme = "pkg";
String type = "generic";
String subpath = fragment.replaceFirst("^/+", "");
URI uri = new URI(scheme, type, pchar, query, subpath);
PackageURL purl = new PackageURL(uri.toASCIIString());
Map<String, String> qualifiers = Arrays.stream(query.split("&"))
.map(kv -> kv.split("="))
.filter(kvArray -> kvArray.length == 2)
.collect(Collectors.toMap(kv -> kv[0], kv -> kv[1]));
PackageURL purl2 = PackageURLBuilder.aPackageURL()
.withType(type)
.withNamespace("")
.withName(genDelims.replace("@", ""))
.withVersion(subDelims + ":")
.withQualifiers(qualifiers)
.withSubpath(subpath)
.build();
assertEquals(purl, purl2);
assertEquals(
uri.getQuery(),
purl.getQualifiers().entrySet().stream()
.map(Map.Entry::toString)
.collect(Collectors.joining("&")));
assertEquals(uri.getFragment(), purl.getSubpath());
assertEquals(uri.getPath(), "/" + purl.getName() + '@' + purl.getVersion());
assertEquals(uri.toASCIIString().replace("pkg://", "pkg:").replaceFirst("&", "%26"), purl.toString());
}
}