2019-02-09 07:01:59 +03:00
|
|
|
package wtwire_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
"github.com/lightningnetwork/lnd/watchtower/wtwire"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testnetChainHash = *chaincfg.TestNet3Params.GenesisHash
|
|
|
|
mainnetChainHash = *chaincfg.MainNetParams.GenesisHash
|
|
|
|
)
|
|
|
|
|
|
|
|
type checkRemoteInitTest struct {
|
|
|
|
name string
|
|
|
|
lFeatures *lnwire.RawFeatureVector
|
|
|
|
lHash chainhash.Hash
|
|
|
|
rFeatures *lnwire.RawFeatureVector
|
|
|
|
rHash chainhash.Hash
|
|
|
|
expErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
var checkRemoteInitTests = []checkRemoteInitTest{
|
|
|
|
{
|
|
|
|
name: "same chain, local-optional remote-required",
|
2019-06-14 03:34:16 +03:00
|
|
|
lFeatures: lnwire.NewRawFeatureVector(wtwire.AltruistSessionsOptional),
|
2019-02-09 07:01:59 +03:00
|
|
|
lHash: testnetChainHash,
|
2019-06-14 03:34:16 +03:00
|
|
|
rFeatures: lnwire.NewRawFeatureVector(wtwire.AltruistSessionsRequired),
|
2019-02-09 07:01:59 +03:00
|
|
|
rHash: testnetChainHash,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "same chain, local-required remote-optional",
|
2019-06-14 03:34:16 +03:00
|
|
|
lFeatures: lnwire.NewRawFeatureVector(wtwire.AltruistSessionsRequired),
|
2019-02-09 07:01:59 +03:00
|
|
|
lHash: testnetChainHash,
|
2019-06-14 03:34:16 +03:00
|
|
|
rFeatures: lnwire.NewRawFeatureVector(wtwire.AltruistSessionsOptional),
|
2019-02-09 07:01:59 +03:00
|
|
|
rHash: testnetChainHash,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "different chain, local-optional remote-required",
|
2019-06-14 03:34:16 +03:00
|
|
|
lFeatures: lnwire.NewRawFeatureVector(wtwire.AltruistSessionsOptional),
|
2019-02-09 07:01:59 +03:00
|
|
|
lHash: testnetChainHash,
|
2019-06-14 03:34:16 +03:00
|
|
|
rFeatures: lnwire.NewRawFeatureVector(wtwire.AltruistSessionsRequired),
|
2019-02-09 07:01:59 +03:00
|
|
|
rHash: mainnetChainHash,
|
|
|
|
expErr: wtwire.NewErrUnknownChainHash(mainnetChainHash),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "different chain, local-required remote-optional",
|
2019-06-14 03:34:16 +03:00
|
|
|
lFeatures: lnwire.NewRawFeatureVector(wtwire.AltruistSessionsOptional),
|
2019-02-09 07:01:59 +03:00
|
|
|
lHash: testnetChainHash,
|
2019-06-14 03:34:16 +03:00
|
|
|
rFeatures: lnwire.NewRawFeatureVector(wtwire.AltruistSessionsRequired),
|
2019-02-09 07:01:59 +03:00
|
|
|
rHash: mainnetChainHash,
|
|
|
|
expErr: wtwire.NewErrUnknownChainHash(mainnetChainHash),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "same chain, remote-unknown-required",
|
2019-06-14 03:34:16 +03:00
|
|
|
lFeatures: lnwire.NewRawFeatureVector(wtwire.AltruistSessionsOptional),
|
2019-02-09 07:01:59 +03:00
|
|
|
lHash: testnetChainHash,
|
|
|
|
rFeatures: lnwire.NewRawFeatureVector(lnwire.GossipQueriesRequired),
|
|
|
|
rHash: testnetChainHash,
|
|
|
|
expErr: wtwire.NewErrUnknownRequiredFeatures(
|
|
|
|
lnwire.GossipQueriesRequired,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCheckRemoteInit asserts the behavior of CheckRemoteInit when called with
|
|
|
|
// the remote party's Init message and the default wtwire.Features. We assert
|
|
|
|
// the validity of advertised features from the perspective of both client and
|
|
|
|
// server, as well as failure cases such as differing chain hashes or unknown
|
|
|
|
// required features.
|
|
|
|
func TestCheckRemoteInit(t *testing.T) {
|
|
|
|
for _, test := range checkRemoteInitTests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
testCheckRemoteInit(t, test)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCheckRemoteInit(t *testing.T, test checkRemoteInitTest) {
|
|
|
|
localInit := wtwire.NewInitMessage(test.lFeatures, test.lHash)
|
|
|
|
remoteInit := wtwire.NewInitMessage(test.rFeatures, test.rHash)
|
|
|
|
|
|
|
|
err := localInit.CheckRemoteInit(remoteInit, wtwire.FeatureNames)
|
|
|
|
switch {
|
|
|
|
|
|
|
|
// Both non-nil, pass.
|
|
|
|
case err == nil && test.expErr == nil:
|
|
|
|
return
|
|
|
|
|
|
|
|
// One is nil and one is non-nil, fail.
|
|
|
|
default:
|
|
|
|
t.Fatalf("error mismatch, want: %v, got: %v", test.expErr, err)
|
|
|
|
|
|
|
|
// Both non-nil, assert same error type.
|
|
|
|
case err != nil && test.expErr != nil:
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare error strings to assert same type.
|
|
|
|
if err.Error() != test.expErr.Error() {
|
|
|
|
t.Fatalf("error mismatch, want: %v, got: %v",
|
|
|
|
test.expErr.Error(), err.Error())
|
|
|
|
}
|
|
|
|
}
|