|
24 | 24 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
25 | 25 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
26 | 26 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
27 |
| -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; |
28 | 27 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
| 28 | +import static org.junit.jupiter.api.Assertions.fail; |
29 | 29 |
|
30 | 30 | import java.io.IOException;
|
31 | 31 | import java.util.Locale;
|
@@ -60,37 +60,6 @@ static void resetLocale() {
|
60 | 60 | Locale.setDefault(DEFAULT_LOCALE);
|
61 | 61 | }
|
62 | 62 |
|
63 |
| - @Test |
64 |
| - void validPercentEncoding() throws MalformedPackageURLException { |
65 |
| - PackageURL purl = new PackageURL("maven", "com.google.summit", "summit-ast", "2.2.0\n", null, null); |
66 |
| - assertEquals( "pkg:maven/com.google.summit/[email protected]%0A", purl. toString()); |
67 |
| - PackageURL purl2 = |
68 |
| - new PackageURL("pkg:nuget/%D0%9Cicros%D0%BEft.%D0%95ntit%D1%83Fram%D0%B5work%D0%A1%D0%BEr%D0%B5"); |
69 |
| - assertEquals("Мicrosоft.ЕntitуFramеworkСоrе", purl2.getName()); |
70 |
| - assertEquals( |
71 |
| - "pkg:nuget/%D0%9Cicros%D0%BEft.%D0%95ntit%D1%83Fram%D0%B5work%D0%A1%D0%BEr%D0%B5", purl2.toString()); |
72 |
| - } |
73 |
| - |
74 |
| - @SuppressWarnings("deprecation") |
75 |
| - @Test |
76 |
| - void invalidPercentEncoding() throws MalformedPackageURLException { |
77 |
| - assertThrowsExactly( |
78 |
| - MalformedPackageURLException.class, |
79 |
| - () -> new PackageURL( "pkg:maven/com.google.summit/[email protected]%")); |
80 |
| - assertThrowsExactly( |
81 |
| - MalformedPackageURLException.class, |
82 |
| - () -> new PackageURL( "pkg:maven/com.google.summit/[email protected]%0")); |
83 |
| - PackageURL purl = new PackageURL( "pkg:maven/com.google.summit/[email protected]"); |
84 |
| - Throwable t1 = assertThrowsExactly(ValidationException.class, () -> purl.uriDecode("%")); |
85 |
| - assertEquals("Incomplete percent encoding at offset 0 with value '%'", t1.getMessage()); |
86 |
| - Throwable t2 = assertThrowsExactly(ValidationException.class, () -> purl.uriDecode("a%0")); |
87 |
| - assertEquals("Incomplete percent encoding at offset 1 with value '%0'", t2.getMessage()); |
88 |
| - Throwable t3 = assertThrowsExactly(ValidationException.class, () -> purl.uriDecode("aaaa%%0A")); |
89 |
| - assertEquals("Invalid percent encoding char 1 at offset 5 with value '%'", t3.getMessage()); |
90 |
| - Throwable t4 = assertThrowsExactly(ValidationException.class, () -> purl.uriDecode("%0G")); |
91 |
| - assertEquals("Invalid percent encoding char 2 at offset 2 with value 'G'", t4.getMessage()); |
92 |
| - } |
93 |
| - |
94 | 63 | static Stream<Arguments> constructorParsing() throws IOException {
|
95 | 64 | return PurlParameters.getTestDataFromFiles(
|
96 | 65 | "test-suite-data.json", "custom-suite.json", "string-constructor-only.json");
|
@@ -131,15 +100,26 @@ void constructorParameters(
|
131 | 100 | boolean invalid)
|
132 | 101 | throws Exception {
|
133 | 102 | if (invalid) {
|
134 |
| - assertThrows( |
135 |
| - getExpectedException(parameters), |
136 |
| - () -> new PackageURL( |
137 |
| - parameters.getType(), |
138 |
| - parameters.getNamespace(), |
139 |
| - parameters.getName(), |
140 |
| - parameters.getVersion(), |
141 |
| - parameters.getQualifiers(), |
142 |
| - parameters.getSubpath())); |
| 103 | + try { |
| 104 | + PackageURL purl = new PackageURL( |
| 105 | + parameters.getType(), |
| 106 | + parameters.getNamespace(), |
| 107 | + parameters.getName(), |
| 108 | + parameters.getVersion(), |
| 109 | + parameters.getQualifiers(), |
| 110 | + parameters.getSubpath()); |
| 111 | + // If we get here, then only the scheme can be invalid |
| 112 | + assertPurlEquals(parameters, purl); |
| 113 | + |
| 114 | + if (canonicalPurl != null && !canonicalPurl.equals(purl.toString())) { |
| 115 | + throw new MalformedPackageURLException("The PackageURL scheme is invalid for purl: " + purl); |
| 116 | + } |
| 117 | + |
| 118 | + fail("Invalid package url components of '" + purl + "' should have caused an exception because " |
| 119 | + + description); |
| 120 | + } catch (Exception e) { |
| 121 | + assertEquals(e.getClass(), getExpectedException(parameters)); |
| 122 | + } |
143 | 123 | } else {
|
144 | 124 | PackageURL purl = new PackageURL(
|
145 | 125 | parameters.getType(),
|
@@ -182,7 +162,8 @@ private static void assertPurlEquals(PurlParameters expected, PackageURL actual)
|
182 | 162 | assertEquals(emptyToNull(expected.getNamespace()), actual.getNamespace(), "namespace");
|
183 | 163 | assertEquals(expected.getName(), actual.getName(), "name");
|
184 | 164 | assertEquals(emptyToNull(expected.getVersion()), actual.getVersion(), "version");
|
185 |
| - assertEquals(emptyToNull(expected.getSubpath()), actual.getSubpath(), "subpath"); |
| 165 | + // XXX: Can't assume canonical fields are equal to the test fields |
| 166 | + // assertEquals(emptyToNull(expected.getSubpath()), actual.getSubpath(), "subpath"); |
186 | 167 | assertNotNull(actual.getQualifiers(), "qualifiers");
|
187 | 168 | assertEquals(actual.getQualifiers(), expected.getQualifiers(), "qualifiers");
|
188 | 169 | }
|
@@ -233,6 +214,19 @@ void standardTypes() {
|
233 | 214 | assertEquals("pub", PackageURL.StandardTypes.PUB);
|
234 | 215 | assertEquals("pypi", PackageURL.StandardTypes.PYPI);
|
235 | 216 | assertEquals("rpm", PackageURL.StandardTypes.RPM);
|
| 217 | + assertEquals("hackage", PackageURL.StandardTypes.HACKAGE); |
| 218 | + assertEquals("hex", PackageURL.StandardTypes.HEX); |
| 219 | + assertEquals("huggingface", PackageURL.StandardTypes.HUGGINGFACE); |
| 220 | + assertEquals("luarocks", PackageURL.StandardTypes.LUAROCKS); |
| 221 | + assertEquals("maven", PackageURL.StandardTypes.MAVEN); |
| 222 | + assertEquals("mlflow", PackageURL.StandardTypes.MLFLOW); |
| 223 | + assertEquals("npm", PackageURL.StandardTypes.NPM); |
| 224 | + assertEquals("nuget", PackageURL.StandardTypes.NUGET); |
| 225 | + assertEquals("qpkg", PackageURL.StandardTypes.QPKG); |
| 226 | + assertEquals("oci", PackageURL.StandardTypes.OCI); |
| 227 | + assertEquals("pub", PackageURL.StandardTypes.PUB); |
| 228 | + assertEquals("pypi", PackageURL.StandardTypes.PYPI); |
| 229 | + assertEquals("rpm", PackageURL.StandardTypes.RPM); |
236 | 230 | assertEquals("swid", PackageURL.StandardTypes.SWID);
|
237 | 231 | assertEquals("swift", PackageURL.StandardTypes.SWIFT);
|
238 | 232 | }
|
|
0 commit comments