forked from layeh/radius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket_test.go
123 lines (109 loc) · 2.94 KB
/
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
package radius_test
import (
"bytes"
"github.com/buptczq/radius"
"net"
"testing"
)
func Test_RFC2865_7_1(t *testing.T) {
// Source: https://tools.ietf.org/html/rfc2865#section-7.1
secret := []byte("xyzzy5461")
// Request
request := []byte{
0x01, 0x00, 0x00, 0x38, 0x0f, 0x40, 0x3f, 0x94, 0x73, 0x97, 0x80, 0x57, 0xbd, 0x83, 0xd5, 0xcb,
0x98, 0xf4, 0x22, 0x7a, 0x01, 0x06, 0x6e, 0x65, 0x6d, 0x6f, 0x02, 0x12, 0x0d, 0xbe, 0x70, 0x8d,
0x93, 0xd4, 0x13, 0xce, 0x31, 0x96, 0xe4, 0x3f, 0x78, 0x2a, 0x0a, 0xee, 0x04, 0x06, 0xc0, 0xa8,
0x01, 0x10, 0x05, 0x06, 0x00, 0x00, 0x00, 0x03,
}
p, err := radius.Parse(request, secret)
if err != nil {
t.Fatal(err)
}
if p.Code != radius.CodeAccessRequest {
t.Fatal("expecting Code = PacketCodeAccessRequest")
}
if p.Identifier != 0 {
t.Fatal("expecting Identifier = 0")
}
if p.Len() != 4 {
t.Fatal("expecting 4 attributes")
}
if p.GetByName("User-Name").(string) != "nemo" {
t.Fatal("expecting User-Name = nemo")
}
if p.Get(radius.AttrUserName).(string) != "nemo" {
t.Fatal("expecting User-Name = nemo")
}
password, _ := radius.UserPassword(p.GetRaw(2), p.Secret, p.Authenticator[:])
if string(password) != "arctangent" {
t.Fatal("expecting User-Password = arctangent")
}
if !p.GetByName("NAS-IP-Address").(net.IP).Equal(net.ParseIP("192.168.1.16")) {
t.Fatal("expecting NAS-IP-Address = 192.168.1.16")
}
if p.GetByName("NAS-Port").(uint32) != 3 {
t.Fatal("expecting NAS-Port = 3")
}
{
wire, err := p.Encode()
if err != nil {
t.Fatal(err)
}
if !RADIUSPacketsEqual(wire, request) {
t.Fatal("expecting q.Encode() and request to be equal")
}
}
}
// RADIUSPacketsEqual returns if two RADIUS packets are equal, ignoring the
// order of attributes of different types.
func RADIUSPacketsEqual(a, b []byte) bool {
if len(a) != len(b) {
return false
}
if !bytes.Equal(a[:4], b[:4]) {
return false
}
// hash is going to be different, as the attribute order could change
aa, err := radius.ParseAttributes(a[20:])
if err != nil {
panic(err)
}
ab, err := radius.ParseAttributes(b[20:])
if err != nil {
panic(err)
}
if len(aa) != len(ab) {
return false
}
for typeA, attrsA := range aa {
if len(attrsA) != len(ab[typeA]) {
return false
}
for i, attrA := range attrsA {
if !bytes.Equal([]byte(attrA), []byte(ab[typeA][i])) {
return false
}
}
}
return true
}
func Test_Vendor_Encode(t *testing.T) {
// Aruba
radius.Builtin.Register("Aruba-Essid-Name", 14823, 5, false, 0, radius.AttributeString)
p := radius.New(radius.CodeAccessRequest, []byte("123"))
p.SetByName("Aruba-Essid-Name", "wireless")
wired, err := p.Encode()
if err != nil {
t.Fatal(err)
}
p, err = radius.Parse(wired, []byte("123"))
if err != nil {
t.Fatal(err)
}
if p.Code != radius.CodeAccessRequest {
t.Fatal("expecting Code = PacketCodeAccessRequest")
}
if p.GetByName("Aruba-Essid-Name").(string) != "wireless" {
t.Fatal("expecting Aruba-Essid-Name = wireless")
}
}