ostrya 0.19.1

Pure-Rust async reimplementation of the ostree repository library
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
#![forbid(unsafe_code)]

//! Composefs export tests.
//!
//! The `ostree` command exported the composefs fixture from the source tree
//! that the bare-user fixture commits. [`Repo::export_composefs`] over the
//! bare-user fixture commit must reproduce the golden image `tree.cfs` byte for
//! byte. The archive fixture holds the same commit, and its export must give
//! the same bytes.
//!
//! Each export must also give the fs-verity digest in the MANIFEST. The
//! fixture generator measured this digest with `composefs-info measure-file`
//! on the image of the `ostree` command. A second test writes the digest into
//! the metadata of a commit and reads it back.
//!
//! The tests check [`VerityPolicy::Disabled`] the same way against
//! `tree-noverity.cfs`. One more test shows that this policy reads no payload.
//! The test rewrites the bytes of a content object in place at the same
//! length. The `Disabled` image stays the same, and the `Computed` image
//! changes.
//!
//! Each golden check also exports through a file descriptor with
//! [`Repo::export_composefs_to`]. The file must hold the same bytes, and the
//! returned digest must equal the fs-verity digest of the file content. One
//! more test requires that [`Transaction::composefs_digest`] over the same tree
//! gives the recorded digest.
//!
//! Two tests build their own tree, which the running user owns. In the first
//! test, a `bare` and a `bare-user` repository hold the tree. The two
//! repositories export the same bytes and the same digest under each verity
//! policy.
//!
//! The `bare` export reads the owner, the mode, and the xattrs from the object
//! inode. The `bare-user` export reads them from `user.ostreemeta`. The mtime
//! is not an input to the image.
//!
//! In the second test, a `bare` repository seals its objects. The kernel
//! measures the image that the export writes with
//! [`ComposefsOptions::RECORDED`], and gets the digest that the commit records.
//!
//! The tests that compare against a golden image or against the recorded
//! digest skip if the composefs fixture is absent. The fixture generator
//! writes no composefs fixture if its `ostree` command has no composefs
//! support, or if `composefs-info` is not available.
//!
//! The export for a mount under [`VerityPolicy::Computed`] compares the
//! fs-verity digest of the image with the key of the commit. One test shows
//! that a commit with no key and a commit with the right key give the image of
//! the [`ComposefsOptions::RECORDED`] export, that a wrong key fails the export
//! after the write, and that a malformed key fails it before the write and
//! before the walk of the tree. A
//! second test shows that the export under [`VerityPolicy::Disabled`] does not
//! read the key.
//!
//! The tests that seal objects also skip if the file system has no fs-verity.
//! All other tests read no composefs fixture and always run.
//!
//! The tests of the entry hook read no image structure. They search the image
//! for unique byte strings, and they compare the image of a hook on one tree
//! with the image of a second tree that stores the same metadata.

mod common;

use std::os::fd::AsFd;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::sync::atomic::{AtomicUsize, Ordering};

use ostrya::{
    Checksum, CommitOptions, ComposefsEntryFn, ComposefsOptions, CreateOptions, DirMeta, EntryKind,
    Error, FileMeta, Metadata, MutableTree, Repo, RepoMode, Value, VerityPolicy,
};
use ostrya_composefs::FsVerityHasher;
use ostrya_core::{ObjectType, Xattrs, loose_path};
use ostrya_rt::block_on;

use common::composefs::{
    DIGEST_KEY, build_owned_source, commit_owned, composefs_dir, dict, key_dict, manifest_digest,
    new_repo, scratch_fixture_repo, verity_supported,
};
use common::{COMMIT, HELLO_TXT, TmpDir, fixture_repo};

/// The loose path of the content object `checksum` in the bare-user repository
/// at `repo_dir`. The object that the caller names is in the tree of `COMMIT`,
/// so the `Computed` image depends on its payload.
fn content_object(repo_dir: &Path, checksum: &str) -> PathBuf {
    repo_dir.join("objects").join(loose_path(
        &Checksum::from_hex(checksum).unwrap(),
        ObjectType::File,
        RepoMode::BareUser,
    ))
}

/// Flips one byte of the payload at `path` in place. The length of the object
/// and its `user.ostreemeta` attribute do not change.
fn rewrite_payload(path: &Path) {
    use std::io::Write;

    let mut bytes = std::fs::read(path).expect("read the object payload");
    assert!(!bytes.is_empty(), "the object has a payload to rewrite");
    bytes[0] ^= 0xff;
    let mut file = std::fs::OpenOptions::new()
        .write(true)
        .open(path)
        .expect("open the object for writing");
    file.write_all(&bytes).expect("rewrite the object payload");
    file.flush().expect("flush the rewritten object");
}

fn to_hex(bytes: &[u8]) -> String {
    bytes.iter().map(|b| format!("{b:02x}")).collect()
}

/// Exports the fixture commit under `verity`. The image must equal
/// `<stem>.cfs` byte for byte, and its fs-verity digest must equal the MANIFEST
/// value at `digest_key`. The fd form must give the same bytes and the same
/// digest.
///
/// Both the `bare-user` and the `archive` fixture hold the same commit, so
/// each export must equal the golden image from `bare-user`. The `ostree`
/// command 2026.1 writes the same image from `archive` (observed).
///
/// Skips if the golden fixture is absent.
fn check_export(stem: &str, digest_key: &str, verity: VerityPolicy) {
    for mode in ["bare-user", "archive"] {
        check_export_from(mode, stem, digest_key, verity);
    }
}

/// [`check_export`] over the fixture repository of `mode`.
fn check_export_from(mode: &str, stem: &str, digest_key: &str, verity: VerityPolicy) {
    let (Some(digest), Ok(golden)) = (
        manifest_digest(digest_key),
        std::fs::read(composefs_dir().join(format!("{stem}.cfs"))),
    ) else {
        eprintln!("composefs fixture {stem} absent; skipping");
        return;
    };

    let scratch = TmpDir::new("composefs-export-to");
    let image_path = scratch.path().join(format!("{stem}.cfs"));
    let repo_dir = fixture_repo(mode);
    let stem = format!("{mode} {stem}");
    let stem = stem.as_str();
    block_on(async {
        let repo = Repo::open(&repo_dir).await.unwrap();
        let commit = Checksum::from_hex(COMMIT).unwrap();
        let image = repo
            .export_composefs(&commit, &ComposefsOptions { verity }, None)
            .await
            .unwrap();

        assert_eq!(
            image.bytes.len(),
            golden.len(),
            "{stem}: image length {} != golden {}",
            image.bytes.len(),
            golden.len()
        );
        if let Some(pos) = image.bytes.iter().zip(&golden).position(|(a, b)| a != b) {
            panic!(
                "{stem}: image diverges from golden at byte {pos:#x}: got {:#04x}, want {:#04x}",
                image.bytes[pos], golden[pos]
            );
        }
        assert_eq!(
            to_hex(&image.fs_verity),
            digest,
            "{stem}: fs-verity digest mismatch"
        );

        let file = std::fs::File::create(&image_path).expect("create the image file");
        let streamed = repo
            .export_composefs_to(&commit, &ComposefsOptions { verity }, None, file.as_fd())
            .await
            .unwrap();
        drop(file);

        let written = std::fs::read(&image_path).expect("read the exported image back");
        assert_eq!(
            written, image.bytes,
            "{stem}: the fd form wrote different bytes than the buffer form"
        );
        assert_eq!(
            to_hex(&streamed),
            digest,
            "{stem}: the fd form returned a different digest"
        );
        assert_eq!(
            streamed,
            FsVerityHasher::hash(&written),
            "{stem}: the returned digest differs from the digest of the file"
        );
    });
}

#[test]
fn export_matches_golden_image_and_digest() {
    check_export("tree", "composefs_digest", VerityPolicy::Computed);
}

#[test]
fn noverity_export_matches_golden_image_and_digest() {
    check_export(
        "tree-noverity",
        "composefs_noverity_digest",
        VerityPolicy::Disabled,
    );
}

#[test]
fn stores_digest_in_commit_metadata() {
    let Some(digest) = manifest_digest("composefs_digest") else {
        eprintln!("composefs fixture absent; skipping");
        return;
    };

    // Copy the fixture repository, so that the transaction publishes into a
    // throwaway copy. The shared unpacked fixture stays unchanged.
    let scratch = TmpDir::new("composefs-meta");
    let dst = scratch_fixture_repo(&scratch, "bare-user");

    block_on(async {
        let repo = Repo::open(&dst).await.unwrap();
        let commit = Checksum::from_hex(COMMIT).unwrap();

        let txn = repo.transaction().await.unwrap();
        let new_commit = repo
            .commit_add_composefs_metadata(&txn, &commit)
            .await
            .unwrap();
        assert_ne!(
            new_commit, commit,
            "adding the digest metadata yields a distinct commit"
        );
        txn.commit().await.unwrap();

        let (obj, _) = repo.load_commit(&new_commit).await.unwrap();
        let value = obj
            .metadata
            .dict_get("ostree.composefs.digest.v0")
            .expect("ostree.composefs.digest.v0 present in the new commit");
        let (_, inner) = value.as_variant().expect("digest value is a variant");
        let bytes = inner.as_bytes().expect("digest is a byte array");
        assert_eq!(
            to_hex(bytes),
            digest,
            "stored digest equals the tool's recorded digest"
        );
    });
}

/// `Transaction::composefs_digest` gives the recorded digest of the fixture
/// tree. The transaction stages nothing, so each
/// object comes from the repository. The value equals the digest that the
/// buffered export returns.
#[test]
fn transaction_digest_matches_recorded_digest() {
    let Some(digest) = manifest_digest("composefs_digest") else {
        eprintln!("composefs fixture absent; skipping");
        return;
    };

    let repo_dir = fixture_repo("bare-user");
    block_on(async {
        let repo = Repo::open(&repo_dir).await.unwrap();
        let (tree, commit) = repo.read_commit(COMMIT).await.unwrap();

        let txn = repo.transaction().await.unwrap();
        let staged = txn.composefs_digest(&tree).await.unwrap();
        txn.abort().await.unwrap();

        assert_eq!(
            to_hex(&staged),
            digest,
            "the staged-tree digest equals the tool's recorded digest"
        );

        let image = repo
            .export_composefs(
                &commit,
                &ComposefsOptions {
                    verity: VerityPolicy::Computed,
                },
                None,
            )
            .await
            .unwrap();
        assert_eq!(
            staged, image.fs_verity,
            "the staged-tree digest equals the exported image's digest"
        );
    });
}

/// Seals each regular `.file` object under `repo_dir/objects` with the
/// parameters that the `ostree` command uses, and returns the number of sealed
/// objects. If the file system refuses the first seal, returns `None`, so that
/// the caller skips.
fn seal_file_objects(repo_dir: &Path) -> Option<usize> {
    let mut sealed = 0;
    for fanout in std::fs::read_dir(repo_dir.join("objects")).unwrap() {
        let fanout = fanout.unwrap().path();
        if !fanout.is_dir() {
            continue;
        }
        for entry in std::fs::read_dir(&fanout).unwrap() {
            let path = entry.unwrap().path();
            let is_file_object = path.extension().is_some_and(|e| e == "file");
            if !is_file_object || !std::fs::symlink_metadata(&path).unwrap().is_file() {
                continue;
            }
            let ro = std::fs::File::open(&path).unwrap();
            match ostrya_sys::enable_verity(ro.as_fd()) {
                Ok(()) => sealed += 1,
                Err(e) if sealed == 0 => {
                    eprintln!("filesystem lacks fs-verity ({e})");
                    return None;
                }
                Err(e) => panic!("seal {}: {e}", path.display()),
            }
        }
    }
    Some(sealed)
}

/// In a bare-user repository with sealed objects,
/// `Transaction::composefs_digest` over the fixture tree gives the digest of
/// the unsealed fixture. It also gives the recorded digest.
///
/// In the sealed copy, the kernel supplies the digest of each backing object.
/// The unit test `every_sealed_fixture_object_yields_the_kernel_digest` in
/// `src/file.rs` shows that each sealed fixture object gives that digest.
///
/// Skips if the fixture is absent or if the file system has no fs-verity.
#[test]
fn sealed_repository_digest_matches_recorded_digest() {
    let Some(digest) = manifest_digest("composefs_digest") else {
        eprintln!("composefs fixture absent; skipping");
        return;
    };

    let scratch = TmpDir::new("composefs-sealed");
    let sealed_dir = scratch_fixture_repo(&scratch, "bare-user");
    let cfg = sealed_dir.join("config");
    let mut text = std::fs::read_to_string(&cfg).unwrap();
    text.push_str("[ex-integrity]\nfsverity=yes\n");
    std::fs::write(&cfg, text).unwrap();
    let Some(sealed) = seal_file_objects(&sealed_dir) else {
        eprintln!("skipping sealed-repository digest check");
        return;
    };
    assert!(sealed > 0, "the fixture holds content objects to seal");

    let digest_of = |repo_dir: PathBuf| async move {
        let repo = Repo::open(&repo_dir).await.unwrap();
        let (tree, _) = repo.read_commit(COMMIT).await.unwrap();
        let txn = repo.transaction().await.unwrap();
        let value = txn.composefs_digest(&tree).await.unwrap();
        txn.abort().await.unwrap();
        value
    };
    block_on(async {
        let from_sealed = digest_of(sealed_dir.clone()).await;
        let from_unsealed = digest_of(fixture_repo("bare-user")).await;
        assert_eq!(
            from_sealed, from_unsealed,
            "the sealed repository reaches the unsealed digest"
        );
        assert_eq!(
            to_hex(&from_sealed),
            digest,
            "the sealed repository reaches the tool's recorded digest"
        );
    });
}

/// The names in `xattrs` as text, for a failure message.
fn xattr_names(xattrs: &Xattrs) -> Vec<String> {
    xattrs
        .iter()
        .map(|(name, _)| String::from_utf8_lossy(name).into_owned())
        .collect()
}

/// The xattr set the walk records for `pairs` of (name, value).
fn expected_xattrs(pairs: &[(&str, &[u8])]) -> Xattrs {
    Xattrs::new(
        pairs
            .iter()
            .map(|(name, value)| (format!("{name}\0").into_bytes(), value.to_vec())),
    )
    .unwrap()
}

/// A `bare` and a `bare-user` repository hold one tree that the running user
/// owns. Under each verity policy, the two repositories export the same image
/// and the same digest.
///
/// The `bare` export reads the owner, the mode, and the xattrs of a file from
/// the object inode. The `bare-user` export reads them from `user.ostreemeta`.
/// A content object stores no mtime. The export sets the mtime of each inode to
/// 0, so the mtime is not an input to the image.
///
/// If the host adds xattrs to a new file, the commit or the xattr check fails.
#[test]
fn bare_and_bare_user_export_the_same_image() {
    use std::os::unix::fs::MetadataExt;

    let scratch = TmpDir::new("composefs-bare-pair");
    let base = scratch.path().join("tree");
    build_owned_source(&base);

    block_on(async {
        let bare = new_repo(&scratch.path().join("bare"), RepoMode::Bare, "").await;
        let user = new_repo(&scratch.path().join("bare-user"), RepoMode::BareUser, "").await;
        let commit = commit_owned(&bare, &base).await;
        assert_eq!(
            commit_owned(&user, &base).await,
            commit,
            "both repositories record the same commit"
        );

        let (commit_obj, _) = bare.load_commit(&commit).await.unwrap();
        let root = bare.load_dirtree(&commit_obj.root_dirtree).await.unwrap();
        let (_, sub_tree, sub_meta) = root
            .dirs
            .iter()
            .find(|(n, _, _)| n == "sub")
            .expect("the tree holds sub");
        let sub = bare.load_dirtree(sub_tree).await.unwrap();
        let files = [
            (
                "hello.txt",
                0o100644,
                expected_xattrs(&[("user.demo", b"value"), ("user.shared", b"s")]),
            ),
            (
                "readonly.txt",
                0o100444,
                expected_xattrs(&[("user.shared", b"s")]),
            ),
            ("run.sh", 0o100755, expected_xattrs(&[])),
            ("big.bin", 0o100644, expected_xattrs(&[])),
            ("empty", 0o100644, expected_xattrs(&[])),
            ("sub/nested.txt", 0o100600, expected_xattrs(&[])),
        ];
        for (name, mode, xattrs) in &files {
            let (tree, leaf) = match name.strip_prefix("sub/") {
                Some(leaf) => (&sub, leaf),
                None => (&root, *name),
            };
            let checksum = tree
                .files
                .iter()
                .find(|(n, _)| n == leaf)
                .map(|(_, c)| *c)
                .unwrap_or_else(|| panic!("the tree holds {name}"));
            let source = std::fs::symlink_metadata(base.join("src").join(name)).unwrap();
            for (label, repo) in [("bare", &bare), ("bare-user", &user)] {
                let file = repo.load_file(&checksum).await.unwrap();
                assert_eq!(
                    (file.uid, file.gid),
                    (source.uid(), source.gid()),
                    "{label} {name}: the owner is the owner of the source file"
                );
                assert_eq!(file.mode, *mode, "{label} {name}: mode");
                assert_eq!(
                    file.xattrs,
                    *xattrs,
                    "{label} {name}: xattrs {:?}, want {:?}",
                    xattr_names(&file.xattrs),
                    xattr_names(xattrs)
                );
            }
        }
        let dirs = [
            (
                "root",
                &commit_obj.root_dirmeta,
                0o040755,
                b"root".as_slice(),
            ),
            ("sub", sub_meta, 0o040750, b"sub".as_slice()),
        ];
        for (label, checksum, mode, value) in dirs {
            let dirmeta = bare.load_dirmeta(checksum).await.unwrap();
            let want = expected_xattrs(&[("user.dir", value)]);
            assert_eq!(
                dirmeta.xattrs,
                want,
                "{label} dirmeta: xattrs {:?}, want {:?}",
                xattr_names(&dirmeta.xattrs),
                xattr_names(&want)
            );
            assert_eq!(dirmeta.mode, mode, "{label} dirmeta: mode");
        }

        let mut images = Vec::new();
        for verity in [VerityPolicy::Computed, VerityPolicy::Disabled] {
            let opts = ComposefsOptions { verity };
            let from_bare = bare.export_composefs(&commit, &opts, None).await.unwrap();
            let from_user = user.export_composefs(&commit, &opts, None).await.unwrap();
            assert_eq!(
                from_bare.bytes.len(),
                from_user.bytes.len(),
                "{verity:?}: bare image length {} != bare-user {}",
                from_bare.bytes.len(),
                from_user.bytes.len()
            );
            if let Some(pos) = from_bare
                .bytes
                .iter()
                .zip(&from_user.bytes)
                .position(|(a, b)| a != b)
            {
                panic!(
                    "{verity:?}: bare image diverges from bare-user at byte {pos:#x}: \
                     got {:#04x}, want {:#04x}",
                    from_bare.bytes[pos], from_user.bytes[pos]
                );
            }
            assert_eq!(
                from_bare.fs_verity, from_user.fs_verity,
                "{verity:?}: the two repositories give the same digest"
            );
            images.push(from_bare.bytes);
        }
        assert_ne!(
            images[0], images[1],
            "the Computed and Disabled images differ"
        );
    });
}

/// Enables fs-verity on `fd`, and tries again on `ETXTBSY`. The kernel refuses
/// to seal an inode that a writable descriptor still holds. A child that
/// another test forks holds a copy of each open descriptor until its `exec`, so
/// the refusal stops when that window closes. If the refusal lasts for all
/// attempts, the function returns it.
fn enable_verity_retrying(fd: std::os::fd::BorrowedFd<'_>) -> rustix::io::Result<()> {
    let mut attempts = 0;
    loop {
        attempts += 1;
        match ostrya_sys::enable_verity(fd) {
            Err(rustix::io::Errno::TXTBSY) if attempts < 50 => {
                std::thread::sleep(std::time::Duration::from_millis(1));
            }
            result => return result,
        }
    }
}

/// A `bare` repository seals its objects and exports an image with
/// [`ComposefsOptions::RECORDED`]. The kernel measures this image and gets the
/// digest that the commit records. Skips if the file system has no fs-verity.
#[test]
fn a_sealed_bare_export_measures_the_recorded_digest() {
    let scratch = TmpDir::new("composefs-sealed-bare");
    let probe = scratch.path().join("probe");
    std::fs::write(&probe, b"probe\n").unwrap();
    let ro = std::fs::File::open(&probe).unwrap();
    match enable_verity_retrying(ro.as_fd()) {
        Ok(()) => {}
        Err(rustix::io::Errno::TXTBSY) => panic!("seal the probe: ETXTBSY after all attempts"),
        Err(e) => {
            eprintln!("skipping: filesystem lacks fs-verity ({e})");
            return;
        }
    }
    drop(ro);

    let base = scratch.path().join("tree");
    build_owned_source(&base);
    let repo_dir = scratch.path().join("repo");
    let image_path = scratch.path().join("root.cfs");

    block_on(async {
        let repo = new_repo(&repo_dir, RepoMode::Bare, "[ex-integrity]\nfsverity=yes\n").await;
        let commit = commit_owned(&repo, &base).await;

        let objects: Vec<PathBuf> = common::regular_objects(&repo_dir)
            .into_iter()
            .filter(|p| p.extension().is_some_and(|e| e == "file"))
            .collect();
        assert_eq!(objects.len(), 6, "the tree holds six regular files");
        for path in &objects {
            let ro = std::fs::File::open(path).unwrap();
            if let Err(e) = ostrya_sys::measure_verity(ro.as_fd()) {
                panic!("{} is not sealed: {e}", path.display());
            }
        }

        let txn = repo.transaction().await.unwrap();
        let stored = repo
            .commit_add_composefs_metadata(&txn, &commit)
            .await
            .unwrap();
        txn.commit().await.unwrap();
        let (obj, _) = repo.load_commit(&stored).await.unwrap();
        let value = obj
            .metadata
            .dict_get("ostree.composefs.digest.v0")
            .expect("the new commit carries the digest key");
        let (_, inner) = value.as_variant().expect("digest value is a variant");
        let recorded = inner.as_bytes().expect("digest is a byte array").to_vec();

        let file = std::fs::File::create(&image_path).expect("create the image file");
        let returned = repo
            .export_composefs_to(&stored, &ComposefsOptions::RECORDED, None, file.as_fd())
            .await
            .unwrap();
        drop(file);

        let ro = std::fs::File::open(&image_path).expect("open the image read-only");
        enable_verity_retrying(ro.as_fd()).expect("seal the image");
        let measured = ostrya_sys::measure_verity(ro.as_fd()).expect("measure the image");
        assert_eq!(
            to_hex(&measured),
            to_hex(&recorded),
            "the kernel measures the image to the recorded digest"
        );
        assert_eq!(
            to_hex(&returned),
            to_hex(&recorded),
            "the export returns the recorded digest"
        );
    });
}

/// The payload of a content object is an input to the `Computed` image and has
/// no effect on the `Disabled` image. The test rewrites the payload in place at
/// the same length. The metadata of each inode stays the same, so the object
/// loads under both policies. The payload is the only input that changes.
///
/// The bare-user fixture and the library are the only inputs, so this test
/// runs whatever composefs support the `ostree` command on the host has.
#[test]
fn disabled_policy_reads_no_payload() {
    let computed = ComposefsOptions {
        verity: VerityPolicy::Computed,
    };
    let disabled = ComposefsOptions {
        verity: VerityPolicy::Disabled,
    };

    let scratch = TmpDir::new("composefs-noverity-payload");
    let repo_dir = scratch_fixture_repo(&scratch, "bare-user");

    block_on(async {
        let repo = Repo::open(&repo_dir).await.unwrap();
        let commit = Checksum::from_hex(COMMIT).unwrap();

        let before_computed = repo
            .export_composefs(&commit, &computed, None)
            .await
            .unwrap();
        let before_disabled = repo
            .export_composefs(&commit, &disabled, None)
            .await
            .unwrap();

        rewrite_payload(&content_object(&repo_dir, HELLO_TXT));

        let after_computed = repo
            .export_composefs(&commit, &computed, None)
            .await
            .unwrap();
        let after_disabled = repo
            .export_composefs(&commit, &disabled, None)
            .await
            .unwrap();

        assert_eq!(
            before_disabled.bytes, after_disabled.bytes,
            "a rewritten payload leaves the Disabled image unchanged"
        );
        assert_ne!(
            before_computed.bytes, after_computed.bytes,
            "a rewritten payload changes the Computed image"
        );
        assert_ne!(
            before_disabled.fs_verity, before_computed.fs_verity,
            "the two policies produce distinct images"
        );
    });
}

/// The inode of the helper tree that carries the attributes under test.
#[derive(Clone, Copy)]
enum Carrier {
    /// The root directory, through its dirmeta object.
    Root,
    /// A regular file in the root. The export adds `trusted.overlay.redirect`
    /// and `trusted.overlay.metacopy` to this inode, in addition to its own
    /// attributes. This case also checks that these two are outside the budget.
    File,
}

/// Exports the composefs image of a tree whose `carrier` inode holds `xattrs`.
/// The tree is in a new repository of its own in `mode`.
///
/// The function builds the dirmeta object directly, because the limit of the
/// host file system decides if a real directory can hold the attributes. If
/// the attributes on a file object reach that limit in a bare-user repository,
/// `mode` is `Archive`. An archive object carries its metadata in its own
/// header.
async fn export_xattrs(
    repo_dir: &Path,
    mode: RepoMode,
    carrier: Carrier,
    xattrs: Xattrs,
) -> Result<(), Error> {
    let repo = Repo::create(repo_dir, CreateOptions::new(mode))
        .await
        .unwrap();
    let txn = repo.transaction().await.unwrap();
    let mut mtree = MutableTree::new();
    let root_xattrs = match carrier {
        Carrier::Root => xattrs,
        Carrier::File => {
            let meta = FileMeta {
                uid: 0,
                gid: 0,
                mode: 0o100644,
                xattrs,
            };
            // A file with content is backed, so the export adds the redirect
            // and metacopy attributes to its inode.
            let file = txn
                .write_regfile_inline(None, &meta, b"backed\n")
                .await
                .unwrap();
            mtree.replace_file("f", file).unwrap();
            Xattrs::empty()
        }
    };
    let dirmeta_bytes = DirMeta {
        uid: 0,
        gid: 0,
        mode: 0o040755,
        xattrs: root_xattrs,
    }
    .serialize()
    .unwrap();
    let dirmeta = txn
        .write_metadata(ObjectType::DirMeta, None, &dirmeta_bytes)
        .await
        .unwrap();
    mtree.set_metadata_checksum(dirmeta);
    let root = txn.write_mtree(&mut mtree).await.unwrap();
    let commit = txn
        .write_commit(
            CommitOptions {
                subject: Some("xattr budget".to_owned()),
                timestamp: Some(0),
                ..CommitOptions::default()
            },
            &root,
        )
        .await
        .unwrap();
    // The staged digest path reads the objects through the transaction. It must
    // reach the same outcome as the export over the published commit.
    let staged = txn.composefs_digest(&root).await.map(|_| ());
    txn.commit().await.unwrap();

    // `Image` does not implement `Debug`, so the code maps each value to `()`.
    let exported = repo
        .export_composefs(&commit, &ComposefsOptions::default(), None)
        .await
        .map(|_| ());
    assert_eq!(
        staged.is_ok(),
        exported.is_ok(),
        "the staged digest path reached {staged:?} and the export {exported:?}"
    );
    exported
}

/// One xattr takes its name, its value, and 7 bytes from the budget of the
/// inode, which is 32755 bytes. At the budget, the export builds the image. One
/// byte past the budget, the export refuses and names the attribute.
#[test]
fn holds_an_inode_to_the_composefs_xattr_budget() {
    let scratch = TmpDir::new("composefs-xattr-budget");
    let name = b"user.long\0".to_vec();
    // 7 + 9 (the name without its NUL) + value == 32755 at the budget.
    let at_budget = 32755 - 7 - 9;
    block_on(async {
        export_xattrs(
            &scratch.path().join("at"),
            RepoMode::BareUser,
            Carrier::Root,
            Xattrs::new([(name.clone(), vec![b'x'; at_budget])]).unwrap(),
        )
        .await
        .expect("the export builds the image at the budget");

        let err = export_xattrs(
            &scratch.path().join("over"),
            RepoMode::BareUser,
            Carrier::Root,
            Xattrs::new([(name, vec![b'x'; at_budget + 1])]).unwrap(),
        )
        .await
        .expect_err("the export refuses one byte past the budget");
        let text = err.to_string();
        assert!(
            matches!(err, Error::Unsupported(_)),
            "the export refused with {err:?}"
        );
        assert!(
            text.contains("user.long") && text.contains("32756"),
            "the refusal names the attribute and the bytes it takes: {text}"
        );
    });
}

/// The budget applies to the whole inode, so two attributes that each fit
/// alone share one budget. The refusal names the attribute that takes the
/// inode past the budget.
#[test]
fn the_composefs_xattr_budget_covers_the_inode() {
    let scratch = TmpDir::new("composefs-xattr-budget-sum");
    // 2 * (7 + 6 + value) == 32756 one byte past the budget.
    let each = (32756 / 2) - 7 - 6;
    block_on(async {
        let err = export_xattrs(
            scratch.path(),
            RepoMode::BareUser,
            Carrier::Root,
            Xattrs::new([
                (b"user.a\0".to_vec(), vec![b'x'; each]),
                (b"user.b\0".to_vec(), vec![b'y'; each]),
            ])
            .unwrap(),
        )
        .await
        .expect_err("the export refuses the pair");
        let text = err.to_string();
        assert!(
            matches!(err, Error::Unsupported(_)),
            "the export refused with {err:?}"
        );
        assert!(
            text.contains("user.b") && text.contains("32756"),
            "the refusal names the second attribute: {text}"
        );
    });
}

/// A regular file has the same budget. The `trusted.overlay.redirect` and
/// `trusted.overlay.metacopy` attributes that the export adds to the inode are
/// outside the budget, so the walk accepts the file at the budget. The test
/// uses an archive repository, because the `user.ostreemeta` attribute of a
/// bare-user object cannot hold the attribute at this size.
#[test]
fn the_composefs_xattr_budget_covers_a_regular_file() {
    let scratch = TmpDir::new("composefs-xattr-budget-file");
    let name = b"user.long\0".to_vec();
    let at_budget = 32755 - 7 - 9;
    block_on(async {
        export_xattrs(
            &scratch.path().join("at"),
            RepoMode::Archive,
            Carrier::File,
            Xattrs::new([(name.clone(), vec![b'x'; at_budget])]).unwrap(),
        )
        .await
        .expect("the export builds the image at the budget");

        let err = export_xattrs(
            &scratch.path().join("over"),
            RepoMode::Archive,
            Carrier::File,
            Xattrs::new([(name, vec![b'x'; at_budget + 1])]).unwrap(),
        )
        .await
        .expect_err("the export refuses one byte past the budget");
        assert!(
            matches!(err, Error::Unsupported(_)),
            "the export refused with {err:?}"
        );
    });
}

/// Commits a tree that holds one symlink whose target is `len` bytes, in a
/// repository of its own under `dir`. Then walks the tree into the image.
async fn export_symlink(dir: &Path, len: usize) -> Result<(), Error> {
    let repo = Repo::create(dir, CreateOptions::new(RepoMode::BareUser))
        .await
        .unwrap();
    let txn = repo.transaction().await.unwrap();
    let meta = FileMeta {
        uid: 0,
        gid: 0,
        mode: 0o120777,
        xattrs: Xattrs::empty(),
    };
    let link = txn
        .write_symlink(&"z".repeat(len), &meta, None)
        .await
        .unwrap();
    let mut mtree = MutableTree::new();
    mtree.replace_file("l", link).unwrap();
    let dirmeta_bytes = DirMeta {
        uid: 0,
        gid: 0,
        mode: 0o040755,
        xattrs: Xattrs::empty(),
    }
    .serialize()
    .unwrap();
    let dirmeta = txn
        .write_metadata(ObjectType::DirMeta, None, &dirmeta_bytes)
        .await
        .unwrap();
    mtree.set_metadata_checksum(dirmeta);
    let root = txn.write_mtree(&mut mtree).await.unwrap();
    let commit = txn
        .write_commit(
            CommitOptions {
                subject: Some("symlink".to_owned()),
                timestamp: Some(0),
                ..CommitOptions::default()
            },
            &root,
        )
        .await
        .unwrap();
    txn.commit().await.unwrap();
    // `Image` does not implement `Debug`, so the code maps the value to `()`.
    repo.export_composefs(&commit, &ComposefsOptions::default(), None)
        .await
        .map(|_| ())
}

/// A symlink stores its target inline, next to a 32-byte compact inode header,
/// so a target with no attributes fits at 4063 bytes. At 4064 bytes, the
/// target does not fit. The `ostree` command 2026.1 writes the image at 4063
/// bytes and aborts at 4064 bytes (observed).
#[test]
fn holds_a_symlink_target_to_its_inode_block() {
    let scratch = TmpDir::new("composefs-symlink-block");
    block_on(async {
        export_symlink(&scratch.path().join("at"), 4063)
            .await
            .expect("the export builds the image at 4063 bytes");

        let err = export_symlink(&scratch.path().join("over"), 4064)
            .await
            .expect_err("the export refuses a target that fills the block");
        let text = err.to_string();
        assert!(
            matches!(err, Error::Unsupported(_)),
            "the export refused with {err:?}"
        );
        assert!(
            text.contains("4064"),
            "the refusal names the length: {text}"
        );
    });
}

/// Commits a tree that holds one regular file whose name is `len` bytes, in a
/// repository of its own under `dir`. Then walks the tree into the image.
async fn export_child_name(dir: &Path, len: usize) -> Result<(), Error> {
    let repo = Repo::create(dir, CreateOptions::new(RepoMode::BareUser))
        .await
        .unwrap();
    let txn = repo.transaction().await.unwrap();
    let meta = FileMeta {
        uid: 0,
        gid: 0,
        mode: 0o100644,
        xattrs: Xattrs::empty(),
    };
    let file = txn
        .write_regfile_inline(None, &meta, b"named\n")
        .await
        .unwrap();
    let mut mtree = MutableTree::new();
    mtree.replace_file(&"n".repeat(len), file).unwrap();
    let dirmeta_bytes = DirMeta {
        uid: 0,
        gid: 0,
        mode: 0o040755,
        xattrs: Xattrs::empty(),
    }
    .serialize()
    .unwrap();
    let dirmeta = txn
        .write_metadata(ObjectType::DirMeta, None, &dirmeta_bytes)
        .await
        .unwrap();
    mtree.set_metadata_checksum(dirmeta);
    let root = txn.write_mtree(&mut mtree).await.unwrap();
    let commit = txn
        .write_commit(
            CommitOptions {
                subject: Some("child name".to_owned()),
                timestamp: Some(0),
                ..CommitOptions::default()
            },
            &root,
        )
        .await
        .unwrap();
    txn.commit().await.unwrap();
    // `Image` does not implement `Debug`, so the code maps the value to `()`.
    repo.export_composefs(&commit, &ComposefsOptions::default(), None)
        .await
        .map(|_| ())
}

/// The image holds a child name of at most 255 bytes. The `ostree` command
/// 2026.1 refuses a 256-byte name with `File name too long` (observed).
#[test]
fn holds_a_child_name_to_255_bytes() {
    let scratch = TmpDir::new("composefs-child-name");
    block_on(async {
        export_child_name(&scratch.path().join("at"), 255)
            .await
            .expect("the export builds the image at 255 bytes");

        let err = export_child_name(&scratch.path().join("over"), 256)
            .await
            .expect_err("the export refuses a 256-byte name");
        let text = err.to_string();
        assert!(
            matches!(err, Error::Unsupported(_)),
            "the export refused with {err:?}"
        );
        assert!(
            text.contains("256 bytes"),
            "the refusal names the length: {text}"
        );
    });
}

/// An xattr name has a one-byte length field in the image, so a name of more
/// than 255 bytes does not fit at any budget. At 255 bytes, the walk
/// accepts the attribute. At 256 bytes, the walk refuses and names it.
#[test]
fn holds_an_xattr_name_to_the_erofs_length_field() {
    let scratch = TmpDir::new("composefs-xattr-name");
    let name_of = |len: usize| {
        let mut name = b"user.".to_vec();
        name.resize(len, b'n');
        name.push(0);
        name
    };
    block_on(async {
        export_xattrs(
            &scratch.path().join("at"),
            RepoMode::Archive,
            Carrier::File,
            Xattrs::new([(name_of(255), b"v".to_vec())]).unwrap(),
        )
        .await
        .expect("the export builds the image at 255 bytes of name");

        let err = export_xattrs(
            &scratch.path().join("over"),
            RepoMode::Archive,
            Carrier::File,
            Xattrs::new([(name_of(256), b"v".to_vec())]).unwrap(),
        )
        .await
        .expect_err("the export refuses a 256-byte name");
        let text = err.to_string();
        assert!(
            matches!(err, Error::Unsupported(_)),
            "the export refused with {err:?}"
        );
        assert!(
            text.contains("user.nn") && text.contains("256"),
            "the refusal names the attribute and its length: {text}"
        );
    });
}

/// `commit_add_composefs_metadata` runs in an `archive` repository and records
/// the digest that a `bare-user` repository records for the same tree.
#[test]
fn digest_metadata_runs_in_an_archive_repository() {
    let Some(digest) = manifest_digest("composefs_digest") else {
        eprintln!("composefs fixture absent; skipping");
        return;
    };

    // The transaction publishes, so the test copies the archive fixture first.
    let scratch = TmpDir::new("composefs-archive-meta");
    let dst = scratch_fixture_repo(&scratch, "archive");

    block_on(async {
        let repo = Repo::open(&dst).await.unwrap();
        assert_eq!(repo.mode(), RepoMode::Archive, "the fixture is archive");
        let commit = Checksum::from_hex(COMMIT).unwrap();

        let txn = repo.transaction().await.unwrap();
        let stored = repo
            .commit_add_composefs_metadata(&txn, &commit)
            .await
            .unwrap();
        txn.commit().await.unwrap();

        let (obj, _) = repo.load_commit(&stored).await.unwrap();
        let value = obj
            .metadata
            .dict_get("ostree.composefs.digest.v0")
            .expect("the new commit carries the digest key");
        let (_, inner) = value.as_variant().expect("digest value is a variant");
        let bytes = inner.as_bytes().expect("digest is a byte array");
        assert_eq!(
            to_hex(bytes),
            digest,
            "the archive repository records the digest the tool recorded"
        );
    });
}

/// The export for a mount writes the bytes of the export, refuses an image
/// over its size limit with no write, and under `Computed` takes the digest
/// of each backing object from the kernel alone. An unsealed object fails
/// the export. The sealed part skips if the file system has no fs-verity.
#[test]
fn the_mount_export_writes_the_export_and_refuses_an_unsealed_object() {
    let scratch = TmpDir::new("composefs-mount-export");
    let repo_dir = scratch_fixture_repo(&scratch, "bare-user");
    let image_path = scratch.path().join("mount.cfs");

    block_on(async {
        let repo = Repo::open(&repo_dir).await.unwrap();
        let commit = Checksum::from_hex(COMMIT).unwrap();
        let disabled = ComposefsOptions {
            verity: VerityPolicy::Disabled,
        };
        let want = repo
            .export_composefs(&commit, &disabled, None)
            .await
            .unwrap();

        let file = std::fs::File::create(&image_path).unwrap();
        let size = repo
            .export_composefs_for_mount(&commit, VerityPolicy::Disabled, file.as_fd(), u64::MAX)
            .await
            .unwrap();
        drop(file);
        assert_eq!(size, want.bytes.len() as u64);
        assert_eq!(std::fs::read(&image_path).unwrap(), want.bytes);

        let file = std::fs::File::create(&image_path).unwrap();
        let err = repo
            .export_composefs_for_mount(&commit, VerityPolicy::Disabled, file.as_fd(), size - 1)
            .await
            .unwrap_err();
        drop(file);
        assert!(
            matches!(&err, Error::Unsupported(text) if text.contains("more than the limit")),
            "{err}"
        );
        assert_eq!(std::fs::metadata(&image_path).unwrap().len(), 0);

        let file = std::fs::File::create(&image_path).unwrap();
        let err = repo
            .export_composefs_for_mount(&commit, VerityPolicy::Computed, file.as_fd(), u64::MAX)
            .await
            .unwrap_err();
        assert!(
            matches!(&err, Error::Unsupported(text) if text.contains("no fs-verity seal")),
            "{err}"
        );
    });

    let Some(sealed) = seal_file_objects(&repo_dir) else {
        eprintln!("skipping the sealed part of the mount export check");
        return;
    };
    assert!(sealed > 0);
    block_on(async {
        let repo = Repo::open(&repo_dir).await.unwrap();
        let commit = Checksum::from_hex(COMMIT).unwrap();
        let want = repo
            .export_composefs(&commit, &ComposefsOptions::RECORDED, None)
            .await
            .unwrap();
        let file = std::fs::File::create(&image_path).unwrap();
        repo.export_composefs_for_mount(&commit, VerityPolicy::Computed, file.as_fd(), u64::MAX)
            .await
            .unwrap();
        drop(file);
        assert_eq!(std::fs::read(&image_path).unwrap(), want.bytes);
    });
}

/// Stores a copy of the commit `commit` with the metadata `metadata`, and
/// returns the checksum of the copy.
async fn store_with_metadata(repo: &Repo, commit: &Checksum, metadata: Value) -> Checksum {
    let (mut obj, _) = repo.load_commit(commit).await.unwrap();
    obj.metadata = metadata;
    let txn = repo.transaction().await.unwrap();
    let written = txn
        .write_metadata(ObjectType::Commit, None, &obj.serialize().unwrap())
        .await
        .unwrap();
    txn.commit().await.unwrap();
    written
}

/// Removes each file object of the repository at `repo_dir`, and returns the
/// number of objects that it removed.
fn remove_file_objects(repo_dir: &Path) -> usize {
    let mut removed = 0;
    for fanout in std::fs::read_dir(repo_dir.join("objects")).unwrap() {
        let fanout = fanout.unwrap().path();
        if !fanout.is_dir() {
            continue;
        }
        for entry in std::fs::read_dir(&fanout).unwrap() {
            let path = entry.unwrap().path();
            if path.extension().is_some_and(|e| e == "file") {
                std::fs::remove_file(&path).unwrap();
                removed += 1;
            }
        }
    }
    removed
}

/// Writes the export for a mount of `commit` under `verity` to `path`.
/// Returns the result of the export and the bytes of the file.
async fn mount_export(
    repo: &Repo,
    commit: &Checksum,
    verity: VerityPolicy,
    path: &Path,
) -> (Result<u64, Error>, Vec<u8>) {
    let file = std::fs::File::create(path).unwrap();
    let result = repo
        .export_composefs_for_mount(commit, verity, file.as_fd(), u64::MAX)
        .await;
    drop(file);
    (result, std::fs::read(path).unwrap())
}

/// Under `Computed`, the export for a mount compares the fs-verity digest of
/// the image with the key of the commit. A commit with no key and a commit
/// with the right key give the image of the `RECORDED` export. A wrong key
/// fails the export after the write. A malformed key fails it before the
/// write and before the walk of the tree: with no file object in the
/// repository, the export fails with `InvalidFormat`. Skips if the file system
/// has no fs-verity.
#[test]
fn the_mount_export_compares_the_key_under_computed() {
    let scratch = TmpDir::new("composefs-mount-key");
    if !verity_supported(scratch.path()) {
        eprintln!("skipping: filesystem lacks fs-verity");
        return;
    }
    let base = scratch.path().join("tree");
    build_owned_source(&base);
    let image_path = scratch.path().join("mount.cfs");
    let repo_dir = scratch.path().join("repo");

    block_on(async {
        let repo = new_repo(
            &repo_dir,
            RepoMode::BareUser,
            "[ex-integrity]\nfsverity=yes\n",
        )
        .await;
        let plain = commit_owned(&repo, &base).await;
        let txn = repo.transaction().await.unwrap();
        let keyed = repo
            .commit_add_composefs_metadata(&txn, &plain)
            .await
            .unwrap();
        txn.commit().await.unwrap();
        let wrong = store_with_metadata(&repo, &plain, key_dict(&[0x11; 32])).await;
        let malformed = store_with_metadata(
            &repo,
            &plain,
            dict(DIGEST_KEY, "ay", Value::Bytes(vec![0x11; 31])),
        )
        .await;
        let want = repo
            .export_composefs(&plain, &ComposefsOptions::RECORDED, None)
            .await
            .unwrap();

        for commit in [&plain, &keyed] {
            let (result, bytes) =
                mount_export(&repo, commit, VerityPolicy::Computed, &image_path).await;
            assert_eq!(result.unwrap(), want.bytes.len() as u64, "commit {commit}");
            assert_eq!(bytes, want.bytes, "commit {commit}");
        }

        let (result, bytes) =
            mount_export(&repo, &wrong, VerityPolicy::Computed, &image_path).await;
        match result.unwrap_err() {
            Error::ComposefsDigestMismatch {
                commit,
                expected,
                actual,
            } => {
                assert_eq!(commit, wrong);
                assert_eq!(expected, [0x11; 32]);
                assert_eq!(actual, want.fs_verity);
            }
            other => panic!("expected ComposefsDigestMismatch, got {other:?}"),
        }
        assert_eq!(bytes, want.bytes, "the image stays in the file");

        assert!(remove_file_objects(&repo_dir) > 0);
        let (result, _) = mount_export(&repo, &plain, VerityPolicy::Computed, &image_path).await;
        let err = result.unwrap_err();
        assert!(matches!(&err, Error::ObjectNotFound { .. }), "{err:?}");

        let (result, bytes) =
            mount_export(&repo, &malformed, VerityPolicy::Computed, &image_path).await;
        let err = result.unwrap_err();
        assert!(matches!(&err, Error::InvalidFormat(_)), "{err:?}");
        assert!(bytes.is_empty(), "a malformed key fails before the write");
    });
}

/// Under `Disabled`, the export for a mount does not read the key. A wrong key
/// and a malformed key give the image of the `Disabled` export.
#[test]
fn the_mount_export_makes_no_comparison_under_disabled() {
    let scratch = TmpDir::new("composefs-mount-nokey");
    let base = scratch.path().join("tree");
    build_owned_source(&base);
    let image_path = scratch.path().join("mount.cfs");

    block_on(async {
        let repo = new_repo(&scratch.path().join("repo"), RepoMode::BareUser, "").await;
        let plain = commit_owned(&repo, &base).await;
        let wrong = store_with_metadata(&repo, &plain, key_dict(&[0x11; 32])).await;
        let malformed = store_with_metadata(
            &repo,
            &plain,
            dict(DIGEST_KEY, "ay", Value::Bytes(vec![0x11; 31])),
        )
        .await;
        let disabled = ComposefsOptions {
            verity: VerityPolicy::Disabled,
        };
        let want = repo
            .export_composefs(&plain, &disabled, None)
            .await
            .unwrap();

        for commit in [&wrong, &malformed] {
            let (result, bytes) =
                mount_export(&repo, commit, VerityPolicy::Disabled, &image_path).await;
            assert_eq!(result.unwrap(), want.bytes.len() as u64, "commit {commit}");
            assert_eq!(bytes, want.bytes, "commit {commit}");
        }
    });
}

/// The test label of the inode at `path`: a SELinux context that names the
/// path, with a NUL at its end. The NUL ends the label, so no label is a part
/// of another label.
fn label_of(path: &Path) -> Vec<u8> {
    let mut label = format!("system_u:object_r:hook_t:s0:{}", path.display()).into_bytes();
    label.push(0);
    label
}

/// Returns the number of times that `needle` occurs in `haystack`.
fn occurrences(haystack: &[u8], needle: &[u8]) -> usize {
    haystack
        .windows(needle.len())
        .filter(|window| *window == needle)
        .count()
}

/// Commits a flat tree in `repo` at timestamp 0 and returns the commit
/// checksum. The root dirmeta holds `root_xattrs`. Each entry of `files` is a
/// regular file with the mode 0644, the owner 0:0, its name, its content, and
/// its xattrs.
async fn commit_flat(
    repo: &Repo,
    root_xattrs: &Xattrs,
    files: &[(&str, &[u8], &Xattrs)],
) -> Checksum {
    let txn = repo.transaction().await.unwrap();
    let mut mtree = MutableTree::new();
    for (name, content, xattrs) in files {
        let meta = FileMeta {
            uid: 0,
            gid: 0,
            mode: 0o100644,
            xattrs: (*xattrs).clone(),
        };
        let file = txn
            .write_regfile_inline(None, &meta, content)
            .await
            .unwrap();
        mtree.replace_file(name, file).unwrap();
    }
    let dirmeta_bytes = DirMeta {
        uid: 0,
        gid: 0,
        mode: 0o040755,
        xattrs: root_xattrs.clone(),
    }
    .serialize()
    .unwrap();
    let dirmeta = txn
        .write_metadata(ObjectType::DirMeta, None, &dirmeta_bytes)
        .await
        .unwrap();
    mtree.set_metadata_checksum(dirmeta);
    let root = txn.write_mtree(&mut mtree).await.unwrap();
    let commit = txn
        .write_commit(
            CommitOptions {
                subject: Some("flat tree".to_owned()),
                timestamp: Some(0),
                ..CommitOptions::default()
            },
            &root,
        )
        .await
        .unwrap();
    txn.commit().await.unwrap();
    commit
}

/// The hook sees each inode of the image one time, the five added top-level
/// directories included, with the kind of the inode. A label that the hook
/// sets on an inode goes into the image. The two export forms give the same
/// bytes with the hook, and the bytes differ from the image with no hook.
#[test]
fn the_entry_hook_sees_and_labels_each_inode() {
    let scratch = TmpDir::new("composefs-hook-label");
    let image_path = scratch.path().join("labeled.cfs");
    let repo_dir = fixture_repo("bare-user");
    let calls = Mutex::new(Vec::new());
    let hook: ComposefsEntryFn<'_> = &|path, kind, meta| {
        calls.lock().unwrap().push((path.to_path_buf(), kind));
        meta.xattrs
            .push((b"security.selinux".to_vec(), label_of(path)));
        Ok(())
    };
    let mut want: Vec<(PathBuf, EntryKind)> = [
        ("/", EntryKind::Directory),
        ("/boot", EntryKind::Directory),
        ("/etc", EntryKind::Directory),
        ("/sysroot", EntryKind::Directory),
        ("/usr", EntryKind::Directory),
        ("/var", EntryKind::Directory),
        ("/empty.txt", EntryKind::Regular),
        ("/hello.txt", EntryKind::Regular),
        ("/link", EntryKind::Symlink),
        ("/subdir", EntryKind::Directory),
        ("/subdir/nested.txt", EntryKind::Regular),
    ]
    .into_iter()
    .map(|(path, kind)| (PathBuf::from(path), kind))
    .collect();
    want.sort_by(|a, b| a.0.cmp(&b.0));

    block_on(async {
        let repo = Repo::open(&repo_dir).await.unwrap();
        let commit = Checksum::from_hex(COMMIT).unwrap();
        let opts = ComposefsOptions::RECORDED;

        let image = repo
            .export_composefs(&commit, &opts, Some(hook))
            .await
            .unwrap();
        let mut seen = std::mem::take(&mut *calls.lock().unwrap());
        seen.sort_by(|a, b| a.0.cmp(&b.0));
        assert_eq!(seen, want, "the hook sees each inode one time");
        for (path, _) in &want {
            assert!(
                occurrences(&image.bytes, &label_of(path)) > 0,
                "the image holds the label of {}",
                path.display()
            );
        }

        let file = std::fs::File::create(&image_path).unwrap();
        let digest = repo
            .export_composefs_to(&commit, &opts, Some(hook), file.as_fd())
            .await
            .unwrap();
        drop(file);
        assert_eq!(std::fs::read(&image_path).unwrap(), image.bytes);
        assert_eq!(digest, image.fs_verity);
        assert_eq!(calls.lock().unwrap().len(), want.len());

        let plain = repo.export_composefs(&commit, &opts, None).await.unwrap();
        assert_ne!(plain.bytes, image.bytes);
    });
}

/// A label that the hook sets on the root gives the image of a tree whose
/// root dirmeta stores the same label. The whiteout stubs copy the label of
/// the root, so the two images are the same only if the stubs copy the label
/// that the hook set.
#[test]
fn the_whiteout_stubs_hold_the_root_label_of_the_hook() {
    let scratch = TmpDir::new("composefs-hook-whiteout");
    let label = label_of(Path::new("/"));
    let opts = ComposefsOptions::RECORDED;
    let hook: ComposefsEntryFn<'_> = &|path, _, meta| {
        if path == Path::new("/") {
            meta.xattrs
                .push((b"security.selinux".to_vec(), label.clone()));
        }
        Ok(())
    };
    block_on(async {
        let repo = new_repo(scratch.path(), RepoMode::Archive, "").await;
        let empty = Xattrs::empty();
        let mut stored_name = b"security.selinux".to_vec();
        stored_name.push(0);
        let stored = Xattrs::new([(stored_name, label.clone())]).unwrap();
        let x = commit_flat(&repo, &empty, &[("f", b"", &empty)]).await;
        let y = commit_flat(&repo, &stored, &[("f", b"", &empty)]).await;

        let hooked = repo.export_composefs(&x, &opts, Some(hook)).await.unwrap();
        let stored = repo.export_composefs(&y, &opts, None).await.unwrap();
        let plain = repo.export_composefs(&x, &opts, None).await.unwrap();
        assert_eq!(hooked.bytes, stored.bytes);
        assert_eq!(hooked.fs_verity, stored.fs_verity);
        assert_ne!(hooked.bytes, plain.bytes);
    });
}

/// A file capability is the xattr `security.capability` with a binary value.
/// The image holds the value that the hook sets byte for byte. On an empty
/// file, the hook gives the image of a tree that stores the same value.
#[test]
fn the_entry_hook_sets_a_binary_capability() {
    const CAP: [u8; 20] = [
        0x01, 0x00, 0x00, 0x02, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00,
    ];
    let opts = ComposefsOptions::RECORDED;
    let on = |target: &'static str| {
        move |path: &Path, _: EntryKind, meta: &mut Metadata| {
            if path == Path::new(target) {
                meta.xattrs
                    .push((b"security.capability".to_vec(), CAP.to_vec()));
            }
            Ok(())
        }
    };
    let on_hello = on("/hello.txt");
    let on_f = on("/f");

    let scratch = TmpDir::new("composefs-hook-capability");
    let repo_dir = fixture_repo("bare-user");
    block_on(async {
        let repo = Repo::open(&repo_dir).await.unwrap();
        let commit = Checksum::from_hex(COMMIT).unwrap();
        let image = repo
            .export_composefs(&commit, &opts, Some(&on_hello))
            .await
            .unwrap();
        let plain = repo.export_composefs(&commit, &opts, None).await.unwrap();
        assert_eq!(occurrences(&image.bytes, &CAP), 1);
        assert_eq!(occurrences(&plain.bytes, &CAP), 0);

        let repo = new_repo(&scratch.path().join("repo"), RepoMode::Archive, "").await;
        let empty = Xattrs::empty();
        let stored = Xattrs::new([(b"security.capability\0".to_vec(), CAP.to_vec())]).unwrap();
        let x = commit_flat(&repo, &empty, &[("f", b"", &empty)]).await;
        let y = commit_flat(&repo, &empty, &[("f", b"", &stored)]).await;
        let hooked = repo.export_composefs(&x, &opts, Some(&on_f)).await.unwrap();
        let stored = repo.export_composefs(&y, &opts, None).await.unwrap();
        assert_eq!(hooked.bytes, stored.bytes);
    });
}

/// Exports the fixture commit with a hook that pushes `xattrs` onto the inode
/// at `target`, and returns the error of the export.
async fn refusal_of(target: &str, xattrs: Vec<(Vec<u8>, Vec<u8>)>) -> Error {
    let repo = Repo::open(&fixture_repo("bare-user")).await.unwrap();
    let commit = Checksum::from_hex(COMMIT).unwrap();
    let hook: ComposefsEntryFn<'_> = &|path, _, meta| {
        if path == Path::new(target) {
            meta.xattrs.extend(xattrs.iter().cloned());
        }
        Ok(())
    };
    // `Image` does not implement `Debug`, so the code maps the value to `()`.
    repo.export_composefs(&commit, &ComposefsOptions::RECORDED, Some(hook))
        .await
        .map(|_| ())
        .expect_err("the export refuses the xattrs of the hook")
}

/// The check after the hook holds each inode to the xattr budget and each
/// name to 255 bytes, the added top-level directories included. It refuses
/// an empty name, a name with a NUL byte, and a name that occurs two times.
/// Each refusal is `Unsupported`, and the writer does not panic.
#[test]
fn the_check_after_the_hook_refuses_xattrs_the_image_cannot_hold() {
    let long_name = |len: usize| {
        let mut name = b"user.".to_vec();
        name.resize(len, b'n');
        name
    };
    block_on(async {
        for target in ["/", "/etc"] {
            let err = refusal_of(target, vec![(b"user.big".to_vec(), vec![b'x'; 70_000])]).await;
            assert!(
                matches!(&err, Error::Unsupported(text)
                    if text.contains("user.big") && text.contains("70015") && text.contains("32755")),
                "{target}: {err:?}"
            );
        }
        for (target, len) in [("/hello.txt", 305), ("/link", 256)] {
            let err = refusal_of(target, vec![(long_name(len), b"v".to_vec())]).await;
            assert!(
                matches!(&err, Error::Unsupported(text)
                    if text.contains(&format!("{len} bytes long")) && text.contains("255")),
                "{target}: {err:?}"
            );
        }
        let cases = [
            (
                vec![
                    (b"user.dup".to_vec(), b"a".to_vec()),
                    (b"user.dup".to_vec(), b"b".to_vec()),
                ],
                "two times",
            ),
            (vec![(Vec::new(), b"v".to_vec())], "empty"),
            (vec![(b"user.a\0b".to_vec(), b"v".to_vec())], "NUL"),
        ];
        for (xattrs, fragment) in cases {
            let err = refusal_of("/subdir/nested.txt", xattrs).await;
            assert!(
                matches!(&err, Error::Unsupported(text) if text.contains(fragment)),
                "{fragment}: {err:?}"
            );
        }
    });
}

/// Xattrs of 5000 bytes on a symlink fit the budget of the inode and fill
/// the inode block, so the block holds no byte of target. The export refuses
/// the symlink with `Unsupported`, and the writer does not panic.
#[test]
fn a_symlink_whose_xattrs_fill_its_block_is_refused() {
    block_on(async {
        let err = refusal_of("/link", vec![(b"user.big".to_vec(), vec![b'x'; 5000])]).await;
        assert!(
            matches!(&err, Error::Unsupported(text) if text.contains("holds 0 bytes of target")),
            "{err:?}"
        );
    });
}

/// An error of the hook stops the export, and the export returns that error
/// unchanged. The file form writes no byte, and the export calls the hook no
/// more after the error.
#[test]
fn an_error_of_the_hook_stops_the_export() {
    let scratch = TmpDir::new("composefs-hook-error");
    let image_path = scratch.path().join("refused.cfs");
    let opts = ComposefsOptions::RECORDED;
    let on_link: ComposefsEntryFn<'_> = &|path, _, _| {
        if path == Path::new("/link") {
            return Err(Error::InvalidInput("the hook refuses /link".to_owned()));
        }
        Ok(())
    };
    let calls = AtomicUsize::new(0);
    let always: ComposefsEntryFn<'_> = &|_, _, _| {
        calls.fetch_add(1, Ordering::Relaxed);
        Err(Error::InvalidInput(
            "the hook refuses each inode".to_owned(),
        ))
    };
    let is_link_refusal =
        |err: &Error| matches!(err, Error::InvalidInput(text) if text == "the hook refuses /link");
    block_on(async {
        let repo = Repo::open(&fixture_repo("bare-user")).await.unwrap();
        let commit = Checksum::from_hex(COMMIT).unwrap();

        let err = repo
            .export_composefs(&commit, &opts, Some(on_link))
            .await
            .map(|_| ())
            .unwrap_err();
        assert!(is_link_refusal(&err), "{err:?}");

        let file = std::fs::File::create(&image_path).unwrap();
        let err = repo
            .export_composefs_to(&commit, &opts, Some(on_link), file.as_fd())
            .await
            .unwrap_err();
        drop(file);
        assert!(is_link_refusal(&err), "{err:?}");
        assert_eq!(std::fs::metadata(&image_path).unwrap().len(), 0);

        let err = repo
            .export_composefs(&commit, &opts, Some(always))
            .await
            .map(|_| ())
            .unwrap_err();
        assert!(matches!(err, Error::InvalidInput(_)), "{err:?}");
        assert_eq!(calls.load(Ordering::Relaxed), 1);
    });
}

/// Two paths that name one file object give two inodes, and the hook sees
/// each one with its own metadata. Each label occurs one time in the image.
#[test]
fn the_hook_labels_each_path_to_one_object_on_its_own() {
    let scratch = TmpDir::new("composefs-hook-shared-object");
    let hook: ComposefsEntryFn<'_> = &|path, _, meta| {
        meta.xattrs
            .push((b"security.selinux".to_vec(), label_of(path)));
        Ok(())
    };
    block_on(async {
        let repo = new_repo(scratch.path(), RepoMode::Archive, "").await;
        let empty = Xattrs::empty();
        let content: &[u8] = b"shared bytes\n";
        let commit = commit_flat(
            &repo,
            &empty,
            &[("a", content, &empty), ("b", content, &empty)],
        )
        .await;
        let image = repo
            .export_composefs(&commit, &ComposefsOptions::RECORDED, Some(hook))
            .await
            .unwrap();
        for path in ["/a", "/b"] {
            assert_eq!(
                occurrences(&image.bytes, &label_of(Path::new(path))),
                1,
                "the label of {path} occurs one time"
            );
        }
    });
}