emergency_lib/
bss_resolver.rs

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
// 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 crate::bss_cache::{Bss, BssId};
use async_trait::async_trait;
use fidl::{Error as FidlError, Socket as FidlSocket};
use fidl_fuchsia_location_position::{Position, PositionExtras};
use fidl_fuchsia_mem::Buffer as MemBuffer;
use fidl_fuchsia_net_http::{
    Body as HttpBody, LoaderProxyInterface, Request as HttpRequest, Response as HttpResponse,
};

use futures::io::AsyncReadExt;
use futures::Future;
use itertools::Itertools;
use serde_json::json;
use serde_json::map::Map as JsonMap;
use serde_json::value::Value as JsonValue;
use static_assertions::assert_eq_size_val;
use std::borrow::Borrow;

// Geographic constants.
const LATITUDE_RANGE: std::ops::RangeInclusive<f64> = -90.0..=90.0;
const LONGITUDE_RANGE: std::ops::RangeInclusive<f64> = -180.0..=180.0;

// Google Maps constants.
const SERVICE_URL: &'static str = "https://www.googleapis.com/geolocation/v1/geolocate";
const MAC_ADDR_KEY: &'static str = "macAddress";
const AP_LIST_KEY: &'static str = "wifiAccessPoints";
const RSSI_KEY: &'static str = "signalStrength";
const LOCATION_KEY: &'static str = "location";
const LATITUDE_KEY: &'static str = "lat";
const LONGITUDE_KEY: &'static str = "lng";
const ACCURACY_KEY: &'static str = "accuracy";

// HTTP constants.
const HTTP_METHOD_POST: &'static str = "POST";

#[async_trait(?Send)]
pub trait BssResolver {
    /// Resolves WLAN BSS (base-station) meta-data to a `Position`.
    async fn resolve<'a, I, T, U>(&self, bsses: I) -> Result<Position, ResolverError>
    where
        I: IntoIterator,
        I::Item: Borrow<(T, U)>,
        T: Borrow<BssId>,
        U: Borrow<Bss>;
}

/// A service for resolving WLAN BSS (base-station) meta-data to `Position`s.
pub struct RealBssResolver<L: LoaderProxyInterface> {
    http_loader: L,
    api_key: String,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ResolverError {
    NoBsses,
    Internal,
    Lookup,
}

impl<L: LoaderProxyInterface> RealBssResolver<L> {
    pub fn new(http_loader: L, api_key: impl Into<String>) -> Self {
        Self { http_loader, api_key: api_key.into() }
    }
}

#[async_trait(?Send)]
impl<L: LoaderProxyInterface> BssResolver for RealBssResolver<L> {
    async fn resolve<'a, I, T, U>(&self, bsses: I) -> Result<Position, ResolverError>
    where
        I: IntoIterator,
        I::Item: Borrow<(T, U)>,
        T: Borrow<BssId>,
        U: Borrow<Bss>,
    {
        let mut bsses = bsses.into_iter().peekable();
        if bsses.peek().is_none() {
            return Err(ResolverError::NoBsses);
        }
        parse_response(send_query(bsses, &self.api_key, &self.http_loader)?.await).await
    }
}

fn send_query<'a, I, T, U>(
    bsses: I,
    api_key: impl AsRef<str>,
    http_loader: &impl LoaderProxyInterface,
) -> Result<impl Future<Output = Result<HttpResponse, FidlError>>, ResolverError>
where
    I: Iterator,
    I::Item: Borrow<(T, U)>,
    T: Borrow<BssId>,
    U: Borrow<Bss>,
{
    let api_key = api_key.as_ref();
    let request_body = serialize_bsses(bsses);
    let request_size = {
        let body_size = request_body.len();
        assert_eq_size_val!(body_size, 0u64);
        u64::try_from(body_size).expect("failed to convert usize to u64")
    };
    let request_vmo = zx::Vmo::create(request_size).map_err(|_| ResolverError::Internal)?;
    let _ = request_vmo.write(&request_body.as_bytes(), 0).map_err(|_| ResolverError::Internal)?;
    Ok(http_loader.fetch(HttpRequest {
        method: Some(HTTP_METHOD_POST.to_owned()),
        url: Some(format!("{}?key={}", SERVICE_URL, api_key)),
        headers: None,
        body: Some(HttpBody::Buffer(MemBuffer { vmo: request_vmo, size: request_size })),
        deadline: None,
        ..Default::default()
    }))
}

async fn parse_response(
    response: Result<HttpResponse, FidlError>,
) -> Result<Position, ResolverError> {
    let response: HttpResponse = response.map_err(|_fidl_error| ResolverError::Internal)?;
    let response: FidlSocket = response.body.ok_or(ResolverError::Lookup)?;
    let response: String = read_socket(response).await.ok_or(ResolverError::Lookup)?;
    json_to_position(serde_json::from_str(&response).map_err(|_| ResolverError::Lookup)?)
}

fn serialize_bsses<'a, I, T, U>(bsses: I) -> String
where
    I: Iterator,
    I::Item: Borrow<(T, U)>,
    T: Borrow<BssId>,
    U: Borrow<Bss>,
{
    let bsses = bsses
        .map(|item| bss_to_json(item.borrow().0.borrow(), item.borrow().1.borrow()))
        .collect::<Vec<_>>();
    json!({ AP_LIST_KEY: bsses }).to_string()
}

fn bss_to_json(bss_id: impl Borrow<BssId>, bss: impl Borrow<Bss>) -> JsonValue {
    let mut json = JsonMap::new();
    json.insert(
        MAC_ADDR_KEY.to_owned(),
        json!(bss_id.borrow().iter().map(|bss_byte| format!("{:02x}", bss_byte)).join(":")),
    );
    if let Some(rssi) = bss.borrow().rssi {
        json.insert(RSSI_KEY.to_owned(), json!(rssi));
    };
    JsonValue::from(json)
}

async fn read_socket(socket: zx::Socket) -> Option<String> {
    let mut buf = Vec::new();
    let mut socket = fuchsia_async::Socket::from_socket(socket);
    match socket.read_to_end(&mut buf).await {
        Ok(_num_bytes_read) => String::from_utf8(buf).ok(),
        Err(_) => None,
    }
}

fn json_to_position(response: JsonValue) -> Result<Position, ResolverError> {
    let latitude = response[LOCATION_KEY][LATITUDE_KEY].as_f64().ok_or(ResolverError::Lookup)?;
    if !LATITUDE_RANGE.contains(&latitude) {
        return Err(ResolverError::Lookup);
    }

    let longitude = response[LOCATION_KEY][LONGITUDE_KEY].as_f64().ok_or(ResolverError::Lookup)?;
    if !LONGITUDE_RANGE.contains(&longitude) {
        return Err(ResolverError::Lookup);
    }

    let extras = match response[ACCURACY_KEY].as_f64() {
        Some(accuracy) => PositionExtras {
            accuracy_meters: Some(accuracy),
            altitude_meters: None,
            ..Default::default()
        },
        None => {
            PositionExtras { accuracy_meters: None, altitude_meters: None, ..Default::default() }
        }
    };

    Ok(Position { latitude, longitude, extras })
}

#[cfg(test)]
mod tests {
    mod request_generation {
        use super::super::test_doubles::HttpRequestValidator;
        use super::super::*;
        use fuchsia_async as fasync;

        #[fasync::run_until_stalled(test)]
        async fn request_uses_post_method() {
            let bsses = vec![([0, 0, 0, 0, 0, 0], Bss { rssi: Some(-30), frequency: Some(2412) })];
            let mut request_method = None;
            let http_loader = HttpRequestValidator::new(|request| {
                request_method = Some(request.method.expect("request had no method"));
            });
            let _ = RealBssResolver::new(http_loader, "fake_key").resolve(bsses).await;
            assert_eq!(request_method.expect("request was never issued"), "POST".to_owned());
        }

        #[fasync::run_until_stalled(test)]
        async fn request_url_is_correct() {
            let bsses = vec![([0, 0, 0, 0, 0, 0], Bss { rssi: Some(-30), frequency: Some(2412) })];
            let mut request_url = None;
            let http_loader = HttpRequestValidator::new(|request| {
                request_url = Some(request.url.expect("request had no url"));
            });
            let _ = RealBssResolver::new(http_loader, "fake_key").resolve(bsses).await;
            assert_eq!(
                request_url.expect("request was never issued"),
                "https://www.googleapis.com/geolocation/v1/geolocate?key=fake_key".to_owned()
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn request_body_contains_all_bsses() {
            let bsses = vec![
                ([0, 0, 0, 0, 0, 0], Bss { rssi: None, frequency: None }),
                ([1, 1, 1, 1, 1, 1], Bss { rssi: None, frequency: None }),
            ];
            let mut request_body = None;
            let http_loader = HttpRequestValidator::new(|request| {
                request_body = Some(request.body.expect("request had no body"));
            });
            let _ = RealBssResolver::new(http_loader, "fake_key").resolve(bsses).await;
            assert_eq!(
                strip_whitespace(read_request_body(
                    request_body.expect("request was never issued")
                )),
                strip_whitespace(
                    r#"{
                    "wifiAccessPoints":[
                        {"macAddress":"00:00:00:00:00:00"},
                        {"macAddress":"01:01:01:01:01:01"}
                    ]
                }"#
                )
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn request_body_includes_rssi() {
            let bsses = vec![([0, 0, 0, 0, 0, 0], Bss { rssi: Some(-30), frequency: None })];
            let mut request_body = None;
            let http_loader = HttpRequestValidator::new(|request| {
                request_body = Some(request.body.expect("request had no body"));
            });
            let _ = RealBssResolver::new(http_loader, "fake_key").resolve(bsses).await;
            assert_eq!(
                strip_whitespace(read_request_body(
                    request_body.expect("request was never issued")
                )),
                strip_whitespace(
                    r#"{
                    "wifiAccessPoints":[
                        {"macAddress":"00:00:00:00:00:00",
                         "signalStrength": -30}
                    ]
                }"#
                )
            )
        }

        #[fasync::run_until_stalled(test)]
        async fn skips_api_query_when_bsses_is_empty() {
            let http_loader =
                HttpRequestValidator::new(|_request| panic!("should not issue API query"));
            let _ = RealBssResolver::new(http_loader, "fake_key")
                .resolve(std::iter::empty::<(BssId, Bss)>())
                .await;
        }

        #[fasync::run_until_stalled(test)]
        async fn returns_no_bsses_error_when_bsses_is_empty() {
            let http_loader = HttpRequestValidator::new(|_| ());
            assert_eq!(
                RealBssResolver::new(http_loader, "fake_key")
                    .resolve(std::iter::empty::<(BssId, Bss)>())
                    .await,
                Err(ResolverError::NoBsses)
            );
        }

        fn read_request_body(body: HttpBody) -> String {
            match body {
                HttpBody::Buffer(shared_buf) => {
                    let mut local_buf = vec![
                        0_u8;
                        usize::try_from(shared_buf.size).expect(
                            "internal error: failed to convert u64 to usize"
                        )
                    ];
                    shared_buf
                        .vmo
                        .read(&mut local_buf, 0)
                        .expect("internal error: failed to read from VMO");
                    String::from_utf8(local_buf)
                        .expect("internal error: failed to convert buffer to UTF-8")
                }
                HttpBody::Stream(_) => panic!("internal error: stream bodies not supported"),
            }
        }

        fn strip_whitespace<S: AsRef<str>>(input: S) -> String {
            input.as_ref().replace("\n", "").replace(" ", "")
        }
    }

    mod response_error_handling {
        use super::super::test_doubles::{HttpByteResponder, HttpFidlResponder};
        use super::super::*;
        use fuchsia_async as fasync;

        #[fasync::run_until_stalled(test)]
        async fn returns_internal_error_on_fidl_error() {
            let bsses = vec![([0, 0, 0, 0, 0, 0], Bss { rssi: None, frequency: None })];
            let http_loader = HttpFidlResponder::new(|| Err(FidlError::InvalidHeader));
            assert_eq!(
                RealBssResolver::new(http_loader, "fake_key").resolve(bsses).await,
                Err(ResolverError::Internal)
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn returns_lookup_error_when_response_has_no_body() {
            let bsses = vec![([0, 0, 0, 0, 0, 0], Bss { rssi: None, frequency: None })];
            let http_loader = HttpFidlResponder::new(|| {
                Ok(HttpResponse {
                    error: None,
                    body: None,
                    final_url: None,
                    status_code: None,
                    status_line: None,
                    headers: None,
                    redirect: None,
                    ..Default::default()
                })
            });
            assert_eq!(
                RealBssResolver::new(http_loader, "fake_key").resolve(bsses).await,
                Err(ResolverError::Lookup)
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn returns_lookup_error_when_body_is_unreadable() {
            let bsses = vec![([0, 0, 0, 0, 0, 0], Bss { rssi: None, frequency: None })];
            let http_loader = HttpFidlResponder::new(|| {
                Ok(HttpResponse {
                    error: None,
                    body: Some(zx::Socket::from(zx::Handle::invalid())),
                    final_url: None,
                    status_code: None,
                    status_line: None,
                    headers: None,
                    redirect: None,
                    ..Default::default()
                })
            });
            assert_eq!(
                RealBssResolver::new(http_loader, "fake_key").resolve(bsses).await,
                Err(ResolverError::Lookup)
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn returns_lookup_error_when_body_is_not_valid_json() {
            let bsses = vec![([0, 0, 0, 0, 0, 0], Bss { rssi: None, frequency: None })];
            let http_loader = HttpByteResponder::new(b"hello world".to_vec());
            assert_eq!(
                RealBssResolver::new(http_loader, "fake_key").resolve(bsses).await,
                Err(ResolverError::Lookup)
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn returns_lookup_error_when_body_is_not_a_dictionary() {
            let bsses = vec![([0, 0, 0, 0, 0, 0], Bss { rssi: None, frequency: None })];
            let http_loader = HttpByteResponder::new(b"42".to_vec());
            assert_eq!(
                RealBssResolver::new(http_loader, "fake_key").resolve(bsses).await,
                Err(ResolverError::Lookup)
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn returns_lookup_error_when_body_is_missing_location() {
            let bsses = vec![([0, 0, 0, 0, 0, 0], Bss { rssi: None, frequency: None })];
            let http_loader = HttpByteResponder::new(b"{}".to_vec());
            assert_eq!(
                RealBssResolver::new(http_loader, "fake_key").resolve(bsses).await,
                Err(ResolverError::Lookup)
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn returns_lookup_error_when_body_is_missing_latitude() {
            let bsses = vec![([0, 0, 0, 0, 0, 0], Bss { rssi: None, frequency: None })];
            let http_loader = HttpByteResponder::new(
                br#"{
                        "location": {
                          "lng": -0.1
                        },
                        "accuracy": 1200.4
                }"#
                .to_vec(),
            );
            assert_eq!(
                RealBssResolver::new(http_loader, "fake_key").resolve(bsses).await,
                Err(ResolverError::Lookup)
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn returns_lookup_error_when_body_is_missing_longitude() {
            let bsses = vec![([0, 0, 0, 0, 0, 0], Bss { rssi: None, frequency: None })];
            let http_loader = HttpByteResponder::new(
                br#"{
                        "location": {
                            "lat": 51.0,
                        },
                        "accuracy": 1200.4
                }"#
                .to_vec(),
            );
            assert_eq!(
                RealBssResolver::new(http_loader, "fake_key").resolve(bsses).await,
                Err(ResolverError::Lookup)
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn returns_lookup_error_when_latitude_is_too_high() {
            let bsses = vec![([0, 0, 0, 0, 0, 0], Bss { rssi: None, frequency: None })];
            let http_loader = HttpByteResponder::new(
                br#"{
                        "location": {
                            "lat": 90.1,
                            "lng": 0.0
                        }
                }"#
                .to_vec(),
            );
            assert_eq!(
                RealBssResolver::new(http_loader, "fake_key").resolve(bsses).await,
                Err(ResolverError::Lookup)
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn returns_lookup_error_when_latitude_is_too_low() {
            let bsses = vec![([0, 0, 0, 0, 0, 0], Bss { rssi: None, frequency: None })];
            let http_loader = HttpByteResponder::new(
                br#"{
                        "location": {
                            "lat": -90.1,
                            "lng": 0.0
                        }
                }"#
                .to_vec(),
            );
            assert_eq!(
                RealBssResolver::new(http_loader, "fake_key").resolve(bsses).await,
                Err(ResolverError::Lookup)
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn returns_lookup_error_when_longitude_is_too_high() {
            let bsses = vec![([0, 0, 0, 0, 0, 0], Bss { rssi: None, frequency: None })];
            let http_loader = HttpByteResponder::new(
                br#"{
                        "location": {
                            "lat": 0.0,
                            "lng": 180.1
                        }
                }"#
                .to_vec(),
            );
            assert_eq!(
                RealBssResolver::new(http_loader, "fake_key").resolve(bsses).await,
                Err(ResolverError::Lookup)
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn returns_lookup_error_when_longitude_is_too_low() {
            let bsses = vec![([0, 0, 0, 0, 0, 0], Bss { rssi: None, frequency: None })];
            let http_loader = HttpByteResponder::new(
                br#"{
                        "location": {
                            "lat": 0.0,
                            "lng": -180.1
                        }
                }"#
                .to_vec(),
            );
            assert_eq!(
                RealBssResolver::new(http_loader, "fake_key").resolve(bsses).await,
                Err(ResolverError::Lookup)
            );
        }
    }

    mod response_success_reporting {
        use super::super::test_doubles::HttpByteResponder;
        use super::super::*;
        use assert_matches::assert_matches;
        use fuchsia_async as fasync;

        #[fasync::run_until_stalled(test)]
        async fn returns_success_when_all_fields_are_present() {
            let bsses = vec![([0, 0, 0, 0, 0, 0], Bss { rssi: None, frequency: None })];
            let http_loader = HttpByteResponder::new(
                br#"{
                        "location": {
                            "lat": 51.0,
                            "lng": -0.1
                        },
                        "accuracy": 1200.4
                }"#
                .to_vec(),
            );
            assert_matches!(
                RealBssResolver::new(http_loader, "fake_key").resolve(bsses).await,
                Ok(_)
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn returns_success_when_all_fields_except_accuracy_are_present() {
            let bsses = vec![([0, 0, 0, 0, 0, 0], Bss { rssi: None, frequency: None })];
            let http_loader = HttpByteResponder::new(
                br#"{
                        "location": {
                            "lat": 51.0,
                            "lng": -0.1
                       }
                }"#
                .to_vec(),
            );
            assert_matches!(
                RealBssResolver::new(http_loader, "fake_key").resolve(bsses).await,
                Ok(_)
            );
        }
    }

    mod response_success_contents {
        use super::super::test_doubles::HttpByteResponder;
        use super::super::*;
        use assert_matches::assert_matches;
        use fuchsia_async as fasync;

        #[fasync::run_until_stalled(test)]
        async fn provides_precise_latitude() {
            let bsses = vec![([0, 0, 0, 0, 0, 0], Bss { rssi: None, frequency: None })];
            let http_loader = HttpByteResponder::new(
                br#"{
                        "location": {
                            "lat": 89.0,
                            "lng": 0
                       }
                }"#
                .to_vec(),
            );
            assert_matches!(
                RealBssResolver::new(http_loader, "fake_key")
                    .resolve(bsses)
                    .await.expect("position is none").latitude,
                // One degree of latitude is approximately 111 kilometers. By limiting rounding
                // error to 1/10,000,000 of a degree, we limit rounding error to
                // 111/10000 = 0.011 meters, or  1.1 cm.
                latitude if (88.999_999_9..89.000_000_1).contains(&latitude)
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn provides_precise_longitude() {
            let bsses = vec![([0, 0, 0, 0, 0, 0], Bss { rssi: None, frequency: None })];
            let http_loader = HttpByteResponder::new(
                br#"{
                        "location": {
                            "lat": 0,
                            "lng": 179.0
                       }
                }"#
                .to_vec(),
            );
            assert_matches!(
                RealBssResolver::new(http_loader, "fake_key")
                    .resolve(bsses)
                    .await.expect("no position").longitude,
                // At the equator, one degree of longitude is approximately 111 kilometers.
                // By limiting rounding error to 1/10,000,000 of a degree, we limit rounding error
                // to 111/10000 = 0.011 meters, or  1.1 cm.
                longitude if (178.999_999_9..179.099_999_9).contains(&longitude)
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn provides_precise_accuracy_when_present() {
            let bsses = vec![([0, 0, 0, 0, 0, 0], Bss { rssi: None, frequency: None })];
            let http_loader = HttpByteResponder::new(
                br#"{
                        "location": {
                            "lat": 51.0,
                            "lng": -0.1
                        },
                        "accuracy": 1200.4
                }"#
                .to_vec(),
            );
            assert_matches!(
                RealBssResolver::new(http_loader, "fake_key")
                    .resolve(bsses)
                    .await.expect("no position").extras.accuracy_meters.expect("accuracy is none"),
                accuracy if (1200.39..1200.41).contains(&accuracy)
            );
        }

        #[fasync::run_until_stalled(test)]
        async fn does_not_fabricate_accuracy() {
            let bsses = vec![([0, 0, 0, 0, 0, 0], Bss { rssi: None, frequency: None })];
            let http_loader = HttpByteResponder::new(
                br#"{
                        "location": {
                            "lat": 51.0,
                            "lng": -0.1
                        }
                }"#
                .to_vec(),
            );
            assert_eq!(
                RealBssResolver::new(http_loader, "fake_key")
                    .resolve(bsses)
                    .await
                    .expect("no position")
                    .extras
                    .accuracy_meters,
                None
            );
        }
    }
}

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

    const HTTP_OK: u32 = 200;

    type FetchResponse = Result<HttpResponse, FidlError>;

    // Test double that
    // 1) invokes `validate` to run test assertions, and
    // 2) returns HTTP_OK, assuming `validate` did not abort
    pub(super) struct HttpRequestValidator<F>
    where
        F: FnMut(HttpRequest) + Send + Sync,
    {
        // RwLock is needed because
        // a) we need interior mutability for F to be FnMut, and
        // b) we need to implement Send and Sync.
        validate: RwLock<F>,
    }

    // Test double that invokes a function which yields `FetchResponse`,
    // and returns that as a FIDL response. Useful for testing error
    // handling.
    pub(super) struct HttpFidlResponder<F>
    where
        F: Fn() -> FetchResponse + Send + Sync,
    {
        fetch: F,
    }

    // Test double that invokes a function which yields a `Vec<u8>`,
    // and returns the Vec as the HTTP response. Useful for testing
    // response parsing.
    pub(super) struct HttpByteResponder {
        response: Vec<u8>,
    }

    impl<F> HttpRequestValidator<F>
    where
        F: FnMut(HttpRequest) + Send + Sync,
    {
        pub(super) fn new(validate: F) -> Self {
            Self { validate: RwLock::new(validate) }
        }
    }

    impl<F> LoaderProxyInterface for HttpRequestValidator<F>
    where
        F: FnMut(HttpRequest) + Send + Sync,
    {
        type FetchResponseFut = Ready<Result<HttpResponse, FidlError>>;

        fn fetch(&self, request: HttpRequest) -> Self::FetchResponseFut {
            // Note: the `&mut *` here is due to https://github.com/rust-lang/rust/issues/65489
            let validate = &mut *self.validate.write().expect("internal error");
            let final_url = request.url.clone();
            validate(request);
            ready(Ok(HttpResponse {
                error: None,
                body: None,
                final_url,
                status_code: Some(HTTP_OK),
                status_line: None,
                headers: None,
                redirect: None,
                ..Default::default()
            }))
        }

        fn start(
            &self,
            _request: HttpRequest,
            _client: ClientEnd<LoaderClientMarker>,
        ) -> Result<(), FidlError> {
            panic!("internal error: this fake does not implement `start()`");
        }
    }

    impl<F> HttpFidlResponder<F>
    where
        F: Fn() -> FetchResponse + Send + Sync,
    {
        pub(super) fn new(fetch: F) -> Self {
            Self { fetch }
        }
    }

    impl<F> LoaderProxyInterface for HttpFidlResponder<F>
    where
        F: Fn() -> FetchResponse + Send + Sync,
    {
        type FetchResponseFut = Ready<FetchResponse>;

        fn fetch(&self, _request: HttpRequest) -> Self::FetchResponseFut {
            ready((self.fetch)())
        }

        fn start(
            &self,
            _request: HttpRequest,
            _client: ClientEnd<LoaderClientMarker>,
        ) -> Result<(), FidlError> {
            panic!("internal error: this stub does not implement `start()`");
        }
    }

    impl HttpByteResponder {
        pub(super) fn new(response: Vec<u8>) -> Self {
            Self { response }
        }
    }

    impl LoaderProxyInterface for HttpByteResponder {
        type FetchResponseFut = Ready<FetchResponse>;

        fn fetch(&self, _request: HttpRequest) -> Self::FetchResponseFut {
            let (local_socket, remote_socket) = zx::Socket::create_stream();
            local_socket.write(&self.response).expect("internal error");
            ready(Ok(HttpResponse {
                error: None,
                body: Some(remote_socket),
                final_url: None,
                status_code: Some(HTTP_OK),
                status_line: None,
                headers: None,
                redirect: None,
                ..Default::default()
            }))
        }

        fn start(
            &self,
            _request: HttpRequest,
            _client: ClientEnd<LoaderClientMarker>,
        ) -> Result<(), FidlError> {
            panic!("internal error: this stub does not implement `start()`");
        }
    }
}