@@ -161,3 +161,111 @@ func (m *mockLndClient) ListChannels(_ context.Context, _, _ bool) (
161161
162162 return m .channels , nil
163163}
164+
165+ // TestChannelRestrictRealToPseudo tests that the ChannelRestrict's RealToPseudo
166+ // method correctly determines which real strings to generate pseudo pairs for
167+ // based on the privacy map db passed to it.
168+ func TestChannelRestrictRealToPseudo (t * testing.T ) {
169+ chanID1 := firewalldb .Uint64ToStr (1 )
170+ chanID2 := firewalldb .Uint64ToStr (2 )
171+ chanID3 := firewalldb .Uint64ToStr (3 )
172+ chanID2Obfuscated := firewalldb .Uint64ToStr (200 )
173+
174+ tests := []struct {
175+ name string
176+ dbPreLoad map [string ]string
177+ expectNewPairs map [string ]bool
178+ }{
179+ {
180+ // If there is no preloaded DB, then we expect all the
181+ // values in the deny list to be returned from the
182+ // RealToPseudo method.
183+ name : "no pre loaded db" ,
184+ expectNewPairs : map [string ]bool {
185+ chanID1 : true ,
186+ chanID2 : true ,
187+ chanID3 : true ,
188+ },
189+ },
190+ {
191+ // If the DB is preloaded with an entry for "channel 2"
192+ // then we don't expect that entry to be returned in the
193+ // set of new pairs.
194+ name : "partially pre-loaded DB" ,
195+ dbPreLoad : map [string ]string {
196+ chanID2 : chanID2Obfuscated ,
197+ },
198+ expectNewPairs : map [string ]bool {
199+ chanID1 : true ,
200+ chanID3 : true ,
201+ },
202+ },
203+ }
204+
205+ // Construct the ChannelRestrict deny list. Note that we repeat one of
206+ // the entries here in order to ensure that the RealToPseudo method is
207+ // forced to look up any real-to-pseudo pairs that it already
208+ // generated.
209+ cr := & ChannelRestrict {
210+ DenyList : []uint64 {
211+ 1 ,
212+ 2 ,
213+ 3 ,
214+ 3 ,
215+ },
216+ }
217+
218+ for _ , test := range tests {
219+ test := test
220+ t .Run (test .name , func (t * testing.T ) {
221+ t .Parallel ()
222+
223+ privMapPairDB := firewalldb .NewPrivacyMapPairs (
224+ test .dbPreLoad ,
225+ )
226+
227+ // Iterate over the preload key value pairs and load
228+ // them into the DB.
229+ expectedDenyList := make (map [uint64 ]bool )
230+ for _ , p := range test .dbPreLoad {
231+ // Add the pseudo value to the expected deny
232+ // list.
233+ pInt , err := firewalldb .StrToUint64 (p )
234+ require .NoError (t , err )
235+
236+ expectedDenyList [pInt ] = true
237+ }
238+
239+ // Call the RealToPseudo method on the ChannelRestrict
240+ // rule. This will return the rule value in its pseudo
241+ // form along with any new privacy map pairs that should
242+ // be added to the DB.
243+ v , newPairs , err := cr .RealToPseudo (privMapPairDB )
244+ require .NoError (t , err )
245+ require .Len (t , newPairs , len (test .expectNewPairs ))
246+
247+ // We add each new pair to the expected deny list too.
248+ for r , p := range newPairs {
249+ require .True (t , test .expectNewPairs [r ])
250+
251+ pInt , err := firewalldb .StrToUint64 (p )
252+ require .NoError (t , err )
253+
254+ expectedDenyList [pInt ] = true
255+ }
256+
257+ denyList , ok := v .(* ChannelRestrict )
258+ require .True (t , ok )
259+
260+ // Assert that the resulting deny list is the same
261+ // length as the un-obfuscated one.
262+ require .Len (t , denyList .DenyList , len (cr .DenyList ))
263+
264+ // Now iterate over the deny list and assert that each
265+ // value appears in our expected deny list.
266+ for _ , channel := range denyList .DenyList {
267+ require .True (t , expectedDenyList [channel ])
268+ }
269+ })
270+ }
271+ }
0 commit comments