forked from layeh/radius
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server-packet_test.go
187 lines (156 loc) · 4.82 KB
/
server-packet_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package radius
import (
"context"
"fmt"
"net"
"sync/atomic"
"testing"
"time"
)
func TestPacketServer_basic(t *testing.T) {
addr, err := net.ResolveUDPAddr("udp", "localhost:0")
if err != nil {
t.Fatal(err)
}
pc, err := net.ListenUDP("udp", addr)
if err != nil {
t.Fatal(err)
}
secret := []byte("123456790")
const UserNameType = 1
server := PacketServer{
SecretSource: StaticSecretSource(secret),
Handler: HandlerFunc(func(w ResponseWriter, r *Request) {
username := String(r.Get(UserNameType))
if username == "tim" {
w.Write(r.Response(CodeAccessAccept))
} else {
w.Write(r.Response(CodeAccessReject))
}
}),
}
var clientErr error
go func() {
defer server.Shutdown(context.Background())
packet := New(CodeAccessRequest, secret)
username, _ := NewString("tim")
packet.Set(UserNameType, username)
client := Client{
Retry: time.Millisecond * 50,
}
response, err := client.Exchange(context.Background(), packet, pc.LocalAddr().String())
if err != nil {
clientErr = err
return
}
if response.Code != CodeAccessAccept {
clientErr = fmt.Errorf("expected CodeAccessAccept, got %s", response.Code)
}
}()
if err := server.Serve(pc); err != ErrServerShutdown {
t.Fatal(err)
}
server.Shutdown(context.Background())
if clientErr != nil {
t.Fatal(err)
}
}
func TestPacketServer_shutdown(t *testing.T) {
secret := []byte(`12345`)
var handlerState int32
handlerCalled := make(chan struct{})
var server *TestServer
handler := HandlerFunc(func(w ResponseWriter, r *Request) {
close(handlerCalled)
atomic.AddInt32(&handlerState, 1)
<-r.Context().Done()
atomic.AddInt32(&handlerState, 1)
time.Sleep(time.Millisecond * 15)
atomic.AddInt32(&handlerState, 1)
})
server = NewTestServer(handler, StaticSecretSource(secret))
defer server.Close()
req := New(CodeAccessRequest, secret)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
client := Client{
Retry: time.Millisecond * 5,
MaxPacketErrors: 2,
}
client.Exchange(ctx, req, server.Addr)
}()
<-handlerCalled
if err := server.Server.Shutdown(context.Background()); err != nil {
t.Fatalf("got ShutDown error %v; expecting nil", err)
}
if state := atomic.LoadInt32(&handlerState); state != 3 {
t.Fatalf("handlerState = %d; expecting 3", state)
}
}
func TestRequest_context(t *testing.T) {
req := &Request{
Packet: &Packet{},
}
if req.Context() != context.Background() {
t.Fatalf("req.Context() = %v; expecting context.Background()", req.Context())
}
req2 := req.WithContext(context.Background())
if req == req2 {
t.Fatal("expected WithContext requests to differ")
}
if req.Packet != req2.Packet {
t.Fatalf("expected WithContext request packets to be the same")
}
func() {
defer func() {
err := recover()
if err == nil {
t.Fatal("expected recover() to be non-nil")
}
errStr, ok := err.(string)
if !ok || errStr != "nil ctx" {
t.Fatalf("got recover() = %v; expected nil ctx", err)
}
}()
req.WithContext(nil)
}()
}
type dummyPacketConn struct{}
func (dummyPacketConn) ReadFrom(b []byte) (n int, addr net.Addr, err error) { panic("unimplemented") }
func (dummyPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) { panic("unimplemented") }
func (dummyPacketConn) Close() error { panic("unimplemented") }
func (dummyPacketConn) LocalAddr() net.Addr { panic("unimplemented") }
func (dummyPacketConn) SetDeadline(t time.Time) error { panic("unimplemented") }
func (dummyPacketConn) SetReadDeadline(t time.Time) error { panic("unimplemented") }
func (dummyPacketConn) SetWriteDeadline(t time.Time) error { panic("unimplemented") }
func TestPacketServer_singleUse(t *testing.T) {
secret := []byte("12345")
serverCtx, serverCtxCancel := context.WithCancel(context.Background())
handlerReceived := make(chan struct{})
server := NewTestServer(HandlerFunc(func(w ResponseWriter, r *Request) {
close(handlerReceived)
<-serverCtx.Done()
}), StaticSecretSource(secret))
go func() {
packet := New(CodeAccessRequest, secret)
Exchange(serverCtx, packet, server.Addr)
}()
<-handlerReceived
shutdownCtx, shutdownCtxCancel := context.WithTimeout(context.Background(), time.Millisecond*25)
defer shutdownCtxCancel()
err := server.Server.Shutdown(shutdownCtx)
if err != context.DeadlineExceeded {
t.Fatalf("got err %v; expecting context.DeadlineExceeded", err)
}
err = server.Server.Serve(dummyPacketConn{})
if err != ErrServerShutdown {
t.Fatalf("got err %v; expecting ErrServerShutdown", err)
}
serverCtxCancel()
time.Sleep(time.Millisecond * 50) // racy otherwise
err = server.Server.Serve(dummyPacketConn{})
if err != ErrServerShutdown {
t.Fatalf("got err %v; expecting ErrServerShutdown", err)
}
}