View Javadoc
1   package org.apache.maven.wagon.providers.ssh.jsch;
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 java.io.IOException;
23  import java.net.ServerSocket;
24  import java.net.Socket;
25  import java.net.SocketTimeoutException;
26  import java.util.Random;
27  
28  import javax.servlet.ServletException;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.apache.maven.wagon.Wagon;
33  import org.apache.maven.wagon.authentication.AuthenticationException;
34  import org.apache.maven.wagon.proxy.ProxyInfo;
35  import org.apache.maven.wagon.repository.Repository;
36  import org.codehaus.plexus.PlexusTestCase;
37  import org.eclipse.jetty.server.Handler;
38  import org.eclipse.jetty.server.HttpConfiguration;
39  import org.eclipse.jetty.server.HttpConnectionFactory;
40  import org.eclipse.jetty.server.Request;
41  import org.eclipse.jetty.server.Server;
42  import org.eclipse.jetty.server.ServerConnector;
43  import org.eclipse.jetty.server.handler.AbstractHandler;
44  
45  public class ScpWagonWithProxyTest
46      extends PlexusTestCase
47  {
48      private boolean handled;
49  
50      public void testHttpProxy()
51          throws Exception
52      {
53          handled = false;
54          Handler handler = new AbstractHandler()
55          {
56              public void handle( String target, Request baseRequest, HttpServletRequest request,
57                  HttpServletResponse response ) throws IOException, ServletException
58              {
59                  assertEquals( "CONNECT", request.getMethod() );
60  
61                  handled = true;
62                  baseRequest.setHandled( true );
63              }
64          };
65  
66          Server server = new Server(  );
67          ServerConnector connector = new ServerConnector( server, new HttpConnectionFactory( new HttpConfiguration() ) );
68          server.addConnector( connector );
69          server.setHandler( handler );
70          server.start();
71  
72          int port = connector.getLocalPort();
73          ProxyInfo proxyInfo = new ProxyInfo();
74          proxyInfo.setHost( "localhost" );
75          proxyInfo.setPort( port );
76          proxyInfo.setType( "http" );
77          proxyInfo.setNonProxyHosts( null );
78  
79          Wagon wagon = (Wagon) lookup( Wagon.ROLE, "scp" );
80          try
81          {
82              wagon.connect( new Repository( "id", "scp://localhost/tmp" ), proxyInfo );
83              fail();
84          }
85          catch ( AuthenticationException e )
86          {
87              assertTrue( handled );
88          }
89          finally
90          {
91              wagon.disconnect();
92              server.stop();
93          }
94      }
95  
96      public void testSocksProxy()
97          throws Exception
98      {
99          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 }