|
| 1 | +// Copyright 2026 Google 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 router |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "io" |
| 20 | + "math" |
| 21 | + "reflect" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/libp2p/go-libp2p" |
| 26 | + "github.com/libp2p/go-libp2p/core/host" |
| 27 | + "github.com/libp2p/go-libp2p/core/network" |
| 28 | + "github.com/libp2p/go-libp2p/core/peer" |
| 29 | + "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client" |
| 30 | + "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay" |
| 31 | + "github.com/multiformats/go-multiaddr" |
| 32 | +) |
| 33 | + |
| 34 | +func TestRelayLimit(t *testing.T) { |
| 35 | + unlimitedDuration := math.MaxUint32 * time.Second |
| 36 | + for _, tc := range []struct { |
| 37 | + name string |
| 38 | + duration time.Duration |
| 39 | + data int64 |
| 40 | + want *relay.RelayLimit |
| 41 | + }{ |
| 42 | + {"no limits", 0, 0, nil}, |
| 43 | + {"duration only", time.Hour, 0, &relay.RelayLimit{Duration: time.Hour, Data: math.MaxInt64}}, |
| 44 | + {"data only", 0, 4096, &relay.RelayLimit{Duration: unlimitedDuration, Data: 4096}}, |
| 45 | + {"both", time.Minute, 4096, &relay.RelayLimit{Duration: time.Minute, Data: 4096}}, |
| 46 | + } { |
| 47 | + t.Run(tc.name, func(t *testing.T) { |
| 48 | + if got := relayLimit(tc.duration, tc.data); !reflect.DeepEqual(got, tc.want) { |
| 49 | + t.Errorf("relayLimit(%v, %d) = %+v, want %+v", tc.duration, tc.data, got, tc.want) |
| 50 | + } |
| 51 | + }) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestByteSizeSet(t *testing.T) { |
| 56 | + for _, tc := range []struct { |
| 57 | + in string |
| 58 | + want ByteSize |
| 59 | + wantErr bool |
| 60 | + }{ |
| 61 | + {"128MiB", 128 << 20, false}, |
| 62 | + {"1GB", 1_000_000_000, false}, |
| 63 | + {"0", 0, false}, |
| 64 | + {"abc", 0, true}, |
| 65 | + {"20EiB", 0, true}, |
| 66 | + } { |
| 67 | + var got ByteSize |
| 68 | + err := got.Set(tc.in) |
| 69 | + if (err != nil) != tc.wantErr || got != tc.want { |
| 70 | + t.Errorf("Set(%q) = %d, %v; want %d, error %v", tc.in, got, err, tc.want, tc.wantErr) |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// A real circuit: the data cap truncates, and a duration-only limit must not cut. |
| 76 | +func TestRelayLimitOnCircuit(t *testing.T) { |
| 77 | + const payload = 8192 |
| 78 | + for _, tc := range []struct { |
| 79 | + name string |
| 80 | + limit *relay.RelayLimit |
| 81 | + complete bool |
| 82 | + }{ |
| 83 | + {"data cap cuts the circuit", relayLimit(0, 4096), false}, |
| 84 | + {"duration only passes everything", relayLimit(time.Hour, 0), true}, |
| 85 | + } { |
| 86 | + t.Run(tc.name, func(t *testing.T) { |
| 87 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 88 | + defer cancel() |
| 89 | + |
| 90 | + relayHost := newLoopbackHost(t, libp2p.DisableRelay()) |
| 91 | + if _, err := relay.New(relayHost, relay.WithLimit(tc.limit)); err != nil { |
| 92 | + t.Fatal(err) |
| 93 | + } |
| 94 | + relayInfo := peer.AddrInfo{ID: relayHost.ID(), Addrs: relayHost.Addrs()} |
| 95 | + |
| 96 | + dst := newLoopbackHost(t, libp2p.EnableRelay()) |
| 97 | + dst.SetStreamHandler("/test", func(s network.Stream) { |
| 98 | + _, _ = s.Write(make([]byte, payload)) |
| 99 | + _ = s.Close() |
| 100 | + }) |
| 101 | + if err := dst.Connect(ctx, relayInfo); err != nil { |
| 102 | + t.Fatal(err) |
| 103 | + } |
| 104 | + if _, err := client.Reserve(ctx, dst, relayInfo); err != nil { |
| 105 | + t.Fatal(err) |
| 106 | + } |
| 107 | + |
| 108 | + src := newLoopbackHost(t, libp2p.EnableRelay()) |
| 109 | + if err := src.Connect(ctx, relayInfo); err != nil { |
| 110 | + t.Fatal(err) |
| 111 | + } |
| 112 | + circuit, err := multiaddr.NewMultiaddr("/p2p/" + relayHost.ID().String() + "/p2p-circuit") |
| 113 | + if err != nil { |
| 114 | + t.Fatal(err) |
| 115 | + } |
| 116 | + if err := src.Connect(ctx, peer.AddrInfo{ID: dst.ID(), Addrs: []multiaddr.Multiaddr{relayHost.Addrs()[0].Encapsulate(circuit)}}); err != nil { |
| 117 | + t.Fatal(err) |
| 118 | + } |
| 119 | + |
| 120 | + s, err := src.NewStream(network.WithAllowLimitedConn(ctx, "test"), dst.ID(), "/test") |
| 121 | + if err != nil { |
| 122 | + t.Fatal(err) |
| 123 | + } |
| 124 | + defer func() { _ = s.Close() }() |
| 125 | + got, _ := io.Copy(io.Discard, s) |
| 126 | + if (got == payload) != tc.complete { |
| 127 | + t.Errorf("read %d of %d bytes through the relay, want complete=%v", got, payload, tc.complete) |
| 128 | + } |
| 129 | + }) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func newLoopbackHost(t *testing.T, opts ...libp2p.Option) host.Host { |
| 134 | + t.Helper() |
| 135 | + h, err := libp2p.New(append(opts, libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))...) |
| 136 | + if err != nil { |
| 137 | + t.Fatal(err) |
| 138 | + } |
| 139 | + t.Cleanup(func() { _ = h.Close() }) |
| 140 | + return h |
| 141 | +} |
0 commit comments