This repository has been archived by the owner on Jun 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
textattrs_test.go
263 lines (242 loc) · 5.61 KB
/
textattrs_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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package stun
import (
"strings"
"testing"
)
func TestSoftware_GetFrom(t *testing.T) {
m := New()
v := "Client v0.0.1"
m.Add(AttrSoftware, []byte(v))
m.WriteHeader()
m2 := &Message{
Raw: make([]byte, 0, 256),
}
software := new(Software)
if _, err := m2.ReadFrom(m.reader()); err != nil {
t.Error(err)
}
if err := software.GetFrom(m); err != nil {
t.Fatal(err)
}
if software.String() != v {
t.Errorf("Expected %q, got %q.", v, software)
}
sAttr, ok := m.Attributes.Get(AttrSoftware)
if !ok {
t.Error("sowfware attribute should be found")
}
s := sAttr.String()
if !strings.HasPrefix(s, "SOFTWARE:") {
t.Error("bad string representation", s)
}
}
func TestSoftware_AddTo_Invalid(t *testing.T) {
m := New()
s := make(Software, 1024)
if err := s.AddTo(m); !IsAttrSizeOverflow(err) {
t.Errorf("AddTo should return *AttrOverflowErr, got: %v", err)
}
if err := s.GetFrom(m); err != ErrAttributeNotFound {
t.Errorf("GetFrom should return %q, got: %v", ErrAttributeNotFound, err)
}
}
func TestSoftware_AddTo_Regression(t *testing.T) {
// s.AddTo checked len(m.Raw) instead of len(s.Raw).
m := &Message{Raw: make([]byte, 2048)}
s := make(Software, 100)
if err := s.AddTo(m); err != nil {
t.Errorf("AddTo should return <nil>, got: %v", err)
}
}
func BenchmarkUsername_AddTo(b *testing.B) {
b.ReportAllocs()
m := new(Message)
u := Username("test")
for i := 0; i < b.N; i++ {
if err := u.AddTo(m); err != nil {
b.Fatal(err)
}
m.Reset()
}
}
func BenchmarkUsername_GetFrom(b *testing.B) {
b.ReportAllocs()
m := new(Message)
Username("test").AddTo(m)
var u Username
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := u.GetFrom(m); err != nil {
b.Fatal(err)
}
u = u[:0]
}
}
func TestUsername(t *testing.T) {
username := "username"
u := NewUsername(username)
m := new(Message)
m.WriteHeader()
t.Run("Bad length", func(t *testing.T) {
badU := make(Username, 600)
if err := badU.AddTo(m); !IsAttrSizeOverflow(err) {
t.Errorf("AddTo should return *AttrOverflowErr, got: %v", err)
}
})
t.Run("AddTo", func(t *testing.T) {
if err := u.AddTo(m); err != nil {
t.Error("errored:", err)
}
t.Run("GetFrom", func(t *testing.T) {
got := new(Username)
if err := got.GetFrom(m); err != nil {
t.Error("errored:", err)
}
if got.String() != username {
t.Errorf("expedted: %s, got: %s", username, got)
}
t.Run("Not found", func(t *testing.T) {
m := new(Message)
u := new(Username)
if err := u.GetFrom(m); err != ErrAttributeNotFound {
t.Error("Should error")
}
})
})
})
t.Run("No allocations", func(t *testing.T) {
m := new(Message)
m.WriteHeader()
u := NewUsername("username")
if allocs := testing.AllocsPerRun(10, func() {
if err := u.AddTo(m); err != nil {
t.Error(err)
}
m.Reset()
}); allocs > 0 {
t.Errorf("got %f allocations, zero expected", allocs)
}
})
}
func TestRealm_GetFrom(t *testing.T) {
m := New()
v := "realm"
m.Add(AttrRealm, []byte(v))
m.WriteHeader()
m2 := &Message{
Raw: make([]byte, 0, 256),
}
r := new(Realm)
if err := r.GetFrom(m2); err != ErrAttributeNotFound {
t.Errorf("GetFrom should return %q, got: %v", ErrAttributeNotFound, err)
}
if _, err := m2.ReadFrom(m.reader()); err != nil {
t.Error(err)
}
if err := r.GetFrom(m); err != nil {
t.Fatal(err)
}
if r.String() != v {
t.Errorf("Expected %q, got %q.", v, r)
}
rAttr, ok := m.Attributes.Get(AttrRealm)
if !ok {
t.Error("realm attribute should be found")
}
s := rAttr.String()
if !strings.HasPrefix(s, "REALM:") {
t.Error("bad string representation", s)
}
}
func TestRealm_AddTo_Invalid(t *testing.T) {
m := New()
r := make(Realm, 1024)
if err := r.AddTo(m); !IsAttrSizeOverflow(err) {
t.Errorf("AddTo should return *AttrOverflowErr, got: %v", err)
}
if err := r.GetFrom(m); err != ErrAttributeNotFound {
t.Errorf("GetFrom should return %q, got: %v", ErrAttributeNotFound, err)
}
}
func TestNonce_GetFrom(t *testing.T) {
m := New()
v := "example.org"
m.Add(AttrNonce, []byte(v))
m.WriteHeader()
m2 := &Message{
Raw: make([]byte, 0, 256),
}
var nonce Nonce
if _, err := m2.ReadFrom(m.reader()); err != nil {
t.Error(err)
}
if err := nonce.GetFrom(m); err != nil {
t.Fatal(err)
}
if nonce.String() != v {
t.Errorf("Expected %q, got %q.", v, nonce)
}
nAttr, ok := m.Attributes.Get(AttrNonce)
if !ok {
t.Error("nonce attribute should be found")
}
s := nAttr.String()
if !strings.HasPrefix(s, "NONCE:") {
t.Error("bad string representation", s)
}
}
func TestNonce_AddTo_Invalid(t *testing.T) {
m := New()
n := make(Nonce, 1024)
if err := n.AddTo(m); !IsAttrSizeOverflow(err) {
t.Errorf("AddTo should return *AttrOverflowErr, got: %v", err)
}
if err := n.GetFrom(m); err != ErrAttributeNotFound {
t.Errorf("GetFrom should return %q, got: %v", ErrAttributeNotFound, err)
}
}
func TestNonce_AddTo(t *testing.T) {
m := New()
n := Nonce("example.org")
if err := n.AddTo(m); err != nil {
t.Error(err)
}
v, err := m.Get(AttrNonce)
if err != nil {
t.Error(err)
}
if string(v) != "example.org" {
t.Errorf("bad nonce %q", v)
}
}
func BenchmarkNonce_AddTo(b *testing.B) {
b.ReportAllocs()
m := New()
n := NewNonce("nonce")
for i := 0; i < b.N; i++ {
if err := n.AddTo(m); err != nil {
b.Fatal(err)
}
m.Reset()
}
}
func BenchmarkNonce_AddTo_BadLength(b *testing.B) {
b.ReportAllocs()
m := New()
n := make(Nonce, 2048)
for i := 0; i < b.N; i++ {
if err := n.AddTo(m); err == nil {
b.Fatal("should error")
}
m.Reset()
}
}
func BenchmarkNonce_GetFrom(b *testing.B) {
b.ReportAllocs()
m := New()
n := NewNonce("nonce")
n.AddTo(m)
for i := 0; i < b.N; i++ {
n.GetFrom(m)
}
}