2016-01-05 19:19:22 +03:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
2016-05-31 02:49:48 +03:00
|
|
|
"bytes"
|
|
|
|
"reflect"
|
2016-01-05 19:19:22 +03:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-05-31 02:49:48 +03:00
|
|
|
func TestHTLCAddRequestEncodeDecode(t *testing.T) {
|
|
|
|
redemptionHashes := make([][20]byte, 1)
|
|
|
|
copy(redemptionHashes[0][:], bytes.Repeat([]byte{0x09}, 20))
|
2016-01-05 19:19:22 +03:00
|
|
|
|
2016-05-31 02:49:48 +03:00
|
|
|
// First create a new HTLCAR message.
|
|
|
|
addReq := &HTLCAddRequest{
|
2016-01-05 19:19:22 +03:00
|
|
|
ChannelID: uint64(12345678),
|
|
|
|
Expiry: uint32(144),
|
|
|
|
Amount: CreditsAmount(123456000),
|
|
|
|
ContractType: uint8(17),
|
|
|
|
RedemptionHashes: redemptionHashes,
|
2016-05-31 02:49:48 +03:00
|
|
|
OnionBlob: []byte{255, 0, 255, 0, 255, 0, 255, 0},
|
2016-01-05 19:19:22 +03:00
|
|
|
}
|
|
|
|
|
2016-05-31 02:49:48 +03:00
|
|
|
// Next encode the HTLCAR message into an empty bytes buffer.
|
|
|
|
var b bytes.Buffer
|
|
|
|
if err := addReq.Encode(&b, 0); err != nil {
|
|
|
|
t.Fatalf("unable to encode HTLCAddRequest: %v", err)
|
|
|
|
}
|
2016-01-05 19:19:22 +03:00
|
|
|
|
2016-05-31 02:49:48 +03:00
|
|
|
// Deserialize the encoded HTLCAR message into a new empty struct.
|
|
|
|
addReq2 := &HTLCAddRequest{}
|
|
|
|
if err := addReq2.Decode(&b, 0); err != nil {
|
|
|
|
t.Fatalf("unable to decode HTLCAddRequest: %v", err)
|
|
|
|
}
|
2016-01-05 19:19:22 +03:00
|
|
|
|
2016-05-31 02:49:48 +03:00
|
|
|
// Assert equality of the two instances.
|
|
|
|
if !reflect.DeepEqual(addReq, addReq2) {
|
|
|
|
t.Fatalf("encode/decode error messages don't match %#v vs %#v",
|
|
|
|
addReq, addReq2)
|
|
|
|
}
|
2016-01-05 19:19:22 +03:00
|
|
|
}
|