Debian Codegraph @

tests/time.rs

raw
1 #[cfg(test)]
2 mod integration {
3 use std::path;
4
5 use assert_cmd::Command;
6 use predicates::prelude::*;
7
8 // The ed25519 detached signature on a-cypherpunks-manifesto.txt was
9 // created on 2018-08-14.
10 //
11 // The inline-signed (.pgp) and cleartext-signed (.csf) variants
12 // have a "not live until 2025-03-06" constraint.
13 //
14 // The SHA-1 detached signature was created around 2007 and requires
15 // a relaxed policy (e.g. --policy-as-of 2008) to verify.
16
17 const ED25519_KEYRING: &str =
18 "emmelie-dorothea-dina-samantha-awina-ed25519.pgp";
19 const ED25519_SIG: &str =
20 "a-cypherpunks-manifesto.txt.ed25519.sig";
21 const ED25519_FPR: &[u8] =
22 b"8E8C33FA4626337976D97978069C0C348DD82C19\n";
23
24 const SHA1_KEYRING: &str = "mrpast.key";
25 const SHA1_SIG: &str = "a-cypherpunks-manifesto.txt.sha1.sig";
26 const SHA1_FPR: &[u8] =
27 b"40A0E8E64DCC61D977DD86D7165F9B3B60351FBC\n";
28
29 const PLAINTEXT: &str = "a-cypherpunks-manifesto.txt";
30 const INLINE_MSG: &str = "a-cypherpunks-manifesto.pgp";
31 const CLEARTEXT_MSG: &str = "a-cypherpunks-manifesto.csf";
32
33 fn sqv() -> Command {
34 let mut cmd = Command::cargo_bin("sqv").expect("have sqv");
35 cmd.current_dir(path::Path::new("tests").join("data"));
36 cmd
37 }
38
39 #[test]
40 fn time() {
41 let policy = path::PathBuf::from(env!("CARGO_TARGET_TMPDIR"))
42 .join("time-test-empty-policy.toml");
43 std::fs::write(&policy, "").unwrap();
44
45 // --time just after the ed25519 signature date: succeeds.
46 sqv()
47 .args(&["--keyring", ED25519_KEYRING,
48 "--time", "2018-08-15",
49 ED25519_SIG, PLAINTEXT])
50 .assert()
51 .success()
52 .stdout(predicate::eq(ED25519_FPR));
53
54 // --time before the signature date: not-after defaults to
55 // ref_time, so the signature is "in the future" → fails.
56 sqv()
57 .args(&["--keyring", ED25519_KEYRING,
58 "--time", "2018-08-13",
59 ED25519_SIG, PLAINTEXT])
60 .assert()
61 .failure();
62
63 // Explicit --not-after overrides the --time default.
64 sqv()
65 .args(&["--keyring", ED25519_KEYRING,
66 "--time", "2018-08-15",
67 "--not-after", "2018-08-15",
68 ED25519_SIG, PLAINTEXT])
69 .assert()
70 .success()
71 .stdout(predicate::eq(ED25519_FPR));
72
73 // Without --time, the SHA-1 signature is rejected by current policy.
74 sqv()
75 .env("SEQUOIA_CRYPTO_POLICY", &policy)
76 .args(&["--keyring", SHA1_KEYRING,
77 SHA1_SIG, PLAINTEXT])
78 .assert()
79 .failure();
80
81 // --time in 2008 relaxes the policy so SHA-1 is accepted.
82 sqv()
83 .env("SEQUOIA_CRYPTO_POLICY", &policy)
84 .args(&["--keyring", SHA1_KEYRING,
85 "--time", "2008-08-03",
86 SHA1_SIG, PLAINTEXT])
87 .assert()
88 .success()
89 .stdout(predicate::eq(SHA1_FPR));
90
91 // Explicit --policy-as-of overrides the --time default.
92 sqv()
93 .env("SEQUOIA_CRYPTO_POLICY", &policy)
94 .args(&["--keyring", SHA1_KEYRING,
95 "--time", "2008-08-03",
96 "--policy-as-of", "2025-01-01",
97 SHA1_SIG, PLAINTEXT])
98 .assert()
99 .failure();
100
101 // Large negative relative offsets that resolve before the 2018
102 // signature regardless of when the suite runs, one per suffix.
103 for offset in &["-100y", "-1200m", "-5000w", "-40000d", "-4000000000s"] {
104 sqv()
105 .args(&["--keyring", ED25519_KEYRING,
106 "--time", offset,
107 ED25519_SIG, PLAINTEXT])
108 .assert()
109 .failure();
110 }
111
112 // Inline-signed message with --time after the liveness date.
113 // The message has a "not live until 2025-03-06" constraint.
114 let tmpdir = tempfile::tempdir().unwrap();
115 let auth = tmpdir.path().join("inline.txt");
116 sqv()
117 .args(&["--keyring", ED25519_KEYRING,
118 "--time", "2025-03-07",
119 "--output", &auth.display().to_string(),
120 "--message", INLINE_MSG])
121 .assert()
122 .success()
123 .stdout(predicate::eq(ED25519_FPR));
124 let reference = path::Path::new("tests").join("data")
125 .join(PLAINTEXT);
126 assert_eq!(std::fs::read(&auth).unwrap(),
127 std::fs::read(&reference).unwrap());
128
129 // Cleartext-signed message with --time after the liveness date.
130 let auth = tmpdir.path().join("cleartext.txt");
131 sqv()
132 .args(&["--keyring", ED25519_KEYRING,
133 "--time", "2025-03-07",
134 "--output", &auth.display().to_string(),
135 "--cleartext", CLEARTEXT_MSG])
136 .assert()
137 .success()
138 .stdout(predicate::eq(ED25519_FPR));
139 let auth = String::from_utf8(std::fs::read(&auth).unwrap())
140 .unwrap();
141 let reference = String::from_utf8(
142 std::fs::read(&reference).unwrap(),
143 ).unwrap();
144 assert_eq!(auth.trim_end(), reference.trim_end());
145 }
146 }