forked from AlexMax/charon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto_test.go
358 lines (314 loc) · 9.57 KB
/
proto_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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* Charon: A game authentication server
* Copyright (C) 2014-2015 Alex Mayfield <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package charon
import (
"bytes"
"testing"
)
func TestServerNegotiateMarshall(t *testing.T) {
expected := []byte("\x01\xCA\x03\xD0\x02\xCC\xDD\xEE\xFFusername\x00")
var packet ServerNegotiate
packet.username = "username"
packet.version = 2
packet.clientSession = 4293844428
actual, err := packet.MarshalBinary()
if err != nil {
t.Errorf("%s", err.Error())
}
if !bytes.Equal(expected, actual) {
t.Errorf("Expected: %v Actual: %v", expected, actual)
}
}
func TestServerNegotiateUnmarshall(t *testing.T) {
valid := []byte("\x01\xCA\x03\xD0\x02\xCC\xDD\xEE\xFFusername\x00")
var packet ServerNegotiate
err := packet.UnmarshalBinary(valid)
if err != nil {
t.Errorf("%s", err.Error())
}
if packet.version != 2 {
t.Errorf("Version is %v instead of 2", packet.version)
}
if packet.clientSession != 4293844428 {
t.Errorf("Client Session is %v instead of 4293844428", packet.clientSession)
}
if packet.username != "username" {
t.Errorf("Username is %v instead of username", packet.username)
}
}
func TestServerNegotiateUnmarshallErrors(t *testing.T) {
errors := [][]byte{
// Too short
[]byte("\x01\xCA"),
// Incorrect header
[]byte("\x01\xCA\x03\x00"),
// Incorrect protocol version
[]byte("\x01\xCA\x03\xD0\xFF"),
// No null at end of username
[]byte("\x01\xCA\x03\xD0\x02\xCC\xDD\xEE\xFFusername"),
}
var err error
var packet ServerNegotiate
for _, test := range errors {
err = packet.UnmarshalBinary(test)
if err == nil {
t.Errorf("%v was incorrectly parsed as valid", test)
}
}
}
func TestAuthNegotiateMarshall(t *testing.T) {
expected := []byte("\x10\xCA\x03\xD0\x02\xCC\xDD\xEE\xFF\xFF\xFF\xFF\xFF\x04\x88\x88\x88\x88username\x00")
var packet AuthNegotiate
packet.version = 2
packet.clientSession = 4293844428
packet.session = 4294967295
packet.salt = []byte("\x88\x88\x88\x88")
packet.username = "username"
actual, err := packet.MarshalBinary()
if err != nil {
t.Errorf("%s", err.Error())
}
if !bytes.Equal(expected, actual) {
t.Errorf("Expected: %v Actual: %v", expected, actual)
}
}
func TestAuthNegotiateUnmarshall(t *testing.T) {
valid := []byte("\x10\xCA\x03\xD0\x02\xCC\xDD\xEE\xFF\xFF\xFF\xFF\xFF\x04\x88\x88\x88\x88username\x00")
var packet AuthNegotiate
err := packet.UnmarshalBinary(valid)
if err != nil {
t.Errorf("%s", err.Error())
}
if packet.version != 2 {
t.Errorf("Version is %v instead of 2", packet.version)
}
if packet.clientSession != 4293844428 {
t.Errorf("Client Session is %v instead of 4293844428", packet.clientSession)
}
if packet.session != 4294967295 {
t.Errorf("Session is %v instead of 4294967295", packet.session)
}
if !bytes.Equal(packet.salt, []byte{136, 136, 136, 136}) {
t.Errorf("Salt is %v instead of [136 136 136 136]", packet.salt)
}
if packet.username != "username" {
t.Errorf("Username is %v instead of username", packet.username)
}
}
func TestAuthNegotiateUnmarshallErrors(t *testing.T) {
errors := [][]byte{
// Too short
[]byte("\x10\xCA"),
// Incorrect header
[]byte("\x10\xCA\x03\xD1"),
// Incorrect protocol version
[]byte("\x10\xCA\x03\xD0\xFF"),
// Incorrect salt size
[]byte("\x10\xCA\x03\xD0\x02\xCC\xDD\xEE\xFF\xFF\xFF\xFF\xFF\xEE\x88\x88\x88\x88"),
// Missing username null terminator
[]byte("\x10\xCA\x03\xD0\x02\xCC\xDD\xEE\xFF\xFF\xFF\xFF\xFF\x04\x88\x88\x88\x88username"),
}
var err error
var packet ServerNegotiate
for _, test := range errors {
err = packet.UnmarshalBinary(test)
if err == nil {
t.Errorf("%v was incorrectly parsed as valid", test)
}
}
}
func TestServerEphemeralMarshall(t *testing.T) {
expected := []byte("\x02\xCA\x03\xD0\xFF\xFF\xFF\xFF\x04\x00\x88\x88\x88\x88")
var packet ServerEphemeral
packet.session = 4294967295
packet.ephemeral = []byte("\x88\x88\x88\x88")
actual, err := packet.MarshalBinary()
if err != nil {
t.Errorf("%s", err.Error())
}
if !bytes.Equal(expected, actual) {
t.Errorf("Expected: %v Actual: %v", expected, actual)
}
}
func TestServerEphemeralUnmarshall(t *testing.T) {
valid := []byte("\x02\xCA\x03\xD0\xFF\xFF\xFF\xFF\x04\x00\x88\x88\x88\x88")
var packet ServerEphemeral
err := packet.UnmarshalBinary(valid)
if err != nil {
t.Errorf("%s", err.Error())
}
if 4294967295 != packet.session {
t.Errorf("Session is %v instead of 4294967295", packet.session)
}
if !bytes.Equal([]byte{136, 136, 136, 136}, packet.ephemeral) {
t.Errorf("Ephemeral is %v instead of [136 136 136 136]", packet.ephemeral)
}
}
func TestServerEphemeralUnmarshallErrors(t *testing.T) {
errors := [][]byte{
// Too short
[]byte("\x02\xCA"),
// Incorrect header
[]byte("\x02\xCA\x03\xD1"),
// Incorrect ephemeral size
[]byte("\x02\xCA\x03\xD0\xFF\xFF\xFF\xFF\xEE\xEE\x88\x88\x88\x88"),
}
var err error
var packet ServerEphemeral
for _, test := range errors {
err = packet.UnmarshalBinary(test)
if err == nil {
t.Errorf("%v was incorrectly parsed as valid", test)
}
}
}
func TestAuthEphemeralMarshall(t *testing.T) {
expected := []byte("\x20\xCA\x03\xD0\xFF\xFF\xFF\xFF\x04\x00\x88\x88\x88\x88")
var packet AuthEphemeral
packet.session = 4294967295
packet.ephemeral = []byte("\x88\x88\x88\x88")
actual, err := packet.MarshalBinary()
if err != nil {
t.Errorf("%s", err.Error())
}
if !bytes.Equal(expected, actual) {
t.Errorf("Expected: %v Actual: %v", expected, actual)
}
}
func TestAuthEphemeralUnmarshall(t *testing.T) {
valid := []byte("\x20\xCA\x03\xD0\xFF\xFF\xFF\xFF\x04\x00\x88\x88\x88\x88")
var packet AuthEphemeral
err := packet.UnmarshalBinary(valid)
if err != nil {
t.Errorf("%s", err.Error())
}
if 4294967295 != packet.session {
t.Errorf("Session is %v instead of 4294967295", packet.session)
}
if !bytes.Equal([]byte{136, 136, 136, 136}, packet.ephemeral) {
t.Errorf("Ephemeral is %v instead of [136 136 136 136]", packet.ephemeral)
}
}
func TestAuthEphemeralUnmarshallErrors(t *testing.T) {
errors := [][]byte{
// Too short
[]byte("\x20\xCA"),
// Incorrect header
[]byte("\x20\xCA\x03\xD1"),
// Incorrect ephemeral size
[]byte("\x20\xCA\x03\xD0\xFF\xFF\xFF\xFF\xEE\xEE\x88\x88\x88\x88"),
}
var err error
var packet AuthEphemeral
for _, test := range errors {
err = packet.UnmarshalBinary(test)
if err == nil {
t.Errorf("%v was incorrectly parsed as valid", test)
}
}
}
func TestServerProofMarshall(t *testing.T) {
expected := []byte("\x03\xCA\x03\xD0\xFF\xFF\xFF\xFF\x04\x00\x88\x88\x88\x88")
var packet ServerProof
packet.session = 4294967295
packet.proof = []byte("\x88\x88\x88\x88")
actual, err := packet.MarshalBinary()
if err != nil {
t.Errorf("%s", err.Error())
}
if !bytes.Equal(expected, actual) {
t.Errorf("Expected: %v Actual: %v", expected, actual)
}
}
func TestServerProofUnmarshall(t *testing.T) {
valid := []byte("\x03\xCA\x03\xD0\xFF\xFF\xFF\xFF\x04\x00\x88\x88\x88\x88")
var packet ServerProof
err := packet.UnmarshalBinary(valid)
if err != nil {
t.Errorf("%s", err.Error())
}
if 4294967295 != packet.session {
t.Errorf("Session is %v instead of 4294967295", packet.session)
}
if !bytes.Equal([]byte{136, 136, 136, 136}, packet.proof) {
t.Errorf("Ephemeral is %v instead of [136 136 136 136]", packet.proof)
}
}
func TestServerProofUnmarshallErrors(t *testing.T) {
errors := [][]byte{
// Too short
[]byte("\x03\xCA"),
// Incorrect header
[]byte("\x03\xCA\x03\xD1"),
// Incorrect proof size
[]byte("\x03\xCA\x03\xD0\xFF\xFF\xFF\xFF\xEE\xEE\x88\x88\x88\x88"),
}
var err error
var packet ServerProof
for _, test := range errors {
err = packet.UnmarshalBinary(test)
if err == nil {
t.Errorf("%v was incorrectly parsed as valid", test)
}
}
}
func TestAuthProofMarshall(t *testing.T) {
expected := []byte("\x30\xCA\x03\xD0\xFF\xFF\xFF\xFF\x04\x00\x88\x88\x88\x88")
var packet AuthProof
packet.session = 4294967295
packet.proof = []byte("\x88\x88\x88\x88")
actual, err := packet.MarshalBinary()
if err != nil {
t.Errorf("%s", err.Error())
}
if !bytes.Equal(expected, actual) {
t.Errorf("Expected: %v Actual: %v", expected, actual)
}
}
func TestAuthProofUnmarshall(t *testing.T) {
valid := []byte("\x30\xCA\x03\xD0\xFF\xFF\xFF\xFF\x04\x00\x88\x88\x88\x88")
var packet AuthProof
err := packet.UnmarshalBinary(valid)
if err != nil {
t.Errorf("%s", err.Error())
}
if 4294967295 != packet.session {
t.Errorf("Session is %v instead of 4294967295", packet.session)
}
if !bytes.Equal([]byte{136, 136, 136, 136}, packet.proof) {
t.Errorf("Ephemeral is %v instead of [136 136 136 136]", packet.proof)
}
}
func TestAuthProofUnmarshallErrors(t *testing.T) {
errors := [][]byte{
// Too short
[]byte("\x30\xCA"),
// Incorrect header
[]byte("\x30\xCA\x03\xD1"),
// Incorrect proof size
[]byte("\x30\xCA\x03\xD0\xFF\xFF\xFF\xFF\xEE\xEE\x88\x88\x88\x88"),
}
var err error
var packet AuthProof
for _, test := range errors {
err = packet.UnmarshalBinary(test)
if err == nil {
t.Errorf("%v was incorrectly parsed as valid", test)
}
}
}