Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / networklocation / wps_harvest / src / gsloc_s1.rs
gsloc_s1.rs165 lines · 6.7 KiBRaw
| 1 | #[cfg(test)] |
| 2 | mod tests { |
| 3 | use super::*; |
| 4 | |
| 5 | /// One wifi entry in a fake response: BSSID plus optional `(lat, lon, acc, alt)`. |
| 6 | type WifiEntry<'a> = (&'a str, Option<(i64, i64, i64, i64)>); |
| 7 | |
| 8 | #[test] |
| 9 | fn envelope_matches_wtfps_byte_for_byte() { |
| 10 | let framed = frame(&[0xAB, 0xCD]); |
| 11 | let mut expect: Vec<u8> = Vec::new(); |
| 12 | expect.extend_from_slice(&[0x00, 0x01, 0x00, 0x05]); |
| 13 | expect.extend_from_slice(b"en_US"); |
| 14 | expect.extend_from_slice(&[0x00, 0x13]); |
| 15 | expect.extend_from_slice(b"com.apple.locationd"); |
| 16 | expect.extend_from_slice(&[0x00, 0x0a]); |
| 17 | expect.extend_from_slice(b"17.5.21F79"); |
| 18 | expect.extend_from_slice(&[0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00]); |
| 19 | expect.push(0x02); // varint length |
| 20 | expect.extend_from_slice(&[0xAB, 0xCD]); |
| 21 | expect.extend_from_slice(&[0xAB, 0xCD]); // body repeated |
| 22 | assert_eq!(framed, expect); |
| 23 | } |
| 24 | |
| 25 | /// The length-prefix is a protobuf varint, so a body of 128 bytes or more takes two bytes |
| 26 | /// where the old `u16` framing always took two. Getting this wrong shifts everything after |
| 27 | /// it and the server returns nothing at all. |
| 28 | #[test] |
| 29 | fn the_length_prefix_is_a_varint_not_a_u16() { |
| 30 | let short = frame(&[0u8; 100]); |
| 31 | let long = frame(&[0u8; 200]); |
| 32 | // 7-byte trailer ends at this offset. |
| 33 | let head = 4 + 5 + 2 + 19 + 2 + 10 + 7; |
| 34 | assert_eq!(short[head], 100, "a short body takes a one-byte varint"); |
| 35 | assert_eq!(short.len(), head + 1 + 200); |
| 36 | assert_eq!(&long[head..head + 2], &[0xC8, 0x01], "200 is two varint bytes"); |
| 37 | assert_eq!(long.len(), head + 2 + 400); |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn the_os_version_length_matches_its_hardcoded_prefix() { |
| 42 | // The 0x00 0x0a before it is literal, so a different string silently corrupts framing. |
| 43 | assert_eq!(OS_VERSION.len(), 0x0a); |
| 44 | } |
| 45 | |
| 46 | /// Build the response body the parser expects: `AppleWLoc { wifi_devices }`. |
| 47 | fn wifi_response(entries: &[WifiEntry<'_>]) -> Vec<u8> { |
| 48 | let mut body = Vec::new(); |
| 49 | for (bssid, loc) in entries { |
| 50 | let mut dev = Vec::new(); |
| 51 | proto::put_bytes(&mut dev, 1, bssid.as_bytes()); |
| 52 | if let Some((lat, lon, acc, alt)) = loc { |
| 53 | let mut l = Vec::new(); |
| 54 | proto::put_int(&mut l, 1, *lat); |
| 55 | proto::put_int(&mut l, 2, *lon); |
| 56 | proto::put_int(&mut l, 3, *acc); |
| 57 | proto::put_int(&mut l, 5, *alt); |
| 58 | proto::put_bytes(&mut dev, 2, &l); |
| 59 | } |
| 60 | proto::put_bytes(&mut body, F_WIFI_DEVICES, &dev); |
| 61 | } |
| 62 | body |
| 63 | } |
| 64 | |
| 65 | #[test] |
| 66 | fn wifi_responses_decode_and_normalize_case() { |
| 67 | let body = wifi_response(&[ |
| 68 | ("AA:BB:CC:DD:EE:01", Some((37_77493000, -122_41942000, 35, 12))), |
| 69 | ("aa:bb:cc:dd:ee:02", Some((51_50735000, -12_776_000, 120, 44))), |
| 70 | ]); |
| 71 | let fixes = parse_wifi(&body); |
| 72 | assert_eq!(fixes.len(), 2); |
| 73 | assert_eq!(fixes[0].bssid, "aa:bb:cc:dd:ee:01"); |
| 74 | assert_eq!(fixes[0].lat_e8, 37_77493000); |
| 75 | assert_eq!(fixes[0].accuracy_m, 35); |
| 76 | assert_eq!(fixes[0].altitude_m, 12); |
| 77 | assert_eq!(fixes[1].lon_e8, -12_776_000); |
| 78 | assert_eq!(fixes[1].altitude_m, 44); |
| 79 | } |
| 80 | |
| 81 | #[test] |
| 82 | fn altitude_decodes_and_its_absence_is_unknown() { |
| 83 | // Apple's "not reported" is -1; a missing field 5 reads the same way. |
| 84 | let body = wifi_response(&[ |
| 85 | ("aa:bb:cc:dd:ee:01", Some((37_00000000, -122_00000000, 30, ALT_NOT_REPORTED))), |
| 86 | ("aa:bb:cc:dd:ee:02", Some((37_00000000, -122_00000000, 30, -430))), |
| 87 | ]); |
| 88 | let fixes = parse_wifi(&body); |
| 89 | assert_eq!(fixes[0].altitude_m, i16::MIN, "-1 means altitude unknown"); |
| 90 | assert_eq!(fixes[1].altitude_m, -430, "below sea level survives (Dead Sea)"); |
| 91 | assert_eq!(clamp_altitude(i64::MAX), i16::MAX); |
| 92 | assert_eq!(clamp_altitude(i64::MIN + 1), i16::MIN + 1); |
| 93 | } |
| 94 | |
| 95 | #[test] |
| 96 | fn beacons_apple_does_not_know_are_dropped() { |
| 97 | let body = wifi_response(&[ |
| 98 | // Apple's "not found": -180 degrees in both axes. |
| 99 | ("aa:bb:cc:dd:ee:01", Some((NOT_FOUND_E8, NOT_FOUND_E8, 500, 10))), |
| 100 | ("aa:bb:cc:dd:ee:02", None), |
| 101 | ("aa:bb:cc:dd:ee:03", Some((37_00000000, -122_00000000, 50, 10))), |
| 102 | ]); |
| 103 | let fixes = parse_wifi(&body); |
| 104 | assert_eq!(fixes.len(), 1, "only the one real fix survives"); |
| 105 | assert_eq!(fixes[0].bssid, "aa:bb:cc:dd:ee:03"); |
| 106 | } |
| 107 | |
| 108 | /// A negative accuracy is not the "unknown" signal — the coordinate sentinel is. Treating |
| 109 | /// it as one, as the old client did, threw away real fixes. |
| 110 | #[test] |
| 111 | fn a_missing_accuracy_does_not_discard_a_real_fix() { |
| 112 | let body = |
| 113 | wifi_response(&[("aa:bb:cc:dd:ee:04", Some((37_00000000, -122_00000000, -1, 10)))]); |
| 114 | let fixes = parse_wifi(&body); |
| 115 | assert_eq!(fixes.len(), 1); |
| 116 | assert_eq!(fixes[0].accuracy_m, -1, "reported as unknown, not dropped"); |
| 117 | } |
| 118 | |
| 119 | #[test] |
| 120 | fn cell_responses_decode() { |
| 121 | let mut body = Vec::new(); |
| 122 | let mut tower = Vec::new(); |
| 123 | proto::put_int(&mut tower, 1, 310); |
| 124 | proto::put_int(&mut tower, 2, 260); |
| 125 | proto::put_int(&mut tower, 3, 123456789); |
| 126 | proto::put_int(&mut tower, 4, 4242); |
| 127 | let mut l = Vec::new(); |
| 128 | proto::put_int(&mut l, 1, 40_71278000); |
| 129 | proto::put_int(&mut l, 2, -74_00594000); |
| 130 | proto::put_int(&mut l, 3, 900); |
| 131 | proto::put_bytes(&mut tower, 5, &l); |
| 132 | proto::put_bytes(&mut body, F_CELL_RESPONSE, &tower); |
| 133 | |
| 134 | let fixes = parse_cells(&body); |
| 135 | assert_eq!(fixes.len(), 1); |
| 136 | assert_eq!(fixes[0].mcc, 310); |
| 137 | assert_eq!(fixes[0].cell_id, 123456789); |
| 138 | assert_eq!(fixes[0].lat_e8, 40_71278000); |
| 139 | assert_eq!(fixes[0].accuracy_m, 900); |
| 140 | } |
| 141 | |
| 142 | /// Cell towers answer in field 22, not field 1. Reading the wrong field returns nothing |
| 143 | /// while looking like an empty database. |
| 144 | #[test] |
| 145 | fn a_cell_response_in_the_old_field_number_is_ignored() { |
| 146 | let mut body = Vec::new(); |
| 147 | let mut tower = Vec::new(); |
| 148 | proto::put_int(&mut tower, 1, 310); |
| 149 | proto::put_bytes(&mut body, 1, &tower); |
| 150 | assert!(parse_cells(&body).is_empty()); |
| 151 | } |
| 152 | |
| 153 | #[test] |
| 154 | fn a_truncated_response_yields_what_decoded_rather_than_failing() { |
| 155 | let body = wifi_response(&[ |
| 156 | ("aa:bb:cc:dd:ee:01", Some((37_00000000, -122_00000000, 30, 10))), |
| 157 | ("aa:bb:cc:dd:ee:02", Some((38_00000000, -121_00000000, 40, 10))), |
| 158 | ]); |
| 159 | for cut in 1..body.len() { |
| 160 | let fixes = parse_wifi(&body[..cut]); |
| 161 | assert!(fixes.len() <= 2); |
| 162 | } |
| 163 | assert_eq!(parse_wifi(&body).len(), 2); |
| 164 | } |
| 165 | } |