lnd.xprv/lnwire/accept_channel_test.go
Steven Roose 2fb7172725
lnwire: Add upfront shutdown messages and feature bit
This commit adds the feature bit and additional fields
required in `open_channel` and `accept_channel` wire
messages for `option_upfront_shutdown_script`.
2019-12-03 11:38:21 +02:00

72 lines
1.6 KiB
Go

package lnwire
import (
"bytes"
"testing"
"github.com/btcsuite/btcd/btcec"
)
// TestDecodeAcceptChannel tests decoding of an accept channel wire message with
// and without the optional upfront shutdown script.
func TestDecodeAcceptChannel(t *testing.T) {
tests := []struct {
name string
shutdownScript DeliveryAddress
}{
{
name: "no upfront shutdown script",
shutdownScript: nil,
},
{
name: "empty byte array",
shutdownScript: []byte{},
},
{
name: "upfront shutdown script set",
shutdownScript: []byte("example"),
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
priv, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
t.Fatalf("cannot create privkey: %v", err)
}
pk := priv.PubKey()
encoded := &AcceptChannel{
PendingChannelID: [32]byte{},
FundingKey: pk,
RevocationPoint: pk,
PaymentPoint: pk,
DelayedPaymentPoint: pk,
HtlcPoint: pk,
FirstCommitmentPoint: pk,
UpfrontShutdownScript: test.shutdownScript,
}
buf := &bytes.Buffer{}
if _, err := WriteMessage(buf, encoded, 0); err != nil {
t.Fatalf("cannot write message: %v", err)
}
msg, err := ReadMessage(buf, 0)
if err != nil {
t.Fatalf("cannot read message: %v", err)
}
decoded := msg.(*AcceptChannel)
if !bytes.Equal(
decoded.UpfrontShutdownScript, encoded.UpfrontShutdownScript,
) {
t.Fatalf("decoded script: %x does not equal encoded script: %x",
decoded.UpfrontShutdownScript, encoded.UpfrontShutdownScript)
}
})
}
}