systemd-journal-sdk-core 0.7.0

Core pure Rust systemd journal file reader and writer primitives
Documentation
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
use super::*;

#[test]
fn sealed_writer_basic_passes_stock_verify() {
    if !journalctl_available() {
        eprintln!("journalctl not available; skipping sealed writer stock verify");
        return;
    }
    let dir = TempDir::new().expect("create temp dir");
    let journal_dir = dir.path().join("journals");
    std::fs::create_dir_all(&journal_dir).expect("create journal dir");
    let path = journal_dir.join("system.journal");
    let repo_file =
        crate::repository::File::from_path(&path).expect("test journal path should parse");

    let seal = test_seal_opts();
    let mut journal_file = JournalFile::create(
        &repo_file,
        JournalFileOptions::new(test_uuid(1), test_uuid(2), test_uuid(3)).with_seal(seal.clone()),
    )
    .expect("create sealed journal");
    let mut writer = JournalWriter::new(&mut journal_file, 1, test_uuid(4)).expect("create writer");
    writer
        .add_entry(
            &mut journal_file,
            &[
                b"MESSAGE=hello sealed world".as_slice(),
                b"PRIORITY=6".as_slice(),
            ],
            1_500_000,
            100,
        )
        .expect("write sealed entry");
    journal_file.sync().expect("sync journal");

    let key = verification_key(&seal);
    let output = Command::new("journalctl")
        .arg("--verify")
        .arg("--verify-key")
        .arg(&key)
        .arg("--file")
        .arg(&path)
        .output()
        .expect("run journalctl verify");
    assert!(
        output.status.success(),
        "journalctl verify failed for sealed file: stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn sealed_writer_interval_crossing_passes_stock_verify() {
    if !journalctl_available() {
        eprintln!("journalctl not available; skipping interval crossing stock verify");
        return;
    }
    let dir = TempDir::new().expect("create temp dir");
    let journal_dir = dir.path().join("journals");
    std::fs::create_dir_all(&journal_dir).expect("create journal dir");
    let path = journal_dir.join("system.journal");
    let repo_file =
        crate::repository::File::from_path(&path).expect("test journal path should parse");

    let seal = test_seal_opts();
    let mut journal_file = JournalFile::create(
        &repo_file,
        JournalFileOptions::new(test_uuid(1), test_uuid(2), test_uuid(3)).with_seal(seal.clone()),
    )
    .expect("create sealed journal");
    let mut writer = JournalWriter::new(&mut journal_file, 1, test_uuid(4)).expect("create writer");

    // Entry in epoch 0 (realtime == start)
    writer
        .add_entry(
            &mut journal_file,
            &[b"MESSAGE=epoch0".as_slice()],
            1_000_000,
            100,
        )
        .expect("write epoch 0");
    // Entry in epoch 1 (crosses interval)
    writer
        .add_entry(
            &mut journal_file,
            &[b"MESSAGE=epoch1".as_slice()],
            2_000_000,
            200,
        )
        .expect("write epoch 1");
    // Entry in epoch 2
    writer
        .add_entry(
            &mut journal_file,
            &[b"MESSAGE=epoch2".as_slice()],
            3_000_000,
            300,
        )
        .expect("write epoch 2");
    journal_file.sync().expect("sync journal");

    let key = verification_key(&seal);
    let output = Command::new("journalctl")
        .arg("--verify")
        .arg("--verify-key")
        .arg(&key)
        .arg("--file")
        .arg(&path)
        .output()
        .expect("run journalctl verify");
    assert!(
        output.status.success(),
        "journalctl verify failed for interval-crossing sealed file: stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn sealed_writer_wrong_key_fails_stock_verify() {
    if !journalctl_available() {
        eprintln!("journalctl not available; skipping wrong key verify");
        return;
    }
    let dir = TempDir::new().expect("create temp dir");
    let journal_dir = dir.path().join("journals");
    std::fs::create_dir_all(&journal_dir).expect("create journal dir");
    let path = journal_dir.join("system.journal");
    let repo_file =
        crate::repository::File::from_path(&path).expect("test journal path should parse");

    let seal = test_seal_opts();
    let mut journal_file = JournalFile::create(
        &repo_file,
        JournalFileOptions::new(test_uuid(1), test_uuid(2), test_uuid(3)).with_seal(seal),
    )
    .expect("create sealed journal");
    let mut writer = JournalWriter::new(&mut journal_file, 1, test_uuid(4)).expect("create writer");
    writer
        .add_entry(
            &mut journal_file,
            &[b"MESSAGE=hello".as_slice()],
            1_500_000,
            100,
        )
        .expect("write sealed entry");
    journal_file.sync().expect("sync journal");

    let wrong_key = "000000000000000000000001/1-f4240";
    let output = Command::new("journalctl")
        .arg("--verify")
        .arg("--verify-key")
        .arg(wrong_key)
        .arg("--file")
        .arg(&path)
        .output()
        .expect("run journalctl verify with wrong key");
    assert!(
        !output.status.success(),
        "journalctl verify should fail with wrong key, but succeeded"
    );
}

#[test]
fn sealed_writer_tampered_data_fails_stock_verify() {
    if !journalctl_available() {
        eprintln!("journalctl not available; skipping tamper verify");
        return;
    }
    let dir = TempDir::new().expect("create temp dir");
    let journal_dir = dir.path().join("journals");
    std::fs::create_dir_all(&journal_dir).expect("create journal dir");
    let path = journal_dir.join("system.journal");
    let repo_file =
        crate::repository::File::from_path(&path).expect("test journal path should parse");

    let seal = test_seal_opts();
    let mut journal_file = JournalFile::create(
        &repo_file,
        JournalFileOptions::new(test_uuid(1), test_uuid(2), test_uuid(3)).with_seal(seal.clone()),
    )
    .expect("create sealed journal");
    let mut writer = JournalWriter::new(&mut journal_file, 1, test_uuid(4)).expect("create writer");
    writer
        .add_entry(
            &mut journal_file,
            &[b"MESSAGE=hello".as_slice()],
            1_500_000,
            100,
        )
        .expect("write sealed entry");
    journal_file.sync().expect("sync journal");

    // Tamper with a byte in the DATA object payload area
    use std::fs::OpenOptions;
    let mut f = OpenOptions::new()
        .write(true)
        .open(&path)
        .expect("open for tamper");
    write_test_bytes_at(&mut f, &[0xff], 512).expect("tamper write");
    drop(f);

    let key = verification_key(&seal);
    let output = Command::new("journalctl")
        .arg("--verify")
        .arg("--verify-key")
        .arg(&key)
        .arg("--file")
        .arg(&path)
        .output()
        .expect("run journalctl verify tampered");
    assert!(
        !output.status.success(),
        "journalctl verify should fail with tampered data, but succeeded"
    );
}

#[test]
fn unsealed_writer_does_not_set_sealed_flags() {
    let dir = TempDir::new().expect("create temp dir");
    let journal_dir = dir.path().join("journals");
    std::fs::create_dir_all(&journal_dir).expect("create journal dir");
    let path = journal_dir.join("system.journal");
    let repo_file =
        crate::repository::File::from_path(&path).expect("test journal path should parse");

    let journal_file: JournalFile<MmapMut> = JournalFile::create(
        &repo_file,
        JournalFileOptions::new(test_uuid(1), test_uuid(2), test_uuid(3)),
    )
    .expect("create unsealed journal");
    let header = journal_file.journal_header_ref();
    assert!(
        !header.has_compatible_flag(HeaderCompatibleFlags::Sealed),
        "unsealed writer set SEALED flag"
    );
    assert!(
        !header.has_compatible_flag(HeaderCompatibleFlags::SealedContinuous),
        "unsealed writer set SEALED_CONTINUOUS flag"
    );
}

#[test]
fn sealed_writer_first_entry_future_epoch_passes_stock_verify() {
    if !journalctl_available() {
        eprintln!("journalctl not available; skipping first-entry future-epoch stock verify");
        return;
    }
    let dir = TempDir::new().expect("create temp dir");
    let journal_dir = dir.path().join("journals");
    std::fs::create_dir_all(&journal_dir).expect("create journal dir");
    let path = journal_dir.join("system.journal");
    let repo_file =
        crate::repository::File::from_path(&path).expect("test journal path should parse");

    let seal = test_seal_opts();
    let mut journal_file = JournalFile::create(
        &repo_file,
        JournalFileOptions::new(test_uuid(1), test_uuid(2), test_uuid(3)).with_seal(seal.clone()),
    )
    .expect("create sealed journal");
    let mut writer = JournalWriter::new(&mut journal_file, 1, test_uuid(4)).expect("create writer");

    // Write the first entry at epoch 2 (realtime = start + 2 * interval = 3_000_000).
    // This exercises FSS epoch-evolution during the first-tag path.
    writer
        .add_entry(
            &mut journal_file,
            &[b"MESSAGE=future epoch first entry".as_slice()],
            3_000_000,
            100,
        )
        .expect("write first entry at future epoch");
    journal_file.sync().expect("sync journal");

    let key = verification_key(&seal);
    let output = Command::new("journalctl")
        .arg("--verify")
        .arg("--verify-key")
        .arg(&key)
        .arg("--file")
        .arg(&path)
        .output()
        .expect("run journalctl verify");
    assert!(
        output.status.success(),
        "journalctl verify failed for first-entry future-epoch sealed file: stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn sealed_writer_entry_before_start_rejected() {
    let dir = TempDir::new().expect("create temp dir");
    let journal_dir = dir.path().join("journals");
    std::fs::create_dir_all(&journal_dir).expect("create journal dir");
    let path = journal_dir.join("system.journal");
    let repo_file =
        crate::repository::File::from_path(&path).expect("test journal path should parse");

    let seal = test_seal_opts();
    let mut journal_file = JournalFile::create(
        &repo_file,
        JournalFileOptions::new(test_uuid(1), test_uuid(2), test_uuid(3)).with_seal(seal.clone()),
    )
    .expect("create sealed journal");
    let mut writer = JournalWriter::new(&mut journal_file, 1, test_uuid(4)).expect("create writer");

    // Stock verification rejects entries older than the first tag epoch,
    // so writers must reject this input instead of producing an invalid file.
    assert!(
        writer
            .add_entry(
                &mut journal_file,
                &[b"MESSAGE=before sealing start".as_slice()],
                500_000,
                100,
            )
            .is_err(),
        "expected before-start entry to be rejected"
    );
}

#[test]
fn sealed_writer_multi_interval_gap_passes_stock_verify() {
    if !journalctl_available() {
        eprintln!("journalctl not available; skipping multi-interval gap stock verify");
        return;
    }
    let dir = TempDir::new().expect("create temp dir");
    let journal_dir = dir.path().join("journals");
    std::fs::create_dir_all(&journal_dir).expect("create journal dir");
    let path = journal_dir.join("system.journal");
    let repo_file =
        crate::repository::File::from_path(&path).expect("test journal path should parse");

    let seal = test_seal_opts();
    let mut journal_file = JournalFile::create(
        &repo_file,
        JournalFileOptions::new(test_uuid(1), test_uuid(2), test_uuid(3)).with_seal(seal.clone()),
    )
    .expect("create sealed journal");
    let mut writer = JournalWriter::new(&mut journal_file, 1, test_uuid(4)).expect("create writer");

    writer
        .add_entry(
            &mut journal_file,
            &[b"MESSAGE=epoch0".as_slice()],
            1_000_000,
            100,
        )
        .expect("write epoch 0");
    writer
        .add_entry(
            &mut journal_file,
            &[b"MESSAGE=epoch5".as_slice()],
            6_000_000,
            200,
        )
        .expect("write epoch 5");
    journal_file.sync().expect("sync journal");

    let key = verification_key(&seal);
    let output = Command::new("journalctl")
        .arg("--verify")
        .arg("--verify-key")
        .arg(&key)
        .arg("--file")
        .arg(&path)
        .output()
        .expect("run journalctl verify");
    assert!(
        output.status.success(),
        "journalctl verify failed for multi-interval gap sealed file: stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn sealed_writer_unaligned_start_uses_systemd_epoch_boundary() {
    if !journalctl_available() {
        eprintln!("journalctl not available; skipping unaligned-start stock verify");
        return;
    }
    let dir = TempDir::new().expect("create temp dir");
    let journal_dir = dir.path().join("journals");
    std::fs::create_dir_all(&journal_dir).expect("create journal dir");
    let path = journal_dir.join("system.journal");
    let repo_file =
        crate::repository::File::from_path(&path).expect("test journal path should parse");

    let seal = SealOptions::new([0u8; 12], 1_000_000, 1_702_717);
    let mut journal_file = JournalFile::create(
        &repo_file,
        JournalFileOptions::new(test_uuid(1), test_uuid(2), test_uuid(3)).with_seal(seal.clone()),
    )
    .expect("create sealed journal");
    let mut writer = JournalWriter::new(&mut journal_file, 1, test_uuid(4)).expect("create writer");

    for (idx, realtime) in [1_702_717, 2_100_000, 2_800_000].into_iter().enumerate() {
        let payload = format!("MESSAGE=unaligned-start-{idx}");
        writer
            .add_entry(
                &mut journal_file,
                &[payload.as_bytes()],
                realtime,
                (idx + 1) as u64,
            )
            .expect("write unaligned-start entry");
    }
    journal_file.sync().expect("sync journal");

    let key = verification_key(&seal);
    let output = Command::new("journalctl")
        .arg("--verify")
        .arg("--verify-key")
        .arg(&key)
        .arg("--file")
        .arg(&path)
        .output()
        .expect("run journalctl verify");
    assert!(
        output.status.success(),
        "journalctl verify failed for unaligned-start sealed file: stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn sealed_writer_empty_file_passes_stock_verify() {
    if !journalctl_available() {
        eprintln!("journalctl not available; skipping empty sealed stock verify");
        return;
    }
    let dir = TempDir::new().expect("create temp dir");
    let journal_dir = dir.path().join("journals");
    std::fs::create_dir_all(&journal_dir).expect("create journal dir");
    let path = journal_dir.join("system.journal");
    let repo_file =
        crate::repository::File::from_path(&path).expect("test journal path should parse");

    let seal = test_seal_opts();
    let mut journal_file = JournalFile::create(
        &repo_file,
        JournalFileOptions::new(test_uuid(1), test_uuid(2), test_uuid(3)).with_seal(seal.clone()),
    )
    .expect("create sealed journal");
    let _writer = JournalWriter::new(&mut journal_file, 1, test_uuid(4)).expect("create writer");
    journal_file.sync().expect("sync journal");

    let key = verification_key(&seal);
    let output = Command::new("journalctl")
        .arg("--verify")
        .arg("--verify-key")
        .arg(&key)
        .arg("--file")
        .arg(&path)
        .output()
        .expect("run journalctl verify");
    assert!(
        output.status.success(),
        "journalctl verify failed for empty sealed file: stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn compact_sealed_writer_passes_stock_verify() {
    if !journalctl_available() {
        eprintln!("journalctl not available; skipping compact+sealed stock verify");
        return;
    }
    let dir = TempDir::new().expect("create temp dir");
    let journal_dir = dir.path().join("journals");
    std::fs::create_dir_all(&journal_dir).expect("create journal dir");
    let path = journal_dir.join("system.journal");
    let repo_file =
        crate::repository::File::from_path(&path).expect("test journal path should parse");

    let seal = test_seal_opts();
    let mut journal_file = JournalFile::create(
        &repo_file,
        JournalFileOptions::new(test_uuid(1), test_uuid(2), test_uuid(3))
            .with_compact(true)
            .with_seal(seal.clone()),
    )
    .expect("create compact sealed journal");
    let mut writer = JournalWriter::new(&mut journal_file, 1, test_uuid(4)).expect("create writer");
    writer
        .add_entry(
            &mut journal_file,
            &[
                b"MESSAGE=compact sealed entry".as_slice(),
                b"PRIORITY=6".as_slice(),
            ],
            1_500_000,
            100,
        )
        .expect("write compact sealed entry");
    journal_file.sync().expect("sync journal");

    let key = verification_key(&seal);
    let output = Command::new("journalctl")
        .arg("--verify")
        .arg("--verify-key")
        .arg(&key)
        .arg("--file")
        .arg(&path)
        .output()
        .expect("run journalctl verify");
    assert!(
        output.status.success(),
        "journalctl verify failed for compact+sealed file: stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}