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
|
package sshd
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/ssh"
)
type fakeNewChannel struct {
channelType string
extraData []byte
}
func (f *fakeNewChannel) Accept() (ssh.Channel, <-chan *ssh.Request, error) {
return nil, nil, nil
}
func (f *fakeNewChannel) Reject(reason ssh.RejectionReason, message string) error {
return nil
}
func (f *fakeNewChannel) ChannelType() string {
return f.channelType
}
func (f *fakeNewChannel) ExtraData() []byte {
return f.extraData
}
func TestPanicDuringSessionIsRecovered(t *testing.T) {
numSessions := 0
conn := newConnection(1, "127.0.0.1:50000")
newChannel := &fakeNewChannel{channelType: "session"}
chans := make(chan ssh.NewChannel, 1)
chans <- newChannel
require.NotPanics(t, func() {
conn.handle(context.Background(), chans, func(context.Context, ssh.Channel, <-chan *ssh.Request) {
numSessions += 1
close(chans)
panic("This is a panic")
})
})
require.Equal(t, numSessions, 1)
}
|