2019-09-05 16:05:38 +03:00
|
|
|
package hop_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
|
|
|
"github.com/lightningnetwork/lnd/record"
|
|
|
|
)
|
|
|
|
|
|
|
|
type decodePayloadTest struct {
|
|
|
|
name string
|
|
|
|
payload []byte
|
|
|
|
expErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
var decodePayloadTests = []decodePayloadTest{
|
2019-10-31 07:19:08 +03:00
|
|
|
{
|
|
|
|
name: "final hop valid",
|
|
|
|
payload: []byte{0x02, 0x00, 0x04, 0x00},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "intermediate hop valid",
|
|
|
|
payload: []byte{0x02, 0x00, 0x04, 0x00, 0x06, 0x08, 0x01, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
},
|
|
|
|
},
|
2019-09-05 16:05:38 +03:00
|
|
|
{
|
|
|
|
name: "final hop no amount",
|
|
|
|
payload: []byte{0x04, 0x00},
|
|
|
|
expErr: hop.ErrInvalidPayload{
|
2019-10-31 07:19:08 +03:00
|
|
|
Type: record.AmtOnionType,
|
|
|
|
Violation: hop.OmittedViolation,
|
|
|
|
FinalHop: true,
|
2019-09-05 16:05:38 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "intermediate hop no amount",
|
|
|
|
payload: []byte{0x04, 0x00, 0x06, 0x08, 0x01, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
},
|
|
|
|
expErr: hop.ErrInvalidPayload{
|
2019-10-31 07:19:08 +03:00
|
|
|
Type: record.AmtOnionType,
|
|
|
|
Violation: hop.OmittedViolation,
|
|
|
|
FinalHop: false,
|
2019-09-05 16:05:38 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "final hop no expiry",
|
|
|
|
payload: []byte{0x02, 0x00},
|
|
|
|
expErr: hop.ErrInvalidPayload{
|
2019-10-31 07:19:08 +03:00
|
|
|
Type: record.LockTimeOnionType,
|
|
|
|
Violation: hop.OmittedViolation,
|
|
|
|
FinalHop: true,
|
2019-09-05 16:05:38 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "intermediate hop no expiry",
|
|
|
|
payload: []byte{0x02, 0x00, 0x06, 0x08, 0x01, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
},
|
|
|
|
expErr: hop.ErrInvalidPayload{
|
2019-10-31 07:19:08 +03:00
|
|
|
Type: record.LockTimeOnionType,
|
|
|
|
Violation: hop.OmittedViolation,
|
|
|
|
FinalHop: false,
|
2019-09-05 16:05:38 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "final hop next sid present",
|
|
|
|
payload: []byte{0x02, 0x00, 0x04, 0x00, 0x06, 0x08, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
},
|
|
|
|
expErr: hop.ErrInvalidPayload{
|
2019-10-31 07:19:08 +03:00
|
|
|
Type: record.NextHopOnionType,
|
|
|
|
Violation: hop.IncludedViolation,
|
|
|
|
FinalHop: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "required type after omitted hop id",
|
2019-10-31 07:21:10 +03:00
|
|
|
payload: []byte{0x02, 0x00, 0x04, 0x00, 0x08, 0x00},
|
2019-10-31 07:19:08 +03:00
|
|
|
expErr: hop.ErrInvalidPayload{
|
|
|
|
Type: 8,
|
|
|
|
Violation: hop.RequiredViolation,
|
|
|
|
FinalHop: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "required type after included hop id",
|
2019-10-31 07:21:10 +03:00
|
|
|
payload: []byte{0x02, 0x00, 0x04, 0x00, 0x06, 0x08, 0x01, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
|
2019-10-31 07:19:08 +03:00
|
|
|
},
|
|
|
|
expErr: hop.ErrInvalidPayload{
|
|
|
|
Type: 8,
|
|
|
|
Violation: hop.RequiredViolation,
|
|
|
|
FinalHop: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-10-31 07:20:49 +03:00
|
|
|
name: "required type zero final hop",
|
2019-10-31 07:21:10 +03:00
|
|
|
payload: []byte{0x00, 0x00, 0x02, 0x00, 0x04, 0x00},
|
2019-10-31 07:19:08 +03:00
|
|
|
expErr: hop.ErrInvalidPayload{
|
|
|
|
Type: 0,
|
|
|
|
Violation: hop.RequiredViolation,
|
|
|
|
FinalHop: true,
|
2019-09-05 16:05:38 +03:00
|
|
|
},
|
|
|
|
},
|
2019-10-31 07:20:49 +03:00
|
|
|
{
|
|
|
|
name: "required type zero final hop zero sid",
|
2019-10-31 07:21:10 +03:00
|
|
|
payload: []byte{0x00, 0x00, 0x02, 0x00, 0x04, 0x00, 0x06, 0x08,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
2019-10-31 07:20:49 +03:00
|
|
|
},
|
|
|
|
expErr: hop.ErrInvalidPayload{
|
2019-10-31 07:21:10 +03:00
|
|
|
Type: 6,
|
|
|
|
Violation: hop.IncludedViolation,
|
2019-10-31 07:20:49 +03:00
|
|
|
FinalHop: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "required type zero intermediate hop",
|
2019-10-31 07:21:10 +03:00
|
|
|
payload: []byte{0x00, 0x00, 0x02, 0x00, 0x04, 0x00, 0x06, 0x08,
|
|
|
|
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
2019-10-31 07:20:49 +03:00
|
|
|
},
|
|
|
|
expErr: hop.ErrInvalidPayload{
|
|
|
|
Type: 0,
|
|
|
|
Violation: hop.RequiredViolation,
|
|
|
|
FinalHop: false,
|
|
|
|
},
|
|
|
|
},
|
2019-09-05 16:05:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestDecodeHopPayloadRecordValidation asserts that parsing the payloads in the
|
|
|
|
// tests yields the expected errors depending on whether the proper fields were
|
|
|
|
// included or omitted.
|
|
|
|
func TestDecodeHopPayloadRecordValidation(t *testing.T) {
|
|
|
|
for _, test := range decodePayloadTests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
testDecodeHopPayloadValidation(t, test)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDecodeHopPayloadValidation(t *testing.T, test decodePayloadTest) {
|
|
|
|
_, err := hop.NewPayloadFromReader(bytes.NewReader(test.payload))
|
|
|
|
if !reflect.DeepEqual(test.expErr, err) {
|
|
|
|
t.Fatalf("expected error mismatch, want: %v, got: %v",
|
|
|
|
test.expErr, err)
|
|
|
|
}
|
|
|
|
}
|