Skip to content

Commit 203d0b7

Browse files
committed
firewall: refactor privacy mapper tests
Pull out transaction related constants to the top of the test. Adds a debug comment that is useful for this code. It is often needed to check the human readable representation of a message.
1 parent a36d6c0 commit 203d0b7

File tree

1 file changed

+74
-32
lines changed

1 file changed

+74
-32
lines changed

firewall/privacy_mapper_test.go

Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package firewall
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"testing"
78
"time"
89

10+
"github.com/btcsuite/btcd/chaincfg/chainhash"
911
"github.com/lightninglabs/lightning-terminal/firewalldb"
1012
"github.com/lightninglabs/lightning-terminal/session"
1113
"github.com/lightningnetwork/lnd/lnrpc"
@@ -19,6 +21,33 @@ import (
1921
// TestPrivacyMapper tests that the PrivacyMapper correctly intercepts specific
2022
// RPC calls.
2123
func TestPrivacyMapper(t *testing.T) {
24+
outPoint := func(txid string, index uint32) string {
25+
return fmt.Sprintf("%s:%d", txid, index)
26+
}
27+
28+
// Define some transaction outpoints used for mapping.
29+
clearTxID := "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcd"
30+
31+
obfusTxID0 := "097ef666a61919ff3413b3b701eae3a5cbac08f70c0ca567806e1fa6acbfe384"
32+
obfusOut0 := uint32(2161781494)
33+
obfusTxID0Reversed, err := chainhash.NewHashFromStr(obfusTxID0)
34+
require.NoError(t, err)
35+
36+
obfusTxID1 := "45ec471bfccb0b7b9a8bc4008248931c59ad994903e07b54f54821ea3ef5cc5c"
37+
obfusOut1 := uint32(1642614131)
38+
39+
// Define some preexisting mappings for the privacy mapper.
40+
mapPreloadRealToPseudo := map[string]string{
41+
"Tinker Bell's pub key": "a44ef01c3bff970ef495c",
42+
"000000000000007b": "47deb774fc605c56",
43+
"0000000000000141": "2fd42e84b9ffaaeb",
44+
"00000000000002a6": "7859bf41241787c2",
45+
"000000000000036c": "1320e5d25b7b5973",
46+
outPoint(clearTxID, 0): outPoint(obfusTxID0, obfusOut0),
47+
outPoint(clearTxID, 1): outPoint(obfusTxID1, obfusOut1),
48+
"01020304": "c8134495",
49+
}
50+
2251
var (
2352
clearForwarding = &lnrpc.ForwardingHistoryResponse{
2453
ForwardingEvents: []*lnrpc.ForwardingEvent{
@@ -61,8 +90,13 @@ func TestPrivacyMapper(t *testing.T) {
6190
RemotePubkey: "01020304",
6291
Initiator: false,
6392
ChanId: 123,
64-
ChannelPoint: "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcd:0",
65-
PendingHtlcs: []*lnrpc.HTLC{{HashLock: []byte("aaaa")}, {HashLock: []byte("bbbb")}},
93+
ChannelPoint: outPoint(
94+
clearTxID, 0,
95+
),
96+
PendingHtlcs: []*lnrpc.HTLC{
97+
{HashLock: []byte("aaaa")},
98+
{HashLock: []byte("bbbb")},
99+
},
66100
},
67101
},
68102
}
@@ -164,24 +198,32 @@ func TestPrivacyMapper(t *testing.T) {
164198
msg: &lnrpc.FeeReportResponse{
165199
ChannelFees: []*lnrpc.ChannelFeeReport{
166200
{
167-
ChanId: 123,
168-
ChannelPoint: "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcd:0",
201+
ChanId: 123,
202+
ChannelPoint: outPoint(
203+
clearTxID, 0,
204+
),
169205
},
170206
{
171-
ChanId: 321,
172-
ChannelPoint: "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcd:1",
207+
ChanId: 321,
208+
ChannelPoint: outPoint(
209+
clearTxID, 1,
210+
),
173211
},
174212
},
175213
},
176214
expectedReplacement: &lnrpc.FeeReportResponse{
177215
ChannelFees: []*lnrpc.ChannelFeeReport{
178216
{
179-
ChanId: 5178778334600911958,
180-
ChannelPoint: "097ef666a61919ff3413b3b701eae3a5cbac08f70c0ca567806e1fa6acbfe384:2161781494",
217+
ChanId: 5178778334600911958,
218+
ChannelPoint: outPoint(
219+
obfusTxID0, obfusOut0,
220+
),
181221
},
182222
{
183-
ChanId: 3446430762436373227,
184-
ChannelPoint: "45ec471bfccb0b7b9a8bc4008248931c59ad994903e07b54f54821ea3ef5cc5c:1642614131",
223+
ChanId: 3446430762436373227,
224+
ChannelPoint: outPoint(
225+
obfusTxID1, obfusOut1,
226+
),
185227
},
186228
},
187229
},
@@ -214,8 +256,10 @@ func TestPrivacyMapper(t *testing.T) {
214256
RemotePubkey: "c8134495",
215257
Initiator: true,
216258
ChanId: 5178778334600911958,
217-
ChannelPoint: "097ef666a61919ff3413b3b701eae3a5cbac08f70c0ca567806e1fa6acbfe384:2161781494",
218-
PendingHtlcs: []*lnrpc.HTLC{{}, {}},
259+
ChannelPoint: outPoint(
260+
obfusTxID0, obfusOut0,
261+
),
262+
PendingHtlcs: []*lnrpc.HTLC{{}, {}},
219263
},
220264
},
221265
},
@@ -242,17 +286,17 @@ func TestPrivacyMapper(t *testing.T) {
242286
Scope: &lnrpc.PolicyUpdateRequest_ChanPoint{
243287
ChanPoint: &lnrpc.ChannelPoint{
244288
FundingTxid: &lnrpc.ChannelPoint_FundingTxidStr{
245-
FundingTxidStr: "097ef666a61919ff3413b3b701eae3a5cbac08f70c0ca567806e1fa6acbfe384",
289+
FundingTxidStr: obfusTxID0,
246290
},
247-
OutputIndex: 2161781494,
291+
OutputIndex: obfusOut0,
248292
},
249293
},
250294
},
251295
expectedReplacement: &lnrpc.PolicyUpdateRequest{
252296
Scope: &lnrpc.PolicyUpdateRequest_ChanPoint{
253297
ChanPoint: &lnrpc.ChannelPoint{
254298
FundingTxid: &lnrpc.ChannelPoint_FundingTxidStr{
255-
FundingTxidStr: "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcd",
299+
FundingTxidStr: clearTxID,
256300
},
257301
OutputIndex: 0,
258302
},
@@ -267,17 +311,17 @@ func TestPrivacyMapper(t *testing.T) {
267311
Scope: &lnrpc.PolicyUpdateRequest_ChanPoint{
268312
ChanPoint: &lnrpc.ChannelPoint{
269313
FundingTxid: &lnrpc.ChannelPoint_FundingTxidBytes{
270-
FundingTxidBytes: []byte{132, 227, 191, 172, 166, 31, 110, 128, 103, 165, 12, 12, 247, 8, 172, 203, 165, 227, 234, 1, 183, 179, 19, 52, 255, 25, 25, 166, 102, 246, 126, 9},
314+
FundingTxidBytes: obfusTxID0Reversed[:],
271315
},
272-
OutputIndex: 2161781494,
316+
OutputIndex: obfusOut0,
273317
},
274318
},
275319
},
276320
expectedReplacement: &lnrpc.PolicyUpdateRequest{
277321
Scope: &lnrpc.PolicyUpdateRequest_ChanPoint{
278322
ChanPoint: &lnrpc.ChannelPoint{
279323
FundingTxid: &lnrpc.ChannelPoint_FundingTxidStr{
280-
FundingTxidStr: "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcd",
324+
FundingTxidStr: clearTxID,
281325
},
282326
OutputIndex: 0,
283327
},
@@ -292,7 +336,7 @@ func TestPrivacyMapper(t *testing.T) {
292336
FailedUpdates: []*lnrpc.FailedUpdate{
293337
{
294338
Outpoint: &lnrpc.OutPoint{
295-
TxidStr: "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcd",
339+
TxidStr: clearTxID,
296340
OutputIndex: 0,
297341
},
298342
},
@@ -302,8 +346,8 @@ func TestPrivacyMapper(t *testing.T) {
302346
FailedUpdates: []*lnrpc.FailedUpdate{
303347
{
304348
Outpoint: &lnrpc.OutPoint{
305-
TxidStr: "097ef666a61919ff3413b3b701eae3a5cbac08f70c0ca567806e1fa6acbfe384",
306-
OutputIndex: 2161781494,
349+
TxidStr: obfusTxID0,
350+
OutputIndex: uint32(obfusOut0),
307351
},
308352
},
309353
},
@@ -332,17 +376,6 @@ func TestPrivacyMapper(t *testing.T) {
332376
sessionID, err := session.IDFromMacaroon(mac)
333377
require.NoError(t, err)
334378

335-
mapPreloadRealToPseudo := map[string]string{
336-
"Tinker Bell's pub key": "a44ef01c3bff970ef495c",
337-
"000000000000007b": "47deb774fc605c56",
338-
"0000000000000141": "2fd42e84b9ffaaeb",
339-
"00000000000002a6": "7859bf41241787c2",
340-
"000000000000036c": "1320e5d25b7b5973",
341-
"abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcd:0": "097ef666a61919ff3413b3b701eae3a5cbac08f70c0ca567806e1fa6acbfe384:2161781494",
342-
"abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcd:1": "45ec471bfccb0b7b9a8bc4008248931c59ad994903e07b54f54821ea3ef5cc5c:1642614131",
343-
"01020304": "c8134495",
344-
}
345-
346379
for _, test := range tests {
347380
t.Run(test.name, func(t *testing.T) {
348381
// Initialize privacy mapping.
@@ -383,6 +416,15 @@ func TestPrivacyMapper(t *testing.T) {
383416
return
384417
}
385418

419+
// Snippet to print the replacement for debugging, works
420+
// only for specific test.
421+
// mes := &lnrpc.PendingChannelsResponse{}
422+
// err = proto.Unmarshal(
423+
// feedback.ReplacementSerialized, mes,
424+
// )
425+
// require.NoError(t, err)
426+
// t.Log(mes)
427+
386428
expectedRaw, err := proto.Marshal(
387429
test.expectedReplacement,
388430
)

0 commit comments

Comments
 (0)