forked from irumiha/radius
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclntsrv_it_test.go
169 lines (153 loc) · 4.62 KB
/
clntsrv_it_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
//go:build integration
// +build integration
/*
Integration tests between radigo client and server
*/
package radigo
import (
"strings"
"testing"
)
var (
dict *Dictionary
stopChan = make(chan struct{})
)
func handleAuth(req *Packet) (rpl *Packet, err error) {
rpl = req.Reply()
rpl.AVPs = append(rpl.AVPs, req.AVPs...)
rpl.Code = AccessAccept
return
}
func handleAcct(req *Packet) (rpl *Packet, err error) {
rpl = req.Reply()
rpl.AVPs = append(rpl.AVPs, req.AVPs...)
rpl.Code = AccountingResponse
return
}
func TestRadServerStart(t *testing.T) {
freeRADIUSDocDictSample := `
# Most of the lines are copied from freeradius documentation here:
# http://networkradius.com/doc/3.0.10/concepts/dictionary/introduction.html
# Attributes
ATTRIBUTE User-Name 1 string
ATTRIBUTE User-Password 2 string
# Alias values
VALUE Framed-Protocol PPP 1
# Vendors
VENDOR Cisco 9
VENDOR Microsoft 311
# Vendor AVPs
BEGIN-VENDOR Cisco
ATTRIBUTE Cisco-AVPair 1 string
ATTRIBUTE Cisco-NAS-Port 2 string
END-VENDOR Cisco
`
dict = RFC2865Dictionary()
// Load some VSA for our tests
if err := dict.ParseFromReader(strings.NewReader(freeRADIUSDocDictSample)); err != nil {
t.Error(err)
}
secrets := NewSecrets(map[string]string{"127.0.0.1": "CGRateS.org"})
dicts := NewDictionaries(map[string]*Dictionary{"127.0.0.1": RFC2865Dictionary()})
go NewServer("tcp", "localhost:1812",
secrets, dicts,
map[PacketCode]func(*Packet) (*Packet, error){AccessRequest: handleAuth},
nil, nil).ListenAndServe(stopChan)
go NewServer("tcp", "localhost:1813",
secrets, dicts,
map[PacketCode]func(*Packet) (*Packet, error){AccountingRequest: handleAcct},
nil, nil).ListenAndServe(stopChan)
}
func TestRadClientAuth(t *testing.T) {
authClnt, err := NewClient("tcp", "localhost:1812", "CGRateS.org", dict, 0, nil, nil)
if err != nil {
t.Error(err)
}
req := authClnt.NewRequest(AccessRequest, 1)
if err := req.AddAVPWithName("User-Name", "flopsy", ""); err != nil {
t.Error(err)
}
if err := req.AddAVPWithName("Cisco-NAS-Port", "CGR1", "Cisco"); err != nil {
t.Error(err)
}
reply, err := authClnt.SendRequest(req)
if err != nil {
t.Error(err)
}
if reply.Code != AccessAccept {
t.Errorf("Received reply: %+v", reply)
}
if len(reply.AVPs) != len(req.AVPs) {
t.Errorf("Expecting: %+v, received: %+v", req.AVPs, reply.AVPs)
}
if avps := reply.AttributesWithName("User-Name", ""); len(avps) != 1 {
t.Errorf("Unexpected AVPs: %+v", avps)
} else if req.AVPs[0].Name != avps[0].Name {
t.Errorf("Expecting: %+v, received: %+v", req.AVPs[0].Name, avps[0].Name)
}
if avps := reply.AttributesWithName("Cisco-NAS-Port", "Cisco"); len(avps) != 1 {
t.Errorf("Unexpected AVPs: %+v", avps)
} else if req.AVPs[1].Name != avps[0].Name {
t.Errorf("Expecting: %+v, received: %+v", req.AVPs[1].Name, avps[0].Name)
}
}
func TestRadClientAccount(t *testing.T) {
acntClnt, err := NewClient("tcp", "localhost:1813", "CGRateS.org", dict, 0, nil, nil)
if err != nil {
t.Error(err)
}
req := acntClnt.NewRequest(AccountingRequest, 2)
if err := req.AddAVPWithName("User-Name", "flopsy", ""); err != nil {
t.Error(err)
}
if err := req.AddAVPWithName("Cisco-NAS-Port", "CGR1", "Cisco"); err != nil {
t.Error(err)
}
reply, err := acntClnt.SendRequest(req)
if err != nil {
t.Error(err)
}
if reply.Code != AccountingResponse {
t.Errorf("Received reply: %+v", reply)
}
if len(reply.AVPs) != len(req.AVPs) {
t.Errorf("Expecting: %+v, received: %+v", req.AVPs, reply.AVPs)
}
}
func TestRadClientAuthDifferentSecret(t *testing.T) {
// for this test the verify for authenticity is done on client side
// client.go line 143
authClnt, err := NewClient("tcp", "localhost:1812", "InvalidSecret", dict, 0, nil, nil)
if err != nil {
t.Error(err)
}
req := authClnt.NewRequest(AccessRequest, 1)
if err := req.AddAVPWithName("User-Name", "flopsy", ""); err != nil {
t.Error(err)
}
if err := req.AddAVPWithName("Cisco-NAS-Port", "CGR1", "Cisco"); err != nil {
t.Error(err)
}
_, err = authClnt.SendRequest(req)
if err == nil || err.Error() != "invalid packet" {
t.Error(err)
}
}
func TestRadClientAccountDifferentSecret(t *testing.T) {
acntClnt, err := NewClient("tcp", "localhost:1813", "InvalidSecret", dict, 0, nil, nil)
if err != nil {
t.Error(err)
}
req := acntClnt.NewRequest(AccountingRequest, 2)
if err := req.AddAVPWithName("User-Name", "flopsy", ""); err != nil {
t.Error(err)
}
if err := req.AddAVPWithName("Cisco-NAS-Port", "CGR1", "Cisco"); err != nil {
t.Error(err)
}
_, err = acntClnt.SendRequest(req)
if err == nil || err.Error() != "invalid packet" {
t.Error(err)
}
close(stopChan)
}