Cotton code
182 lines · 5.5 KB · RustRaw
//! The copy-before-read rule, and what a failed open means.use super::*;use crate::test_support::temp_layout;fn write(path: &Path, bytes: &[u8]) {    if let Some(parent) = path.parent() {        std::fs::create_dir_all(parent).expect("the folder");    }    std::fs::write(path, bytes).expect("the file");}fn a_database(path: &Path) {    let db = crate::test_support::open_fixture_db(path);    db.execute_batch("CREATE TABLE rows (id INTEGER PRIMARY KEY); INSERT INTO rows VALUES (1),(2)")        .expect("some rows");}#[test]fn a_copy_takes_the_wal_and_the_shm_with_it() {    let (dir, layout) = temp_layout();    let source = dir.path().join("live.db");    a_database(&source);    write(&sidecar(&source, "-wal"), b"synthetic wal");    write(&sidecar(&source, "-shm"), b"synthetic shm");    let destination = snapshot_path_for(&layout, "live.db");    copy_for_read(&source, &destination).expect("a copy");    assert!(destination.exists());    assert_eq!(        std::fs::read(sidecar(&destination, "-wal")).expect("the wal"),        b"synthetic wal"    );    assert_eq!(        std::fs::read(sidecar(&destination, "-shm")).expect("the shm"),        b"synthetic shm"    );}#[test]fn the_previous_passs_sidecars_go_before_the_new_copy_lands() {    let (dir, layout) = temp_layout();    let source = dir.path().join("live.db");    a_database(&source);    let destination = snapshot_path_for(&layout, "live.db");    // A stale `-wal` beside a fresh database is a file SQLite will happily    // replay the wrong pages out of.    write(&sidecar(&destination, "-wal"), b"last week's wal");    copy_for_read(&source, &destination).expect("a copy");    assert!(!sidecar(&destination, "-wal").exists());}#[test]fn it_opens_the_copy_and_never_the_file_the_daemon_holds() {    let (dir, layout) = temp_layout();    let source = dir.path().join("chat.db");    a_database(&source);    let db = open_live_copy(&layout, &source, "messages.db").expect("a snapshot");    let count: i64 = db        .query_row("SELECT count(*) FROM rows", [], |row| row.get(0))        .expect("a count");    assert_eq!(count, 2);    assert!(snapshot_path_for(&layout, "messages.db").exists());}#[test]fn a_snapshot_open_is_read_only_so_a_source_can_never_write_to_one() {    let (dir, layout) = temp_layout();    let source = dir.path().join("chat.db");    a_database(&source);    let db = open_live_copy(&layout, &source, "messages.db").expect("a snapshot");    assert!(db.execute("INSERT INTO rows VALUES (3)", []).is_err());}#[test]fn a_file_that_is_not_there_is_not_found() {    let (dir, layout) = temp_layout();    let missing = dir.path().join("nothing.db");    assert_eq!(        open_live_copy(&layout, &missing, "x.db")            .expect_err("no file")            .state,        SourceState::NotFound    );}#[test]fn it_reads_a_refused_open_as_the_person_not_having_said_yes_yet() {    let path = Path::new("/Users/someone/Library/Messages/chat.db");    for message in [        "Operation not permitted (os error 1)",        "Permission denied (os error 13)",        "unable to open database file",        "SQLITE_CANTOPEN: unable to open database file",        "not authorized",    ] {        let status = classify_read_failure(message, path);        assert_eq!(status.state, SourceState::NoPermission, "{message}");        assert!(status            .detail            .expect("a sentence")            .contains("Full Disk Access"));    }}#[test]fn a_missing_file_is_a_different_sentence_from_a_refused_one() {    let path = Path::new("/Users/someone/Library/Messages/chat.db");    let status = classify_read_failure("No such file or directory (os error 2)", path);    assert_eq!(status.state, SourceState::NotFound);    assert!(status        .detail        .expect("a sentence")        .contains("is not on this Mac"));}#[test]fn anything_else_is_an_error_carrying_its_own_words() {    let status = classify_read_failure("database disk image is malformed", Path::new("/x/y.db"));    assert_eq!(status.state, SourceState::Error);    assert_eq!(        status.detail.as_deref(),        Some("database disk image is malformed")    );}#[test]fn an_io_error_is_classified_by_its_kind_not_by_reading_its_words() {    let path = Path::new("/x/y.db");    assert_eq!(        classify_io_failure(            &std::io::Error::new(std::io::ErrorKind::NotFound, "gone"),            path        )        .state,        SourceState::NotFound    );    assert_eq!(        classify_io_failure(            &std::io::Error::new(std::io::ErrorKind::PermissionDenied, "no"),            path        )        .state,        SourceState::NoPermission    );}#[test]fn only_a_permission_answer_means_nothing_should_retry() {    assert!(is_waiting_on_the_person(&SourceStatus::no_permission("x")));    for status in [        SourceStatus::ready(0),        SourceStatus::not_found("x"),        SourceStatus::error("x"),    ] {        assert!(!is_waiting_on_the_person(&status));    }}#[test]fn discarding_takes_the_sidecars_with_the_snapshot() {    let (dir, _layout) = temp_layout();    let path = dir.path().join("snap.db");    write(&path, b"db");    write(&sidecar(&path, "-wal"), b"wal");    write(&sidecar(&path, "-shm"), b"shm");    discard_snapshot(&path);    assert!(!path.exists());    assert!(!sidecar(&path, "-wal").exists());    assert!(!sidecar(&path, "-shm").exists());    // A second discard on a path that is already gone is not an error: a pass    // that failed before it copied still runs its cleanup.    discard_snapshot(&path);}