Skip to content

Commit 71540b6

Browse files
committed
Unit test framework for protocol message CBOR encoding/decoding
Fixes #63
1 parent 33aaa56 commit 71540b6

File tree

10 files changed

+426
-5
lines changed

10 files changed

+426
-5
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ jobs:
2020
runs-on: ubuntu-latest
2121
steps:
2222
- uses: actions/checkout@v2
23+
- uses: actions/setup-go@v3
24+
with:
25+
go-version: '<1.18'
2326
- name: golangci-lint
24-
uses: golangci/golangci-lint-action@v2
27+
uses: golangci/golangci-lint-action@v3
2528
with:
2629
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
2730
version: v1.43 # current version at time of commit
@@ -35,9 +38,6 @@ jobs:
3538
# Optional: show only new issues if it's a pull request. The default value is `false`.
3639
# only-new-issues: true
3740

38-
# Optional: if set to true then the action will use pre-installed Go.
39-
# skip-go-installation: true
40-
4141
# Optional: if set to true then the action don't cache or restore ~/go/pkg.
4242
# skip-pkg-cache: true
4343

.github/workflows/pr.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: pr
2+
on:
3+
pull_request:
4+
5+
jobs:
6+
run-tests:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
- uses: actions/setup-go@v3
11+
with:
12+
go-version: '<1.18'
13+
- run: |
14+
make test
15+
16+
build-test-program:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v2
20+
- uses: actions/setup-go@v3
21+
with:
22+
go-version: '<1.18'
23+
- run: |
24+
make

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ $(BINARY): $(GO_FILES)
1313
go mod tidy
1414
go build -o $(BINARY) ./cmd/$(BINARY)
1515

16-
.PHONY: build image
16+
.PHONY: build clean test
1717

1818
# Alias for building program binary
1919
build: $(BINARY)
2020

2121
clean:
2222
rm -f $(BINARY)
23+
24+
test:
25+
go test -v ./...
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package blockfetch
2+
3+
import (
4+
"encoding/hex"
5+
"github.com/cloudstruct/go-ouroboros-network/protocol"
6+
"github.com/cloudstruct/go-ouroboros-network/utils"
7+
"reflect"
8+
"testing"
9+
)
10+
11+
type testDefinition struct {
12+
CborHex string
13+
Message protocol.Message
14+
MessageType uint
15+
}
16+
17+
// TODO: implement tests for more messages
18+
var tests = []testDefinition{
19+
{
20+
CborHex: "8105",
21+
Message: NewMsgBatchDone(),
22+
MessageType: MESSAGE_TYPE_BATCH_DONE,
23+
},
24+
}
25+
26+
func TestDecode(t *testing.T) {
27+
for _, test := range tests {
28+
cborData, err := hex.DecodeString(test.CborHex)
29+
if err != nil {
30+
t.Fatalf("failed to decode CBOR hex: %s", err)
31+
}
32+
msg, err := NewMsgFromCbor(test.MessageType, cborData)
33+
if err != nil {
34+
t.Fatalf("failed to decode CBOR: %s", err)
35+
}
36+
// Set the raw CBOR so the comparison should succeed
37+
test.Message.SetCbor(cborData)
38+
if !reflect.DeepEqual(msg, test.Message) {
39+
t.Fatalf("CBOR did not decode to expected message object\n got: %#v\n wanted: %#v", msg, test.Message)
40+
}
41+
}
42+
}
43+
44+
func TestEncode(t *testing.T) {
45+
for _, test := range tests {
46+
cborData, err := utils.CborEncode(test.Message)
47+
if err != nil {
48+
t.Fatalf("failed to encode message to CBOR: %s", err)
49+
}
50+
cborHex := hex.EncodeToString(cborData)
51+
if cborHex != test.CborHex {
52+
t.Fatalf("message did not encode to expected CBOR\n got: %s\n wanted: %s", cborHex, test.CborHex)
53+
}
54+
}
55+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package chainsync
2+
3+
import (
4+
"encoding/hex"
5+
"github.com/cloudstruct/go-ouroboros-network/protocol"
6+
"github.com/cloudstruct/go-ouroboros-network/utils"
7+
"reflect"
8+
"testing"
9+
)
10+
11+
type testDefinition struct {
12+
CborHex string
13+
Message protocol.Message
14+
MessageType uint
15+
ProtocolMode protocol.ProtocolMode
16+
}
17+
18+
// TODO: implement tests for more messages
19+
var tests = []testDefinition{
20+
{
21+
CborHex: "8107",
22+
Message: NewMsgDone(),
23+
MessageType: MESSAGE_TYPE_DONE,
24+
},
25+
}
26+
27+
func TestDecode(t *testing.T) {
28+
for _, test := range tests {
29+
cborData, err := hex.DecodeString(test.CborHex)
30+
if err != nil {
31+
t.Fatalf("failed to decode CBOR hex: %s", err)
32+
}
33+
msg, err := NewMsgFromCbor(test.ProtocolMode, test.MessageType, cborData)
34+
if err != nil {
35+
t.Fatalf("failed to decode CBOR: %s", err)
36+
}
37+
// Set the raw CBOR so the comparison should succeed
38+
test.Message.SetCbor(cborData)
39+
if !reflect.DeepEqual(msg, test.Message) {
40+
t.Fatalf("CBOR did not decode to expected message object\n got: %#v\n wanted: %#v", msg, test.Message)
41+
}
42+
}
43+
}
44+
45+
func TestEncode(t *testing.T) {
46+
for _, test := range tests {
47+
cborData, err := utils.CborEncode(test.Message)
48+
if err != nil {
49+
t.Fatalf("failed to encode message to CBOR: %s", err)
50+
}
51+
cborHex := hex.EncodeToString(cborData)
52+
if cborHex != test.CborHex {
53+
t.Fatalf("message did not encode to expected CBOR\n got: %s\n wanted: %s", cborHex, test.CborHex)
54+
}
55+
}
56+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package handshake
2+
3+
import (
4+
"encoding/hex"
5+
"github.com/cloudstruct/go-ouroboros-network/protocol"
6+
"github.com/cloudstruct/go-ouroboros-network/utils"
7+
"reflect"
8+
"testing"
9+
)
10+
11+
type testDefinition struct {
12+
CborHex string
13+
Message protocol.Message
14+
MessageType uint
15+
}
16+
17+
// TODO: implement tests for more messages
18+
var tests = []testDefinition{}
19+
20+
func TestDecode(t *testing.T) {
21+
for _, test := range tests {
22+
cborData, err := hex.DecodeString(test.CborHex)
23+
if err != nil {
24+
t.Fatalf("failed to decode CBOR hex: %s", err)
25+
}
26+
msg, err := NewMsgFromCbor(test.MessageType, cborData)
27+
if err != nil {
28+
t.Fatalf("failed to decode CBOR: %s", err)
29+
}
30+
// Set the raw CBOR so the comparison should succeed
31+
test.Message.SetCbor(cborData)
32+
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)
34+
}
35+
}
36+
}
37+
38+
func TestEncode(t *testing.T) {
39+
for _, test := range tests {
40+
cborData, err := utils.CborEncode(test.Message)
41+
if err != nil {
42+
t.Fatalf("failed to encode message to CBOR: %s", err)
43+
}
44+
cborHex := hex.EncodeToString(cborData)
45+
if cborHex != test.CborHex {
46+
t.Fatalf("message did not encode to expected CBOR\n got: %s\n wanted: %s", cborHex, test.CborHex)
47+
}
48+
}
49+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package keepalive
2+
3+
import (
4+
"encoding/hex"
5+
"github.com/cloudstruct/go-ouroboros-network/protocol"
6+
"github.com/cloudstruct/go-ouroboros-network/utils"
7+
"reflect"
8+
"testing"
9+
)
10+
11+
type testDefinition struct {
12+
CborHex string
13+
Message protocol.Message
14+
MessageType uint
15+
}
16+
17+
var tests = []testDefinition{
18+
{
19+
CborHex: "8200193039",
20+
Message: NewMsgKeepAlive(12345),
21+
MessageType: MESSAGE_TYPE_KEEP_ALIVE,
22+
},
23+
{
24+
CborHex: "8201193039",
25+
Message: NewMsgKeepAliveResponse(12345),
26+
MessageType: MESSAGE_TYPE_KEEP_ALIVE_RESPONSE,
27+
},
28+
{
29+
CborHex: "8102",
30+
Message: NewMsgDone(),
31+
MessageType: MESSAGE_TYPE_DONE,
32+
},
33+
}
34+
35+
func TestDecode(t *testing.T) {
36+
for _, test := range tests {
37+
cborData, err := hex.DecodeString(test.CborHex)
38+
if err != nil {
39+
t.Fatalf("failed to decode CBOR hex: %s", err)
40+
}
41+
msg, err := NewMsgFromCbor(test.MessageType, cborData)
42+
if err != nil {
43+
t.Fatalf("failed to decode CBOR: %s", err)
44+
}
45+
// Set the raw CBOR so the comparison should succeed
46+
test.Message.SetCbor(cborData)
47+
if !reflect.DeepEqual(msg, test.Message) {
48+
t.Fatalf("CBOR did not decode to expected message object\n got: %#v\n wanted: %#v", msg, test.Message)
49+
}
50+
}
51+
}
52+
53+
func TestEncode(t *testing.T) {
54+
for _, test := range tests {
55+
cborData, err := utils.CborEncode(test.Message)
56+
if err != nil {
57+
t.Fatalf("failed to encode message to CBOR: %s", err)
58+
}
59+
cborHex := hex.EncodeToString(cborData)
60+
if cborHex != test.CborHex {
61+
t.Fatalf("message did not encode to expected CBOR\n got: %s\n wanted: %s", cborHex, test.CborHex)
62+
}
63+
}
64+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package localstatequery
2+
3+
import (
4+
"encoding/hex"
5+
"github.com/cloudstruct/go-ouroboros-network/protocol"
6+
"github.com/cloudstruct/go-ouroboros-network/utils"
7+
"reflect"
8+
"testing"
9+
)
10+
11+
type testDefinition struct {
12+
CborHex string
13+
Message protocol.Message
14+
MessageType uint
15+
}
16+
17+
// TODO: implement tests for more messages
18+
var tests = []testDefinition{
19+
{
20+
CborHex: "8107",
21+
Message: NewMsgDone(),
22+
MessageType: MESSAGE_TYPE_DONE,
23+
},
24+
}
25+
26+
func TestDecode(t *testing.T) {
27+
for _, test := range tests {
28+
cborData, err := hex.DecodeString(test.CborHex)
29+
if err != nil {
30+
t.Fatalf("failed to decode CBOR hex: %s", err)
31+
}
32+
msg, err := NewMsgFromCbor(test.MessageType, cborData)
33+
if err != nil {
34+
t.Fatalf("failed to decode CBOR: %s", err)
35+
}
36+
// Set the raw CBOR so the comparison should succeed
37+
test.Message.SetCbor(cborData)
38+
if !reflect.DeepEqual(msg, test.Message) {
39+
t.Fatalf("CBOR did not decode to expected message object\n got: %#v\n wanted: %#v", msg, test.Message)
40+
}
41+
}
42+
}
43+
44+
func TestEncode(t *testing.T) {
45+
for _, test := range tests {
46+
cborData, err := utils.CborEncode(test.Message)
47+
if err != nil {
48+
t.Fatalf("failed to encode message to CBOR: %s", err)
49+
}
50+
cborHex := hex.EncodeToString(cborData)
51+
if cborHex != test.CborHex {
52+
t.Fatalf("message did not encode to expected CBOR\n got: %s\n wanted: %s", cborHex, test.CborHex)
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)