lnwire: add feature vector update validation helpers

This commit is contained in:
Carla Kirk-Cohen committed 2023-05-04 10:35:43 -04:00
1 parent 3f9f0ea5d1
commit 1a9d743e18
2 files changed
+157

No files matched your search

+100
View File
@@ -396,3 +396,103 @@ func TestIsEmptyFeatureVector(t *testing.T) {
fv.Unset(StaticRemoteKeyOptional)
require.True(t, fv.IsEmpty())
}
// TestValidatePairs tests that feature vectors can only set the required or
// optional feature bit in a pair, not both.
func TestValidatePairs(t *testing.T) {
t.Parallel()
rfv := NewRawFeatureVector(
StaticRemoteKeyOptional,
StaticRemoteKeyRequired,
)
require.Equal(t, ErrFeaturePairExists, rfv.ValidatePairs())
rfv = NewRawFeatureVector(
StaticRemoteKeyOptional,
PaymentAddrRequired,
)
require.Nil(t, rfv.ValidatePairs())
}
// TestValidateUpdate tests validation of an update to a feature vector.
func TestValidateUpdate(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
currentFeatures []FeatureBit
newFeatures []FeatureBit
err error
}{
{
name: "defined feature bit set, can include",
currentFeatures: []FeatureBit{
StaticRemoteKeyOptional,
},
newFeatures: []FeatureBit{
StaticRemoteKeyOptional,
},
err: nil,
},
{
name: "defined feature bit not already set",
currentFeatures: []FeatureBit{
StaticRemoteKeyOptional,
},
newFeatures: []FeatureBit{
StaticRemoteKeyOptional,
PaymentAddrRequired,
},
err: ErrFeatureStandard,
},
{
name: "known feature missing",
currentFeatures: []FeatureBit{
StaticRemoteKeyOptional,
PaymentAddrRequired,
},
newFeatures: []FeatureBit{
StaticRemoteKeyOptional,
},
err: ErrFeatureStandard,
},
{
name: "can set unknown feature",
currentFeatures: []FeatureBit{
StaticRemoteKeyOptional,
},
newFeatures: []FeatureBit{
StaticRemoteKeyOptional,
FeatureBit(1001),
},
err: nil,
},
{
name: "can unset unknown feature",
currentFeatures: []FeatureBit{
StaticRemoteKeyOptional,
FeatureBit(1001),
},
newFeatures: []FeatureBit{
StaticRemoteKeyOptional,
},
err: nil,
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
currentFV := NewRawFeatureVector(
testCase.currentFeatures...,
)
newFV := NewRawFeatureVector(testCase.newFeatures...)
err := currentFV.ValidateUpdate(newFV)
require.ErrorIs(t, err, testCase.err)
})
}
}