Skip to content

Commit f5f05f7

Browse files
committed
test: encode/decode unit tests for handshake messages
Fixes #67
1 parent 289bdec commit f5f05f7

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

protocol/handshake/messages_test.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,41 @@ type testDefinition struct {
1414
MessageType uint
1515
}
1616

17-
// TODO: implement tests for more messages
18-
var tests = []testDefinition{}
17+
var tests = []testDefinition{
18+
{
19+
CborHex: "8200a4078202f4088202f4098202f40a8202f4",
20+
MessageType: MESSAGE_TYPE_PROPOSE_VERSIONS,
21+
Message: NewMsgProposeVersions(
22+
map[uint16]interface{}{
23+
7: []interface{}{uint64(2), false},
24+
8: []interface{}{uint64(2), false},
25+
9: []interface{}{uint64(2), false},
26+
10: []interface{}{uint64(2), false},
27+
},
28+
),
29+
},
30+
{
31+
CborHex: "83010a8202f4",
32+
MessageType: MESSAGE_TYPE_ACCEPT_VERSION,
33+
Message: NewMsgAcceptVersion(10, []interface{}{uint64(2), false}),
34+
},
35+
{
36+
CborHex: "82028200840708090a",
37+
MessageType: MESSAGE_TYPE_REFUSE,
38+
Message: NewMsgRefuse(
39+
[]interface{}{
40+
uint64(REFUSE_REASON_VERSION_MISMATCH),
41+
[]interface{}{
42+
uint64(7),
43+
uint64(8),
44+
uint64(9),
45+
uint64(10),
46+
},
47+
},
48+
),
49+
},
50+
// TODO: add more tests for other refusal types
51+
}
1952

2053
func TestDecode(t *testing.T) {
2154
for _, test := range tests {
@@ -30,7 +63,7 @@ func TestDecode(t *testing.T) {
3063
// Set the raw CBOR so the comparison should succeed
3164
test.Message.SetCbor(cborData)
3265
if !reflect.DeepEqual(msg, test.Message) {
33-
t.Fatalf("CBOR did not decode to expected message object\n got: %#v\n wanted: %#v", msg, test.Message)
66+
t.Fatalf("CBOR did not decode to expected message object\n got: %#v\n wanted: %#v", msg, test.Message)
3467
}
3568
}
3669
}
@@ -43,7 +76,7 @@ func TestEncode(t *testing.T) {
4376
}
4477
cborHex := hex.EncodeToString(cborData)
4578
if cborHex != test.CborHex {
46-
t.Fatalf("message did not encode to expected CBOR\n got: %s\n wanted: %s", cborHex, test.CborHex)
79+
t.Fatalf("message did not encode to expected CBOR\n got: %s\n wanted: %s", cborHex, test.CborHex)
4780
}
4881
}
4982
}

0 commit comments

Comments
 (0)