-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathark_version_test.go
More file actions
229 lines (207 loc) · 5.16 KB
/
Copy pathark_version_test.go
File metadata and controls
229 lines (207 loc) · 5.16 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package serverconn
import (
"testing"
"github.com/lightninglabs/wavelength/arkrpc"
mailboxconn "github.com/lightninglabs/wavelength/mailbox/conn"
"github.com/stretchr/testify/require"
)
// activeArkPolicy builds an ACTIVE policy for the given version, used to
// populate the operator's advertised policy list in fake GetInfo responses.
func activeArkPolicy(version uint32) *arkrpc.ArkVersionPolicy {
return &arkrpc.ArkVersionPolicy{
Version: version,
State: arkrpc.ArkVersionPolicy_STATE_ACTIVE,
}
}
// disabledArkPolicy builds a DISABLED policy for the given version.
func disabledArkPolicy(version uint32) *arkrpc.ArkVersionPolicy {
return &arkrpc.ArkVersionPolicy{
Version: version,
State: arkrpc.ArkVersionPolicy_STATE_DISABLED,
}
}
// TestResolveArkVersionSelection covers the pure selection decision: normal
// preference selection, an operator selecting an unsupported version, a zero
// selection (no overlap or pre-versioning server) which is always fatal, and
// a no-overlap response with a non-empty list.
func TestResolveArkVersionSelection(t *testing.T) {
t.Parallel()
tests := []struct {
name string
resp *arkrpc.GetInfoResponse
client []uint32
wantSelected uint32
wantErr bool
}{
{
name: "normal v1 selection",
resp: &arkrpc.GetInfoResponse{
SelectedArkVersion: 1,
},
client: []uint32{
1,
},
wantSelected: 1,
},
{
name: "operator selects v2 client supports it",
resp: &arkrpc.GetInfoResponse{
SelectedArkVersion: 2,
},
client: []uint32{
1,
2,
},
wantSelected: 2,
},
{
name: "operator selects unsupported version",
resp: &arkrpc.GetInfoResponse{
SelectedArkVersion: 2,
},
client: []uint32{
1,
},
wantErr: true,
},
{
name: "zero selection is fatal",
resp: &arkrpc.GetInfoResponse{},
client: []uint32{
1,
},
wantErr: true,
},
{
name: "no overlap is fatal",
resp: &arkrpc.GetInfoResponse{
ArkVersionPolicies: []*arkrpc.ArkVersionPolicy{
activeArkPolicy(2),
activeArkPolicy(3),
},
},
client: []uint32{
1,
},
wantErr: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
selected, err := resolveArkVersionSelection(
tc.resp, tc.client,
)
if tc.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, tc.wantSelected, selected)
})
}
}
// TestValidateRefreshSelection proves a refresh cannot renegotiate: it accepts
// only a matching selection and treats any zero or different selection as a
// terminal ARK_VERSION_MISMATCH. There is no legacy fallback.
func TestValidateRefreshSelection(t *testing.T) {
t.Parallel()
tests := []struct {
name string
boundArk uint32
resp *arkrpc.GetInfoResponse
wantErr bool
}{
{
name: "matching v1",
boundArk: 1,
resp: &arkrpc.GetInfoResponse{
SelectedArkVersion: 1,
},
},
{
name: "matching v2",
boundArk: 2,
resp: &arkrpc.GetInfoResponse{
SelectedArkVersion: 2,
},
},
{
name: "v1 rejects zero selection",
boundArk: 1,
resp: &arkrpc.GetInfoResponse{},
wantErr: true,
},
{
name: "v1 rejects nil response",
boundArk: 1,
resp: nil,
wantErr: true,
},
{
name: "v1 rejects different selection",
boundArk: 1,
resp: &arkrpc.GetInfoResponse{
SelectedArkVersion: 2,
},
wantErr: true,
},
{
name: "v2 rejects zero selection",
boundArk: 2,
resp: &arkrpc.GetInfoResponse{},
wantErr: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
// ValidateRefreshSelection returns a typed pointer;
// compare the pointer directly to avoid the
// nil-interface pitfall.
statusErr := ValidateRefreshSelection(
tc.resp, tc.boundArk,
)
if tc.wantErr {
require.NotNil(t, statusErr)
require.Equal(
t, mailboxconn.StatusArkVersionMismatch,
statusErr.Code(),
)
return
}
require.Nil(t, statusErr)
})
}
}
// TestValidateRefreshSelectionSelectedButDisabled proves a refresh that
// re-selects the bound version but advertises it as DISABLED is a terminal,
// mandatory-upgrade failure: a permanent UPGRADE_REQUIRED status error,
// distinct from the renegotiation mismatch path.
func TestValidateRefreshSelectionSelectedButDisabled(t *testing.T) {
t.Parallel()
resp := &arkrpc.GetInfoResponse{
SelectedArkVersion: 1,
ArkVersionPolicies: []*arkrpc.ArkVersionPolicy{
disabledArkPolicy(1),
},
}
statusErr := ValidateRefreshSelection(resp, 1)
require.NotNil(t, statusErr)
require.Equal(
t, mailboxconn.StatusUpgradeRequired, statusErr.Code(),
)
}
// TestValidateRefreshSelectionActivePolicyOk proves a matching selection with a
// non-disabled (here ACTIVE) policy for the bound version is a normal, accepted
// refresh — an advertised policy alone does not make a refresh fail.
func TestValidateRefreshSelectionActivePolicyOk(t *testing.T) {
t.Parallel()
resp := &arkrpc.GetInfoResponse{
SelectedArkVersion: 1,
ArkVersionPolicies: []*arkrpc.ArkVersionPolicy{
activeArkPolicy(1),
},
}
require.Nil(t, ValidateRefreshSelection(resp, 1))
}