1 use crate::fs::*;
2 use crate::patch::*;
3 use crate::pristine::*;
4 use crate::record::*;
5 use crate::*;
6 
hash_mismatch(patch: &Patch3) -> Result<(), anyhow::Error>7 fn hash_mismatch(patch: &Patch3) -> Result<(), anyhow::Error> {
8 env_logger::try_init().unwrap_or(());
9 use crate::patch::*;
10 let mut buf = tempfile::NamedTempFile::new()?;
11 let mut h = patch.serialize(&mut buf)?;
12 match h {
13 crate::pristine::Hash::Blake3(ref mut h) => h[0] = h[0].wrapping_add(1),
14 _ => unreachable!(),
15 }
16 match Patch3::deserialize(buf.path().to_str().unwrap(), &h) {
17 Err(e) => {
18 let e = e.downcast();
19 if let Ok(Error::PatchHashMismatch { .. }) = e {
20 } else {
21 unreachable!()
22 }
23 }
24 _ => unreachable!(),
25 }
26 
27 let mut f = PatchFile::open(buf.path().to_str().unwrap())?;
28 assert_eq!(f.read_header()?, patch.header);
29 assert_eq!(f.read_dependencies()?, patch.dependencies);
30 assert_eq!(f.read_metadata()?, &patch.metadata[..]);
31 assert_eq!(f.read_changes()?, patch.changes);
32 Ok(())
33 }
34 
35 #[test]
hash_mism() -> Result<(), anyhow::Error>36 fn hash_mism() -> Result<(), anyhow::Error> {
37 env_logger::try_init().unwrap_or(());
38 
39 let contents = b"a\nb\nc\nd\ne\nf\n";
40 let mut repo = working_copy::memory::Memory::new();
41 let patches = patchstore::memory::Memory::new();
42 repo.add_file("file", contents.to_vec());
43 repo.add_file("file2", contents.to_vec());
44 
45 let mut env = pristine::sanakirja::Pristine::new_anon()?;
46 let mut txn = env.mut_txn_begin();
47 let branch = txn.open_or_create_branch("main")?;
48 let mut branch = branch.borrow_mut();
49 add_file(&mut txn, "file")?;
50 add_file(&mut txn, "file2")?;
51 
52 let mut state = Builder::new();
53 state
54 .record(
55 &mut txn,
56 Algorithm::Myers,
57 &mut branch,
58 &mut repo,
59 &patches,
60 "",
61 )
62 .unwrap();
63 let rec = state.finish();
64 let changes: Vec<_> = rec.actions
65 .into_iter()
66 .flat_map(|x| x.globalize(&txn).into_iter())
67 .collect();
68 info!("changes = {:?}", changes);
69 let patch0 = crate::patch::Patch3::make_patch(
70 &txn,
71 &branch,
72 changes,
73 rec.contents,
74 crate::patch::PatchHeader {
75 name: "test".to_string(),
76 authors: vec![],
77 description: None,
78 timestamp: chrono::Utc::now(),
79 },
80 Vec::new(),
81 );
82 
83 apply::apply_local_patch(&patches, &mut txn, &mut branch, &patch0, &rec.updatables)?;
84 
85 hash_mismatch(&patch0)?;
86 
87 
88 Ok(())
89 }
90