-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclient_test.go
More file actions
382 lines (340 loc) · 8.66 KB
/
Copy pathclient_test.go
File metadata and controls
382 lines (340 loc) · 8.66 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package mosh
import (
"io"
"os/exec"
"regexp"
"strconv"
"strings"
"sync"
"testing"
"time"
)
var clientAnsiRe = regexp.MustCompile(`\x1b\[[0-9;?]*[A-Za-z]`)
func clientStripANSI(s string) string {
return clientAnsiRe.ReplaceAllString(s, "")
}
// TestClientServerE2E tests the Go client against the Go server.
func TestClientServerE2E(t *testing.T) {
srv, err := NewServer("/bin/sh", 0, 0)
if err != nil {
t.Fatal(err)
}
go srv.Serve()
defer srv.Close()
<-srv.started
client, err := Dial("127.0.0.1", srv.Port(), srv.KeyBase64())
if err != nil {
t.Fatal(err)
}
defer client.Close()
// Wait for shell prompt.
var gotOutput bool
for i := 0; i < 40; i++ {
out := client.Recv(500 * time.Millisecond)
if len(out) > 0 {
gotOutput = true
break
}
}
if !gotOutput {
t.Fatal("no output from server")
}
// Send a command and verify echo.
marker := "GOCLIENT_" + strconv.FormatInt(time.Now().UnixNano(), 36)
client.Send([]byte("echo " + marker + "\n"))
var allOutput string
deadline := time.After(10 * time.Second)
for {
select {
case <-deadline:
t.Fatalf("marker not echoed. Got: %q", clientStripANSI(allOutput))
default:
}
out := client.Recv(500 * time.Millisecond)
if out != nil {
allOutput += string(out)
if strings.Contains(clientStripANSI(allOutput), marker) {
t.Log("Go client → Go server E2E passed")
return
}
}
}
}
// TestClientResize tests resize via the Go client.
func TestClientResize(t *testing.T) {
srv, err := NewServer("/bin/sh", 0, 0)
if err != nil {
t.Fatal(err)
}
go srv.Serve()
defer srv.Close()
<-srv.started
client, err := Dial("127.0.0.1", srv.Port(), srv.KeyBase64())
if err != nil {
t.Fatal(err)
}
defer client.Close()
// Wait for shell.
for i := 0; i < 20; i++ {
if out := client.Recv(500 * time.Millisecond); len(out) > 0 {
break
}
}
// Resize and verify via tput.
client.Resize(132, 43)
time.Sleep(200 * time.Millisecond)
client.Send([]byte("tput cols; tput lines\n"))
var allOutput string
deadline := time.After(10 * time.Second)
for {
select {
case <-deadline:
t.Logf("output: %q", allOutput)
t.Log("resize sent (tput output not verified)")
return
default:
}
out := client.Recv(500 * time.Millisecond)
if out != nil {
allOutput += string(out)
stripped := clientStripANSI(allOutput)
if strings.Contains(stripped, "132") && strings.Contains(stripped, "43") {
t.Log("Go client resize verified: 132x43")
return
}
}
}
}
// TestClientServeRW tests the Go client against ServeRW (bridge mode).
func TestClientServeRW(t *testing.T) {
srv, err := NewServer("", 0, 0)
if err != nil {
t.Fatal(err)
}
// Two pipe pairs: one for terminal output, one for keystrokes.
outR, outW := io.Pipe() // terminal output: outW writes → server reads from outR
inR, inW := io.Pipe() // keystrokes: server writes to inW → inR reads
rw := struct {
io.Reader
io.Writer
io.Closer
}{outR, inW, outR}
var resizeCols, resizeRows uint16
var resizeMu sync.Mutex
go srv.ServeRW(&rw, func(c, r uint16) {
resizeMu.Lock()
resizeCols, resizeRows = c, r
resizeMu.Unlock()
})
defer func() {
outR.Close()
outW.Close()
inR.Close()
inW.Close()
srv.conn.Close()
}()
client, err := Dial("127.0.0.1", srv.Port(), srv.KeyBase64())
if err != nil {
t.Fatal(err)
}
defer client.Close()
// Write terminal output into the pipe — simulates latch rendering.
go outW.Write([]byte("hello from latch\r\n"))
// Client should receive it via mosh transport.
var allOutput string
deadline := time.After(10 * time.Second)
for {
select {
case <-deadline:
t.Fatalf("did not receive bridge output. Got: %q", allOutput)
default:
}
out := client.Recv(500 * time.Millisecond)
if out != nil {
allOutput += string(out)
if strings.Contains(clientStripANSI(allOutput), "hello from latch") {
break
}
}
}
// Send keystrokes — should arrive on the keystroke pipe.
client.Send([]byte("test-input"))
keystrokeDone := make(chan string, 1)
go func() {
buf := make([]byte, 256)
n, err := inR.Read(buf)
if err != nil {
keystrokeDone <- ""
return
}
keystrokeDone <- string(buf[:n])
}()
select {
case got := <-keystrokeDone:
if !strings.Contains(got, "test-input") {
t.Fatalf("keystroke mismatch: got %q, want substring %q", got, "test-input")
}
case <-time.After(5 * time.Second):
t.Fatal("timed out waiting for keystrokes on pipe")
}
// Drain any extra data from the pipe (cumulative diffs may resend keystrokes).
go func() {
buf := make([]byte, 4096)
for {
inR.Read(buf)
}
}()
// Resize.
client.Resize(200, 50)
time.Sleep(500 * time.Millisecond)
resizeMu.Lock()
c, r := resizeCols, resizeRows
resizeMu.Unlock()
if c != 200 || r != 50 {
t.Fatalf("resize callback got %dx%d, want 200x50", c, r)
}
t.Log("Go client → ServeRW bridge E2E passed")
}
// TestClientAgainstRealMoshServer tests Go client against the C mosh-server.
func TestClientAgainstRealMoshServer(t *testing.T) {
if _, err := exec.LookPath("mosh-server"); err != nil {
t.Skip("mosh-server not installed")
}
// Start real mosh-server — it forks and prints CONNECT line to stdout.
cmd := exec.Command("mosh-server", "new", "-p", "0", "-c", "256")
stdout, err := cmd.StdoutPipe()
if err != nil {
t.Fatal(err)
}
cmd.Stderr = cmd.Stdout // merge stderr into stdout
if err := cmd.Start(); err != nil {
t.Skipf("mosh-server failed to start: %v", err)
}
defer cmd.Process.Kill()
// Read the CONNECT line (mosh-server writes it then the parent exits).
buf := make([]byte, 4096)
var output string
for {
n, err := stdout.Read(buf)
if n > 0 {
output += string(buf[:n])
}
if strings.Contains(output, "MOSH CONNECT") {
break
}
if err != nil {
break
}
}
cmd.Wait()
port, key := parseMoshConnect(output)
if port == 0 || key == "" {
t.Fatalf("bad MOSH CONNECT: %q", output)
}
t.Logf("real mosh-server on port %d", port)
client, err := Dial("127.0.0.1", port, key)
if err != nil {
t.Fatal(err)
}
defer client.Close()
// The real mosh-server expects an initial resize before producing output.
client.Resize(80, 24)
// Wait for shell.
var gotOutput bool
for i := 0; i < 40; i++ {
if recv := client.Recv(500 * time.Millisecond); len(recv) > 0 {
gotOutput = true
break
}
}
if !gotOutput {
t.Fatal("no output from real mosh-server")
}
// Echo a marker.
marker := "GOREAL_" + strconv.FormatInt(time.Now().UnixNano(), 36)
client.Send([]byte("echo " + marker + "\n"))
var allOutput string
deadline := time.After(10 * time.Second)
for {
select {
case <-deadline:
t.Fatalf("marker not echoed. Got: %q", allOutput)
default:
}
recv := client.Recv(500 * time.Millisecond)
if recv != nil {
allOutput += string(recv)
if strings.Contains(clientStripANSI(allOutput), marker) {
t.Log("Go client → real mosh-server E2E passed")
return
}
}
}
}
// TestFastTypingNoDuplication verifies that rapid keystrokes don't cause
// character doubling due to overlapping diffs from the same base.
func TestFastTypingNoDuplication(t *testing.T) {
srv, err := NewServer("/bin/sh", 0, 0)
if err != nil {
t.Fatal(err)
}
go srv.Serve()
defer srv.Close()
<-srv.started
client, err := Dial("127.0.0.1", srv.Port(), srv.KeyBase64())
if err != nil {
t.Fatal(err)
}
defer client.Close()
// Wait for shell prompt.
for i := 0; i < 40; i++ {
if out := client.Recv(500 * time.Millisecond); len(out) > 0 {
break
}
}
// Send "echo TESTMARKER\n" as rapid individual keystrokes with no delay.
cmd := "echo TESTMARKER\n"
for _, ch := range cmd {
client.Send([]byte{byte(ch)})
}
// Collect output until we see TESTMARKER in the echo output.
var allOutput string
deadline := time.After(10 * time.Second)
for {
select {
case <-deadline:
t.Fatalf("marker not echoed. Got: %q", clientStripANSI(allOutput))
default:
}
out := client.Recv(500 * time.Millisecond)
if out != nil {
allOutput += string(out)
stripped := clientStripANSI(allOutput)
if strings.Contains(stripped, "TESTMARKER") {
// Check that TESTMARKER appears but TTEESSTTMMAARRKKEERR does not.
if strings.Contains(stripped, "TTEESSTTMMAARRKKEERR") {
t.Fatalf("character doubling detected: %q", stripped)
}
t.Log("fast typing test passed: no character duplication")
return
}
}
}
}
// parseMoshConnect extracts port and key from mosh-server output.
func parseMoshConnect(output string) (int, string) {
for _, line := range strings.Split(output, "\n") {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "MOSH CONNECT ") {
parts := strings.Fields(line)
if len(parts) >= 4 {
port, err := strconv.Atoi(parts[2])
if err != nil {
continue
}
return port, parts[3]
}
}
}
return 0, ""
}