forked from layeh/radius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
54 lines (47 loc) · 1.19 KB
/
server_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
package radius_test
import (
"context"
"fmt"
"github.com/buptczq/radius"
"testing"
"time"
)
func TestPacketServer_basic(t *testing.T) {
secret := []byte("123456790")
server := radius.PacketServer{
Addr: "127.0.0.1:1812",
Network: "udp",
SecretSource: radius.StaticSecretSource(secret),
Handler: radius.HandlerFunc(func(w radius.ResponseWriter, r *radius.Request) {
username := r.Packet.GetByName("User-Name").(string)
if username == "tim" {
w.Write(r.Response(radius.CodeAccessAccept))
} else {
w.Write(r.Response(radius.CodeAccessReject))
}
}),
}
var clientErr error
go func() {
defer server.Shutdown(context.Background())
packet := radius.New(radius.CodeAccessRequest, secret)
packet.SetByName("User-Name", "tim")
client := radius.Client{
Retry: time.Millisecond * 50,
}
response, err := client.Exchange(context.Background(), packet, "127.0.0.1:1812")
if err != nil {
clientErr = err
return
}
if response.Code != radius.CodeAccessAccept {
clientErr = fmt.Errorf("expected CodeAccessAccept, got %s\n", response.Code)
}
}()
if err := server.ListenAndServe(); err != nil {
t.Fatal(err)
}
if clientErr != nil {
t.Fail()
}
}