Cotton code
158 lines · 5.2 KB · RustRaw
//! `apps/cotton/__tests__/ingest/attributedBody.test.ts`, on blobs built//! byte by byte.//!//! SYNTHETIC on purpose, and it has to be: the real blobs on this Mac are the//! owner's own messages, and nothing derived from them may reach the tree//! ([nothing-real-ever-goes-in-cotton-s-public-tree]). Every fixture below is//! assembled here from the format's own rules, which is also the only way to//! cover the two long-length forms — the owner's file has no 70 kB message.//!//! ONE case of the TypeScript file is gone: "takes a Buffer as readily as a//! Uint8Array". Node has two byte types and the decoder had to accept both;//! Rust has one, and `&[u8]` is what rusqlite hands back.use super::*;/// The typedstream length prefix, in the three forms the format uses.fn length_bytes(byte_length: usize) -> Vec<u8> {    if byte_length < 0x80 {        return vec![byte_length as u8];    }    if byte_length <= 0xffff {        return vec![            0x81,            (byte_length & 0xff) as u8,            ((byte_length >> 8) & 0xff) as u8,        ];    }    vec![        0x82,        (byte_length & 0xff) as u8,        ((byte_length >> 8) & 0xff) as u8,        ((byte_length >> 16) & 0xff) as u8,        ((byte_length >> 24) & 0xff) as u8,    ]}/// A blob shaped like the ones Messages writes: some archiver preamble, the/// `NSString` class marker, a `+`, the length, the UTF-8, and a tail of/// attribute runs the decoder must ignore.fn blob_for(text: &str) -> Vec<u8> {    let body = text.as_bytes();    let mut out: Vec<u8> = vec![        0x04, 0x0b, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x74, 0x79, 0x70, 0x65, 0x64, 0x81, 0xe8,        0x03, 0x84, 0x01, 0x40, 0x84, 0x84, 0x84, 0x12,    ];    out.extend_from_slice(NS_STRING);    out.extend_from_slice(&[0x01, 0x94, 0x84, 0x01]);    out.push(0x2b);    out.extend_from_slice(&length_bytes(body.len()));    out.extend_from_slice(body);    out.extend_from_slice(&[0x86, 0x84, 0x02, 0x69, 0x49, 0x01, 0x00]);    out}fn decode(blob: &[u8]) -> Option<String> {    decode_attributed_body(Some(blob))}#[test]fn it_reads_a_short_body_where_the_length_is_one_byte() {    assert_eq!(decode(&blob_for("on my way")).as_deref(), Some("on my way"));}#[test]fn it_reads_a_body_whose_length_needs_the_0x81_u16_form() {    let text = "x".repeat(400);    let blob = blob_for(&text);    // The fixture really is exercising the long form, not the short one.    assert!(blob.contains(&0x81));    assert_eq!(decode(&blob).as_deref(), Some(text.as_str()));}#[test]fn it_reads_a_body_whose_length_needs_the_0x82_u32_form() {    let text = "y".repeat(70_000);    assert_eq!(decode(&blob_for(&text)).as_deref(), Some(text.as_str()));}#[test]fn it_counts_length_in_bytes_not_characters_for_multi_byte_utf8() {    let text = "नमस्ते, चलिए मिलते हैं";    assert!(text.len() > text.chars().count());    assert_eq!(decode(&blob_for(text)).as_deref(), Some(text));}#[test]fn it_reads_emoji_including_a_surrogate_pair_and_a_zwj_sequence() {    let text = "done 🎉 👩‍💻";    assert_eq!(decode(&blob_for(text)).as_deref(), Some(text));}#[test]fn it_reads_a_body_that_is_exactly_127_bytes_the_last_one_byte_length() {    let text = "z".repeat(127);    assert_eq!(decode(&blob_for(&text)).as_deref(), Some(text.as_str()));}#[test]fn it_reads_a_body_of_exactly_128_bytes_the_first_that_needs_the_u16_form() {    let text = "z".repeat(128);    assert_eq!(decode(&blob_for(&text)).as_deref(), Some(text.as_str()));}#[test]fn it_answers_none_for_an_empty_blob_rather_than_an_empty_string() {    assert_eq!(decode(&[]), None);    assert_eq!(decode_attributed_body(None), None);}#[test]fn it_answers_none_when_there_is_no_ns_string_marker_at_all() {    assert_eq!(decode(&[0x04, 0x0b, 0x2b, 0x05, 1, 2, 3, 4, 5]), None);}#[test]fn it_answers_none_when_the_declared_length_runs_past_the_end_of_the_blob() {    let blob = blob_for("hello");    let truncated = &blob[..blob.len() - 8];    assert_eq!(decode(truncated), None);}#[test]fn it_answers_none_for_a_length_byte_in_neither_the_short_nor_the_two_long_forms() {    let mut blob = blob_for("hello");    let plus = blob        .iter()        .position(|byte| *byte == 0x2b)        .expect("the fixture's own tag");    blob[plus + 1] = 0x80; // not a length: 0x80 is not 0x81, 0x82 or < 0x80    assert_eq!(decode(&blob), None);}#[test]fn it_answers_none_for_a_body_that_decodes_to_nothing_but_whitespace() {    assert_eq!(decode(&blob_for("   \n ")), None);}#[test]fn it_skips_a_plus_that_belongs_to_the_text_and_finds_the_one_that_frames_it() {    // The class marker is what anchors the read, so a `+` earlier in the blob    // is not a candidate at all.    assert_eq!(        decode(&blob_for("+1 on that")).as_deref(),        Some("+1 on that")    );}#[test]fn it_reads_a_body_that_follows_the_mutable_class_marker() {    let text = "from an older archive";    let mut blob: Vec<u8> = vec![0x04, 0x0b, 0x84, 0x12];    blob.extend_from_slice(NS_MUTABLE_STRING);    blob.extend_from_slice(&[0x01, 0x94]);    blob.push(0x2b);    blob.extend_from_slice(&length_bytes(text.len()));    blob.extend_from_slice(text.as_bytes());    assert_eq!(decode(&blob).as_deref(), Some(text));}