1 package org.apache.maven.wagon.providers.ssh.jsch;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.mortbay.jetty.Handler;
38 import org.mortbay.jetty.Request;
39 import org.mortbay.jetty.Server;
40 import org.mortbay.jetty.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, HttpServletRequest request, HttpServletResponse response, int dispatch )
54 throws IOException, ServletException
55 {
56 assertEquals( "CONNECT", request.getMethod() );
57
58 handled = true;
59 ( (Request) request ).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 if ( server != null )
87 {
88 server.stop();
89 }
90 }
91 }
92
93 public void testSocksProxy()
94 throws Exception
95 {
96 handled = false;
97
98 int port = ( Math.abs( new Random().nextInt() ) % 2048 ) + 1024;
99
100 SocksServer s = new SocksServer( port );
101 Thread t = new Thread( s );
102 t.setDaemon( true );
103 t.start();
104
105 ProxyInfo proxyInfo = new ProxyInfo();
106 proxyInfo.setHost( "localhost" );
107 proxyInfo.setPort( port );
108 proxyInfo.setType( "socks_5" );
109 proxyInfo.setNonProxyHosts( null );
110
111 Wagon wagon = (Wagon) lookup( Wagon.ROLE, "scp" );
112 try
113 {
114 wagon.connect( new Repository( "id", "scp://localhost/tmp" ), proxyInfo );
115 fail();
116 }
117 catch ( AuthenticationException e )
118 {
119 assertTrue( handled );
120 }
121 finally
122 {
123 t.interrupt();
124 }
125 }
126
127 private final class SocksServer
128 implements Runnable
129 {
130
131 private final int port;
132
133 private String userAgent;
134
135 private SocksServer( int port )
136 {
137 this.port = port;
138 }
139
140 public void run()
141 {
142 ServerSocket ssock = null;
143 try
144 {
145 try
146 {
147 ssock = new ServerSocket( port );
148 ssock.setSoTimeout( 2000 );
149 }
150 catch ( IOException e )
151 {
152 return;
153 }
154
155 while ( !Thread.currentThread().isInterrupted() )
156 {
157 Socket sock = null;
158 try
159 {
160 try
161 {
162 sock = ssock.accept();
163 }
164 catch ( SocketTimeoutException e )
165 {
166 continue;
167 }
168
169 handled = true;
170 }
171 catch ( IOException e )
172 {
173 }
174 finally
175 {
176 if ( sock != null )
177 {
178 try
179 {
180 sock.close();
181 }
182 catch ( IOException e )
183 {
184 }
185 }
186 }
187 }
188 }
189 finally
190 {
191 if ( ssock != null )
192 {
193 try
194 {
195 ssock.close();
196 }
197 catch ( IOException e )
198 {
199 }
200 }
201 }
202 }
203 }
204 }