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 { 077 RemoteRepository repo = new RemoteRepository.Builder( "test", "default", "http://host:invalid" ).build(); 078 assertNull( selector.getProxy( repo ) ); 079 } 080 081 @Test 082 public void testGetProxy_OpaqueUrl() 083 { 084 RemoteRepository repo = new RemoteRepository.Builder( "test", "default", "classpath:base" ).build(); 085 assertNull( selector.getProxy( repo ) ); 086 } 087 088 @Test 089 public void testGetProxy_NullSelector() 090 { 091 RemoteRepository repo = new RemoteRepository.Builder( "test", "default", "http://repo.eclipse.org/" ).build(); 092 java.net.ProxySelector.setDefault( null ); 093 assertNull( selector.getProxy( repo ) ); 094 } 095 096 @Test 097 public void testGetProxy_NoProxies() 098 { 099 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}