-
Notifications
You must be signed in to change notification settings - Fork 2
/
message_test.go
82 lines (75 loc) · 2.03 KB
/
message_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
// SPDX-FileCopyrightText: 2021 Alvar Penning
//
// SPDX-License-Identifier: GPL-3.0-or-later
package xochimilco
import (
"encoding"
"reflect"
"testing"
)
func TestMessageMarshall(t *testing.T) {
testcases := []struct {
t messageType
m encoding.BinaryMarshaler
}{
{
t: sessOffer,
m: &offerMessage{
idKey: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2},
spKey: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2},
spSig: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4},
},
},
{
t: sessAck,
m: &ackMessage{
idKey: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2},
eKey: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2},
cipher: []byte{1, 2, 3, 4, 5, 6, 7},
},
},
{
t: sessData,
m: &dataMessage{1, 2, 3, 4, 5, 6, 7},
},
{
t: sessClose,
m: &closeMessage{0xff},
},
}
for _, testcase := range testcases {
txt, err := marshalMessage(testcase.t, testcase.m)
if err != nil {
t.Fatal(err)
}
ty, m, err := unmarshalMessage(txt)
if err != nil {
t.Fatal(err)
} else if ty != testcase.t {
t.Errorf("unexpected type, %d %d", ty, testcase.t)
} else if !reflect.DeepEqual(m, testcase.m) {
t.Errorf("messages differ, %#v %#v", m, testcase.m)
}
}
}
func TestMessageUnmarshalInvalid(t *testing.T) {
inputs := []string{
"",
Prefix,
Suffix,
Suffix + Prefix,
Prefix + "0" + Suffix,
Prefix + "1" + Suffix,
Prefix + "2" + Suffix,
Prefix + "4" + Suffix,
Prefix + "5" + Suffix,
Prefix + "42" + Suffix,
Prefix + "3💩💩💩" + Suffix,
}
for _, input := range inputs {
_, _, err := unmarshalMessage(input)
if err == nil {
t.Errorf("%s did not error", input)
}
}
}