| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one | |
| * or more contributor license agreements. See the NOTICE file | |
| * distributed with this work for additional information | |
| * regarding copyright ownership. The ASF licenses this file | |
| * to you under the Apache License, Version 2.0 (the | |
| * "License"); you may not use this file except in compliance | |
| * with the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, | |
| * software distributed under the License is distributed on an | |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| * KIND, either express or implied. See the License for the | |
| * specific language governing permissions and limitations | |
| * under the License. | |
| */ | |
| package org.apache.maven.plugins.dependency; | |
| import javax.inject.Inject; | |
| import java.io.IOException; | |
| import java.io.OutputStream; | |
| import java.net.InetAddress; | |
| import java.net.InetSocketAddress; | |
| import java.net.URI; | |
| import java.nio.file.Files; | |
| import java.nio.file.Path; | |
| import java.nio.file.Paths; | |
| import java.util.Arrays; | |
| import java.util.Collections; | |
| import java.util.List; | |
| import com.sun.net.httpserver.BasicAuthenticator; | |
| import com.sun.net.httpserver.HttpContext; | |
| import com.sun.net.httpserver.HttpExchange; | |
| import com.sun.net.httpserver.HttpServer; | |
| import org.apache.maven.api.plugin.testing.Basedir; | |
| import org.apache.maven.api.plugin.testing.InjectMojo; | |
| import org.apache.maven.api.plugin.testing.MojoParameter; | |
| import org.apache.maven.api.plugin.testing.MojoTest; | |
| import org.apache.maven.execution.MavenSession; | |
| import org.apache.maven.plugin.MojoExecutionException; | |
| import org.apache.maven.plugin.MojoFailureException; | |
| import org.apache.maven.plugins.dependency.utils.ResolverUtil; | |
| import org.apache.maven.settings.Proxy; | |
| import org.apache.maven.settings.Server; | |
| import org.apache.maven.settings.Settings; | |
| import org.eclipse.aether.DefaultRepositorySystemSession; | |
| import org.eclipse.aether.RepositorySystem; | |
| import org.eclipse.aether.repository.LocalRepository; | |
| import org.eclipse.aether.repository.RepositoryPolicy; | |
| import org.eclipse.aether.util.repository.AuthenticationBuilder; | |
| import org.eclipse.aether.util.repository.DefaultAuthenticationSelector; | |
| import org.eclipse.aether.util.repository.DefaultProxySelector; | |
| import org.junit.jupiter.api.BeforeEach; | |
| import org.junit.jupiter.api.Test; | |
| import org.junit.jupiter.api.io.TempDir; | |
| import static org.apache.maven.api.plugin.testing.MojoExtension.getTestPath; | |
| import static org.apache.maven.api.plugin.testing.MojoExtension.getVariableValueFromObject; | |
| import static org.apache.maven.api.plugin.testing.MojoExtension.setVariableValueToObject; | |
| import static org.junit.jupiter.api.Assertions.assertEquals; | |
| import static org.junit.jupiter.api.Assertions.assertThrows; | |
| import static org.junit.jupiter.api.Assertions.assertTrue; | |
| import static org.mockito.ArgumentMatchers.anyList; | |
| import static org.mockito.ArgumentMatchers.eq; | |
| import static org.mockito.Mockito.mock; | |
| import static org.mockito.Mockito.verify; | |
| import static org.mockito.Mockito.when; | |
| @MojoTest(realRepositorySession = true) | |
| @Basedir("/unit/get-test") | |
| class TestGetMojo { | |
| private static final String PROXY_HOST = "proxy.invalid"; | |
| @Inject | |
| private MavenSession session; | |
| @Inject | |
| private RepositorySystem repositorySystem; | |
| @TempDir | |
| private Path isolatedLocalRepository; | |
| @BeforeEach | |
| void setUp() { | |
| Settings settings = new Settings(); | |
| when(session.getSettings()).thenReturn(settings); | |
| Server server = new Server(); | |
| server.setId("myserver"); | |
| server.setUsername("foo"); | |
| server.setPassword("bar"); | |
| settings.addServer(server); | |
| } | |
| /** | |
| * Mirrors the {@code <server>} and {@code <proxy>} entries of the session settings into the repository session, | |
| * the way {@code DefaultRepositorySystemSessionFactory} does when Maven builds that session for real. The | |
| * repositories named by the {@code remoteRepositories} parameter get their credentials and proxies from those | |
| * selectors, by way of {@code RepositorySystem.newResolutionRepositories}, and the test harness has already | |
| * created the repository session by the time {@link #setUp()} declares any of this. | |
| * <p> | |
| * The local repository is swapped for an empty one at the same time, so that the tests below depend on the | |
| * transfer actually happening rather than on what an earlier test left in the shared local repository. | |
| */ | |
| private void applySettingsToRepositorySession() { | |
| Settings settings = session.getSettings(); | |
| DefaultAuthenticationSelector authenticationSelector = new DefaultAuthenticationSelector(); | |
| for (Server server : settings.getServers()) { | |
| authenticationSelector.add( | |
| server.getId(), | |
| new AuthenticationBuilder() | |
| .addUsername(server.getUsername()) | |
| .addPassword(server.getPassword()) | |
| .addPrivateKey(server.getPrivateKey(), server.getPassphrase()) | |
| .build()); | |
| } | |
| DefaultProxySelector proxySelector = new DefaultProxySelector(); | |
| for (Proxy proxy : settings.getProxies()) { | |
| if (!proxy.isActive()) { | |
| // Maven filters the inactive ones out before they ever reach the selector | |
| continue; | |
| } | |
| proxySelector.add( | |
| new org.eclipse.aether.repository.Proxy( | |
| proxy.getProtocol(), | |
| proxy.getHost(), | |
| proxy.getPort(), | |
| new AuthenticationBuilder() | |
| .addUsername(proxy.getUsername()) | |
| .addPassword(proxy.getPassword()) | |
| .build()), | |
| proxy.getNonProxyHosts()); | |
| } | |
| DefaultRepositorySystemSession repositorySession = | |
| new DefaultRepositorySystemSession(session.getRepositorySession()); | |
| repositorySession.setAuthenticationSelector(authenticationSelector); | |
| repositorySession.setProxySelector(proxySelector); | |
| repositorySession.setLocalRepositoryManager(repositorySystem.newLocalRepositoryManager( | |
| repositorySession, new LocalRepository(isolatedLocalRepository.toFile()))); | |
| when(session.getRepositorySession()).thenReturn(repositorySession); | |
| } | |
| /** | |
| * Test transitive parameter | |
| * | |
| * @throws Exception in case of errors | |
| */ | |
| @Test | |
| @InjectMojo(goal = "get") | |
| @MojoParameter(name = "transitive", value = "false") | |
| void testTransitive(GetMojo mojo) throws Exception { | |
| mojo.setGroupId("org.apache.maven"); | |
| mojo.setArtifactId("maven-model"); | |
| mojo.setVersion("2.0.9"); | |
| mojo.execute(); | |
| } | |
| /** | |
| * Test remote repositories parameter | |
| * | |
| * @throws Exception in case of errors | |
| */ | |
| @Test | |
| @InjectMojo(goal = "get") | |
| @MojoParameter( | |
| name = "remoteRepositories", | |
| value = "central::default::https://repo.maven.apache.org/maven2," | |
| + "central::::https://repo.maven.apache.org/maven2," | |
| + "https://repo.maven.apache.org/maven2") | |
| void testRemoteRepositories(GetMojo mojo) throws Exception { | |
| // the parameter is a List<String>, and a comma-separated value must arrive as one element per | |
| // repository -- not as a single element still containing the commas | |
| List<String> repositories = getVariableValueFromObject(mojo, "remoteRepositories"); | |
| assertEquals( | |
| Arrays.asList( | |
| "central::default::https://repo.maven.apache.org/maven2", | |
| "central::::https://repo.maven.apache.org/maven2", | |
| "https://repo.maven.apache.org/maven2"), | |
| repositories); | |
| mojo.setGroupId("org.apache.maven"); | |
| mojo.setArtifactId("maven-model"); | |
| mojo.setVersion("2.0.9"); | |
| mojo.execute(); | |
| } | |
| @Test | |
| void testExplicitRemoteRepositoriesAlwaysRefresh() throws Exception { | |
| ResolverUtil resolverUtil = mock(ResolverUtil.class); | |
| when(resolverUtil.remoteRepositories(anyList(), eq(RepositoryPolicy.UPDATE_POLICY_ALWAYS))) | |
| .thenReturn(Collections.emptyList()); | |
| GetMojo mojo = new GetMojo(resolverUtil); | |
| setRemoteRepositories(mojo, "central::default::https://repo.maven.apache.org/maven2"); | |
| mojo.setGroupId("org.apache.maven"); | |
| mojo.setArtifactId("maven-model"); | |
| mojo.setVersion("2.0.9"); | |
| mojo.execute(); | |
| verify(resolverUtil) | |
| .remoteRepositories( | |
| Collections.singletonList("central::default::https://repo.maven.apache.org/maven2"), | |
| RepositoryPolicy.UPDATE_POLICY_ALWAYS); | |
| } | |
| /** | |
| * Test that neither an artifact nor a complete GAV is a failure rather than an attempted resolution. | |
| */ | |
| @Test | |
| @InjectMojo(goal = "get") | |
| void testMissingArtifact(GetMojo mojo) { | |
| assertThrows(MojoFailureException.class, mojo::execute); | |
| } | |
| /** | |
| * Test that a malformed <code>artifact</code> parameter is reported as a failure. | |
| */ | |
| @Test | |
| @InjectMojo(goal = "get") | |
| @MojoParameter(name = "artifact", value = "org.apache.maven:maven-model") | |
| void testInvalidArtifact(GetMojo mojo) { | |
| assertThrows(MojoFailureException.class, mojo::execute); | |
| } | |
| /** | |
| * Test remote repositories parameter with basic authentication | |
| * | |
| * @throws Exception in case of errors | |
| */ | |
| @Test | |
| @InjectMojo(goal = "get") | |
| void testRemoteRepositoriesAuthentication(GetMojo mojo) throws Exception { | |
| HttpServer server = createServer(); | |
| try { | |
| setRemoteRepositories(mojo, "myserver::default::" + serverUrl(server)); | |
| applySettingsToRepositorySession(); | |
| mojo.setGroupId("test"); | |
| mojo.setArtifactId("test"); | |
| mojo.setVersion("1.0"); | |
| mojo.execute(); | |
| } finally { | |
| server.stop(0); | |
| } | |
| } | |
| /** | |
| * Test that an active proxy from the settings is applied to the repositories given by the | |
| * <code>remoteRepositories</code> parameter. The proxy points at a host that cannot resolve, so a successful | |
| * resolution would mean the proxy was never applied. | |
| * | |
| * @throws Exception in case of errors | |
| */ | |
| @Test | |
| @InjectMojo(goal = "get") | |
| void testRemoteRepositoriesProxy(GetMojo mojo) throws Exception { | |
| HttpServer server = createServer(); | |
| try { | |
| setRemoteRepositories(mojo, "myserver::default::" + serverUrl(server)); | |
| session.getSettings().addProxy(createProxy(null)); | |
| applySettingsToRepositorySession(); | |
| mojo.setGroupId("test"); | |
| mojo.setArtifactId("test"); | |
| mojo.setVersion("1.0"); | |
| MojoExecutionException e = assertThrows(MojoExecutionException.class, mojo::execute); | |
| assertTrue( | |
| mentionsProxyHost(e), | |
| "Expected the resolution to have been attempted through the unreachable proxy, got: " + e); | |
| } finally { | |
| server.stop(0); | |
| } | |
| } | |
| /** | |
| * Test that <code>nonProxyHosts</code> excludes a repository from the proxy that would otherwise match it: the | |
| * same unreachable proxy as above must not be applied, so the resolution has to succeed. | |
| * <p> | |
| * This one asserts a success, so on its own it cannot tell "nonProxyHosts was honoured" apart from "proxies are | |
| * not applied at all". It is only meaningful together with {@link #testRemoteRepositoriesProxy(GetMojo)}, which | |
| * fails in that second case -- do not delete one and keep the other. | |
| * | |
| * @throws Exception in case of errors | |
| */ | |
| @Test | |
| @InjectMojo(goal = "get") | |
| void testRemoteRepositoriesNonProxyHosts(GetMojo mojo) throws Exception { | |
| HttpServer server = createServer(); | |
| try { | |
| String url = serverUrl(server); | |
| setRemoteRepositories(mojo, "myserver::default::" + url); | |
| session.getSettings().addProxy(createProxy(URI.create(url).getHost())); | |
| applySettingsToRepositorySession(); | |
| mojo.setGroupId("test"); | |
| mojo.setArtifactId("test"); | |
| mojo.setVersion("1.0"); | |
| mojo.execute(); | |
| } finally { | |
| server.stop(0); | |
| } | |
| } | |
| private void setRemoteRepositories(GetMojo mojo, String... repositories) throws Exception { | |
| setVariableValueToObject(mojo, "remoteRepositories", Arrays.asList(repositories)); | |
| } | |
| /** | |
| * Whether the failure came from the proxy rather than from, say, a missing artifact or a rejected login — the | |
| * host of {@link #createProxy(String)} only ever appears if the proxy really was applied to the repository. | |
| */ | |
| private boolean mentionsProxyHost(Throwable throwable) { | |
| for (Throwable cause = throwable; cause != null; cause = cause.getCause()) { | |
| if (cause.getMessage() != null && cause.getMessage().contains(PROXY_HOST)) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| /** | |
| * An active HTTP proxy pointing at a name that is guaranteed not to resolve (RFC 2606 reserved TLD), so that | |
| * applying it is always observable as a failure. | |
| */ | |
| private Proxy createProxy(String nonProxyHosts) { | |
| Proxy proxy = new Proxy(); | |
| proxy.setActive(true); | |
| proxy.setProtocol("http"); | |
| proxy.setHost(PROXY_HOST); | |
| proxy.setPort(3128); | |
| proxy.setNonProxyHosts(nonProxyHosts); | |
| return proxy; | |
| } | |
| private String serverUrl(HttpServer server) { | |
| return "http://" + InetAddress.getLoopbackAddress().getHostName() + ":" | |
| + server.getAddress().getPort() + "/maven"; | |
| } | |
| /** | |
| * A started server that serves the test repository under {@code /maven}, behind Basic authentication that | |
| * accepts only the credentials of the {@code myserver} entry declared in {@link #setUp()}. | |
| */ | |
| private HttpServer createServer() throws IOException { | |
| Path root = Paths.get(getTestPath("repository")).toAbsolutePath().normalize(); | |
| HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); | |
| HttpContext context = server.createContext("/maven", exchange -> serve(exchange, root)); | |
| context.setAuthenticator(new BasicAuthenticator("myrealm") { | |
| @Override | |
| public boolean checkCredentials(String username, String password) { | |
| return "foo".equals(username) && "bar".equals(password); | |
| } | |
| }); | |
| server.start(); | |
| return server; | |
| } | |
| private static void serve(HttpExchange exchange, Path root) throws IOException { | |
| try { | |
| String relative = exchange.getRequestURI().getPath().substring("/maven".length()); | |
| Path file = root.resolve(relative.replaceFirst("^/+", "")).normalize(); | |
| String method = exchange.getRequestMethod(); | |
| if (!file.startsWith(root) || !Files.isRegularFile(file)) { | |
| exchange.sendResponseHeaders(404, -1); | |
| } else if ("GET".equals(method)) { | |
| exchange.sendResponseHeaders(200, Files.size(file)); | |
| try (OutputStream body = exchange.getResponseBody()) { | |
| Files.copy(file, body); | |
| } | |
| } else { | |
| exchange.sendResponseHeaders(405, -1); | |
| } | |
| } finally { | |
| exchange.close(); | |
| } | |
| } | |
| } |