forked from gliderlabs/ssh
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathptyalloc_test.go
More file actions
70 lines (58 loc) · 1.63 KB
/
Copy pathptyalloc_test.go
File metadata and controls
70 lines (58 loc) · 1.63 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
package ssh
import (
"errors"
"sync/atomic"
"testing"
)
// A server that allocates a pty needs to see the allocation fail: the request
// loop refuses the pty-req and carries on, so wrapping the handler is the only
// place left to log why, or to refuse the session outright.
func TestAllocatePtyHandlerCanBeWrapped(t *testing.T) {
t.Parallel()
var (
called atomic.Bool
failed atomic.Bool
)
srv := &Server{
Handler: func(s Session) { _ = s.Exit(0) },
PtyHandler: func(ctx Context, s Session, pty Pty) (func() error, error) {
called.Store(true)
closer, err := AllocatePtyHandler(ctx, s, pty)
if err != nil {
failed.Store(true)
}
return closer, err
},
}
session, _, cleanup := newTestSession(t, srv, nil)
defer cleanup()
if err := session.RequestPty("xterm", 40, 80, nil); err != nil {
t.Skipf("cannot allocate a pty here: %v", err)
}
if !called.Load() {
t.Fatal("the wrapper should have run")
}
if failed.Load() {
t.Fatal("allocation was expected to succeed here")
}
}
func TestWrappingSeesTheAllocationError(t *testing.T) {
t.Parallel()
var seen atomic.Value
srv := &Server{
Handler: func(s Session) { _ = s.Exit(0) },
PtyHandler: func(Context, Session, Pty) (func() error, error) {
err := errors.New("no /dev/ptmx here")
seen.Store(err.Error())
return nil, err
},
}
session, _, cleanup := newTestSession(t, srv, nil)
defer cleanup()
if err := session.RequestPty("xterm", 40, 80, nil); err == nil {
t.Fatal("expected the pty request to be refused")
}
if got, _ := seen.Load().(string); got != "no /dev/ptmx here" {
t.Fatalf("the server never saw the failure, got %q", got)
}
}