-
Notifications
You must be signed in to change notification settings - Fork 181
/
client_test.go
162 lines (138 loc) · 4.08 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package radius
import (
"context"
"strings"
"sync/atomic"
"testing"
"time"
)
func TestClient_Exchange_expired(t *testing.T) {
handler := HandlerFunc(func(w ResponseWriter, r *Request) {
// ignore
})
server := NewTestServer(handler, StaticSecretSource([]byte(`12345`)))
defer server.Close()
ctx, cancel := context.WithTimeout(context.Background(), -time.Hour)
defer cancel()
req := New(CodeAccessRequest, []byte(`secret`))
client := Client{}
resp, err := client.Exchange(ctx, req, server.Addr)
if resp != nil {
t.Fatalf("got non-nil response (%v); expected nil", resp)
}
if err == nil {
t.Fatal("got nil error; expected one")
}
if err != context.DeadlineExceeded {
t.Fatalf("got err = %v; expected context.DeadlineExceeded", err)
}
}
func TestClient_Exchange_retry(t *testing.T) {
secret := []byte(`12345`)
var attempts int32
handler := HandlerFunc(func(w ResponseWriter, r *Request) {
if atomic.AddInt32(&attempts, 1) == 4 {
w.Write(r.Response(CodeAccessAccept))
}
})
server := NewTestServer(handler, StaticSecretSource(secret))
defer server.Close()
req := New(CodeAccessRequest, secret)
client := Client{
Retry: time.Millisecond * 5,
}
resp, err := client.Exchange(context.Background(), req, server.Addr)
if err != nil {
t.Fatalf("got err %s; expected nil", err)
}
if resp.Code != CodeAccessAccept {
t.Fatalf("got code %s; expected %s", resp.Code, CodeAccessAccept)
}
if attempts := atomic.LoadInt32(&attempts); attempts != 4 {
t.Fatalf("response received in %d attemps; expecting 4", attempts)
}
}
func TestClient_Exchange_cancelled(t *testing.T) {
secret := []byte(`12345`)
handler := HandlerFunc(func(w ResponseWriter, r *Request) {
// ignore
})
server := NewTestServer(handler, StaticSecretSource(secret))
defer server.Close()
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(time.Millisecond * 50)
cancel()
}()
req := New(CodeAccessRequest, secret)
client := Client{
Retry: time.Millisecond * 5,
}
resp, err := client.Exchange(ctx, req, server.Addr)
if resp != nil {
t.Fatalf("got non-nil response (%v); expected nil", resp)
}
if err != context.Canceled {
t.Fatalf("got error = %v; expecting context.Canceled", err)
}
}
func TestClient_Exchange_invalidPacket(t *testing.T) {
secret := []byte(`12345`)
var server *TestServer
handler := HandlerFunc(func(w ResponseWriter, r *Request) {
// write bad data to client
server.l.WriteTo([]byte(`AAAA`), r.RemoteAddr)
})
server = NewTestServer(handler, StaticSecretSource(secret))
defer server.Close()
req := New(CodeAccessRequest, secret)
client := Client{
Retry: time.Millisecond * 5,
MaxPacketErrors: 2,
}
resp, err := client.Exchange(context.Background(), req, server.Addr)
if resp != nil {
t.Fatalf("got non-nil response (%v); expected nil", resp)
}
if expecting := `packet not at least 20 bytes long`; !strings.Contains(err.Error(), expecting) {
t.Fatalf("got error = %v; expecting %s", err, expecting)
}
}
func TestClient_Exchange_nonauthenticPacket(t *testing.T) {
secret := []byte(`12345`)
var server *TestServer
handler := HandlerFunc(func(w ResponseWriter, r *Request) {
resp := r.Response(CodeAccessAccept)
resp.Authenticator = [16]byte{}
w.Write(resp)
})
server = NewTestServer(handler, StaticSecretSource(secret))
defer server.Close()
req := New(CodeAccessRequest, secret)
client := Client{
Retry: time.Millisecond * 5,
MaxPacketErrors: 2,
}
resp, err := client.Exchange(context.Background(), req, server.Addr)
if resp != nil {
t.Fatalf("got non-nil response (%v); expected nil", resp)
}
if _, ok := err.(*NonAuthenticResponseError); !ok {
t.Fatalf("got error %T; expecting NonAuthenticResponseError", err)
}
}
func TestClient_Exchange_nilContext(t *testing.T) {
defer func() {
err := recover()
if err == nil {
t.Fatalf("got nil recover; expected value")
}
errStr, ok := err.(string)
if !ok || errStr != "nil context" {
panic(err)
}
}()
req := New(CodeAccessRequest, []byte(``))
//lint:ignore SA1012 This test is specifically checking for a nil context
Exchange(nil, req, "")
}