Skip to content

Commit

Permalink
Fix for eclipse-tycho#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 Dec 31, 2023
1 parent a350281 commit df34368
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public class DefaultRepositoryIdManager implements IRepositoryIdManager {

private Map<URI, String> knownMavenRepositoryIds = new ConcurrentHashMap<>();

/**
* Map of known mirror identifiers, indexed by their URI.
*/
private Map<URI, String> knownMavenMirrorIds = new ConcurrentHashMap<>();

@Override
public void addMapping(String mavenRepositoryId, URI location) {
if (mavenRepositoryId == null)
Expand All @@ -52,6 +57,15 @@ public void addMapping(String mavenRepositoryId, URI location) {
logger.warn("p2 repository with URL " + key + " is associated with multiple IDs; was '" + previousValue
+ "', now is '" + mavenRepositoryId + "'");
}

// Checks if a mirror exists for the given location
MavenRepositoryLocation repositoryLocation = new MavenRepositoryLocation(mavenRepositoryId, key);
MavenRepositoryLocation mirrorLocation = settings.getMirror(repositoryLocation);
if (mirrorLocation != null) {
// A mirror exists. Adds its id to the map of known mirror ids.
URI mirrorKey = normalize(mirrorLocation.getURL());
knownMavenMirrorIds.put(mirrorKey, mirrorLocation.getId());
}
}

private static URI normalize(URI location) {
Expand Down Expand Up @@ -118,8 +132,8 @@ private static boolean certainlyNoRemoteURL(URI location) {

@Override
public Stream<MavenRepositoryLocation> getKnownMavenRepositoryLocations() {
return knownMavenRepositoryIds.entrySet().stream()
.map(e -> new MavenRepositoryLocation(e.getValue(), e.getKey()));
return Stream.concat(knownMavenRepositoryIds.entrySet().stream(), knownMavenMirrorIds.entrySet().stream())
.map(e -> new MavenRepositoryLocation(e.getValue(), e.getKey()));
}

@Override
Expand Down
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 @@ -29,17 +29,47 @@ 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();
}

@Test
public void testAuthMirror() throws Exception {
Verifier verifier = createVerifier("settings-auth-mirror.xml");
verifier.addCliOption("-P=repository");
verifier.executeGoal("package");
verifier.verifyErrorFreeLog();
}

@Test
public void testMirror() throws Exception {
Verifier verifier = createVerifier("settings-mirror.xml");
verifier.addCliOption("-P=repository");
verifier.executeGoal("package");
verifier.verifyErrorFreeLog();
}

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

@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.addCliOption("-P=target-definition");
verifier.executeGoal("package");
verifier.verifyErrorFreeLog();
}

@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.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 @@ -85,6 +135,8 @@ private Verifier createVerifier(String settingsFile, String settingsSecurityFile
new File("projects/target.httpAuthentication/" + settingsFile));
Properties systemProperties = verifier.getSystemProperties();
systemProperties.setProperty("p2.repo", p2RepoUrl);
systemProperties.setProperty("p2.mirror", p2MirrorUrl);
systemProperties.setProperty("p2.authMirror", p2AuthMirrorUrl);
if (settingsSecurityFile != null) {
// see
// org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher#SYSTEM_PROPERTY_SEC_LOCATION
Expand All @@ -93,4 +145,5 @@ private Verifier createVerifier(String settingsFile, String settingsSecurityFile
}
return verifier;
}

}

0 comments on commit df34368

Please sign in to comment.