@@ -166,9 +166,10 @@ func TestTunnelOptions(t *testing.T) {
166
166
167
167
}
168
168
169
+ //TODO teardown the tunnel
169
170
func TestTunnel (t * testing.T ) {
170
171
expected := "ABC"
171
- tun := prepareTunnel (t )
172
+ tun := prepareTunnel (t , false )
172
173
173
174
select {
174
175
case <- tun .Ready :
@@ -191,6 +192,37 @@ func TestTunnel(t *testing.T) {
191
192
if expected != response {
192
193
t .Errorf ("expected: %s, value: %s" , expected , response )
193
194
}
195
+
196
+ tun .Stop ()
197
+ }
198
+
199
+ func TestInsecureTunnel (t * testing.T ) {
200
+ expected := "ABC"
201
+ tun := prepareTunnel (t , true )
202
+
203
+ select {
204
+ case <- tun .Ready :
205
+ t .Log ("tunnel is ready to accept connections" )
206
+ case <- time .After (1 * time .Second ):
207
+ t .Errorf ("no connection after a while" )
208
+ return
209
+ }
210
+
211
+ resp , err := http .Get (fmt .Sprintf ("http://%s/%s" , tun .listener .Addr (), expected ))
212
+ if err != nil {
213
+ t .Errorf ("error while making local connection: %v" , err )
214
+ return
215
+ }
216
+ defer resp .Body .Close ()
217
+
218
+ body , _ := ioutil .ReadAll (resp .Body )
219
+ response := string (body )
220
+
221
+ if expected != response {
222
+ t .Errorf ("expected: %s, value: %s" , expected , response )
223
+ }
224
+
225
+ tun .Stop ()
194
226
}
195
227
196
228
func TestRandomLocalPort (t * testing.T ) {
@@ -244,13 +276,18 @@ func TestMain(m *testing.M) {
244
276
245
277
// prepareTunnel creates a Tunnel object making sure all infrastructure
246
278
// dependencies (ssh and http servers) are ready.
247
- func prepareTunnel (t * testing.T ) * Tunnel {
248
- sshAddr := createSSHServer (keyPath )
249
- generateKnownHosts (sshAddr .String (), publicKeyPath , knownHostsPath )
250
- s , _ := NewServer ("mole" , sshAddr .String (), "" )
279
+ func prepareTunnel (t * testing.T , insecure bool ) * Tunnel {
280
+ ssh := createSSHServer (keyPath )
281
+ srv , _ := NewServer ("mole" , ssh .Addr ().String (), "" )
251
282
252
- httpAddr := createWebServer ()
253
- tun := & Tunnel {local : "127.0.0.1:0" , server : s , remote : httpAddr .String (), done : make (chan error ), Ready : make (chan bool , 1 )}
283
+ srv .Insecure = insecure
284
+
285
+ if ! insecure {
286
+ generateKnownHosts (ssh .Addr ().String (), publicKeyPath , knownHostsPath )
287
+ }
288
+
289
+ web := createWebServer ()
290
+ tun := & Tunnel {local : "127.0.0.1:0" , server : srv , remote : web .Addr ().String (), done : make (chan error ), Ready : make (chan bool , 1 )}
254
291
255
292
go func (t * testing.T ) {
256
293
err := tun .Start ()
@@ -322,19 +359,24 @@ func get(client http.Client, resource string) (string, error) {
322
359
//
323
360
// Example: If the request URI is /this-is-a-test, the response will be
324
361
// this-is-a-test
325
- func createWebServer () net.Addr {
362
+ func createWebServer () net.Listener {
326
363
327
364
handler := func (w http.ResponseWriter , r * http.Request ) {
328
365
fmt .Fprintf (w , r .URL .Path [1 :])
329
366
}
330
- http .HandleFunc ("/" , handler )
367
+
368
+ mux := http .NewServeMux ()
369
+ mux .HandleFunc ("/" , handler )
370
+
371
+ server := & http.Server {
372
+ Handler : mux ,
373
+ }
374
+
331
375
l , _ := net .Listen ("tcp" , "127.0.0.1:0" )
332
376
333
- go func (l net.Listener ) {
334
- http .Serve (l , nil )
335
- }(l )
377
+ go server .Serve (l )
336
378
337
- return l . Addr ()
379
+ return l
338
380
}
339
381
340
382
// createSSHServer starts a SSH server that authenticates connections using
@@ -347,7 +389,7 @@ func createWebServer() net.Addr {
347
389
// References:
348
390
// https://gist.github.com/jpillora/b480fde82bff51a06238
349
391
// https://tools.ietf.org/html/rfc4254#section-7.2
350
- func createSSHServer (keyPath string ) net.Addr {
392
+ func createSSHServer (keyPath string ) net.Listener {
351
393
conf := & ssh.ServerConfig {
352
394
PublicKeyCallback : func (conn ssh.ConnMetadata , key ssh.PublicKey ) (* ssh.Permissions , error ) {
353
395
return & ssh.Permissions {}, nil
@@ -398,7 +440,7 @@ func createSSHServer(keyPath string) net.Addr {
398
440
}
399
441
}(l )
400
442
401
- return l . Addr ()
443
+ return l
402
444
}
403
445
404
446
// generateKnownHosts creates a new "known_hosts" file on a given path with a
0 commit comments