-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathauto_test.go
157 lines (120 loc) · 4.22 KB
/
auto_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package sshego
import (
"context"
"fmt"
"net"
//"strings"
"testing"
"time"
cv "github.com/glycerine/goconvey/convey"
ssh "github.com/glycerine/sshego/xendor/github.com/glycerine/xcryptossh"
)
func Test060AutoRedialWithTricorder(t *testing.T) {
cv.Convey("sshego.Tricorder has auto-redial on disconnect capability.", t, func() {
// start a simple TCP server that is the target of the forward through the sshd,
// so we can confirm the client has made the connection.
// generate a random payload for the client to send to the server.
payloadByteCount := 50
confirmationPayload := RandomString(payloadByteCount)
confirmationReply := RandomString(payloadByteCount)
tcpSrvLsn, tcpSrvPort := GetAvailPort()
var nc net.Conn
tcpServerMgr := ssh.NewHalter()
StartBackgroundTestTcpServer(
tcpServerMgr,
payloadByteCount,
confirmationPayload,
confirmationReply,
tcpSrvLsn,
&nc)
s := MakeTestSshClientAndServer(true)
defer TempDirCleanup(s.SrvCfg.Origdir, s.SrvCfg.Tempdir)
dest := fmt.Sprintf("127.0.0.1:%v", tcpSrvPort)
tofu := true
dc := &DialConfig{
ClientKnownHostsPath: s.CliCfg.ClientKnownHostsPath,
Mylogin: s.Mylogin,
RsaPath: s.RsaPath,
TotpUrl: s.Totp,
Pw: s.Pw,
Sshdhost: s.SrvCfg.EmbeddedSSHd.Host,
Sshdport: s.SrvCfg.EmbeddedSSHd.Port,
DownstreamHostPort: dest,
TofuAddIfNotKnown: tofu,
LocalNickname: "test060",
}
pp("060 1st time: tcpSrvPort = %v. dest='%v'", tcpSrvPort, dest)
var channelToTcpServer net.Conn
var err error
ctx := context.Background()
pp("making tri: s.CliCfg.LocalToRemote.Listen.Addr='%v'",
s.CliCfg.LocalToRemote.Listen.Addr)
tri, err := NewTricorder(dc, s.CliCfg.Halt, "test060")
panicOn(err)
bkg := context.Background()
channelToTcpServer, err = tri.SSHChannel(bkg, "direct-tcpip", dest)
cv.So(err, cv.ShouldBeNil)
cv.So(tri, cv.ShouldNotBeNil)
pp("fine with DialGetTricorder.")
<-tcpServerMgr.ReadyChan()
pp("060 1st time nc = '%#v'", nc)
pp("060 1st time nc.LocalAddr='%v'", nc.LocalAddr())
checkReconNeeded := tri.cfg.ClientReconnectNeededTower.Subscribe(nil)
VerifyClientServerExchangeAcrossSshd(channelToTcpServer, confirmationPayload, confirmationReply, payloadByteCount)
tcpServerMgr.RequestStop()
<-tcpServerMgr.DoneChan()
nc.Close()
nc = nil
channelToTcpServer.Close()
pp("starting on 2nd confirmation")
s.SrvCfg.Halt.RequestStop()
<-s.SrvCfg.Halt.DoneChan()
// after killing remote sshd
var uhp2 *UHP
select {
case uhp2 = <-checkReconNeeded:
pp("good, 060 got needReconnectCh to '%#v'", uhp2)
case <-time.After(5 * time.Second):
panic("never received <-checkReconNeeded: timeout after 5 seconds")
}
cv.So(uhp2.User, cv.ShouldEqual, s.Mylogin)
destHostPort := fmt.Sprintf("%v:%v", s.SrvCfg.EmbeddedSSHd.Host, s.SrvCfg.EmbeddedSSHd.Port)
cv.So(uhp2.HostPort, cv.ShouldEqual, destHostPort)
// so restart the sshd server
pp("waiting for destHostPort='%v' to be availble", destHostPort)
panicOn(s.SrvCfg.Esshd.Stop())
s.SrvCfg.Reset()
s.SrvCfg.NewEsshd()
s.SrvCfg.Esshd.Start(ctx)
serverDone2 := ssh.NewHalter()
confirmationPayload2 := RandomString(payloadByteCount)
confirmationReply2 := RandomString(payloadByteCount)
StartBackgroundTestTcpServer(
serverDone2,
payloadByteCount,
confirmationPayload2,
confirmationReply2,
tcpSrvLsn, &nc)
time.Sleep(time.Second)
// tri should automaticly re-Dial.
channelToTcpServer2, err := tri.SSHChannel(ctx, "direct-tcpip", dest)
panicOn(err)
<-serverDone2.ReadyChan()
pp("060 2nd time nc.LocalAddr='%v'", nc.LocalAddr())
i := 0
for k, v := range tri.sshChannels {
pp("tri.sshChannels[%v]=%p -> %p", i, k, v)
i++
}
VerifyClientServerExchangeAcrossSshd(channelToTcpServer2, confirmationPayload2, confirmationReply2, payloadByteCount)
// tcp-server should have exited because it got the expected
// message and replied with the agreed upon reply and then exited.
serverDone2.RequestStop()
<-serverDone2.DoneChan()
nc.Close()
// done with testing, cleanup
s.SrvCfg.Esshd.Stop()
<-s.SrvCfg.Esshd.Halt.DoneChan()
cv.So(true, cv.ShouldEqual, true) // we should get here.
})
}