-
Notifications
You must be signed in to change notification settings - Fork 143
/
client_test.go
95 lines (85 loc) · 1.85 KB
/
client_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
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"testing"
"github.com/kr/pty"
"github.com/pion/webrtc/v3"
"golang.org/x/crypto/ssh/terminal"
)
func TestClientDataChannelOnMessage(t *testing.T) {
cs := clientSession{}
cs.errChan = make(chan error, 1)
cs.oldTerminalState = &terminal.State{}
onMessage := cs.dataChannelOnMessage()
quitPayload := webrtc.DataChannelMessage{IsString: true, Data: []byte("quit")}
onMessage(quitPayload)
select {
case err := <-cs.errChan:
if err != nil {
t.Error(err)
}
default:
t.Error(errors.New("should return errChan"))
}
stdoutMock := tmpFile()
stdout := os.Stdout
os.Stdout = stdoutMock
binaryPayload := webrtc.DataChannelMessage{IsString: false, Data: []byte("s")}
onMessage(binaryPayload)
os.Stdout = stdout
stdoutMock.Seek(0, 0)
msg, _ := ioutil.ReadAll(stdoutMock)
if string(msg) != "s" {
t.Error("bytes not written to stdout")
}
}
func TestSendTermSize(t *testing.T) {
hs := hostSession{ptmxReady: true}
c := exec.Command("sh")
var err error
hs.ptmx, err = pty.Start(c)
if err != nil {
t.Error(err)
}
if err := pty.Setsize(hs.ptmx, &pty.Winsize{
Rows: 19,
Cols: 29,
X: 9,
Y: 8,
}); err != nil {
t.Error(err)
}
dcSend := func(msg string) error {
onMessage, hs := makeShPty(t)
size, err := pty.GetsizeFull(hs.ptmx)
if err != nil {
t.Error(err)
}
if fmt.Sprintf("%v", size) != "&{0 0 0 0}" {
t.Error("wrong size", size)
}
onMessage(webrtc.DataChannelMessage{IsString: true, Data: []byte(msg)})
select {
case err := <-hs.errChan:
if err != nil {
t.Error(err)
}
default:
}
size, err = pty.GetsizeFull(hs.ptmx)
if err != nil {
t.Error(err)
}
if fmt.Sprintf("%v", size) != "&{19 29 9 8}" {
t.Error("wrong size", size)
}
return nil
}
if err := sendTermSize(hs.ptmx, dcSend); err != nil {
t.Error(err)
}
}