|
| 1 | +// Copyright 2026 The Swarm Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package libp2p |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/ethersphere/bee/v2/pkg/crypto" |
| 12 | + "github.com/ethersphere/bee/v2/pkg/log" |
| 13 | + "github.com/ethersphere/bee/v2/pkg/statestore/mock" |
| 14 | + "github.com/ethersphere/bee/v2/pkg/swarm" |
| 15 | + libp2ppeer "github.com/libp2p/go-libp2p/core/peer" |
| 16 | +) |
| 17 | + |
| 18 | +func TestBee260BackwardCompatibility(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + |
| 21 | + tests := []struct { |
| 22 | + name string |
| 23 | + userAgent string |
| 24 | + want bool |
| 25 | + }{ |
| 26 | + // Versions < 2.7.0 should require backward compatibility |
| 27 | + { |
| 28 | + name: "version 2.6.0", |
| 29 | + userAgent: "bee/2.6.0 go1.22.0 linux/amd64", |
| 30 | + want: true, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "version 2.6.5", |
| 34 | + userAgent: "bee/2.6.5 go1.22.0 linux/amd64", |
| 35 | + want: true, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "version 2.5.0", |
| 39 | + userAgent: "bee/2.5.0 go1.21.0 linux/amd64", |
| 40 | + want: true, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "version 2.6.0-beta1", |
| 44 | + userAgent: "bee/2.6.0-beta1 go1.22.0 linux/amd64", |
| 45 | + want: true, |
| 46 | + }, |
| 47 | + // Versions >= 2.7.0 should NOT require backward compatibility |
| 48 | + { |
| 49 | + name: "version 2.7.0", |
| 50 | + userAgent: "bee/2.7.0 go1.23.0 linux/amd64", |
| 51 | + want: false, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "version 2.8.0", |
| 55 | + userAgent: "bee/2.8.0 go1.23.0 linux/amd64", |
| 56 | + want: false, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "version 3.0.0", |
| 60 | + userAgent: "bee/3.0.0 go1.25.0 linux/amd64", |
| 61 | + want: false, |
| 62 | + }, |
| 63 | + // Pre-release versions >= 2.7.0 should NOT require backward compatibility |
| 64 | + // This is the critical fix: 2.7.0-rcX should be treated as >= 2.7.0 |
| 65 | + { |
| 66 | + name: "version 2.7.0-rc1", |
| 67 | + userAgent: "bee/2.7.0-rc1 go1.23.0 linux/amd64", |
| 68 | + want: false, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "version 2.7.0-rc12", |
| 72 | + userAgent: "bee/2.7.0-rc12-b39629d5-dirty go1.25.6 linux/amd64", |
| 73 | + want: false, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "version 2.7.0-beta1", |
| 77 | + userAgent: "bee/2.7.0-beta1 go1.23.0 linux/amd64", |
| 78 | + want: false, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "version 2.8.0-rc1", |
| 82 | + userAgent: "bee/2.8.0-rc1 go1.24.0 linux/amd64", |
| 83 | + want: false, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "version 2.9.0-beta1", |
| 87 | + userAgent: "bee/2.9.0-beta1 go1.24.0 linux/amd64", |
| 88 | + want: false, |
| 89 | + }, |
| 90 | + // Edge cases that should return false (not requiring backward compat) |
| 91 | + { |
| 92 | + name: "empty user agent", |
| 93 | + userAgent: "", |
| 94 | + want: false, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "malformed user agent missing space", |
| 98 | + userAgent: "bee/2.6.0", |
| 99 | + want: false, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "non-bee user agent", |
| 103 | + userAgent: "other/1.0.0 go1.22.0 linux/amd64", |
| 104 | + want: false, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "invalid version format", |
| 108 | + userAgent: "bee/invalid go1.22.0 linux/amd64", |
| 109 | + want: false, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "default libp2p user agent", |
| 113 | + userAgent: "github.com/libp2p/go-libp2p", |
| 114 | + want: false, |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + for _, tc := range tests { |
| 119 | + t.Run(tc.name, func(t *testing.T) { |
| 120 | + t.Parallel() |
| 121 | + |
| 122 | + // Create a service with minimal configuration |
| 123 | + ctx, cancel := context.WithCancel(context.Background()) |
| 124 | + defer cancel() |
| 125 | + |
| 126 | + swarmKey, err := crypto.GenerateSecp256k1Key() |
| 127 | + if err != nil { |
| 128 | + t.Fatal(err) |
| 129 | + } |
| 130 | + |
| 131 | + overlay := swarm.RandAddress(t) |
| 132 | + addr := ":0" |
| 133 | + networkID := uint64(1) |
| 134 | + |
| 135 | + statestore := mock.NewStateStore() |
| 136 | + defer statestore.Close() |
| 137 | + |
| 138 | + s, err := New(ctx, crypto.NewDefaultSigner(swarmKey), networkID, overlay, addr, nil, statestore, nil, log.Noop, nil, Options{}) |
| 139 | + if err != nil { |
| 140 | + t.Fatal(err) |
| 141 | + } |
| 142 | + defer s.Close() |
| 143 | + |
| 144 | + // Create a random test peer ID - we only need any valid libp2p peer ID |
| 145 | + // The peerstore lookup will be mocked by setting the AgentVersion directly |
| 146 | + libp2pPeerID, err := libp2ppeer.Decode("16Uiu2HAm3g4hXfCWTDhPBq3KkqpV3wGkPVgMJY3Jt8gGTYWiTWNZ") |
| 147 | + if err != nil { |
| 148 | + t.Fatal(err) |
| 149 | + } |
| 150 | + |
| 151 | + // Set the user agent in the peerstore if provided |
| 152 | + if tc.userAgent != "" { |
| 153 | + if err := s.host.Peerstore().Put(libp2pPeerID, "AgentVersion", tc.userAgent); err != nil { |
| 154 | + t.Fatal(err) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + // Test the backward compatibility check |
| 159 | + got := s.bee260BackwardCompatibility(libp2pPeerID) |
| 160 | + if got != tc.want { |
| 161 | + t.Errorf("bee260BackwardCompatibility() = %v, want %v (userAgent: %q)", got, tc.want, tc.userAgent) |
| 162 | + } |
| 163 | + }) |
| 164 | + } |
| 165 | +} |
0 commit comments