1use onestore::{
2 Arena, ExGuid, RevisionIndex, Section, Store,
3 document::{Document, Kind},
4 op::{Edit, Op},
5 page::Page,
6 protected::{Error, Key, Limits, UnlockedSection, rekey},
7};
8use std::{fs, path::Path};
9
10/// The native fixtures with their passwords.
11fn fixtures() -> Vec<(Vec<u8>, String)> {
12 [
13 ("native-encrypted", "encrypted-01/notebook/synthetic.one"),
14 ("native-protected-boundaries", "notebook/synthetic.one"),
15 ]
16 .into_iter()
17 .map(|(root, notebook)| {
18 let root = Path::new("../../corpus").join(root);
19 let manifest: serde_json::Value =
20 serde_json::from_slice(&fs::read(root.join("manifest.json")).unwrap()).unwrap();
21 (
22 fs::read(root.join(notebook)).unwrap(),
23 manifest["password"].as_str().unwrap().to_owned(),
24 )
25 })
26 .collect()
27}
28
29/// Every page of a section `Section::open` or `Section::unlock` reads, in order.
30fn pages(section: &mut Section<'_>) -> Vec<(ExGuid, Page)> {
31 section
32 .pages()
33 .unwrap()
34 .into_iter()
35 .map(|(space, ..)| (space, section.page(space).unwrap()))
36 .collect()
37}
38
39fn objects(pages: &[(ExGuid, Page)]) -> Vec<&Vec<onestore::page::PageObject>> {
40 pages.iter().map(|(_, page)| &page.objects).collect()
41}
42
43#[test]
44fn known_passwords_open_independent_native_fixtures() {
45 for ((bytes, password), count) in fixtures().into_iter().zip([1, 11]) {
46 let store = Store::parse(&bytes).unwrap();
47 let index = RevisionIndex::parse(&store).unwrap();
48 assert!(Document::parse(&index).unwrap().pages().unwrap().is_empty());
49 let unlocked = UnlockedSection::open(&index, &password, Limits::default()).unwrap();
50 let document = unlocked.document().unwrap();
51 assert_eq!(document.pages().unwrap().len(), count);
52 assert!(
53 document
54 .spaces
55 .values()
56 .flat_map(|s| s.revisions.values())
57 .flat_map(|r| r.nodes.values())
58 .all(|n| !matches!(n.kind, Kind::Encrypted { .. }))
59 );
60 // A section opened under its key reads as the document does.
61 let arena = Arena::default();
62 assert!(Section::open(&arena, bytes.clone()).is_err());
63 let key = Key::open(&bytes, &password).unwrap();
64 let mut section = Section::unlock(&arena, bytes.clone(), &key).unwrap();
65 let read = pages(&mut section);
66 assert_eq!(read.len(), count);
67 for (space, page) in &read {
68 assert_eq!(*page, Page::from_space(&document, *space).unwrap());
69 }
70 assert!(matches!(
71 Key::open(&bytes, "deliberately incorrect fixture password"),
72 Err(Error::PasswordMismatch)
73 ));
74 }
75}
76
77#[test]
78fn limits_and_password_bytes_are_explicit() {
79 let (bytes, password) = fixtures().remove(1);
80 let store = Store::parse(&bytes).unwrap();
81 let index = RevisionIndex::parse(&store).unwrap();
82 for limits in [
83 Limits {
84 kdf_rounds: 0,
85 ..Limits::default()
86 },
87 Limits {
88 decoded_bytes: 0,
89 ..Limits::default()
90 },
91 Limits {
92 object_visits: 0,
93 ..Limits::default()
94 },
95 ] {
96 assert!(matches!(
97 UnlockedSection::open(&index, &password, limits),
98 Err(Error::Limit)
99 ));
100 }
101 for changed in [
102 password.replace("e\u{301}", "é"),
103 format!("{password}\n"),
104 password.to_uppercase(),
105 ] {
106 assert!(matches!(
107 Key::open(&bytes, &changed),
108 Err(Error::PasswordMismatch)
109 ));
110 }
111 assert!(matches!(
112 Key::open(&bytes, &"x".repeat(65537)),
113 Err(Error::Limit)
114 ));
115 let ordinary = onestore::create_section("ordinary.one", "Fictitious", "Author").unwrap();
116 assert!(matches!(
117 Key::open(&ordinary, &password),
118 Err(Error::Unsupported)
119 ));
120 // A key opens only the section whose encryption data it came from.
121 let other = Key::new(&password).unwrap();
122 assert!(matches!(
123 Section::unlock(&Arena::default(), bytes, &other),
124 Err(Error::PasswordMismatch)
125 ));
126}
127
128/// Appends an attachment and text to the first text paragraph of `page`; none without one.
129fn edit(space: ExGuid, page: &Page) -> Option<(Edit, Page)> {
130 use onestore::page::{Attachment, PageObject, Paragraph, ParagraphContent};
131 let mut edited = page.clone();
132 let paragraphs = edited.objects.iter_mut().find_map(|object| match object {
133 PageObject::Outline(outline) if outline.paragraphs.iter().any(|p| p.text().is_some()) => {
134 Some(&mut outline.paragraphs)
135 }
136 _ => None,
137 })?;
138 let at = paragraphs.iter().position(|p| p.text().is_some()).unwrap();
139 let mut file = paragraphs[at].clone();
140 file.id = onestore::page::text::new_id().unwrap();
141 file.lists.clear();
142 file.tags.clear();
143 file.style = None;
144 file.format = Default::default();
145 file.content = ParagraphContent::Attachment(Attachment {
146 id: onestore::page::text::new_id().unwrap(),
147 filename: "sealed.txt".into(),
148 source_path: None,
149 size: Some([24.0, 24.0]),
150 layout: Default::default(),
151 bytes: Some(std::sync::Arc::from(&b"A payload that stays sealed"[..])),
152 preview: None,
153 recording: None,
154 tags: Vec::new(),
155 });
156 paragraphs.insert(at + 1, file);
157 let text = paragraphs[at].text_mut().unwrap();
158 let format = text.text.format_at(0).unwrap().clone();
159 text.text
160 .append(Paragraph::new(" still protected".to_owned(), format))
161 .unwrap();
162 let ops = onestore::op::lower_page(page, &edited).unwrap();
163 let edit = Edit {
164 at: 134_000_000_000_000_000,
165 ops: ops.into_iter().map(|op| Op::Page { space, op }).collect(),
166 };
167 Some((edit, edited))
168}
169
170/// Every revision of every space of `bytes` names its key, as MS-ONESTORE 2.5.19 asks.
171fn keyed(bytes: &[u8]) -> bool {
172 let store = Store::parse(bytes).unwrap();
173 let index = RevisionIndex::parse(&store).unwrap();
174 index
175 .spaces
176 .values()
177 .flat_map(|space| space.revisions.values())
178 .all(|revision| revision.encrypted && revision.nodes[0].id == 0x7c)
179}
180
181/// The first text paragraph of every page gains text and an attachment; each seal appends
182/// one revision to that page alone, stores none of it in the clear, and reads back as the
183/// edited model under the same password.
184#[test]
185fn a_protected_page_edit_is_stored_under_the_section_key() {
186 for (mut bytes, password) in fixtures() {
187 let key = Key::open(&bytes, &password).unwrap();
188 let mut expected =
189 pages(&mut Section::unlock(&Arena::default(), bytes.clone(), &key).unwrap());
190 let mut edited = 0;
191 for (space, page) in &mut expected {
192 let Some((edit, after)) = edit(*space, page) else {
193 continue;
194 };
195 *page = after;
196 edited += 1;
197 let arena = Arena::default();
198 let mut section = Section::unlock(&arena, bytes.clone(), &key).unwrap();
199 section.apply("Rust", &edit).unwrap();
200 let transaction = section.seal().unwrap().unwrap();
201 let mut written = bytes.clone();
202 transaction.apply(&mut written).unwrap();
203 assert_eq!(written, section.image());
204 for clear in [&b" still protected"[..], b"stays sealed"] {
205 let utf16: Vec<u8> = String::from_utf8_lossy(clear)
206 .encode_utf16()
207 .flat_map(u16::to_le_bytes)
208 .collect();
209 for clear in [clear, &utf16[..]] {
210 assert!(!written.windows(clear.len()).any(|w| w == clear));
211 }
212 }
213 let revisions = |bytes: &[u8]| {
214 let store = Store::parse(bytes).unwrap();
215 let index = RevisionIndex::parse(&store).unwrap();
216 index
217 .spaces
218 .iter()
219 .map(|(id, space)| (*id, space.revisions.len()))
220 .collect::<Vec<_>>()
221 };
222 let grown: Vec<_> = revisions(&bytes)
223 .into_iter()
224 .zip(revisions(&written))
225 .filter(|(before, after)| before != after)
226 .map(|(before, _)| before.0)
227 .collect();
228 assert_eq!(grown, [*space]);
229 // The section kept open reads its own seal back.
230 assert_eq!(section.page(*space).unwrap().objects, page.objects);
231 bytes = written;
232 }
233 assert!(edited > 0);
234 let stored = pages(&mut Section::unlock(&Arena::default(), bytes.clone(), &key).unwrap());
235 assert_eq!(objects(&stored), objects(&expected));
236 // The document reader agrees, under the password alone.
237 let store = Store::parse(&bytes).unwrap();
238 let index = RevisionIndex::parse(&store).unwrap();
239 let unlocked = UnlockedSection::open(&index, &password, Limits::default()).unwrap();
240 let document = unlocked.document().unwrap();
241 for (space, page) in &expected {
242 assert_eq!(
243 Page::from_space(&document, *space).unwrap().objects,
244 page.objects
245 );
246 }
247 }
248}
249
250/// The first edit of a page Snowbound made adds its metadata root in a dependent revision
251/// under the key, which the password alone reads back.
252#[test]
253fn a_protected_first_edit_declares_its_root_in_a_dependent_revision() {
254 let plain = onestore::create_section("first.one", "Original", "Author").unwrap();
255 let key = Key::new("password").unwrap();
256 let mut bytes = rekey(&plain, None, Some(&key)).unwrap();
257 let arena = Arena::default();
258 let mut section = Section::unlock(&arena, bytes.clone(), &key).unwrap();
259 let (space, page) = pages(&mut section).remove(0);
260 let (edit, after) = edit(space, &page).unwrap();
261 section.apply("Rust", &edit).unwrap();
262 let transaction = section.seal().unwrap().unwrap();
263 transaction.apply(&mut bytes).unwrap();
264 let store = Store::parse(&bytes).unwrap();
265 let index = RevisionIndex::parse(&store).unwrap();
266 let revision = &index.spaces[&space].revisions[&index.active(space).unwrap()];
267 assert!(revision.encrypted && revision.dependency.is_some());
268 assert!(revision.nodes.iter().any(|node| node.id == 0x5a));
269 let unlocked = UnlockedSection::open(&index, "password", Limits::default()).unwrap();
270 let document = unlocked.document().unwrap();
271 assert_eq!(
272 Page::from_space(&document, space).unwrap().objects,
273 after.objects
274 );
275}
276
277/// OneNote's revisions that depend on an earlier one carry no key node of their own.
278#[test]
279fn a_native_revision_inherits_the_key_of_its_dependency() {
280 let bytes = fs::read("../../corpus/protected-edit/native-after/synthetic.one").unwrap();
281 let (_, password) = fixtures().remove(0);
282 let store = Store::parse(&bytes).unwrap();
283 let index = RevisionIndex::parse(&store).unwrap();
284 assert!(index.spaces.values().any(|space| {
285 space.revisions.values().any(|revision| {
286 revision.encrypted && revision.nodes.first().is_none_or(|node| node.id != 0x7c)
287 })
288 }));
289 let key = Key::open(&bytes, &password).unwrap();
290 let read = pages(&mut Section::unlock(&Arena::default(), bytes, &key).unwrap());
291 assert!(format!("{:?}", read[0].1.objects).contains("Native edit after Rust."));
292}
293
294/// Setting, changing and removing a password rewrites the section under new identities,
295/// keeping every page, version and payload; each password opens only its own image.
296#[test]
297fn a_password_is_set_changed_and_removed() {
298 let plain = fs::read("../../corpus/page-versions/native/step-09/notebook/History.one").unwrap();
299 let arena = Arena::default();
300 let mut section = Section::open(&arena, plain.clone()).unwrap();
301 let before = pages(&mut section);
302 let versions = section.versions().unwrap();
303 assert!(!versions.is_empty());
304 let identity = |bytes: &[u8]| {
305 let store = Store::parse(bytes).unwrap();
306 (
307 store.header.file_id,
308 RevisionIndex::parse(&store).unwrap().root,
309 )
310 };
311
312 let first = Key::new("first password").unwrap();
313 let protected = rekey(&plain, None, Some(&first)).unwrap();
314 assert!(keyed(&protected));
315 assert_eq!(protected[128..148], plain[128..148]);
316 assert_ne!(identity(&protected).0, identity(&plain).0);
317 assert_ne!(identity(&protected).1, identity(&plain).1);
318 assert!(Section::open(&Arena::default(), protected.clone()).is_err());
319 // The encryption data OneNote writes, which the password alone opens again.
320 let reopened = Key::open(&protected, "first password").unwrap();
321 assert_eq!(reopened.secret(), first.secret());
322 assert!(matches!(
323 Key::open(&protected, "First password"),
324 Err(Error::PasswordMismatch)
325 ));
326 let arena = Arena::default();
327 let mut section = Section::unlock(&arena, protected.clone(), &reopened).unwrap();
328 assert_eq!(objects(&pages(&mut section)), objects(&before));
329 assert_eq!(
330 section
331 .versions()
332 .unwrap()
333 .iter()
334 .map(|(_, versions)| versions.len())
335 .collect::<Vec<_>>(),
336 versions
337 .iter()
338 .map(|(_, versions)| versions.len())
339 .collect::<Vec<_>>()
340 );
341 let store = Store::parse(&protected).unwrap();
342 let index = RevisionIndex::parse(&store).unwrap();
343 let unlocked = UnlockedSection::open(&index, "first password", Limits::default()).unwrap();
344 assert_eq!(
345 unlocked.document().unwrap().pages().unwrap().len(),
346 before.len()
347 );
348
349 let second = Key::new("second password").unwrap();
350 assert!(matches!(
351 rekey(&protected, None, Some(&second)),
352 Err(Error::PasswordMismatch)
353 ));
354 let changed = rekey(&protected, Some(&first), Some(&second)).unwrap();
355 assert_ne!(first.secret(), second.secret());
356 assert!(matches!(
357 Key::open(&changed, "first password"),
358 Err(Error::PasswordMismatch)
359 ));
360 assert!(matches!(
361 Section::unlock(&Arena::default(), changed.clone(), &first),
362 Err(Error::PasswordMismatch)
363 ));
364 let arena = Arena::default();
365 let mut section = Section::unlock(&arena, changed.clone(), &second).unwrap();
366 assert_eq!(objects(&pages(&mut section)), objects(&before));
367
368 let removed = rekey(&changed, Some(&second), None).unwrap();
369 assert!(
370 RevisionIndex::parse(&Store::parse(&removed).unwrap())
371 .unwrap()
372 .spaces
373 .values()
374 .flat_map(|space| space.revisions.values())
375 .all(|revision| !revision.encrypted)
376 );
377 let arena = Arena::default();
378 let mut section = Section::open(&arena, removed).unwrap();
379 assert_eq!(objects(&pages(&mut section)), objects(&before));
380 assert_eq!(section.versions().unwrap().len(), versions.len());
381}
382
383/// Payloads survive a password of their own: every native attachment boundary length.
384#[test]
385fn payloads_survive_a_new_password() {
386 let (bytes, password) = fixtures().remove(1);
387 let key = Key::open(&bytes, &password).unwrap();
388 let before = pages(&mut Section::unlock(&Arena::default(), bytes.clone(), &key).unwrap());
389 let new = Key::new("another").unwrap();
390 let changed = rekey(&bytes, Some(&key), Some(&new)).unwrap();
391 let after = pages(&mut Section::unlock(&Arena::default(), changed, &new).unwrap());
392 assert_eq!(objects(&after), objects(&before));
393}
394
395/// What OneNote 2010 wrote setting, changing and removing a password in the lab, and its edit
396/// of a section Snowbound protected, edited and gave another password, each read here.
397#[test]
398fn onenote_protected_changed_and_removed_sections_read_back() {
399 let root = Path::new("../../corpus/protected-sections");
400 let manifest: serde_json::Value =
401 serde_json::from_slice(&fs::read(root.join("manifest.json")).unwrap()).unwrap();
402 let plain = fs::read(root.join("source/synthetic.one")).unwrap();
403 let titles = |section: &mut Section<'_>| -> Vec<String> {
404 section
405 .pages()
406 .unwrap()
407 .into_iter()
408 .map(|(_, title, _)| title)
409 .collect()
410 };
411 let expected = titles(&mut Section::open(&Arena::default(), plain).unwrap());
412 assert_eq!(expected.len(), 2);
413 for (file, password) in manifest["native"].as_object().unwrap() {
414 let bytes = fs::read(root.join("native").join(file)).unwrap();
415 let arena = Arena::default();
416 let mut section = match password.as_str() {
417 Some(password) => {
418 let key = Key::open(&bytes, password).unwrap();
419 assert!(Section::open(&Arena::default(), bytes.clone()).is_err());
420 Section::unlock(&arena, bytes, &key).unwrap()
421 }
422 None => Section::open(&arena, bytes).unwrap(),
423 };
424 let read = titles(&mut section);
425 assert_eq!(read.len(), 2, "{file}");
426 if file.starts_with("snowbound") {
427 let (space, ..) = section.pages().unwrap()[0];
428 let page = format!("{:?}", section.page(space).unwrap().objects);
429 assert!(page.contains("typed by OneNote"), "{file}");
430 } else {
431 assert_eq!(read, expected, "{file}");
432 }
433 }
434}