2019-09-05 16:05:38 +03:00
|
|
|
package hop_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
2019-11-05 02:10:00 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2019-09-05 16:05:38 +03:00
|
|
|
"github.com/lightningnetwork/lnd/record"
|
|
|
|
)
|
|
|
|
|
2021-03-25 05:47:33 +03:00
|
|
|
const testUnknownRequiredType = 0x10
|
|
|
|
|
2019-09-05 16:05:38 +03:00
|
|
|
type decodePayloadTest struct {
|
2019-11-19 14:32:56 +03:00
|
|
|
name string
|
|
|
|
payload []byte
|
|
|
|
expErr error
|
|
|
|
expCustomRecords map[uint64][]byte
|
|
|
|
shouldHaveMPP bool
|
2019-09-05 16:05:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-03-25 05:47:33 +03:00
|
|
|
name: "required type after omitted hop id",
|
|
|
|
payload: []byte{
|
|
|
|
0x02, 0x00, 0x04, 0x00,
|
|
|
|
testUnknownRequiredType, 0x00,
|
|
|
|
},
|
2019-10-31 07:19:08 +03:00
|
|
|
expErr: hop.ErrInvalidPayload{
|
2021-03-25 05:47:33 +03:00
|
|
|
Type: testUnknownRequiredType,
|
2019-10-31 07:19:08 +03:00
|
|
|
Violation: hop.RequiredViolation,
|
|
|
|
FinalHop: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "required type after included hop id",
|
2021-03-25 05:47:33 +03:00
|
|
|
payload: []byte{
|
|
|
|
0x02, 0x00, 0x04, 0x00, 0x06, 0x08, 0x01, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
testUnknownRequiredType, 0x00,
|
2019-10-31 07:19:08 +03:00
|
|
|
},
|
|
|
|
expErr: hop.ErrInvalidPayload{
|
2021-03-25 05:47:33 +03:00
|
|
|
Type: testUnknownRequiredType,
|
2019-10-31 07:19:08 +03:00
|
|
|
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-11-05 02:10:00 +03:00
|
|
|
Type: record.NextHopOnionType,
|
2019-10-31 07:21:10 +03:00
|
|
|
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-11-11 12:37:24 +03:00
|
|
|
{
|
|
|
|
name: "required type in custom range",
|
|
|
|
payload: []byte{0x02, 0x00, 0x04, 0x00,
|
2019-11-19 14:32:56 +03:00
|
|
|
0xfe, 0x00, 0x01, 0x00, 0x00, 0x02, 0x10, 0x11,
|
|
|
|
},
|
|
|
|
expCustomRecords: map[uint64][]byte{
|
|
|
|
65536: {0x10, 0x11},
|
2019-11-11 12:37:24 +03:00
|
|
|
},
|
|
|
|
},
|
2019-11-05 02:10:00 +03:00
|
|
|
{
|
|
|
|
name: "valid intermediate hop",
|
|
|
|
payload: []byte{0x02, 0x00, 0x04, 0x00, 0x06, 0x08, 0x01, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
},
|
|
|
|
expErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid final hop",
|
|
|
|
payload: []byte{0x02, 0x00, 0x04, 0x00},
|
|
|
|
expErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "intermediate hop with mpp",
|
|
|
|
payload: []byte{
|
|
|
|
// amount
|
|
|
|
0x02, 0x00,
|
|
|
|
// cltv
|
|
|
|
0x04, 0x00,
|
|
|
|
// next hop id
|
|
|
|
0x06, 0x08,
|
|
|
|
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
// mpp
|
|
|
|
0x08, 0x21,
|
|
|
|
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
|
|
|
|
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
|
|
|
|
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
|
|
|
|
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
|
|
|
|
0x08,
|
|
|
|
},
|
|
|
|
expErr: hop.ErrInvalidPayload{
|
|
|
|
Type: record.MPPOnionType,
|
|
|
|
Violation: hop.IncludedViolation,
|
|
|
|
FinalHop: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "final hop with mpp",
|
|
|
|
payload: []byte{
|
|
|
|
// amount
|
|
|
|
0x02, 0x00,
|
|
|
|
// cltv
|
|
|
|
0x04, 0x00,
|
|
|
|
// mpp
|
|
|
|
0x08, 0x21,
|
|
|
|
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
|
|
|
|
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
|
|
|
|
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
|
|
|
|
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
|
|
|
|
0x08,
|
|
|
|
},
|
|
|
|
expErr: nil,
|
|
|
|
shouldHaveMPP: true,
|
|
|
|
},
|
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) {
|
2019-11-05 02:10:00 +03:00
|
|
|
var (
|
|
|
|
testTotalMsat = lnwire.MilliSatoshi(8)
|
|
|
|
testAddr = [32]byte{
|
|
|
|
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
|
|
|
|
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
|
|
|
|
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
|
|
|
|
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
p, err := hop.NewPayloadFromReader(bytes.NewReader(test.payload))
|
2019-09-05 16:05:38 +03:00
|
|
|
if !reflect.DeepEqual(test.expErr, err) {
|
|
|
|
t.Fatalf("expected error mismatch, want: %v, got: %v",
|
|
|
|
test.expErr, err)
|
|
|
|
}
|
2019-11-05 02:10:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert MPP fields if we expect them.
|
|
|
|
if test.shouldHaveMPP {
|
|
|
|
if p.MPP == nil {
|
|
|
|
t.Fatalf("payload should have MPP record")
|
|
|
|
}
|
|
|
|
if p.MPP.TotalMsat() != testTotalMsat {
|
|
|
|
t.Fatalf("invalid total msat")
|
|
|
|
}
|
|
|
|
if p.MPP.PaymentAddr() != testAddr {
|
|
|
|
t.Fatalf("invalid payment addr")
|
|
|
|
}
|
|
|
|
} else if p.MPP != nil {
|
|
|
|
t.Fatalf("unexpected MPP payload")
|
|
|
|
}
|
2019-11-19 14:32:56 +03:00
|
|
|
|
|
|
|
// Convert expected nil map to empty map, because we always expect an
|
|
|
|
// initiated map from the payload.
|
2019-12-12 02:01:55 +03:00
|
|
|
expCustomRecords := make(record.CustomSet)
|
2019-11-19 14:32:56 +03:00
|
|
|
if test.expCustomRecords != nil {
|
|
|
|
expCustomRecords = test.expCustomRecords
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(expCustomRecords, p.CustomRecords()) {
|
|
|
|
t.Fatalf("invalid custom records")
|
|
|
|
}
|
2019-09-05 16:05:38 +03:00
|
|
|
}
|