001package org.eclipse.aether.util.repository;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static org.junit.Assert.*;
023
024import java.io.IOException;
025import java.net.Authenticator;
026import java.net.InetSocketAddress;
027import java.net.PasswordAuthentication;
028import java.net.SocketAddress;
029import java.net.URI;
030import java.net.URL;
031import java.util.Arrays;
032import java.util.Collections;
033import java.util.List;
034
035import org.eclipse.aether.DefaultRepositorySystemSession;
036import org.eclipse.aether.repository.Authentication;
037import org.eclipse.aether.repository.AuthenticationContext;
038import org.eclipse.aether.repository.Proxy;
039import org.eclipse.aether.repository.ProxySelector;
040import org.eclipse.aether.repository.RemoteRepository;
041import org.junit.After;
042import org.junit.Before;
043import org.junit.Test;
044
045public class JreProxySelectorTest
046{
047
048    private abstract class AbstractProxySelector
049        extends java.net.ProxySelector
050    {
051        @Override
052        public void connectFailed( URI uri, SocketAddress sa, IOException ioe )
053        {
054        }
055    }
056
057    private ProxySelector selector = new JreProxySelector();
058
059    private java.net.ProxySelector original;
060
061    @Before
062    public void init()
063    {
064        original = java.net.ProxySelector.getDefault();
065    }
066
067    @After
068    public void exit()
069    {
070        java.net.ProxySelector.setDefault( original );
071        Authenticator.setDefault( null );
072    }
073
074    @Test
075    public void testGetProxy_InvalidUrl()
076        throws Exception
077    {
078        RemoteRepository repo = new RemoteRepository.Builder( "test", "default", "http://host:invalid" ).build();
079        assertNull( selector.getProxy( repo ) );
080    }
081
082    @Test
083    public void testGetProxy_OpaqueUrl()
084        throws Exception
085    {
086        RemoteRepository repo = new RemoteRepository.Builder( "test", "default", "classpath:base" ).build();
087        assertNull( selector.getProxy( repo ) );
088    }
089
090    @Test
091    public void testGetProxy_NullSelector()
092        throws Exception
093    {
094        RemoteRepository repo = new RemoteRepository.Builder( "test", "default", "http://repo.eclipse.org/" ).build();
095        java.net.ProxySelector.setDefault( null );
096        assertNull( selector.getProxy( repo ) );
097    }
098
099    @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}