|
| 1 | +// Copyright 2023 Blink Labs, LLC. |
| 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 | +) |
| 24 | + |
| 25 | +func TestBasicHandshake(t *testing.T) { |
| 26 | + mockConn := ouroboros_mock.NewConnection( |
| 27 | + []ouroboros_mock.ConversationEntry{ |
| 28 | + ouroboros_mock.ConversationEntryHandshakeRequestGeneric, |
| 29 | + ouroboros_mock.ConversationEntryHandshakeResponse, |
| 30 | + }, |
| 31 | + ) |
| 32 | + oConn, err := ouroboros.New( |
| 33 | + ouroboros.WithConnection(mockConn), |
| 34 | + ouroboros.WithNetworkMagic(ouroboros_mock.MockNetworkMagic), |
| 35 | + ) |
| 36 | + if err != nil { |
| 37 | + t.Fatalf("unexpected error when creating Ouroboros object: %s", err) |
| 38 | + } |
| 39 | + // Async error handler |
| 40 | + go func() { |
| 41 | + err, ok := <-oConn.ErrorChan() |
| 42 | + if !ok { |
| 43 | + return |
| 44 | + } |
| 45 | + // We can't call t.Fatalf() from a different Goroutine, so we panic instead |
| 46 | + panic(fmt.Sprintf("unexpected Ouroboros connection error: %s", err)) |
| 47 | + }() |
| 48 | + // Close Ouroboros connection |
| 49 | + if err := oConn.Close(); err != nil { |
| 50 | + t.Fatalf("unexpected error when closing Ouroboros object: %s", err) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestDoubleStart(t *testing.T) { |
| 55 | + mockConn := ouroboros_mock.NewConnection( |
| 56 | + []ouroboros_mock.ConversationEntry{ |
| 57 | + ouroboros_mock.ConversationEntryHandshakeRequestGeneric, |
| 58 | + ouroboros_mock.ConversationEntryHandshakeResponse, |
| 59 | + }, |
| 60 | + ) |
| 61 | + oConn, err := ouroboros.New( |
| 62 | + ouroboros.WithConnection(mockConn), |
| 63 | + ouroboros.WithNetworkMagic(ouroboros_mock.MockNetworkMagic), |
| 64 | + ) |
| 65 | + if err != nil { |
| 66 | + t.Fatalf("unexpected error when creating Ouroboros object: %s", err) |
| 67 | + } |
| 68 | + // Async error handler |
| 69 | + go func() { |
| 70 | + err, ok := <-oConn.ErrorChan() |
| 71 | + if !ok { |
| 72 | + return |
| 73 | + } |
| 74 | + // We can't call t.Fatalf() from a different Goroutine, so we panic instead |
| 75 | + panic(fmt.Sprintf("unexpected Ouroboros connection error: %s", err)) |
| 76 | + }() |
| 77 | + // Try to start the Handshake client again |
| 78 | + oConn.Handshake().Client.Start() |
| 79 | + // Close Ouroboros connection |
| 80 | + if err := oConn.Close(); err != nil { |
| 81 | + t.Fatalf("unexpected error when closing Ouroboros object: %s", err) |
| 82 | + } |
| 83 | +} |
0 commit comments