-
Notifications
You must be signed in to change notification settings - Fork 19
/
ud_test.go
147 lines (120 loc) · 4.15 KB
/
ud_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
package sshego
import (
"context"
"fmt"
"net"
"os"
"testing"
"time"
cv "github.com/glycerine/goconvey/convey"
)
// ud_test.go: unix domain socket test.
// this can be flakey in batch run of all tests...
func Test401UnixDomainSocketListening(t *testing.T) {
cv.Convey("Instead of -listen and -remote only forwarding via connections, if given a path instead of a port it should listen on a unix domain socket.", t, func() {
// since can be red under go test -v but green
// when run standalone, try sleeping for a few seconds to
// let ports reset?
time.Sleep(time.Second * 2)
// generate a random payload for the client to send to the server.
payloadByteCount := 50
confirmationPayload := RandomString(payloadByteCount)
confirmationReply := RandomString(payloadByteCount)
serverDone := make(chan bool)
udpath := startBackgroundTestUnixDomainServer(
serverDone,
payloadByteCount,
confirmationPayload,
confirmationReply)
defer os.Remove(udpath)
s := MakeTestSshClientAndServer(true)
defer TempDirCleanup(s.SrvCfg.Origdir, s.SrvCfg.Tempdir)
dest := udpath
// below over SSH should be equivalent of the following
// non-encrypted ping/pong.
if false {
udUnencPingPong(udpath, confirmationPayload, confirmationReply, payloadByteCount)
}
if 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: true,
}
ctx := context.Background()
// first time we add the server key
channelToTcpServer, _, _, err := dc.Dial(ctx, nil, false)
cv.So(err.Error(), cv.ShouldContainSubstring, "Re-run without -new")
// second time we connect based on that server key
dc.TofuAddIfNotKnown = false
channelToTcpServer, _, _, err = dc.Dial(ctx, nil, false)
cv.So(err, cv.ShouldBeNil)
VerifyClientServerExchangeAcrossSshd(channelToTcpServer, confirmationPayload, confirmationReply, payloadByteCount)
channelToTcpServer.Close()
}
// tcp-server should have exited because it got the expected
// message and replied with the agreed upon reply and then exited.
<-serverDone
// done with testing, cleanup
s.SrvCfg.Esshd.Stop()
<-s.SrvCfg.Esshd.Halt.DoneChan()
cv.So(true, cv.ShouldEqual, true) // we should get here.
})
}
func udUnencPingPong(dest, confirmationPayload, confirmationReply string, payloadByteCount int) {
conn, err := net.Dial("unix", dest)
panicOn(err)
m, err := conn.Write([]byte(confirmationPayload))
panicOn(err)
if m != payloadByteCount {
panic("too short a write!")
}
// check reply
rep := make([]byte, payloadByteCount)
m, err = conn.Read(rep)
panicOn(err)
if m != payloadByteCount {
panic("too short a reply!")
}
srep := string(rep)
if srep != confirmationReply {
panic(fmt.Errorf("saw '%s' but expected '%s'", srep, confirmationReply))
}
pp("reply success! server back to -> client: we got the expected srep reply '%s'", srep)
conn.Close()
}
func startBackgroundTestUnixDomainServer(serverDone chan bool, payloadByteCount int, confirmationPayload string, confirmationReply string) (udpath string) {
udpath = "/tmp/ud_test.sock." + RandomString(20)
lsn, err := net.Listen("unix", udpath)
panicOn(err)
go func() {
udServerConn, err := lsn.Accept()
panicOn(err)
b := make([]byte, payloadByteCount)
n, err := udServerConn.Read(b)
panicOn(err)
if n != payloadByteCount {
panic(fmt.Errorf("read too short! got %v but expected %v", n, payloadByteCount))
}
saw := string(b)
if saw != confirmationPayload {
panic(fmt.Errorf("expected '%s', but saw '%s'", confirmationPayload, saw))
}
pp("client -> server success! server got expected confirmation payload of '%s'", saw)
// reply back
n, err = udServerConn.Write([]byte(confirmationReply))
panicOn(err)
if n != payloadByteCount {
panic(fmt.Errorf("write too short! got %v but expected %v", n, payloadByteCount))
}
//udServerConn.Close()
close(serverDone)
}()
return udpath
}