flpdf-cli 0.2.0

Command-line tools for flpdf, mirroring qpdf inspection and rewrite subcommands.
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! `--qdf` flag and `qdf-fix` subcommand integration tests (flpdf-9hc.6.8).
//!
//! Pins the CLI wiring for QDF:
//!  - `flpdf rewrite --qdf` produces canonical QDF (uncompressed, normalized,
//!    `%QDF-1.0`, `%% Original object ID:`, classic `xref` table, no XRef /
//!    ObjStm stream, indirect `/Length H 0 R` with a holder object).
//!  - Top-level `flpdf --qdf` behaves identically to `rewrite --qdf`.
//!  - The `qdf` subcommand is a byte-for-byte alias of `rewrite --qdf`
//!    (modulo the random trailer `/ID`, which neither path makes
//!    deterministic — see the `/ID`-line normalization in the alias test).
//!  - `qdf-fix` repairs a hand-edited stream's `/Length` holder.
//!  - Conflict diagnostics: `--qdf` + `--object-streams=generate|disable`
//!    is non-fatal (stderr diagnostic, ObjStm still off); `--qdf` +
//!    `--linearize` is fatal (exit 1).
//!
//! qpdf is used only as an external `--check` oracle and follows the same
//! skip policy as `cli_object_streams_qpdf_parity.rs`: hard-fail on CI,
//! soft-skip locally.

use assert_cmd::Command;
use predicates::prelude::*;
use std::io::Write;
use std::process::Command as ShellCommand;

// ---------------------------------------------------------------------------
// qpdf availability guard (same policy as cli_object_streams_qpdf_parity.rs)
// ---------------------------------------------------------------------------

fn qpdf_available() -> bool {
    ShellCommand::new("qpdf")
        .arg("--version")
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false)
}

/// Returns `true` when the caller should return early (qpdf missing and skip
/// allowed). Panics on CI where qpdf is a hard dependency.
#[must_use]
fn skip_if_qpdf_missing() -> bool {
    if qpdf_available() {
        return false;
    }
    let on_ci = std::env::var_os("CI").is_some();
    if on_ci {
        panic!(
            "qpdf is required for cli_qdf tests on CI; install qpdf \
             in the workflow before running this test suite"
        );
    }
    eprintln!(
        "skipping: qpdf not available (target_os={}, CI={})",
        std::env::consts::OS,
        on_ci
    );
    true
}

/// `qpdf --check <path>` exit status.
fn qpdf_check(path: &std::path::Path) -> std::process::ExitStatus {
    ShellCommand::new("qpdf")
        .args(["--check", path.to_str().unwrap()])
        .status()
        .expect("failed to spawn qpdf")
}

// ---------------------------------------------------------------------------
// Fixture: a minimal single-page PDF with one content stream (so the QDF
// indirect `/Length H 0 R` holder is exercised).
// ---------------------------------------------------------------------------

fn fixture_with_stream() -> tempfile::NamedTempFile {
    let mut fixture = tempfile::NamedTempFile::new().unwrap();

    let objects: Vec<Vec<u8>> = vec![
        b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n".to_vec(),
        b"2 0 obj\n<< /Type /Pages /Count 1 /Kids [3 0 R] >>\nendobj\n".to_vec(),
        // `/Resources` is a required (inheritable) Page attribute; qpdf 12.x
        // warns and bumps `qpdf --check` to exit 3 without it (qpdf 11.x was
        // silent), so the empty (but valid) resources dict is spelled out.
        b"3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842] /Resources << >> /Contents 4 0 R >>\nendobj\n"
            .to_vec(),
        b"4 0 obj\n<< /Length 9 >>\nstream\nHello PDF\nendstream\nendobj\n".to_vec(),
    ];

    let mut bytes = b"%PDF-1.7\n".to_vec();
    let mut offsets = Vec::with_capacity(objects.len() + 1);
    for object in &objects {
        offsets.push(bytes.len() as u32);
        bytes.extend_from_slice(object);
    }

    let start_xref = bytes.len();
    bytes.extend_from_slice(format!("xref\n0 {}\n", objects.len() + 1).as_bytes());
    bytes.extend_from_slice(format!("{:010} 65535 f\n", 0).as_bytes());
    for &offset in &offsets {
        bytes.extend_from_slice(format!("{:010} 00000 n \n", offset).as_bytes());
    }
    bytes.extend_from_slice(
        format!(
            "trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{}\n%%EOF\n",
            objects.len() + 1,
            start_xref
        )
        .as_bytes(),
    );

    fixture.as_file_mut().write_all(&bytes).unwrap();
    fixture
}

/// Assert the bytes look like canonical QDF.
fn assert_canonical_qdf(rendered: &[u8]) {
    let contains = |needle: &[u8]| rendered.windows(needle.len()).any(|w| w == needle);

    assert!(contains(b"%QDF-1.0"), "expected %QDF-1.0 header marker");
    assert!(
        contains(b"%% Original object ID:"),
        "expected %% Original object ID: comments"
    );
    assert!(
        contains(b"\nxref\n"),
        "expected a classic `xref` table keyword"
    );
    assert!(!contains(b"/Type /XRef"), "QDF must not use an xref stream");
    assert!(
        !contains(b"/Type /ObjStm"),
        "QDF must not use object streams"
    );
    // Indirect stream length: `/Length H 0 R` (not an inline integer).
    let has_indirect_length = rendered
        .windows(b"/Length ".len())
        .enumerate()
        .filter(|(_, w)| *w == b"/Length ")
        .any(|(i, _)| {
            let tail = &rendered[i + b"/Length ".len()..];
            // first non-digit run then ` 0 R`
            let mut j = 0;
            while j < tail.len() && tail[j].is_ascii_digit() {
                j += 1;
            }
            j > 0 && tail[j..].starts_with(b" 0 R")
        });
    assert!(
        has_indirect_length,
        "expected an indirect `/Length H 0 R` stream length"
    );
}

/// Strip the line containing the trailer `/ID [...]` so two QDF outputs that
/// differ only by the per-process random `/ID` compare equal. The `qdf`
/// subcommand keeps its back-compat arg set (no `--static-id`), so raw-byte
/// identity is not achievable by flag alignment.
fn strip_id_line(bytes: &[u8]) -> Vec<u8> {
    let mut out = Vec::with_capacity(bytes.len());
    for line in bytes.split_inclusive(|&b| b == b'\n') {
        if line.windows(4).any(|w| w == b"/ID ") {
            continue;
        }
        out.extend_from_slice(line);
    }
    out
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[test]
fn rewrite_qdf_produces_canonical_qdf() {
    let input = fixture_with_stream();
    let temp = tempfile::tempdir().unwrap();
    let output = temp.path().join("out.pdf");

    Command::cargo_bin("flpdf")
        .unwrap()
        .args([
            "rewrite",
            "--qdf",
            input.path().to_str().unwrap(),
            output.to_str().unwrap(),
        ])
        .assert()
        .success();

    let rendered = std::fs::read(&output).unwrap();
    assert_canonical_qdf(&rendered);

    if skip_if_qpdf_missing() {
        return;
    }
    assert!(
        qpdf_check(&output).success(),
        "qpdf --check should pass on rewrite --qdf output"
    );
}

#[test]
fn top_level_qdf_matches_rewrite_qdf() {
    let input = fixture_with_stream();
    let temp = tempfile::tempdir().unwrap();
    let rewrite_out = temp.path().join("rewrite.pdf");
    let toplevel_out = temp.path().join("toplevel.pdf");

    Command::cargo_bin("flpdf")
        .unwrap()
        .args([
            "rewrite",
            "--qdf",
            input.path().to_str().unwrap(),
            rewrite_out.to_str().unwrap(),
        ])
        .assert()
        .success();

    Command::cargo_bin("flpdf")
        .unwrap()
        .args([
            "--qdf",
            input.path().to_str().unwrap(),
            toplevel_out.to_str().unwrap(),
        ])
        .assert()
        .success();

    let a = strip_id_line(&std::fs::read(&rewrite_out).unwrap());
    let b = strip_id_line(&std::fs::read(&toplevel_out).unwrap());
    assert_eq!(
        a, b,
        "top-level --qdf must be identical to `rewrite --qdf` (modulo /ID)"
    );
    assert_canonical_qdf(&std::fs::read(&toplevel_out).unwrap());
}

#[test]
fn qdf_subcommand_is_alias_of_rewrite_qdf() {
    let input = fixture_with_stream();
    let temp = tempfile::tempdir().unwrap();
    let rewrite_out = temp.path().join("rewrite.pdf");
    let subcmd_out = temp.path().join("sub.pdf");

    Command::cargo_bin("flpdf")
        .unwrap()
        .args([
            "rewrite",
            "--qdf",
            input.path().to_str().unwrap(),
            rewrite_out.to_str().unwrap(),
        ])
        .assert()
        .success();

    Command::cargo_bin("flpdf")
        .unwrap()
        .args([
            "qdf",
            input.path().to_str().unwrap(),
            subcmd_out.to_str().unwrap(),
        ])
        .assert()
        .success();

    let a = strip_id_line(&std::fs::read(&rewrite_out).unwrap());
    let b = strip_id_line(&std::fs::read(&subcmd_out).unwrap());
    assert_eq!(
        a, b,
        "`qdf` subcommand must be a byte-for-byte alias of `rewrite --qdf` \
         (modulo the per-process random trailer /ID)"
    );
}

#[test]
fn qdf_fix_repairs_hand_edited_stream_length() {
    let input = fixture_with_stream();
    let temp = tempfile::tempdir().unwrap();
    let qdf_out = temp.path().join("qdf.pdf");
    let edited = temp.path().join("edited.pdf");
    let fixed = temp.path().join("fixed.pdf");

    Command::cargo_bin("flpdf")
        .unwrap()
        .args([
            "rewrite",
            "--qdf",
            input.path().to_str().unwrap(),
            qdf_out.to_str().unwrap(),
        ])
        .assert()
        .success();

    // Hand-edit the stream payload: grow "Hello PDF" -> "Hello PDF EDITED!!!"
    // The QDF indirect /Length holder still says the old length until fixed.
    // QDF form always inserts a newline before `endstream` (qpdf --qdf
    // parity), regardless of the caller's `newline_before_endstream` setting,
    // so `endstream` sits on its own line.
    let original = std::fs::read(&qdf_out).unwrap();
    let edited_bytes = {
        let from = b"stream\nHello PDF\nendstream".as_slice();
        let to = b"stream\nHello PDF EDITED!!!\nendstream".as_slice();
        let pos = original
            .windows(from.len())
            .position(|w| w == from)
            .expect("stream payload present in QDF output");
        let mut v = original[..pos].to_vec();
        v.extend_from_slice(to);
        v.extend_from_slice(&original[pos + from.len()..]);
        v
    };
    std::fs::write(&edited, &edited_bytes).unwrap();

    Command::cargo_bin("flpdf")
        .unwrap()
        .args(["qdf-fix", edited.to_str().unwrap(), fixed.to_str().unwrap()])
        .assert()
        .success();

    if skip_if_qpdf_missing() {
        return;
    }
    // The hand-edited (unfixed) file has a stale /Length and fails qpdf;
    // after qdf-fix the indirect length holder is corrected and it passes.
    // NOTE: qpdf emits "WARNING ... expected endstream / recovered stream
    // length" lines on the *pre-fix* (intentionally corrupt) file. Those
    // warnings are expected here and are not a test failure.
    assert!(
        !qpdf_check(&edited).success(),
        "the hand-edited (unfixed) QDF should fail qpdf --check"
    );
    assert!(
        qpdf_check(&fixed).success(),
        "qdf-fix output should pass qpdf --check"
    );
}

#[test]
fn qdf_object_streams_generate_emits_diagnostic_and_proceeds() {
    let input = fixture_with_stream();
    let temp = tempfile::tempdir().unwrap();
    let output = temp.path().join("out.pdf");

    Command::cargo_bin("flpdf")
        .unwrap()
        .args([
            "rewrite",
            "--qdf",
            "--object-streams=generate",
            input.path().to_str().unwrap(),
            output.to_str().unwrap(),
        ])
        .assert()
        .success()
        .stderr(predicate::str::contains(
            "--qdf forces object streams off; ignoring --object-streams=generate",
        ));

    let rendered = std::fs::read(&output).unwrap();
    assert!(
        !rendered
            .windows(b"/Type /ObjStm".len())
            .any(|w| w == b"/Type /ObjStm"),
        "QDF must not emit ObjStm even with --object-streams=generate"
    );
}

#[test]
fn qdf_object_streams_disable_emits_diagnostic() {
    let input = fixture_with_stream();
    let temp = tempfile::tempdir().unwrap();
    let output = temp.path().join("out.pdf");

    Command::cargo_bin("flpdf")
        .unwrap()
        .args([
            "rewrite",
            "--qdf",
            "--object-streams=disable",
            input.path().to_str().unwrap(),
            output.to_str().unwrap(),
        ])
        .assert()
        .success()
        .stderr(predicate::str::contains(
            "--qdf forces object streams off; ignoring --object-streams=disable",
        ));
}

#[test]
fn qdf_linearize_is_rejected_rewrite() {
    let input = fixture_with_stream();
    let temp = tempfile::tempdir().unwrap();
    let output = temp.path().join("out.pdf");

    Command::cargo_bin("flpdf")
        .unwrap()
        .args([
            "rewrite",
            "--qdf",
            "--linearize",
            input.path().to_str().unwrap(),
            output.to_str().unwrap(),
        ])
        .assert()
        .failure()
        .code(1)
        .stderr(predicate::str::contains(
            "--qdf and --linearize cannot be used together",
        ));
}

#[test]
fn qdf_linearize_is_rejected_top_level() {
    let input = fixture_with_stream();
    let temp = tempfile::tempdir().unwrap();
    let output = temp.path().join("out.pdf");

    Command::cargo_bin("flpdf")
        .unwrap()
        .args([
            "--qdf",
            "--linearize",
            input.path().to_str().unwrap(),
            output.to_str().unwrap(),
        ])
        .assert()
        .failure()
        .code(1)
        .stderr(predicate::str::contains(
            "--qdf and --linearize cannot be used together",
        ));
}

/// Regression for roborev #194: top-level `--qdf` combined with a page
/// operation must be rejected (exit 1) rather than silently emitting a
/// non-QDF document.
#[test]
fn qdf_page_ops_is_rejected_top_level() {
    let input = fixture_with_stream();
    let temp = tempfile::tempdir().unwrap();
    let output = temp.path().join("out.pdf");

    Command::cargo_bin("flpdf")
        .unwrap()
        .args([
            "--qdf",
            "--rotate=+90",
            input.path().to_str().unwrap(),
            output.to_str().unwrap(),
        ])
        .assert()
        .failure()
        .code(1)
        .stderr(predicate::str::contains(
            "--qdf cannot be combined with --pages/--rotate/--split-pages",
        ));
}