1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
//! Session demultiplexer.
use std::{
    collections::{BTreeSet, HashMap, HashSet},
    hash::Hash,
    io::Write,
    mem,
    sync::Arc,
};

use anyhow::Result;
use rand::{rngs::OsRng, Rng};
use tokio::sync::OwnedMutexGuard;

use super::{
    session::{Builder, Session, SessionInfo},
    types::{Message, SessionID},
};
use crate::common::{
    crypto::signature,
    namespace::Namespace,
    sgx::{EnclaveIdentity, QuotePolicy},
    time::insecure_posix_time,
};

/// Shared pointer to a multiplexed session.
pub type SharedSession<PeerID> = Arc<tokio::sync::Mutex<MultiplexedSession<PeerID>>>;

/// Key for use in the by-idle-time index.
pub type SessionByTimeKey<PeerID> = (i64, PeerID, SessionID);

/// Sessions error.
#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("max concurrent sessions reached")]
    MaxConcurrentSessions,
}

/// A multiplexed session.
pub struct MultiplexedSession<PeerID> {
    /// Peer identifier (needed for resolution when only given the shared pointer).
    peer_id: PeerID,
    /// Session identifier (needed for resolution when only given the shared pointer).
    session_id: SessionID,
    /// The actual session.
    inner: Session,
}

impl<PeerID> MultiplexedSession<PeerID> {
    /// Return the session's peer ID.
    pub fn get_peer_id(&self) -> &PeerID {
        &self.peer_id
    }

    /// Set the session's peer ID.
    pub fn set_peer_id(&mut self, peer_id: PeerID) {
        self.peer_id = peer_id;
    }

    /// Return the session ID.
    pub fn get_session_id(&self) -> &SessionID {
        &self.session_id
    }

    /// Session information.
    pub fn info(&self) -> Option<Arc<SessionInfo>> {
        self.inner.session_info()
    }

    /// Whether the session is in closed state.
    pub fn is_closed(&self) -> bool {
        self.inner.is_closed()
    }

    /// Process incoming session data.
    pub async fn process_data<W: Write>(
        &mut self,
        data: &[u8],
        writer: W,
    ) -> Result<Option<Message>> {
        self.inner.process_data(data, writer).await
    }

    /// Write message to session and generate a response.
    pub fn write_message<W: Write>(&mut self, msg: Message, mut writer: W) -> Result<()> {
        self.inner.write_message(msg, &mut writer)
    }

    /// Return remote node identifier.
    pub fn get_remote_node(&self) -> Result<signature::PublicKey> {
        self.inner.get_remote_node()
    }

    /// Set the remote node identifier.
    pub fn set_remote_node(&mut self, node: signature::PublicKey) -> Result<()> {
        self.inner.set_remote_node(node)
    }

    /// Whether the session handshake has completed and the session
    /// is in transport mode.
    pub fn is_connected(&self) -> bool {
        self.inner.is_connected()
    }

    /// Whether the session is in unauthenticated transport state. In this state the session can
    /// only be used to transmit a close notification.
    pub fn is_unauthenticated(&self) -> bool {
        self.inner.is_unauthenticated()
    }

    /// Mark the session as closed.
    ///
    /// After the session is closed it can no longer be used to transmit
    /// or receive messages and any such use will result in an error.
    pub fn close(&mut self) {
        self.inner.close()
    }
}

/// Structure used for session accounting.
pub struct SessionMeta<PeerID: Clone + Ord + Hash> {
    /// Peer identifier.
    peer_id: PeerID,
    /// Session identifier.
    session_id: SessionID,
    /// Timestamp when the session was last accessed.
    last_access_time: i64,
    /// The shared session pointer that needs to be locked for access.
    inner: SharedSession<PeerID>,
}

impl<PeerID> SessionMeta<PeerID>
where
    PeerID: Clone + Ord + Hash,
{
    /// Key for ordering in the by-idle-time index.
    fn by_time_key(&self) -> SessionByTimeKey<PeerID> {
        (self.last_access_time, self.peer_id.clone(), self.session_id)
    }
}

/// Session indices and management operations.
pub struct Sessions<PeerID: Clone + Ord + Hash> {
    /// Session builder.
    builder: Builder,
    /// Maximum number of sessions.
    max_sessions: usize,
    /// Maximum number of sessions per peer.
    max_sessions_per_peer: usize,
    /// Stale session timeout (in seconds).
    stale_session_timeout: i64,

    /// A map of sessions for each peer.
    by_peer: HashMap<PeerID, HashMap<SessionID, SessionMeta<PeerID>>>,
    /// A set of all sessions, ordered by idle time.
    by_idle_time: BTreeSet<SessionByTimeKey<PeerID>>,
}

impl<PeerID> Sessions<PeerID>
where
    PeerID: Clone + Ord + Hash,
{
    /// Create a new session management instance.
    pub fn new(
        builder: Builder,
        max_sessions: usize,
        max_sessions_per_peer: usize,
        stale_session_timeout: i64,
    ) -> Self {
        Self {
            builder,
            max_sessions,
            max_sessions_per_peer,
            stale_session_timeout,
            by_peer: HashMap::new(),
            by_idle_time: BTreeSet::new(),
        }
    }

    /// Set the session builder to use.
    pub fn set_builder(&mut self, builder: Builder) {
        self.builder = builder;
    }

    /// Update remote enclave identity verification in the session builder
    /// and clear all sessions if the identity has changed.
    pub fn update_enclaves(
        &mut self,
        enclaves: Option<HashSet<EnclaveIdentity>>,
    ) -> Vec<SharedSession<PeerID>> {
        if self.builder.get_remote_enclaves() == &enclaves {
            return vec![];
        }

        self.builder = mem::take(&mut self.builder).remote_enclaves(enclaves);
        self.drain()
    }

    /// Update quote policy used for remote quote verification in the session builder
    /// and clear all sessions if the policy has changed.
    pub fn update_quote_policy(&mut self, policy: QuotePolicy) -> Vec<SharedSession<PeerID>> {
        let policy = Some(Arc::new(policy));
        if self.builder.get_quote_policy() == &policy {
            return vec![];
        }

        self.builder = mem::take(&mut self.builder).quote_policy(policy);
        self.drain()
    }

    /// Update remote runtime ID for node identity verification in the session builder
    /// and clear all sessions if the runtime ID has changed.
    pub fn update_runtime_id(&mut self, id: Option<Namespace>) -> Vec<SharedSession<PeerID>> {
        if self.builder.get_remote_runtime_id() == &id {
            return vec![];
        }

        self.builder = mem::take(&mut self.builder).remote_runtime_id(id);
        self.drain()
    }

    /// Create a new multiplexed responder session.
    pub fn create_responder(
        &mut self,
        peer_id: PeerID,
        session_id: SessionID,
    ) -> MultiplexedSession<PeerID> {
        // If no quote policy is set, use the local one.
        if self.builder.get_quote_policy().is_none() {
            let policy = self
                .builder
                .get_local_identity()
                .as_ref()
                .and_then(|id| id.quote_policy());

            self.builder = mem::take(&mut self.builder).quote_policy(policy);
        }

        MultiplexedSession {
            peer_id: peer_id.clone(),
            session_id,
            inner: self.builder.clone().build_responder(),
        }
    }

    /// Create a new multiplexed initiator session.
    pub fn create_initiator(&self, peer_id: PeerID) -> MultiplexedSession<PeerID> {
        let session_id = SessionID::random();

        MultiplexedSession {
            peer_id: peer_id.clone(),
            session_id,
            inner: self.builder.clone().build_initiator(),
        }
    }

    /// Fetch an existing session given its identifier.
    pub fn get(
        &mut self,
        peer_id: &PeerID,
        session_id: &SessionID,
    ) -> Option<SharedSession<PeerID>> {
        // Check if peer exists.
        let sessions = match self.by_peer.get_mut(peer_id) {
            Some(sessions) => sessions,
            None => return None,
        };

        // Check if the session exists. If so, return it.
        let session = match sessions.get_mut(session_id) {
            Some(session) => session,
            None => return None,
        };

        Self::update_access_time(session, &mut self.by_idle_time);

        Some(session.inner.clone())
    }

    /// Fetch an existing session from one of the given peers. If no peers
    /// are provided, a session from any peer will be returned.
    pub fn find(&mut self, peer_ids: &[PeerID]) -> Option<SharedSession<PeerID>> {
        match peer_ids.is_empty() {
            true => self.find_any(),
            false => self.find_one(peer_ids),
        }
    }

    /// Fetch an existing session from any peer.
    pub fn find_any(&mut self) -> Option<SharedSession<PeerID>> {
        if self.by_idle_time.is_empty() {
            return None;
        }

        // Check if there is a session that is not currently in use.
        for (_, peer_id, session_id) in self.by_idle_time.iter() {
            let session = self
                .by_peer
                .get_mut(peer_id)
                .unwrap()
                .get_mut(session_id)
                .unwrap();

            if session.inner.clone().try_lock_owned().is_ok() {
                Self::update_access_time(session, &mut self.by_idle_time);
                return Some(session.inner.clone());
            }
        }

        // If all sessions are in use, return a random one.
        let n = OsRng.gen_range(0..self.by_idle_time.len());
        let (_, peer_id, session_id) = self.by_idle_time.iter().nth(n).unwrap();
        let session = self
            .by_peer
            .get_mut(peer_id)
            .unwrap()
            .get_mut(session_id)
            .unwrap();

        Self::update_access_time(session, &mut self.by_idle_time);

        Some(session.inner.clone())
    }

    /// Fetch an existing session from one of the given peers.
    pub fn find_one(&mut self, peer_ids: &[PeerID]) -> Option<SharedSession<PeerID>> {
        let mut all_sessions = vec![];

        for peer_id in peer_ids.iter() {
            let sessions = match self.by_peer.get_mut(peer_id) {
                Some(sessions) => sessions,
                None => return None,
            };

            // Check if peer has a session that is not currently in use.
            let session = sessions
                .values_mut()
                .filter(|s| s.inner.clone().try_lock_owned().is_ok())
                .min_by_key(|s| s.last_access_time);

            if let Some(session) = session {
                Self::update_access_time(session, &mut self.by_idle_time);
                return Some(session.inner.clone());
            }

            for session in sessions.values() {
                all_sessions.push((session.peer_id.clone(), session.session_id));
            }
        }

        if all_sessions.is_empty() {
            return None;
        }

        // If all sessions are in use, return a random one.
        let n = OsRng.gen_range(0..all_sessions.len());
        let (peer_id, session_id) = all_sessions.get(n).unwrap();
        let session = self
            .by_peer
            .get_mut(peer_id)
            .unwrap()
            .get_mut(session_id)
            .unwrap();

        Self::update_access_time(session, &mut self.by_idle_time);

        Some(session.inner.clone())
    }

    /// Remove one session to free up a slot for the given peer.
    pub fn remove_for(
        &mut self,
        peer_id: &PeerID,
        now: i64,
    ) -> Result<Option<OwnedMutexGuard<MultiplexedSession<PeerID>>>, Error> {
        if let Some(session) = self.remove_from(peer_id)? {
            return Ok(Some(session));
        }
        self.remove_one(now)
    }

    /// Remove one existing session from the given peer if the peer has reached
    /// the maximum number of sessions or if the total number of sessions exceeds
    /// the global session limit.
    pub fn remove_from(
        &mut self,
        peer_id: &PeerID,
    ) -> Result<Option<OwnedMutexGuard<MultiplexedSession<PeerID>>>, Error> {
        // Check if peer exists.
        let sessions = match self.by_peer.get_mut(peer_id) {
            Some(sessions) => sessions,
            None => return Ok(None),
        };

        // Check if the peer has max sessions or if no more sessions are available globally.
        // If so, remove the oldest or return an error.
        if sessions.len() < self.max_sessions_per_peer
            && self.by_idle_time.len() < self.max_sessions
        {
            return Ok(None);
        }

        // Force close the oldest idle session.
        let remove_session = sessions
            .iter()
            .min_by_key(|(_, s)| {
                if let Ok(_inner) = s.inner.try_lock() {
                    s.last_access_time
                } else {
                    i64::MAX // Session is currently in use.
                }
            })
            .map(|(_, s)| s.inner.clone())
            .ok_or(Error::MaxConcurrentSessions)?;

        let session = match remove_session.try_lock_owned() {
            Ok(inner) => inner,
            Err(_) => return Err(Error::MaxConcurrentSessions), // All sessions are in use.
        };

        self.remove(&session);

        Ok(Some(session))
    }

    /// Remove one stale session if the total number of sessions exceeds
    /// the global session limit.
    pub fn remove_one(
        &mut self,
        now: i64,
    ) -> Result<Option<OwnedMutexGuard<MultiplexedSession<PeerID>>>, Error> {
        // Check if there are too many sessions. If so, remove one or return an error.
        if self.by_idle_time.len() < self.max_sessions {
            return Ok(None);
        }

        // Attempt to prune stale sessions, starting with the oldest ones.
        let mut remove_session: Option<OwnedMutexGuard<MultiplexedSession<PeerID>>> = None;

        for (last_process_frame_time, peer_id, session_id) in self.by_idle_time.iter() {
            if now.saturating_sub(*last_process_frame_time) < self.stale_session_timeout {
                // This is the oldest session, all next ones will be more fresh.
                return Err(Error::MaxConcurrentSessions);
            }

            // Fetch session and attempt to lock it.
            if let Some(sessions) = self.by_peer.get(peer_id) {
                if let Some(session) = sessions.get(session_id) {
                    if let Ok(session) = session.inner.clone().try_lock_owned() {
                        remove_session = Some(session);
                        break;
                    }
                }
            }
        }

        // Check if we found a session that can be removed.
        let session = match remove_session {
            Some(session) => session,
            None => return Err(Error::MaxConcurrentSessions), // All stale sessions are in use.
        };

        self.remove(&session);

        Ok(Some(session))
    }

    /// Add a session if there is an available spot.
    pub fn add(
        &mut self,
        session: MultiplexedSession<PeerID>,
        now: i64,
    ) -> Result<SharedSession<PeerID>, Error> {
        if self.by_idle_time.len() >= self.max_sessions {
            return Err(Error::MaxConcurrentSessions);
        }

        let sessions = self.by_peer.entry(session.peer_id.clone()).or_default();
        if sessions.len() >= self.max_sessions_per_peer {
            return Err(Error::MaxConcurrentSessions);
        }

        let peer_id = session.peer_id.clone();
        let session_id = session.session_id;

        let session = SessionMeta {
            inner: Arc::new(tokio::sync::Mutex::new(session)),
            peer_id,
            session_id,
            last_access_time: now,
        };
        let inner = session.inner.clone();

        self.by_idle_time.insert(session.by_time_key());
        sessions.insert(session.session_id, session);

        Ok(inner)
    }

    /// Remove a session that must be currently owned by the caller.
    pub fn remove(&mut self, session: &OwnedMutexGuard<MultiplexedSession<PeerID>>) {
        let sessions = self.by_peer.get_mut(&session.peer_id).unwrap();
        let session_meta = sessions.get(&session.session_id).unwrap();
        let key = session_meta.by_time_key();
        sessions.remove(&session.session_id);
        self.by_idle_time.remove(&key);

        // If peer doesn't have any more sessions, remove the peer.
        if sessions.is_empty() {
            self.by_peer.remove(&session.peer_id);
        }
    }

    /// Removes and returns all sessions.
    pub fn drain(&mut self) -> Vec<SharedSession<PeerID>> {
        self.by_idle_time.clear();

        let mut all_sessions = vec![];
        for (_, mut sessions) in self.by_peer.drain() {
            for (_, session) in sessions.drain() {
                all_sessions.push(session.inner);
            }
        }

        all_sessions
    }

    fn update_access_time(
        session: &mut SessionMeta<PeerID>,
        by_idle_time: &mut BTreeSet<SessionByTimeKey<PeerID>>,
    ) {
        // Remove old idle time.
        by_idle_time.remove(&session.by_time_key());

        // Update idle time.
        session.last_access_time = insecure_posix_time();
        by_idle_time.insert(session.by_time_key());
    }

    /// Number of all sessions.
    #[cfg(test)]
    fn session_count(&self) -> usize {
        self.by_idle_time.len()
    }

    /// Number of all peers.
    #[cfg(test)]
    fn peer_count(&self) -> usize {
        self.by_peer.len()
    }
}

#[cfg(test)]
mod test {
    use crate::enclave_rpc::{session::Builder, types::SessionID};

    use super::{Error, Sessions};

    fn ids() -> (Vec<Vec<u8>>, Vec<SessionID>) {
        let peer_ids: Vec<Vec<u8>> = (1..8).map(|x| vec![x]).collect();
        let session_ids: Vec<SessionID> = (1..8).map(|_| SessionID::random()).collect();

        (peer_ids, session_ids)
    }

    #[test]
    fn test_add() {
        let (peer_ids, session_ids) = ids();
        let mut sessions = Sessions::new(Builder::default(), 4, 2, 60);

        let test_vector = vec![
            (&peer_ids[0], &session_ids[0], 1, 1, true),
            (&peer_ids[0], &session_ids[1], 2, 1, true), // Different session ID.
            (&peer_ids[0], &session_ids[2], 2, 1, false), // Too many sessions per peer.
            (&peer_ids[1], &session_ids[0], 3, 2, true), // Different peer ID.
            (&peer_ids[2], &session_ids[2], 4, 3, true), // Different peer ID and session ID.
            (&peer_ids[3], &session_ids[3], 4, 3, false), // Too many sessions.
        ];

        let now = 0;
        for (peer_id, session_id, num_sessions, num_peers, created) in test_vector {
            let session = sessions.create_responder(peer_id.clone(), session_id.clone());
            let res = sessions.add(session, now);
            match created {
                true => {
                    assert!(res.is_ok(), "session should be created");
                    let s = res.unwrap();
                    let s_owned = s.try_lock().unwrap();
                    assert_eq!(&s_owned.peer_id, peer_id);
                    assert_eq!(&s_owned.session_id, session_id);
                }
                false => {
                    assert!(res.is_err(), "session should not be created");
                    assert!(matches!(res, Err(Error::MaxConcurrentSessions)));
                }
            };
            assert_eq!(sessions.session_count(), num_sessions);
            assert_eq!(sessions.peer_count(), num_peers);
        }
    }

    #[test]
    fn test_get() {
        let (peer_ids, session_ids) = ids();
        let mut sessions = Sessions::new(Builder::default(), 8, 2, 60);

        let test_vector = vec![
            (&peer_ids[0], &session_ids[0], true),
            (&peer_ids[0], &session_ids[1], false), // Different peer ID.
            (&peer_ids[1], &session_ids[0], false), // Different session ID.
            (&peer_ids[1], &session_ids[1], false), // Different peer ID and session ID.
        ];

        let now = 0;
        for (peer_id, session_id, create) in test_vector {
            if create {
                let session = sessions.create_responder(peer_id.clone(), session_id.clone());
                let _ = sessions.add(session, now);
            }

            let maybe_s = sessions.get(peer_id, session_id);
            match create {
                true => {
                    assert!(maybe_s.is_some(), "session should exist");
                    let s = maybe_s.unwrap();
                    let s_owned = s.try_lock_owned().unwrap();
                    assert_eq!(&s_owned.peer_id, peer_id);
                    assert_eq!(&s_owned.session_id, session_id);
                }
                false => assert!(maybe_s.is_none(), "session should not exist"),
            }
        }
    }

    #[test]
    fn test_find_any() {
        let (peer_ids, session_ids) = ids();
        let mut sessions = Sessions::new(Builder::default(), 8, 2, 60);

        let test_vector = vec![
            (&peer_ids[0], &session_ids[0]),
            (&peer_ids[0], &session_ids[1]),
            (&peer_ids[1], &session_ids[2]),
        ];

        // No sessions.
        let maybe_s = sessions.find_any();
        assert!(maybe_s.is_none(), "session should not be found");

        let mut now = 0;
        for (peer_id, session_id) in test_vector {
            let session = sessions.create_responder(peer_id.clone(), session_id.clone());
            let _ = sessions.add(session, now);
            now += 1
        }

        // No sessions in use.
        let maybe_s = sessions.find_any();
        assert!(maybe_s.is_some(), "session should be found");
        let s = maybe_s.unwrap();
        let s1_owned = s.try_lock_owned().unwrap(); // Session now in use.
        assert_eq!(&s1_owned.peer_id, &peer_ids[0]);
        assert_eq!(&s1_owned.session_id, &session_ids[0]);

        // One session in use.
        let maybe_s = sessions.find_any();
        assert!(maybe_s.is_some(), "session should be found");
        let s = maybe_s.unwrap();
        let s2_owned = s.try_lock_owned().unwrap(); // Session now in use.
        assert_eq!(&s2_owned.peer_id, &peer_ids[0]);
        assert_eq!(&s2_owned.session_id, &session_ids[1]); // Different session found.

        // Two sessions in use.
        let maybe_s = sessions.find_any();
        assert!(maybe_s.is_some(), "session should be found");
        let s = maybe_s.unwrap();
        let s3_owned = s.try_lock_owned().unwrap(); // Session now in use.
        assert_eq!(&s3_owned.peer_id, &peer_ids[1]);
        assert_eq!(&s3_owned.session_id, &session_ids[2]); // Different session found.

        // All sessions in use.
        let maybe_s = sessions.find_any();
        assert!(maybe_s.is_some(), "session should be found");
        let s = maybe_s.unwrap();
        let res = s.try_lock_owned(); // Session now in use.
        assert!(res.is_err(), "session should be in use");

        // Free one session.
        drop(s2_owned);

        // Two sessions in use.
        let maybe_s = sessions.find_any();
        assert!(maybe_s.is_some(), "session should be found");
        let s = maybe_s.unwrap();
        let s_owned = s.try_lock_owned().unwrap(); // Session now in use.
        assert_eq!(&s_owned.peer_id, &peer_ids[0]);
        assert_eq!(&s_owned.session_id, &session_ids[1]);
    }

    #[test]
    fn test_find_one() {
        let (peer_ids, session_ids) = ids();
        let mut sessions = Sessions::new(Builder::default(), 8, 2, 60);

        let test_vector = vec![
            (&peer_ids[2], &session_ids[0]), // Incorrect peer.
            (&peer_ids[0], &session_ids[0]),
            (&peer_ids[3], &session_ids[1]), // Incorrect peer.
            (&peer_ids[0], &session_ids[1]),
            (&peer_ids[3], &session_ids[2]), // Incorrect peer.
            (&peer_ids[1], &session_ids[2]),
            (&peer_ids[2], &session_ids[2]), // Incorrect peer.
        ];

        // No sessions.
        let maybe_s = sessions.find_one(&peer_ids[0..2]);
        assert!(maybe_s.is_none(), "session should not be found");

        let mut now = 0;
        for (peer_id, session_id) in test_vector {
            let session = sessions.create_responder(peer_id.clone(), session_id.clone());
            let _ = sessions.add(session, now);
            now += 1
        }

        // Peers without sessions.
        let maybe_s = sessions.find_one(&peer_ids[4..]);
        assert!(maybe_s.is_none(), "session should not be found");

        // No sessions in use.
        let maybe_s = sessions.find_one(&peer_ids[0..2]);
        assert!(maybe_s.is_some(), "session should be found");
        let s = maybe_s.unwrap();
        let s1_owned = s.try_lock_owned().unwrap(); // Session now in use.
        assert_eq!(&s1_owned.peer_id, &peer_ids[0]);
        assert_eq!(&s1_owned.session_id, &session_ids[0]);

        // One session in use.
        let maybe_s = sessions.find_one(&peer_ids[0..2]);
        assert!(maybe_s.is_some(), "session should be found");
        let s = maybe_s.unwrap();
        let s2_owned = s.try_lock_owned().unwrap(); // Session now in use.
        assert_eq!(&s2_owned.peer_id, &peer_ids[0]);
        assert_eq!(&s2_owned.session_id, &session_ids[1]); // Different session found.

        // Two sessions in use.
        let maybe_s = sessions.find_one(&peer_ids[0..2]);
        assert!(maybe_s.is_some(), "session should be found");
        let s = maybe_s.unwrap();
        let s3_owned = s.try_lock_owned().unwrap(); // Session now in use.
        assert_eq!(&s3_owned.peer_id, &peer_ids[1]);
        assert_eq!(&s3_owned.session_id, &session_ids[2]); // Different session found.

        // All sessions in use.
        let maybe_s = sessions.find_one(&peer_ids[0..2]);
        assert!(maybe_s.is_some(), "session should be found");
        let s = maybe_s.unwrap();
        let res = s.try_lock_owned(); // Session now in use.
        assert!(res.is_err(), "session should be in use");

        // Free one session.
        drop(s2_owned);

        // Two sessions in use.
        let maybe_s = sessions.find_one(&peer_ids[0..2]);
        assert!(maybe_s.is_some(), "session should be found");
        let s = maybe_s.unwrap();
        let s_owned = s.try_lock_owned().unwrap(); // Session now in use.
        assert_eq!(&s_owned.peer_id, &peer_ids[0]);
        assert_eq!(&s_owned.session_id, &session_ids[1]);
    }

    #[test]
    fn test_remove_from() {
        let (peer_ids, session_ids) = ids();
        let mut sessions = Sessions::new(Builder::default(), 4, 2, 60);

        let test_vector = vec![
            (&peer_ids[0], &session_ids[0]),
            (&peer_ids[1], &session_ids[1]),
            (&peer_ids[2], &session_ids[2]),
            (&peer_ids[2], &session_ids[3]), // Max sessions per peer reached.
                                             // Max sessions reached.
        ];

        let mut now = 0;
        for (peer_id, session_id) in test_vector.clone() {
            let session = sessions.create_responder(peer_id.clone(), session_id.clone());
            let _ = sessions.add(session, now);
            now += 1;
        }

        // Removing one session from an unknown peer should have no effect,
        // even if all global session slots are occupied.
        let res = sessions.remove_from(&peer_ids[3]);
        assert!(res.is_ok(), "remove_from should succeed");
        let maybe_s_owned = res.unwrap();
        assert!(maybe_s_owned.is_none(), "no sessions should be removed");
        assert_eq!(sessions.session_count(), 4);
        assert_eq!(sessions.peer_count(), 3);

        // Removing one session for one of the existing peers should work
        // as it should force evict an old session.
        // Note that each peer has 2 available slots, but globally there are
        // only 4 slots so if global slots are full this should trigger peer
        // session eviction.
        let res = sessions.remove_from(&peer_ids[0]);
        assert!(res.is_ok(), "remove_from should succeed");
        let maybe_s_owned = res.unwrap();
        assert!(maybe_s_owned.is_some(), "one session should be removed");
        let s_owned = maybe_s_owned.unwrap();
        assert_eq!(&s_owned.peer_id, &peer_ids[0]);
        assert_eq!(&s_owned.session_id, &session_ids[0]);
        assert_eq!(sessions.session_count(), 3);
        assert_eq!(sessions.peer_count(), 2);

        // Removing another session should fail as one global session slot
        // is available.
        for peer_id in vec![&peer_ids[0], &peer_ids[1]] {
            let res = sessions.remove_from(peer_id);
            assert!(res.is_ok(), "remove_from should succeed");
            let maybe_s_owned = res.unwrap();
            assert!(maybe_s_owned.is_none(), "no sessions should be removed");
            assert_eq!(sessions.session_count(), 3);
            assert_eq!(sessions.peer_count(), 2);
        }

        // Removing one session from a peer with max sessions should succeed
        // even if one global slot is available.
        let res = sessions.remove_from(&peer_ids[2]);
        assert!(res.is_ok(), "remove_from should succeed");
        let maybe_s_owned = res.unwrap();
        assert!(maybe_s_owned.is_some(), "one session should be removed");
        let s_owned = maybe_s_owned.unwrap();
        assert_eq!(&s_owned.peer_id, &peer_ids[2]);
        assert_eq!(&s_owned.session_id, &session_ids[2]);
        assert_eq!(sessions.session_count(), 2);
        assert_eq!(sessions.peer_count(), 2);
    }

    #[test]
    fn test_remove_one() {
        let (peer_ids, session_ids) = ids();
        let mut sessions = Sessions::new(Builder::default(), 4, 2, 60);

        let test_vector = vec![
            (&peer_ids[0], &session_ids[0]),
            (&peer_ids[1], &session_ids[1]),
            (&peer_ids[2], &session_ids[2]),
            (&peer_ids[2], &session_ids[3]), // Max sessions reached.
        ];

        let mut now = 0;
        for (peer_id, session_id) in test_vector.clone() {
            let session = sessions.create_responder(peer_id.clone(), session_id.clone());
            let _ = sessions.add(session, now);
            now += 1;
        }

        // Forward time (stale_session_timeout - test_vector.len() - 1).
        now += 60 - 4 - 1;

        // Removing one session should fail as there are none stale sessions.
        let res = sessions.remove_one(now);
        assert!(res.is_err(), "remove_one should fail");
        assert!(matches!(res, Err(Error::MaxConcurrentSessions)));
        assert_eq!(sessions.session_count(), 4);
        assert_eq!(sessions.peer_count(), 3);

        // Forward time.
        now += 1;

        // Removing one session should succeed as no session slots
        // are available and there is one stale session.
        let res = sessions.remove_one(now);
        assert!(res.is_ok(), "remove_one should succeed");
        let maybe_s_owned = res.unwrap();
        assert!(maybe_s_owned.is_some(), "one session should be removed");
        let s_owned = maybe_s_owned.unwrap();
        assert_eq!(&s_owned.peer_id, &peer_ids[0]);
        assert_eq!(&s_owned.session_id, &session_ids[0]);
        assert_eq!(sessions.session_count(), 3);
        assert_eq!(sessions.peer_count(), 2);

        // Forward time.
        now += 100;

        // Removing one session should fail even though there are stale sessions
        // because there is one session slot available.
        let res = sessions.remove_one(now);
        assert!(res.is_ok(), "remove_one should succeed");
        let maybe_s_owned = res.unwrap();
        assert!(maybe_s_owned.is_none(), "no sessions should be removed");
        assert_eq!(sessions.session_count(), 3);
        assert_eq!(sessions.peer_count(), 2);
    }

    #[test]
    fn test_remove() {
        let (peer_ids, session_ids) = ids();
        let mut sessions = Sessions::new(Builder::default(), 8, 2, 60);

        let test_vector = vec![
            (&peer_ids[0], &session_ids[0], 3, 2),
            (&peer_ids[1], &session_ids[1], 2, 1),
            (&peer_ids[2], &session_ids[2], 1, 1),
            (&peer_ids[2], &session_ids[3], 0, 0),
        ];

        let now = 0;
        for (peer_id, session_id, _, _) in test_vector.clone() {
            let session = sessions.create_responder(peer_id.clone(), session_id.clone());
            let _ = sessions.add(session, now);
        }

        for (peer_id, session_id, num_sessions, num_peers) in test_vector {
            let maybe_s = sessions.get(peer_id, session_id);
            assert!(maybe_s.is_some(), "session should exist");
            let s = maybe_s.unwrap();
            let s_owned = s.try_lock_owned().unwrap();

            sessions.remove(&s_owned);
            assert_eq!(sessions.session_count(), num_sessions);
            assert_eq!(sessions.peer_count(), num_peers);
        }
    }

    #[test]
    fn test_clear() {
        let (peer_ids, session_ids) = ids();
        let mut sessions = Sessions::new(Builder::default(), 8, 2, 60);

        let test_vector = vec![
            (&peer_ids[0], &session_ids[0]),
            (&peer_ids[1], &session_ids[1]),
            (&peer_ids[2], &session_ids[2]),
            (&peer_ids[2], &session_ids[3]),
        ];

        let now = 0;
        for (peer_id, session_id) in test_vector.clone() {
            let session = sessions.create_responder(peer_id.clone(), session_id.clone());
            let _ = sessions.add(session, now);
        }

        let removed_sessions = sessions.drain();
        assert_eq!(removed_sessions.len(), 4);
        assert_eq!(sessions.session_count(), 0);
        assert_eq!(sessions.peer_count(), 0);
    }
}