#[cfg(test)] mod tests { use super::*; /// One wifi entry in a fake response: BSSID plus optional `(lat, lon, acc, alt)`. type WifiEntry<'a> = (&'a str, Option<(i64, i64, i64, i64)>); #[test] fn envelope_matches_wtfps_byte_for_byte() { let framed = frame(&[0xAB, 0xCD]); let mut expect: Vec = Vec::new(); expect.extend_from_slice(&[0x00, 0x01, 0x00, 0x05]); expect.extend_from_slice(b"en_US"); expect.extend_from_slice(&[0x00, 0x13]); expect.extend_from_slice(b"com.apple.locationd"); expect.extend_from_slice(&[0x00, 0x0a]); expect.extend_from_slice(b"17.5.21F79"); expect.extend_from_slice(&[0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00]); expect.push(0x02); // varint length expect.extend_from_slice(&[0xAB, 0xCD]); expect.extend_from_slice(&[0xAB, 0xCD]); // body repeated assert_eq!(framed, expect); } /// The length-prefix is a protobuf varint, so a body of 128 bytes or more takes two bytes /// where the old `u16` framing always took two. Getting this wrong shifts everything after /// it and the server returns nothing at all. #[test] fn the_length_prefix_is_a_varint_not_a_u16() { let short = frame(&[0u8; 100]); let long = frame(&[0u8; 200]); // 7-byte trailer ends at this offset. let head = 4 + 5 + 2 + 19 + 2 + 10 + 7; assert_eq!(short[head], 100, "a short body takes a one-byte varint"); assert_eq!(short.len(), head + 1 + 200); assert_eq!(&long[head..head + 2], &[0xC8, 0x01], "200 is two varint bytes"); assert_eq!(long.len(), head + 2 + 400); } #[test] fn the_os_version_length_matches_its_hardcoded_prefix() { // The 0x00 0x0a before it is literal, so a different string silently corrupts framing. assert_eq!(OS_VERSION.len(), 0x0a); } /// Build the response body the parser expects: `AppleWLoc { wifi_devices }`. fn wifi_response(entries: &[WifiEntry<'_>]) -> Vec { let mut body = Vec::new(); for (bssid, loc) in entries { let mut dev = Vec::new(); proto::put_bytes(&mut dev, 1, bssid.as_bytes()); if let Some((lat, lon, acc, alt)) = loc { let mut l = Vec::new(); proto::put_int(&mut l, 1, *lat); proto::put_int(&mut l, 2, *lon); proto::put_int(&mut l, 3, *acc); proto::put_int(&mut l, 5, *alt); proto::put_bytes(&mut dev, 2, &l); } proto::put_bytes(&mut body, F_WIFI_DEVICES, &dev); } body } #[test] fn wifi_responses_decode_and_normalize_case() { let body = wifi_response(&[ ("AA:BB:CC:DD:EE:01", Some((37_77493000, -122_41942000, 35, 12))), ("aa:bb:cc:dd:ee:02", Some((51_50735000, -12_776_000, 120, 44))), ]); let fixes = parse_wifi(&body); assert_eq!(fixes.len(), 2); assert_eq!(fixes[0].bssid, "aa:bb:cc:dd:ee:01"); assert_eq!(fixes[0].lat_e8, 37_77493000); assert_eq!(fixes[0].accuracy_m, 35); assert_eq!(fixes[0].altitude_m, 12); assert_eq!(fixes[1].lon_e8, -12_776_000); assert_eq!(fixes[1].altitude_m, 44); } #[test] fn altitude_decodes_and_its_absence_is_unknown() { // Apple's "not reported" is -1; a missing field 5 reads the same way. let body = wifi_response(&[ ("aa:bb:cc:dd:ee:01", Some((37_00000000, -122_00000000, 30, ALT_NOT_REPORTED))), ("aa:bb:cc:dd:ee:02", Some((37_00000000, -122_00000000, 30, -430))), ]); let fixes = parse_wifi(&body); assert_eq!(fixes[0].altitude_m, i16::MIN, "-1 means altitude unknown"); assert_eq!(fixes[1].altitude_m, -430, "below sea level survives (Dead Sea)"); assert_eq!(clamp_altitude(i64::MAX), i16::MAX); assert_eq!(clamp_altitude(i64::MIN + 1), i16::MIN + 1); } #[test] fn beacons_apple_does_not_know_are_dropped() { let body = wifi_response(&[ // Apple's "not found": -180 degrees in both axes. ("aa:bb:cc:dd:ee:01", Some((NOT_FOUND_E8, NOT_FOUND_E8, 500, 10))), ("aa:bb:cc:dd:ee:02", None), ("aa:bb:cc:dd:ee:03", Some((37_00000000, -122_00000000, 50, 10))), ]); let fixes = parse_wifi(&body); assert_eq!(fixes.len(), 1, "only the one real fix survives"); assert_eq!(fixes[0].bssid, "aa:bb:cc:dd:ee:03"); } /// A negative accuracy is not the "unknown" signal — the coordinate sentinel is. Treating /// it as one, as the old client did, threw away real fixes. #[test] fn a_missing_accuracy_does_not_discard_a_real_fix() { let body = wifi_response(&[("aa:bb:cc:dd:ee:04", Some((37_00000000, -122_00000000, -1, 10)))]); let fixes = parse_wifi(&body); assert_eq!(fixes.len(), 1); assert_eq!(fixes[0].accuracy_m, -1, "reported as unknown, not dropped"); } #[test] fn cell_responses_decode() { let mut body = Vec::new(); let mut tower = Vec::new(); proto::put_int(&mut tower, 1, 310); proto::put_int(&mut tower, 2, 260); proto::put_int(&mut tower, 3, 123456789); proto::put_int(&mut tower, 4, 4242); let mut l = Vec::new(); proto::put_int(&mut l, 1, 40_71278000); proto::put_int(&mut l, 2, -74_00594000); proto::put_int(&mut l, 3, 900); proto::put_bytes(&mut tower, 5, &l); proto::put_bytes(&mut body, F_CELL_RESPONSE, &tower); let fixes = parse_cells(&body); assert_eq!(fixes.len(), 1); assert_eq!(fixes[0].mcc, 310); assert_eq!(fixes[0].cell_id, 123456789); assert_eq!(fixes[0].lat_e8, 40_71278000); assert_eq!(fixes[0].accuracy_m, 900); } /// Cell towers answer in field 22, not field 1. Reading the wrong field returns nothing /// while looking like an empty database. #[test] fn a_cell_response_in_the_old_field_number_is_ignored() { let mut body = Vec::new(); let mut tower = Vec::new(); proto::put_int(&mut tower, 1, 310); proto::put_bytes(&mut body, 1, &tower); assert!(parse_cells(&body).is_empty()); } #[test] fn a_truncated_response_yields_what_decoded_rather_than_failing() { let body = wifi_response(&[ ("aa:bb:cc:dd:ee:01", Some((37_00000000, -122_00000000, 30, 10))), ("aa:bb:cc:dd:ee:02", Some((38_00000000, -121_00000000, 40, 10))), ]); for cut in 1..body.len() { let fixes = parse_wifi(&body[..cut]); assert!(fixes.len() <= 2); } assert_eq!(parse_wifi(&body).len(), 2); } }