forked from layeh/radius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attribute_test.go
137 lines (120 loc) · 2.74 KB
/
attribute_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
package radius
import (
"bytes"
"crypto/rand"
"encoding/hex"
"net"
"testing"
)
func TestNewUserPassword_length(t *testing.T) {
tbl := []struct {
Password string
EncodedLength int
}{
{"", 16},
{"abc", 16},
{"0123456789abcde", 16},
{"0123456789abcdef", 16},
{"0123456789abcdef0", 16 * 2},
{"0123456789abcdef0123456789abcdef0123456789abcdef", 16 * 3},
}
secret := []byte(`12345`)
ra := []byte(`0123456789abcdef`)
for _, x := range tbl {
attr, err := NewUserPassword([]byte(x.Password), secret, ra)
if err != nil {
t.Fatal(err)
}
if len(attr) != x.EncodedLength {
t.Fatalf("expected encoded length of %#v = %d, got %d", x.Password, x.EncodedLength, len(attr))
}
}
}
func TestTunnelPassword(t *testing.T) {
roundtrip := []string{
"",
"a",
"Hello",
"0123456789abcde",
"0123456789abcdef",
"0123456789abcdef0123456789abcdef0123456789abcdef",
}
salt := []byte{0x83, 0x45}
secret := []byte("secret")
var requestAuthenticator [16]byte
if _, err := rand.Read(requestAuthenticator[:]); err != nil {
t.Fatal(err)
}
for _, password := range roundtrip {
t.Run(password, func(t *testing.T) {
password := []byte(password)
a, err := NewTunnelPassword(password, salt, secret, requestAuthenticator[:])
if err != nil {
t.Fatalf("unexpected NewTunnelPassword error %s", err)
}
decryptedPassword, decryptedSalt, err := TunnelPassword(a, secret, requestAuthenticator[:])
if err != nil {
t.Fatalf("unexpected TunnelPassword error %s", err)
}
if !bytes.Equal(password, decryptedPassword) {
t.Fatalf("got password %s; expecting %s", decryptedPassword, password)
}
if !bytes.Equal(salt, decryptedSalt) {
t.Fatalf("got salt %s; expecting %s", decryptedSalt, salt)
}
})
}
}
func TestIPv6Prefix(t *testing.T) {
tests := []struct {
Mask string
Expected string
}{
{
Mask: "3a00::/8",
Expected: "00083a",
},
{
Mask: "3A11::/4",
Expected: "000430",
},
{
Mask: "3F11::/5",
Expected: "000538",
},
}
for _, tt := range tests {
t.Run(tt.Mask, func(t *testing.T) {
_, network, err := net.ParseCIDR(tt.Mask)
if err != nil {
t.Fatal(err)
}
attr, err := NewIPv6Prefix(network)
if err != nil {
t.Fatal(err)
}
if enc := hex.EncodeToString(attr); tt.Expected != enc {
t.Fatalf("got %s, expected %s", enc, tt.Expected)
}
ipNet, err := IPv6Prefix(attr)
if err != nil {
t.Fatal(err)
}
if !ipNetEquals(network, ipNet) {
t.Fatalf("got %v, expected %v", ipNet, network)
}
})
}
}
func ipNetEquals(a, b *net.IPNet) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
if a.Mask.String() != b.Mask.String() {
return false
}
return a.IP.Equal(b.IP)
}