1//! A section written anew under new identities (`protected::rekey`), as a password, a move or
2//! a copy into another notebook writes one: every page, version and conflict page comes along,
3//! each under the identities OneNote 2010 derives from its own.
4
5use onestore::{
6 Arena, ExGuid, RevisionIndex, Section, Store,
7 document::{Document, Kind},
8 page::Page,
9 protected::rekey,
10};
11
12fn read(path: &str) -> Vec<u8> {
13 std::fs::read(format!(
14 "{}/../../corpus/{path}",
15 env!("CARGO_MANIFEST_DIR")
16 ))
17 .unwrap()
18}
19
20/// A page as a reader sees it: its title, its versions' authors and its conflict pages'
21/// titles and users.
22type Seen = (String, Vec<Option<String>>, Vec<(String, String)>);
23
24/// What a reader sees of a section's pages, in order.
25fn seen(image: &[u8]) -> Vec<Seen> {
26 let arena = Arena::default();
27 let mut section = Section::open(&arena, image.to_vec()).unwrap();
28 let versions = section.versions().unwrap();
29 let conflicts = section.conflicts().unwrap();
30 let mut seen = Vec::new();
31 for (space, title, _) in section.pages().unwrap() {
32 let page: Page = section.page(space).unwrap();
33 assert_eq!(page.title, title);
34 let versions: Vec<Option<String>> = (versions.iter())
35 .filter(|(listed, _)| *listed == space)
36 .flat_map(|(_, versions)| versions)
37 .map(|version| {
38 section.version(space, version.context).unwrap();
39 version.author.clone()
40 })
41 .collect();
42 let conflicts = (conflicts.iter())
43 .filter(|(listed, _)| *listed == space)
44 .flat_map(|(_, conflicts)| conflicts)
45 .map(|conflict| {
46 section.page(conflict.space).unwrap();
47 (conflict.title.clone(), conflict.user.clone())
48 })
49 .collect();
50 seen.push((title, versions, conflicts));
51 }
52 seen
53}
54
55/// The identity OneNote 2010 gives the metadata of the page in object space `space`.
56fn metadata(space: ExGuid) -> ExGuid {
57 let salt = [
58 0x31, 0xc0, 0xa8, 0x22, 0, 0x36, 0xee, 0x42, 0xb7, 0x14, 0xd7, 0xac, 0xda, 0x24, 0x35, 0xe8,
59 ];
60 let mut guid = space.guid;
61 guid.iter_mut()
62 .zip(salt)
63 .for_each(|(byte, salt)| *byte ^= salt);
64 ExGuid { guid, n: 1 }
65}
66
67/// The page metadata the section's page list holds, each with the page space it names.
68fn listed(image: &[u8]) -> Vec<(ExGuid, ExGuid)> {
69 let store = Store::parse(image).unwrap();
70 let index = RevisionIndex::parse(&store).unwrap();
71 let document = Document::parse(&index).unwrap();
72 let revision = document.active(document.root).unwrap();
73 let spaces: Vec<ExGuid> = index.spaces.keys().copied().collect();
74 (revision.nodes.iter())
75 .filter(|(_, node)| matches!(node.kind, Kind::Metadata { .. }))
76 .map(|(id, _)| {
77 let space = spaces.iter().find(|space| metadata(**space) == *id);
78 (*id, *space.unwrap_or(&ExGuid::default()))
79 })
80 .collect()
81}
82
83fn rewritten_whole(path: &str) {
84 rewritten(path, read(path));
85}
86
87fn rewritten(path: &str, image: Vec<u8>) {
88 let rewritten = rekey(&image, None, None).unwrap();
89 let before = seen(&image);
90 assert!(!before.is_empty());
91 assert_eq!(seen(&rewritten), before, "{path}");
92 let store = |image: &[u8]| Store::parse(image).unwrap().header.file_id;
93 assert_ne!(store(&rewritten), store(&image));
94 for image in [&image, &rewritten] {
95 let listed = listed(image);
96 assert!(!listed.is_empty(), "{path}");
97 for (id, space) in listed {
98 assert_ne!(
99 space,
100 ExGuid::default(),
101 "{path}: metadata {id} names no page"
102 );
103 }
104 }
105}
106
107#[test]
108fn versions_come_along() {
109 rewritten_whole("page-versions/native/step-02/notebook/History.one");
110}
111
112#[test]
113fn conflict_pages_come_along() {
114 rewritten_whole("conflict-page/native-three/notebook/synthetic.one");
115}
116
117#[test]
118fn a_section_snowbound_made_comes_along() {
119 let mut image = onestore::create_empty_section("Made.one", None).unwrap();
120 let page = onestore::PageCreation::new(None, Some("Made"), "Author").unwrap();
121 let edit = onestore::op::Edit {
122 at: 134_040_000_000_000_000,
123 ops: vec![onestore::op::Op::Section(onestore::op::SectionOp::Create(
124 page,
125 ))],
126 };
127 let arena = Arena::default();
128 let mut section = Section::open(&arena, image.clone()).unwrap();
129 section.apply("Author", &edit).unwrap();
130 section.seal().unwrap().unwrap().apply(&mut image).unwrap();
131 rewritten("Made.one", image);
132}