|
8 | 8 | "testing" |
9 | 9 |
|
10 | 10 | "github.com/btcsuite/btcd/chaincfg/chainhash" |
| 11 | + "github.com/lightninglabs/lightning-terminal/firewall/mock" |
11 | 12 | "github.com/lightninglabs/lightning-terminal/firewalldb" |
12 | 13 | "github.com/lightninglabs/lndclient" |
13 | 14 | "github.com/lightningnetwork/lnd/lnrpc" |
@@ -161,3 +162,91 @@ func (m *mockLndClient) ListChannels(_ context.Context, _, _ bool) ( |
161 | 162 |
|
162 | 163 | return m.channels, nil |
163 | 164 | } |
| 165 | + |
| 166 | +// TestChannelRestrictRealToPseudo tests that the ChannelRestrict's RealToPseudo |
| 167 | +// method correctly determines which real strings to generate pseudo pairs for |
| 168 | +// based on the privacy map db passed to it. |
| 169 | +func TestChannelRestrictRealToPseudo(t *testing.T) { |
| 170 | + chanID1 := firewalldb.Uint64ToStr(1) |
| 171 | + chanID2 := firewalldb.Uint64ToStr(2) |
| 172 | + chanID3 := firewalldb.Uint64ToStr(3) |
| 173 | + chanID2Obfuscated := firewalldb.Uint64ToStr(200) |
| 174 | + |
| 175 | + tests := []struct { |
| 176 | + name string |
| 177 | + dbPreLoad map[string]string |
| 178 | + expectNewPairs map[string]bool |
| 179 | + }{ |
| 180 | + { |
| 181 | + // If there is no preloaded DB, then we expect all the |
| 182 | + // values in the deny list to be returned from the |
| 183 | + // RealToPseudo method. |
| 184 | + name: "no pre loaded db", |
| 185 | + expectNewPairs: map[string]bool{ |
| 186 | + chanID1: true, |
| 187 | + chanID2: true, |
| 188 | + chanID3: true, |
| 189 | + }, |
| 190 | + }, |
| 191 | + { |
| 192 | + // If the DB is preloaded with an entry for "channel 2" |
| 193 | + // then we don't expect that entry to be returned in the |
| 194 | + // set of new pairs. |
| 195 | + name: "partially pre-loaded DB", |
| 196 | + dbPreLoad: map[string]string{ |
| 197 | + chanID2: chanID2Obfuscated, |
| 198 | + }, |
| 199 | + expectNewPairs: map[string]bool{ |
| 200 | + chanID1: true, |
| 201 | + chanID3: true, |
| 202 | + }, |
| 203 | + }, |
| 204 | + } |
| 205 | + |
| 206 | + cr := &ChannelRestrict{ |
| 207 | + DenyList: []uint64{ |
| 208 | + 1, |
| 209 | + 2, |
| 210 | + 3, |
| 211 | + }, |
| 212 | + } |
| 213 | + |
| 214 | + for _, test := range tests { |
| 215 | + test := test |
| 216 | + t.Run(test.name, func(t *testing.T) { |
| 217 | + t.Parallel() |
| 218 | + |
| 219 | + var privDB firewalldb.PrivacyMapDB |
| 220 | + if len(test.dbPreLoad) != 0 { |
| 221 | + privDB = mock.NewPrivacyMapDB() |
| 222 | + } |
| 223 | + |
| 224 | + var expectedDenyList []string |
| 225 | + for r, p := range test.dbPreLoad { |
| 226 | + err := privDB.View( |
| 227 | + func(tx firewalldb.PrivacyMapTx) error { |
| 228 | + return tx.NewPair(r, p) |
| 229 | + }, |
| 230 | + ) |
| 231 | + require.NoError(t, err) |
| 232 | + |
| 233 | + expectedDenyList = append(expectedDenyList, p) |
| 234 | + } |
| 235 | + |
| 236 | + v, newPairs, err := cr.RealToPseudo(privDB) |
| 237 | + require.NoError(t, err) |
| 238 | + require.Len(t, newPairs, len(test.expectNewPairs)) |
| 239 | + |
| 240 | + for r, p := range newPairs { |
| 241 | + require.True(t, test.expectNewPairs[r]) |
| 242 | + |
| 243 | + expectedDenyList = append(expectedDenyList, p) |
| 244 | + } |
| 245 | + |
| 246 | + denyList, ok := v.(*ChannelRestrict) |
| 247 | + require.True(t, ok) |
| 248 | + |
| 249 | + require.EqualValues(t, v, denyList) |
| 250 | + }) |
| 251 | + } |
| 252 | +} |
0 commit comments