rules: add test to demo failure for closed channels

This also introduces the use of testify mock to check which calls were
made.
This commit is contained in:
bitromortac 2026-02-02 10:17:34 +01:00
parent 79ae6e26ad
commit 7e0c04fea5
No known key found for this signature in database
GPG key ID: 1965063FC13BEBE2
2 changed files with 78 additions and 35 deletions

View file

@ -12,6 +12,7 @@ import (
"github.com/lightninglabs/lightning-terminal/session"
"github.com/lightninglabs/lndclient"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
@ -37,22 +38,25 @@ func TestChannelRestrictCheckRequest(t *testing.T) {
ctx := context.Background()
mgr := NewChannelRestrictMgr()
cfg := &mockLndClient{
channels: []lndclient.ChannelInfo{
{
ChannelID: chanID1,
ChannelPoint: chanPointStr1,
},
{
ChannelID: chanID2,
ChannelPoint: chanPointStr2,
},
{
ChannelID: chanID3,
ChannelPoint: chanPointStr3,
},
cfg := &mockLndClient{}
cfg.On(
"ListChannels", mock.Anything, mock.Anything, mock.Anything,
mock.Anything,
).Return([]lndclient.ChannelInfo{
{
ChannelID: chanID1,
ChannelPoint: chanPointStr1,
},
}
{
ChannelID: chanID2,
ChannelPoint: chanPointStr2,
},
{
ChannelID: chanID3,
ChannelPoint: chanPointStr3,
},
}, nil)
enf, err := mgr.NewEnforcer(ctx, cfg, &ChannelRestrict{
DenyList: []uint64{
chanID1, chanID2,
@ -149,18 +153,23 @@ func newTXID() ([]byte, uint32, error) {
type mockLndClient struct {
lndclient.LightningClient
Config
channels []lndclient.ChannelInfo
mock.Mock
}
func (m *mockLndClient) GetLndClient() lndclient.LightningClient {
return m
}
func (m *mockLndClient) ListChannels(_ context.Context, _, _ bool,
_ ...lndclient.ListChannelsOption) ([]lndclient.ChannelInfo, error) {
func (m *mockLndClient) ListChannels(ctx context.Context, public, active bool,
opts ...lndclient.ListChannelsOption) ([]lndclient.ChannelInfo, error) {
return m.channels, nil
args := m.Called(ctx, public, active, opts)
if args.Get(0) != nil {
return args.Get(0).([]lndclient.ChannelInfo), args.Error(1)
}
return nil, args.Error(1)
}
// TestChannelRestrictRealToPseudo tests that the ChannelRestrict's RealToPseudo
@ -292,3 +301,29 @@ func TestChannelRestrictRealToPseudo(t *testing.T) {
})
}
}
// TestChannelRestrictResilience ensures that the ChannelRestrictEnforcer is
// resilient to missing channels during initialization.
func TestChannelRestrictResilience(t *testing.T) {
var (
ctx = context.Background()
mgr = NewChannelRestrictMgr()
)
chanID1, _ := firewalldb.NewPseudoUint64()
// Initially, LND has no channels.
cfg := &mockLndClient{}
cfg.On(
"ListChannels", mock.Anything, mock.Anything, mock.Anything,
mock.Anything,
).Return([]lndclient.ChannelInfo{}, nil)
// We create an enforcer that denies chanID1 (maybe a closed channel or
// generally unknown). This will be fixed in a future commit.
_, err := mgr.NewEnforcer(ctx, cfg, &ChannelRestrict{
DenyList: []uint64{chanID1},
})
require.ErrorContains(t, err, "invalid channel ID")
cfg.AssertExpectations(t)
}

View file

@ -12,10 +12,12 @@ import (
"github.com/lightninglabs/lndclient"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/routing/route"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
// TestPeerRestrictCheckRequest ensures that the PeerRestrictEnforcer correctly
// accepts or denys a request.
func TestPeerRestrictCheckRequest(t *testing.T) {
txid1, index1, err := newTXID()
@ -51,28 +53,31 @@ func TestPeerRestrictCheckRequest(t *testing.T) {
ctx := context.Background()
mgr := NewPeerRestrictMgr()
cfg := &mockLndClient{
channels: []lndclient.ChannelInfo{
{
ChannelPoint: chanPointStr1,
PubKeyBytes: peerKey1,
},
{
ChannelPoint: chanPointStr2,
PubKeyBytes: peerKey2,
},
{
ChannelPoint: chanPointStr3,
PubKeyBytes: peerKey3,
},
cfg := &mockLndClient{}
cfg.On(
"ListChannels", mock.Anything, mock.Anything, mock.Anything,
mock.Anything,
).Return([]lndclient.ChannelInfo{
{
ChannelPoint: chanPointStr1,
PubKeyBytes: peerKey1,
},
}
{
ChannelPoint: chanPointStr2,
PubKeyBytes: peerKey2,
},
{
ChannelPoint: chanPointStr3,
PubKeyBytes: peerKey3,
},
}, nil)
enf, err := mgr.NewEnforcer(ctx, cfg, &PeerRestrict{
DenyList: []string{
peerID1, peerID2,
},
})
require.NoError(t, err)
// A request for an irrelevant URI should be allowed.
@ -198,6 +203,9 @@ func TestPeerRestrictCheckRequest(t *testing.T) {
ctx, "/lnrpc.Lightning/BatchOpenChannel", batchOpenReq,
)
require.NoError(t, err)
// Assert expected calls were made.
cfg.AssertExpectations(t)
}
// TestPeerRestrictionRealToPseudo tests that the PeerRestriction's RealToPseudo