From b10e3fc1816cb026b2ca2588b5c4cb8f3522030f Mon Sep 17 00:00:00 2001 From: "Jose M. Arnesto" Date: Sun, 10 Dec 2023 21:54:45 +0100 Subject: [PATCH] Fix for #2930 and test cases 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. --- .../DefaultMavenRepositorySettings.java | 6 ++ .../DefaultRepositoryIdManager.java | 7 +- .../tycho/MavenRepositorySettings.java | 6 ++ .../settings-auth-mirror.xml | 18 ++++ .../settings-mirror.xml | 11 +++ .../PasswordProtectedP2RepositoryTest.java | 83 ++++++++++++++++++- 6 files changed, 124 insertions(+), 7 deletions(-) create mode 100644 tycho-its/projects/target.httpAuthentication/settings-auth-mirror.xml create mode 100644 tycho-its/projects/target.httpAuthentication/settings-mirror.xml diff --git a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/repository/DefaultMavenRepositorySettings.java b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/repository/DefaultMavenRepositorySettings.java index d25b658821..891205eeaa 100644 --- a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/repository/DefaultMavenRepositorySettings.java +++ b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/repository/DefaultMavenRepositorySettings.java @@ -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; @@ -158,4 +159,9 @@ public void initialize() throws InitializationException { mirrors = Collections.emptyList(); } } + + @Override + public Stream getMirrors() { + return mirrors.stream().map(m -> new MavenRepositoryLocation(m.getId(), URI.create(m.getUrl()))); + } } diff --git a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/repository/DefaultRepositoryIdManager.java b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/repository/DefaultRepositoryIdManager.java index 94c9e0cee8..88d23cb918 100644 --- a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/repository/DefaultRepositoryIdManager.java +++ b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/repository/DefaultRepositoryIdManager.java @@ -118,9 +118,10 @@ private static boolean certainlyNoRemoteURL(URI location) { @Override public Stream 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() { diff --git a/tycho-api/src/main/java/org/eclipse/tycho/MavenRepositorySettings.java b/tycho-api/src/main/java/org/eclipse/tycho/MavenRepositorySettings.java index 68286e8df3..a2f3a8c10a 100644 --- a/tycho-api/src/main/java/org/eclipse/tycho/MavenRepositorySettings.java +++ b/tycho-api/src/main/java/org/eclipse/tycho/MavenRepositorySettings.java @@ -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 @@ -60,4 +61,9 @@ public String toString() { */ Credentials getCredentials(MavenRepositoryLocation location); + /** + * Returns all configured mirror locations. + */ + Stream getMirrors(); + } diff --git a/tycho-its/projects/target.httpAuthentication/settings-auth-mirror.xml b/tycho-its/projects/target.httpAuthentication/settings-auth-mirror.xml new file mode 100644 index 0000000000..9c47af0398 --- /dev/null +++ b/tycho-its/projects/target.httpAuthentication/settings-auth-mirror.xml @@ -0,0 +1,18 @@ + + + + test-auth-mirror + mirror-user + mirror-password + + + + + test-auth-mirror + ${p2.authMirror} + test-server + p2 + p2 + + + diff --git a/tycho-its/projects/target.httpAuthentication/settings-mirror.xml b/tycho-its/projects/target.httpAuthentication/settings-mirror.xml new file mode 100644 index 0000000000..170f31c7f5 --- /dev/null +++ b/tycho-its/projects/target.httpAuthentication/settings-mirror.xml @@ -0,0 +1,11 @@ + + + + test-mirror + ${p2.mirror} + test-server + p2 + p2 + + + diff --git a/tycho-its/src/test/java/org/eclipse/tycho/test/target/PasswordProtectedP2RepositoryTest.java b/tycho-its/src/test/java/org/eclipse/tycho/test/target/PasswordProtectedP2RepositoryTest.java index 2479da3911..41a0e2fd07 100644 --- a/tycho-its/src/test/java/org/eclipse/tycho/test/target/PasswordProtectedP2RepositoryTest.java +++ b/tycho-its/src/test/java/org/eclipse/tycho/test/target/PasswordProtectedP2RepositoryTest.java @@ -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; @@ -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"); @@ -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"); @@ -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; } + }