mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-12-11 21:32:06 +01:00
htlcswitch/test: more realistic mock encryption
This mock is used in the switch test TestUpdateFailMalformedHTLCErrorConversion. But because the mock isn't very realistic, it doesn't detect problems in the handling of malformed failures in the link.
This commit is contained in:
@@ -419,12 +419,16 @@ func (o *mockObfuscator) Reextract(
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var fakeHmac = []byte("hmachmachmachmachmachmachmachmac")
|
||||||
|
|
||||||
func (o *mockObfuscator) EncryptFirstHop(failure lnwire.FailureMessage) (
|
func (o *mockObfuscator) EncryptFirstHop(failure lnwire.FailureMessage) (
|
||||||
lnwire.OpaqueReason, error) {
|
lnwire.OpaqueReason, error) {
|
||||||
|
|
||||||
o.failure = failure
|
o.failure = failure
|
||||||
|
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
|
b.Write(fakeHmac)
|
||||||
|
|
||||||
if err := lnwire.EncodeFailure(&b, failure, 0); err != nil {
|
if err := lnwire.EncodeFailure(&b, failure, 0); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -436,7 +440,12 @@ func (o *mockObfuscator) IntermediateEncrypt(reason lnwire.OpaqueReason) lnwire.
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *mockObfuscator) EncryptMalformedError(reason lnwire.OpaqueReason) lnwire.OpaqueReason {
|
func (o *mockObfuscator) EncryptMalformedError(reason lnwire.OpaqueReason) lnwire.OpaqueReason {
|
||||||
return reason
|
var b bytes.Buffer
|
||||||
|
b.Write(fakeHmac)
|
||||||
|
|
||||||
|
b.Write(reason)
|
||||||
|
|
||||||
|
return b.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
// mockDeobfuscator mock implementation of the failure deobfuscator which
|
// mockDeobfuscator mock implementation of the failure deobfuscator which
|
||||||
@@ -447,7 +456,13 @@ func newMockDeobfuscator() ErrorDecrypter {
|
|||||||
return &mockDeobfuscator{}
|
return &mockDeobfuscator{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *mockDeobfuscator) DecryptError(reason lnwire.OpaqueReason) (*ForwardingError, error) {
|
func (o *mockDeobfuscator) DecryptError(reason lnwire.OpaqueReason) (
|
||||||
|
*ForwardingError, error) {
|
||||||
|
|
||||||
|
if !bytes.Equal(reason[:32], fakeHmac) {
|
||||||
|
return nil, errors.New("fake decryption error")
|
||||||
|
}
|
||||||
|
reason = reason[32:]
|
||||||
|
|
||||||
r := bytes.NewReader(reason)
|
r := bytes.NewReader(reason)
|
||||||
failure, err := lnwire.DecodeFailure(r, 0)
|
failure, err := lnwire.DecodeFailure(r, 0)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package htlcswitch
|
package htlcswitch
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -23,7 +22,6 @@ import (
|
|||||||
"github.com/lightningnetwork/lnd/lntypes"
|
"github.com/lightningnetwork/lnd/lntypes"
|
||||||
"github.com/lightningnetwork/lnd/lnwire"
|
"github.com/lightningnetwork/lnd/lnwire"
|
||||||
"github.com/lightningnetwork/lnd/ticker"
|
"github.com/lightningnetwork/lnd/ticker"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -4088,10 +4086,10 @@ func TestSwitchHoldForward(t *testing.T) {
|
|||||||
expectedFailure := &lnwire.FailInvalidOnionKey{
|
expectedFailure := &lnwire.FailInvalidOnionKey{
|
||||||
OnionSHA256: shaOnionBlob,
|
OnionSHA256: shaOnionBlob,
|
||||||
}
|
}
|
||||||
var b bytes.Buffer
|
|
||||||
require.NoError(t, lnwire.EncodeFailure(&b, expectedFailure, 0))
|
|
||||||
|
|
||||||
assert.Equal(t, lnwire.OpaqueReason(b.Bytes()), failPacket.Reason)
|
fwdErr, err := newMockDeobfuscator().DecryptError(failPacket.Reason)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, expectedFailure, fwdErr.WireMessage())
|
||||||
|
|
||||||
assertNumCircuits(t, c.s, 0, 0)
|
assertNumCircuits(t, c.s, 0, 0)
|
||||||
|
|
||||||
@@ -5515,10 +5513,13 @@ func testSwitchAliasInterceptFail(t *testing.T, zeroConf bool) {
|
|||||||
failHtlc, ok := failPacket.htlc.(*lnwire.UpdateFailHTLC)
|
failHtlc, ok := failPacket.htlc.(*lnwire.UpdateFailHTLC)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
|
|
||||||
r := bytes.NewReader(failHtlc.Reason)
|
fwdErr, err := newMockDeobfuscator().DecryptError(
|
||||||
failure, err := lnwire.DecodeFailure(r, 0)
|
failHtlc.Reason,
|
||||||
|
)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
failure := fwdErr.WireMessage()
|
||||||
|
|
||||||
failureMsg, ok := failure.(*lnwire.FailTemporaryChannelFailure)
|
failureMsg, ok := failure.(*lnwire.FailTemporaryChannelFailure)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user