1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.internal.impl;
20
21 import org.eclipse.aether.artifact.DefaultArtifact;
22 import org.eclipse.aether.metadata.DefaultMetadata;
23 import org.eclipse.aether.metadata.Metadata;
24 import org.junit.jupiter.api.Test;
25
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertThrows;
28
29 class DefaultLocalPathComposerTest {
30
31 private final DefaultLocalPathComposer composer = new DefaultLocalPathComposer();
32
33 @Test
34 void testGetPathForArtifact() {
35 String path = composer.getPathForArtifact(new DefaultArtifact("g.id", "a-id", "jar", "1.0"), true);
36 assertEquals("g/id/a-id/1.0/a-id-1.0.jar", path);
37 }
38
39 @Test
40 void testGetPathForMetadata() {
41 Metadata metadata = new DefaultMetadata("g.id", "a-id", "1.0", "maven-metadata.xml", Metadata.Nature.RELEASE);
42 String path = composer.getPathForMetadata(metadata, "central");
43 assertEquals("g/id/a-id/1.0/maven-metadata-central.xml", path);
44 }
45
46 @Test
47 void testGetPathForMetadataRejectsTraversalInVersion() {
48 Metadata metadata = new DefaultMetadata(
49 "g.id", "a-id", "../../../../tmp/PWNED-SNAPSHOT", "maven-metadata.xml", Metadata.Nature.RELEASE);
50 assertThrows(IllegalArgumentException.class, () -> composer.getPathForMetadata(metadata, "central"));
51 }
52
53 @Test
54 void testGetPathForArtifactRejectsTraversalInVersion() {
55 assertThrows(
56 IllegalArgumentException.class,
57 () -> composer.getPathForArtifact(new DefaultArtifact("g.id", "a-id", "jar", "../../escape"), true));
58 }
59
60 @Test
61 void testGetPathForMetadataRejectsSlashInVersion() {
62 Metadata metadata =
63 new DefaultMetadata("g.id", "a-id", "1.0/../../etc", "maven-metadata.xml", Metadata.Nature.RELEASE);
64 assertThrows(IllegalArgumentException.class, () -> composer.getPathForMetadata(metadata, "central"));
65 }
66
67 @Test
68 void testGetPathForArtifactRejectsBackslashInVersion() {
69 assertThrows(
70 IllegalArgumentException.class,
71 () -> composer.getPathForArtifact(new DefaultArtifact("g.id", "a-id", "jar", "1.0\\..\\escape"), true));
72 }
73
74 @Test
75 void testGetPathForMetadataAcceptsNormalVersions() {
76
77 Metadata metadata =
78 new DefaultMetadata("g.id", "a-id", "1.0.0-SNAPSHOT", "maven-metadata.xml", Metadata.Nature.SNAPSHOT);
79 String path = composer.getPathForMetadata(metadata, "central");
80 assertEquals("g/id/a-id/1.0.0-SNAPSHOT/maven-metadata-central.xml", path);
81 }
82
83 @Test
84 void testGetPathForArtifactRejectsLeadingDotGroupId() {
85 assertThrows(
86 IllegalArgumentException.class,
87 () -> composer.getPathForArtifact(new DefaultArtifact(".leading", "a-id", "jar", "1.0"), true));
88 }
89
90 @Test
91 void testGetPathForArtifactRejectsDoubleLeadingDotGroupId() {
92 assertThrows(
93 IllegalArgumentException.class,
94 () -> composer.getPathForArtifact(new DefaultArtifact("..leading", "a-id", "jar", "1.0"), true));
95 }
96
97 @Test
98 void testGetPathForArtifactRejectsConsecutiveDotsInGroupId() {
99 assertThrows(
100 IllegalArgumentException.class,
101 () -> composer.getPathForArtifact(new DefaultArtifact("g..id", "a-id", "jar", "1.0"), true));
102 }
103
104 @Test
105 void testGetPathForArtifactRejectsTrailingDotInGroupId() {
106 assertThrows(
107 IllegalArgumentException.class,
108 () -> composer.getPathForArtifact(new DefaultArtifact("g.id.", "a-id", "jar", "1.0"), true));
109 }
110
111 @Test
112 void testGetPathForArtifactRejectsColon() {
113 assertThrows(
114 IllegalArgumentException.class,
115 () -> composer.getPathForArtifact(new DefaultArtifact("g:id", "a-id", "jar", "1.0"), true));
116 assertThrows(
117 IllegalArgumentException.class,
118 () -> composer.getPathForArtifact(new DefaultArtifact("g.id", "a-id", "jar", "1.0:1"), true));
119 }
120
121 @Test
122 void testGetPathForMetadataRejectsLeadingDotGroupId() {
123 Metadata metadata =
124 new DefaultMetadata(".leading", "a-id", "1.0", "maven-metadata.xml", Metadata.Nature.RELEASE);
125 assertThrows(IllegalArgumentException.class, () -> composer.getPathForMetadata(metadata, "central"));
126 }
127
128 @Test
129 void testGetPathForArtifactAcceptsNormalCoordinates() {
130 String path = composer.getPathForArtifact(
131 new DefaultArtifact("org.apache.maven", "commons-io", "jar", "1.0-SNAPSHOT"), true);
132 assertEquals("org/apache/maven/commons-io/1.0-SNAPSHOT/commons-io-1.0-SNAPSHOT.jar", path);
133 }
134 }