enprot 0.5.52

Engyon Protected Text (EPT) — confidentiality processor and capability ledger
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use crate::Fixture;
use assert_cmd::prelude::*;
use predicates::prelude::*;
use std::fs;
use std::process::Command;

#[test]
fn help_produces_usage() {
    Command::cargo_bin("enprot")
        .unwrap()
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("Usage:"));
    Command::cargo_bin("enprot")
        .unwrap()
        .arg("-h")
        .assert()
        .success()
        .stdout(predicate::str::contains("Usage:"));
}

#[test]
fn success_on_no_operation() {
    let ept = Fixture::copy("sample/test.ept");
    Command::cargo_bin("enprot")
        .unwrap()
        .arg("passthrough")
        .arg(&ept.path)
        .assert()
        .success();
    // file should be unchanged
    assert_eq!(
        &fs::read_to_string(&ept.source).unwrap(),
        &fs::read_to_string(&ept.path).unwrap()
    );
}

#[test]
fn verbosity() {
    let ept = Fixture::copy("sample/test.ept");
    Command::cargo_bin("enprot")
        .unwrap()
        .arg("passthrough")
        .arg(&ept.path)
        .assert()
        .success()
        .stderr(predicate::str::contains("LEFT_SEP").not());
    Command::cargo_bin("enprot")
        .unwrap()
        .arg("-v")
        .arg("passthrough")
        .arg(&ept.path)
        .assert()
        .success()
        .stderr(predicate::str::contains("LEFT_SEP"));
    Command::cargo_bin("enprot")
        .unwrap()
        .arg("--verbose")
        .arg("passthrough")
        .arg(&ept.path)
        .assert()
        .success()
        .stderr(predicate::str::contains("LEFT_SEP"));
    // file should be unchanged
    assert_eq!(
        &fs::read_to_string(&ept.source).unwrap(),
        &fs::read_to_string(&ept.path).unwrap()
    );
}

#[test]
fn output() {
    let ept = Fixture::copy("sample/test.ept");
    let output = Fixture::blank("out.ept");
    Command::cargo_bin("enprot")
        .unwrap()
        .arg("encrypt")
        .arg("--cipher")
        .arg("aes-256-siv")
        .arg("-w")
        .arg("Agent_007")
        .arg("--pbkdf")
        .arg("legacy")
        .arg("-k")
        .arg("Agent_007=password")
        .arg(&ept.path)
        .arg("-o")
        .arg(&output.path)
        .assert()
        .success();
    // original file should be unchanged
    assert_eq!(
        &fs::read_to_string(&ept.source).unwrap(),
        &fs::read_to_string(&ept.path).unwrap()
    );
    assert_eq!(
        &fs::read_to_string(&output.path).unwrap(),
        &fs::read_to_string("test-data/test-encrypt-agent007.ept").unwrap()
    );
}

#[test]
fn output_multiple() {
    let ept1 = Fixture::copy("sample/test.ept");
    let ept2 = Fixture::copy("sample/simple.ept");
    let ept3 = Fixture::copy("sample/simple.ept");
    let out1 = Fixture::blank("out1.ept");
    let out2 = Fixture::blank("out2.ept");
    Command::cargo_bin("enprot")
        .unwrap()
        .arg("encrypt")
        .arg("--cipher")
        .arg("aes-256-siv")
        .arg("-w")
        .arg("Agent_007")
        .arg("--pbkdf")
        .arg("legacy")
        .arg("-k")
        .arg("Agent_007=password")
        .arg(&ept1.path)
        .arg("-o")
        .arg(&out1.path)
        .arg(&ept2.path)
        .arg("-o")
        .arg(&out2.path)
        .arg(&ept3.path)
        .assert()
        .success();
    // these originals should be unchanged
    assert_eq!(
        &fs::read_to_string(&ept1.source).unwrap(),
        &fs::read_to_string(&ept1.path).unwrap()
    );
    assert_eq!(
        &fs::read_to_string(&ept2.source).unwrap(),
        &fs::read_to_string(&ept2.path).unwrap()
    );
    // these two have outputs specified
    assert_eq!(
        &fs::read_to_string(&out1.path).unwrap(),
        &fs::read_to_string("test-data/test-encrypt-agent007.ept").unwrap()
    );
    assert_eq!(
        &fs::read_to_string(&out2.path).unwrap(),
        &fs::read_to_string("test-data/simple-encrypt-agent007.ept").unwrap()
    );
    // no output specified for this one, so the input is the output
    assert_eq!(
        &fs::read_to_string(&ept3.path).unwrap(),
        &fs::read_to_string("test-data/simple-encrypt-agent007.ept").unwrap()
    );
}

#[test]
fn jobs_zero_rejected() {
    let ept = Fixture::copy("sample/test.ept");
    Command::cargo_bin("enprot")
        .unwrap()
        .arg("--jobs")
        .arg("0")
        .arg("passthrough")
        .arg(&ept.path)
        .assert()
        .failure()
        .stderr(predicate::str::contains("must be at least 1"));
}

/// Regression for the typed ConfigIssue gate (TODO.complete/33).
/// `--fips` + explicit `--policy default` must fail with the new
/// typed message, and the message must mention both flags so the
/// user sees the conflict in one read.
#[test]
fn fips_policy_conflict_typed_message() {
    let ept = Fixture::copy("sample/test.ept");
    Command::cargo_bin("enprot")
        .unwrap()
        .arg("--fips")
        .arg("--policy")
        .arg("default")
        .arg("passthrough")
        .arg(&ept.path)
        .assert()
        .failure()
        .stderr(
            predicates::str::contains("--fips forces --policy=nist")
                .and(predicates::str::contains("--policy=default")),
        );
}

/// The signer-without-anchor warning should not block execution —
/// passthrough still produces output. This locks in the warning-only
/// severity that ConfigIssue::SignerWithoutAnchor carries.
#[test]
fn signer_without_anchor_is_warning_only() {
    let ept = Fixture::copy("sample/test.ept");
    // Generate a real ed25519 keypair so the signer path is exercised
    // end-to-end. The warning fires at validate time, before the key
    // is loaded; passthrough doesn't load it regardless.
    let dir = tempfile::tempdir().unwrap();
    let priv_pem = dir.path().join("priv.pem");
    Command::cargo_bin("enprot")
        .unwrap()
        .args(["keygen", "ed25519", "--out-priv"])
        .arg(&priv_pem)
        .assert()
        .success();
    Command::cargo_bin("enprot")
        .unwrap()
        .arg("--signer")
        .arg(&priv_pem)
        .arg("passthrough")
        .arg(&ept.path)
        .assert()
        .success()
        .stderr(predicates::str::contains("warning:"));
}

/// `enprot sbom` (TODO.complete/62): valid SPDX JSON on stdout with
/// the full embedded dependency tree; `--output` writes the file.
#[test]
fn sbom_spdx_json_shape() {
    use std::process::Command;
    let out = Command::cargo_bin("enprot")
        .unwrap()
        .args(["sbom"])
        .output()
        .unwrap();
    assert!(out.status.success());
    let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert_eq!(v["spdxVersion"], "SPDX-2.3");
    assert_eq!(v["SPDXID"], "SPDXRef-DOCUMENT");
    let pkgs = v["packages"].as_array().unwrap();
    assert!(
        pkgs.len() > 100,
        "full lockfile tree expected, got {pkgs_len}",
        pkgs_len = pkgs.len()
    );
    let names: Vec<&str> = pkgs.iter().filter_map(|p| p["name"].as_str()).collect();
    assert!(names.contains(&"enprot"));
    assert!(names.contains(&"botan"));
    assert!(names.contains(&"librnp"));
    // Every non-self package is the target of exactly one DEPENDS_ON.
    let rels = v["relationships"].as_array().unwrap();
    assert!(rels.iter().any(|r| r["relationshipType"] == "DESCRIBES"));
}

#[test]
fn sbom_cyclonedx_and_output_file() {
    use std::process::Command;
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("sbom.json");
    let out = Command::cargo_bin("enprot")
        .unwrap()
        .args(["sbom", "--sbom-format", "cyclonedx-json", "--output"])
        .arg(&path)
        .output()
        .unwrap();
    assert!(out.status.success());
    let v: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
    assert_eq!(v["bomFormat"], "CycloneDX");
    assert_eq!(v["specVersion"], "1.5");
    assert_eq!(v["metadata"]["component"]["name"], "enprot");
}

/// --audit-log records one JSONL line per invocation, and `enprot
/// audit query` filters them (TODO.complete/63).
#[test]
fn audit_log_records_and_queries() {
    use std::process::Command;
    let dir = tempfile::tempdir().unwrap();
    let log = dir.path().join("audit.jsonl");
    let ept = Fixture::copy("sample/test.ept");

    for (word, file) in [("W1", "a.ept"), ("W2", "b.ept")] {
        let _ = Command::cargo_bin("enprot")
            .unwrap()
            .args(["--audit-log"])
            .arg(&log)
            .args(["passthrough", "-w", word])
            .arg(&ept.path)
            .arg("-o")
            .arg(dir.path().join(file))
            .output()
            .unwrap();
    }

    let text = std::fs::read_to_string(&log).unwrap();
    let lines: Vec<&str> = text.lines().collect();
    assert_eq!(lines.len(), 2, "{text}");
    for l in &lines {
        assert!(l.contains(r#""type":"record""#), "{l}");
        assert!(l.contains(r#""op":"passthrough""#), "{l}");
    }
    assert!(lines[0].contains(r#""words":["W1"]"#), "got: {}", lines[0]);

    // Query by word returns exactly the matching line.
    let out = Command::cargo_bin("enprot")
        .unwrap()
        .args(["audit", "query", "--log"])
        .arg(&log)
        .args(["--word", "W2"])
        .output()
        .unwrap();
    assert!(out.status.success());
    let s = String::from_utf8_lossy(&out.stdout);
    assert_eq!(s.lines().count(), 1);
    assert!(s.contains(r#""words":["W2"]"#));
}

/// Signed audit records verify; tampering with a record is detected
/// and `audit verify` exits non-zero.
#[test]
fn audit_sign_and_verify_tamper_evidence() {
    use std::process::Command;
    let dir = tempfile::tempdir().unwrap();
    let log = dir.path().join("audit.jsonl");
    let priv_pem = dir.path().join("priv.pem");
    let pub_pem = dir.path().join("pub.pem");
    Command::cargo_bin("enprot")
        .unwrap()
        .args(["keygen", "ed25519", "--out-priv"])
        .arg(&priv_pem)
        .args(["--out-pub"])
        .arg(&pub_pem)
        .assert()
        .success();

    let ept = Fixture::copy("sample/simple.ept");
    let _ = Command::cargo_bin("enprot")
        .unwrap()
        .args(["--audit-log"])
        .arg(&log)
        .args(["--signer"])
        .arg(&priv_pem)
        .args(["passthrough"])
        .arg(&ept.path)
        .arg("-o")
        .arg(dir.path().join("out.ept"))
        .output()
        .unwrap();

    // Record + signature lines present; verify passes.
    let text = std::fs::read_to_string(&log).unwrap();
    assert!(text.contains(r#""type":"signature""#));
    let ok = Command::cargo_bin("enprot")
        .unwrap()
        .args(["audit", "verify", "--log"])
        .arg(&log)
        .args(["--trust-root"])
        .arg(&pub_pem)
        .output()
        .unwrap();
    assert!(
        ok.status.success(),
        "{}",
        String::from_utf8_lossy(&ok.stderr)
    );

    // Tamper: flip the recorded op. Verify must fail.
    let tampered = text.replace("passthrough", "passthru");
    std::fs::write(&log, tampered).unwrap();
    let bad = Command::cargo_bin("enprot")
        .unwrap()
        .args(["audit", "verify", "--log"])
        .arg(&log)
        .args(["--trust-root"])
        .arg(&pub_pem)
        .output()
        .unwrap();
    assert!(!bad.status.success(), "tampered log must not verify");
}

/// `--streaming` output must be byte-identical to the default
/// in-memory path for passthrough and for an encrypt/decrypt round
/// trip (TODO.complete/35).
#[test]
fn streaming_matches_default_path() {
    use std::process::Command;
    let dir = tempfile::tempdir().unwrap();
    let ept = Fixture::copy("sample/test.ept");

    let run = |streaming: bool, args: &[&str], out: &std::path::Path| {
        let mut cmd = Command::cargo_bin("enprot").unwrap();
        if streaming {
            cmd.arg("--streaming");
        }
        assert!(
            cmd.args(args)
                .arg(&ept.path)
                .arg("-o")
                .arg(out)
                .output()
                .unwrap()
                .status
                .success()
        );
    };

    // Passthrough.
    run(false, &["passthrough"], &dir.path().join("p1"));
    run(true, &["passthrough"], &dir.path().join("p2"));
    assert_eq!(
        std::fs::read(dir.path().join("p1")).unwrap(),
        std::fs::read(dir.path().join("p2")).unwrap()
    );

    // Encrypt + decrypt round trip entirely through the streaming path.
    run(
        true,
        &["encrypt", "-w", "Agent_007", "-k", "Agent_007=pw"],
        &dir.path().join("enc"),
    );
    let enc = std::fs::read_to_string(dir.path().join("enc")).unwrap();
    assert!(enc.contains("ENCRYPTED Agent_007"), "got: {enc}");
    let enc_path = dir.path().join("enc");
    let out = dir.path().join("dec");
    let ok = Command::cargo_bin("enprot")
        .unwrap()
        .arg("--streaming")
        .args(["decrypt", "-w", "Agent_007", "-k", "Agent_007=pw"])
        .arg(&enc_path)
        .arg("-o")
        .arg(&out)
        .output()
        .unwrap();
    assert!(
        ok.status.success(),
        "{}",
        String::from_utf8_lossy(&ok.stderr)
    );
    assert_eq!(
        std::fs::read_to_string(&out).unwrap(),
        std::fs::read_to_string(&ept.source).unwrap()
    );
}