| 1 |
package handshake |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"math" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/stretchr/testify/assert" |
| 9 |
"github.com/stretchr/testify/require" |
| 10 |
"google.golang.org/protobuf/encoding/protowire" |
| 11 |
) |
| 12 |
|
| 13 |
func TestPayloadRoundTrip(t *testing.T) { |
| 14 |
t.Run("all fields set", func(t *testing.T) { |
| 15 |
data := MarshalPayload(nil, Payload{ |
| 16 |
Cert: []byte("test-cert-bytes"), |
| 17 |
CertVersion: 2, |
| 18 |
InitiatorIndex: 12345, |
| 19 |
ResponderIndex: 67890, |
| 20 |
Time: 1234567890, |
| 21 |
}) |
| 22 |
|
| 23 |
got, err := UnmarshalPayload(data) |
| 24 |
require.NoError(t, err) |
| 25 |
|
| 26 |
assert.Equal(t, []byte("test-cert-bytes"), got.Cert) |
| 27 |
assert.Equal(t, uint32(12345), got.InitiatorIndex) |
| 28 |
assert.Equal(t, uint32(67890), got.ResponderIndex) |
| 29 |
assert.Equal(t, uint64(1234567890), got.Time) |
| 30 |
assert.Equal(t, uint32(2), got.CertVersion) |
| 31 |
}) |
| 32 |
|
| 33 |
t.Run("minimal fields", func(t *testing.T) { |
| 34 |
data := MarshalPayload(nil, Payload{InitiatorIndex: 1}) |
| 35 |
|
| 36 |
got, err := UnmarshalPayload(data) |
| 37 |
require.NoError(t, err) |
| 38 |
|
| 39 |
assert.Equal(t, uint32(1), got.InitiatorIndex) |
| 40 |
assert.Equal(t, uint32(0), got.ResponderIndex) |
| 41 |
assert.Equal(t, uint64(0), got.Time) |
| 42 |
assert.Nil(t, got.Cert) |
| 43 |
}) |
| 44 |
|
| 45 |
t.Run("empty payload", func(t *testing.T) { |
| 46 |
data := MarshalPayload(nil, Payload{}) |
| 47 |
|
| 48 |
got, err := UnmarshalPayload(data) |
| 49 |
require.NoError(t, err) |
| 50 |
|
| 51 |
assert.Equal(t, uint32(0), got.InitiatorIndex) |
| 52 |
}) |
| 53 |
|
| 54 |
t.Run("large cert bytes", func(t *testing.T) { |
| 55 |
bigCert := make([]byte, 4096) |
| 56 |
for i := range bigCert { |
| 57 |
bigCert[i] = byte(i % 256) |
| 58 |
} |
| 59 |
|
| 60 |
data := MarshalPayload(nil, Payload{ |
| 61 |
Cert: bigCert, |
| 62 |
CertVersion: 2, |
| 63 |
InitiatorIndex: 999, |
| 64 |
}) |
| 65 |
|
| 66 |
got, err := UnmarshalPayload(data) |
| 67 |
require.NoError(t, err) |
| 68 |
|
| 69 |
assert.Equal(t, bigCert, got.Cert) |
| 70 |
assert.Equal(t, uint32(999), got.InitiatorIndex) |
| 71 |
}) |
| 72 |
|
| 73 |
t.Run("append to existing buffer", func(t *testing.T) { |
| 74 |
prefix := []byte("prefix") |
| 75 |
data := MarshalPayload(prefix, Payload{InitiatorIndex: 42}) |
| 76 |
|
| 77 |
assert.Equal(t, []byte("prefix"), data[:6]) |
| 78 |
|
| 79 |
got, err := UnmarshalPayload(data[6:]) |
| 80 |
require.NoError(t, err) |
| 81 |
assert.Equal(t, uint32(42), got.InitiatorIndex) |
| 82 |
}) |
| 83 |
} |
| 84 |
|
| 85 |
func TestPayloadUnknownFields(t *testing.T) { |
| 86 |
t.Run("unknown field in outer message is skipped", func(t *testing.T) { |
| 87 |
// Marshal a normal payload then append an unknown field (field 99, varint) |
| 88 |
data := MarshalPayload(nil, Payload{InitiatorIndex: 42}) |
| 89 |
data = protowire.AppendTag(data, 99, protowire.VarintType) |
| 90 |
data = protowire.AppendVarint(data, 12345) |
| 91 |
|
| 92 |
got, err := UnmarshalPayload(data) |
| 93 |
require.NoError(t, err) |
| 94 |
assert.Equal(t, uint32(42), got.InitiatorIndex) |
| 95 |
}) |
| 96 |
|
| 97 |
t.Run("unknown field in details is skipped", func(t *testing.T) { |
| 98 |
// Build details with a known field + unknown field |
| 99 |
var details []byte |
| 100 |
details = protowire.AppendTag(details, fieldInitiatorIndex, protowire.VarintType) |
| 101 |
details = protowire.AppendVarint(details, 77) |
| 102 |
// Unknown field 50, varint |
| 103 |
details = protowire.AppendTag(details, 50, protowire.VarintType) |
| 104 |
details = protowire.AppendVarint(details, 9999) |
| 105 |
// Another known field after the unknown one |
| 106 |
details = protowire.AppendTag(details, fieldResponderIndex, protowire.VarintType) |
| 107 |
details = protowire.AppendVarint(details, 88) |
| 108 |
|
| 109 |
// Wrap in outer message |
| 110 |
var data []byte |
| 111 |
data = protowire.AppendTag(data, 1, protowire.BytesType) |
| 112 |
data = protowire.AppendBytes(data, details) |
| 113 |
|
| 114 |
got, err := UnmarshalPayload(data) |
| 115 |
require.NoError(t, err) |
| 116 |
assert.Equal(t, uint32(77), got.InitiatorIndex) |
| 117 |
assert.Equal(t, uint32(88), got.ResponderIndex) |
| 118 |
}) |
| 119 |
|
| 120 |
t.Run("reserved fields 6 and 7 are skipped", func(t *testing.T) { |
| 121 |
// Fields 6 and 7 are reserved in the proto definition |
| 122 |
var details []byte |
| 123 |
details = protowire.AppendTag(details, fieldInitiatorIndex, protowire.VarintType) |
| 124 |
details = protowire.AppendVarint(details, 100) |
| 125 |
details = protowire.AppendTag(details, 6, protowire.VarintType) |
| 126 |
details = protowire.AppendVarint(details, 1) |
| 127 |
details = protowire.AppendTag(details, 7, protowire.VarintType) |
| 128 |
details = protowire.AppendVarint(details, 2) |
| 129 |
|
| 130 |
var data []byte |
| 131 |
data = protowire.AppendTag(data, 1, protowire.BytesType) |
| 132 |
data = protowire.AppendBytes(data, details) |
| 133 |
|
| 134 |
got, err := UnmarshalPayload(data) |
| 135 |
require.NoError(t, err) |
| 136 |
assert.Equal(t, uint32(100), got.InitiatorIndex) |
| 137 |
}) |
| 138 |
} |
| 139 |
|
| 140 |
func TestPayloadBytesConsumed(t *testing.T) { |
| 141 |
t.Run("all bytes consumed on valid input", func(t *testing.T) { |
| 142 |
original := Payload{ |
| 143 |
Cert: []byte("cert"), |
| 144 |
CertVersion: 2, |
| 145 |
InitiatorIndex: 100, |
| 146 |
ResponderIndex: 200, |
| 147 |
Time: 999, |
| 148 |
} |
| 149 |
data := MarshalPayload(nil, original) |
| 150 |
|
| 151 |
got, err := UnmarshalPayload(data) |
| 152 |
require.NoError(t, err) |
| 153 |
|
| 154 |
// Re-marshal and compare — proves we consumed and reproduced all fields |
| 155 |
remarshaled := MarshalPayload(nil, got) |
| 156 |
assert.Equal(t, data, remarshaled) |
| 157 |
}) |
| 158 |
} |
| 159 |
|
| 160 |
// wrapDetails wraps raw detail bytes in the outer NebulaHandshake envelope |
| 161 |
// so UnmarshalPayload can reach unmarshalPayloadDetails. |
| 162 |
func wrapDetails(details []byte) []byte { |
| 163 |
var out []byte |
| 164 |
out = protowire.AppendTag(out, 1, protowire.BytesType) |
| 165 |
out = protowire.AppendBytes(out, details) |
| 166 |
return out |
| 167 |
} |
| 168 |
|
| 169 |
func TestPayloadUnmarshalErrors(t *testing.T) { |
| 170 |
t.Run("nil input", func(t *testing.T) { |
| 171 |
got, err := UnmarshalPayload(nil) |
| 172 |
require.NoError(t, err) |
| 173 |
assert.Equal(t, uint32(0), got.InitiatorIndex) |
| 174 |
}) |
| 175 |
|
| 176 |
t.Run("truncated outer tag", func(t *testing.T) { |
| 177 |
_, err := UnmarshalPayload([]byte{0x80}) |
| 178 |
assert.Error(t, err) |
| 179 |
}) |
| 180 |
|
| 181 |
t.Run("truncated outer details field", func(t *testing.T) { |
| 182 |
_, err := UnmarshalPayload([]byte{0x0a, 0x64, 0x01, 0x02, 0x03, 0x04, 0x05}) |
| 183 |
assert.Error(t, err) |
| 184 |
}) |
| 185 |
|
| 186 |
t.Run("truncated outer unknown field", func(t *testing.T) { |
| 187 |
// Valid tag for unknown field 99 varint, but no value follows |
| 188 |
var data []byte |
| 189 |
data = protowire.AppendTag(data, 99, protowire.VarintType) |
| 190 |
_, err := UnmarshalPayload(data) |
| 191 |
assert.Error(t, err) |
| 192 |
}) |
| 193 |
|
| 194 |
t.Run("truncated details tag", func(t *testing.T) { |
| 195 |
_, err := UnmarshalPayload(wrapDetails([]byte{0x80})) |
| 196 |
assert.Error(t, err) |
| 197 |
}) |
| 198 |
|
| 199 |
t.Run("truncated cert bytes", func(t *testing.T) { |
| 200 |
// Field 1 (cert), bytes type, length 10 but only 2 bytes |
| 201 |
var details []byte |
| 202 |
details = protowire.AppendTag(details, fieldCert, protowire.BytesType) |
| 203 |
details = append(details, 0x0a, 0x01, 0x02) // length 10, only 2 bytes |
| 204 |
_, err := UnmarshalPayload(wrapDetails(details)) |
| 205 |
assert.Error(t, err) |
| 206 |
}) |
| 207 |
|
| 208 |
t.Run("truncated initiator index varint", func(t *testing.T) { |
| 209 |
var details []byte |
| 210 |
details = protowire.AppendTag(details, fieldInitiatorIndex, protowire.VarintType) |
| 211 |
details = append(details, 0x80) // incomplete varint |
| 212 |
_, err := UnmarshalPayload(wrapDetails(details)) |
| 213 |
assert.Error(t, err) |
| 214 |
}) |
| 215 |
|
| 216 |
t.Run("truncated responder index varint", func(t *testing.T) { |
| 217 |
var details []byte |
| 218 |
details = protowire.AppendTag(details, fieldResponderIndex, protowire.VarintType) |
| 219 |
details = append(details, 0x80) |
| 220 |
_, err := UnmarshalPayload(wrapDetails(details)) |
| 221 |
assert.Error(t, err) |
| 222 |
}) |
| 223 |
|
| 224 |
t.Run("truncated time varint", func(t *testing.T) { |
| 225 |
var details []byte |
| 226 |
details = protowire.AppendTag(details, fieldTime, protowire.VarintType) |
| 227 |
details = append(details, 0x80) |
| 228 |
_, err := UnmarshalPayload(wrapDetails(details)) |
| 229 |
assert.Error(t, err) |
| 230 |
}) |
| 231 |
|
| 232 |
t.Run("truncated cert version varint", func(t *testing.T) { |
| 233 |
var details []byte |
| 234 |
details = protowire.AppendTag(details, fieldCertVersion, protowire.VarintType) |
| 235 |
details = append(details, 0x80) |
| 236 |
_, err := UnmarshalPayload(wrapDetails(details)) |
| 237 |
assert.Error(t, err) |
| 238 |
}) |
| 239 |
|
| 240 |
t.Run("truncated unknown field in details", func(t *testing.T) { |
| 241 |
var details []byte |
| 242 |
details = protowire.AppendTag(details, 50, protowire.VarintType) |
| 243 |
details = append(details, 0x80) // incomplete varint |
| 244 |
_, err := UnmarshalPayload(wrapDetails(details)) |
| 245 |
assert.Error(t, err) |
| 246 |
}) |
| 247 |
|
| 248 |
t.Run("cert with wrong wire type rejected", func(t *testing.T) { |
| 249 |
// fieldCert as Varint instead of Bytes. |
| 250 |
var details []byte |
| 251 |
details = protowire.AppendTag(details, fieldCert, protowire.VarintType) |
| 252 |
details = protowire.AppendVarint(details, 42) |
| 253 |
_, err := UnmarshalPayload(wrapDetails(details)) |
| 254 |
assert.Error(t, err) |
| 255 |
}) |
| 256 |
|
| 257 |
t.Run("initiator index with wrong wire type rejected", func(t *testing.T) { |
| 258 |
// fieldInitiatorIndex as Bytes instead of Varint. |
| 259 |
var details []byte |
| 260 |
details = protowire.AppendTag(details, fieldInitiatorIndex, protowire.BytesType) |
| 261 |
details = protowire.AppendBytes(details, []byte{1, 2, 3}) |
| 262 |
_, err := UnmarshalPayload(wrapDetails(details)) |
| 263 |
assert.Error(t, err) |
| 264 |
}) |
| 265 |
|
| 266 |
t.Run("time with wrong wire type rejected", func(t *testing.T) { |
| 267 |
var details []byte |
| 268 |
details = protowire.AppendTag(details, fieldTime, protowire.BytesType) |
| 269 |
details = protowire.AppendBytes(details, []byte{1, 2, 3}) |
| 270 |
_, err := UnmarshalPayload(wrapDetails(details)) |
| 271 |
assert.Error(t, err) |
| 272 |
}) |
| 273 |
|
| 274 |
t.Run("cert version with wrong wire type rejected", func(t *testing.T) { |
| 275 |
var details []byte |
| 276 |
details = protowire.AppendTag(details, fieldCertVersion, protowire.BytesType) |
| 277 |
details = protowire.AppendBytes(details, []byte{1, 2, 3}) |
| 278 |
_, err := UnmarshalPayload(wrapDetails(details)) |
| 279 |
assert.Error(t, err) |
| 280 |
}) |
| 281 |
|
| 282 |
t.Run("repeated singular field follows proto3 last-wins", func(t *testing.T) { |
| 283 |
// Per proto3, multiple instances of a singular field are accepted and |
| 284 |
// the last value wins. We keep this behavior so that peers using |
| 285 |
// alternative encoders aren't rejected. |
| 286 |
var details []byte |
| 287 |
details = protowire.AppendTag(details, fieldInitiatorIndex, protowire.VarintType) |
| 288 |
details = protowire.AppendVarint(details, 1) |
| 289 |
details = protowire.AppendTag(details, fieldInitiatorIndex, protowire.VarintType) |
| 290 |
details = protowire.AppendVarint(details, 42) |
| 291 |
got, err := UnmarshalPayload(wrapDetails(details)) |
| 292 |
require.NoError(t, err) |
| 293 |
assert.Equal(t, uint32(42), got.InitiatorIndex) |
| 294 |
}) |
| 295 |
|
| 296 |
t.Run("initiator index varint overflow rejected", func(t *testing.T) { |
| 297 |
var details []byte |
| 298 |
details = protowire.AppendTag(details, fieldInitiatorIndex, protowire.VarintType) |
| 299 |
details = protowire.AppendVarint(details, math.MaxUint32+1) |
| 300 |
_, err := UnmarshalPayload(wrapDetails(details)) |
| 301 |
assert.Error(t, err) |
| 302 |
}) |
| 303 |
|
| 304 |
t.Run("cert version varint overflow rejected", func(t *testing.T) { |
| 305 |
var details []byte |
| 306 |
details = protowire.AppendTag(details, fieldCertVersion, protowire.VarintType) |
| 307 |
details = protowire.AppendVarint(details, math.MaxUint32+1) |
| 308 |
_, err := UnmarshalPayload(wrapDetails(details)) |
| 309 |
assert.Error(t, err) |
| 310 |
}) |
| 311 |
|
| 312 |
} |
| 313 |
|
| 314 |
// FuzzPayload feeds arbitrary bytes through UnmarshalPayload to confirm it |
| 315 |
// never panics, and for any input that parses cleanly, that re-marshal + |
| 316 |
// re-parse is a fix-point. Inputs come from an authenticated peer (post- |
| 317 |
// noise-decrypt), so the threat model is "valid peer behaving arbitrarily," |
| 318 |
// not "unauthenticated injection." |
| 319 |
func FuzzPayload(f *testing.F) { |
| 320 |
// Seed corpus with a handful of known-good shapes. |
| 321 |
f.Add(MarshalPayload(nil, Payload{})) |
| 322 |
f.Add(MarshalPayload(nil, Payload{Cert: []byte{1, 2, 3}, CertVersion: 2})) |
| 323 |
f.Add(MarshalPayload(nil, Payload{InitiatorIndex: 42, Time: 1})) |
| 324 |
f.Add(MarshalPayload(nil, Payload{ |
| 325 |
Cert: []byte("seed-cert"), |
| 326 |
InitiatorIndex: 1, |
| 327 |
ResponderIndex: 2, |
| 328 |
Time: 3, |
| 329 |
CertVersion: 2, |
| 330 |
})) |
| 331 |
f.Add([]byte{}) |
| 332 |
f.Add([]byte{0xff}) |
| 333 |
|
| 334 |
f.Fuzz(func(t *testing.T, data []byte) { |
| 335 |
p1, err := UnmarshalPayload(data) |
| 336 |
if err != nil { |
| 337 |
return |
| 338 |
} |
| 339 |
|
| 340 |
// For any input that parses, re-marshaling and re-parsing must |
| 341 |
// yield an equivalent Payload. This catches dispatch bugs (e.g. |
| 342 |
// emitting a field on marshal that we don't accept on parse) and |
| 343 |
// any non-idempotent parsing behavior. |
| 344 |
b2 := MarshalPayload(nil, p1) |
| 345 |
p2, err := UnmarshalPayload(b2) |
| 346 |
if err != nil { |
| 347 |
t.Fatalf("re-parse of self-marshaled payload failed: %v\nintermediate: %x\n", err, b2) |
| 348 |
} |
| 349 |
if !payloadsEqual(p1, p2) { |
| 350 |
t.Fatalf("re-marshal not idempotent\nfirst: %+v\nsecond: %+v", p1, p2) |
| 351 |
} |
| 352 |
}) |
| 353 |
} |
| 354 |
|
| 355 |
func payloadsEqual(a, b Payload) bool { |
| 356 |
return bytes.Equal(a.Cert, b.Cert) && |
| 357 |
a.InitiatorIndex == b.InitiatorIndex && |
| 358 |
a.ResponderIndex == b.ResponderIndex && |
| 359 |
a.Time == b.Time && |
| 360 |
a.CertVersion == b.CertVersion |
| 361 |
} |