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.util.repository;
20  
21  import java.io.IOException;
22  import java.net.Authenticator;
23  import java.net.InetSocketAddress;
24  import java.net.PasswordAuthentication;
25  import java.net.SocketAddress;
26  import java.net.URI;
27  import java.net.URL;
28  import java.util.Arrays;
29  import java.util.Collections;
30  import java.util.List;
31  import java.util.Objects;
32  
33  import org.eclipse.aether.internal.test.util.TestUtils;
34  import org.eclipse.aether.repository.Authentication;
35  import org.eclipse.aether.repository.AuthenticationContext;
36  import org.eclipse.aether.repository.Proxy;
37  import org.eclipse.aether.repository.ProxySelector;
38  import org.eclipse.aether.repository.RemoteRepository;
39  import org.junit.jupiter.api.AfterEach;
40  import org.junit.jupiter.api.BeforeEach;
41  import org.junit.jupiter.api.Test;
42  
43  import static org.junit.jupiter.api.Assertions.*;
44  
45  public class JreProxySelectorTest {
46  
47      private abstract static class AbstractProxySelector extends java.net.ProxySelector {
48          @Override
49          public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {}
50      }
51  
52      private final ProxySelector selector = new JreProxySelector();
53  
54      private java.net.ProxySelector original;
55  
56      @BeforeEach
57      void init() {
58          original = java.net.ProxySelector.getDefault();
59      }
60  
61      @AfterEach
62      void exit() {
63          java.net.ProxySelector.setDefault(original);
64          Authenticator.setDefault(null);
65      }
66  
67      @Test
68      void testGetProxy_InvalidUrl() {
69          RemoteRepository repo = new RemoteRepository.Builder("test", "default", "http://host:invalid").build();
70          assertNull(selector.getProxy(repo));
71      }
72  
73      @Test
74      void testGetProxy_OpaqueUrl() {
75          RemoteRepository repo = new RemoteRepository.Builder("test", "default", "classpath:base").build();
76          assertNull(selector.getProxy(repo));
77      }
78  
79      @Test
80      void testGetProxy_NullSelector() {
81          RemoteRepository repo = new RemoteRepository.Builder("test", "default", "http://repo.eclipse.org/").build();
82          java.net.ProxySelector.setDefault(null);
83          assertNull(selector.getProxy(repo));
84      }
85  
86      @Test
87      void testGetProxy_NoProxies() {
88          RemoteRepository repo = new RemoteRepository.Builder("test", "default", "http://repo.eclipse.org/").build();
89          java.net.ProxySelector.setDefault(new AbstractProxySelector() {
90              @Override
91              public List<java.net.Proxy> select(URI uri) {
92                  return Collections.emptyList();
93              }
94          });
95          assertNull(selector.getProxy(repo));
96      }
97  
98      @Test
99      void testGetProxy_DirectProxy() {
100         RemoteRepository repo = new RemoteRepository.Builder("test", "default", "http://repo.eclipse.org/").build();
101         final InetSocketAddress addr = InetSocketAddress.createUnresolved("proxy", 8080);
102         java.net.ProxySelector.setDefault(new AbstractProxySelector() {
103             @Override
104             public List<java.net.Proxy> select(URI uri) {
105                 return Arrays.asList(java.net.Proxy.NO_PROXY, new java.net.Proxy(java.net.Proxy.Type.HTTP, addr));
106             }
107         });
108         assertNull(selector.getProxy(repo));
109     }
110 
111     @Test
112     void testGetProxy_HttpProxy() throws Exception {
113         final RemoteRepository repo =
114                 new RemoteRepository.Builder("test", "default", "http://repo.eclipse.org/").build();
115         final URL url = new URI(repo.getUrl()).toURL();
116         final InetSocketAddress addr = InetSocketAddress.createUnresolved("proxy", 8080);
117         java.net.ProxySelector.setDefault(new AbstractProxySelector() {
118             @Override
119             public List<java.net.Proxy> select(URI uri) {
120                 if (repo.getHost().equalsIgnoreCase(uri.getHost())) {
121                     return Collections.singletonList(new java.net.Proxy(java.net.Proxy.Type.HTTP, addr));
122                 }
123                 return Collections.emptyList();
124             }
125         });
126         Authenticator.setDefault(new Authenticator() {
127             @Override
128             protected PasswordAuthentication getPasswordAuthentication() {
129                 if (Authenticator.RequestorType.PROXY.equals(getRequestorType())
130                         && addr.getHostName().equals(getRequestingHost())
131                         && addr.getPort() == getRequestingPort()
132                         && Objects.equals(url, getRequestingURL())) {
133                     return new PasswordAuthentication("proxyuser", "proxypass".toCharArray());
134                 }
135                 return super.getPasswordAuthentication();
136             }
137         });
138 
139         Proxy proxy = selector.getProxy(repo);
140         assertNotNull(proxy);
141         assertEquals(addr.getHostName(), proxy.getHost());
142         assertEquals(addr.getPort(), proxy.getPort());
143         assertEquals(Proxy.TYPE_HTTP, proxy.getType());
144 
145         RemoteRepository repo2 =
146                 new RemoteRepository.Builder(repo).setProxy(proxy).build();
147         Authentication auth = proxy.getAuthentication();
148         assertNotNull(auth);
149         try (AuthenticationContext authCtx = AuthenticationContext.forProxy(TestUtils.newSession(), repo2)) {
150             assertEquals("proxyuser", authCtx.get(AuthenticationContext.USERNAME));
151             assertEquals("proxypass", authCtx.get(AuthenticationContext.PASSWORD));
152         }
153     }
154 }