View Javadoc
1   package org.eclipse.aether.util.repository;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.*;
23  
24  import java.io.IOException;
25  import java.net.Authenticator;
26  import java.net.InetSocketAddress;
27  import java.net.PasswordAuthentication;
28  import java.net.SocketAddress;
29  import java.net.URI;
30  import java.net.URL;
31  import java.util.Arrays;
32  import java.util.Collections;
33  import java.util.List;
34  
35  import org.eclipse.aether.DefaultRepositorySystemSession;
36  import org.eclipse.aether.repository.Authentication;
37  import org.eclipse.aether.repository.AuthenticationContext;
38  import org.eclipse.aether.repository.Proxy;
39  import org.eclipse.aether.repository.ProxySelector;
40  import org.eclipse.aether.repository.RemoteRepository;
41  import org.junit.After;
42  import org.junit.Before;
43  import org.junit.Test;
44  
45  public class JreProxySelectorTest
46  {
47  
48      private abstract class AbstractProxySelector
49          extends java.net.ProxySelector
50      {
51          @Override
52          public void connectFailed( URI uri, SocketAddress sa, IOException ioe )
53          {
54          }
55      }
56  
57      private ProxySelector selector = new JreProxySelector();
58  
59      private java.net.ProxySelector original;
60  
61      @Before
62      public void init()
63      {
64          original = java.net.ProxySelector.getDefault();
65      }
66  
67      @After
68      public void exit()
69      {
70          java.net.ProxySelector.setDefault( original );
71          Authenticator.setDefault( null );
72      }
73  
74      @Test
75      public void testGetProxy_InvalidUrl()
76          throws Exception
77      {
78          RemoteRepository repo = new RemoteRepository.Builder( "test", "default", "http://host:invalid" ).build();
79          assertNull( selector.getProxy( repo ) );
80      }
81  
82      @Test
83      public void testGetProxy_OpaqueUrl()
84          throws Exception
85      {
86          RemoteRepository repo = new RemoteRepository.Builder( "test", "default", "classpath:base" ).build();
87          assertNull( selector.getProxy( repo ) );
88      }
89  
90      @Test
91      public void testGetProxy_NullSelector()
92          throws Exception
93      {
94          RemoteRepository repo = new RemoteRepository.Builder( "test", "default", "http://repo.eclipse.org/" ).build();
95          java.net.ProxySelector.setDefault( null );
96          assertNull( selector.getProxy( repo ) );
97      }
98  
99      @Test
100     public void testGetProxy_NoProxies()
101         throws Exception
102     {
103         RemoteRepository repo = new RemoteRepository.Builder( "test", "default", "http://repo.eclipse.org/" ).build();
104         java.net.ProxySelector.setDefault( new AbstractProxySelector()
105         {
106             @Override
107             public List<java.net.Proxy> select( URI uri )
108             {
109                 return Collections.emptyList();
110             }
111 
112         } );
113         assertNull( selector.getProxy( repo ) );
114     }
115 
116     @Test
117     public void testGetProxy_DirectProxy()
118         throws Exception
119     {
120         RemoteRepository repo = new RemoteRepository.Builder( "test", "default", "http://repo.eclipse.org/" ).build();
121         final InetSocketAddress addr = InetSocketAddress.createUnresolved( "proxy", 8080 );
122         java.net.ProxySelector.setDefault( new AbstractProxySelector()
123         {
124             @Override
125             public List<java.net.Proxy> select( URI uri )
126             {
127                 return Arrays.asList( java.net.Proxy.NO_PROXY, new java.net.Proxy( java.net.Proxy.Type.HTTP, addr ) );
128             }
129 
130         } );
131         assertNull( selector.getProxy( repo ) );
132     }
133 
134     @Test
135     public void testGetProxy_HttpProxy()
136         throws Exception
137     {
138         final RemoteRepository repo =
139             new RemoteRepository.Builder( "test", "default", "http://repo.eclipse.org/" ).build();
140         final URL url = new URL( repo.getUrl() );
141         final InetSocketAddress addr = InetSocketAddress.createUnresolved( "proxy", 8080 );
142         java.net.ProxySelector.setDefault( new AbstractProxySelector()
143         {
144             @Override
145             public List<java.net.Proxy> select( URI uri )
146             {
147                 if ( repo.getHost().equalsIgnoreCase( uri.getHost() ) )
148                 {
149                     return Arrays.asList( new java.net.Proxy( java.net.Proxy.Type.HTTP, addr ) );
150                 }
151                 return Collections.emptyList();
152             }
153 
154         } );
155         Authenticator.setDefault( new Authenticator()
156         {
157             @Override
158             protected PasswordAuthentication getPasswordAuthentication()
159             {
160                 if ( Authenticator.RequestorType.PROXY.equals( getRequestorType() )
161                     && addr.getHostName().equals( getRequestingHost() ) && addr.getPort() == getRequestingPort()
162                     && url.equals( getRequestingURL() ) )
163                 {
164                     return new PasswordAuthentication( "proxyuser", "proxypass".toCharArray() );
165                 }
166                 return super.getPasswordAuthentication();
167             }
168         } );
169 
170         Proxy proxy = selector.getProxy( repo );
171         assertNotNull( proxy );
172         assertEquals( addr.getHostName(), proxy.getHost() );
173         assertEquals( addr.getPort(), proxy.getPort() );
174         assertEquals( Proxy.TYPE_HTTP, proxy.getType() );
175 
176         RemoteRepository repo2 = new RemoteRepository.Builder( repo ).setProxy( proxy ).build();
177         Authentication auth = proxy.getAuthentication();
178         assertNotNull( auth );
179         AuthenticationContext authCtx = AuthenticationContext.forProxy( new DefaultRepositorySystemSession(), repo2 );
180         assertEquals( "proxyuser", authCtx.get( AuthenticationContext.USERNAME ) );
181         assertEquals( "proxypass", authCtx.get( AuthenticationContext.PASSWORD ) );
182     }
183 
184 }