2016-12-07 18:46:22 +03:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2016-12-24 23:46:42 +03:00
|
|
|
|
|
|
|
"github.com/roasbeef/btcd/btcec"
|
2017-01-06 00:56:27 +03:00
|
|
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
2016-12-07 18:46:22 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
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-02-17 12:29:23 +03:00
|
|
|
func TestNodeAnnouncementValidation(t *testing.T) {
|
2016-12-07 18:46:22 +03:00
|
|
|
getKeys := func(s string) (*btcec.PrivateKey, *btcec.PublicKey) {
|
|
|
|
return btcec.PrivKeyFromBytes(btcec.S256(), []byte(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
nodePrivKey, nodePubKey := getKeys("node-id-1")
|
|
|
|
|
|
|
|
var hash []byte
|
|
|
|
na := &NodeAnnouncement{
|
|
|
|
Timestamp: maxUint32,
|
2017-02-17 12:29:23 +03:00
|
|
|
Addresses: someAddresses,
|
2016-12-07 18:46:22 +03:00
|
|
|
NodeID: nodePubKey,
|
|
|
|
RGBColor: someRGB,
|
|
|
|
Alias: someAlias,
|
2017-03-20 12:24:55 +03:00
|
|
|
Features: someFeatures,
|
2016-12-07 18:46:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
dataToSign, _ := na.DataToSign()
|
2017-01-06 00:56:27 +03:00
|
|
|
hash = chainhash.DoubleHashB(dataToSign)
|
2016-12-07 18:46:22 +03:00
|
|
|
|
|
|
|
signature, _ := nodePrivKey.Sign(hash)
|
|
|
|
na.Signature = signature
|
|
|
|
|
|
|
|
if err := na.Validate(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 12:29:23 +03:00
|
|
|
func TestNodeAnnouncementPayloadLength(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
|
|
|
}
|
|
|
|
}
|