Skip to content

Commit

Permalink
Fix for #2930 and test cases
Browse files Browse the repository at this point in the history
Mirrors are now included in the returned list of known repo locations.
Also, these test cases have been added:

- testMirror: the test server is accessed over a mirror without
authentication.

- testAuthMirror: the test server is accessed over a mirror with
authentication.

- testTargetDefinitionMirror: this one tries to resolve a target
definition from a p2 repo accessed over a mirror with no authentication.

- testTargetDefinitionAuthMirror: this one tries to resolve a target
definition from a p2 repo accessed over an authenticated mirror. This is
the one that actually reproduced the bug that has been fixed here.
  • Loading branch information
kevloral committed Jan 21, 2024
1 parent a350281 commit b10e3fc
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
Expand Down Expand Up @@ -158,4 +159,9 @@ public void initialize() throws InitializationException {
mirrors = Collections.emptyList();
}
}

@Override
public Stream<MavenRepositoryLocation> getMirrors() {
return mirrors.stream().map(m -> new MavenRepositoryLocation(m.getId(), URI.create(m.getUrl())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,10 @@ private static boolean certainlyNoRemoteURL(URI location) {

@Override
public Stream<MavenRepositoryLocation> getKnownMavenRepositoryLocations() {
return knownMavenRepositoryIds.entrySet().stream()
.map(e -> new MavenRepositoryLocation(e.getValue(), e.getKey()));
}
// Returns both repository and mirror locations
return Stream.concat(knownMavenRepositoryIds.entrySet().stream()
.map(e -> new MavenRepositoryLocation(e.getValue(), e.getKey())), settings.getMirrors());
}

@Override
public MavenRepositorySettings getSettings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.tycho;

import java.net.URI;
import java.util.stream.Stream;

/**
* Provides the mirror configuration and credentials from the Maven settings for loading remote p2
Expand Down Expand Up @@ -60,4 +61,9 @@ public String toString() {
*/
Credentials getCredentials(MavenRepositoryLocation location);

/**
* Returns all configured mirror locations.
*/
Stream<MavenRepositoryLocation> getMirrors();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<settings>
<servers>
<server>
<id>test-auth-mirror</id>
<username>mirror-user</username>
<password>mirror-password</password>
</server>
</servers>
<mirrors>
<mirror>
<id>test-auth-mirror</id>
<url>${p2.authMirror}</url>
<mirrorOf>test-server</mirrorOf>
<layout>p2</layout>
<mirrorOfLayouts>p2</mirrorOfLayouts>
</mirror>
</mirrors>
</settings>
11 changes: 11 additions & 0 deletions tycho-its/projects/target.httpAuthentication/settings-mirror.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<settings>
<mirrors>
<mirror>
<id>test-mirror</id>
<url>${p2.mirror}</url>
<mirrorOf>test-server</mirrorOf>
<layout>p2</layout>
<mirrorOfLayouts>p2</mirrorOfLayouts>
</mirror>
</mirrors>
</settings>
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
package org.eclipse.tycho.test.target;

import java.io.File;
import java.util.Properties;

import org.apache.maven.it.Verifier;
import org.eclipse.tycho.test.AbstractTychoIntegrationTest;
Expand All @@ -29,17 +28,59 @@ public class PasswordProtectedP2RepositoryTest extends AbstractTychoIntegrationT
private HttpServer server;
private String p2RepoUrl;

private HttpServer mirror;
private String p2MirrorUrl;

private HttpServer authMirror;
private String p2AuthMirrorUrl;

@Before
public void startServer() throws Exception {
server = HttpServer.startServer("test-user", "test-password");
p2RepoUrl = server.addServer("foo", ResourceUtil.resolveTestResource("repositories/e342"));

mirror = HttpServer.startServer();
p2MirrorUrl = mirror.addServer("bar", ResourceUtil.resolveTestResource("repositories/e342"));

authMirror = HttpServer.startServer("mirror-user", "mirror-password");
p2AuthMirrorUrl = authMirror.addServer("bar", ResourceUtil.resolveTestResource("repositories/e342"));
}

@After
public void stopServer() throws Exception {
authMirror.stop();
mirror.stop();
server.stop();
}

/**
* Tries to access a p2 repository over an authenticated mirror.
*
* @throws Exception
*/
@Test
public void testAuthMirror() throws Exception {
Verifier verifier = createVerifier("settings-auth-mirror.xml");
verifier.setSystemProperty("p2.authMirror", p2AuthMirrorUrl);
verifier.addCliOption("-P=repository");
verifier.executeGoal("package");
verifier.verifyErrorFreeLog();
}

/**
* Tries to access a p2 repository over a mirror with no authentication.
*
* @throws Exception
*/
@Test
public void testMirror() throws Exception {
Verifier verifier = createVerifier("settings-mirror.xml");
verifier.setSystemProperty("p2.mirror", p2MirrorUrl);
verifier.addCliOption("-P=repository");
verifier.executeGoal("package");
verifier.verifyErrorFreeLog();
}

@Test
public void testRepository() throws Exception {
Verifier verifier = createVerifier("settings.xml");
Expand All @@ -66,6 +107,40 @@ public void testTargetDefinition() throws Exception {
verifier.verifyErrorFreeLog();
}

/**
* Tries to resolve a target definition from a p2 repository accessed over a
* mirror with no authentication.
*
* @throws Exception
*/
@Test
public void testTargetDefinitionMirror() throws Exception {
Verifier verifier = createVerifier("settings-mirror.xml");
File platformFile = new File(verifier.getBasedir(), "platform.target");
TargetDefinitionUtil.setRepositoryURLs(platformFile, p2RepoUrl);
verifier.setSystemProperty("p2.mirror", p2MirrorUrl);
verifier.addCliOption("-P=target-definition");
verifier.executeGoal("package");
verifier.verifyErrorFreeLog();
}

/**
* Tries to resolve a target definition from a p2 repository accessed over an
* authenticated mirror.
*
* @throws Exception
*/
@Test
public void testTargetDefinitionAuthMirror() throws Exception {
Verifier verifier = createVerifier("settings-auth-mirror.xml");
File platformFile = new File(verifier.getBasedir(), "platform.target");
TargetDefinitionUtil.setRepositoryURLs(platformFile, p2RepoUrl);
verifier.setSystemProperty("p2.authMirror", p2AuthMirrorUrl);
verifier.addCliOption("-P=target-definition");
verifier.executeGoal("package");
verifier.verifyErrorFreeLog();
}

@Test
public void testTargetDefinitionEncrypted() throws Exception {
Verifier verifier = createVerifier("settings-encrypted.xml", "settings-security.xml");
Expand All @@ -83,14 +158,14 @@ private Verifier createVerifier(String settingsFile) throws Exception {
private Verifier createVerifier(String settingsFile, String settingsSecurityFile) throws Exception {
Verifier verifier = getVerifier("target.httpAuthentication", false,
new File("projects/target.httpAuthentication/" + settingsFile));
Properties systemProperties = verifier.getSystemProperties();
systemProperties.setProperty("p2.repo", p2RepoUrl);
verifier.setSystemProperty("p2.repo", p2RepoUrl);
if (settingsSecurityFile != null) {
// see
// org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher#SYSTEM_PROPERTY_SEC_LOCATION
systemProperties.setProperty("settings.security",
verifier.setSystemProperty("settings.security",
new File("projects/target.httpAuthentication/" + settingsSecurityFile).getAbsolutePath());
}
return verifier;
}

}

0 comments on commit b10e3fc

Please sign in to comment.