|
| 1 | +// Copyright 2025 - See NOTICE file for copyright holders. |
| 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 | +// This test uses the wire/net/libp2p implementation of Account and Address |
| 16 | +// to test the default implementation of wire. |
| 17 | +// |
| 18 | +//nolint:testpackage |
| 19 | +package libp2p |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "math/rand" |
| 24 | + "net" |
| 25 | + "sync" |
| 26 | + "testing" |
| 27 | + "time" |
| 28 | + |
| 29 | + "perun.network/go-perun/channel" |
| 30 | + |
| 31 | + "github.com/stretchr/testify/assert" |
| 32 | + "github.com/stretchr/testify/require" |
| 33 | + |
| 34 | + "perun.network/go-perun/wire" |
| 35 | + wirenet "perun.network/go-perun/wire/net" |
| 36 | + perunio "perun.network/go-perun/wire/perunio/serializer" |
| 37 | + wiretest "perun.network/go-perun/wire/test" |
| 38 | + ctxtest "polycry.pt/poly-go/context/test" |
| 39 | + "polycry.pt/poly-go/test" |
| 40 | +) |
| 41 | + |
| 42 | +const timeout = 100 * time.Millisecond |
| 43 | + |
| 44 | +func TestExchangeAddrs_ConnFail(t *testing.T) { |
| 45 | + rng := test.Prng(t) |
| 46 | + a, _ := newPipeConnPair() |
| 47 | + a.Close() |
| 48 | + acc := wiretest.NewRandomAccountMap(rng, channel.TestBackendID) |
| 49 | + defer acc[channel.TestBackendID].(*Account).Close() |
| 50 | + addr, err := wirenet.ExchangeAddrsPassive(context.Background(), acc, a) |
| 51 | + assert.Nil(t, addr) |
| 52 | + assert.Error(t, err) |
| 53 | +} |
| 54 | + |
| 55 | +func TestExchangeAddrs_Success(t *testing.T) { |
| 56 | + rng := test.Prng(t) |
| 57 | + conn0, conn1 := newPipeConnPair() |
| 58 | + defer conn0.Close() |
| 59 | + account0, account1 := wiretest.NewRandomAccountMap(rng, channel.TestBackendID), wiretest.NewRandomAccountMap(rng, channel.TestBackendID) |
| 60 | + defer account0[channel.TestBackendID].(*Account).Close() |
| 61 | + defer account1[channel.TestBackendID].(*Account).Close() |
| 62 | + var wg sync.WaitGroup |
| 63 | + wg.Add(1) |
| 64 | + |
| 65 | + go func() { |
| 66 | + defer wg.Done() |
| 67 | + defer conn1.Close() |
| 68 | + |
| 69 | + recvAddr0, err := wirenet.ExchangeAddrsPassive(context.Background(), account1, conn1) |
| 70 | + assert.NoError(t, err) |
| 71 | + assert.True(t, channel.EqualWireMaps(recvAddr0, wire.AddressMapfromAccountMap(account0))) |
| 72 | + }() |
| 73 | + |
| 74 | + err := wirenet.ExchangeAddrsActive(context.Background(), account0, wire.AddressMapfromAccountMap(account1), conn0) |
| 75 | + require.NoError(t, err) |
| 76 | + |
| 77 | + wg.Wait() |
| 78 | +} |
| 79 | + |
| 80 | +func TestExchangeAddrs_BogusMsg(t *testing.T) { |
| 81 | + rng := test.Prng(t) |
| 82 | + acc := wiretest.NewRandomAccountMap(rng, channel.TestBackendID) |
| 83 | + defer acc[channel.TestBackendID].(*Account).Close() |
| 84 | + conn := newMockConn() |
| 85 | + conn.recvQueue <- newRandomEnvelope(rng, wire.NewPingMsg()) |
| 86 | + addr, err := wirenet.ExchangeAddrsPassive(context.Background(), acc, conn) |
| 87 | + |
| 88 | + assert.Error(t, err, "ExchangeAddrs should error when peer sends a non-AuthResponseMsg") |
| 89 | + assert.Nil(t, addr) |
| 90 | +} |
| 91 | + |
| 92 | +func TestExchangeAddrs_Timeout(t *testing.T) { |
| 93 | + rng := test.Prng(t) |
| 94 | + a, _ := newPipeConnPair() |
| 95 | + |
| 96 | + ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 97 | + defer cancel() |
| 98 | + ctxtest.AssertTerminates(t, 20*timeout, func() { |
| 99 | + acc := wiretest.NewRandomAccountMap(rng, channel.TestBackendID) |
| 100 | + defer acc[channel.TestBackendID].(*Account).Close() |
| 101 | + addr, err := wirenet.ExchangeAddrsPassive(ctx, acc, a) |
| 102 | + assert.Nil(t, addr) |
| 103 | + assert.Error(t, err) |
| 104 | + }) |
| 105 | +} |
| 106 | + |
| 107 | +// newPipeConnPair creates endpoints that are connected via pipes. |
| 108 | +func newPipeConnPair() (a wirenet.Conn, b wirenet.Conn) { |
| 109 | + c0, c1 := net.Pipe() |
| 110 | + ser := perunio.Serializer() |
| 111 | + return wirenet.NewIoConn(c0, ser), wirenet.NewIoConn(c1, ser) |
| 112 | +} |
| 113 | + |
| 114 | +// NewRandomEnvelope returns an envelope around message m with random sender and |
| 115 | +// recipient generated using randomness from rng. |
| 116 | +func newRandomEnvelope(rng *rand.Rand, m wire.Msg) *wire.Envelope { |
| 117 | + return &wire.Envelope{ |
| 118 | + Sender: NewRandomAddresses(rng), |
| 119 | + Recipient: NewRandomAddresses(rng), |
| 120 | + Msg: m, |
| 121 | + } |
| 122 | +} |
0 commit comments