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.Request;
39  import org.eclipse.jetty.server.Server;
40  import org.eclipse.jetty.server.handler.AbstractHandler;
41  
42  public class ScpWagonWithProxyTest
43      extends PlexusTestCase
44  {
45      private boolean handled;
46  
47      public void testHttpProxy()
48          throws Exception
49      {
50          handled = false;
51          Handler handler = new AbstractHandler()
52          {
53              public void handle( String target, Request baseRequest, HttpServletRequest request,
54                  HttpServletResponse response ) throws IOException, ServletException
55              {
56                  assertEquals( "CONNECT", request.getMethod() );
57  
58                  handled = true;
59                  baseRequest.setHandled( true );
60              }
61          };
62  
63          Server server = new Server( 0 );
64          server.setHandler( handler );
65          server.start();
66  
67          int port = server.getConnectors()[0].getLocalPort();
68          ProxyInfo proxyInfo = new ProxyInfo();
69          proxyInfo.setHost( "localhost" );
70          proxyInfo.setPort( port );
71          proxyInfo.setType( "http" );
72          proxyInfo.setNonProxyHosts( null );
73  
74          Wagon wagon = (Wagon) lookup( Wagon.ROLE, "scp" );
75          try
76          {
77              wagon.connect( new Repository( "id", "scp://localhost/tmp" ), proxyInfo );
78              fail();
79          }
80          catch ( AuthenticationException e )
81          {
82              assertTrue( handled );
83          }
84          finally
85          {
86              wagon.disconnect();
87              server.stop();
88          }
89      }
90  
91      public void testSocksProxy()
92          throws Exception
93      {
94          handled = false;
95  
96          int port = ( Math.abs( new Random().nextInt() ) % 2048 ) + 1024;
97  
98          SocksServer s = new SocksServer( port );
99          Thread t = new Thread( s );
100         t.setDaemon( true );
101         t.start();
102 
103         ProxyInfo proxyInfo = new ProxyInfo();
104         proxyInfo.setHost( "localhost" );
105         proxyInfo.setPort( port );
106         proxyInfo.setType( "socks_5" );
107         proxyInfo.setNonProxyHosts( null );
108 
109         Wagon wagon = (Wagon) lookup( Wagon.ROLE, "scp" );
110         try
111         {
112             wagon.connect( new Repository( "id", "scp://localhost/tmp" ), proxyInfo );
113             fail();
114         }
115         catch ( AuthenticationException e )
116         {
117             assertTrue( handled );
118         }
119         finally
120         {
121             wagon.disconnect();
122             t.interrupt();
123         }
124     }
125 
126     private final class SocksServer
127         implements Runnable
128     {
129 
130         private final int port;
131 
132         private String userAgent;
133 
134         private SocksServer( int port )
135         {
136             this.port = port;
137         }
138 
139         public void run()
140         {
141             ServerSocket ssock = null;
142             try
143             {
144                 try
145                 {
146                     ssock = new ServerSocket( port );
147                     ssock.setSoTimeout( 2000 );
148                 }
149                 catch ( IOException e )
150                 {
151                     return;
152                 }
153 
154                 while ( !Thread.currentThread().isInterrupted() )
155                 {
156                     Socket sock = null;
157                     try
158                     {
159                         try
160                         {
161                             sock = ssock.accept();
162                         }
163                         catch ( SocketTimeoutException e )
164                         {
165                             continue;
166                         }
167 
168                         handled = true;
169                     }
170                     catch ( IOException e )
171                     {
172                     }
173                     finally
174                     {
175                         if ( sock != null )
176                         {
177                             try
178                             {
179                                 sock.close();
180                             }
181                             catch ( IOException e )
182                             {
183                             }
184                         }
185                     }
186                 }
187             }
188             finally
189             {
190                 if ( ssock != null )
191                 {
192                     try
193                     {
194                         ssock.close();
195                     }
196                     catch ( IOException e )
197                     {
198                     }
199                 }
200             }
201         }
202     }
203 }