View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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          // Verify that normal version strings with dots and dashes are still accepted
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 }