|
| 1 | +// Copyright 2023 Blink Labs Software |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package handshake_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "testing" |
| 20 | + |
| 21 | + ouroboros "github.com/blinklabs-io/gouroboros" |
| 22 | + "github.com/blinklabs-io/gouroboros/internal/test/ouroboros_mock" |
| 23 | + "github.com/blinklabs-io/gouroboros/protocol" |
| 24 | + "github.com/blinklabs-io/gouroboros/protocol/handshake" |
| 25 | +) |
| 26 | + |
| 27 | +func TestServerBasicHandshake(t *testing.T) { |
| 28 | + mockConn := ouroboros_mock.NewConnection( |
| 29 | + ouroboros_mock.ProtocolRoleServer, |
| 30 | + []ouroboros_mock.ConversationEntry{ |
| 31 | + // MsgProposeVersions from mock client |
| 32 | + { |
| 33 | + Type: ouroboros_mock.EntryTypeOutput, |
| 34 | + ProtocolId: handshake.ProtocolId, |
| 35 | + OutputMessages: []protocol.Message{ |
| 36 | + handshake.NewMsgProposeVersions( |
| 37 | + protocol.ProtocolVersionMap{ |
| 38 | + (10 + protocol.ProtocolVersionNtCOffset): protocol.VersionDataNtC9to14(ouroboros_mock.MockNetworkMagic), |
| 39 | + (11 + protocol.ProtocolVersionNtCOffset): protocol.VersionDataNtC9to14(ouroboros_mock.MockNetworkMagic), |
| 40 | + (12 + protocol.ProtocolVersionNtCOffset): protocol.VersionDataNtC9to14(ouroboros_mock.MockNetworkMagic), |
| 41 | + }, |
| 42 | + ), |
| 43 | + }, |
| 44 | + }, |
| 45 | + // MsgAcceptVersion from server |
| 46 | + { |
| 47 | + Type: ouroboros_mock.EntryTypeInput, |
| 48 | + IsResponse: true, |
| 49 | + ProtocolId: handshake.ProtocolId, |
| 50 | + MsgFromCborFunc: handshake.NewMsgFromCbor, |
| 51 | + InputMessageType: handshake.MessageTypeAcceptVersion, |
| 52 | + InputMessage: handshake.NewMsgAcceptVersion( |
| 53 | + (12 + protocol.ProtocolVersionNtCOffset), |
| 54 | + protocol.VersionDataNtC9to14(ouroboros_mock.MockNetworkMagic), |
| 55 | + ), |
| 56 | + }, |
| 57 | + }, |
| 58 | + ) |
| 59 | + oConn, err := ouroboros.New( |
| 60 | + ouroboros.WithConnection(mockConn), |
| 61 | + ouroboros.WithNetworkMagic(ouroboros_mock.MockNetworkMagic), |
| 62 | + ouroboros.WithServer(true), |
| 63 | + ) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("unexpected error when creating Ouroboros object: %s", err) |
| 66 | + } |
| 67 | + // Async error handler |
| 68 | + go func() { |
| 69 | + err, ok := <-oConn.ErrorChan() |
| 70 | + if !ok { |
| 71 | + return |
| 72 | + } |
| 73 | + // We can't call t.Fatalf() from a different Goroutine, so we panic instead |
| 74 | + panic(fmt.Sprintf("unexpected Ouroboros error: %s", err)) |
| 75 | + }() |
| 76 | + // Close Ouroboros connection |
| 77 | + if err := oConn.Close(); err != nil { |
| 78 | + t.Fatalf("unexpected error when closing Ouroboros object: %s", err) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestServerHandshakeRefuseVersionMismatch(t *testing.T) { |
| 83 | + expectedErr := fmt.Errorf("handshake failed: refused due to version mismatch") |
| 84 | + mockConn := ouroboros_mock.NewConnection( |
| 85 | + ouroboros_mock.ProtocolRoleServer, |
| 86 | + []ouroboros_mock.ConversationEntry{ |
| 87 | + // MsgProposeVersions from mock client |
| 88 | + { |
| 89 | + Type: ouroboros_mock.EntryTypeOutput, |
| 90 | + ProtocolId: handshake.ProtocolId, |
| 91 | + OutputMessages: []protocol.Message{ |
| 92 | + handshake.NewMsgProposeVersions( |
| 93 | + protocol.ProtocolVersionMap{ |
| 94 | + (100 + protocol.ProtocolVersionNtCOffset): protocol.VersionDataNtC9to14(ouroboros_mock.MockNetworkMagic), |
| 95 | + (101 + protocol.ProtocolVersionNtCOffset): protocol.VersionDataNtC9to14(ouroboros_mock.MockNetworkMagic), |
| 96 | + (102 + protocol.ProtocolVersionNtCOffset): protocol.VersionDataNtC9to14(ouroboros_mock.MockNetworkMagic), |
| 97 | + }, |
| 98 | + ), |
| 99 | + }, |
| 100 | + }, |
| 101 | + // MsgRefuse from server |
| 102 | + { |
| 103 | + Type: ouroboros_mock.EntryTypeInput, |
| 104 | + IsResponse: true, |
| 105 | + ProtocolId: handshake.ProtocolId, |
| 106 | + MsgFromCborFunc: handshake.NewMsgFromCbor, |
| 107 | + InputMessageType: handshake.MessageTypeRefuse, |
| 108 | + InputMessage: handshake.NewMsgRefuse( |
| 109 | + []any{ |
| 110 | + handshake.RefuseReasonVersionMismatch, |
| 111 | + protocol.GetProtocolVersionsNtC(), |
| 112 | + }, |
| 113 | + ), |
| 114 | + }, |
| 115 | + }, |
| 116 | + ) |
| 117 | + oConn, err := ouroboros.New( |
| 118 | + ouroboros.WithConnection(mockConn), |
| 119 | + ouroboros.WithNetworkMagic(ouroboros_mock.MockNetworkMagic), |
| 120 | + ouroboros.WithServer(true), |
| 121 | + ) |
| 122 | + if err != nil { |
| 123 | + if err.Error() != expectedErr.Error() { |
| 124 | + t.Fatalf("unexpected error when creating Ouroboros object: %s", err) |
| 125 | + } |
| 126 | + } |
| 127 | + // Async error handler |
| 128 | + go func() { |
| 129 | + err, ok := <-oConn.ErrorChan() |
| 130 | + if !ok { |
| 131 | + return |
| 132 | + } |
| 133 | + panic(fmt.Sprintf("unexpected Ouroboros error: %s", err)) |
| 134 | + }() |
| 135 | +} |
0 commit comments