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
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use {
    async_trait::async_trait,
    fidl::Error as FidlError,
    fidl_fuchsia_wlan_policy::{
        Bss as WlanPolicyBss, ScanErrorCode, ScanResult, ScanResultIteratorProxyInterface,
    },
    futures::{
        future::BoxFuture,
        task::{Context, Poll},
        Future, FutureExt, Stream, StreamExt,
    },
    std::{collections::BTreeMap, pin::Pin},
    thiserror::Error,
};

#[async_trait(?Send)]
pub trait BssCache {
    /// Updates the cache with BSSes from `new_bsses`.
    async fn update<I: ScanResultIteratorProxyInterface>(
        &mut self,
        new_bsses: I,
    ) -> Result<(), UpdateError>;

    /// Returns an iterator over the known BSSes.
    fn iter(&self) -> Box<dyn Iterator<Item = (&'_ BssId, &'_ Bss)> + '_>;
}

/// A cache for WLAN Basic Service Sets, also known as WLAN base-stations.
#[derive(Default)]
pub struct RealBssCache {
    bss_map: BTreeMap<BssId, Bss>,
}

pub type BssId = [u8; BSS_ADDR_LEN_BYTES];

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Bss {
    pub(crate) rssi: Option<i8>,
    pub(crate) frequency: Option<u32>,
}

#[derive(Clone, Copy, Debug, Error, PartialEq)]
pub enum UpdateError {
    #[error("found BSSes, but no BSS IDs")]
    NoBssIds,
    #[error("found no BSSes")]
    NoBsses,
    #[error("connection to iterator failed")]
    Ipc,
    #[error("iterator reported error")]
    Service,
}

// Length of a BSS ID. Governed by IEEE standards.
const BSS_ADDR_LEN_BYTES: usize = 6;

// Upper bound on the number of BSSes cached. Our goals in tuning this value are:
// * retain enough BSSes to give a tight radius of confidence, and
// * minimize the potential for exhausting memory
//
// This value was chosen somewhat arbitrarily, and may be tuned based on field
// data about the radius of confidence we achieve.
const MAX_BSSES: usize = 20;

// Upper bound on the number of IPCs to `ScanResultIterator`. Every non-terminal
// IPC should yield at least one `ScanResult`. And, in normal operation, we expect
// that a `ScanResult` will have at least one BSS. Hence, we set this limit to
// equal the limit on BSSes.
const MAX_IPCS: usize = MAX_BSSES;

impl RealBssCache {
    pub fn new() -> Self {
        Default::default()
    }

    fn prune_to_size(&mut self) {
        if let Some(first_overflowed_bssid) = self.bss_map.keys().cloned().nth(MAX_BSSES) {
            self.bss_map.split_off(&first_overflowed_bssid);
        }
    }
}

#[async_trait(?Send)]
impl BssCache for RealBssCache {
    async fn update<I: ScanResultIteratorProxyInterface>(
        &mut self,
        new_bsses: I,
    ) -> Result<(), UpdateError> {
        let mut iterator_service_result = ScanResultStream::new(new_bsses)
            .take(MAX_IPCS)
            .collect::<Vec<Result<Vec<ScanResult>, UpdateError>>>()
            .await
            .into_iter()
            .peekable();
        // If we have no results, report the appropriate error.
        match iterator_service_result.peek() {
            None => return Err(UpdateError::NoBsses), // First IPC yielded empty set.
            Some(Err(e)) => return Err(*e),           // First IPC yielded error.
            Some(Ok(_)) => (),
        };

        let mut bss_list = iterator_service_result
            .filter_map(|res| res.ok()) // Since we have at least one result, ignore errors.
            .flatten() // Flatten per-IPC `Vec`s into single `Vec`.
            .flat_map(|network| network.entries) // Project `Vec` of BSSes out of each network.
            .flatten() // Flatten per-network `Vec`s of BSSes to single `Vec`.
            .peekable();
        if bss_list.peek().is_none() {
            return Err(UpdateError::NoBsses);
        };

        let mut valid_bss_list = bss_list
            .filter_map(|bss: WlanPolicyBss| match bss.bssid {
                Some(id) => Some((id, Bss { rssi: bss.rssi, frequency: bss.frequency })),
                None => None,
            })
            .peekable();
        if valid_bss_list.peek().is_none() {
            return Err(UpdateError::NoBssIds);
        }

        self.bss_map = valid_bss_list.collect();
        self.prune_to_size();
        Ok(())
    }

    fn iter(&self) -> Box<dyn Iterator<Item = (&'_ BssId, &'_ Bss)> + '_> {
        Box::new(self.bss_map.iter())
    }
}

type GetNextResponse = Result<Result<Vec<ScanResult>, ScanErrorCode>, FidlError>;

// ScanResultStream adapts the FIDL `ScanResultIterator` into a Rust
// `Stream`.  For more details, see the documentation for
// `ScanResultStream::poll_next()`, below.
struct ScanResultStream<'a, F, I>
where
    F: Future<Output = GetNextResponse> + Send,
    I: ScanResultIteratorProxyInterface<GetNextResponseFut = F>,
{
    iterator_service: Option<I>,
    pending_ipc: Option<BoxFuture<'a, F::Output>>,
}

impl<'a, F, I> ScanResultStream<'a, F, I>
where
    F: Future<Output = GetNextResponse> + Send,
    I: ScanResultIteratorProxyInterface<GetNextResponseFut = F>,
{
    fn new(iterator_service: I) -> Self {
        Self { iterator_service: Some(iterator_service), pending_ipc: None }
    }
}

impl<'a, F, I> Unpin for ScanResultStream<'a, F, I>
where
    F: Future<Output = GetNextResponse> + Send,
    I: ScanResultIteratorProxyInterface<GetNextResponseFut = F>,
{
}

impl<'a, F, I> Stream for ScanResultStream<'a, F, I>
where
    F: Future<Output = GetNextResponse> + Send + 'a,
    I: ScanResultIteratorProxyInterface<GetNextResponseFut = F>,
{
    type Item = Result<Vec<ScanResult>, UpdateError>;

    // Calls through to `ScanResultIterator.GetNext()`.
    // * If the call yields a `FidlError` or a `ScanErrorCode`, returns an `UpdateError`,
    //   and ensures that subsequent calls yield None.
    // * If the call yields an empty `Vec` yields None.
    // * Otherwise, returns the `Vec` from the call to `GetNext()`.
    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        // Note: we `take()` `iterator_service` here, and only replace
        // `iterator_service` if
        // a) the IPC yielded a `ScanResult`, or
        // b) the IPC is still pending.
        //
        // By doing so, we ensure that, if the IPC yielded an empty
        // result, or an error, then subsequent calls to `poll_next()`
        // a) will _not_ issue an IPC, and
        // b) will return None.
        let iterator_service = match self.iterator_service.take() {
            Some(is) => is,
            None => return Poll::Ready(None),
        };
        let mut fut = match self.pending_ipc.take() {
            Some(ipc) => ipc,
            None => iterator_service.get_next().boxed(),
        };
        match fut.poll_unpin(cx) {
            Poll::Pending => {
                self.pending_ipc = Some(fut);
                self.iterator_service = Some(iterator_service);
                Poll::Pending
            }
            Poll::Ready(fidl_result) => Poll::Ready(match flatten_get_next_error(fidl_result) {
                Ok(res) => {
                    if res.is_empty() {
                        None
                    } else {
                        self.iterator_service = Some(iterator_service);
                        Some(Ok(res))
                    }
                }
                Err(e) => Some(Err(e)),
            }),
        }
    }
}

fn flatten_get_next_error(fidl_result: GetNextResponse) -> Result<Vec<ScanResult>, UpdateError> {
    match fidl_result {
        Ok(service_result) => match service_result {
            Ok(scan_results) => Ok(scan_results),
            Err(_) => Err(UpdateError::Service),
        },
        Err(_) => Err(UpdateError::Ipc),
    }
}

#[cfg(test)]
mod tests {
    mod single_call_success {
        use {
            super::super::*,
            assert_matches::assert_matches,
            fidl_fuchsia_wlan_policy::{
                Compatibility::Supported, NetworkIdentifier, SecurityType::Wpa2,
            },
            fuchsia_async as fasync,
            test_doubles::FakeScanResultIterator,
        };

        #[fasync::run_until_stalled(test)]
        async fn caches_single_bss_with_just_bss_data() {
            let mut cache = RealBssCache::new();
            let result = cache
                .update(FakeScanResultIterator::new_single_step(vec![ScanResult {
                    id: None,
                    entries: Some(vec![WlanPolicyBss {
                        bssid: Some([0, 1, 2, 3, 4, 5]),
                        rssi: None,
                        frequency: None,
                        timestamp_nanos: None,
                        ..Default::default()
                    }]),
                    compatibility: None,
                    ..Default::default()
                }]))
                .await;
            assert_eq!(result, Ok(()));
            assert_eq!(
                cache.iter().next(),
                Some((&[0, 1, 2, 3, 4, 5], &Bss { rssi: None, frequency: None }))
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn caches_single_bss_with_all_data() {
            let mut cache = RealBssCache::new();
            let result = cache
                .update(FakeScanResultIterator::new_single_step(vec![ScanResult {
                    id: Some(NetworkIdentifier { ssid: vec![b'a'], type_: Wpa2 }),
                    entries: Some(vec![WlanPolicyBss {
                        bssid: Some([0, 1, 2, 3, 4, 5]),
                        rssi: Some(-1),
                        frequency: Some(2412),
                        timestamp_nanos: Some(1),
                        ..Default::default()
                    }]),
                    compatibility: Some(Supported),
                    ..Default::default()
                }]))
                .await;
            assert_eq!(result, Ok(()));
            assert_eq!(
                cache.iter().next(),
                Some((&[0, 1, 2, 3, 4, 5], &Bss { rssi: Some(-1), frequency: Some(2412) }))
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn caches_multiple_bsses_from_single_network() {
            let mut cache = RealBssCache::new();
            let result = cache
                .update(FakeScanResultIterator::new_single_step(vec![ScanResult {
                    id: None,
                    entries: Some(vec![
                        WlanPolicyBss {
                            bssid: Some([0, 0, 0, 0, 0, 0]),
                            rssi: None,
                            frequency: None,
                            timestamp_nanos: None,
                            ..Default::default()
                        },
                        WlanPolicyBss {
                            bssid: Some([1, 1, 1, 1, 1, 1]),
                            rssi: None,
                            frequency: None,
                            timestamp_nanos: None,
                            ..Default::default()
                        },
                    ]),
                    compatibility: None,
                    ..Default::default()
                }]))
                .await;
            assert_eq!(result, Ok(()));

            let bsses: BTreeMap<&BssId, &Bss> = cache.iter().collect();
            assert_eq!(bsses.get(&[0, 0, 0, 0, 0, 0]), Some(&&Bss { rssi: None, frequency: None }));
            assert_eq!(bsses.get(&[1, 1, 1, 1, 1, 1]), Some(&&Bss { rssi: None, frequency: None }));
        }

        #[fasync::run_until_stalled(test)]
        async fn deduplicates_bsses_from_single_network() {
            let mut cache = RealBssCache::new();
            let result = cache
                .update(FakeScanResultIterator::new_single_step(vec![ScanResult {
                    id: None,
                    entries: Some(vec![
                        WlanPolicyBss {
                            bssid: Some([0, 1, 2, 3, 4, 5]),
                            rssi: Some(-1),
                            frequency: Some(2412),
                            timestamp_nanos: Some(1),
                            ..Default::default()
                        },
                        WlanPolicyBss {
                            bssid: Some([0, 1, 2, 3, 4, 5]),
                            rssi: Some(-2),
                            frequency: Some(2432),
                            timestamp_nanos: Some(2),
                            ..Default::default()
                        },
                    ]),
                    compatibility: None,
                    ..Default::default()
                }]))
                .await;
            assert_eq!(result, Ok(()));

            let mut bsses = cache.iter();
            assert_matches!(bsses.next(), Some((&[0, 1, 2, 3, 4, 5], _)));
            assert_eq!(bsses.next(), None);
        }

        #[fasync::run_until_stalled(test)]
        async fn caches_multiple_bsses_from_multiple_networks() {
            let mut cache = RealBssCache::new();
            let result = cache
                .update(FakeScanResultIterator::new_single_step(vec![
                    ScanResult {
                        id: None,
                        entries: Some(vec![WlanPolicyBss {
                            bssid: Some([0, 0, 0, 0, 0, 0]),
                            rssi: None,
                            frequency: None,
                            timestamp_nanos: None,
                            ..Default::default()
                        }]),
                        compatibility: None,
                        ..Default::default()
                    },
                    ScanResult {
                        id: None,
                        entries: Some(vec![WlanPolicyBss {
                            bssid: Some([1, 1, 1, 1, 1, 1]),
                            rssi: None,
                            frequency: None,
                            timestamp_nanos: None,
                            ..Default::default()
                        }]),
                        compatibility: None,
                        ..Default::default()
                    },
                ]))
                .await;
            assert_eq!(result, Ok(()));

            let bsses: BTreeMap<&BssId, &Bss> = cache.iter().collect();
            assert_eq!(bsses.get(&[0, 0, 0, 0, 0, 0]), Some(&&Bss { rssi: None, frequency: None }));
            assert_eq!(bsses.get(&[1, 1, 1, 1, 1, 1]), Some(&&Bss { rssi: None, frequency: None }));
        }

        #[fasync::run_until_stalled(test)]
        async fn deduplicates_bsses_from_multiple_networks() {
            let mut cache = RealBssCache::new();
            let result = cache
                .update(FakeScanResultIterator::new_single_step(vec![
                    ScanResult {
                        id: None,
                        entries: Some(vec![WlanPolicyBss {
                            bssid: Some([0, 1, 2, 3, 4, 5]),
                            rssi: Some(-1),
                            frequency: Some(2412),
                            timestamp_nanos: Some(1),
                            ..Default::default()
                        }]),
                        compatibility: None,
                        ..Default::default()
                    },
                    ScanResult {
                        id: None,
                        entries: Some(vec![WlanPolicyBss {
                            bssid: Some([0, 1, 2, 3, 4, 5]),
                            rssi: Some(-2),
                            frequency: Some(2432),
                            timestamp_nanos: Some(2),
                            ..Default::default()
                        }]),
                        compatibility: None,
                        ..Default::default()
                    },
                ]))
                .await;
            assert_eq!(result, Ok(()));

            let mut bsses = cache.iter();
            assert_matches!(bsses.next(), Some((&[0, 1, 2, 3, 4, 5], _)));
            assert_eq!(bsses.next(), None);
        }

        #[fasync::run_until_stalled(test)]
        async fn honors_max_bss_limit() {
            let mut cache = RealBssCache::new();
            let bsses: Vec<_> = (0..MAX_BSSES + 1)
                .map(|i| WlanPolicyBss {
                    bssid: Some(
                        BssId::try_from(&i.to_le_bytes()[0..BSS_ADDR_LEN_BYTES])
                            .expect("internal error"),
                    ),
                    rssi: None,
                    frequency: None,
                    timestamp_nanos: None,
                    ..Default::default()
                })
                .collect();
            let scan_results = vec![ScanResult {
                id: None,
                entries: Some(bsses),
                compatibility: None,
                ..Default::default()
            }];
            let _ = cache.update(FakeScanResultIterator::new_single_step(scan_results)).await;
            assert_eq!(cache.iter().count(), MAX_BSSES);
        }

        #[fasync::run_until_stalled(test)]
        async fn does_not_count_bad_bsses_toward_max_bss_limit() {
            let mut cache = RealBssCache::new();
            let bad_bss = std::iter::once(WlanPolicyBss {
                bssid: None,
                rssi: None,
                frequency: None,
                timestamp_nanos: None,
                ..Default::default()
            });
            let good_bsses = (0..MAX_BSSES).map(|i| WlanPolicyBss {
                bssid: Some(
                    BssId::try_from(&i.to_le_bytes()[0..BSS_ADDR_LEN_BYTES])
                        .expect("internal error"),
                ),
                rssi: None,
                frequency: None,
                timestamp_nanos: None,
                ..Default::default()
            });
            let bsses: Vec<_> = bad_bss.chain(good_bsses).collect();
            let scan_results = vec![ScanResult {
                id: None,
                entries: Some(bsses),
                compatibility: None,
                ..Default::default()
            }];
            let _ = cache.update(FakeScanResultIterator::new_single_step(scan_results)).await;
            assert_eq!(cache.iter().count(), MAX_BSSES);
        }

        #[fasync::run_until_stalled(test)]
        async fn does_not_count_duplicate_bsses_toward_max_bss_limit() {
            let mut cache = RealBssCache::new();
            let duplicate_bsses = vec![
                WlanPolicyBss {
                    bssid: Some([0, 0, 0, 0, 0, 0]),
                    rssi: None,
                    frequency: None,
                    timestamp_nanos: None,
                    ..Default::default()
                },
                WlanPolicyBss {
                    bssid: Some([0, 0, 0, 0, 0, 0]),
                    rssi: None,
                    frequency: None,
                    timestamp_nanos: None,
                    ..Default::default()
                },
            ];
            let unique_bsses = (1..MAX_BSSES).map(|i| WlanPolicyBss {
                bssid: Some(
                    BssId::try_from(&i.to_le_bytes()[0..BSS_ADDR_LEN_BYTES])
                        .expect("internal error"),
                ),
                rssi: None,
                frequency: None,
                timestamp_nanos: None,
                ..Default::default()
            });
            let bsses: Vec<_> = duplicate_bsses.into_iter().chain(unique_bsses).collect();
            let scan_results = vec![ScanResult {
                id: None,
                entries: Some(bsses),
                compatibility: None,
                ..Default::default()
            }];
            let _ = cache.update(FakeScanResultIterator::new_single_step(scan_results)).await;
            assert_eq!(cache.iter().count(), MAX_BSSES);
        }
    }

    mod single_call_failure {
        use {
            super::super::*,
            fuchsia_async as fasync,
            test_doubles::{FakeScanResultIterator, StubScanResultIterator},
        };

        #[fasync::run_until_stalled(test)]
        async fn returns_ipc_error_on_fidl_error() {
            assert_eq!(
                RealBssCache::new()
                    .update(StubScanResultIterator::new(|| Err(fidl::Error::InvalidHeader)))
                    .await,
                Err(UpdateError::Ipc)
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn returns_service_error_on_general_scan_error() {
            assert_eq!(
                RealBssCache::new()
                    .update(StubScanResultIterator::new(|| Ok(Err(ScanErrorCode::GeneralError))))
                    .await,
                Err(UpdateError::Service)
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn returns_no_bsses_error_on_empty_scan_results() {
            assert_eq!(
                RealBssCache::new().update(FakeScanResultIterator::new_single_step(vec![])).await,
                Err(UpdateError::NoBsses)
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn returns_no_bsses_error_on_network_without_entries_vector() {
            assert_eq!(
                RealBssCache::new()
                    .update(FakeScanResultIterator::new_single_step(vec![ScanResult {
                        id: None,
                        entries: None,
                        compatibility: None,
                        ..Default::default()
                    }]))
                    .await,
                Err(UpdateError::NoBsses)
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn returns_no_bsses_error_on_network_with_empty_entries_vector() {
            assert_eq!(
                RealBssCache::new()
                    .update(FakeScanResultIterator::new_single_step(vec![ScanResult {
                        id: None,
                        entries: Some(Vec::new()),
                        compatibility: None,
                        ..Default::default()
                    }]))
                    .await,
                Err(UpdateError::NoBsses)
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn returns_no_bss_ids_error_on_bss_without_bssid() {
            assert_eq!(
                RealBssCache::new()
                    .update(FakeScanResultIterator::new_single_step(vec![ScanResult {
                        id: None,
                        entries: Some(vec![WlanPolicyBss {
                            bssid: None,
                            rssi: Some(-1),
                            frequency: Some(2414),
                            timestamp_nanos: Some(1),
                            ..Default::default()
                        }]),
                        compatibility: None,
                        ..Default::default()
                    }]))
                    .await,
                Err(UpdateError::NoBssIds),
            );
        }
    }

    mod multiple_calls {
        use {
            super::super::*,
            fidl_fuchsia_wlan_policy::{
                Compatibility::Supported, NetworkIdentifier, SecurityType::Wpa2,
            },
            fuchsia_async as fasync,
            test_doubles::FakeScanResultIterator,
        };

        #[fasync::run_until_stalled(test)]
        async fn is_non_empty_after_new_non_empty_data() {
            let mut cache = RealBssCache::new();
            let _ = cache
                .update(FakeScanResultIterator::new_single_step(vec![ScanResult {
                    id: None,
                    entries: Some(vec![WlanPolicyBss {
                        bssid: Some([0, 0, 0, 0, 0, 0]),
                        rssi: None,
                        frequency: None,
                        timestamp_nanos: None,
                        ..Default::default()
                    }]),
                    compatibility: None,
                    ..Default::default()
                }]))
                .await;
            let _ = cache
                .update(FakeScanResultIterator::new_single_step(vec![ScanResult {
                    id: None,
                    entries: Some(vec![WlanPolicyBss {
                        bssid: Some([1, 1, 1, 1, 1, 1]),
                        rssi: None,
                        frequency: None,
                        timestamp_nanos: None,
                        ..Default::default()
                    }]),
                    compatibility: None,
                    ..Default::default()
                }]))
                .await;

            // Note: we refrain from making a stronger assertion
            // (e.g. that only the new BSS is retained), to avoid
            // needing to revise this test if we change the caching
            // policy in the future.
            //
            // Said differently, we believe that
            // a) the assertion below will hold true under any reasonable
            //    caching policy, and
            // b) there is no pressing need to validate the more specific
            //    behavior of the current caching policy.
            assert!(cache.iter().next().is_some());
        }

        #[fasync::run_until_stalled(test)]
        async fn is_non_empty_after_new_empty_data() {
            let mut cache = RealBssCache::new();
            let _ = cache
                .update(FakeScanResultIterator::new_single_step(vec![ScanResult {
                    id: None,
                    entries: Some(vec![WlanPolicyBss {
                        bssid: Some([0, 0, 0, 0, 0, 0]),
                        rssi: None,
                        frequency: None,
                        timestamp_nanos: None,
                        ..Default::default()
                    }]),
                    compatibility: None,
                    ..Default::default()
                }]))
                .await;

            // Note: we populate everything except `entries.bssid`, to
            // ensure that the implementation doesn't short-circuit
            // due to any other data being missing.
            let _ = cache
                .update(FakeScanResultIterator::new_single_step(vec![ScanResult {
                    id: Some(NetworkIdentifier { ssid: vec![b'a'], type_: Wpa2 }),
                    entries: Some(vec![WlanPolicyBss {
                        bssid: None,
                        rssi: Some(-1),
                        frequency: Some(2412),
                        timestamp_nanos: Some(1),
                        ..Default::default()
                    }]),
                    compatibility: Some(Supported),
                    ..Default::default()
                }]))
                .await;

            // Note: for now, we make the assumption that having
            // _some_ location information is better than having none,
            // even if the data we have is old. If this changes, we
            // should remove this test.
            assert!(cache.iter().next().is_some());
        }
    }

    mod multi_step_iteration {
        use {super::super::*, fuchsia_async as fasync, test_doubles::FakeScanResultIterator};

        #[fasync::run_until_stalled(test)]
        async fn reads_all_scan_results() {
            let mut cache = RealBssCache::new();
            let result = cache
                .update(FakeScanResultIterator::new_multi_step(vec![
                    vec![ScanResult {
                        id: None,
                        entries: Some(vec![WlanPolicyBss {
                            bssid: Some([0, 0, 0, 0, 0, 0]),
                            rssi: None,
                            frequency: None,
                            timestamp_nanos: None,
                            ..Default::default()
                        }]),
                        compatibility: None,
                        ..Default::default()
                    }],
                    vec![ScanResult {
                        id: None,
                        entries: Some(vec![WlanPolicyBss {
                            bssid: Some([1, 1, 1, 1, 1, 1]),
                            rssi: None,
                            frequency: None,
                            timestamp_nanos: None,
                            ..Default::default()
                        }]),
                        compatibility: None,
                        ..Default::default()
                    }],
                ]))
                .await;
            assert_eq!(result, Ok(()));
            assert_eq!(2, cache.iter().count());
        }

        #[fasync::run_until_stalled(test)]
        async fn finds_later_bsses_even_if_first_iteration_yields_no_bsses() {
            let mut cache = RealBssCache::new();
            let result = cache
                .update(FakeScanResultIterator::new_multi_step(vec![
                    vec![ScanResult {
                        id: None,
                        entries: None,
                        compatibility: None,
                        ..Default::default()
                    }],
                    vec![ScanResult {
                        id: None,
                        entries: Some(vec![WlanPolicyBss {
                            bssid: Some([0, 0, 0, 0, 0, 0]),
                            rssi: None,
                            frequency: None,
                            timestamp_nanos: None,
                            ..Default::default()
                        }]),
                        compatibility: None,
                        ..Default::default()
                    }],
                ]))
                .await;
            assert_eq!(result, Ok(()));
            assert_eq!(1, cache.iter().count());
        }

        #[fasync::run_until_stalled(test)]
        async fn finds_later_bss_ids_even_if_first_iteration_yields_no_bss_ids() {
            let mut cache = RealBssCache::new();
            let result = cache
                .update(FakeScanResultIterator::new_multi_step(vec![
                    vec![ScanResult {
                        id: None,
                        entries: Some(vec![WlanPolicyBss {
                            bssid: None,
                            rssi: None,
                            frequency: None,
                            timestamp_nanos: None,
                            ..Default::default()
                        }]),

                        compatibility: None,
                        ..Default::default()
                    }],
                    vec![ScanResult {
                        id: None,
                        entries: Some(vec![WlanPolicyBss {
                            bssid: Some([0, 0, 0, 0, 0, 0]),
                            rssi: None,
                            frequency: None,
                            timestamp_nanos: None,
                            ..Default::default()
                        }]),
                        compatibility: None,
                        ..Default::default()
                    }],
                ]))
                .await;
            assert_eq!(result, Ok(()));
            assert_eq!(1, cache.iter().count());
        }
    }

    mod ipc_interactions {
        use {
            super::super::*,
            fidl_fuchsia_wlan_policy::{NetworkIdentifier, SecurityType::Wpa2},
            fuchsia_async as fasync,
            test_doubles::{RawStubScanResultIterator, StubScanResultIterator},
        };

        #[fasync::run_until_stalled(test)]
        async fn stops_sending_ipcs_when_get_next_yields_fidl_error() {
            let mut cache = RealBssCache::new();
            let mut scan_results = vec![Err(fidl::Error::InvalidHeader)].into_iter();
            let _ = cache
                .update(StubScanResultIterator::new(|| {
                    scan_results.next().expect("already consumed all `scan_results`")
                }))
                .await;
        }

        #[fasync::run_until_stalled(test)]
        async fn stops_sending_ipcs_when_get_next_yields_scan_error() {
            let mut cache = RealBssCache::new();
            let mut scan_results = vec![Ok(Err(ScanErrorCode::GeneralError))].into_iter();
            let _ = cache
                .update(StubScanResultIterator::new(|| {
                    scan_results.next().expect("already consumed all `scan_results`")
                }))
                .await;
        }

        #[fasync::run_until_stalled(test)]
        async fn stops_sending_ipcs_when_get_next_yields_empty_vec() {
            let mut cache = RealBssCache::new();
            let mut scan_results = vec![Ok(Ok(vec![]))].into_iter();
            let _ = cache
                .update(StubScanResultIterator::new(|| {
                    scan_results.next().expect("already consumed all `scan_results`")
                }))
                .await;
        }

        #[fasync::run_until_stalled(test)]
        async fn drives_pending_ipc_to_completion() {
            let mut cache = RealBssCache::new();
            let mut poll_results = vec![Poll::Pending, Poll::Ready(Ok(Ok(vec![])))].into_iter();
            let mut futures = vec![futures::future::poll_fn(|cx| {
                let res = poll_results.next().expect("already consumed all `poll_results`");
                cx.waker().wake_by_ref();
                res
            })]
            .into_iter();
            let _ = cache
                .update(RawStubScanResultIterator::new(|| {
                    futures.next().expect("already consumed all `futures`")
                }))
                .await;
        }

        #[fasync::run_until_stalled(test)]
        async fn honors_max_ipc_limit() {
            let mut cache = RealBssCache::new();
            let mut scan_results = (0..MAX_IPCS)
                .map(|i| {
                    Ok(Ok(vec![ScanResult {
                        id: Some(NetworkIdentifier { ssid: i.to_le_bytes().to_vec(), type_: Wpa2 }),
                        entries: Some(vec![WlanPolicyBss {
                            bssid: Some(
                                BssId::try_from(&i.to_le_bytes()[0..BSS_ADDR_LEN_BYTES])
                                    .expect("internal error"),
                            ),
                            rssi: None,
                            frequency: None,
                            timestamp_nanos: None,
                            ..Default::default()
                        }]),
                        compatibility: None,
                        ..Default::default()
                    }]))
                })
                .collect::<Vec<_>>()
                .into_iter();
            let _ = cache
                .update(StubScanResultIterator::new(|| {
                    scan_results.next().expect("already consumed all `scan_results`")
                }))
                .await;
        }
    }
}

#[cfg(test)]
mod test_doubles {
    use {
        super::*,
        futures::future::{ready, Ready},
        std::sync::RwLock,
    };

    // Test double that returns scan results from initially provided data.
    // After exhausting the initial data, perpetually returns an empty `Vec`.
    // Useful for testing success cases.
    pub(super) struct FakeScanResultIterator {
        // Why do we need an RwLock here?
        //
        // 1) We need to return a `Vec<ScanResult>`
        // 2) `ScanResult` is not `Copy` or `Clone`
        // 3) Given 1 and 2, `get_next()` needs to move data
        //
        // Given just the constraints above, we might consider `Cell` or `RefCell`. However,
        // `ScanResultIteratorProxyInterface` is `Sync`, while `Cell` and `RefCell` are not.
        scan_results: RwLock<Vec<Vec<ScanResult>>>,
    }

    // Test double that invokes a function which yields a `GetNextResponse`.
    // Useful for testing error handling.
    pub(super) struct StubScanResultIterator<F: FnMut() -> GetNextResponse>(RwLock<F>);

    // Test double that invokes a function with yields a `GetNextResponse` `Future`.
    // Useful for testing interaction with asynchronous IPCs.
    pub(super) struct RawStubScanResultIterator<F, R>(RwLock<F>)
    where
        F: FnMut() -> R,
        R: Future<Output = GetNextResponse>;

    impl FakeScanResultIterator {
        // Returns an iterator which yields `scan_results` all at once.
        pub(super) fn new_single_step(scan_results: Vec<ScanResult>) -> Self {
            Self::new_multi_step(vec![scan_results])
        }

        // Returns an iterator which yields one element of `scan_results` at a time.
        // Note, however, that each element is _itself_ a `Vec<ScanResult>`.
        pub(super) fn new_multi_step(scan_results: Vec<Vec<ScanResult>>) -> Self {
            Self { scan_results: RwLock::new(scan_results) }
        }
    }

    impl ScanResultIteratorProxyInterface for FakeScanResultIterator {
        type GetNextResponseFut = Ready<GetNextResponse>;

        fn get_next(&self) -> Self::GetNextResponseFut {
            let mut scan_results = self.scan_results.write().expect("internal error");
            ready(Ok(Ok(if scan_results.is_empty() { Vec::new() } else { scan_results.remove(0) })))
        }
    }

    impl<F: FnMut() -> GetNextResponse + Send + Sync> StubScanResultIterator<F> {
        pub(super) fn new(get_next: F) -> Self {
            Self(RwLock::new(get_next))
        }
    }

    impl<F: FnMut() -> GetNextResponse + Send + Sync> ScanResultIteratorProxyInterface
        for StubScanResultIterator<F>
    {
        type GetNextResponseFut = Ready<GetNextResponse>;

        fn get_next(&self) -> Self::GetNextResponseFut {
            // Note: the `&mut *` here is due to https://github.com/rust-lang/rust/issues/65489
            let response_func = &mut *self.0.write().expect("internal error");
            ready(response_func())
        }
    }

    impl<F, R> RawStubScanResultIterator<F, R>
    where
        F: FnMut() -> R + Send + Sync,
        R: Future<Output = GetNextResponse> + Send,
    {
        pub(super) fn new(get_next: F) -> Self {
            Self(RwLock::new(get_next))
        }
    }

    impl<F, R> ScanResultIteratorProxyInterface for RawStubScanResultIterator<F, R>
    where
        F: FnMut() -> R + Send + Sync,
        R: Future<Output = GetNextResponse> + Send,
    {
        type GetNextResponseFut = R;

        fn get_next(&self) -> Self::GetNextResponseFut {
            // Note: the `&mut *` here is due to https://github.com/rust-lang/rust/issues/65489
            let response_func = &mut *self.0.write().expect("internal error");
            response_func()
        }
    }

    mod tests {
        mod fake_scan_result_iterator {
            use {super::super::*, fuchsia_async as fasync};

            #[fasync::run_until_stalled(test)]
            async fn single_step_yields_all_scan_results_at_once() {
                let iter = FakeScanResultIterator::new_single_step(vec![
                    ScanResult {
                        id: None,
                        entries: None,
                        compatibility: None,
                        ..Default::default()
                    },
                    ScanResult {
                        id: None,
                        entries: None,
                        compatibility: None,
                        ..Default::default()
                    },
                ]);
                assert_eq!(2, iter.get_next().await.unwrap().unwrap().len());
            }

            #[fasync::run_until_stalled(test)]
            async fn initially_empty_iterator_yields_empty_vec() {
                let iter = FakeScanResultIterator::new_single_step(Vec::new());
                assert_eq!(Vec::<ScanResult>::new(), iter.get_next().await.unwrap().unwrap());
            }

            #[fasync::run_until_stalled(test)]
            async fn emptied_iterator_yields_empty_vec() {
                let iter = FakeScanResultIterator::new_single_step(vec![ScanResult {
                    id: None,
                    entries: None,
                    compatibility: None,
                    ..Default::default()
                }]);
                let _ = iter.get_next().await.unwrap().unwrap();
                assert_eq!(Vec::<ScanResult>::new(), iter.get_next().await.unwrap().unwrap());
            }

            #[fasync::run_until_stalled(test)]
            async fn multi_step_yields_scan_results_iteratively() {
                let iter = FakeScanResultIterator::new_multi_step(vec![
                    vec![ScanResult {
                        id: None,
                        entries: None,
                        compatibility: None,
                        ..Default::default()
                    }],
                    vec![ScanResult {
                        id: None,
                        entries: None,
                        compatibility: None,
                        ..Default::default()
                    }],
                ]);
                assert_eq!(1, iter.get_next().await.unwrap().unwrap().len());
                assert_eq!(1, iter.get_next().await.unwrap().unwrap().len());
                assert_eq!(0, iter.get_next().await.unwrap().unwrap().len());
            }
        }
    }
}