Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
41544f4d010000000a0000000100000001000000010000000100000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
019900000028b52ffd0058850400c248212220a5ce030307662162861441404b
b05d7329045b9fbced9f1bdb009060022202a9010d6b415d9b156b39a6e6ef84
1190428a6e86962aaa8d7e30665019eb494b0f64ffda5a25728de0cee66110b7
07f0056c9a206aba28bed449df51de613cbf27ee09f3f409019f069f54625ba4
fb2e193e64b633afc3b62737b769d2411962176473850200401812312b66020a
00000028b52ffd00580900000004b800000028b52ffd00587905003f6469643a
61746f6d69633a34354c5a5155495a4e583556464f3755585a48424f55344c4d
5542365536324e5a45554e3651503256505137414e4c4c54433251408ac892ac
7b9297f52f6e5009dfd89c6fbb727d67a3851176e262658997c97da4ef57bf96
66818fb314a792c8d94d34b1284657a543d845748e3bf5eea614e10b20141276
4afb5f3460f3a0e4f99ac6c5f0608d5fe4cc462f82fb8e4eb06ea3c013f09faf
af0d0765643235353139105300000028b52ffd00585502006403000300010000
00110005000001ffff030500110505ffff03050101050b05612e747874010007
0b00010b100210000110160301000016070060eb4ec23ac782d048dd017618c3
1c03305400000028b52ffd00585d0200f40301000005612e7478740103000000
000100000005616c70686100010101050b000100010462657461000102010b10
00020001000167616d6d6100010301101603009d12ab9c63ac285d2000000000
08c8575298d796b23e275133b1097d19ac2ebf0e01b26a58149f9e39299dc350
160000001f00000028b52ffd0058b10000612e747874616c7068610a62657461
0a67616d6d610a1412764afb5f3460f3a0e4f99ac6c5f0608d5fe4cc462f82fb
8e4eb06ea3c013
23 changes: 15 additions & 8 deletions atomic-repository/src/repository/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,21 @@ impl Repository {
if !active.insert(hash) {
return Err(RepositoryError::Apply("cyclic change dependency".into()));
}
let change = self.load_change(&hash)?;
if change
.hash()
.map_err(|e| RepositoryError::Apply(e.to_string()))?
!= hash
{
return Err(RepositoryError::Apply("change hash mismatch".into()));
}
// A change's identity is the hash of its stored encoding. One
// recorded before file-header version 2 gets a different hash when
// the current writer re-encodes it, so the stored bytes are what
// is checked against the requested hash.
let verified = self
.change_store
.load_verified_change(&hash, None)
.map_err(|e| match e {
ChangeStoreError::NotFound { hash } => RepositoryError::ChangeNotFound { hash },
ChangeStoreError::HashMismatch { .. } => {
RepositoryError::Apply("change hash mismatch".into())
}
other => RepositoryError::Database(other.to_string()),
})?;
let change = verified.change();
stack.push((hash, true));
for dep in change.dependencies().iter().rev() {
stack.push((*dep, false));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
use super::*;
use crate::apply::CrossViewInsertOptions;
use atomic_core::pristine::{CapabilityMutTxnT, MutTxnT, SPAN_RANGE_CAPABILITY};

/// A change recorded by the released atomic 0.19.1: it adds `a.txt` holding
/// `LEGACY_CONTENT`, depends on nothing, and is stored in ATOM file-header
/// version 1, as every change recorded before 0.20 is.
const LEGACY_FILE_ADD_HEX: &str = include_str!(
"../../../../atomic-core/src/change/format_v3/fixtures/atomic_0_19_1_file_add.hex"
);
const LEGACY_CONTENT: &[u8] = b"alpha\nbeta\ngamma\n";
const EDITED_CONTENT: &[u8] = b"alpha\nbeta\ngamma\nspan line\n";

fn legacy_file_add() -> (Hash, Vec<u8>) {
let hex: String = LEGACY_FILE_ADD_HEX
.chars()
.filter(|c| !c.is_ascii_whitespace())
.collect();
let bytes: Vec<u8> = (0..hex.len())
.step_by(2)
.map(|i| u8::from_str_radix(&hex[i..i + 2], 16).unwrap())
.collect();
let (change, hash) = Change::deserialize(&mut std::io::Cursor::new(&bytes)).unwrap();
assert_eq!(u32::from_le_bytes(bytes[4..8].try_into().unwrap()), 1);
assert_ne!(
change.hash().unwrap(),
hash,
"the current writer gives a version 1 change a different hash"
);
(hash, bytes)
}

fn record_all(repo: &TestRepository, message: &str) -> Hash {
*repo
.record(
ChangeHeader::new(message),
RecordOptions::new()
.with_all(true)
.save_to_store(true)
.apply_after_record(true)
.sync_vault(false)
.enrich_kg(false),
)
.unwrap()
.hash()
}

fn enable_spans(repo: &TestRepository) {
let mut txn = repo.pristine().write_txn().unwrap();
txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap();
txn.commit().unwrap();
}

/// A repository whose only history is the 0.19.1 change, checked out.
fn repo_with_legacy_history() -> (tempfile::TempDir, TestRepository, Hash) {
let (legacy, bytes) = legacy_file_add();
let (temp, repo) = create_temp_repo();
repo.import_change_bytes(&legacy, &bytes).unwrap();
repo.insert_change_rec(&legacy, InsertOptions::default())
.unwrap();
std::fs::write(temp.path().join("a.txt"), LEGACY_CONTENT).unwrap();
assert_eq!(content(&repo, "dev"), LEGACY_CONTENT);
(temp, repo, legacy)
}

fn content(repo: &TestRepository, view: &str) -> Vec<u8> {
repo.get_file_contents_on_view(&["a.txt".to_string()], view)
.unwrap()
.remove(0)
.expect("a.txt is present on the view")
}

/// The sender turns spans on and edits the file the 0.19.1 change added;
/// returns the span change, which depends on the 0.19.1 change.
fn record_span_edit(temp: &tempfile::TempDir, repo: &TestRepository, legacy: &Hash) -> Hash {
enable_spans(repo);
std::fs::write(temp.path().join("a.txt"), EDITED_CONTENT).unwrap();
let span_edit = record_all(repo, "span edit");
let change = repo.load_change(&span_edit).unwrap();
assert!(
change.hashed.span.is_some(),
"the edit must be a span change"
);
assert!(
change.dependencies().contains(legacy),
"the span change must depend on the 0.19.1 change"
);
span_edit
}

fn span_edit_bytes(repo: &TestRepository, hash: &Hash) -> Vec<u8> {
std::fs::read(repo.change_store().change_path(hash)).unwrap()
}

/// Pulling: another clone of the same history receives the span change. Its
/// dependency was recorded before 0.20, so its identity is the hash of its
/// original bytes, not of a re-encoding by the current writer.
#[test]
fn span_change_on_pre_0_20_history_inserts_into_another_clone() {
let (sender_dir, sender, legacy) = repo_with_legacy_history();
let span_edit = record_span_edit(&sender_dir, &sender, &legacy);
let bytes = span_edit_bytes(&sender, &span_edit);

for recursive in [false, true] {
let (_receiver_dir, receiver, _) = repo_with_legacy_history();
receiver.import_change_bytes(&span_edit, &bytes).unwrap();
if recursive {
receiver
.insert_change_rec(&span_edit, InsertOptions::default())
.unwrap();
} else {
receiver
.insert_change(&span_edit, InsertOptions::default())
.unwrap();
}
assert_eq!(content(&receiver, "dev"), EDITED_CONTENT);
}
}

/// Cloning: a fresh repository receives the whole history at once.
#[test]
fn span_change_and_its_pre_0_20_dependency_insert_into_an_empty_repository() {
let (sender_dir, sender, legacy) = repo_with_legacy_history();
let span_edit = record_span_edit(&sender_dir, &sender, &legacy);
let (_, legacy_bytes) = legacy_file_add();

let (_receiver_dir, receiver) = create_temp_repo();
receiver
.import_change_bytes(&legacy, &legacy_bytes)
.unwrap();
receiver
.import_change_bytes(&span_edit, &span_edit_bytes(&sender, &span_edit))
.unwrap();
receiver
.insert_change_rec(&span_edit, InsertOptions::default())
.unwrap();
assert_eq!(content(&receiver, "dev"), EDITED_CONTENT);
}

/// No remote involved: the span edit is recorded on one view and promoted to
/// another view of the same repository.
#[test]
fn span_change_on_pre_0_20_history_inserts_from_another_view() {
let (temp, mut repo, legacy) = repo_with_legacy_history();
repo.create_view_from("feature", "dev").unwrap();
repo.switch_view("feature").unwrap();
record_span_edit(&temp, &repo, &legacy);
assert_eq!(content(&repo, "dev"), LEGACY_CONTENT);

repo.switch_view("dev").unwrap();
repo.insert_from_view(CrossViewInsertOptions::new("feature", "dev"))
.unwrap();
assert_eq!(content(&repo, "dev"), EDITED_CONTENT);
}

/// The identity check still holds for the stored bytes: a dependency whose
/// file no longer decodes to its own hash must stop the insert.
#[test]
fn span_insert_still_refuses_a_dependency_stored_under_the_wrong_hash() {
let (sender_dir, sender, legacy) = repo_with_legacy_history();
let span_edit = record_span_edit(&sender_dir, &sender, &legacy);
let bytes = span_edit_bytes(&sender, &span_edit);

let (receiver_dir, receiver, _) = repo_with_legacy_history();
receiver.import_change_bytes(&span_edit, &bytes).unwrap();
// Replace the stored dependency with a different, valid change.
let other = create_test_change("not the dependency");
let mut other_bytes = Vec::new();
other.serialize(&mut other_bytes).unwrap();
std::fs::write(receiver.change_store().change_path(&legacy), other_bytes).unwrap();
drop(receiver);
let receiver = TestRepository::new(Repository::open(receiver_dir.path()).unwrap());

assert!(receiver
.insert_change_rec(&span_edit, InsertOptions::default())
.is_err());
}
1 change: 1 addition & 0 deletions atomic-repository/src/repository/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mod edit_tests;
mod history_tests;
mod init_tests;
mod integration_tests;
mod legacy_history_span_insert_tests;
mod materialize_fail_closed_tests;
mod merge_property_tests;
mod native_index_repair_tests;
Expand Down
Loading