2016-12-07 18:46:22 +03:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNodeAnnouncementEncodeDecode(t *testing.T) {
|
2017-03-20 12:24:55 +03:00
|
|
|
na := &NodeAnnouncement{
|
2016-12-07 18:46:22 +03:00
|
|
|
Signature: someSig,
|
|
|
|
Timestamp: maxUint32,
|
|
|
|
NodeID: pubKey,
|
|
|
|
RGBColor: someRGB,
|
|
|
|
Alias: someAlias,
|
2017-02-17 12:29:23 +03:00
|
|
|
Addresses: someAddresses,
|
2017-03-20 12:24:55 +03:00
|
|
|
Features: someFeatures,
|
2016-12-07 18:46:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next encode the NA message into an empty bytes buffer.
|
|
|
|
var b bytes.Buffer
|
2017-03-20 12:24:55 +03:00
|
|
|
if err := na.Encode(&b, 0); err != nil {
|
2016-12-07 18:46:22 +03:00
|
|
|
t.Fatalf("unable to encode NodeAnnouncement: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize the encoded NA message into a new empty struct.
|
2017-03-20 12:24:55 +03:00
|
|
|
na2 := &NodeAnnouncement{}
|
|
|
|
if err := na2.Decode(&b, 0); err != nil {
|
2016-12-07 18:46:22 +03:00
|
|
|
t.Fatalf("unable to decode NodeAnnouncement: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-03-20 12:24:55 +03:00
|
|
|
// We do not encode the feature map in feature vector, for that reason
|
|
|
|
// the node announcement messages will differ. Set feature map with nil
|
|
|
|
// in order to use deep equal function.
|
|
|
|
na.Features.featuresMap = nil
|
|
|
|
|
2016-12-07 18:46:22 +03:00
|
|
|
// Assert equality of the two instances.
|
2017-03-20 12:24:55 +03:00
|
|
|
if !reflect.DeepEqual(na, na2) {
|
2016-12-07 18:46:22 +03:00
|
|
|
t.Fatalf("encode/decode error messages don't match %#v vs %#v",
|
2017-03-20 12:24:55 +03:00
|
|
|
na, na2)
|
2016-12-07 18:46:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 21:37:41 +03:00
|
|
|
func TestNodeAnnoucementPayloadLength(t *testing.T) {
|
2016-12-24 23:46:42 +03:00
|
|
|
na := &NodeAnnouncement{
|
|
|
|
Signature: someSig,
|
|
|
|
Timestamp: maxUint32,
|
|
|
|
NodeID: pubKey,
|
|
|
|
RGBColor: someRGB,
|
|
|
|
Alias: someAlias,
|
2017-02-17 12:29:23 +03:00
|
|
|
Addresses: someAddresses,
|
2017-03-20 12:24:55 +03:00
|
|
|
Features: someFeatures,
|
2016-12-24 23:46:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
if err := na.Encode(&b, 0); err != nil {
|
|
|
|
t.Fatalf("unable to encode node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
serializedLength := uint32(b.Len())
|
2017-03-20 12:24:55 +03:00
|
|
|
if serializedLength != 167 {
|
2016-12-24 23:46:42 +03:00
|
|
|
t.Fatalf("payload length estimate is incorrect: expected %v "+
|
2017-03-20 12:24:55 +03:00
|
|
|
"got %v", 167, serializedLength)
|
2017-02-17 12:29:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if na.MaxPayloadLength(0) != 8192 {
|
|
|
|
t.Fatalf("max payload length doesn't match: expected 8192, got %v",
|
|
|
|
na.MaxPayloadLength(0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateAlias(t *testing.T) {
|
|
|
|
if err := someAlias.Validate(); err != nil {
|
|
|
|
t.Fatalf("alias was invalid: %v", err)
|
2016-12-24 23:46:42 +03:00
|
|
|
}
|
|
|
|
}
|