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      {
77          RemoteRepository repo = new RemoteRepository.Builder( "test", "default", "http://host:invalid" ).build();
78          assertNull( selector.getProxy( repo ) );
79      }
80  
81      @Test
82      public void testGetProxy_OpaqueUrl()
83      {
84          RemoteRepository repo = new RemoteRepository.Builder( "test", "default", "classpath:base" ).build();
85          assertNull( selector.getProxy( repo ) );
86      }
87  
88      @Test
89      public void testGetProxy_NullSelector()
90      {
91          RemoteRepository repo = new RemoteRepository.Builder( "test", "default", "http://repo.eclipse.org/" ).build();
92          java.net.ProxySelector.setDefault( null );
93          assertNull( selector.getProxy( repo ) );
94      }
95  
96      @Test
97      public void testGetProxy_NoProxies()
98      {
99          RemoteRepository repo = new RemoteRepository.Builder( "test", "default", "http://repo.eclipse.org/" ).build();
100         java.net.ProxySelector.setDefault( new AbstractProxySelector()
101         {
102             @Override
103             public List<java.net.Proxy> select( URI uri )
104             {
105                 return Collections.emptyList();
106             }
107 
108         } );
109         assertNull( selector.getProxy( repo ) );
110     }
111 
112     @Test
113     public void testGetProxy_DirectProxy()
114     {
115         RemoteRepository repo = new RemoteRepository.Builder( "test", "default", "http://repo.eclipse.org/" ).build();
116         final InetSocketAddress addr = InetSocketAddress.createUnresolved( "proxy", 8080 );
117         java.net.ProxySelector.setDefault( new AbstractProxySelector()
118         {
119             @Override
120             public List<java.net.Proxy> select( URI uri )
121             {
122                 return Arrays.asList( java.net.Proxy.NO_PROXY, new java.net.Proxy( java.net.Proxy.Type.HTTP, addr ) );
123             }
124 
125         } );
126         assertNull( selector.getProxy( repo ) );
127     }
128 
129     @Test
130     public void testGetProxy_HttpProxy()
131         throws Exception
132     {
133         final RemoteRepository repo =
134             new RemoteRepository.Builder( "test", "default", "http://repo.eclipse.org/" ).build();
135         final URL url = new URL( repo.getUrl() );
136         final InetSocketAddress addr = InetSocketAddress.createUnresolved( "proxy", 8080 );
137         java.net.ProxySelector.setDefault( new AbstractProxySelector()
138         {
139             @Override
140             public List<java.net.Proxy> select( URI uri )
141             {
142                 if ( repo.getHost().equalsIgnoreCase( uri.getHost() ) )
143                 {
144                     return Arrays.asList( new java.net.Proxy( java.net.Proxy.Type.HTTP, addr ) );
145                 }
146                 return Collections.emptyList();
147             }
148 
149         } );
150         Authenticator.setDefault( new Authenticator()
151         {
152             @Override
153             protected PasswordAuthentication getPasswordAuthentication()
154             {
155                 if ( Authenticator.RequestorType.PROXY.equals( getRequestorType() )
156                     && addr.getHostName().equals( getRequestingHost() ) && addr.getPort() == getRequestingPort()
157                     && url.equals( getRequestingURL() ) )
158                 {
159                     return new PasswordAuthentication( "proxyuser", "proxypass".toCharArray() );
160                 }
161                 return super.getPasswordAuthentication();
162             }
163         } );
164 
165         Proxy proxy = selector.getProxy( repo );
166         assertNotNull( proxy );
167         assertEquals( addr.getHostName(), proxy.getHost() );
168         assertEquals( addr.getPort(), proxy.getPort() );
169         assertEquals( Proxy.TYPE_HTTP, proxy.getType() );
170 
171         RemoteRepository repo2 = new RemoteRepository.Builder( repo ).setProxy( proxy ).build();
172         Authentication auth = proxy.getAuthentication();
173         assertNotNull( auth );
174         AuthenticationContext authCtx = AuthenticationContext.forProxy( new DefaultRepositorySystemSession(), repo2 );
175         assertEquals( "proxyuser", authCtx.get( AuthenticationContext.USERNAME ) );
176         assertEquals( "proxypass", authCtx.get( AuthenticationContext.PASSWORD ) );
177     }
178 
179 }