2017-02-16 15:31:19 +03:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2017-02-23 03:00:59 +03:00
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
2017-02-16 15:31:19 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestFeaturesRemoteRequireError checks that we throw an error if remote peer
|
|
|
|
// has required feature which we don't support.
|
|
|
|
func TestFeaturesRemoteRequireError(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-02-17 17:28:11 +03:00
|
|
|
const (
|
|
|
|
first = "first"
|
|
|
|
second = "second"
|
2017-02-16 15:31:19 +03:00
|
|
|
)
|
|
|
|
|
2017-02-17 17:28:11 +03:00
|
|
|
localFeatures := NewFeatureVector([]Feature{
|
|
|
|
{first, OptionalFlag},
|
|
|
|
})
|
2017-02-16 15:31:19 +03:00
|
|
|
|
2017-02-17 17:28:11 +03:00
|
|
|
remoteFeatures := NewFeatureVector([]Feature{
|
2017-02-23 03:00:59 +03:00
|
|
|
{first, OptionalFlag},
|
2017-02-17 17:28:11 +03:00
|
|
|
{second, RequiredFlag},
|
|
|
|
})
|
2017-02-16 15:31:19 +03:00
|
|
|
|
|
|
|
if _, err := localFeatures.Compare(remoteFeatures); err == nil {
|
|
|
|
t.Fatal("error wasn't received")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFeaturesLocalRequireError checks that we throw an error if local peer has
|
|
|
|
// required feature which remote peer don't support.
|
|
|
|
func TestFeaturesLocalRequireError(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-02-17 17:28:11 +03:00
|
|
|
const (
|
|
|
|
first = "first"
|
|
|
|
second = "second"
|
2017-02-16 15:31:19 +03:00
|
|
|
)
|
|
|
|
|
2017-02-17 17:28:11 +03:00
|
|
|
localFeatures := NewFeatureVector([]Feature{
|
2017-02-23 03:00:59 +03:00
|
|
|
{first, OptionalFlag},
|
2017-02-17 17:28:11 +03:00
|
|
|
{second, RequiredFlag},
|
|
|
|
})
|
2017-02-16 15:31:19 +03:00
|
|
|
|
2017-02-17 17:28:11 +03:00
|
|
|
remoteFeatures := NewFeatureVector([]Feature{
|
|
|
|
{first, OptionalFlag},
|
|
|
|
})
|
2017-02-16 15:31:19 +03:00
|
|
|
|
|
|
|
if _, err := localFeatures.Compare(remoteFeatures); err == nil {
|
|
|
|
t.Fatal("error wasn't received")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestOptionalFeature checks that if remote peer don't have the feature but
|
|
|
|
// on our side this feature is optional than we mark this feature as disabled.
|
|
|
|
func TestOptionalFeature(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-02-17 17:28:11 +03:00
|
|
|
const first = "first"
|
2017-02-16 15:31:19 +03:00
|
|
|
|
2017-02-17 17:28:11 +03:00
|
|
|
localFeatures := NewFeatureVector([]Feature{
|
|
|
|
{first, OptionalFlag},
|
|
|
|
})
|
2017-02-16 15:31:19 +03:00
|
|
|
|
2017-02-17 17:28:11 +03:00
|
|
|
remoteFeatures := NewFeatureVector([]Feature{})
|
2017-02-16 15:31:19 +03:00
|
|
|
|
|
|
|
shared, err := localFeatures.Compare(remoteFeatures)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error while feature vector compare: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if shared.IsActive(first) {
|
|
|
|
t.Fatal("locally feature was set but remote peer notified us" +
|
|
|
|
" that it don't have it")
|
|
|
|
}
|
2017-04-20 02:16:55 +03:00
|
|
|
|
|
|
|
// A feature with a non-existent name shouldn't be active.
|
|
|
|
if shared.IsActive("nothere") {
|
|
|
|
t.Fatal("non-existent feature shouldn't be active")
|
|
|
|
}
|
2017-02-16 15:31:19 +03:00
|
|
|
}
|
|
|
|
|
2017-02-17 17:28:11 +03:00
|
|
|
// TestSetRequireAfterInit checks that we can change the feature flag after
|
|
|
|
// initialization.
|
|
|
|
func TestSetRequireAfterInit(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-02-17 17:28:11 +03:00
|
|
|
const first = "first"
|
|
|
|
|
|
|
|
localFeatures := NewFeatureVector([]Feature{
|
|
|
|
{first, OptionalFlag},
|
|
|
|
})
|
|
|
|
localFeatures.SetFeatureFlag(first, RequiredFlag)
|
|
|
|
remoteFeatures := NewFeatureVector([]Feature{})
|
|
|
|
|
|
|
|
_, err := localFeatures.Compare(remoteFeatures)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("feature was set as required but error wasn't "+
|
|
|
|
"returned: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 15:31:19 +03:00
|
|
|
// TestDecodeEncodeFeaturesVector checks that feature vector might be
|
|
|
|
// successfully encoded and decoded.
|
|
|
|
func TestDecodeEncodeFeaturesVector(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-02-17 17:28:11 +03:00
|
|
|
const first = "first"
|
2017-02-16 15:31:19 +03:00
|
|
|
|
2017-02-17 17:28:11 +03:00
|
|
|
f := NewFeatureVector([]Feature{
|
|
|
|
{first, OptionalFlag},
|
|
|
|
})
|
2017-02-16 15:31:19 +03:00
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
if err := f.Encode(&b); err != nil {
|
|
|
|
t.Fatalf("error while encoding feature vector: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nf, err := NewFeatureVectorFromReader(&b)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error while decoding feature vector: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert equality of the two instances.
|
|
|
|
if !reflect.DeepEqual(f.flags, nf.flags) {
|
|
|
|
t.Fatalf("encode/decode feature vector don't match %v vs "+
|
2017-02-23 03:00:59 +03:00
|
|
|
"%v", spew.Sdump(f), spew.Sdump(nf))
|
2017-02-16 15:31:19 +03:00
|
|
|
}
|
|
|
|
}
|
2017-04-20 02:16:55 +03:00
|
|
|
|
|
|
|
func TestFeatureFlagString(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-04-20 02:16:55 +03:00
|
|
|
if OptionalFlag.String() != "optional" {
|
|
|
|
t.Fatalf("incorrect string, expected optional got %v",
|
|
|
|
OptionalFlag.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if RequiredFlag.String() != "required" {
|
|
|
|
t.Fatalf("incorrect string, expected required got %v",
|
|
|
|
OptionalFlag.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
fakeFlag := featureFlag(9)
|
|
|
|
if fakeFlag.String() != "<unknown>" {
|
|
|
|
t.Fatalf("incorrect string, expected <unknown> got %v",
|
|
|
|
fakeFlag.String())
|
|
|
|
}
|
|
|
|
}
|