001package org.apache.maven.wagon.providers.ssh.jsch;
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 java.io.IOException;
023import java.net.ServerSocket;
024import java.net.Socket;
025import java.net.SocketTimeoutException;
026import java.util.Random;
027
028import javax.servlet.ServletException;
029import javax.servlet.http.HttpServletRequest;
030import javax.servlet.http.HttpServletResponse;
031
032import org.apache.maven.wagon.Wagon;
033import org.apache.maven.wagon.authentication.AuthenticationException;
034import org.apache.maven.wagon.proxy.ProxyInfo;
035import org.apache.maven.wagon.repository.Repository;
036import org.codehaus.plexus.PlexusTestCase;
037import org.eclipse.jetty.server.Handler;
038import org.eclipse.jetty.server.HttpConfiguration;
039import org.eclipse.jetty.server.HttpConnectionFactory;
040import org.eclipse.jetty.server.Request;
041import org.eclipse.jetty.server.Server;
042import org.eclipse.jetty.server.ServerConnector;
043import org.eclipse.jetty.server.handler.AbstractHandler;
044
045public class ScpWagonWithProxyTest
046    extends PlexusTestCase
047{
048    private boolean handled;
049
050    public void testHttpProxy()
051        throws Exception
052    {
053        handled = false;
054        Handler handler = new AbstractHandler()
055        {
056            public void handle( String target, Request baseRequest, HttpServletRequest request,
057                HttpServletResponse response ) throws IOException, ServletException
058            {
059                assertEquals( "CONNECT", request.getMethod() );
060
061                handled = true;
062                baseRequest.setHandled( true );
063            }
064        };
065
066        Server server = new Server(  );
067        ServerConnector connector = new ServerConnector( server, new HttpConnectionFactory( new HttpConfiguration() ) );
068        server.addConnector( connector );
069        server.setHandler( handler );
070        server.start();
071
072        int port = connector.getLocalPort();
073        ProxyInfo proxyInfo = new ProxyInfo();
074        proxyInfo.setHost( "localhost" );
075        proxyInfo.setPort( port );
076        proxyInfo.setType( "http" );
077        proxyInfo.setNonProxyHosts( null );
078
079        Wagon wagon = (Wagon) lookup( Wagon.ROLE, "scp" );
080        try
081        {
082            wagon.connect( new Repository( "id", "scp://localhost/tmp" ), proxyInfo );
083            fail();
084        }
085        catch ( AuthenticationException e )
086        {
087            assertTrue( handled );
088        }
089        finally
090        {
091            wagon.disconnect();
092            server.stop();
093        }
094    }
095
096    public void testSocksProxy()
097        throws Exception
098    {
099        handled = false;
100
101        int port = ( Math.abs( new Random().nextInt() ) % 2048 ) + 1024;
102
103        SocksServer s = new SocksServer( port );
104        Thread t = new Thread( s );
105        t.setDaemon( true );
106        t.start();
107
108        ProxyInfo proxyInfo = new ProxyInfo();
109        proxyInfo.setHost( "localhost" );
110        proxyInfo.setPort( port );
111        proxyInfo.setType( "socks_5" );
112        proxyInfo.setNonProxyHosts( null );
113
114        Wagon wagon = (Wagon) lookup( Wagon.ROLE, "scp" );
115        try
116        {
117            wagon.connect( new Repository( "id", "scp://localhost/tmp" ), proxyInfo );
118            fail();
119        }
120        catch ( AuthenticationException e )
121        {
122            assertTrue( handled );
123        }
124        finally
125        {
126            wagon.disconnect();
127            t.interrupt();
128        }
129    }
130
131    private final class SocksServer
132        implements Runnable
133    {
134
135        private final int port;
136
137        private String userAgent;
138
139        private SocksServer( int port )
140        {
141            this.port = port;
142        }
143
144        public void run()
145        {
146            ServerSocket ssock = null;
147            try
148            {
149                try
150                {
151                    ssock = new ServerSocket( port );
152                    ssock.setSoTimeout( 2000 );
153                }
154                catch ( IOException e )
155                {
156                    return;
157                }
158
159                while ( !Thread.currentThread().isInterrupted() )
160                {
161                    Socket sock = null;
162                    try
163                    {
164                        try
165                        {
166                            sock = ssock.accept();
167                        }
168                        catch ( SocketTimeoutException e )
169                        {
170                            continue;
171                        }
172
173                        handled = true;
174                    }
175                    catch ( IOException e )
176                    {
177                    }
178                    finally
179                    {
180                        if ( sock != null )
181                        {
182                            try
183                            {
184                                sock.close();
185                            }
186                            catch ( IOException e )
187                            {
188                            }
189                        }
190                    }
191                }
192            }
193            finally
194            {
195                if ( ssock != null )
196                {
197                    try
198                    {
199                        ssock.close();
200                    }
201                    catch ( IOException e )
202                    {
203                    }
204                }
205            }
206        }
207    }
208}