Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / networklocation / wps_harvest / src / gsloc.rs
gsloc.rs491 lines · 19.1 KiBRaw
| 1 | //! Client for Apple's location service at `gs-loc.apple.com/clls/wloc`. |
| 2 | //! |
| 3 | //! Ported from the Kotlin client this app used to carry, recovered in |
| 4 | //! `reference/ApplePositioningService.kt.txt`; the wire schema is `reference/apple_wps.proto`. |
| 5 | //! |
| 6 | //! Apple does not return a device position. Given beacon identifiers it returns each beacon's |
| 7 | //! own coordinates and horizontal accuracy — which is precisely a beacon database lookup, and |
| 8 | //! why this makes a store builder rather than a positioning client. |
| 9 | //! |
| 10 | //! It also returns **more beacons than were asked for**: a query for one BSSID comes back with |
| 11 | //! a few hundred others in the same area. That is what makes a crawl possible at all, since |
| 12 | //! every response both answers the query and extends the frontier. |
| 13 | //! |
| 14 | //! ## This talks to somebody else's service |
| 15 | //! |
| 16 | //! Requests are serialized through one client and rate-limited by [`Client::min_interval`], |
| 17 | //! defaulting to something a person could plausibly generate. Errors back off rather than |
| 18 | //! retrying hard. Doing otherwise would be both rude and self-defeating. |
| 19 | |
| 20 | use std::time::{Duration, Instant}; |
| 21 | |
| 22 | use crate::proto::{self, Field}; |
| 23 | |
| 24 | const ENDPOINT: &str = "https://gs-loc.apple.com/clls/wloc"; |
| 25 | |
| 26 | /// Apple prefixes the response protobuf with a fixed-size header. |
| 27 | const RESPONSE_HEADER_BYTES: usize = 10; |
| 28 | |
| 29 | /// OS version string in the request envelope. Must be 10 bytes to match the hardcoded length. |
| 30 | const OS_VERSION: &str = "17.5.21F79"; |
| 31 | |
| 32 | /// Coordinates arrive as integer degrees x 1e8, which is exactly what the store keeps. |
| 33 | pub const COORD_SCALE: i64 = 100_000_000; |
| 34 | |
| 35 | /// What Apple returns for a beacon it does not know: -180 degrees, in both axes. |
| 36 | const NOT_FOUND_E8: i64 = -18_000_000_000; |
| 37 | |
| 38 | // Field numbers within `AppleWLoc`, from wtfps's `src/apple.proto`. |
| 39 | const F_WIFI_DEVICES: u32 = 2; |
| 40 | const F_CELL_RESPONSE: u32 = 22; |
| 41 | const F_CELL_REQUEST: u32 = 25; |
| 42 | |
| 43 | /// Altitude (Apple's `location.5`) meaning "not reported": Apple sends -1. |
| 44 | pub const ALT_NOT_REPORTED: i64 = -1; |
| 45 | |
| 46 | /// Clamp a source-reported altitude into the store's `i16` metres, mapping Apple's -1 |
| 47 | /// "not reported" to [`crate::record::ALT_UNKNOWN`]. Saturates rather than wrapping: nothing |
| 48 | /// real sits outside ±32 km, so saturation can only trigger on garbage, which then reads as |
| 49 | /// extreme rather than plausible. |
| 50 | pub fn clamp_altitude(metres: i64) -> i16 { |
| 51 | if metres == ALT_NOT_REPORTED { |
| 52 | i16::MIN |
| 53 | } else { |
| 54 | metres.clamp(i16::MIN as i64 + 1, i16::MAX as i64) as i16 |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /// A beacon Apple knows the location of. |
| 59 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 60 | pub struct WifiFix { |
| 61 | /// Lowercase `aa:bb:cc:dd:ee:ff`, as echoed back. |
| 62 | pub bssid: String, |
| 63 | /// Latitude in degrees x 1e8. |
| 64 | pub lat_e8: i64, |
| 65 | /// Longitude in degrees x 1e8. |
| 66 | pub lon_e8: i64, |
| 67 | /// Horizontal accuracy in metres. |
| 68 | pub accuracy_m: i32, |
| 69 | /// Altitude in metres, or [`crate::record::ALT_UNKNOWN`]. Present on essentially every |
| 70 | /// WiFi fix (Apple's `location.5`); cell responses have no altitude field at all. |
| 71 | pub altitude_m: i16, |
| 72 | } |
| 73 | |
| 74 | /// A cell tower Apple knows the location of. |
| 75 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 76 | pub struct CellFix { |
| 77 | /// Mobile country code. |
| 78 | pub mcc: i32, |
| 79 | /// Mobile network code. |
| 80 | pub mnc: i32, |
| 81 | /// Cell id as Apple reports it. |
| 82 | pub cell_id: i64, |
| 83 | /// TAC or LAC. |
| 84 | pub area: i32, |
| 85 | /// Latitude in degrees x 1e8. |
| 86 | pub lat_e8: i64, |
| 87 | /// Longitude in degrees x 1e8. |
| 88 | pub lon_e8: i64, |
| 89 | /// Horizontal accuracy in metres. |
| 90 | pub accuracy_m: i32, |
| 91 | } |
| 92 | |
| 93 | /// What went wrong with a request. Callers distinguish these because they back off |
| 94 | /// differently: a transport hiccup is worth retrying, a refusal is not. |
| 95 | #[derive(Debug)] |
| 96 | pub enum Error { |
| 97 | /// The request never completed. |
| 98 | Transport(String), |
| 99 | /// A non-200 response. |
| 100 | Status(u16), |
| 101 | /// A 200 that did not contain a parseable body. |
| 102 | Malformed(&'static str), |
| 103 | } |
| 104 | |
| 105 | impl std::fmt::Display for Error { |
| 106 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 107 | match self { |
| 108 | Error::Transport(e) => write!(f, "transport: {e}"), |
| 109 | Error::Status(c) => write!(f, "HTTP {c}"), |
| 110 | Error::Malformed(m) => write!(f, "malformed response: {m}"), |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | impl std::error::Error for Error {} |
| 116 | |
| 117 | /// A rate-limited gs-loc client. |
| 118 | pub struct Client { |
| 119 | agent: ureq::Agent, |
| 120 | /// Minimum wall time between two requests from this client. |
| 121 | pub min_interval: Duration, |
| 122 | last: Option<Instant>, |
| 123 | } |
| 124 | |
| 125 | impl Client { |
| 126 | /// Build a client. |
| 127 | /// |
| 128 | /// A global timeout is not optional: without one a stalled connection hangs the crawl |
| 129 | /// indefinitely, and the service does sometimes accept a connection and then never answer. |
| 130 | pub fn new(min_interval: Duration) -> Client { |
| 131 | let config = ureq::Agent::config_builder() |
| 132 | .timeout_global(Some(Duration::from_secs(20))) |
| 133 | .timeout_connect(Some(Duration::from_secs(10))) |
| 134 | .build(); |
| 135 | Client { agent: config.into(), min_interval, last: None } |
| 136 | } |
| 137 | |
| 138 | fn throttle(&mut self) { |
| 139 | if let Some(last) = self.last { |
| 140 | let elapsed = last.elapsed(); |
| 141 | if elapsed < self.min_interval { |
| 142 | std::thread::sleep(self.min_interval - elapsed); |
| 143 | } |
| 144 | } |
| 145 | self.last = Some(Instant::now()); |
| 146 | } |
| 147 | |
| 148 | /// Ask for the locations of `bssids`. |
| 149 | /// |
| 150 | /// The response carries far more devices than were sent — Apple volunteers the neighbours |
| 151 | /// of anything it recognises, which is the whole basis of the crawl. There is no field |
| 152 | /// asking for that; it is simply what the service does. |
| 153 | pub fn query_wifi(&mut self, bssids: &[String]) -> Result<Vec<WifiFix>, Error> { |
| 154 | if bssids.is_empty() { |
| 155 | return Ok(Vec::new()); |
| 156 | } |
| 157 | let mut body = Vec::new(); |
| 158 | for b in bssids { |
| 159 | let mut dev = Vec::new(); |
| 160 | proto::put_bytes(&mut dev, 1, b.as_bytes()); |
| 161 | proto::put_bytes(&mut body, F_WIFI_DEVICES, &dev); |
| 162 | } |
| 163 | let resp = self.post(&body)?; |
| 164 | Ok(parse_wifi(&resp)) |
| 165 | } |
| 166 | |
| 167 | /// Ask for the location of one cell tower. |
| 168 | /// |
| 169 | /// `AppleWLoc` carries a single `cell_tower_request` (field 25) and answers in a repeated |
| 170 | /// `cell_tower_response` (field 22), so unlike WiFi this is one tower per request. |
| 171 | pub fn query_cell(&mut self, mcc: u32, mnc: u32, cell_id: u32, tac: u32) -> Result<Vec<CellFix>, Error> { |
| 172 | let mut tower = Vec::new(); |
| 173 | proto::put_int(&mut tower, 1, mcc as i64); |
| 174 | proto::put_int(&mut tower, 2, mnc as i64); |
| 175 | proto::put_int(&mut tower, 3, cell_id as i64); |
| 176 | proto::put_int(&mut tower, 4, tac as i64); |
| 177 | let mut body = Vec::new(); |
| 178 | proto::put_bytes(&mut body, F_CELL_REQUEST, &tower); |
| 179 | |
| 180 | let resp = self.post(&body)?; |
| 181 | Ok(parse_cells(&resp)) |
| 182 | } |
| 183 | |
| 184 | fn post(&mut self, payload: &[u8]) -> Result<Vec<u8>, Error> { |
| 185 | self.throttle(); |
| 186 | let framed = frame(payload); |
| 187 | let mut resp = self |
| 188 | .agent |
| 189 | .post(ENDPOINT) |
| 190 | .header("Content-Type", "application/x-www-form-urlencoded") |
| 191 | .header("Accept", "*/*") |
| 192 | .header("Accept-Charset", "utf-8") |
| 193 | .header("Accept-Language", "en-us") |
| 194 | .header("User-Agent", "locationd/1753.17 CFNetwork/711.1.12 Darwin/14.0.0") |
| 195 | .send(&framed[..]) |
| 196 | .map_err(|e| match e { |
| 197 | ureq::Error::StatusCode(code) => Error::Status(code), |
| 198 | other => Error::Transport(other.to_string()), |
| 199 | })?; |
| 200 | |
| 201 | let raw = resp |
| 202 | .body_mut() |
| 203 | .with_config() |
| 204 | .limit(64 * 1024 * 1024) |
| 205 | .read_to_vec() |
| 206 | .map_err(|e| Error::Transport(e.to_string()))?; |
| 207 | if raw.len() <= RESPONSE_HEADER_BYTES { |
| 208 | return Err(Error::Malformed("shorter than the response header")); |
| 209 | } |
| 210 | Ok(raw[RESPONSE_HEADER_BYTES..].to_vec()) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /// Wrap `payload` in Apple's request envelope. |
| 215 | /// |
| 216 | /// Byte-for-byte from `joelkoen/wtfps` (`src/fetch.rs`), which is a working client. The |
| 217 | /// envelope recovered from this app's old Kotlin implementation was **wrong** in three ways |
| 218 | /// and produced an empty response every time: |
| 219 | /// |
| 220 | /// * its trailer was eight bytes (`00 00 00 01` then `00 00 00 00`); the real one is **seven**; |
| 221 | /// * it length-prefixed the body with a big-endian `u16`; the real one uses a protobuf |
| 222 | /// **varint**, which for a short body is a single byte — so the two together put two spurious |
| 223 | /// zero bytes in front of every request; |
| 224 | /// * it sent the body once. The real one sends it **twice**, length-delimited and then again. |
| 225 | /// |
| 226 | /// The duplicate looks like a mistake and may well be one, but it is what the working client |
| 227 | /// does and this is not the place to find out whether Apple tolerates its absence. |
| 228 | pub fn frame(payload: &[u8]) -> Vec<u8> { |
| 229 | let mut out = Vec::with_capacity(payload.len() * 2 + 64); |
| 230 | out.extend_from_slice(&[0x00, 0x01, 0x00, 0x05]); |
| 231 | out.extend_from_slice(b"en_US"); |
| 232 | out.extend_from_slice(&[0x00, 0x13]); |
| 233 | out.extend_from_slice(b"com.apple.locationd"); |
| 234 | out.extend_from_slice(&[0x00, 0x0a]); |
| 235 | out.extend_from_slice(OS_VERSION.as_bytes()); |
| 236 | out.extend_from_slice(&[0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00]); |
| 237 | proto::put_varint(&mut out, payload.len() as u64); |
| 238 | out.extend_from_slice(payload); |
| 239 | out.extend_from_slice(payload); |
| 240 | out |
| 241 | } |
| 242 | |
| 243 | /// A location submessage. `None` when the beacon is unknown, which Apple signals by returning |
| 244 | /// -180 degrees in both axes rather than by omitting the field. |
| 245 | /// |
| 246 | /// Returns `(lat, lon, accuracy_m, altitude_m)`. Only WiFi carries fields 5/6 (altitude and |
| 247 | /// vertical accuracy); cell locations top out at field 4, so their altitude reads as |
| 248 | /// "not reported" without any special-casing. |
| 249 | fn parse_location(body: &[u8]) -> Option<(i64, i64, i32, i16)> { |
| 250 | let mut lat = None; |
| 251 | let mut lon = None; |
| 252 | let mut acc: i64 = -1; |
| 253 | let mut alt: i64 = ALT_NOT_REPORTED; |
| 254 | let mut r = proto::Reader::new(body); |
| 255 | while let Some((field, v)) = r.next() { |
| 256 | if let Field::Varint(raw) = v { |
| 257 | match field { |
| 258 | 1 => lat = Some(raw as i64), |
| 259 | 2 => lon = Some(raw as i64), |
| 260 | 3 => acc = raw as i64, |
| 261 | 5 => alt = raw as i64, |
| 262 | _ => {} |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | let (lat, lon) = (lat?, lon?); |
| 267 | if lat == NOT_FOUND_E8 || lon == NOT_FOUND_E8 { |
| 268 | return None; |
| 269 | } |
| 270 | // Accuracy is int64 on the wire but only ever a plausible radius in metres; clamp rather |
| 271 | // than truncate so a hostile or garbled value cannot wrap into something small and |
| 272 | // confident-looking. |
| 273 | Some((lat, lon, acc.clamp(-1, i32::MAX as i64) as i32, clamp_altitude(alt))) |
| 274 | } |
| 275 | |
| 276 | fn parse_wifi(body: &[u8]) -> Vec<WifiFix> { |
| 277 | let mut out = Vec::new(); |
| 278 | let mut r = proto::Reader::new(body); |
| 279 | while let Some((field, v)) = r.next() { |
| 280 | let Field::Bytes(dev) = v else { continue }; |
| 281 | if field != F_WIFI_DEVICES { |
| 282 | continue; |
| 283 | } |
| 284 | let mut bssid = None; |
| 285 | let mut loc = None; |
| 286 | let mut dr = proto::Reader::new(dev); |
| 287 | while let Some((df, dv)) = dr.next() { |
| 288 | match (df, dv) { |
| 289 | (1, Field::Bytes(s)) => bssid = std::str::from_utf8(s).ok(), |
| 290 | (2, Field::Bytes(b)) => loc = parse_location(b), |
| 291 | _ => {} |
| 292 | } |
| 293 | } |
| 294 | if let (Some(bssid), Some((lat_e8, lon_e8, accuracy_m, altitude_m))) = (bssid, loc) { |
| 295 | out.push(WifiFix { bssid: bssid.to_ascii_lowercase(), lat_e8, lon_e8, accuracy_m, altitude_m }); |
| 296 | } |
| 297 | } |
| 298 | out |
| 299 | } |
| 300 | |
| 301 | fn parse_cells(body: &[u8]) -> Vec<CellFix> { |
| 302 | let mut out = Vec::new(); |
| 303 | let mut r = proto::Reader::new(body); |
| 304 | while let Some((field, v)) = r.next() { |
| 305 | let Field::Bytes(tower) = v else { continue }; |
| 306 | if field != F_CELL_RESPONSE { |
| 307 | continue; |
| 308 | } |
| 309 | let (mut mcc, mut mnc, mut cell_id, mut area) = (None, None, None, None); |
| 310 | let mut loc = None; |
| 311 | let mut tr = proto::Reader::new(tower); |
| 312 | while let Some((tf, tv)) = tr.next() { |
| 313 | match (tf, tv) { |
| 314 | (1, Field::Varint(x)) => mcc = Some(x as i64 as i32), |
| 315 | (2, Field::Varint(x)) => mnc = Some(x as i64 as i32), |
| 316 | (3, Field::Varint(x)) => cell_id = Some(x as i64), |
| 317 | (4, Field::Varint(x)) => area = Some(x as i64 as i32), |
| 318 | (5, Field::Bytes(b)) => loc = parse_location(b), |
| 319 | _ => {} |
| 320 | } |
| 321 | } |
| 322 | if let (Some(mcc), Some(mnc), Some(cell_id), Some(area), Some((lat_e8, lon_e8, acc, _))) = |
| 323 | (mcc, mnc, cell_id, area, loc) |
| 324 | { |
| 325 | out.push(CellFix { mcc, mnc, cell_id, area, lat_e8, lon_e8, accuracy_m: acc }); |
| 326 | } |
| 327 | } |
| 328 | out |
| 329 | } |
| 330 | |
| 331 | #[cfg(test)] |
| 332 | mod tests { |
| 333 | use super::*; |
| 334 | |
| 335 | #[test] |
| 336 | fn envelope_matches_wtfps_byte_for_byte() { |
| 337 | let framed = frame(&[0xAB, 0xCD]); |
| 338 | let mut expect: Vec<u8> = Vec::new(); |
| 339 | expect.extend_from_slice(&[0x00, 0x01, 0x00, 0x05]); |
| 340 | expect.extend_from_slice(b"en_US"); |
| 341 | expect.extend_from_slice(&[0x00, 0x13]); |
| 342 | expect.extend_from_slice(b"com.apple.locationd"); |
| 343 | expect.extend_from_slice(&[0x00, 0x0a]); |
| 344 | expect.extend_from_slice(b"17.5.21F79"); |
| 345 | expect.extend_from_slice(&[0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00]); |
| 346 | expect.push(0x02); // varint length |
| 347 | expect.extend_from_slice(&[0xAB, 0xCD]); |
| 348 | expect.extend_from_slice(&[0xAB, 0xCD]); // body repeated |
| 349 | assert_eq!(framed, expect); |
| 350 | } |
| 351 | |
| 352 | /// The length-prefix is a protobuf varint, so a body of 128 bytes or more takes two bytes |
| 353 | /// where the old `u16` framing always took two. Getting this wrong shifts everything after |
| 354 | /// it and the server returns nothing at all. |
| 355 | #[test] |
| 356 | fn the_length_prefix_is_a_varint_not_a_u16() { |
| 357 | let short = frame(&[0u8; 100]); |
| 358 | let long = frame(&[0u8; 200]); |
| 359 | // 7-byte trailer ends at this offset. |
| 360 | let head = 4 + 5 + 2 + 19 + 2 + 10 + 7; |
| 361 | assert_eq!(short[head], 100, "a short body takes a one-byte varint"); |
| 362 | assert_eq!(short.len(), head + 1 + 200); |
| 363 | assert_eq!(&long[head..head + 2], &[0xC8, 0x01], "200 is two varint bytes"); |
| 364 | assert_eq!(long.len(), head + 2 + 400); |
| 365 | } |
| 366 | |
| 367 | #[test] |
| 368 | fn the_os_version_length_matches_its_hardcoded_prefix() { |
| 369 | // The 0x00 0x0a before it is literal, so a different string silently corrupts framing. |
| 370 | assert_eq!(OS_VERSION.len(), 0x0a); |
| 371 | } |
| 372 | |
| 373 | /// Build the response body the parser expects: `AppleWLoc { wifi_devices }`. |
| 374 | fn wifi_response(entries: &[(&str, Option<(i64, i64, i64, i64)>)]) -> Vec<u8> { |
| 375 | let mut body = Vec::new(); |
| 376 | for (bssid, loc) in entries { |
| 377 | let mut dev = Vec::new(); |
| 378 | proto::put_bytes(&mut dev, 1, bssid.as_bytes()); |
| 379 | if let Some((lat, lon, acc, alt)) = loc { |
| 380 | let mut l = Vec::new(); |
| 381 | proto::put_int(&mut l, 1, *lat); |
| 382 | proto::put_int(&mut l, 2, *lon); |
| 383 | proto::put_int(&mut l, 3, *acc); |
| 384 | proto::put_int(&mut l, 5, *alt); |
| 385 | proto::put_bytes(&mut dev, 2, &l); |
| 386 | } |
| 387 | proto::put_bytes(&mut body, F_WIFI_DEVICES, &dev); |
| 388 | } |
| 389 | body |
| 390 | } |
| 391 | |
| 392 | #[test] |
| 393 | fn wifi_responses_decode_and_normalize_case() { |
| 394 | let body = wifi_response(&[ |
| 395 | ("AA:BB:CC:DD:EE:01", Some((37_77493000, -122_41942000, 35, 12))), |
| 396 | ("aa:bb:cc:dd:ee:02", Some((51_50735000, -0_12776000, 120, 44))), |
| 397 | ]); |
| 398 | let fixes = parse_wifi(&body); |
| 399 | assert_eq!(fixes.len(), 2); |
| 400 | assert_eq!(fixes[0].bssid, "aa:bb:cc:dd:ee:01"); |
| 401 | assert_eq!(fixes[0].lat_e8, 37_77493000); |
| 402 | assert_eq!(fixes[0].accuracy_m, 35); |
| 403 | assert_eq!(fixes[0].altitude_m, 12); |
| 404 | assert_eq!(fixes[1].lon_e8, -0_12776000); |
| 405 | assert_eq!(fixes[1].altitude_m, 44); |
| 406 | } |
| 407 | |
| 408 | #[test] |
| 409 | fn altitude_decodes_and_its_absence_is_unknown() { |
| 410 | // Apple's "not reported" is -1; a missing field 5 reads the same way. |
| 411 | let body = wifi_response(&[ |
| 412 | ("aa:bb:cc:dd:ee:01", Some((37_00000000, -122_00000000, 30, ALT_NOT_REPORTED))), |
| 413 | ("aa:bb:cc:dd:ee:02", Some((37_00000000, -122_00000000, 30, -430))), |
| 414 | ]); |
| 415 | let fixes = parse_wifi(&body); |
| 416 | assert_eq!(fixes[0].altitude_m, i16::MIN, "-1 means altitude unknown"); |
| 417 | assert_eq!(fixes[1].altitude_m, -430, "below sea level survives (Dead Sea)"); |
| 418 | assert_eq!(clamp_altitude(i64::MAX), i16::MAX); |
| 419 | assert_eq!(clamp_altitude(i64::MIN + 1), i16::MIN + 1); |
| 420 | } |
| 421 | |
| 422 | #[test] |
| 423 | fn beacons_apple_does_not_know_are_dropped() { |
| 424 | let body = wifi_response(&[ |
| 425 | // Apple's "not found": -180 degrees in both axes. |
| 426 | ("aa:bb:cc:dd:ee:01", Some((NOT_FOUND_E8, NOT_FOUND_E8, 500, 10))), |
| 427 | ("aa:bb:cc:dd:ee:02", None), |
| 428 | ("aa:bb:cc:dd:ee:03", Some((37_00000000, -122_00000000, 50, 10))), |
| 429 | ]); |
| 430 | let fixes = parse_wifi(&body); |
| 431 | assert_eq!(fixes.len(), 1, "only the one real fix survives"); |
| 432 | assert_eq!(fixes[0].bssid, "aa:bb:cc:dd:ee:03"); |
| 433 | } |
| 434 | |
| 435 | /// A negative accuracy is not the "unknown" signal — the coordinate sentinel is. Treating |
| 436 | /// it as one, as the old client did, threw away real fixes. |
| 437 | #[test] |
| 438 | fn a_missing_accuracy_does_not_discard_a_real_fix() { |
| 439 | let body = wifi_response(&[("aa:bb:cc:dd:ee:04", Some((37_00000000, -122_00000000, -1, 10)))]); |
| 440 | let fixes = parse_wifi(&body); |
| 441 | assert_eq!(fixes.len(), 1); |
| 442 | assert_eq!(fixes[0].accuracy_m, -1, "reported as unknown, not dropped"); |
| 443 | } |
| 444 | |
| 445 | #[test] |
| 446 | fn cell_responses_decode() { |
| 447 | let mut body = Vec::new(); |
| 448 | let mut tower = Vec::new(); |
| 449 | proto::put_int(&mut tower, 1, 310); |
| 450 | proto::put_int(&mut tower, 2, 260); |
| 451 | proto::put_int(&mut tower, 3, 123456789); |
| 452 | proto::put_int(&mut tower, 4, 4242); |
| 453 | let mut l = Vec::new(); |
| 454 | proto::put_int(&mut l, 1, 40_71278000); |
| 455 | proto::put_int(&mut l, 2, -74_00594000); |
| 456 | proto::put_int(&mut l, 3, 900); |
| 457 | proto::put_bytes(&mut tower, 5, &l); |
| 458 | proto::put_bytes(&mut body, F_CELL_RESPONSE, &tower); |
| 459 | |
| 460 | let fixes = parse_cells(&body); |
| 461 | assert_eq!(fixes.len(), 1); |
| 462 | assert_eq!(fixes[0].mcc, 310); |
| 463 | assert_eq!(fixes[0].cell_id, 123456789); |
| 464 | assert_eq!(fixes[0].lat_e8, 40_71278000); |
| 465 | assert_eq!(fixes[0].accuracy_m, 900); |
| 466 | } |
| 467 | |
| 468 | /// Cell towers answer in field 22, not field 1. Reading the wrong field returns nothing |
| 469 | /// while looking like an empty database. |
| 470 | #[test] |
| 471 | fn a_cell_response_in_the_old_field_number_is_ignored() { |
| 472 | let mut body = Vec::new(); |
| 473 | let mut tower = Vec::new(); |
| 474 | proto::put_int(&mut tower, 1, 310); |
| 475 | proto::put_bytes(&mut body, 1, &tower); |
| 476 | assert!(parse_cells(&body).is_empty()); |
| 477 | } |
| 478 | |
| 479 | #[test] |
| 480 | fn a_truncated_response_yields_what_decoded_rather_than_failing() { |
| 481 | let body = wifi_response(&[ |
| 482 | ("aa:bb:cc:dd:ee:01", Some((37_00000000, -122_00000000, 30, 10))), |
| 483 | ("aa:bb:cc:dd:ee:02", Some((38_00000000, -121_00000000, 40, 10))), |
| 484 | ]); |
| 485 | for cut in 1..body.len() { |
| 486 | let fixes = parse_wifi(&body[..cut]); |
| 487 | assert!(fixes.len() <= 2); |
| 488 | } |
| 489 | assert_eq!(parse_wifi(&body).len(), 2); |
| 490 | } |
| 491 | } |