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