Repository navigation
Expand file tree
/
Copy patherrors_test.go
More file actions
271 lines (244 loc) · 6.13 KB
/
Copy patherrors_test.go
File metadata and controls
271 lines (244 loc) · 6.13 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package bgp
import (
"encoding/binary"
"testing"
)
func TestNotificationCodeString(t *testing.T) {
t.Parallel()
tests := []struct {
code NotificationCode
want string
}{
{
code: NotificationMessageHeaderError,
want: "Message Header Error",
},
{
code: NotificationOpenMessageError,
want: "OPEN Message Error",
},
{
code: NotificationUpdateMessageError,
want: "UPDATE Message Error",
},
{
code: NotificationHoldTimerExpired,
want: "Hold Timer Expired",
},
{
code: NotificationFSMError,
want: "Finite State Machine Error",
},
{
code: NotificationCease,
want: "Cease",
},
{
code: NotificationRouteRefreshMessageError,
want: "ROUTE-REFRESH Message Error",
},
{
code: NotificationSendHoldTimerExpired,
want: "Send Hold Timer Expired",
},
{
code: NotificationCode(255),
want: "unknown(255)",
},
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
t.Parallel()
if got := tt.code.String(); got != tt.want {
t.Fatalf("unexpected string: got %q, want %q", got, tt.want)
}
})
}
}
func TestSubcodeStringCease(t *testing.T) {
t.Parallel()
tests := []struct {
subcode uint8
want string
}{
{
subcode: SubcodeCeaseMaximumPrefixesReached,
want: "Maximum Number of Prefixes Reached",
},
{
subcode: SubcodeCeaseAdministrativeShutdown,
want: "Administrative Shutdown",
},
{
subcode: SubcodeCeasePeerDeconfigured,
want: "Peer De-configured",
},
{
subcode: SubcodeCeaseAdministrativeReset,
want: "Administrative Reset",
},
{
subcode: SubcodeCeaseConnectionRejected,
want: "Connection Rejected",
},
{
subcode: SubcodeCeaseOtherConfigurationChange,
want: "Other Configuration Change",
},
{
subcode: SubcodeCeaseConnectionCollisionResolution,
want: "Connection Collision Resolution",
},
{
subcode: SubcodeCeaseOutOfResources,
want: "Out of Resources",
},
{
subcode: SubcodeCeaseHardReset,
want: "Hard Reset",
},
{
subcode: SubcodeCeaseBFDDown,
want: "BFD Down",
},
{
subcode: 0,
want: "Unspecific",
},
{
subcode: 11,
want: "unknown(11)",
},
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
t.Parallel()
if got := subcodeString(NotificationCease, tt.subcode); got != tt.want {
t.Fatalf("unexpected string: got %q, want %q", got, tt.want)
}
})
}
}
func TestSubcodeStringRouteRefresh(t *testing.T) {
t.Parallel()
tests := []struct {
subcode uint8
want string
}{
{
subcode: SubcodeInvalidMessageLength,
want: "Invalid Message Length",
},
{
subcode: 0,
want: "Unspecific",
},
{
subcode: 2,
want: "unknown(2)",
},
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
t.Parallel()
if got := subcodeString(NotificationRouteRefreshMessageError, tt.subcode); got != tt.want {
t.Fatalf("unexpected string: got %q, want %q", got, tt.want)
}
})
}
}
func TestMessageErrorError(t *testing.T) {
t.Parallel()
tests := []struct {
name string
err *MessageError
want string
}{
{
name: "header",
err: headerError(SubcodeConnectionNotSynchronized, nil, "invalid message header marker"),
want: "bgp: invalid message header marker " +
"(NOTIFICATION Message Header Error: Connection Not Synchronized)",
},
{
name: "open",
err: openError(SubcodeUnacceptableHoldTime, nil, "unacceptable hold time"),
want: "bgp: unacceptable hold time " +
"(NOTIFICATION OPEN Message Error: Unacceptable Hold Time)",
},
{
name: "update",
err: updateError(SubcodeMalformedASPath, nil, "AS_PATH segment truncated"),
want: "bgp: AS_PATH segment truncated " +
"(NOTIFICATION UPDATE Message Error: Malformed AS_PATH)",
},
{
name: "unspecific subcode",
err: openError(0, nil, "OPEN capability truncated"),
want: "bgp: OPEN capability truncated " +
"(NOTIFICATION OPEN Message Error: Unspecific)",
},
{
name: "unknown subcode",
err: headerError(99, nil, "something new"),
want: "bgp: something new " +
"(NOTIFICATION Message Header Error: unknown(99))",
},
{
name: "unknown code",
err: newMessageError(NotificationCode(255), 1, nil,
"something newer"),
want: "bgp: something newer (NOTIFICATION unknown(255): unknown(1))",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := tt.err.Error(); got != tt.want {
t.Fatalf("unexpected error string:\ngot: %q\nwant: %q", got, tt.want)
}
})
}
}
// TestMessageErrorDataCloned pins the ownership rule documented on
// MessageError: its Data never references the parsed input buffer, so it
// remains valid after the buffer is reused. Contrast with Notification,
// whose parsed Data is a view of the input.
func TestMessageErrorDataCloned(t *testing.T) {
t.Parallel()
// A length mismatch echoes the input's wire length field as data.
b := testMessage(MessageTypeKeepalive, nil)
binary.BigEndian.PutUint16(b[markerLen:], headerLen+1)
_, err := ParseMessage(b)
merr := wantMessageError(t, err, &Notification{
Code: NotificationMessageHeaderError,
Subcode: SubcodeBadMessageLength,
Data: []byte{0x00, headerLen + 1},
})
// Scrambling the input must not disturb the error's diagnostic data.
for i := range b {
b[i] = 0xa5
}
if d := diff(t, []byte{0x00, headerLen + 1}, merr.Data); d != "" {
t.Fatalf("unexpected data after input reuse (-want +got):\n%s", d)
}
}
func TestMessageErrorNotification(t *testing.T) {
t.Parallel()
merr := updateError(SubcodeInvalidNetworkField, []byte{0x01}, "prefix truncated")
want := &Notification{
Code: NotificationUpdateMessageError,
Subcode: SubcodeInvalidNetworkField,
Data: []byte{0x01},
}
if d := diff(t, want, merr.Notification()); d != "" {
t.Fatalf("unexpected NOTIFICATION (-want +got):\n%s", d)
}
// The Notification owns its Data: mutating one produced earlier must
// not reach back into the error, or into another Notification.
n := merr.Notification()
n.Data[0] = 0xff
if d := diff(t, []byte{0x01}, merr.Data); d != "" {
t.Fatalf("mutation reached the MessageError (-want +got):\n%s", d)
}
}