Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_core/tests/stamp.rs

2.0 KiB, 1 run

created by r1870400018:76258, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1use oxedyne_fe2o3_core::file::{
2 stamp,
3 stamped,
4};
5
6
7const PREFIX: &str = "# ore-unit";
8
9/// **A stamp detects any changed byte.** A stamped text matches its stamp; changing, adding or
10/// removing any one byte, after the line or in it, makes it a hand edit; a text whose first line is
11/// not the prefix and a space carries no stamp at all.
12#[test]
13fn a_stamp_detects_any_changed_byte() {
14 let body = "[Service]\nMemoryMax=1536M\nNice=10\n";
15 let text = stamp(body, PREFIX);
16 assert!(text.starts_with("# ore-unit sha256:"), "{}", text);
17 assert_eq!(text.lines().next().map(|l| l.len()), Some(PREFIX.len() + 8 + 64));
18 assert!(text.ends_with(body));
19 assert_eq!(stamped(&text, PREFIX), Some(true));
20 // The digest is SHA-256 of the body alone: the empty body's is the published one.
21 assert_eq!(
22 stamp("", PREFIX),
23 "# ore-unit sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n",
24 );
25 // Every byte, flipped in turn, line and body alike.
26 let bytes = text.as_bytes();
27 for i in PREFIX.len() + 1..bytes.len() {
28 let mut b = bytes.to_vec();
29 b[i] = match b[i] {
30 b'0' => b'1',
31 _ => b'0',
32 };
33 let changed = String::from_utf8_lossy(&b).to_string();
34 assert_eq!(stamped(&changed, PREFIX), Some(false), "byte {} changed and still matched", i);
35 }
36 // Added and removed bytes.
37 assert_eq!(stamped(&fmt_plus(&text, "# by hand\n"), PREFIX), Some(false));
38 assert_eq!(stamped(&text[..text.len() - 1], PREFIX), Some(false));
39 let line = text.lines().next().unwrap_or("");
40 assert_eq!(stamped(line, PREFIX), Some(false));
41 // No stamp.
42 assert_eq!(stamped(body, PREFIX), None);
43 assert_eq!(stamped("", PREFIX), None);
44 assert_eq!(stamped(&text.replacen("# ore-unit ", "# ore-unitx", 1), PREFIX), None);
45 assert_eq!(stamped(&text, "# other"), None);
46}
47
48fn fmt_plus(a: &str, b: &str) -> String {
49 let mut s = a.to_string();
50 s.push_str(b);
51 s
52}