MCPcopy
hub / github.com/1Panel-dev/KubePi / TestWriteFromPTY

Function TestWriteFromPTY

thirdparty/gotty/webtty/webtty_test.go:17–69  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

15}
16
17func TestWriteFromPTY(t *testing.T) {
18 connInPipeReader, connInPipeWriter := io.Pipe() // in to conn
19 connOutPipeReader, _ := io.Pipe() // out from conn
20
21 conn := pipePair{
22 connOutPipeReader,
23 connInPipeWriter,
24 }
25 dt, err := New(conn)
26 if err != nil {
27 t.Fatalf("Unexpected error from New(): %s", err)
28 }
29
30 ctx, cancel := context.WithCancel(context.Background())
31 var wg sync.WaitGroup
32 wg.Add(1)
33 go func() {
34 wg.Done()
35 err := dt.Run(ctx)
36 if err != nil {
37 t.Fatalf("Unexpected error from Run(): %s", err)
38 }
39 }()
40
41 message := []byte("foobar")
42 n, err := dt.TTY().Write(message)
43 if err != nil {
44 t.Fatalf("Unexpected error from Write(): %s", err)
45 }
46 if n != len(message) {
47 t.Fatalf("Write() accepted `%d` for message `%s`", n, message)
48 }
49
50 buf := make([]byte, 1024)
51 n, err = connInPipeReader.Read(buf)
52 if err != nil {
53 t.Fatalf("Unexpected error from Read(): %s", err)
54 }
55 if buf[0] != Output {
56 t.Fatalf("Unexpected message type `%c`", buf[0])
57 }
58 decoded := make([]byte, 1024)
59 n, err = base64.StdEncoding.Decode(decoded, buf[1:n])
60 if err != nil {
61 t.Fatalf("Unexpected error from Decode(): %s", err)
62 }
63 if !bytes.Equal(decoded[:n], message) {
64 t.Fatalf("Unexpected message received: `%s`", decoded[:n])
65 }
66
67 cancel()
68 wg.Wait()
69}
70
71func TestWriteFromConn(t *testing.T) {
72 connInPipeReader, connInPipeWriter := io.Pipe() // in to conn

Callers

nothing calls this directly

Calls 5

NewFunction · 0.70
AddMethod · 0.65
RunMethod · 0.45
WriteMethod · 0.45
ReadMethod · 0.45

Tested by

no test coverage detected