2017-02-16 15:31:19 +03:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestInitEncodeDecode(t *testing.T) {
|
2017-02-17 17:28:11 +03:00
|
|
|
const somefeature = "somefeature"
|
2017-02-16 15:31:19 +03:00
|
|
|
|
2017-02-17 17:28:11 +03:00
|
|
|
gf := NewFeatureVector([]Feature{
|
|
|
|
{somefeature, OptionalFlag},
|
|
|
|
})
|
|
|
|
lf := NewFeatureVector([]Feature{
|
|
|
|
{somefeature, OptionalFlag},
|
|
|
|
})
|
2017-02-16 15:31:19 +03:00
|
|
|
|
|
|
|
init1 := &Init{
|
|
|
|
GlobalFeatures: gf,
|
|
|
|
LocalFeatures: lf,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next encode the init message into an empty bytes buffer.
|
|
|
|
var b bytes.Buffer
|
|
|
|
if err := init1.Encode(&b, 0); err != nil {
|
|
|
|
t.Fatalf("unable to encode init: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize the encoded init message into a new empty struct.
|
|
|
|
init2 := &Init{}
|
|
|
|
if err := init2.Decode(&b, 0); err != nil {
|
|
|
|
t.Fatalf("unable to decode init: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We not encode the feature map in feature vector, for that reason the
|
2017-02-17 17:28:11 +03:00
|
|
|
// init messages will differ. Set feature map with nil in
|
2017-02-16 15:31:19 +03:00
|
|
|
// order to use deep equal function.
|
2017-02-17 17:28:11 +03:00
|
|
|
init1.GlobalFeatures.featuresMap = nil
|
|
|
|
init1.LocalFeatures.featuresMap = nil
|
2017-02-16 15:31:19 +03:00
|
|
|
|
|
|
|
// Assert equality of the two instances.
|
|
|
|
if !reflect.DeepEqual(init1, init2) {
|
|
|
|
t.Fatalf("encode/decode init messages don't match %#v vs %#v",
|
|
|
|
init1, init2)
|
|
|
|
}
|
|
|
|
}
|