Skip to content

Commit

Permalink
feat: add keepalive test
Browse files Browse the repository at this point in the history
Signed-off-by: Ales Verbic <[email protected]>
  • Loading branch information
verbotenj committed Mar 10, 2024
1 parent 9f9589f commit 94277d7
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
28 changes: 24 additions & 4 deletions internal/test/ouroboros_mock/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ import (
)

const (
MockNetworkMagic uint32 = 999999
MockProtocolVersionNtC uint16 = (14 + protocol.ProtocolVersionNtCOffset)
MockProtocolVersionNtN uint16 = 13
MockKeepAliveCookie uint16 = 999
MockNetworkMagic uint32 = 999999
MockProtocolVersionNtC uint16 = (14 + protocol.ProtocolVersionNtCOffset)
MockProtocolVersionNtN uint16 = 13
MockKeepAliveCookie uint16 = 999
MockKeepAliveWrongCookie uint16 = 1000
)

type EntryType int
Expand Down Expand Up @@ -109,6 +110,16 @@ var ConversationEntryKeepAliveResponse = ConversationEntry{
},
}

// ConversationEntryKeepAliveResponse is a pre-defined conversation entry for a keep-alive response
var ConversationEntryKeepAliveResponseWrongResponse = ConversationEntry{
Type: EntryTypeOutput,
ProtocolId: keepalive.ProtocolId,
IsResponse: true,
OutputMessages: []protocol.Message{
keepalive.NewMsgKeepAliveResponse(MockKeepAliveWrongCookie),
},
}

// ConversationKeepAlive is a pre-defined conversation with a NtN handshake and repeated keep-alive requests
// and responses
var ConversationKeepAlive = []ConversationEntry{
Expand All @@ -124,6 +135,15 @@ var ConversationKeepAlive = []ConversationEntry{
ConversationEntryKeepAliveResponse,
}

// ConversationKeepAlive is a pre-defined conversation with a NtN handshake and repeated keep-alive requests
// and responses
var ConversationKeepAliveWrongResponse = []ConversationEntry{
ConversationEntryHandshakeRequestGeneric,
ConversationEntryHandshakeNtNResponse,
ConversationEntryKeepAliveRequest,
ConversationEntryKeepAliveResponseWrongResponse,
}

// ConversationKeepAliveClose is a pre-defined conversation with a NtN handshake that will close the connection
// after receiving a keep-alive request
var ConversationKeepAliveClose = []ConversationEntry{
Expand Down
68 changes: 68 additions & 0 deletions protocol/keepalive/serverclient_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package keepalive_test

import (
"testing"

ouroboros "github.com/blinklabs-io/gouroboros"
"github.com/blinklabs-io/gouroboros/internal/test/ouroboros_mock"
"github.com/blinklabs-io/gouroboros/protocol/keepalive"
)

func TestServerKeepaliveHandling(t *testing.T) {

keepAliveSentCounter := 0

keepAliveConfig := keepalive.NewConfig(
keepalive.WithKeepAliveFunc(func(protocolID uint16) error {
keepAliveSentCounter++
return nil
}),
)

mockConn := ouroboros_mock.NewConnection(
ouroboros_mock.ProtocolRoleNone,
ouroboros_mock.ConversationKeepAlive,
).(*ouroboros_mock.Connection)

oConn, err := ouroboros.New(
ouroboros.WithConnection(mockConn),
ouroboros.WithNetworkMagic(ouroboros_mock.MockNetworkMagic),
ouroboros.WithNodeToNode(true),
ouroboros.WithKeepAlive(true),
ouroboros.WithKeepAliveConfig(keepAliveConfig),
)
if err != nil {
t.Fatalf("unexpected error when creating Connection object: %s", err)
}

// TODO Add test condition

// Close connection
if err := oConn.Close(); err != nil {
t.Fatalf("unexpected error when closing Connection object: %s", err)
}
}

func TestServerKeepaliveHandlingWithWrongResponse(t *testing.T) {
mockConn := ouroboros_mock.NewConnection(
ouroboros_mock.ProtocolRoleNone,
ouroboros_mock.ConversationKeepAliveWrongResponse,
).(*ouroboros_mock.Connection)

oConn, err := ouroboros.New(
ouroboros.WithConnection(mockConn),
ouroboros.WithNetworkMagic(ouroboros_mock.MockNetworkMagic),
ouroboros.WithNodeToNode(true),
ouroboros.WithKeepAlive(true),
)
if err != nil {
t.Fatalf("unexpected error when creating Connection object: %s", err)
}

// TODO Add test condition

// Close connection
if err := oConn.Close(); err != nil {
t.Fatalf("unexpected error when closing Connection object: %s", err)
}
}

0 comments on commit 94277d7

Please sign in to comment.