accept_channel_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package lnwire
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/btcsuite/btcd/btcec/v2"
  6. )
  7. // TestDecodeAcceptChannel tests decoding of an accept channel wire message with
  8. // and without the optional upfront shutdown script.
  9. func TestDecodeAcceptChannel(t *testing.T) {
  10. tests := []struct {
  11. name string
  12. shutdownScript DeliveryAddress
  13. }{
  14. {
  15. name: "no upfront shutdown script",
  16. shutdownScript: nil,
  17. },
  18. {
  19. name: "empty byte array",
  20. shutdownScript: []byte{},
  21. },
  22. {
  23. name: "upfront shutdown script set",
  24. shutdownScript: []byte("example"),
  25. },
  26. }
  27. for _, test := range tests {
  28. test := test
  29. t.Run(test.name, func(t *testing.T) {
  30. priv, err := btcec.NewPrivateKey()
  31. if err != nil {
  32. t.Fatalf("cannot create privkey: %v", err)
  33. }
  34. pk := priv.PubKey()
  35. encoded := &AcceptChannel{
  36. PendingChannelID: [32]byte{},
  37. FundingKey: pk,
  38. RevocationPoint: pk,
  39. PaymentPoint: pk,
  40. DelayedPaymentPoint: pk,
  41. HtlcPoint: pk,
  42. FirstCommitmentPoint: pk,
  43. UpfrontShutdownScript: test.shutdownScript,
  44. }
  45. buf := &bytes.Buffer{}
  46. if _, err := WriteMessage(buf, encoded, 0); err != nil {
  47. t.Fatalf("cannot write message: %v", err)
  48. }
  49. msg, err := ReadMessage(buf, 0)
  50. if err != nil {
  51. t.Fatalf("cannot read message: %v", err)
  52. }
  53. decoded := msg.(*AcceptChannel)
  54. if !bytes.Equal(
  55. decoded.UpfrontShutdownScript, encoded.UpfrontShutdownScript,
  56. ) {
  57. t.Fatalf("decoded script: %x does not equal encoded script: %x",
  58. decoded.UpfrontShutdownScript, encoded.UpfrontShutdownScript)
  59. }
  60. })
  61. }
  62. }