-
Notifications
You must be signed in to change notification settings - Fork 98
/
session_serve_test.go
142 lines (119 loc) · 3.94 KB
/
session_serve_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
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
package remotedialer
import (
"bytes"
"context"
"errors"
"fmt"
"math/rand"
"net"
"reflect"
"strings"
"testing"
"time"
)
func TestSession_clientConnect(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
msgProto, msgAddr := "testproto", "testaddr"
s := setupDummySession(t, 0)
s.auth = func(proto, address string) bool { return proto == msgProto && address == msgAddr }
dialerC := make(chan struct{})
s.dialer = func(ctx context.Context, network, address string) (net.Conn, error) {
close(dialerC)
clientConn, _ := net.Pipe()
return clientConn, nil
}
connID := getDummyConnectionID()
if err := s.clientConnect(ctx, newConnect(connID, msgProto, msgAddr)); err != nil {
t.Fatal(err)
}
select {
case <-dialerC:
case <-time.After(1 * time.Second):
t.Errorf("timed out waiting for dialer")
}
if conn := s.getConnection(connID); conn == nil {
t.Errorf("Connection not found in session for ID %d", connID)
}
}
func TestSession_addRemoveRemoteClient(t *testing.T) {
s := setupDummySession(t, 0)
clientKey, sessionKey := "test", rand.Int()
msgAddress := fmt.Sprintf("%s/%d", clientKey, sessionKey)
if err := s.addRemoteClient(msgAddress); err != nil {
t.Fatal(err)
}
if got, want := s.getSessionKeys(clientKey), map[int]bool{sessionKey: true}; !reflect.DeepEqual(got, want) {
t.Errorf("remote client session was not added correctly, got %v, want %v", got, want)
}
if err := s.removeRemoteClient(msgAddress); err != nil {
t.Fatal(err)
}
if got, want := s.getSessionKeys(clientKey), 0; len(got) != want {
t.Errorf("remote client session was not removed correctly, got %v, want len(%d)", got, want)
}
}
func TestSession_connectionData(t *testing.T) {
s := setupDummySession(t, 0)
connID := getDummyConnectionID()
conn := newConnection(connID, s, "test", "test")
s.addConnection(connID, conn)
data := "testing!"
s.connectionData(connID, strings.NewReader(data))
if got, want := conn.buffer.offerCount, int64(len(data)); got != want {
t.Errorf("incorrect data length, got %d, want %d", got, want)
}
buf := make([]byte, conn.buffer.offerCount)
if _, err := conn.buffer.Read(buf); err != nil {
t.Fatal(err)
}
if got, want := string(buf), data; got != want {
t.Errorf("incorrect data, got %q, want %q", got, want)
}
}
func TestSession_pauseResumeConnection(t *testing.T) {
s := setupDummySession(t, 0)
connID := getDummyConnectionID()
conn := newConnection(connID, s, "test", "test")
s.addConnection(connID, conn)
s.pauseConnection(connID)
if !conn.backPressure.paused {
t.Errorf("connection was not paused correctly")
}
s.resumeConnection(connID)
if conn.backPressure.paused {
t.Errorf("connection was not resumed correctly")
}
}
func TestSession_closeConnection(t *testing.T) {
s := setupDummySession(t, 0)
var msg *message
s.conn = &fakeWSConn{
writeMessageCallback: func(msgType int, deadline time.Time, data []byte) (err error) {
if !deadline.IsZero() && deadline.Before(time.Now()) {
return errors.New("deadline exceeded")
}
msg, err = newServerMessage(bytes.NewReader(data))
return
},
}
connID := getDummyConnectionID()
conn := newConnection(connID, s, "test", "test")
s.addConnection(connID, conn)
// Ensure Error message is sent regardless of the WriteDeadline value, see https://github.com/rancher/remotedialer/pull/79
_ = conn.SetWriteDeadline(time.Now())
expectedErr := errors.New("connection closed")
s.closeConnection(connID, expectedErr)
if s.getConnection(connID) != nil {
t.Errorf("connection was not closed correctly")
}
if conn.err == nil || msg == nil {
t.Fatal("message not sent on closed connection")
} else if msg.messageType != Error {
t.Errorf("incorrect message type sent")
} else if got, want := msg.Err().Error(), expectedErr.Error(); got != want {
t.Errorf("wrong error, got %v, want %v", got, want)
} else if got, want := conn.err, expectedErr; !errors.Is(got, want) {
t.Errorf("wrong error, got %v, want %v", got, want)
}
}