Skip to main content

tor_linkspec/ids/
by_id.rs

1//! Define a type for a set of HasRelayIds objects that can be looked up by any
2//! of their keys.
3
4use tor_basic_utils::{n_key_list, n_key_set};
5use tor_llcrypto::pk::ed25519::Ed25519Identity;
6use tor_llcrypto::pk::rsa::RsaIdentity;
7
8use crate::{HasRelayIds, RelayIdRef};
9
10n_key_list! {
11    /// A list of objects that can be accessed by relay identity.
12    ///
13    /// Multiple objects in the list can have a given relay identity.
14    ///
15    /// # Invariants
16    ///
17    /// Every object in the list **must** have at least one recognized relay identity; if it does
18    /// not, it cannot be inserted.
19    ///
20    /// This list may panic or give incorrect results if the values can change their keys through
21    /// interior mutability.
22    #[derive(Clone, Debug)]
23    pub struct[H:HasRelayIds] ListByRelayIds[H] for H
24    {
25        (Option) rsa: RsaIdentity { rsa_identity() },
26        (Option) ed25519: Ed25519Identity { ed_identity() },
27    }
28}
29
30n_key_set! {
31    /// A set of objects that can be accessed by relay identity.
32    ///
33    /// No more than one object in the set can have any given relay identity.
34    ///
35    /// # Invariants
36    ///
37    /// Every object in the set MUST have at least one recognized relay
38    /// identity; if it does not, it cannot be inserted.
39    ///
40    /// This set may panic or give incorrect results if the values can change their
41    /// keys through interior mutability.
42    ///
43    #[derive(Clone, Debug)]
44    pub struct[H:HasRelayIds] ByRelayIds[H] for H
45    {
46        (Option) rsa: RsaIdentity { rsa_identity() },
47        (Option) ed25519: Ed25519Identity { ed_identity() },
48    }
49}
50
51impl<H: HasRelayIds> ByRelayIds<H> {
52    /// Return the value in this set (if any) that has the key `key`.
53    pub fn by_id<'a, T>(&self, key: T) -> Option<&H>
54    where
55        T: Into<RelayIdRef<'a>>,
56    {
57        match key.into() {
58            RelayIdRef::Ed25519(ed) => self.by_ed25519(ed),
59            RelayIdRef::Rsa(rsa) => self.by_rsa(rsa),
60        }
61    }
62
63    /// Return the value in this set (if any) that has the key `key`.
64    pub fn remove_by_id<'a, T>(&mut self, key: T) -> Option<H>
65    where
66        T: Into<RelayIdRef<'a>>,
67    {
68        match key.into() {
69            RelayIdRef::Ed25519(ed) => self.remove_by_ed25519(ed),
70            RelayIdRef::Rsa(rsa) => self.remove_by_rsa(rsa),
71        }
72    }
73
74    /// Modify the value in this set (if any) that has the key `key`.
75    ///
76    /// Return values are as for [`modify_by_ed25519`](Self::modify_by_ed25519)
77    pub fn modify_by_id<'a, T, F>(&mut self, key: T, func: F) -> Vec<H>
78    where
79        T: Into<RelayIdRef<'a>>,
80        F: FnOnce(&mut H),
81    {
82        match key.into() {
83            RelayIdRef::Ed25519(ed) => self.modify_by_ed25519(ed, func),
84            RelayIdRef::Rsa(rsa) => self.modify_by_rsa(rsa, func),
85        }
86    }
87
88    /// Return the value in this set (if any) that has _all_ the relay IDs
89    /// that `key` does.
90    ///
91    /// Return `None` if `key` has no relay IDs.
92    pub fn by_all_ids<T>(&self, key: &T) -> Option<&H>
93    where
94        T: HasRelayIds,
95    {
96        let any_id = key.identities().next()?;
97        self.by_id(any_id)
98            .filter(|val| val.has_all_relay_ids_from(key))
99    }
100
101    /// Modify the value in this set (if any) that has _all_ the relay IDs
102    /// that `key` does.
103    ///
104    /// Return values are as for [`modify_by_ed25519`](Self::modify_by_ed25519)
105    pub fn modify_by_all_ids<T, F>(&mut self, key: &T, func: F) -> Vec<H>
106    where
107        T: HasRelayIds,
108        F: FnOnce(&mut H),
109    {
110        let any_id = match key.identities().next() {
111            Some(id) => id,
112            None => return Vec::new(),
113        };
114        self.modify_by_id(any_id, |val| {
115            if val.has_all_relay_ids_from(key) {
116                func(val);
117            }
118        })
119    }
120
121    /// Remove the single value in this set (if any) that has _exactly the same_
122    /// relay IDs that `key` does
123    pub fn remove_exact<T>(&mut self, key: &T) -> Option<H>
124    where
125        T: HasRelayIds,
126    {
127        let any_id = key.identities().next()?;
128        if self
129            .by_id(any_id)
130            .filter(|ent| ent.same_relay_ids(key))
131            .is_some()
132        {
133            self.remove_by_id(any_id)
134        } else {
135            None
136        }
137    }
138
139    /// Remove the single value in this set (if any) that has all the same
140    /// relay IDs that `key` does. If `key` does not have any relay IDs, no
141    /// value is returned.
142    pub fn remove_by_all_ids<T>(&mut self, key: &T) -> Option<H>
143    where
144        T: HasRelayIds,
145    {
146        let any_id = key.identities().next()?;
147        if self
148            .by_id(any_id)
149            .filter(|ent| ent.has_all_relay_ids_from(key))
150            .is_some()
151        {
152            self.remove_by_id(any_id)
153        } else {
154            None
155        }
156    }
157
158    /// Return a reference to every element in this set that shares _any_ ID
159    /// with `key`.
160    ///
161    /// No element is returned more than once.
162    pub fn all_overlapping<T>(&self, key: &T) -> Vec<&H>
163    where
164        T: HasRelayIds,
165    {
166        use by_address::ByAddress;
167        use std::collections::HashSet;
168
169        let mut items: HashSet<ByAddress<&H>> = HashSet::new();
170
171        for ident in key.identities() {
172            if let Some(found) = self.by_id(ident) {
173                items.insert(ByAddress(found));
174            }
175        }
176
177        items.into_iter().map(|by_addr| by_addr.0).collect()
178    }
179
180    /// Return true if any element in this set shares _any_ ID with `key`.
181    pub fn contains_any_overlapping<T>(&self, key: &T) -> bool
182    where
183        T: HasRelayIds,
184    {
185        // NOTE: If this ever shows up in profiles, we can add a more efficient
186        // implementation, but for now this is fine.
187        !self.all_overlapping(key).is_empty()
188    }
189}
190
191impl<H: HasRelayIds> ListByRelayIds<H> {
192    /// Return an iterator of the values in this list that have the key `key`.
193    pub fn by_id<'a, T>(&self, key: T) -> ListByRelayIdsIter<H>
194    where
195        T: Into<RelayIdRef<'a>>,
196    {
197        match key.into() {
198            RelayIdRef::Ed25519(ed) => self.by_ed25519(ed),
199            RelayIdRef::Rsa(rsa) => self.by_rsa(rsa),
200        }
201    }
202
203    /// Return the values in this list that have *all* the relay IDs that `key` does.
204    ///
205    /// Returns an empty iterator if `key` has no relay IDs.
206    pub fn by_all_ids<'a>(&'a self, key: &'a impl HasRelayIds) -> impl Iterator<Item = &'a H> + 'a {
207        key.identities()
208            .next()
209            .map_or_else(Default::default, |id| self.by_id(id))
210            .filter(|val| val.has_all_relay_ids_from(key))
211    }
212
213    /// Return a reference to every element in this set that shares *any* ID with `key`.
214    ///
215    /// No element is returned more than once. Equality is compared using
216    /// [`ByAddress`](by_address::ByAddress).
217    pub fn all_overlapping<T>(&self, key: &T) -> Vec<&H>
218    where
219        T: HasRelayIds,
220    {
221        use by_address::ByAddress;
222        use std::collections::HashSet;
223
224        let mut items: HashSet<ByAddress<&H>> = HashSet::new();
225
226        for ident in key.identities() {
227            for found in self.by_id(ident) {
228                items.insert(ByAddress(found));
229            }
230        }
231
232        items.into_iter().map(|by_addr| by_addr.0).collect()
233    }
234
235    /// Return a reference to every element in this list whose relay IDs are a subset of the relay
236    /// IDs that `key` has.
237    ///
238    /// No element is returned more than once. Equality is compared using
239    /// [`ByAddress`](by_address::ByAddress).
240    pub fn all_subset<T>(&self, key: &T) -> Vec<&H>
241    where
242        T: HasRelayIds,
243    {
244        use by_address::ByAddress;
245        use std::collections::HashSet;
246
247        let mut items: HashSet<ByAddress<&H>> = HashSet::new();
248
249        for ident in key.identities() {
250            for found in self.by_id(ident) {
251                // if 'key's relay ids are a superset of 'found's relay ids
252                if key.has_all_relay_ids_from(found) {
253                    items.insert(ByAddress(found));
254                }
255            }
256        }
257
258        items.into_iter().map(|by_addr| by_addr.0).collect()
259    }
260
261    /// Return the values in this list that have the key `key` and where `filter` returns `true`.
262    pub fn remove_by_id<'a, T>(&mut self, key: T, filter: impl FnMut(&H) -> bool) -> Vec<H>
263    where
264        T: Into<RelayIdRef<'a>>,
265    {
266        match key.into() {
267            RelayIdRef::Ed25519(ed) => self.remove_by_ed25519(ed, filter),
268            RelayIdRef::Rsa(rsa) => self.remove_by_rsa(rsa, filter),
269        }
270    }
271
272    /// Remove and return the values in this list that have *exactly the same* relay IDs that `key`
273    /// does.
274    pub fn remove_exact<T>(&mut self, key: &T) -> Vec<H>
275    where
276        T: HasRelayIds,
277    {
278        let Some(id) = key.identities().next() else {
279            return Vec::new();
280        };
281
282        self.remove_by_id(id, |val| val.same_relay_ids(key))
283    }
284
285    /// Remove and return the values in this list that have all the same relay IDs that `key` does.
286    ///
287    /// If `key` has no relay IDs, then no values are removed.
288    pub fn remove_by_all_ids<T>(&mut self, key: &T) -> Vec<H>
289    where
290        T: HasRelayIds,
291    {
292        let Some(id) = key.identities().next() else {
293            return Vec::new();
294        };
295
296        self.remove_by_id(id, |val| val.has_all_relay_ids_from(key))
297    }
298}
299
300pub use tor_basic_utils::n_key_list::Error as ListByRelayIdsError;
301pub use tor_basic_utils::n_key_set::Error as ByRelayIdsError;
302
303#[cfg(test)]
304mod test {
305    // @@ begin test lint list maintained by maint/add_warning @@
306    #![allow(clippy::bool_assert_comparison)]
307    #![allow(clippy::clone_on_copy)]
308    #![allow(clippy::dbg_macro)]
309    #![allow(clippy::mixed_attributes_style)]
310    #![allow(clippy::print_stderr)]
311    #![allow(clippy::print_stdout)]
312    #![allow(clippy::single_char_pattern)]
313    #![allow(clippy::unwrap_used)]
314    #![allow(clippy::unchecked_time_subtraction)]
315    #![allow(clippy::useless_vec)]
316    #![allow(clippy::needless_pass_by_value)]
317    #![allow(clippy::string_slice)] // See arti#2571
318    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
319
320    use super::*;
321    use crate::{RelayIds, RelayIdsBuilder};
322
323    fn sort<T: std::cmp::Ord>(i: impl Iterator<Item = T>) -> Vec<T> {
324        let mut v: Vec<_> = i.collect();
325        v.sort();
326        v
327    }
328
329    #[test]
330    fn lookup() {
331        let rsa1: RsaIdentity = (*b"12345678901234567890").into();
332        let rsa2: RsaIdentity = (*b"abcefghijklmnopqrstu").into();
333        let rsa3: RsaIdentity = (*b"abcefghijklmnopQRSTU").into();
334        let ed1: Ed25519Identity = (*b"12345678901234567890123456789012").into();
335        let ed2: Ed25519Identity = (*b"abcefghijklmnopqrstuvwxyzABCDEFG").into();
336        let ed3: Ed25519Identity = (*b"abcefghijklmnopqrstuvwxyz1234567").into();
337
338        let keys1 = RelayIdsBuilder::default()
339            .rsa_identity(rsa1)
340            .ed_identity(ed1)
341            .build()
342            .unwrap();
343
344        let keys2 = RelayIdsBuilder::default()
345            .rsa_identity(rsa2)
346            .ed_identity(ed2)
347            .build()
348            .unwrap();
349
350        // `ByRelayIds` and `ListByRelayIds` work similarly in the case where we only add a single
351        // value per key, so we can test them both here with the same test cases.
352
353        let mut set = ByRelayIds::new();
354        set.insert(keys1.clone());
355        set.insert(keys2.clone());
356
357        let mut list = ListByRelayIds::new();
358        list.insert(keys1.clone());
359        list.insert(keys2.clone());
360
361        // Try by_id
362        assert_eq!(set.by_id(&rsa1), Some(&keys1));
363        assert_eq!(set.by_id(&ed1), Some(&keys1));
364        assert_eq!(set.by_id(&rsa2), Some(&keys2));
365        assert_eq!(set.by_id(&ed2), Some(&keys2));
366        assert_eq!(set.by_id(&rsa3), None);
367        assert_eq!(set.by_id(&ed3), None);
368        assert_eq!(sort(list.by_id(&rsa1)), [&keys1]);
369        assert_eq!(sort(list.by_id(&ed1)), [&keys1]);
370        assert_eq!(sort(list.by_id(&rsa2)), [&keys2]);
371        assert_eq!(sort(list.by_id(&ed2)), [&keys2]);
372        assert_eq!(list.by_id(&rsa3).len(), 0);
373        assert_eq!(list.by_id(&ed3).len(), 0);
374
375        // Try exact lookup
376        assert_eq!(set.by_all_ids(&keys1), Some(&keys1));
377        assert_eq!(set.by_all_ids(&keys2), Some(&keys2));
378        assert_eq!(set.by_all_ids(&RelayIds::empty()), None);
379        assert_eq!(sort(list.by_all_ids(&keys1)), [&keys1]);
380        assert_eq!(sort(list.by_all_ids(&keys2)), [&keys2]);
381        assert!(sort(list.by_all_ids(&RelayIds::empty())).is_empty());
382        {
383            let search = RelayIdsBuilder::default()
384                .rsa_identity(rsa1)
385                .build()
386                .unwrap();
387            assert_eq!(set.by_all_ids(&search), Some(&keys1));
388            assert_eq!(sort(list.by_all_ids(&search)), [&keys1]);
389        }
390        {
391            let search = RelayIdsBuilder::default()
392                .rsa_identity(rsa1)
393                .ed_identity(ed2)
394                .build()
395                .unwrap();
396            assert_eq!(set.by_all_ids(&search), None);
397            assert!(sort(list.by_all_ids(&search)).is_empty());
398        }
399
400        // Try looking for overlap
401        assert_eq!(set.all_overlapping(&keys1), vec![&keys1]);
402        assert_eq!(set.all_overlapping(&keys2), vec![&keys2]);
403        assert_eq!(list.all_overlapping(&keys1), vec![&keys1]);
404        assert_eq!(list.all_overlapping(&keys2), vec![&keys2]);
405        {
406            let search = RelayIdsBuilder::default()
407                .rsa_identity(rsa1)
408                .ed_identity(ed2)
409                .build()
410                .unwrap();
411            let answer = set.all_overlapping(&search);
412            assert_eq!(answer.len(), 2);
413            assert!(answer.contains(&&keys1));
414            assert!(answer.contains(&&keys2));
415            let answer = list.all_overlapping(&search);
416            assert_eq!(answer.len(), 2);
417            assert!(answer.contains(&&keys1));
418            assert!(answer.contains(&&keys2));
419        }
420        {
421            let search = RelayIdsBuilder::default()
422                .rsa_identity(rsa2)
423                .build()
424                .unwrap();
425            assert_eq!(set.all_overlapping(&search), vec![&keys2]);
426            assert_eq!(list.all_overlapping(&search), vec![&keys2]);
427        }
428        {
429            let search = RelayIdsBuilder::default()
430                .rsa_identity(rsa3)
431                .build()
432                .unwrap();
433            assert!(set.all_overlapping(&search).is_empty());
434            assert!(list.all_overlapping(&search).is_empty());
435        }
436    }
437
438    #[test]
439    fn remove_exact() {
440        let rsa1: RsaIdentity = (*b"12345678901234567890").into();
441        let rsa2: RsaIdentity = (*b"abcefghijklmnopqrstu").into();
442        let ed1: Ed25519Identity = (*b"12345678901234567890123456789012").into();
443        let ed2: Ed25519Identity = (*b"abcefghijklmnopqrstuvwxyzABCDEFG").into();
444
445        let keys1 = RelayIdsBuilder::default()
446            .rsa_identity(rsa1)
447            .ed_identity(ed1)
448            .build()
449            .unwrap();
450
451        let keys2 = RelayIdsBuilder::default()
452            .rsa_identity(rsa2)
453            .ed_identity(ed2)
454            .build()
455            .unwrap();
456
457        // `ByRelayIds` and `ListByRelayIds` work similarly in the case where we only add a single
458        // value per key, so we can test them both here with the same test cases.
459
460        let mut set = ByRelayIds::new();
461        set.insert(keys1.clone());
462        set.insert(keys2.clone());
463        assert_eq!(set.len(), 2);
464
465        let mut list = ListByRelayIds::new();
466        list.insert(keys1.clone());
467        list.insert(keys2.clone());
468        assert_eq!(list.len(), 2);
469
470        assert_eq!(set.remove_exact(&keys1), Some(keys1.clone()));
471        assert_eq!(set.len(), 1);
472        assert_eq!(list.remove_exact(&keys1), vec![keys1.clone()]);
473        assert_eq!(list.len(), 1);
474
475        {
476            let search = RelayIdsBuilder::default().ed_identity(ed2).build().unwrap();
477
478            // We're calling remove_exact, but we did not list _all_ the keys in keys2.
479            assert_eq!(set.remove_exact(&search), None);
480            assert_eq!(set.len(), 1);
481            assert_eq!(list.remove_exact(&search), vec![]);
482            assert_eq!(list.len(), 1);
483
484            // If we were to use `remove_by_all_ids` with a search that didn't
485            // match, it wouldn't work.
486            let no_match = RelayIdsBuilder::default()
487                .ed_identity(ed2)
488                .rsa_identity(rsa1)
489                .build()
490                .unwrap();
491            assert_eq!(set.remove_by_all_ids(&no_match), None);
492            assert_eq!(set.len(), 1);
493            assert_eq!(list.remove_by_all_ids(&no_match), vec![]);
494            assert_eq!(list.len(), 1);
495
496            // If we use `remove_by_all_ids` with the original search, though,
497            // it will remove the element.
498            assert_eq!(set.remove_by_all_ids(&search), Some(keys2.clone()));
499            assert!(set.is_empty());
500            assert_eq!(list.remove_by_all_ids(&search), vec![keys2.clone()]);
501            assert!(list.is_empty());
502        }
503    }
504
505    #[test]
506    fn all_subset() {
507        let rsa1: RsaIdentity = (*b"12345678901234567890").into();
508        let rsa2: RsaIdentity = (*b"abcefghijklmnopqrstu").into();
509        let ed1: Ed25519Identity = (*b"12345678901234567890123456789012").into();
510
511        // one rsa id and one ed id
512        let keys1 = RelayIdsBuilder::default()
513            .rsa_identity(rsa1)
514            .ed_identity(ed1)
515            .build()
516            .unwrap();
517
518        // one rsa id
519        let keys2 = RelayIdsBuilder::default()
520            .rsa_identity(rsa2)
521            .build()
522            .unwrap();
523
524        let mut list = ListByRelayIds::new();
525        list.insert(keys1.clone());
526        list.insert(keys2.clone());
527
528        assert_eq!(list.all_subset(&keys1), vec![&keys1]);
529        assert_eq!(list.all_subset(&keys2), vec![&keys2]);
530
531        {
532            let search = RelayIdsBuilder::default()
533                .rsa_identity(rsa1)
534                .build()
535                .unwrap();
536            assert!(list.all_subset(&search).is_empty());
537        }
538
539        {
540            let search = RelayIdsBuilder::default().ed_identity(ed1).build().unwrap();
541            assert!(list.all_subset(&search).is_empty());
542        }
543
544        {
545            let search = RelayIdsBuilder::default()
546                .rsa_identity(rsa2)
547                .build()
548                .unwrap();
549            assert_eq!(list.all_subset(&search), vec![&keys2]);
550        }
551
552        {
553            let search = RelayIdsBuilder::default()
554                .ed_identity(ed1)
555                .rsa_identity(rsa2)
556                .build()
557                .unwrap();
558            assert_eq!(list.all_subset(&search), vec![&keys2]);
559        }
560    }
561
562    #[test]
563    fn list_by_relay_ids() {
564        #[derive(Clone, Debug)]
565        struct ErsatzChannel<T> {
566            val: T,
567            ids: RelayIds,
568        }
569
570        impl<T> ErsatzChannel<T> {
571            fn new(val: T, ids: RelayIds) -> Self {
572                Self { val, ids }
573            }
574        }
575
576        impl<T> HasRelayIds for ErsatzChannel<T> {
577            fn identity(&self, key_type: crate::RelayIdType) -> Option<RelayIdRef<'_>> {
578                self.ids.identity(key_type)
579            }
580        }
581
582        // helper to build a `RelayIds` to make tests shorter
583        fn ids(
584            rsa: impl Into<Option<RsaIdentity>>,
585            ed: impl Into<Option<Ed25519Identity>>,
586        ) -> RelayIds {
587            let mut ids = RelayIdsBuilder::default();
588            if let Some(rsa) = rsa.into() {
589                ids.rsa_identity(rsa);
590            }
591            if let Some(ed) = ed.into() {
592                ids.ed_identity(ed);
593            }
594            ids.build().unwrap()
595        }
596
597        // ids for relay A
598        let rsa_a: RsaIdentity = (*b"12345678901234567890").into();
599        let ed_a: Ed25519Identity = (*b"12345678901234567890123456789012").into();
600
601        // ids for relay B
602        let ed_b: Ed25519Identity = (*b"abcefghijklmnopqrstuvwxyzABCDEFG").into();
603        let rsa_b: RsaIdentity = (*b"abcefghijklmnopqrstu").into();
604
605        // channel to A with all ids
606        let channel_a_all = ErsatzChannel::new("channel-a-all", ids(rsa_a, ed_a));
607
608        // channel to A with only the rsa id
609        let channel_a_rsa_only_1 = ErsatzChannel::new("channel-a-rsa-only-1", ids(rsa_a, None));
610
611        // channel to A with only the rsa id; this could for example represent a channel with the
612        // same relay id as above but at a different ip address
613        let channel_a_rsa_only_2 = ErsatzChannel::new("channel-a-rsa-only-2", ids(rsa_a, None));
614
615        // channel to A with only the ed id
616        let channel_a_ed_only = ErsatzChannel::new("channel-a-ed-only", ids(None, ed_a));
617
618        // channel to B with all ids
619        let channel_b_all = ErsatzChannel::new("channel-b-all", ids(rsa_b, ed_b));
620
621        // an "invalid" channel with A's rsa id and B's ed id; this could for example represent an
622        // in-progress pending channel that hasn't been verified yet
623        let channel_invalid = ErsatzChannel::new("channel-invalid", ids(rsa_a, ed_b));
624
625        let mut list = ListByRelayIds::new();
626        list.insert(channel_a_all.clone());
627        list.insert(channel_a_rsa_only_1.clone());
628        list.insert(channel_a_rsa_only_2.clone());
629        list.insert(channel_a_ed_only.clone());
630        list.insert(channel_b_all.clone());
631        list.insert(channel_invalid.clone());
632
633        // look up by A's rsa id
634        assert_eq!(
635            sort(list.by_id(&rsa_a).map(|x| x.val)),
636            [
637                "channel-a-all",
638                "channel-a-rsa-only-1",
639                "channel-a-rsa-only-2",
640                "channel-invalid",
641            ],
642        );
643
644        // look up by A's ed id
645        assert_eq!(
646            sort(list.by_id(&ed_a).map(|x| x.val)),
647            ["channel-a-all", "channel-a-ed-only"],
648        );
649
650        // look up by B's rsa id
651        assert_eq!(sort(list.by_id(&rsa_b).map(|x| x.val)), ["channel-b-all"]);
652
653        // look up by B's ed id
654        assert_eq!(
655            sort(list.by_id(&ed_b).map(|x| x.val)),
656            ["channel-b-all", "channel-invalid"],
657        );
658
659        // look up by both A's rsa id and ed id
660        assert_eq!(
661            sort(list.by_all_ids(&ids(rsa_a, ed_a)).map(|x| x.val)),
662            ["channel-a-all"],
663        );
664
665        // look up by both B's rsa id and ed id
666        assert_eq!(
667            sort(list.by_all_ids(&ids(rsa_b, ed_b)).map(|x| x.val)),
668            ["channel-b-all"],
669        );
670
671        // look up by either A's rsa id or ed id
672        assert_eq!(
673            sort(
674                list.all_overlapping(&ids(rsa_a, ed_a))
675                    .into_iter()
676                    .map(|x| x.val)
677            ),
678            [
679                "channel-a-all",
680                "channel-a-ed-only",
681                "channel-a-rsa-only-1",
682                "channel-a-rsa-only-2",
683                "channel-invalid",
684            ],
685        );
686
687        // look up where channel's ids are a subset of A's ids
688        assert_eq!(
689            sort(
690                list.all_subset(&ids(rsa_a, ed_a))
691                    .into_iter()
692                    .map(|x| x.val)
693            ),
694            [
695                "channel-a-all",
696                "channel-a-ed-only",
697                "channel-a-rsa-only-1",
698                "channel-a-rsa-only-2",
699            ],
700        );
701
702        // some sanity checks
703        assert_eq!(list.by_all_ids(&ids(None, None)).count(), 0);
704        assert!(list.all_overlapping(&ids(None, None)).is_empty());
705        assert!(list.all_subset(&ids(None, None)).is_empty());
706        assert_eq!(
707            sort(
708                list.all_overlapping(&ids(rsa_a, None))
709                    .into_iter()
710                    .map(|x| x.val)
711            ),
712            sort(list.by_id(&rsa_a).map(|x| x.val)),
713        );
714        assert_eq!(
715            sort(
716                list.all_overlapping(&ids(None, ed_b))
717                    .into_iter()
718                    .map(|x| x.val)
719            ),
720            sort(list.by_id(&ed_b).map(|x| x.val)),
721        );
722        assert_eq!(
723            sort(list.by_id(&rsa_a).map(|x| x.val)),
724            sort(list.by_rsa(&rsa_a).map(|x| x.val)),
725        );
726        assert_eq!(
727            sort(list.by_id(&ed_a).map(|x| x.val)),
728            sort(list.by_ed25519(&ed_a).map(|x| x.val)),
729        );
730
731        // remove channels with exactly A's rsa id and ed id
732        {
733            let mut list = list.clone();
734            assert_eq!(
735                sort(
736                    list.remove_exact(&ids(rsa_a, ed_a))
737                        .into_iter()
738                        .map(|x| x.val)
739                ),
740                ["channel-a-all"],
741            );
742            assert_eq!(list.by_all_ids(&ids(rsa_a, ed_a)).count(), 0);
743        }
744
745        // remove channels with exactly A's rsa id and no ed id
746        {
747            let mut list = list.clone();
748            assert_eq!(
749                sort(
750                    list.remove_exact(&ids(rsa_a, None))
751                        .into_iter()
752                        .map(|x| x.val)
753                ),
754                ["channel-a-rsa-only-1", "channel-a-rsa-only-2"],
755            );
756            assert_eq!(
757                sort(list.by_all_ids(&ids(rsa_a, None)).map(|x| x.val)),
758                ["channel-a-all", "channel-invalid"],
759            );
760        }
761
762        // remove channels with at least A's rsa id
763        {
764            let mut list = list.clone();
765            assert_eq!(
766                sort(
767                    list.remove_by_all_ids(&ids(rsa_a, None))
768                        .into_iter()
769                        .map(|x| x.val)
770                ),
771                [
772                    "channel-a-all",
773                    "channel-a-rsa-only-1",
774                    "channel-a-rsa-only-2",
775                    "channel-invalid",
776                ],
777            );
778            assert_eq!(list.by_all_ids(&ids(rsa_a, None)).count(), 0);
779        }
780    }
781}