-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathnode_test.go
More file actions
150 lines (140 loc) · 3.62 KB
/
Copy pathnode_test.go
File metadata and controls
150 lines (140 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2025 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package node_test
import (
"testing"
"github.com/ethersphere/bee/v2/pkg/api"
"github.com/ethersphere/bee/v2/pkg/node"
)
func TestValidatePublicAddress(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
addr string
expErr bool
}{
{
name: "empty host",
addr: ":1635",
expErr: true,
},
{
name: "localhost",
addr: "localhost:1635",
expErr: true,
},
{
name: "loopback",
addr: "127.0.0.1:1635",
expErr: true,
},
{
name: "loopback ipv6",
addr: "[::1]:1635",
expErr: true,
},
{
name: "missing port",
addr: "1.2.3.4",
expErr: true,
},
{
name: "empty port",
addr: "1.2.3.4:",
expErr: true,
},
{
name: "invalid port number",
addr: "1.2.3.4:abc",
expErr: true,
},
{
name: "valid",
addr: "1.2.3.4:1635",
expErr: false,
},
{
name: "valid ipv6",
addr: "[2001:db8::1]:1635",
expErr: false,
},
{
name: "empty",
addr: "",
expErr: false,
},
{
name: "valid hostname",
addr: "example.com:8080",
expErr: false,
},
{
name: "valid hostname with hyphen",
addr: "test-example.com:8080",
expErr: false,
},
{
name: "private IP",
addr: "192.168.1.1:8080",
expErr: true,
},
{
name: "invalid hostname format",
addr: "invalid..hostname:8080",
expErr: true,
},
{
name: "hostname starts with hyphen",
addr: "-test.com:8080",
expErr: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := node.ValidatePublicAddress(tc.addr)
if tc.expErr && err == nil {
t.Fatal("expected error, but got none")
}
if !tc.expErr && err != nil {
t.Fatalf("expected no error, but got: %v", err)
}
})
}
}
func TestUseEmbeddedSnapshot(t *testing.T) {
t.Parallel()
const (
mainnet = uint64(1)
testnet = uint64(10)
)
testCases := []struct {
name string
skip bool
batchStoreExists bool
resync bool
networkID uint64
mode api.BeeNodeMode
want bool
}{
{name: "first boot on mainnet", networkID: mainnet, mode: api.FullMode, want: true},
{name: "existing store, no resync", batchStoreExists: true, networkID: mainnet, mode: api.FullMode, want: false},
{name: "resync on a fresh store", resync: true, networkID: mainnet, mode: api.FullMode, want: true},
// New behavior: resync rebuilds from the snapshot even with a store present.
{name: "resync on an existing store uses the snapshot", batchStoreExists: true, resync: true, networkID: mainnet, mode: api.FullMode, want: true},
{name: "resync with skip syncs from the chain", batchStoreExists: true, resync: true, skip: true, networkID: mainnet, mode: api.FullMode, want: false},
{name: "skip on first boot", skip: true, networkID: mainnet, mode: api.FullMode, want: false},
{name: "light node uses the snapshot", networkID: mainnet, mode: api.LightMode, want: true},
{name: "ultra-light never uses the snapshot", resync: true, networkID: mainnet, mode: api.UltraLightMode, want: false},
{name: "non-mainnet never uses the snapshot", resync: true, networkID: testnet, mode: api.FullMode, want: false},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got := node.UseEmbeddedSnapshot(tc.skip, tc.batchStoreExists, tc.resync, tc.networkID, tc.mode)
if got != tc.want {
t.Fatalf("UseEmbeddedSnapshot = %v, want %v", got, tc.want)
}
})
}
}