ostrya 0.12.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
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
//! `Repo::receive` at a `Commit` with a server commit: the checks, the commit
//! that the server builds, its refs, its signatures, and the races of two
//! sessions.
//!
//! A test client drives the session over two in-process pipes with the frame
//! codec of `ostrya::remote::proto`. The tree of each server commit is the
//! tree of the golden fixture commit.
//!
//! The module `tool` compares the commit of the server with the commit of
//! `ostree commit` over equal inputs. It runs only if the `ostree` command is
//! installed, and `OSTRYA_REQUIRE_OSTREE` turns the skip into a failure.

#![cfg(feature = "receive")]

mod common;

use std::sync::Arc;

use common::receive::{
    Obj, build_commit, collections_policy, commit_objects, connect, fixture_objects, new_repo,
    outcomes, policy_allowing, returned_code, session, staging_entries,
};
use common::{
    ROOT_DIRMETA, ROOT_DIRTREE, TmpDir, file_inventory, tool_fsck, writer_child_main,
    writer_child_with,
};
use ostrya::remote::proto::{CommitRequest, ErrorMessage, Message, ServerCommit};
use ostrya::remote::{Encoding, ErrorCode, Expected, RefOutcome, RefUpdate};
use ostrya::{
    Checksum, Commit, Ed25519Signer, Ed25519Verifier, ObjectType, ReceivePolicies, ReceivePolicy,
    ReceiveReport, ReceiveVerify, Ref, Repo, RepoMode, Signer, SigningKey, Summary, TrustedKeys,
    Type, Value, Verifier,
};
use ostrya_rt::block_on;

/// The base64 of the 64-byte ed25519 secret key of a server signer.
const SERVER_SECRET_B64: &str =
    "Rdkqts/v6AXV6N+XOTlBGoGiItwTHGWaip72oFWzQDDMGsL2Sdl/Bcj+xg4EA2iJqHUuBsmdnNCFKNZMBqTiFw==";
/// The ed25519 secret key of a second server signer.
const SECOND_SERVER_SECRET_B64: &str =
    "l/anh/DXkHapldUSa0GdgaH5ISN03SHhYNSK+Ru3aAtpU8w6rtwuSrlO1kD+T1+eddwLhqHxd9ui+B+v52iDOw==";
/// An ed25519 secret key that no server signer holds.
const OTHER_SECRET_B64: &str =
    "5ILWxT+l9G/u3h0BptRpmSi35C9uog7YDdD+Fp1Xk+Hz52p0NlYh6xBA73kJEJKhKbbnjcE0rsWA5XA/K5Sq5Q==";

const ED25519_KEY: &str = "ostree.sign.ed25519";

/// The timestamp of each server commit that states one.
const TIMESTAMP: u64 = 1_700_000_000;

#[test]
#[ignore = "helper process for the timestamp test"]
fn writer_child_subprocess() {
    writer_child_main(|path, branch| {
        let repo = block_on(Repo::open(path)).unwrap();
        let sc = ServerCommit {
            timestamp: None,
            ..server_commit(branch)
        };
        committed(server_push(
            &repo,
            &policy_allowing(&["c:**"]),
            &[branch],
            &tree_objects(),
            sc,
        ));
    });
}

// ---------------------------------------------------------------------------
// Helpers.
// ---------------------------------------------------------------------------

fn checksum(hex: &str) -> Checksum {
    Checksum::from_hex(hex).unwrap()
}

/// The objects of the fixture tree, without the fixture commit.
fn tree_objects() -> Vec<Obj> {
    fixture_objects(Encoding::Raw)
        .into_iter()
        .filter(|o| o.ty != ObjectType::Commit)
        .collect()
}

/// The bytes of the `v` `value`, the form of a metadata entry of a server
/// commit.
fn v(value: &Value) -> Vec<u8> {
    ostrya_core::to_bytes(&Type::Variant, value).unwrap()
}

/// The bytes of a `v` that holds the string `value`.
fn text(value: &str) -> Vec<u8> {
    v(&Value::variant(Type::Str, Value::Str(value.into())))
}

/// A server commit on `branch` over the fixture tree, with no tag, no bound
/// name, the subject `subject`, the body `body`, one metadata entry, and
/// [`TIMESTAMP`].
fn server_commit(branch: &str) -> ServerCommit {
    ServerCommit {
        branch: branch.into(),
        tags: Vec::new(),
        bind_refs: Vec::new(),
        root_dirtree: checksum(ROOT_DIRTREE),
        root_dirmeta: checksum(ROOT_DIRMETA),
        subject: "subject".into(),
        body: "body".into(),
        metadata: vec![("version".into(), text("1"))],
        timestamp: Some(TIMESTAMP),
        orphan: false,
    }
}

/// The output of one push session: the result that the server returned, and
/// the reply that the client read.
type Pushed = (
    ostrya::Result<ReceiveReport>,
    Result<Vec<RefOutcome>, ErrorMessage>,
);

/// One session: `Hello` with `names`, one object stream with `objects` and
/// the detached metadata `meta`, and the `Commit` `request`.
fn push_request(
    repo: &Repo,
    policy: &ReceivePolicy,
    names: &[&str],
    objects: &[Obj],
    meta: &[(Checksum, Vec<u8>)],
    request: CommitRequest,
) -> Pushed {
    let names: Vec<String> = names.iter().map(|&n| n.into()).collect();
    session(repo, policy, |c| script(c, names, objects, meta, request))
}

async fn script(
    mut c: common::receive::Client,
    names: Vec<String>,
    objects: &[Obj],
    meta: &[(Checksum, Vec<u8>)],
    request: CommitRequest,
) -> Result<Vec<RefOutcome>, ErrorMessage> {
    let names: Vec<&str> = names.iter().map(String::as_str).collect();
    c.hello_reply(&names).await;
    if !objects.is_empty() || !meta.is_empty() {
        for o in objects {
            c.object(o.ty, o.checksum, o.encoding, &o.bytes)
                .await
                .unwrap();
        }
        for (commit, dict) in meta {
            c.object(ObjectType::CommitMeta, *commit, Encoding::Raw, dict)
                .await
                .unwrap();
        }
        c.objects_end().await;
    }
    c.send(&Message::Commit(request)).await.unwrap();
    c.commit_reply().await
}

/// The `Commit` request of the server commit `sc` alone.
fn request(sc: ServerCommit) -> CommitRequest {
    CommitRequest {
        updates: Vec::new(),
        force: false,
        server_commit: Some(Box::new(sc)),
    }
}

/// One session that sends `objects` and the server commit `sc`, with `names`
/// in `Hello`.
fn server_push(
    repo: &Repo,
    policy: &ReceivePolicy,
    names: &[&str],
    objects: &[Obj],
    sc: ServerCommit,
) -> Pushed {
    push_request(repo, policy, names, objects, &[], request(sc))
}

/// [`server_push`] with the branch and the tags of `sc` in `Hello`.
fn push_sc(repo: &Repo, policy: &ReceivePolicy, objects: &[Obj], sc: ServerCommit) -> Pushed {
    let names = hello_names(&sc);
    let names: Vec<&str> = names.iter().map(String::as_str).collect();
    server_push(repo, policy, &names, objects, sc)
}

/// The branch and the tags of `sc`.
fn hello_names(sc: &ServerCommit) -> Vec<String> {
    std::iter::once(sc.branch.clone())
        .chain(sc.tags.iter().cloned())
        .collect()
}

/// Asserts that a push succeeded and returns its report.
///
/// The `CommitReply` holds the outcomes of the report, each with the name
/// that the client sent. The report holds the canonical ref of each name.
fn committed(pushed: Pushed) -> ReceiveReport {
    let (result, reply) = pushed;
    let report = result.unwrap_or_else(|e| panic!("the push failed: {e}"));
    let reply = reply.unwrap_or_else(|e| panic!("the push failed: {e:?}"));
    assert_eq!(reply.len(), report.refs.len(), "CommitReply is the report");
    for (sent, held) in reply.iter().zip(&report.refs) {
        let name: Ref = sent.name.parse().unwrap();
        assert_eq!(name.name(), held.name.name(), "{sent:?} {held:?}");
        assert_eq!((sent.old, sent.new), (held.old, held.new), "{sent:?}");
    }
    report
}

/// Asserts that a push failed with `code`, on the wire and to the caller, and
/// returns the `Error` message.
fn refused(pushed: Pushed, code: ErrorCode) -> ErrorMessage {
    let (result, reply) = pushed;
    let error = reply.expect_err("the push is refused");
    assert_eq!(error.code, code, "{error:?}");
    assert_eq!(returned_code(&result), Some(code));
    error
}

/// The new commit of a server commit: the `new` of the first entry.
fn new_commit(report: &ReceiveReport) -> Checksum {
    report.refs[0].new.unwrap()
}

/// The objects and refs of a repository, for [`assert_unchanged`].
type Snapshot = (Vec<(String, Vec<u8>)>, Vec<(String, Vec<u8>)>);

fn snapshot(repo: &Repo) -> Snapshot {
    (
        file_inventory(repo.path(), "objects"),
        file_inventory(repo.path(), "refs"),
    )
}

/// Asserts that a failed session published nothing, wrote no ref, and left no
/// staging entry.
fn assert_unchanged(repo: &Repo, before: &Snapshot) {
    let after = snapshot(repo);
    assert_eq!(after.0, before.0, "no object published");
    assert_eq!(after.1, before.1, "no ref written");
    assert!(
        staging_entries(repo.path()).is_empty(),
        "no staging entry left"
    );
}

/// The commit of the ref file at `relpath` under the repository, if any.
fn ref_at(repo: &Repo, relpath: &str) -> Option<Checksum> {
    std::fs::read_to_string(repo.path().join(relpath))
        .ok()
        .map(|text| checksum(text.trim()))
}

fn load(repo: &Repo, commit: &Checksum) -> (Vec<u8>, Commit) {
    let bytes = block_on(repo.load_object_bytes(ObjectType::Commit, commit)).unwrap();
    let parsed = Commit::parse(&bytes).unwrap();
    (bytes, parsed)
}

/// The bytes of the commit that a server commit of `sc` writes with `parent`,
/// the refs `refs` in its ref binding, and the collection binding
/// `collection`. The metadata of `sc` holds no binding key.
fn expected_bytes(
    sc: &ServerCommit,
    parent: Option<Checksum>,
    refs: &[&str],
    collection: Option<&str>,
) -> Vec<u8> {
    let metadata = sc.metadata.iter().map(|(key, value)| {
        let value = ostrya_core::from_bytes(&Type::Variant, value).unwrap();
        (key.clone(), value)
    });
    Commit {
        metadata: ostrya::commit::commit_metadata(metadata, Some(refs), collection),
        parent,
        related: Vec::new(),
        subject: sc.subject.clone(),
        body: sc.body.clone(),
        timestamp: sc.timestamp.unwrap(),
        root_dirtree: sc.root_dirtree,
        root_dirmeta: sc.root_dirmeta,
    }
    .serialize()
    .unwrap()
}

fn sha(bytes: &[u8]) -> Checksum {
    common::receive::sha(bytes)
}

fn secret(b64: &str) -> Vec<u8> {
    ostrya::base64::decode(b64).unwrap()
}

fn server_signer(secret_b64: &str) -> Arc<SigningKey> {
    Arc::new(SigningKey::ed25519(&secret(secret_b64)).unwrap())
}

fn ed25519_signature(secret_b64: &str, payload: &[u8]) -> Vec<u8> {
    let signer = Ed25519Signer::from_secret_key(&secret(secret_b64)).unwrap();
    block_on(signer.sign(payload)).unwrap()
}

/// The blobs under `key` in the detached metadata of `commit`.
fn stored_signatures(repo: &Repo, commit: &Checksum, key: &str) -> Vec<Vec<u8>> {
    let Some(dict) = block_on(repo.read_commit_detached_metadata(commit)).unwrap() else {
        return Vec::new();
    };
    let Some(value) = dict.dict_get(key) else {
        return Vec::new();
    };
    let (_, list) = value.as_variant().unwrap();
    list.as_array()
        .unwrap()
        .iter()
        .map(|blob| blob.as_bytes().unwrap().to_vec())
        .collect()
}

/// A repository whose branch `main` holds a server commit, and that commit.
fn repo_with_main(tmp: &TmpDir, core: &str) -> (Repo, Checksum) {
    let repo = new_repo(tmp, RepoMode::Archive, core);
    let report = committed(push_sc(
        &repo,
        &policy_allowing(&["c:**"]),
        &tree_objects(),
        server_commit("main"),
    ));
    let tip = new_commit(&report);
    (repo, tip)
}

// ---------------------------------------------------------------------------
// The commit and its parent.
// ---------------------------------------------------------------------------

/// A server commit on an absent branch has no parent. The reply has one
/// entry for the branch, and the branch names the new commit. The commit is
/// the commit of the fields of the client, with the ref binding of the branch
/// and no collection binding in a repository with no collection id.
#[test]
fn a_server_commit_on_an_absent_branch_has_no_parent() {
    let tmp = TmpDir::new("sc-absent");
    let repo = new_repo(&tmp, RepoMode::Archive, "");
    let sc = server_commit("main");
    let report = committed(push_sc(
        &repo,
        &policy_allowing(&["c:**"]),
        &tree_objects(),
        sc.clone(),
    ));
    let expected = expected_bytes(&sc, None, &["main"], None);
    let commit = sha(&expected);
    assert_eq!(
        outcomes(&report),
        vec![RefOutcome {
            name: "main".into(),
            old: None,
            new: Some(commit),
        }]
    );
    assert_eq!(ref_at(&repo, "refs/heads/main"), Some(commit));
    let (bytes, parsed) = load(&repo, &commit);
    assert_eq!(bytes, expected);
    assert_eq!(parsed.parent, None);
    assert_eq!(parsed.root_dirtree, checksum(ROOT_DIRTREE));
    assert_eq!(parsed.root_dirmeta, checksum(ROOT_DIRMETA));
    assert_eq!(parsed.timestamp, TIMESTAMP);
    assert!(report.warnings.is_empty(), "{:?}", report.warnings);
    assert!(staging_entries(repo.path()).is_empty());
}

/// A server commit whose metadata carries `digest` under
/// `ostree.composefs.digest.v0`, over the fixture tree, on `branch`.
fn keyed_server_commit(branch: &str, digest: &[u8; 32]) -> ServerCommit {
    let mut sc = server_commit(branch);
    sc.metadata.push((
        common::composefs::DIGEST_KEY.into(),
        v(&Value::variant(
            Type::array(Type::Byte),
            Value::Bytes(digest.to_vec()),
        )),
    ));
    sc
}

/// With `[ex-ostrya] generate-composefs=true`, a server commit whose
/// metadata carries the digest of its tree stores a valid composefs image of
/// the new commit. A digest that is not the digest of the tree fails the
/// session with `checksum-mismatch`, and a malformed key fails it with
/// `protocol`. The session then publishes nothing.
#[test]
fn a_keyed_server_commit_stores_its_image() {
    let config = "[ex-ostrya]\ngenerate-composefs=true\n";
    let tmp = TmpDir::new("sc-cfs");
    let repo = new_repo(&tmp, RepoMode::BareUser, config);
    let digest = common::receive::fixture_tree_digest();
    let report = committed(push_sc(
        &repo,
        &policy_allowing(&["c:**"]),
        &tree_objects(),
        keyed_server_commit("main", &digest),
    ));
    let commit = new_commit(&report);
    assert_eq!(
        block_on(repo.check_composefs_image(&commit)).unwrap(),
        ostrya::ComposefsImageState::Valid
    );
    assert!(staging_entries(repo.path()).is_empty());

    let tmp = TmpDir::new("sc-cfs-wrong");
    let repo = new_repo(&tmp, RepoMode::BareUser, config);
    let before = snapshot(&repo);
    let (result, reply) = push_sc(
        &repo,
        &policy_allowing(&["c:**"]),
        &tree_objects(),
        keyed_server_commit("main", &[0x5a; 32]),
    );
    let error = reply.expect_err("the push is refused");
    assert_eq!(error.code, ErrorCode::ChecksumMismatch, "{error:?}");
    assert!(result.is_err(), "{result:?}");
    assert_unchanged(&repo, &before);
    assert!(!repo.path().join("extensions/ostrya/composefs").exists());

    let tmp = TmpDir::new("sc-cfs-malformed");
    let repo = new_repo(&tmp, RepoMode::BareUser, config);
    let before = snapshot(&repo);
    let mut sc = server_commit("main");
    sc.metadata
        .push((common::composefs::DIGEST_KEY.into(), text("digest")));
    let (result, reply) = push_sc(&repo, &policy_allowing(&["c:**"]), &tree_objects(), sc);
    let error = reply.expect_err("the push is refused");
    assert_eq!(error.code, ErrorCode::Protocol, "{error:?}");
    assert!(result.is_err(), "{result:?}");
    assert_unchanged(&repo, &before);
    assert!(!repo.path().join("extensions/ostrya/composefs").exists());
}

/// A second server commit on a branch takes the tip as its parent. The reply
/// gives the tip as the old commit of the branch.
#[test]
fn a_server_commit_takes_the_tip_as_parent() {
    let tmp = TmpDir::new("sc-parent");
    let (repo, first) = repo_with_main(&tmp, "");
    let sc = ServerCommit {
        subject: "second".into(),
        ..server_commit("main")
    };
    let report = committed(push_sc(&repo, &policy_allowing(&["c:**"]), &[], sc.clone()));
    let second = new_commit(&report);
    assert_eq!(report.refs[0].old, Some(first));
    assert_eq!(load(&repo, &second).1.parent, Some(first));
    assert_eq!(
        load(&repo, &second).0,
        expected_bytes(&sc, Some(first), &["main"], None)
    );
    assert_eq!(ref_at(&repo, "refs/heads/main"), Some(second));
}

/// `orphan` on a branch that exists needs `d`. With `c` alone it is
/// `non-fast-forward`, and nothing changes. With `cd`, the commit has no
/// parent and the branch moves. `orphan` on an absent branch needs `c` alone.
#[test]
fn orphan_on_a_present_branch_needs_d() {
    let tmp = TmpDir::new("sc-orphan");
    let (repo, first) = repo_with_main(&tmp, "");
    let orphan = ServerCommit {
        subject: "orphan".into(),
        orphan: true,
        ..server_commit("main")
    };
    let before = snapshot(&repo);
    let error = refused(
        push_sc(&repo, &policy_allowing(&["c:**"]), &[], orphan.clone()),
        ErrorCode::NonFastForward,
    );
    assert!(error.message.contains("'main'"), "{error:?}");
    assert_unchanged(&repo, &before);

    let report = committed(push_sc(
        &repo,
        &policy_allowing(&["cd:main"]),
        &[],
        orphan.clone(),
    ));
    let commit = new_commit(&report);
    assert_eq!(report.refs[0].old, Some(first));
    assert_eq!(load(&repo, &commit).1.parent, None);
    assert_eq!(ref_at(&repo, "refs/heads/main"), Some(commit));

    let report = committed(push_sc(
        &repo,
        &policy_allowing(&["c:**"]),
        &[],
        ServerCommit {
            branch: "other".into(),
            ..orphan
        },
    ));
    assert_eq!(report.refs[0].old, None);
    assert_eq!(load(&repo, &new_commit(&report)).1.parent, None);
}

/// The branch needs `c`: `w`, `t`, and `d` do not take its place.
#[test]
fn the_branch_needs_c() {
    let tmp = TmpDir::new("sc-needs-c");
    let repo = new_repo(&tmp, RepoMode::Archive, "");
    let before = snapshot(&repo);
    for allow in [&["wtd:**"][..], &["c:other", "w:main"][..]] {
        let error = refused(
            push_sc(
                &repo,
                &policy_allowing(allow),
                &tree_objects(),
                server_commit("main"),
            ),
            ErrorCode::RefDenied,
        );
        assert!(error.message.contains("no c on ref 'main'"), "{error:?}");
        assert_unchanged(&repo, &before);
    }
    let denying = ReceivePolicy {
        rules: ostrya::access::AccessRules {
            allow: vec![ostrya::access::AllowItem::parse("c:**").unwrap()],
            deny: vec![ostrya_core::RefPattern::parse("main").unwrap()],
        }
        .into(),
        ..ReceivePolicy::default()
    };
    let error = refused(
        server_push(
            &repo,
            &policy_allowing(&["r:main", "c:other"]),
            &["main"],
            &tree_objects(),
            server_commit("main"),
        ),
        ErrorCode::RefDenied,
    );
    assert!(error.message.contains("no c"), "{error:?}");
    // A deny item takes the `r` of `Hello` too, so `Hello` refuses the ref.
    let (result, error) = session(&repo, &denying, |mut c| async move {
        c.hello(&["main"]).await.unwrap();
        c.error().await
    });
    assert_eq!(error.code, ErrorCode::RefDenied);
    assert_eq!(returned_code(&result), Some(ErrorCode::RefDenied));
    assert_unchanged(&repo, &before);
}

/// Each tag needs `t` or `w`. A tag with `c` or `d` alone is `ref-denied`,
/// and nothing changes.
#[test]
fn a_tag_needs_t_or_w() {
    let tmp = TmpDir::new("sc-tag-rights");
    let repo = new_repo(&tmp, RepoMode::Archive, "");
    let before = snapshot(&repo);
    let sc = ServerCommit {
        tags: vec!["v1".into()],
        ..server_commit("main")
    };
    for allow in [&["c:main", "cd:v1"][..], &["c:main", "r:v1"][..]] {
        let error = refused(
            push_sc(&repo, &policy_allowing(allow), &tree_objects(), sc.clone()),
            ErrorCode::RefDenied,
        );
        assert!(error.message.contains("no t or w on ref 'v1'"), "{error:?}");
        assert_unchanged(&repo, &before);
    }
    for allow in [&["c:main", "t:v1"][..], &["c:main", "w:v1"][..]] {
        let tmp = TmpDir::new("sc-tag-rights-ok");
        let repo = new_repo(&tmp, RepoMode::Archive, "");
        let report = committed(push_sc(
            &repo,
            &policy_allowing(allow),
            &tree_objects(),
            sc.clone(),
        ));
        assert_eq!(ref_at(&repo, "refs/heads/v1"), report.refs[1].new);
    }
}

/// A tag that exists is `ref-mismatch` with the ref and its commit, also
/// with `w` on the tag. The branch does not change.
#[test]
fn an_existing_tag_is_ref_mismatch() {
    let tmp = TmpDir::new("sc-tag-exists");
    let (repo, first) = repo_with_main(&tmp, "");
    let tagged = ServerCommit {
        tags: vec!["v1".into()],
        ..server_commit("other")
    };
    let report = committed(push_sc(
        &repo,
        &policy_allowing(&["c:**", "t:v1"]),
        &[],
        tagged,
    ));
    let tag = report.refs[1].new.unwrap();
    for allow in [&["c:**", "t:v1"][..], &["c:**", "w:v1"][..]] {
        let before = snapshot(&repo);
        let sc = ServerCommit {
            subject: "again".into(),
            tags: vec!["v1".into()],
            ..server_commit("main")
        };
        let error = refused(
            push_sc(&repo, &policy_allowing(allow), &[], sc),
            ErrorCode::RefMismatch,
        );
        let current = error.current.unwrap();
        assert_eq!(current.name, "v1");
        assert_eq!(current.commit, Some(tag));
        assert_unchanged(&repo, &before);
        assert_eq!(ref_at(&repo, "refs/heads/main"), Some(first));
    }
}

/// Each tag gets a ref at the new commit, and a bound name gets none. The
/// reply holds the branch, then each tag in order. The ref binding holds the
/// branch, the tags, and the bound names, sorted, with duplicates kept.
#[test]
fn tags_get_refs_and_bound_names_do_not() {
    let tmp = TmpDir::new("sc-tags");
    let repo = new_repo(&tmp, RepoMode::Archive, "");
    let sc = ServerCommit {
        tags: vec!["v2".into(), "v1".into()],
        bind_refs: vec!["alias".into(), "main".into(), "alias".into()],
        ..server_commit("main")
    };
    let report = committed(push_sc(
        &repo,
        &policy_allowing(&["c:main", "t:v1", "t:v2"]),
        &tree_objects(),
        sc.clone(),
    ));
    let commit = sha(&expected_bytes(
        &sc,
        None,
        &["main", "v2", "v1", "alias", "main", "alias"],
        None,
    ));
    let entry = |name: &str| RefOutcome {
        name: name.into(),
        old: None,
        new: Some(commit),
    };
    assert_eq!(
        outcomes(&report),
        vec![entry("main"), entry("v2"), entry("v1")]
    );
    for name in ["main", "v1", "v2"] {
        assert_eq!(ref_at(&repo, &format!("refs/heads/{name}")), Some(commit));
    }
    assert_eq!(ref_at(&repo, "refs/heads/alias"), None);
    assert_eq!(
        load(&repo, &commit).1.ref_bindings(),
        ["alias", "alias", "main", "main", "v1", "v2"]
    );
}

/// The metadata of the commit holds the entries of the client in order, with
/// a duplicate key, without the binding keys of the client, and then the
/// binding keys of the server. The collection binding is the own collection
/// id in the local refs, absent in a repository with no collection id, and
/// the collection of a collection namespace.
#[test]
fn the_metadata_and_the_bindings() {
    let client = vec![
        ("b".to_owned(), text("1")),
        (
            "ostree.ref-binding".to_owned(),
            v(&Value::variant(
                Type::parse("as").unwrap(),
                Value::Array(vec![Value::Str("x".into())]),
            )),
        ),
        ("a".to_owned(), v(&Value::variant(Type::U32, Value::U32(7)))),
        ("ostree.collection-binding".to_owned(), text("org.x")),
        ("b".to_owned(), text("2")),
    ];
    let kept = vec![
        ("b".to_owned(), text("1")),
        ("a".to_owned(), v(&Value::variant(Type::U32, Value::U32(7)))),
        ("b".to_owned(), text("2")),
    ];
    let sc = ServerCommit {
        metadata: client,
        ..server_commit("main")
    };
    let expected = ServerCommit {
        metadata: kept,
        ..server_commit("main")
    };
    let cases: [(&str, Option<&str>, Option<&str>, &str); 4] = [
        ("", None, None, "refs/heads/main"),
        (
            "collection-id=org.example.Own\n",
            None,
            Some("org.example.Own"),
            "refs/heads/main",
        ),
        (
            "",
            Some("org.example.Os"),
            Some("org.example.Os"),
            "refs/mirrors/org.example.Os/main",
        ),
        (
            "collection-id=org.example.Own\n",
            Some("org.example.Os"),
            Some("org.example.Os"),
            "refs/mirrors/org.example.Os/main",
        ),
    ];
    for (core, hello, binding, relpath) in cases {
        let tmp = TmpDir::new("sc-metadata");
        let repo = new_repo(&tmp, RepoMode::Archive, core);
        let (policy, branch) = match hello {
            Some(id) => (collections_policy(&[id], &["c:**"]), format!("@{id}:main")),
            None => (policy_allowing(&["c:**"]), "main".to_owned()),
        };
        let report = committed(push_request(
            &repo,
            &policy,
            &[&branch],
            &tree_objects(),
            &[],
            request(ServerCommit {
                branch: branch.clone(),
                ..sc.clone()
            }),
        ));
        let commit = new_commit(&report);
        assert_eq!(
            load(&repo, &commit).0,
            expected_bytes(&expected, None, &["main"], binding),
            "{core} {hello:?}"
        );
        assert_eq!(load(&repo, &commit).1.collection_binding(), binding);
        assert_eq!(ref_at(&repo, relpath), Some(commit));
        let canonical = match hello {
            Some(id) => Ref::collection(id, "main"),
            None => Ref::local("main"),
        };
        assert_eq!(report.refs[0].name, canonical);
    }
}

/// The branch and each tag of a server commit take `NAME` or `@ID:NAME`.
/// `@X:a` with the tag `@X:t` writes `refs/mirrors/X/a` and
/// `refs/mirrors/X/t`, and the commit has the collection binding `X` and the
/// ref binding `[a, t, b]` with the bound name `b`. `@OWN:a` with the tag `t`
/// writes the local refs, with the collection binding `OWN`.
#[test]
fn a_server_commit_writes_the_namespace_of_its_branch() {
    let tmp = TmpDir::new("sc-namespace");
    let repo = new_repo(&tmp, RepoMode::Archive, "collection-id=org.example.Own\n");
    let mut policy = collections_policy(&["org.example.X"], &["c:a", "t:t"]);
    policy.rules.local = policy_allowing(&["c:a", "t:t"]).rules.local;
    let sc = ServerCommit {
        tags: vec!["@org.example.X:t".into()],
        bind_refs: vec!["b".into()],
        ..server_commit("@org.example.X:a")
    };
    let report = committed(push_sc(&repo, &policy, &tree_objects(), sc.clone()));
    let commit = new_commit(&report);
    let expected = ServerCommit {
        branch: "a".into(),
        tags: vec!["t".into()],
        ..sc
    };
    assert_eq!(
        load(&repo, &commit).0,
        expected_bytes(&expected, None, &["a", "t", "b"], Some("org.example.X"))
    );
    assert_eq!(ref_at(&repo, "refs/mirrors/org.example.X/a"), Some(commit));
    assert_eq!(ref_at(&repo, "refs/mirrors/org.example.X/t"), Some(commit));
    assert_eq!(ref_at(&repo, "refs/heads/a"), None);
    let names: Vec<Ref> = report.refs.iter().map(|r| r.name.clone()).collect();
    assert_eq!(
        names,
        [
            Ref::collection("org.example.X", "a"),
            Ref::collection("org.example.X", "t"),
        ]
    );

    let sc = ServerCommit {
        tags: vec!["t".into()],
        ..server_commit("@org.example.Own:a")
    };
    let report = committed(push_sc(&repo, &policy, &tree_objects(), sc));
    let commit = new_commit(&report);
    let (_, parsed) = load(&repo, &commit);
    assert_eq!(parsed.collection_binding(), Some("org.example.Own"));
    assert_eq!(parsed.ref_bindings(), ["a", "t"]);
    assert_eq!(ref_at(&repo, "refs/heads/a"), Some(commit));
    assert_eq!(ref_at(&repo, "refs/heads/t"), Some(commit));
    assert_eq!(report.refs[0].name, Ref::local("a"));
}

/// The branch and the tags of a server commit are in one namespace, after
/// the map of `@OWN:` to the local refs. `main` with the tag `@X:t`, and
/// `@X:a` with the tag `@Y:t`, are `protocol`. `main` with the tag `@OWN:t`
/// passes. The rights of each ref come from its own namespace: a tag with
/// no `t` or `w` in its namespace is `ref-denied`.
#[test]
fn the_refs_of_a_server_commit_are_in_one_namespace() {
    let tmp = TmpDir::new("sc-one-namespace");
    let repo = new_repo(&tmp, RepoMode::Archive, "collection-id=org.example.Own\n");
    let mut policy = collections_policy(&["org.example.X", "org.example.Y"], &["c:**", "t:**"]);
    policy.rules.local = policy_allowing(&["c:**", "t:**"]).rules.local;
    let before = snapshot(&repo);
    for (branch, tag) in [
        ("main", "@org.example.X:t"),
        ("@org.example.X:a", "@org.example.Y:t"),
        ("@org.example.X:a", "t"),
    ] {
        let sc = ServerCommit {
            tags: vec![tag.into()],
            ..server_commit(branch)
        };
        let error = refused(
            push_sc(&repo, &policy, &tree_objects(), sc),
            ErrorCode::Protocol,
        );
        assert!(error.message.contains("namespace"), "{error:?}");
        assert_unchanged(&repo, &before);
    }

    let mut tags_elsewhere = collections_policy(&["org.example.X"], &["t:**"]);
    tags_elsewhere.rules.local = policy_allowing(&["c:**"]).rules.local;
    let sc = ServerCommit {
        tags: vec!["@org.example.Own:t".into()],
        ..server_commit("main")
    };
    let error = refused(
        push_sc(&repo, &tags_elsewhere, &tree_objects(), sc.clone()),
        ErrorCode::RefDenied,
    );
    assert!(error.message.contains("no t or w on ref 't'"), "{error:?}");
    assert_unchanged(&repo, &before);

    let report = committed(push_sc(&repo, &policy, &tree_objects(), sc));
    let commit = new_commit(&report);
    assert_eq!(ref_at(&repo, "refs/heads/main"), Some(commit));
    assert_eq!(ref_at(&repo, "refs/heads/t"), Some(commit));
}

/// The entries of the client under `ostree.composefs.digest.v0` and
/// `ostree.sizes` come after the binding keys, the digest first, as in a
/// commit of `ostree commit`.
#[test]
fn the_digest_and_the_sizes_come_after_the_bindings() {
    let tmp = TmpDir::new("sc-digest");
    let repo = new_repo(&tmp, RepoMode::Archive, "");
    let digest = Value::variant(Type::parse("ay").unwrap(), Value::Bytes(vec![7; 32]));
    let sizes = Value::variant(
        Type::parse("aay").unwrap(),
        Value::Array(vec![Value::Bytes(vec![1, 2])]),
    );
    let sc = ServerCommit {
        metadata: vec![
            ("ostree.sizes".into(), v(&sizes)),
            ("ostree.composefs.digest.v0".into(), v(&digest)),
            ("k".into(), text("v")),
        ],
        ..server_commit("main")
    };
    let report = committed(push_sc(
        &repo,
        &policy_allowing(&["c:**"]),
        &tree_objects(),
        sc,
    ));
    let commit = load(&repo, &new_commit(&report)).1;
    let Value::Array(entries) = &commit.metadata else {
        panic!("the metadata is an array")
    };
    let keys: Vec<&str> = entries
        .iter()
        .map(|entry| match entry {
            Value::Tuple(fields) => fields[0].as_str().unwrap(),
            other => panic!("an entry {other:?}"),
        })
        .collect();
    assert_eq!(
        keys,
        [
            "k",
            "ostree.ref-binding",
            "ostree.composefs.digest.v0",
            "ostree.sizes"
        ]
    );
    assert_eq!(commit.metadata.dict_get("ostree.sizes"), Some(&sizes));
}

/// The commit takes the timestamp of the client. With no timestamp, it takes
/// the clock of the server, also when the server process has
/// `SOURCE_DATE_EPOCH`. That case runs in a child process, so no test writes
/// to the environment of this process.
#[test]
fn the_timestamp_rule() {
    let tmp = TmpDir::new("sc-timestamp");
    let repo = new_repo(&tmp, RepoMode::Archive, "");
    let sc = ServerCommit {
        timestamp: Some(12_345),
        ..server_commit("given")
    };
    let report = committed(push_sc(
        &repo,
        &policy_allowing(&["c:**"]),
        &tree_objects(),
        sc,
    ));
    assert_eq!(load(&repo, &new_commit(&report)).1.timestamp, 12_345);

    let now = || {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs()
    };
    let before = now();
    let sc = ServerCommit {
        timestamp: None,
        ..server_commit("clock")
    };
    let report = committed(push_sc(&repo, &policy_allowing(&["c:**"]), &[], sc));
    let after = now();
    let timestamp = load(&repo, &new_commit(&report)).1.timestamp;
    assert!(
        (before..=after).contains(&timestamp),
        "{before} {timestamp} {after}"
    );

    let before = now();
    writer_child_with(repo.path(), "child", |command| {
        command.env("SOURCE_DATE_EPOCH", "1");
    })
    .wait();
    let after = now();
    let repo = block_on(Repo::open(repo.path())).unwrap();
    let commit = ref_at(&repo, "refs/heads/child").unwrap();
    let timestamp = load(&repo, &commit).1.timestamp;
    assert!(
        (before..=after).contains(&timestamp),
        "{before} {timestamp} {after}"
    );
}

// ---------------------------------------------------------------------------
// Signatures.
// ---------------------------------------------------------------------------

/// The server key of the policy signs the commit, over the stored commit
/// bytes.
#[test]
fn server_keys_sign_the_commit() {
    let tmp = TmpDir::new("sc-sign");
    let repo = new_repo(&tmp, RepoMode::Archive, "");
    let policy = ReceivePolicy {
        signers: vec![server_signer(SERVER_SECRET_B64)],
        ..policy_allowing(&["c:**"])
    };
    let report = committed(push_sc(
        &repo,
        &policy,
        &tree_objects(),
        server_commit("main"),
    ));
    let commit = new_commit(&report);
    let (bytes, _) = load(&repo, &commit);
    assert_eq!(
        stored_signatures(&repo, &commit, ED25519_KEY),
        vec![ed25519_signature(SERVER_SECRET_B64, &bytes)]
    );
    let public = secret(SERVER_SECRET_B64)[32..].to_vec();
    let verifier = Ed25519Verifier::new([public], Vec::<Vec<u8>>::new()).unwrap();
    let outcome = block_on(repo.verify_commit(&commit, &[Arc::new(verifier)])).unwrap();
    assert!(outcome.valid, "the server signature verifies");
}

/// A server commit onto a tip that names a dependency signs the new commit
/// alone. The dependency gets no server signature.
#[test]
fn a_dependency_of_the_tip_gets_no_server_signature() {
    let tmp = TmpDir::new("sc-sign-dependency");
    let repo = new_repo(&tmp, RepoMode::Archive, "");
    let dep = build_commit(None, "dependency", Value::Array(Vec::new()), &[]);
    let tip = build_commit(
        None,
        "tip",
        Value::Array(Vec::new()),
        &[("lib/d", &dep.checksum.as_bytes()[..])],
    );
    let mut objects = tree_objects();
    objects.extend([dep.clone(), tip.clone()]);
    let updates = vec![RefUpdate {
        name: "main".into(),
        expected: Expected::Absent,
        new: Some(tip.checksum),
    }];
    committed(push_request(
        &repo,
        &policy_allowing(&["w:**"]),
        &["main"],
        &objects,
        &[],
        CommitRequest {
            updates,
            force: false,
            server_commit: None,
        },
    ));

    let policy = ReceivePolicy {
        signers: vec![server_signer(SERVER_SECRET_B64)],
        ..policy_allowing(&["c:**"])
    };
    let report = committed(push_sc(&repo, &policy, &[], server_commit("main")));
    let commit = new_commit(&report);
    let (bytes, parsed) = load(&repo, &commit);
    assert_eq!(parsed.parent, Some(tip.checksum));
    assert!(parsed.related.is_empty());
    assert_eq!(
        stored_signatures(&repo, &commit, ED25519_KEY),
        vec![ed25519_signature(SERVER_SECRET_B64, &bytes)]
    );
    assert!(stored_signatures(&repo, &dep.checksum, ED25519_KEY).is_empty());
    assert!(stored_signatures(&repo, &tip.checksum, ED25519_KEY).is_empty());
}

/// A policy with `verify` and no `allow_unsigned` accepts a server commit,
/// which carries no signature of the client.
#[test]
fn verify_does_not_apply() {
    let tmp = TmpDir::new("sc-verify");
    let repo = new_repo(&tmp, RepoMode::Archive, "");
    let public = secret(OTHER_SECRET_B64)[32..].to_vec();
    let verifier: Arc<dyn Verifier> =
        Arc::new(Ed25519Verifier::new([public], Vec::<Vec<u8>>::new()).unwrap());
    let policy = ReceivePolicy {
        verify: ReceiveVerify::Keys(Arc::new(TrustedKeys::new(vec![verifier]).unwrap())),
        allow_unsigned: false,
        ..policy_allowing(&["c:**"])
    };
    let report = committed(push_sc(
        &repo,
        &policy,
        &tree_objects(),
        server_commit("main"),
    ));
    assert_eq!(ref_at(&repo, "refs/heads/main"), Some(new_commit(&report)));
}

/// Two key groups that read one key file sign the commit once.
#[test]
fn two_key_groups_with_one_key_file_sign_once() {
    let tmp = TmpDir::new("sc-one-key-twice");
    let repo = new_repo(&tmp, RepoMode::Archive, "");
    let key = tmp.path().join("server.ed25519");
    std::fs::write(&key, format!("{SERVER_SECRET_B64}\n")).unwrap();
    let group = |name: &str| {
        format!(
            "[ex-ostrya key \"{name}\"]\ntype=ed25519\nsecret-key-file={}\n",
            key.display()
        )
    };
    let config = repo.path().join("config");
    let mut text = std::fs::read_to_string(&config).unwrap();
    text.push_str(&format!(
        "[ex-ostrya client \"c\"]\nauth=ssh:c\nallow=c:**\nsign=first;second\n{}{}",
        group("first"),
        group("second")
    ));
    std::fs::write(&config, text).unwrap();
    let repo = block_on(Repo::open(repo.path())).unwrap();
    let policies = block_on(ReceivePolicies::from_config(&repo)).unwrap();
    let policy = policies.get("c").unwrap();
    assert_eq!(policy.signers.len(), 2);
    let report = committed(push_sc(
        &repo,
        policy,
        &tree_objects(),
        server_commit("main"),
    ));
    let commit = new_commit(&report);
    let (bytes, _) = load(&repo, &commit);
    assert_eq!(
        stored_signatures(&repo, &commit, ED25519_KEY),
        vec![ed25519_signature(SERVER_SECRET_B64, &bytes)]
    );
}

/// An `orphan` server commit whose inputs equal those of a commit that the
/// repository holds gets the checksum of that commit. Its stored signature
/// stays, a second key adds one, and a key that signed it adds none. The
/// branch does not change.
#[test]
fn a_commit_with_equal_inputs_keeps_its_stored_signature() {
    let tmp = TmpDir::new("sc-equal");
    let repo = new_repo(&tmp, RepoMode::Archive, "");
    let orphan = ServerCommit {
        orphan: true,
        ..server_commit("main")
    };
    let first = ReceivePolicy {
        signers: vec![server_signer(SERVER_SECRET_B64)],
        ..policy_allowing(&["cd:**"])
    };
    let commit = new_commit(&committed(push_sc(
        &repo,
        &first,
        &tree_objects(),
        orphan.clone(),
    )));
    let bytes = load(&repo, &commit).0;
    let both = ReceivePolicy {
        signers: vec![
            server_signer(SERVER_SECRET_B64),
            server_signer(SECOND_SERVER_SECRET_B64),
        ],
        ..policy_allowing(&["cd:**"])
    };
    let report = committed(push_sc(&repo, &both, &[], orphan));
    assert_eq!(
        outcomes(&report),
        vec![RefOutcome {
            name: "main".into(),
            old: Some(commit),
            new: Some(commit),
        }]
    );
    assert_eq!(
        stored_signatures(&repo, &commit, ED25519_KEY),
        vec![
            ed25519_signature(SERVER_SECRET_B64, &bytes),
            ed25519_signature(SECOND_SERVER_SECRET_B64, &bytes),
        ]
    );
}

/// A GPG server key signs the commit under the update lock, and the
/// signature verifies.
#[cfg(feature = "sign-gpg")]
#[test]
fn a_gpg_server_key_signs_the_commit() {
    if !common::gnupg_available(&["gpg", "gpgconf"]) {
        eprintln!("skipping a_gpg_server_key_signs_the_commit: no GnuPG");
        return;
    }
    let tmp = TmpDir::new("sc-gpg");
    let home = common::receive::GnupgHome::new(
        &tmp.path().join("gnupg"),
        "Receive Server <server@example.org>",
    );
    let signer = ostrya::GpgSigner::new("server@example.org").with_homedir(&home.dir);
    let policy = ReceivePolicy {
        signers: vec![Arc::new(block_on(SigningKey::gpg(signer)).unwrap())],
        ..policy_allowing(&["c:**"])
    };
    let repo = new_repo(&tmp, RepoMode::Archive, "");
    let report = committed(push_sc(
        &repo,
        &policy,
        &tree_objects(),
        server_commit("main"),
    ));
    let commit = new_commit(&report);
    assert_eq!(stored_signatures(&repo, &commit, "ostree.gpgsigs").len(), 1);
    let verifier = ostrya::GpgVerifier::from_keyring_bytes([home.export()]).unwrap();
    let outcome = block_on(repo.verify_commit(&commit, &[Arc::new(verifier)])).unwrap();
    assert!(outcome.valid, "the server signature verifies");
}

// ---------------------------------------------------------------------------
// The refusals before the update lock.
// ---------------------------------------------------------------------------

/// A tree that the session did not send and the repository does not hold is
/// `missing-objects`, and the detail names the root dirtree.
#[test]
fn missing_tree_objects_are_missing_objects() {
    let tmp = TmpDir::new("sc-missing");
    let repo = new_repo(&tmp, RepoMode::Archive, "");
    let before = snapshot(&repo);
    let error = refused(
        push_sc(
            &repo,
            &policy_allowing(&["c:**"]),
            &[],
            server_commit("main"),
        ),
        ErrorCode::MissingObjects,
    );
    assert!(
        error
            .missing
            .iter()
            .any(|n| n.ty == ObjectType::DirTree && n.checksum == checksum(ROOT_DIRTREE)),
        "{error:?}"
    );
    assert_unchanged(&repo, &before);
}

/// A detached metadata dict, and a staged commit object, in a session with a
/// server commit are `protocol`.
#[test]
fn detached_metadata_in_a_server_commit_session_is_protocol() {
    let tmp = TmpDir::new("sc-detached");
    let repo = new_repo(&tmp, RepoMode::Archive, "");
    let before = snapshot(&repo);
    let dict =
        ostrya_core::to_bytes(&Type::parse("a{sv}").unwrap(), &Value::Array(Vec::new())).unwrap();
    let commit = sha(&expected_bytes(
        &server_commit("main"),
        None,
        &["main"],
        None,
    ));
    let error = refused(
        push_request(
            &repo,
            &policy_allowing(&["c:**"]),
            &["main"],
            &tree_objects(),
            &[(commit, dict)],
            request(server_commit("main")),
        ),
        ErrorCode::Protocol,
    );
    assert!(error.message.contains("detached metadata"), "{error:?}");
    assert_unchanged(&repo, &before);

    let error = refused(
        push_sc(
            &repo,
            &policy_allowing(&["c:**"]),
            &fixture_objects(Encoding::Raw),
            server_commit("main"),
        ),
        ErrorCode::Protocol,
    );
    assert!(error.message.contains("is staged"), "{error:?}");
    assert_unchanged(&repo, &before);
}

/// The form of the message: a server commit with an update, a server commit
/// with `force`, a tag that `Hello` does not name, and a ref that the message
/// writes two times are each `protocol`.
#[test]
fn the_form_of_a_server_commit_message() {
    let tmp = TmpDir::new("sc-form");
    let repo = new_repo(&tmp, RepoMode::Archive, "");
    let before = snapshot(&repo);
    let policy = policy_allowing(&["c:**", "t:**", "w:**"]);
    let with_update = CommitRequest {
        updates: vec![RefUpdate {
            name: "other".into(),
            expected: Expected::Absent,
            new: Some(checksum(ROOT_DIRTREE)),
        }],
        ..request(server_commit("main"))
    };
    let with_force = CommitRequest {
        force: true,
        ..request(server_commit("main"))
    };
    let tagged = |tags: &[&str]| ServerCommit {
        tags: tags.iter().map(|&t| t.into()).collect(),
        ..server_commit("main")
    };
    let cases: [(&[&str], CommitRequest, &str); 5] = [
        (&["main", "other"], with_update, "ref updates"),
        (&["main"], with_force, "force"),
        (&["main"], request(tagged(&["v1"])), "'v1' was not named"),
        (&["main"], request(tagged(&["main"])), "more than once"),
        (
            &["main", "v1"],
            request(tagged(&["v1", "v1"])),
            "more than once",
        ),
    ];
    for (names, request, needle) in cases {
        let error = refused(
            push_request(&repo, &policy, names, &tree_objects(), &[], request),
            ErrorCode::Protocol,
        );
        assert!(error.message.contains(needle), "{needle}: {error:?}");
        assert_unchanged(&repo, &before);
    }
}

/// The bytes of a `v` that holds a `y` under `levels` levels of maybe.
fn nested_maybe(levels: usize) -> Vec<u8> {
    let ty = (0..levels).fold(Type::Byte, |ty, _| Type::maybe(ty));
    let value = (0..levels).fold(Value::Byte(1), |value, _| {
        Value::Maybe(Some(Box::new(value)))
    });
    v(&Value::variant(ty, value))
}

/// A metadata value that a commit cannot hold within the depth limit is
/// `protocol` before the update lock, and the session writes nothing. A `y`
/// under 125 levels of maybe is the deepest value that a commit holds, and
/// the commit holds its bytes. The message carries 126 levels.
#[test]
fn a_metadata_value_too_deep_for_a_commit_is_protocol() {
    let tmp = TmpDir::new("sc-depth");
    let repo = new_repo(&tmp, RepoMode::Archive, "");
    let policy = policy_allowing(&["c:**"]);
    let deep = |branch: &str, levels: usize| ServerCommit {
        metadata: vec![("deep".into(), nested_maybe(levels))],
        ..server_commit(branch)
    };
    let before = snapshot(&repo);
    let error = refused(
        push_sc(&repo, &policy, &tree_objects(), deep("main", 126)),
        ErrorCode::Protocol,
    );
    assert!(error.message.contains("'deep'"), "{error:?}");
    assert_unchanged(&repo, &before);

    let sc = deep("main", 125);
    let report = committed(push_sc(&repo, &policy, &tree_objects(), sc.clone()));
    let commit = new_commit(&report);
    assert_eq!(
        load(&repo, &commit).0,
        expected_bytes(&sc, None, &["main"], None)
    );
}

/// A branch or a tag of 64 lowercase hex characters, and a bound name that
/// fails the refspec rule or holds `:`, are `invalid-ref`.
#[test]
fn invalid_names_are_invalid_ref() {
    let tmp = TmpDir::new("sc-names");
    let repo = new_repo(&tmp, RepoMode::Archive, "");
    let before = snapshot(&repo);
    let hex = "a".repeat(64);
    let policy = policy_allowing(&["c:**", "t:**"]);
    let cases = [
        (vec![hex.clone()], server_commit(&hex)),
        (
            vec!["main".to_owned(), hex.clone()],
            ServerCommit {
                tags: vec![hex.clone()],
                ..server_commit("main")
            },
        ),
        (
            vec!["main".to_owned()],
            ServerCommit {
                bind_refs: vec!["origin:main".into()],
                ..server_commit("main")
            },
        ),
        (
            vec!["main".to_owned()],
            ServerCommit {
                bind_refs: vec!["a b".into()],
                ..server_commit("main")
            },
        ),
    ];
    for (names, sc) in cases {
        let names: Vec<&str> = names.iter().map(String::as_str).collect();
        let error = refused(
            server_push(&repo, &policy, &names, &tree_objects(), sc),
            ErrorCode::InvalidRef,
        );
        assert!(error.message.contains("invalid"), "{error:?}");
        assert_unchanged(&repo, &before);
    }
}

/// In the local refs of a repository with a collection id, a server commit
/// cannot write `ostree-metadata`, as a branch or as a tag.
#[test]
fn ostree_metadata_is_ref_denied_in_the_local_refs_of_a_collection_repo() {
    let tmp = TmpDir::new("sc-anchor-ref");
    let repo = new_repo(&tmp, RepoMode::Archive, "collection-id=org.example.C\n");
    let before = snapshot(&repo);
    let policy = policy_allowing(&["c:**", "t:**"]);
    for sc in [
        server_commit("ostree-metadata"),
        ServerCommit {
            tags: vec!["ostree-metadata".into()],
            ..server_commit("main")
        },
    ] {
        let error = refused(
            push_sc(&repo, &policy, &tree_objects(), sc),
            ErrorCode::RefDenied,
        );
        assert!(error.message.contains("anchor"), "{error:?}");
        assert_unchanged(&repo, &before);
    }
}

/// In a repository with a collection id and a policy that regenerates the
/// summary, a server commit also writes the anchor commit, and the summary
/// names the new commit.
#[test]
fn a_collection_repo_writes_the_anchor_after_a_server_commit() {
    let tmp = TmpDir::new("sc-anchor");
    let repo = new_repo(&tmp, RepoMode::Archive, "collection-id=org.example.C\n");
    let policy = ReceivePolicy {
        update_summary: true,
        ..policy_allowing(&["c:**"])
    };
    let report = committed(push_sc(
        &repo,
        &policy,
        &tree_objects(),
        server_commit("main"),
    ));
    assert!(report.warnings.is_empty(), "{:?}", report.warnings);
    let commit = new_commit(&report);
    assert_eq!(
        load(&repo, &commit).1.collection_binding(),
        Some("org.example.C")
    );
    let anchor = block_on(repo.resolve_ref_tip(&Ref::local("ostree-metadata")))
        .unwrap()
        .expect("the anchor ref");
    let summary = Summary::parse(&block_on(repo.read_summary()).unwrap().unwrap()).unwrap();
    assert_eq!(summary.lookup("ostree-metadata"), Some(anchor));
    assert_eq!(summary.lookup("main"), Some(commit));
}

// ---------------------------------------------------------------------------
// Races.
// ---------------------------------------------------------------------------

/// Runs two sessions of `repo` at the same time, each one with its policy and
/// its server commit, and no object.
fn race(
    repo: &Repo,
    first: (&ReceivePolicy, ServerCommit),
    second: (&ReceivePolicy, ServerCommit),
) -> (Pushed, Pushed) {
    let (c1, in1, out1) = connect();
    let (c2, in2, out2) = connect();
    let names1 = hello_names(&first.1);
    let names2 = hello_names(&second.1);
    let ((r1, reply1), (r2, reply2)) = block_on(futures_lite::future::zip(
        futures_lite::future::zip(
            repo.receive(in1, out1, first.0),
            script(c1, names1, &[], &[], request(first.1)),
        ),
        futures_lite::future::zip(
            repo.receive(in2, out2, second.0),
            script(c2, names2, &[], &[], request(second.1)),
        ),
    ));
    ((r1, reply1), (r2, reply2))
}

/// Two server commits on one branch at the same time both succeed. The
/// second one under the update lock takes the first one as its parent.
#[test]
fn two_server_commits_on_one_branch_chain() {
    let tmp = TmpDir::new("sc-race-chain");
    let (repo, tip) = repo_with_main(&tmp, "");
    let policy = policy_allowing(&["c:**"]);
    let one = ServerCommit {
        subject: "one".into(),
        ..server_commit("main")
    };
    let two = ServerCommit {
        subject: "two".into(),
        ..server_commit("main")
    };
    let (a, b) = race(&repo, (&policy, one), (&policy, two));
    let (a, b) = (committed(a), committed(b));
    let (early, late) = if a.refs[0].old == Some(tip) {
        (a, b)
    } else {
        (b, a)
    };
    let first = new_commit(&early);
    let second = new_commit(&late);
    assert_eq!(early.refs[0].old, Some(tip));
    assert_eq!(late.refs[0].old, Some(first));
    assert_eq!(load(&repo, &first).1.parent, Some(tip));
    assert_eq!(load(&repo, &second).1.parent, Some(first));
    assert_eq!(ref_at(&repo, "refs/heads/main"), Some(second));
}

/// Two server commits that create one tag at the same time: one succeeds,
/// and the other is `ref-mismatch` with the commit of the winner.
#[test]
fn two_server_commits_that_create_one_tag() {
    let tmp = TmpDir::new("sc-race-tag");
    let (repo, _) = repo_with_main(&tmp, "");
    let policy = policy_allowing(&["c:**", "t:v1"]);
    let tagged = |branch: &str| ServerCommit {
        tags: vec!["v1".into()],
        ..server_commit(branch)
    };
    let (a, b) = race(&repo, (&policy, tagged("main")), (&policy, tagged("other")));
    let (ok, failed) = match (a.0.is_ok(), b.0.is_ok()) {
        (true, false) => (a, b),
        (false, true) => (b, a),
        other => panic!("expected one success, got {other:?}"),
    };
    let winner = new_commit(&committed(ok));
    let error = refused(failed, ErrorCode::RefMismatch);
    let current = error.current.unwrap();
    assert_eq!(current.name, "v1");
    assert_eq!(current.commit, Some(winner));
    assert_eq!(ref_at(&repo, "refs/heads/v1"), Some(winner));
}

/// An `orphan` server commit with `c` alone on an absent branch races a
/// server commit that creates the branch. If the orphan commit comes second,
/// it is `non-fast-forward`. If it comes first, both succeed, and the other
/// commit takes it as its parent.
#[test]
fn an_orphan_commit_that_comes_second_is_non_fast_forward() {
    let tmp = TmpDir::new("sc-race-orphan");
    let repo = new_repo(&tmp, RepoMode::Archive, "");
    committed(push_sc(
        &repo,
        &policy_allowing(&["c:**"]),
        &tree_objects(),
        server_commit("seed"),
    ));
    let policy = policy_allowing(&["c:**"]);
    let orphan = ServerCommit {
        subject: "orphan".into(),
        orphan: true,
        ..server_commit("main")
    };
    let normal = ServerCommit {
        subject: "normal".into(),
        ..server_commit("main")
    };
    let (o, n) = race(&repo, (&policy, orphan), (&policy, normal));
    match (o.0.is_ok(), n.0.is_ok()) {
        (false, true) => {
            let created = new_commit(&committed(n));
            refused(o, ErrorCode::NonFastForward);
            assert_eq!(ref_at(&repo, "refs/heads/main"), Some(created));
        }
        (true, true) => {
            let root = new_commit(&committed(o));
            let child = new_commit(&committed(n));
            assert_eq!(load(&repo, &child).1.parent, Some(root));
            assert_eq!(ref_at(&repo, "refs/heads/main"), Some(child));
        }
        other => panic!("unexpected outcome {other:?}"),
    }
}

// ---------------------------------------------------------------------------
// Comparison with the `ostree` command.
// ---------------------------------------------------------------------------

/// The commit of the server with the inputs of a commit of `ostree commit`
/// has the checksum of that commit.
///
/// Each case runs `ostree commit --no-xattrs -b BRANCH` with `--bind-ref`
/// names, a subject, a body, and metadata, in an `archive` repository of the
/// tool. The port then reads the commit and its tree objects from that
/// repository. A session sends the tree objects and a server commit with the
/// fields of the commit: its subject, body, timestamp, and metadata without
/// the two binding keys. The reply must give the checksum of the tool.
mod tool {
    use super::*;
    use std::path::{Path, PathBuf};
    use std::process::Command;

    /// The timestamp of each commit of the tool.
    const EPOCH: &str = "1700000000";

    /// A tree with a file, a directory with a file, and a symlink.
    fn tree(base: &Path) -> PathBuf {
        use std::os::unix::fs::PermissionsExt;
        let root = base.join("tree");
        std::fs::create_dir_all(root.join("dir")).unwrap();
        std::fs::write(root.join("a"), "alpha\n").unwrap();
        std::fs::write(root.join("dir/b"), "beta\n").unwrap();
        std::os::unix::fs::symlink("a", root.join("link")).unwrap();
        for (path, mode) in [("a", 0o644), ("dir/b", 0o755), ("dir", 0o755), ("", 0o755)] {
            std::fs::set_permissions(root.join(path), std::fs::Permissions::from_mode(mode))
                .unwrap();
        }
        root
    }

    fn run(repo: &Path, args: &[&str]) -> String {
        let out = Command::new("ostree")
            .arg(format!("--repo={}", repo.display()))
            .args(args)
            .env("SOURCE_DATE_EPOCH", EPOCH)
            .output()
            .unwrap();
        assert!(
            out.status.success(),
            "ostree {args:?}: {}",
            String::from_utf8_lossy(&out.stderr)
        );
        String::from_utf8(out.stdout).unwrap()
    }

    /// An `archive` repository of the tool at `<base>/tool`, with the
    /// collection id `collection`.
    fn tool_repo(base: &Path, collection: Option<&str>) -> PathBuf {
        let repo = base.join("tool");
        let mut args = vec!["init".to_owned(), "--mode=archive".to_owned()];
        if let Some(id) = collection {
            args.push(format!("--collection-id={id}"));
        }
        let args: Vec<&str> = args.iter().map(String::as_str).collect();
        run(&repo, &args);
        repo
    }

    /// Runs `ostree commit --no-xattrs` of `tree` in `repo` with `args` and
    /// the subject `subject`, and returns the commit.
    fn tool_commit(repo: &Path, tree: &Path, subject: &str, args: &[&str]) -> Checksum {
        let subject = format!("--subject={subject}");
        let mut all = vec![
            "commit",
            "--no-xattrs",
            subject.as_str(),
            "--body=Body\nline",
            "--add-metadata-string=version=1.0",
            "--add-metadata=n=uint32 1",
            "--add-metadata-string=version=2.0",
        ];
        all.extend_from_slice(args);
        all.push(tree.to_str().unwrap());
        checksum(run(repo, &all).trim())
    }

    /// The server commit of the commit `commit` of the tool repository
    /// `tool`, on `branch` with `tags`, `bind_refs`, and `orphan`.
    fn fields(
        tool: &Path,
        commit: &Checksum,
        branch: &str,
        tags: &[&str],
        bind_refs: &[&str],
        orphan: bool,
    ) -> ServerCommit {
        let repo = block_on(Repo::open(tool)).unwrap();
        let (_, parsed) = load(&repo, commit);
        let Value::Array(entries) = parsed.metadata else {
            panic!("the metadata is an array")
        };
        let metadata = entries
            .into_iter()
            .filter_map(|entry| {
                let Value::Tuple(mut fields) = entry else {
                    panic!("an entry")
                };
                let value = fields.pop().unwrap();
                let Value::Str(key) = fields.pop().unwrap() else {
                    panic!("a key")
                };
                (key != "ostree.ref-binding" && key != "ostree.collection-binding")
                    .then(|| (key, v(&value)))
            })
            .collect();
        let names = |names: &[&str]| names.iter().map(|&n| n.to_owned()).collect();
        ServerCommit {
            branch: branch.into(),
            tags: names(tags),
            bind_refs: names(bind_refs),
            root_dirtree: parsed.root_dirtree,
            root_dirmeta: parsed.root_dirmeta,
            subject: parsed.subject,
            body: parsed.body,
            metadata,
            timestamp: Some(parsed.timestamp),
            orphan,
        }
    }

    /// The tree objects of `commit` of the tool repository `tool`, with the
    /// content objects as their `.filez` files.
    fn tree_of(tool: &Path, commit: &Checksum) -> Vec<Obj> {
        commit_objects(tool, commit, Encoding::Deflate)
            .into_iter()
            .filter(|o| o.ty != ObjectType::Commit)
            .collect()
    }

    /// Sends the tree of `commit` of the tool and the server commit `sc` to
    /// `port`, and returns the new commit. With a `collection`, the branch and
    /// each tag of `sc` are `@ID:NAME`.
    fn server(
        port: &Repo,
        policy: &ReceivePolicy,
        collection: Option<&str>,
        tool: &Path,
        commit: &Checksum,
        mut sc: ServerCommit,
    ) -> Checksum {
        if let Some(id) = collection {
            sc.branch = format!("@{id}:{}", sc.branch);
            for tag in &mut sc.tags {
                *tag = format!("@{id}:{tag}");
            }
        }
        let names = hello_names(&sc);
        let names: Vec<&str> = names.iter().map(String::as_str).collect();
        let objects = tree_of(tool, commit);
        let report = committed(push_request(
            port,
            policy,
            &names,
            &objects,
            &[],
            request(sc),
        ));
        new_commit(&report)
    }

    /// Returns `true` if the tool is installed, else writes the skip.
    fn available(test: &str) -> bool {
        let found = common::ostree_available();
        if !found {
            eprintln!("skipping {test}: `ostree` is not installed");
        }
        found
    }

    /// With no collection id on either side, a server commit equals
    /// `ostree commit -b main --bind-ref v1 --bind-ref alias`, where `v1` is
    /// a tag and `alias` a bound name. A second commit on the branch takes the
    /// tip as its parent on both sides. An `orphan` commit with `cd` equals
    /// `--parent=none`. `ostree fsck` passes on the repository of the port,
    /// and `ostree show` reads the commit.
    #[test]
    fn a_server_commit_equals_the_commit_of_the_tool() {
        if !available("a_server_commit_equals_the_commit_of_the_tool") {
            return;
        }
        let tmp = TmpDir::new("sc-tool-local");
        let tree = tree(tmp.path());
        let tool = tool_repo(tmp.path(), None);
        let port = new_repo(&tmp, RepoMode::Archive, "");
        let policy = policy_allowing(&["cd:main", "t:v1", "t:v2"]);

        let first = tool_commit(
            &tool,
            &tree,
            "first",
            &["-b", "main", "--bind-ref=v1", "--bind-ref=alias"],
        );
        let sc = fields(&tool, &first, "main", &["v1"], &["alias"], false);
        assert_eq!(server(&port, &policy, None, &tool, &first, sc), first);
        assert_eq!(ref_at(&port, "refs/heads/v1"), Some(first));
        assert_eq!(ref_at(&port, "refs/heads/alias"), None);

        let second = tool_commit(&tool, &tree, "second", &["-b", "main"]);
        let sc = fields(&tool, &second, "main", &[], &[], false);
        assert_eq!(server(&port, &policy, None, &tool, &second, sc), second);
        assert_eq!(load(&port, &second).1.parent, Some(first));

        let orphan = tool_commit(&tool, &tree, "orphan", &["-b", "main", "--parent=none"]);
        let sc = fields(&tool, &orphan, "main", &[], &[], true);
        assert_eq!(server(&port, &policy, None, &tool, &orphan, sc), orphan);
        assert_eq!(ref_at(&port, "refs/heads/main"), Some(orphan));

        tool_fsck(port.path());
        let shown = run(port.path(), &["show", &orphan.to_hex()]);
        assert!(shown.contains(&orphan.to_hex()), "{shown}");
        assert!(shown.contains("orphan"), "{shown}");
    }

    /// With the collection id `org.example.T` in the repository of the tool,
    /// a server commit equals the commit of the tool in two namespaces of the
    /// port: the local refs of a repository with the same id, and the
    /// collection `org.example.T` of a repository with another id or with no
    /// id. `ostree refs -c` lists the collection ref that the port writes.
    #[test]
    fn a_server_commit_equals_the_commit_of_the_tool_with_a_collection() {
        if !available("a_server_commit_equals_the_commit_of_the_tool_with_a_collection") {
            return;
        }
        const ID: &str = "org.example.T";
        let tmp = TmpDir::new("sc-tool-collection");
        let tree = tree(tmp.path());
        let tool = tool_repo(tmp.path(), Some(ID));
        let first = tool_commit(&tool, &tree, "first", &["-b", "main", "--bind-ref=v1"]);
        let second = tool_commit(&tool, &tree, "second", &["-b", "main"]);
        for (core, hello, relpath) in [
            (format!("collection-id={ID}\n"), None, "refs/heads"),
            (
                "collection-id=org.example.Other\n".to_owned(),
                Some(ID),
                "refs/mirrors/org.example.T",
            ),
            (String::new(), Some(ID), "refs/mirrors/org.example.T"),
        ] {
            let base = TmpDir::new("sc-tool-port");
            let port = new_repo(&base, RepoMode::Archive, &core);
            let policy = match hello {
                Some(id) => collections_policy(&[id], &["c:main", "t:v1", "t:v2"]),
                None => policy_allowing(&["c:main", "t:v1", "t:v2"]),
            };
            let sc = fields(&tool, &first, "main", &["v1"], &[], false);
            assert_eq!(
                server(&port, &policy, hello, &tool, &first, sc),
                first,
                "{core} {hello:?}"
            );
            let sc = fields(&tool, &second, "main", &[], &[], false);
            assert_eq!(
                server(&port, &policy, hello, &tool, &second, sc),
                second,
                "{core} {hello:?}"
            );
            assert_eq!(ref_at(&port, &format!("{relpath}/main")), Some(second));
            assert_eq!(ref_at(&port, &format!("{relpath}/v1")), Some(first));
            tool_fsck(port.path());
            if hello.is_some() {
                let listed = run(port.path(), &["refs", "-c"]);
                assert!(listed.contains("(org.example.T, main)"), "{listed}");
                assert!(listed.contains("(org.example.T, v1)"), "{listed}");
            }
        }
    }
}