mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-27 14:11:04 +02:00
lnwire: replace all wire msg tests with a single property-based test
This commit does away with all the old manual message equality tests and replace it with a single property-based test that uses the testing/quick package. This test uses a single scenario which MUST hold for all the messages type and all possible messages generated for those types. As a result we are able to do away with all the prior manually generated test data as the fuzzer to scan the input space looking for a message that violates the scenario.
This commit is contained in:
@@ -1,76 +1,14 @@
|
||||
package lnwire
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNodeAnnouncementEncodeDecode(t *testing.T) {
|
||||
na := &NodeAnnouncement{
|
||||
Signature: someSig,
|
||||
Timestamp: maxUint32,
|
||||
NodeID: pubKey,
|
||||
RGBColor: someRGB,
|
||||
Alias: someAlias,
|
||||
Addresses: someAddresses,
|
||||
Features: someFeatures,
|
||||
}
|
||||
|
||||
// Next encode the NA message into an empty bytes buffer.
|
||||
var b bytes.Buffer
|
||||
if err := na.Encode(&b, 0); err != nil {
|
||||
t.Fatalf("unable to encode NodeAnnouncement: %v", err)
|
||||
}
|
||||
|
||||
// Deserialize the encoded NA message into a new empty struct.
|
||||
na2 := &NodeAnnouncement{}
|
||||
if err := na2.Decode(&b, 0); err != nil {
|
||||
t.Fatalf("unable to decode NodeAnnouncement: %v", err)
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
// Assert equality of the two instances.
|
||||
if !reflect.DeepEqual(na, na2) {
|
||||
t.Fatalf("encode/decode error messages don't match %#v vs %#v",
|
||||
na, na2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeAnnoucementPayloadLength(t *testing.T) {
|
||||
na := &NodeAnnouncement{
|
||||
Signature: someSig,
|
||||
Timestamp: maxUint32,
|
||||
NodeID: pubKey,
|
||||
RGBColor: someRGB,
|
||||
Alias: someAlias,
|
||||
Addresses: someAddresses,
|
||||
Features: someFeatures,
|
||||
}
|
||||
|
||||
var b bytes.Buffer
|
||||
if err := na.Encode(&b, 0); err != nil {
|
||||
t.Fatalf("unable to encode node: %v", err)
|
||||
}
|
||||
|
||||
serializedLength := uint32(b.Len())
|
||||
if serializedLength != 167 {
|
||||
t.Fatalf("payload length estimate is incorrect: expected %v "+
|
||||
"got %v", 167, serializedLength)
|
||||
}
|
||||
|
||||
if na.MaxPayloadLength(0) != 8192 {
|
||||
t.Fatalf("max payload length doesn't match: expected 8192, got %v",
|
||||
na.MaxPayloadLength(0))
|
||||
}
|
||||
}
|
||||
import "testing"
|
||||
|
||||
func TestValidateAlias(t *testing.T) {
|
||||
if err := someAlias.Validate(); err != nil {
|
||||
aliasStr := "012345678901234567890"
|
||||
alias := NewAlias(aliasStr)
|
||||
if err := alias.Validate(); err != nil {
|
||||
t.Fatalf("alias was invalid: %v", err)
|
||||
}
|
||||
if aliasStr != alias.String() {
|
||||
t.Fatalf("aliases don't match")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user