//! The phone has no `Buffer` and Hermes has no `btoa`, so the device id and the //! signature are encoded by hand on three sides now. The one property that //! matters is that Node's `base64url` — what the server decodes with — reads //! back exactly these bytes, so every expected string below was printed by //! running the TypeScript beside `Buffer.from(bytes).toString("base64url")` //! and pasting what the two agreed on. use super::*; /// The TypeScript test's own generator: `(i * 37 + 11) % 256`. fn ramp(length: usize) -> Vec { (0..length).map(|i| ((i * 37 + 11) % 256) as u8).collect() } #[test] fn round_trips_bytes_with_no_padding_and_the_url_alphabet() { let bytes = [0u8, 1, 2, 250, 251, 252, 253, 254, 255]; let encoded = base64url_encode(&bytes); assert!(!encoded.contains(['+', '/', '='])); assert_eq!(base64url_decode(&encoded).as_deref(), Some(&bytes[..])); } /// Vectors: the TypeScript's answer on every length class, which is also /// `Buffer.from(bytes).toString("base64url")` — the two were printed together /// and compared before being pasted here. #[test] fn matches_nodes_answer_on_every_length_class() { const EXPECTED: [(usize, &str); 10] = [ (0, ""), (1, "Cw"), (2, "CzA"), (3, "CzBV"), (4, "CzBVeg"), (5, "CzBVep8"), (31, "CzBVep_E6Q4zWH2ix-wRNluApcrvFDleg6jN8hc8YQ"), (32, "CzBVep_E6Q4zWH2ix-wRNluApcrvFDleg6jN8hc8YYY"), (33, "CzBVep_E6Q4zWH2ix-wRNluApcrvFDleg6jN8hc8YYar"), ( 64, "CzBVep_E6Q4zWH2ix-wRNluApcrvFDleg6jN8hc8YYar0PUaP2SJrtP4HUJnjLHW-yBFao-02f4jSG2St9wBJg", ), ]; for (length, encoded) in EXPECTED { let bytes = ramp(length); assert_eq!(base64url_encode(&bytes), encoded, "encoding {length} bytes"); assert_eq!( base64url_decode(encoded), Some(bytes), "decoding {length} bytes" ); } } /// Vectors: the alphabet's own edges — 62 is `-`, 63 is `_`, and neither `+` /// nor `/` can appear in an answer. #[test] fn spells_the_url_alphabet_at_its_edges() { assert_eq!( base64url_encode(&[0, 1, 2, 250, 251, 252, 253, 254, 255]), "AAEC-vv8_f7_" ); assert_eq!(base64url_encode(&[255, 255, 255]), "____"); assert_eq!(base64url_encode(&[251, 255, 190]), "-_--"); assert_eq!(base64url_encode(&[1]), "AQ"); assert_eq!(base64url_encode(&[0, 0]), "AAA"); } #[test] fn refuses_anything_outside_the_alphabet_and_an_impossible_length() { assert_eq!(base64url_decode("ab+c"), None); assert_eq!(base64url_decode("ab="), None); assert_eq!(base64url_decode("a"), None); // Five characters is `4 + 1`: the same impossible remainder, one group on. assert_eq!(base64url_decode("AAAAA"), None); // Non-ASCII fails the alphabet, as the TypeScript's regex does. assert_eq!(base64url_decode("AA😀"), None); } /// One byte string, one encoding — like Node's strict reading. A device id /// that differs only in the trailing bits would be a second name for one key. #[test] fn refuses_non_canonical_trailing_bits() { // "AQ" is [1]; "AR" carries the same byte plus a stray low bit nothing // decodes to. assert_eq!(base64url_decode("AQ"), Some(vec![1])); assert_eq!(base64url_decode("AR"), None); // Three characters: two bytes plus two trailing bits that must be zero. assert_eq!(base64url_decode("AAA"), Some(vec![0, 0])); assert_eq!(base64url_decode("AAB"), None); // Vectors for the same rule one group along, and on the second character // of a two-character group. assert_eq!(base64url_decode("AAAAAB"), None); assert_eq!(base64url_decode("Aa"), None); assert_eq!(base64url_decode("Ab"), None); } /// The captured device id from the TypeScript test: its last character sits on /// a 4-character boundary and stays canonical; flipping its low bits is a /// different, refused string. #[test] fn a_real_device_id_decodes_and_its_flipped_twin_does_not() { let id = "J2pwKHAjQ4-sUysPuoxOSvu0QntvCBuWZa4pPWaRW1I"; let decoded = base64url_decode(id).expect("the captured device id decodes"); assert_eq!(decoded.len(), 32); // Vector: the 32 bytes Node's `base64url` reads out of that string. assert_eq!( decoded, vec![ 39, 106, 112, 40, 112, 35, 67, 143, 172, 83, 43, 15, 186, 140, 78, 74, 251, 180, 66, 123, 111, 8, 27, 150, 101, 174, 41, 61, 102, 145, 91, 82 ] ); assert_eq!(base64url_encode(&decoded), id); assert_eq!( base64url_decode("J2pwKHAjQ4-sUysPuoxOSvu0QntvCBuWZa4pPWaRW1J"), None ); } #[test] fn the_empty_string_is_the_empty_byte_string() { assert_eq!(base64url_decode(""), Some(Vec::new())); assert_eq!(base64url_encode(&[]), ""); }