ostrya 0.11.2

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
//! GPG (OpenPGP) signature verification, keyring management, and signing.
//!
//! The `verify-gpg` feature turns on this module. [`GpgVerifier`],
//! [`GpgKey`], and [`MAX_KEYRING`] are items of the `ostrya-sign` crate,
//! re-exported here. The verifier parses keyrings and verifies signatures in
//! the process, with rPGP. This module adds the items that read ostree paths
//! and the keyring files of a repository: the trait [`FromOstreeKeyrings`],
//! [`Repo::gpg_import_keys`], and [`Repo::gpg_list_keys`]. The trait builds a
//! verifier from the keyrings of a repository handle, from the bytes of a
//! keyring, or from the global trusted keyrings.
//!
//! The `sign-gpg` feature adds `GpgSigner`, which signs with
//! `gpg --detach-sign`. This feature also turns on `verify-gpg`. `GpgSigner` is
//! an item of the `ostrya-sign` crate, re-exported here.
//!
//! This crate runs the `gpg` command for these operations only:
//!
//! - The signing run of `GpgSigner`.
//! - The secret-key listing of `GpgSigner`.
//! - The export of `GpgSigner` under the `receive` and `sign-gpg` features.
//!   It gives the minimal public certificate of a server signing key, so the
//!   receive path can recognize a signature that this key made.
//!
//! The private key stays with GnuPG and its agent. It never goes through this
//! crate.

use std::os::fd::BorrowedFd;
use std::path::{Path, PathBuf};

use ostrya_sign::{gpg_keyring_keys, gpg_merge_keyring, gpg_offered_keys};
use rustix::fs::{AtFlags, FileType, Mode, OFlags};
use rustix::io::Errno;

use crate::config::{remote_keyring_name, valid_remote_name};
use crate::error::{Error, Result};
use crate::remotes_dir;
use crate::repo::{RemotesDir, Repo};
use crate::sign::read_key_source;
use crate::summary::write_root_file_blocking;

#[cfg(feature = "sign-gpg")]
pub use ostrya_sign::GpgSigner;
pub use ostrya_sign::{GpgKey, GpgVerifier, MAX_KEYRING};

/// The system directory of the keyrings that every remote trusts.
const GLOBAL_TRUSTED_GPG_D: &str = "/usr/share/ostree/trusted.gpg.d";
/// The environment variable that overrides the global trusted-keyring
/// directory. The `ostree` command reads the same variable.
const OSTREE_GPG_HOME_ENV: &str = "OSTREE_GPG_HOME";

/// A GPG verifier that loads the keyrings that ostree trusts.
///
/// The `ostrya-sign` crate has no ostree search path, so this crate adds the
/// constructors as a trait. A caller brings the trait into scope to call them.
///
/// The keyring of a remote is `<remote>.trustedkeys.gpg` in the repository.
/// When the repository holds no keyring of the remote, the keyring is the file
/// of that name in the remotes directory of the handle.
/// [`for_remote_in`](FromOstreeKeyrings::for_remote_in) reads the keyring of a
/// remote through a repository handle, so it reads the same keyring as a pull
/// of that remote.
/// [`for_remote_keyrings`](FromOstreeKeyrings::for_remote_keyrings) takes the
/// bytes of that keyring from the caller.
/// [`from_system_trust`](FromOstreeKeyrings::from_system_trust) reads the
/// global trusted keyrings alone.
pub trait FromOstreeKeyrings: Sized {
    /// Creates a verifier from the keyrings that `remote` of the repository
    /// handle `repo` trusts, together with the `gpgkeypath` entries of the
    /// remote.
    ///
    /// The keyring of the remote is one of these two files:
    ///
    /// - `<remote>.trustedkeys.gpg` in the repository.
    /// - `<remote>.trustedkeys.gpg` in the remotes directory of the handle
    ///   ([`Repo::remotes_dir`]), only when the repository holds no keyring
    ///   of the remote. A handle with no remotes directory reads no such
    ///   file.
    ///
    /// The trusted set is the union of these keyrings, in load order:
    ///
    /// - The keyring of the remote.
    /// - The global trusted set of
    ///   [`from_system_trust`](FromOstreeKeyrings::from_system_trust), only
    ///   when neither file exists.
    /// - Each entry of `keypath`, which holds the values of the `gpgkeypath`
    ///   key of the remote.
    ///
    /// A file exists if its open does not give "No such file or directory".
    /// So an empty repository keyring hides the keyring of the remotes
    /// directory and stops the read of the global trusted directory. A
    /// symlink to no file is no keyring. The rules of
    /// [`for_remote_keyrings`](FromOstreeKeyrings::for_remote_keyrings) apply
    /// to each source. The call blocks the calling thread while it reads the
    /// keyrings.
    ///
    /// The provided method reads the keyring of the remote and gives its
    /// bytes to [`for_remote_keyrings`](FromOstreeKeyrings::for_remote_keyrings).
    /// The method of [`GpgVerifier`] names the keyring of the remotes
    /// directory in a refusal, as `the keyring '<path>' of the remotes
    /// directory`. `<path>` is the file name joined to the path of a
    /// [`Path`](RemotesDir::Path) value as given, or to the path of an
    /// [`InRepo`](RemotesDir::InRepo) value, relative to the repository. For
    /// a [`Fd`](RemotesDir::Fd) value, `<path>` is the file name alone.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidInput`] if [`valid_remote_name`] refuses `remote`.
    ///   The call refuses the name before it reads a keyring.
    /// - [`Error::Signature`] if a keyring of the remote cannot be opened or
    ///   read for a reason other than its absence, for example because it is
    ///   a directory. The message names the keyring. The call reads the
    ///   keyrings of the remote before the other sources, so this error comes
    ///   first.
    /// - The errors of
    ///   [`for_remote_keyrings`](FromOstreeKeyrings::for_remote_keyrings).
    fn for_remote_in(repo: &Repo, remote: &str, keypath: &[String]) -> Result<Self> {
        let keyring = read_remote_keyring(repo.repo_fd(), repo.remotes_dir(), remote)?;
        Self::for_remote_keyrings(keyring.map(|keyring| keyring.bytes), remote, keypath)
    }

    /// Creates a verifier from the whole trusted set of a remote.
    ///
    /// `repo_keyring` holds the bytes of the keyring of the remote, or `None`
    /// if the remote has no keyring. A caller with no repository handle reads
    /// this file itself and gives its bytes. To give the keyring that a pull
    /// reads, the caller reads `<remote>.trustedkeys.gpg` in the repository.
    /// If the open of that file gives "No such file or directory", the caller
    /// reads the file of that name in the remotes directory. A symlink to no
    /// file is an absent file. If neither file exists, `repo_keyring` is
    /// `None`. A pull trusts the union of these keyrings, in load order:
    ///
    /// - `repo_keyring`.
    /// - The global trusted set of
    ///   [`from_system_trust`](FromOstreeKeyrings::from_system_trust), only
    ///   when `repo_keyring` is `None`. An empty keyring exists, so it stops
    ///   the read of the global trusted directory.
    /// - Each entry of `keypath`, which holds the values of the `gpgkeypath`
    ///   key of the remote.
    ///
    /// The call reads no `<remote>.trustedkeys.gpg` file, in the repository
    /// or in a remotes directory. A refusal of `repo_keyring` names it as
    /// `the keyring '<remote>.trustedkeys.gpg'`.
    ///
    /// `repo_keyring` and each global keyring file must be a binary OpenPGP
    /// packet stream. The load refuses each keyring that
    /// [`add_binary_keyring`](GpgVerifier::add_binary_keyring) refuses, and
    /// the refusal names the keyring. Armor, a GnuPG keybox, bytes that do not
    /// frame as a whole packet stream, and a keyring over a limit are
    /// examples. An empty keyring adds no key.
    ///
    /// A `keypath` entry is a keyring file or a directory of keyring files.
    /// The load reads each regular file of a directory, with any name, in name
    /// order. It does not read a subdirectory or a symlink of the directory.
    /// If an entry cannot be read, the build fails. So a missing keyring path
    /// gives an error, and the trusted set does not become smaller with no
    /// message. The load skips a `keypath` file that does not hold the bytes
    /// `-----BEGIN PGP` and that is a GnuPG keybox or is not a whole OpenPGP
    /// packet stream. Such a file adds no key and gives no message. Each other
    /// `keypath` file that the load cannot read whole as a keyring fails the
    /// build: a file that cannot be read, a file over a limit of a keyring,
    /// armor that does not decode to a keyring, and a packet stream that the
    /// parser refuses.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidInput`] if [`valid_remote_name`] refuses `remote`.
    ///   The call refuses the name before it reads a keyring.
    /// - [`Error::Signature`] if the metadata of a `keypath` entry cannot be
    ///   read, or a directory that an entry names cannot be listed. The
    ///   message names `gpgkeypath` and the entry.
    /// - [`Error::Signature`] if the global trusted directory is read and
    ///   cannot be listed for a reason other than its absence. The message
    ///   names the directory.
    /// - The errors of
    ///   [`add_binary_keyring`](GpgVerifier::add_binary_keyring) for
    ///   `repo_keyring`.
    /// - The errors of
    ///   [`add_binary_keyring_files`](GpgVerifier::add_binary_keyring_files)
    ///   for each global keyring file.
    /// - The errors of
    ///   [`add_keyring_files_skipping`](GpgVerifier::add_keyring_files_skipping)
    ///   for each `keypath` file.
    fn for_remote_keyrings(
        repo_keyring: Option<Vec<u8>>,
        remote: &str,
        keypath: &[String],
    ) -> Result<Self>;

    /// Creates a verifier from the global trusted keyrings only.
    ///
    /// The global trusted set is the keyrings in the directory that the
    /// `OSTREE_GPG_HOME` environment variable names. If this variable is unset
    /// or empty, the directory is `/usr/share/ostree/trusted.gpg.d/`. The load
    /// takes each regular file whose name ends in `.gpg`, a hidden name and
    /// the name `.gpg` included. It skips a symlink, a subdirectory, and a
    /// name that ends in `.GPG`. The keyrings load in name order. No keyring
    /// of a remote takes part.
    ///
    /// Each file must be a binary OpenPGP packet stream. The load refuses
    /// each file that
    /// [`add_binary_keyring_files`](GpgVerifier::add_binary_keyring_files)
    /// refuses, and the refusal names the path. So an armored file, a GnuPG
    /// keybox, and a file that is not a keyring are refused. An empty file
    /// adds no key.
    ///
    /// This is the trust for a commit that names no remote. If the directory
    /// does not exist, the trusted set is empty.
    ///
    /// # Errors
    ///
    /// - [`Error::Signature`] if the directory cannot be listed for a reason
    ///   other than its absence. A path that names a regular file is an
    ///   example. The message names the directory.
    /// - The errors of
    ///   [`add_binary_keyring_files`](GpgVerifier::add_binary_keyring_files)
    ///   for each keyring file.
    fn from_system_trust() -> Result<Self>;
}

impl FromOstreeKeyrings for GpgVerifier {
    fn for_remote_in(repo: &Repo, remote: &str, keypath: &[String]) -> Result<GpgVerifier> {
        let keyring = read_remote_keyring(repo.repo_fd(), repo.remotes_dir(), remote)?;
        remote_keyrings(keyring, remote, keypath, true)
    }

    fn for_remote_keyrings(
        repo_keyring: Option<Vec<u8>>,
        remote: &str,
        keypath: &[String],
    ) -> Result<GpgVerifier> {
        let keyring = repo_keyring.map(|bytes| RemoteKeyring::in_repo(bytes, remote));
        remote_keyrings(keyring, remote, keypath, true)
    }

    fn from_system_trust() -> Result<GpgVerifier> {
        let mut verifier = GpgVerifier::default();
        verifier.add_binary_keyring_files(keyring_files_in(&global_trusted_dir())?)?;
        Ok(verifier)
    }
}

/// The keyring of a remote that a read found: the repository keyring, or the
/// keyring of the remotes directory of a handle.
pub(crate) struct RemoteKeyring {
    /// The bytes of the keyring.
    pub(crate) bytes: Vec<u8>,
    /// The name of the keyring in a refusal.
    pub(crate) subject: String,
}

impl RemoteKeyring {
    /// Returns the repository keyring of `remote`, with the bytes `bytes`.
    fn in_repo(bytes: Vec<u8>, remote: &str) -> RemoteKeyring {
        RemoteKeyring {
            bytes,
            subject: format!("the keyring '{}'", remote_keyring_name(remote)),
        }
    }
}

/// Reads the keyring of `remote` for the repository at `repo_fd`, with the
/// remotes directory `dir`.
///
/// The keyring is `<remote>.trustedkeys.gpg` of the repository. Only when no
/// file is at that name, the keyring is the file of that name in `dir`. If
/// `dir` is `None`, or neither file exists, the result is `None`. A file
/// exists if its open does not give "No such file or directory", so a
/// symlink to no file is no keyring, and an absent directory or an absent
/// parent of it holds no keyring. The read follows a symlink. The `ostree`
/// command was observed to follow a symlink at the repository keyring too.
///
/// The read refuses a remote name that [`valid_remote_name`] refuses with
/// [`Error::InvalidInput`], before it opens a file. Each other refusal is
/// [`Error::Signature`], and it names the keyring: a file that cannot be
/// opened for a reason other than its absence, a file that is not a regular
/// file, a file that cannot be read, and a file over [`MAX_KEYRING`]. The
/// call blocks the calling thread.
pub(crate) fn read_remote_keyring(
    repo_fd: BorrowedFd<'_>,
    dir: Option<&RemotesDir>,
    remote: &str,
) -> Result<Option<RemoteKeyring>> {
    if !valid_remote_name(remote) {
        return Err(Error::InvalidInput(format!(
            "invalid remote name: {remote}"
        )));
    }
    let name = remote_keyring_name(remote);
    let repo_subject = format!("the keyring '{name}'");
    if let Some(bytes) = read_keyring_at(repo_fd, Path::new(&name), &repo_subject)? {
        return Ok(Some(RemoteKeyring {
            bytes,
            subject: repo_subject,
        }));
    }
    let Some(dir) = dir else {
        return Ok(None);
    };
    let (base, at, display) = remotes_dir::entry(repo_fd, dir, &name);
    let subject = format!(
        "the keyring '{}' of the remotes directory",
        display.display()
    );
    Ok(read_keyring_at(base, &at, &subject)?.map(|bytes| RemoteKeyring { bytes, subject }))
}

/// Creates a verifier from the keyrings of a remote, as
/// [`for_remote_keyrings`](FromOstreeKeyrings::for_remote_keyrings) does.
///
/// `keyring` is the keyring of the remote that [`read_remote_keyring`] gives,
/// and a refusal of it names its subject. If `global` is `false`, the global
/// trusted set takes no part, and the trusted set is `keyring` and the
/// entries of `keypath` alone. If `global` is `true`, the global trusted set
/// takes part only when `keyring` is `None`.
///
/// The call refuses a name that [`valid_remote_name`] refuses with
/// [`Error::InvalidInput`], before it reads a keyring.
pub(crate) fn remote_keyrings(
    keyring: Option<RemoteKeyring>,
    remote: &str,
    keypath: &[String],
    global: bool,
) -> Result<GpgVerifier> {
    let global_dir = global.then(global_trusted_dir);
    remote_keyrings_in(keyring, remote, keypath, global_dir.as_deref())
}

/// Runs [`remote_keyrings`] with the global trusted directory `global_dir`,
/// or with no global trusted set if `global_dir` is `None`.
///
/// `keyring` takes only a binary packet stream, and so does each global
/// keyring file. The call lists `global_dir` only when `keyring` is `None`.
///
/// The call adds `keyring`, then lists `global_dir` and the `keypath`
/// entries, and reads their files.
fn remote_keyrings_in(
    keyring: Option<RemoteKeyring>,
    remote: &str,
    keypath: &[String],
    global_dir: Option<&Path>,
) -> Result<GpgVerifier> {
    if !valid_remote_name(remote) {
        return Err(Error::InvalidInput(format!(
            "invalid remote name: {remote}"
        )));
    }
    // The verifier is new, and an error drops it. So the result trusts all
    // the sources or the call fails.
    let mut verifier = GpgVerifier::default();
    let own_keyring = keyring.is_some();
    if let Some(keyring) = keyring {
        verifier.add_binary_keyring(&keyring.bytes, &keyring.subject)?;
    }
    let global = match global_dir {
        Some(dir) if !own_keyring => keyring_files_in(dir)?,
        _ => Vec::new(),
    };
    let keypath = keypath_files("gpgkeypath", keypath)?;
    verifier.add_binary_keyring_files(global)?;
    verifier.add_keyring_files_skipping(keypath)?;
    Ok(verifier)
}

/// Creates a verifier from the keyrings that `keypath` names, and from no
/// other source.
///
/// No keyring of a remote and no global trusted set take part. Each entry
/// is a keyring file or a directory of keyring files, as
/// [`for_remote_keyrings`](FromOstreeKeyrings::for_remote_keyrings) reads a
/// `keypath` entry. If an entry cannot be read, the build fails. The load
/// skips and refuses the files that
/// [`add_keyring_files_skipping`](GpgVerifier::add_keyring_files_skipping)
/// skips and refuses. `key` is the configuration key of the entries, and a
/// refusal names it.
///
/// If the keyrings hold no certificate, the build fails with
/// [`Error::Signature`]. An empty file, a directory with no regular file, and
/// a directory of files that the load skips are examples. A verifier with
/// no trusted key refuses every commit.
pub(crate) fn from_keypath(key: &str, keypath: &[String]) -> Result<GpgVerifier> {
    let mut verifier = GpgVerifier::default();
    verifier.add_keyring_files_skipping(keypath_files(key, keypath)?)?;
    if verifier.is_empty() {
        return Err(Error::Signature(format!(
            "{key} names no key: the keyrings it names hold no certificate"
        )));
    }
    Ok(verifier)
}

/// Methods that manage the GPG keyring of a remote.
impl Repo {
    /// Imports OpenPGP certificates into the trusted keyring of `remote`.
    ///
    /// The keyring is `<remote>.trustedkeys.gpg` at the repository root.
    /// Each stream of `keys` is a binary or ASCII-armored certificate stream,
    /// as an exported public keyring is. The call decodes each stream by
    /// itself, and joins the streams in order into one offered stream. The
    /// call returns the number of certificates that the keyring did not
    /// already hold.
    ///
    /// The call reads and writes the repository keyring alone. When the
    /// remotes directory of the handle ([`Repo::remotes_dir`]) holds the
    /// keyring of `remote` and no file is at the path of the repository
    /// keyring, the call creates the repository keyring. That keyring hides
    /// the keyring of the directory. To trust the keys of the directory too,
    /// import that keyring as well. The call copies the keys, so a later
    /// change to the keyring of the directory has no effect.
    ///
    /// If `key_ids` is not empty, the call imports only the keys of the
    /// offered stream that these selectors name. A selector is a fingerprint,
    /// a key id, or a user id substring.
    ///
    /// # Import rules
    ///
    /// The call replaces the keyring atomically. The read of the keyring
    /// follows a symlink, and a symlink to no file is no keyring. The write
    /// puts a regular file at the path of the symlink, and does not change the
    /// target. The keyring keeps the packet stream that it already held. The
    /// packets of each added certificate follow, as `keys` wrote them, with no
    /// Trust packets. The keyring is written in the binary form, so an armored
    /// keyring keeps its packets and loses its armor. `gpg` and the `ostree`
    /// command both read a keyring of this form.
    ///
    /// If the keyring already holds a key, the call keeps the certificate of
    /// that key as it is, and counts the key as already held. So a new user id
    /// or a new subkey of a held key gets into the keyring only through
    /// [`Repo::remove_remote_keyring`] and a new import. The `ostree` command
    /// merges the offered user ids, signatures, and subkeys into the held
    /// certificate. Both report `Imported 0 GPG keys`.
    ///
    /// Two statements are exceptions. Each one replaces the held certificate:
    ///
    /// - A key revocation signature that verifies under the key that it
    ///   revokes, or under a designated revoker of that key. So a revoked key
    ///   stops speaking for the remote.
    /// - A key expiry that is later than the expiry of the held certificate.
    ///   An absent expiry is later than any instant. So a key whose owner
    ///   extended its life speaks again.
    ///
    /// The call looks for a designated revoker in all certificates of the
    /// offered stream and of the keyring. Each replacement writes the keyring
    /// again, with no Trust packets, and the count still treats the key as
    /// already held. A bare revocation certificate holds no public-key packet
    /// and no certificate, so the call refuses it. The re-export of the revoked
    /// key is the stream that brings a revocation in.
    ///
    /// The offered streams are untrusted input. The sum of their lengths is
    /// at most 4 MiB, and the joined stream holds at most 256 certificates.
    /// No stream may be a GnuPG keybox. The offered stream and the keyring of
    /// the remote both go through the reader of a verification load. So if
    /// either stream does not frame to its end, the import fails, and the
    /// keyring does not change.
    ///
    /// # Locks
    ///
    /// The call takes the repository lock shared, as
    /// [`LockKind`](crate::LockKind) describes. It then takes the update lock,
    /// as [`Repo::begin_update`] does. It reads, merges, and writes the keyring
    /// under both locks, so two imports into one keyring keep the keys of
    /// both. The call parses the offered streams before it takes the locks.
    /// [`UpdateGuard`](crate::UpdateGuard) describes a caller that holds a
    /// guard of this repository.
    ///
    /// # Errors
    ///
    /// For each error, the keyring does not change. The one exception is a
    /// failed sync of the repository directory, which comes after the rename
    /// of the new keyring.
    ///
    /// - [`Error::InvalidInput`] if [`valid_remote_name`] refuses `remote`.
    ///   The call refuses the name before it parses `keys` and before it
    ///   takes a lock.
    /// - [`Error::Signature`] if the keyring cannot be opened or read for a
    ///   reason other than its absence, or is not a regular file. The message
    ///   names the keyring.
    /// - [`Error::Signature`] if the keyring is over 4 MiB, or the streams of
    ///   `keys` together are over 4 MiB. The call refuses the size of the
    ///   keyring before it reads the keyring.
    /// - [`Error::Signature`] if the offered stream or the keyring holds more
    ///   than 256 certificates, or if a stream of `keys` or the keyring is a
    ///   GnuPG keybox.
    /// - [`Error::Signature`] if the offered stream or the keyring does not
    ///   parse as an OpenPGP keyring.
    /// - [`Error::Signature`] if the offered stream holds no certificate, or a
    ///   selector in `key_ids` names no key in it.
    /// - [`Error::Signature`] if an armored stream is not valid UTF-8.
    /// - [`Error::Core`] if the armor of a stream holds text that is not valid
    ///   base64.
    /// - [`Error::Core`] if `[core] fsync`, `[core] locking`, or
    ///   `[core] lock-timeout-secs` has a value that does not parse.
    /// - [`Error::InvalidFormat`] if `[core] lock-timeout-secs` is less than
    ///   `-1`.
    /// - [`Error::LockTimeout`] if the wait for a lock passes
    ///   `[core] lock-timeout-secs`.
    /// - [`Error::Io`] if a lock file, the write of the keyring, or the sync
    ///   of the repository directory fails.
    pub async fn gpg_import_keys(
        &self,
        remote: &str,
        keys: &[&[u8]],
        key_ids: &[String],
    ) -> Result<usize> {
        if !valid_remote_name(remote) {
            return Err(Error::InvalidInput(format!(
                "invalid remote name: {remote}"
            )));
        }
        let name = remote_keyring_name(remote);
        let fsync = self.config().fsync()?;
        let subject = format!("the keyring '{name}'");
        // Parsing untrusted certificate streams is CPU work over owned copies
        // of its inputs, so it runs on the blocking pool. The offered streams
        // are read before the locks, and only the read of the keyring, the
        // merge, and the write run under them. The read refuses streams over
        // `MAX_KEYRING` together before it reads a byte. So such streams get
        // no copy, and their read runs here.
        let len: u64 = keys.iter().map(|stream| stream.len() as u64).sum();
        let offered = if len > MAX_KEYRING {
            gpg_offered_keys(keys, key_ids, &subject)
        } else {
            let offered: Vec<Vec<u8>> = keys.iter().map(|stream| stream.to_vec()).collect();
            let ids = key_ids.to_vec();
            let read_subject = subject.clone();
            ostrya_rt::unblock(move || {
                let streams: Vec<&[u8]> = offered.iter().map(Vec::as_slice).collect();
                gpg_offered_keys(&streams, &ids, &read_subject)
            })
            .await
        };
        self.write_locked(move |repo| {
            let existing =
                read_keyring_at(repo.repo_fd(), Path::new(&name), &subject)?.unwrap_or_default();
            let (imported, keyring) = gpg_merge_keyring(&existing, offered, &subject)?;
            write_root_file_blocking(repo.repo_fd(), &name, &keyring, fsync)?;
            Ok(imported)
        })
        .await
    }

    /// Returns the keys in the trusted keyring of `remote`.
    ///
    /// The keyring is `<remote>.trustedkeys.gpg` at the repository root. Only
    /// when the repository holds no such file, the keyring is
    /// `<remote>.trustedkeys.gpg` in the remotes directory of the handle
    /// ([`Repo::remotes_dir`]). The `ostree` command reads the two files in
    /// this way. A handle with no remotes directory reads the repository
    /// keyring alone. The read follows a symlink, and a symlink to no file is
    /// no keyring.
    ///
    /// The keys come in the order of their certificates in the keyring. If
    /// neither keyring exists, the list is empty.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidInput`] if [`valid_remote_name`] refuses `remote`.
    ///   The call refuses the name before it reads a keyring.
    /// - [`Error::Signature`] if a keyring cannot be opened or read for a
    ///   reason other than its absence, or is not a regular file. The message
    ///   names the keyring. A keyring of the remotes directory is named as
    ///   [`for_remote_in`](FromOstreeKeyrings::for_remote_in) names it.
    /// - [`Error::Signature`] if the keyring is over 4 MiB, is a GnuPG keybox,
    ///   holds more than 256 certificates, or does not parse as an OpenPGP
    ///   keyring.
    /// - [`Error::Signature`] if an armored keyring is not valid UTF-8.
    /// - [`Error::Core`] if the armor of the keyring holds text that is not
    ///   valid base64.
    pub async fn gpg_list_keys(&self, remote: &str) -> Result<Vec<GpgKey>> {
        if !valid_remote_name(remote) {
            return Err(Error::InvalidInput(format!(
                "invalid remote name: {remote}"
            )));
        }
        let repo = self.clone();
        let remote = remote.to_owned();
        // The read resolves the names through descriptors, and the parse is
        // CPU work, so both run in one job on the blocking pool.
        ostrya_rt::unblock(move || {
            let Some(keyring) = read_remote_keyring(repo.repo_fd(), repo.remotes_dir(), &remote)?
            else {
                return Ok(Vec::new());
            };
            Ok(gpg_keyring_keys(&keyring.bytes, &keyring.subject)?)
        })
        .await
    }
}

/// Returns the directory of the keyrings that every remote trusts.
///
/// If the `OSTREE_GPG_HOME` environment variable has a value that is not
/// empty, the result is this value. If not, the result is the system directory
/// `/usr/share/ostree/trusted.gpg.d`.
fn global_trusted_dir() -> PathBuf {
    match std::env::var_os(OSTREE_GPG_HOME_ENV) {
        Some(dir) if !dir.is_empty() => PathBuf::from(dir),
        _ => PathBuf::from(GLOBAL_TRUSTED_GPG_D),
    }
}

/// Returns the keyring files in `dir`, the global trusted directory, sorted by
/// name.
///
/// The list holds each regular file whose name ends in the bytes `.gpg`. So
/// it holds a hidden name such as `.a.gpg`, and the name `.gpg` itself. It
/// does not hold a name that ends in `.GPG`. The file type comes from the
/// directory entry, and the call does not follow a symlink. So the list holds
/// no subdirectory, no symlink, no fifo, and no socket.
///
/// On a file system that gives no file type in the directory entry, the call
/// reads the type with `lstat`. If the entry is removed after the listing,
/// that read finds no file, and the list does not hold the entry.
///
/// If the directory does not exist, the result is an empty list. If the
/// directory cannot be listed for another reason, the call fails with
/// [`Error::Signature`], and the message names the directory. A path that
/// names a file that is not a directory is an example.
fn keyring_files_in(dir: &Path) -> Result<Vec<PathBuf>> {
    let refuse = |e: std::io::Error| {
        Error::Signature(format!(
            "the global trusted directory '{}' cannot be listed: {e}",
            dir.display()
        ))
    };
    let entries = match std::fs::read_dir(dir) {
        Ok(entries) => entries,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
        Err(e) => return Err(refuse(e)),
    };
    let mut files = Vec::new();
    for entry in entries {
        let entry = entry.map_err(refuse)?;
        if !entry.file_name().as_encoded_bytes().ends_with(b".gpg") {
            continue;
        }
        let file_type = match entry.file_type() {
            Ok(file_type) => file_type,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
            Err(e) => return Err(refuse(e)),
        };
        if file_type.is_file() {
            files.push(entry.path());
        }
    }
    files.sort();
    Ok(files)
}

/// Returns the keyring files that the entries of a keyring path name, in
/// entry order.
///
/// An entry that names a file gives that file. An entry that names a directory
/// gives the files of [`keypath_dir_files`]. If an entry cannot be read, the
/// refusal names `key`, the configuration key of the entry, and the text of
/// the entry.
fn keypath_files(key: &str, keypath: &[String]) -> Result<Vec<PathBuf>> {
    let mut paths = Vec::new();
    for entry in keypath {
        let path = Path::new(entry);
        let meta = std::fs::metadata(path).map_err(|e| keypath_refusal(key, entry, &e))?;
        if meta.is_dir() {
            paths.extend(keypath_dir_files(key, entry, path)?);
        } else {
            paths.push(path.to_owned());
        }
    }
    Ok(paths)
}

/// Returns the regular files in `dir`, the directory of the keyring path
/// entry `entry`, sorted by name.
///
/// The `ostree` command reads the files of a `gpgkeypath` directory in this
/// way. The list holds each regular file with any name, a hidden name
/// included. The file type comes from the directory entry, and the call does
/// not follow a symlink. So the list holds no subdirectory, no symlink, no
/// fifo, and no socket. The call does not read a subdirectory.
///
/// On a file system that gives no file type in the directory entry, the call
/// reads the type with `lstat`. If the entry is removed after the listing,
/// that read finds no file, and the list does not hold the entry.
///
/// If the directory cannot be listed, the refusal names `key`, the
/// configuration key of the entry, and the text of the entry.
fn keypath_dir_files(key: &str, entry: &str, dir: &Path) -> Result<Vec<PathBuf>> {
    let refuse = |e: std::io::Error| keypath_refusal(key, entry, &e);
    let mut files = Vec::new();
    for dirent in std::fs::read_dir(dir).map_err(refuse)? {
        let dirent = dirent.map_err(refuse)?;
        let file_type = match dirent.file_type() {
            Ok(file_type) => file_type,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
            Err(e) => return Err(refuse(e)),
        };
        if file_type.is_file() {
            files.push(dirent.path());
        }
    }
    files.sort();
    Ok(files)
}

/// Returns the refusal of the keyring path entry `entry`, which cannot be
/// read. `key` is the configuration key of the entry.
fn keypath_refusal(key: &str, entry: &str, e: &std::io::Error) -> Error {
    Error::Signature(format!("{key} entry '{entry}' cannot be read: {e}"))
}

/// Reads the keyring at `path`, resolved against `base`, through
/// [`read_key_source`], the reader of each key source.
///
/// Returns `None` if no file is at `path`. If a symlink is at `path`, the
/// read follows it. The reader accepts only a regular file, up to
/// [`MAX_KEYRING`]. A refusal names `subject`, so an operator can find the
/// keyring.
///
/// The call checks the type before it opens the file, so it opens no device
/// and no socket. It opens the file with `O_NONBLOCK`, and the reader checks
/// the type again, so a fifo that replaces the file after the check does not
/// block the read.
///
/// A keyring has its own limit, because it holds certificates and no base64
/// lines. The call refuses a keyring over the limit by name, and does not read
/// the part under the limit. `gpgkeypath` applies the same rule to an entry
/// that names nothing.
fn read_keyring_at(base: BorrowedFd<'_>, path: &Path, subject: &str) -> Result<Option<Vec<u8>>> {
    let refuse_open = |e: Errno| Error::Signature(format!("{subject} cannot be opened: {e}"));
    match rustix::fs::statat(base, path, AtFlags::empty()) {
        Ok(stat) if FileType::from_raw_mode(stat.st_mode) == FileType::RegularFile => {}
        Ok(_) => return Err(Error::Signature(format!("{subject} is not a regular file"))),
        Err(Errno::NOENT) => return Ok(None),
        Err(e) => return Err(refuse_open(e)),
    }
    // `NONBLOCK` makes a fifo answer the open at once. Without it, the open
    // waits for a writer. On a regular file, the flag has no effect on the
    // read that the reader makes.
    let fd = match rustix::fs::openat(
        base,
        path,
        OFlags::RDONLY | OFlags::NONBLOCK | OFlags::NOCTTY | OFlags::CLOEXEC,
        Mode::empty(),
    ) {
        Ok(fd) => fd,
        Err(Errno::NOENT) => return Ok(None),
        Err(e) => return Err(refuse_open(e)),
    };
    Ok(Some(read_key_source(
        std::fs::File::from(fd),
        subject,
        MAX_KEYRING,
    )?))
}

/// Returns a scratch directory path for one test fixture, unique in the
/// process.
///
/// The path is the GnuPG home of the `gpg` runs of the fixture, or a directory
/// of keyring files. The fixtures of this module get their paths from this
/// function.
#[cfg(test)]
fn scratch_dir() -> PathBuf {
    use std::sync::atomic::{AtomicU64, Ordering};

    static COUNTER: AtomicU64 = AtomicU64::new(0);
    std::env::temp_dir().join(format!(
        "ostrya-gpg-{}-{}",
        std::process::id(),
        COUNTER.fetch_add(1, Ordering::Relaxed)
    ))
}

/// Stops each GnuPG daemon of the home directory `dir`, and removes its
/// socket directory.
///
/// GnuPG makes the socket directory under the user runtime directory. It names
/// that directory from the path string of `dir`, so the call also works after
/// the removal of `dir`. The fixtures of this module call it before they
/// remove their home. The call ignores failures.
#[cfg(test)]
fn remove_home_sockets(dir: &Path) {
    use std::process::{Command, Stdio};

    for action in [&["--kill", "all"][..], &["--remove-socketdir"][..]] {
        let _ = Command::new("gpgconf")
            .arg("--homedir")
            .arg(dir)
            .args(action)
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status();
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use std::os::fd::{AsFd, OwnedFd};

    use super::*;

    /// The global trusted directory gives each regular file whose name ends
    /// in `.gpg`, a hidden name and the name `.gpg` included, sorted by name.
    /// The list holds no other name, no subdirectory, no symlink, and no fifo.
    #[cfg(unix)]
    #[test]
    fn keyring_files_in_selects_gpg_and_sorts() {
        use std::os::unix::fs::symlink;

        let dir = scratch_dir();
        std::fs::create_dir_all(dir.join("d.gpg")).unwrap();
        for name in [
            "b.gpg",
            "a.gpg",
            ".gpg",
            ".a.gpg",
            "a.GPG",
            "a.gpg.bak",
            "notes.txt",
            "keyring",
            "d.gpg/c.gpg",
        ] {
            std::fs::write(dir.join(name), b"").unwrap();
        }
        symlink(dir.join("b.gpg"), dir.join("link.gpg")).unwrap();
        match std::process::Command::new("mkfifo")
            .arg(dir.join("fifo.gpg"))
            .status()
        {
            Ok(status) if status.success() => {}
            other => eprintln!("skipping the fifo arm: mkfifo did not create a fifo: {other:?}"),
        }
        let files = keyring_files_in(&dir);
        std::fs::remove_dir_all(&dir).unwrap();
        let names: Vec<String> = files
            .unwrap()
            .iter()
            .map(|p| p.file_name().unwrap().to_string_lossy().into_owned())
            .collect();
        assert_eq!(names, [".a.gpg", ".gpg", "a.gpg", "b.gpg"]);
    }

    #[test]
    fn keyring_files_in_missing_dir_is_empty() {
        assert!(keyring_files_in(&scratch_dir()).unwrap().is_empty());
    }

    /// A global trusted directory that cannot be listed fails the read with
    /// [`Error::Signature`], and the message names the directory. A path that
    /// names a regular file and a directory with mode 000 are the cases.
    #[cfg(unix)]
    #[test]
    fn a_global_directory_that_cannot_be_listed_is_refused_by_its_path() {
        use std::os::unix::fs::PermissionsExt;

        let dir = scratch_dir();
        let file = dir.join("file");
        let locked = dir.join("locked");
        std::fs::create_dir_all(&locked).unwrap();
        std::fs::write(&file, b"").unwrap();
        std::fs::write(locked.join("a.gpg"), b"").unwrap();
        std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o000)).unwrap();
        let listed = std::fs::read_dir(&locked).is_ok();
        let from_file = keyring_files_in(&file);
        let from_locked = keyring_files_in(&locked);
        std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o755)).unwrap();
        std::fs::remove_dir_all(&dir).unwrap();
        let mut cases = vec![(file, from_file)];
        // A process that holds the override of file permissions, as root
        // does, lists the directory. The arm then has no refusal to observe.
        if listed {
            eprintln!("skipping the mode 000 arm: the directory lists with mode 000");
        } else {
            cases.push((locked, from_locked));
        }
        for (path, result) in cases {
            let expected = format!(
                "the global trusted directory '{}' cannot be listed: ",
                path.display()
            );
            assert!(
                matches!(&result, Err(Error::Signature(m)) if m.starts_with(&expected)),
                "{:?}",
                result.err()
            );
        }
    }

    /// The global trusted directory is a fallback. A repository keyring that
    /// exists, an empty one included, stops its listing, so a file there that
    /// the load refuses gives no error. With no repository keyring, the load
    /// reads the directory and refuses the file by its path.
    #[test]
    fn a_repository_keyring_stops_the_read_of_the_global_directory() {
        let dir = scratch_dir();
        std::fs::create_dir_all(&dir).unwrap();
        let junk = dir.join("junk.gpg");
        std::fs::write(&junk, b"This directory holds the trusted keys.\n").unwrap();
        let remote = "ostrya-global-fallback-test";
        let empty = || Some(RemoteKeyring::in_repo(Vec::new(), remote));
        let with_keyring = remote_keyrings_in(empty(), remote, &[], Some(&dir));
        let without = remote_keyrings_in(None, remote, &[], Some(&dir));
        let no_global = remote_keyrings_in(None, remote, &[], None);
        std::fs::remove_dir_all(&dir).unwrap();
        assert!(with_keyring.unwrap().is_empty());
        assert!(no_global.unwrap().is_empty());
        let named = format!("the keyring '{}'", junk.display());
        assert!(
            matches!(&without, Err(Error::Signature(m)) if m.contains(&named)),
            "{:?}",
            without.err()
        );
    }

    /// A key in the global trusted directory takes part only when the
    /// repository holds no keyring of the remote. An armored file in the
    /// global directory and an armored repository keyring are refused by
    /// name.
    #[test]
    fn the_global_directory_trusts_its_keys_only_with_no_repository_keyring() {
        use ostrya_sign::Verifier as _;

        if !gpg_available() {
            assert!(
                std::env::var_os("OSTRYA_REQUIRE_GNUPG").is_none(),
                "OSTRYA_REQUIRE_GNUPG is set and `gpg` is not available"
            );
            eprintln!("skipping: gpg not available");
            return;
        }
        let repo_key = KeyFixture::new("Repository <repository@ostrya.example>");
        let global_key = KeyFixture::new("Global <global@ostrya.example>");
        let payload = b"global payload";
        let signature = global_key.sign(payload);
        let dir = scratch_dir();
        let global = dir.join("global");
        let armored = dir.join("armored");
        std::fs::create_dir_all(&global).unwrap();
        std::fs::create_dir_all(&armored).unwrap();
        std::fs::write(global.join("k.gpg"), global_key.export(false)).unwrap();
        std::fs::write(armored.join("k.gpg"), global_key.export(true)).unwrap();
        let remote = "ostrya-global-trust-test";
        let load = |repo_keyring: Option<Vec<u8>>, dir: &Path| {
            let keyring = repo_keyring.map(|bytes| RemoteKeyring::in_repo(bytes, remote));
            remote_keyrings_in(keyring, remote, &[], Some(dir))
        };
        let verdict = |verifier: Result<GpgVerifier>| {
            verifier
                .unwrap()
                .verify(payload, std::slice::from_ref(&signature))
                .unwrap()
                .valid
        };
        let fallback = verdict(load(None, &global));
        let shadowed = verdict(load(Some(repo_key.export(false)), &global));
        let armored_global = load(None, &armored);
        let armored_repo = load(Some(repo_key.export(true)), &global);
        std::fs::remove_dir_all(&dir).unwrap();
        assert!(fallback);
        assert!(!shadowed);
        let named = format!("the keyring '{}'", armored.join("k.gpg").display());
        assert!(
            matches!(&armored_global, Err(Error::Signature(m)) if m.contains(&named)
                && m.contains("armor")),
            "{:?}",
            armored_global.err()
        );
        let named = format!("the keyring '{remote}.trustedkeys.gpg'");
        assert!(
            matches!(&armored_repo, Err(Error::Signature(m)) if m.contains(&named)
                && m.contains("armor")),
            "{:?}",
            armored_repo.err()
        );
    }

    /// Opens the directory at `path` for the keyring read of a test.
    fn open_dir(path: &Path) -> OwnedFd {
        OwnedFd::from(std::fs::File::open(path).unwrap())
    }

    /// Reads the keyring of `remote` in the directory `repo` with the remotes
    /// directory `dir`, as a pull reads it.
    fn read_in(
        repo: &Path,
        dir: Option<&RemotesDir>,
        remote: &str,
    ) -> Result<Option<RemoteKeyring>> {
        read_remote_keyring(open_dir(repo).as_fd(), dir, remote)
    }

    /// Reads the keyring of `remote` as [`read_in`] does, and builds a
    /// verifier with the global trusted directory `global`.
    fn load_in(repo: &Path, dir: &RemotesDir, remote: &str, global: &Path) -> Result<GpgVerifier> {
        let keyring = read_in(repo, Some(dir), remote)?;
        remote_keyrings_in(keyring, remote, &[], Some(global))
    }

    /// A keyring of the remote in the remotes directory that exists, an empty
    /// one included, stops the listing of the global trusted directory, so a
    /// file there that the load refuses gives no error. A keyring of another
    /// remote in the remotes directory does not stop it, so the load reads
    /// the global directory and refuses the file by its path.
    #[test]
    fn a_keyring_of_the_remotes_directory_stops_the_read_of_the_global_directory() {
        let dir = scratch_dir();
        let repo = dir.join("repo");
        let global = dir.join("global");
        let remotes = dir.join("remotes.d");
        for path in [&repo, &global, &remotes] {
            std::fs::create_dir_all(path).unwrap();
        }
        let junk = global.join("junk.gpg");
        std::fs::write(&junk, b"This directory holds the trusted keys.\n").unwrap();
        let remote = "ostrya-dir-fallback-test";
        let remotes_dir = RemotesDir::path(&remotes);
        std::fs::write(remotes.join("other.trustedkeys.gpg"), b"").unwrap();
        let without = load_in(&repo, &remotes_dir, remote, &global);
        std::fs::write(remotes.join(remote_keyring_name(remote)), b"").unwrap();
        let with_keyring = load_in(&repo, &remotes_dir, remote, &global);
        std::fs::remove_dir_all(&dir).unwrap();
        assert!(with_keyring.unwrap().is_empty());
        let named = format!("the keyring '{}'", junk.display());
        assert!(
            matches!(&without, Err(Error::Signature(m)) if m.contains(&named)),
            "{:?}",
            without.err()
        );
    }

    /// A key in the global trusted directory takes part only when no keyring
    /// of the remote exists in the remotes directory. The key of the keyring
    /// of the remotes directory takes part. An armored keyring of the remotes
    /// directory is refused by its path, with the words "of the remotes
    /// directory".
    #[test]
    fn the_global_directory_trusts_its_keys_only_with_no_keyring_of_the_remotes_directory() {
        use ostrya_sign::Verifier as _;

        if !gpg_available() {
            assert!(
                std::env::var_os("OSTRYA_REQUIRE_GNUPG").is_none(),
                "OSTRYA_REQUIRE_GNUPG is set and `gpg` is not available"
            );
            eprintln!("skipping: gpg not available");
            return;
        }
        let dir_key = KeyFixture::new("Directory <directory@ostrya.example>");
        let global_key = KeyFixture::new("Global <global@ostrya.example>");
        let payload = b"directory payload";
        let dir_signature = dir_key.sign(payload);
        let global_signature = global_key.sign(payload);
        let dir = scratch_dir();
        let repo = dir.join("repo");
        let global = dir.join("global");
        let remotes = dir.join("remotes.d");
        for path in [&repo, &global, &remotes] {
            std::fs::create_dir_all(path).unwrap();
        }
        std::fs::write(global.join("k.gpg"), global_key.export(false)).unwrap();
        let remote = "ostrya-dir-trust-test";
        let keyring = remotes.join(remote_keyring_name(remote));
        let remotes_dir = RemotesDir::path(&remotes);
        let load = || load_in(&repo, &remotes_dir, remote, &global);
        let verdicts = |verifier: Result<GpgVerifier>| {
            let verifier = verifier.unwrap();
            [&global_signature, &dir_signature].map(|signature| {
                verifier
                    .verify(payload, std::slice::from_ref(signature))
                    .unwrap()
                    .valid
            })
        };
        let fallback = verdicts(load());
        std::fs::write(&keyring, dir_key.export(false)).unwrap();
        let shadowed = verdicts(load());
        std::fs::write(&keyring, dir_key.export(true)).unwrap();
        let armored = load();
        std::fs::remove_dir_all(&dir).unwrap();
        assert_eq!(fallback, [true, false]);
        assert_eq!(shadowed, [false, true]);
        let named = format!(
            "the keyring '{}' of the remotes directory",
            keyring.display()
        );
        assert!(
            matches!(&armored, Err(Error::Signature(m)) if m.contains(&named)
                && m.contains("armor")),
            "{:?}",
            armored.err()
        );
    }

    /// A repository keyring that exists hides the keyring of the remotes
    /// directory: the read does not open the file of the directory. A
    /// directory is at the path of the keyring of the remotes directory, so
    /// an open of that path gives a refusal. A repository keyring with bytes
    /// and an empty one give the repository keyring. A repository keyring
    /// that cannot be read is refused by its name, and the read does not go
    /// on to the remotes directory.
    #[test]
    fn the_repository_keyring_hides_the_keyring_of_the_remotes_directory() {
        let dir = scratch_dir();
        let repo = dir.join("repo");
        let remotes = dir.join("remotes.d");
        let remote = "ostrya-hide-test";
        let name = remote_keyring_name(remote);
        std::fs::create_dir_all(remotes.join(&name)).unwrap();
        std::fs::create_dir_all(&repo).unwrap();
        let remotes_dir = RemotesDir::path(&remotes);
        let read = || read_in(&repo, Some(&remotes_dir), remote);
        std::fs::write(repo.join(&name), b"repository bytes").unwrap();
        let with_bytes = read();
        std::fs::write(repo.join(&name), b"").unwrap();
        let empty = read();
        std::fs::remove_file(repo.join(&name)).unwrap();
        std::fs::create_dir(repo.join(&name)).unwrap();
        let unreadable = read();
        std::fs::remove_dir_all(&dir).unwrap();
        let subject = format!("the keyring '{name}'");
        for (result, bytes) in [(with_bytes, &b"repository bytes"[..]), (empty, &b""[..])] {
            let keyring = result.unwrap().unwrap();
            assert_eq!(keyring.bytes, bytes);
            assert_eq!(keyring.subject, subject);
        }
        let expected = format!("{subject} is not a regular file");
        assert!(
            matches!(&unreadable, Err(Error::Signature(m)) if *m == expected),
            "{:?}",
            unreadable.err().map(|e| e.to_string())
        );
    }

    /// A fifo and a character device 0:0 at the name of the repository
    /// keyring, and a symlink to each, are refused as files that are not
    /// regular files. The read checks the type before the open, so it does
    /// not open the device: an open of a device 0:0 gives an error, and the
    /// refusal would then be one of the open. A process needs no capability
    /// to create a character device 0:0.
    #[cfg(unix)]
    #[test]
    fn a_fifo_and_a_device_at_the_repository_keyring_are_refused_before_an_open() {
        let dir = scratch_dir();
        let repo = dir.join("repo");
        let remote = "ostrya-special-test";
        let name = remote_keyring_name(remote);
        std::fs::create_dir_all(&repo).unwrap();
        let fifo = dir.join("fifo");
        let device = dir.join("device");
        let special = [
            (&fifo, FileType::Fifo, 0),
            (
                &device,
                FileType::CharacterDevice,
                rustix::fs::makedev(0, 0),
            ),
        ];
        for (path, file_type, dev) in special {
            let mode = Mode::from_raw_mode(0o600);
            rustix::fs::mknodat(rustix::fs::CWD, path, file_type, mode, dev).unwrap();
        }
        let mut results = Vec::new();
        for target in [&fifo, &device] {
            std::fs::rename(target, repo.join(&name)).unwrap();
            results.push(read_in(&repo, None, remote));
            std::fs::rename(repo.join(&name), target).unwrap();
            std::os::unix::fs::symlink(target, repo.join(&name)).unwrap();
            results.push(read_in(&repo, None, remote));
            std::fs::remove_file(repo.join(&name)).unwrap();
        }
        std::fs::remove_dir_all(&dir).unwrap();
        let expected = format!("the keyring '{name}' is not a regular file");
        for result in results {
            assert!(
                matches!(&result, Err(Error::Signature(m)) if *m == expected),
                "{:?}",
                result.err().map(|e| e.to_string())
            );
        }
    }

    /// The read takes the keyring of the remotes directory when no file is at
    /// the name of the repository keyring, and when a symlink to no file is
    /// there.
    #[cfg(unix)]
    #[test]
    fn the_keyring_of_the_remotes_directory_is_read_when_the_repository_keyring_is_absent() {
        let dir = scratch_dir();
        let repo = dir.join("repo");
        let remotes = dir.join("remotes.d");
        let remote = "ostrya-absent-test";
        let name = remote_keyring_name(remote);
        std::fs::create_dir_all(&repo).unwrap();
        std::fs::create_dir_all(&remotes).unwrap();
        std::fs::write(remotes.join(&name), b"directory bytes").unwrap();
        let remotes_dir = RemotesDir::path(&remotes);
        let absent = read_in(&repo, Some(&remotes_dir), remote);
        std::os::unix::fs::symlink(dir.join("absent.gpg"), repo.join(&name)).unwrap();
        let dangling = read_in(&repo, Some(&remotes_dir), remote);
        std::fs::remove_dir_all(&dir).unwrap();
        let subject = format!(
            "the keyring '{}' of the remotes directory",
            remotes.join(&name).display()
        );
        for result in [absent, dangling] {
            let keyring = result.unwrap().unwrap();
            assert_eq!(keyring.bytes, b"directory bytes");
            assert_eq!(keyring.subject, subject);
        }
    }

    /// A keyring of the remotes directory that cannot be read is refused by
    /// its path, for each form of [`RemotesDir`]. A directory is at the path
    /// of the keyring. A [`Path`](RemotesDir::Path) value names the path as
    /// given, an [`InRepo`](RemotesDir::InRepo) value names it relative to
    /// the repository, and a [`Fd`](RemotesDir::Fd) value names the file
    /// alone. A remotes directory that is a regular file gives a refusal of
    /// the open.
    #[test]
    fn a_keyring_of_the_remotes_directory_that_cannot_be_read_is_refused_by_its_path() {
        let dir = scratch_dir();
        let repo = dir.join("repo");
        let remotes = repo.join("remotes.d");
        let remote = "ostrya-refusal-test";
        let name = remote_keyring_name(remote);
        std::fs::create_dir_all(remotes.join(&name)).unwrap();
        let file = dir.join("file");
        std::fs::write(&file, b"").unwrap();
        let forms = [
            (RemotesDir::path(&remotes), remotes.join(&name)),
            (
                RemotesDir::InRepo(PathBuf::from("remotes.d")),
                Path::new("remotes.d").join(&name),
            ),
            (RemotesDir::from(open_dir(&remotes)), PathBuf::from(&name)),
        ];
        let results: Vec<_> = forms
            .into_iter()
            .map(|(form, path)| (read_in(&repo, Some(&form), remote), path))
            .collect();
        let not_a_dir = read_in(&repo, Some(&RemotesDir::path(&file)), remote);
        std::fs::remove_dir_all(&dir).unwrap();
        for (result, path) in results {
            let expected = format!(
                "the keyring '{}' of the remotes directory is not a regular file",
                path.display()
            );
            assert!(
                matches!(&result, Err(Error::Signature(m)) if *m == expected),
                "{expected}: {:?}",
                result.err().map(|e| e.to_string())
            );
        }
        let expected = format!(
            "the keyring '{}' of the remotes directory cannot be opened: ",
            file.join(&name).display()
        );
        assert!(
            matches!(&not_a_dir, Err(Error::Signature(m)) if m.starts_with(&expected)),
            "{:?}",
            not_a_dir.err().map(|e| e.to_string())
        );
    }

    /// The read gives no keyring when neither file exists: with no remotes
    /// directory, with an absent remotes directory, and with an absent parent
    /// of it. The remotes directory of the first case holds a keyring of the
    /// remote, and the read does not take it.
    #[test]
    fn no_remotes_directory_and_an_absent_one_give_no_keyring() {
        let dir = scratch_dir();
        let repo = dir.join("repo");
        let remotes = dir.join("remotes.d");
        let remote = "ostrya-none-test";
        std::fs::create_dir_all(&repo).unwrap();
        std::fs::create_dir_all(&remotes).unwrap();
        std::fs::write(remotes.join(remote_keyring_name(remote)), b"").unwrap();
        let none = read_in(&repo, None, remote);
        let absent = read_in(&repo, Some(&RemotesDir::path(dir.join("absent"))), remote);
        let parent = RemotesDir::path(dir.join("absent").join("remotes.d"));
        let absent_parent = read_in(&repo, Some(&parent), remote);
        std::fs::remove_dir_all(&dir).unwrap();
        for result in [none, absent, absent_parent] {
            assert!(matches!(result, Ok(None)), "{:?}", result.err());
        }
    }

    /// The files of a `gpgkeypath` directory are its regular files with any
    /// name, sorted by name. The list holds no subdirectory, no symlink, and
    /// no fifo, and the read does not go into a subdirectory.
    #[cfg(unix)]
    #[test]
    fn keypath_dir_files_takes_each_regular_file_and_sorts() {
        use std::os::unix::fs::symlink;

        let dir = scratch_dir();
        std::fs::create_dir_all(dir.join("sub")).unwrap();
        for name in [
            "z.asc",
            "a.keyring",
            "README",
            ".gpg",
            ".a.gpg",
            "sub/b.gpg",
        ] {
            std::fs::write(dir.join(name), b"").unwrap();
        }
        symlink(dir.join("README"), dir.join("link.gpg")).unwrap();
        symlink(dir.join("absent"), dir.join("dangling.gpg")).unwrap();
        symlink(dir.join("sub"), dir.join("linkdir")).unwrap();
        match std::process::Command::new("mkfifo")
            .arg(dir.join("fifo.gpg"))
            .status()
        {
            Ok(status) if status.success() => {}
            other => eprintln!("skipping the fifo arm: mkfifo did not create a fifo: {other:?}"),
        }
        let files = keypath_dir_files("gpgkeypath", "keys", &dir);
        std::fs::remove_dir_all(&dir).unwrap();
        let names: Vec<String> = files
            .unwrap()
            .iter()
            .map(|p| p.file_name().unwrap().to_string_lossy().into_owned())
            .collect();
        assert_eq!(names, [".a.gpg", ".gpg", "README", "a.keyring", "z.asc"]);
    }

    /// A `gpgkeypath` directory that cannot be listed fails the read. The
    /// refusal names the configuration key and the entry.
    #[cfg(unix)]
    #[test]
    fn a_keypath_directory_that_cannot_be_listed_is_refused_by_the_entry() {
        use std::os::unix::fs::PermissionsExt;

        let dir = scratch_dir();
        let keys = dir.join("keys");
        std::fs::create_dir_all(&keys).unwrap();
        std::fs::write(keys.join("a.gpg"), b"").unwrap();
        std::fs::set_permissions(&keys, std::fs::Permissions::from_mode(0o000)).unwrap();
        let entry = keys.to_string_lossy().into_owned();
        let listed = std::fs::read_dir(&keys).is_ok();
        let result = keypath_files("gpgkeypath", std::slice::from_ref(&entry));
        std::fs::set_permissions(&keys, std::fs::Permissions::from_mode(0o755)).unwrap();
        std::fs::remove_dir_all(&dir).unwrap();
        // A process that holds the override of file permissions, as root
        // does, lists the directory. The case then has no refusal to observe.
        if listed {
            eprintln!("skipping: the directory lists with mode 000");
            return;
        }
        let expected = format!("gpgkeypath entry '{entry}' cannot be read: ");
        assert!(
            matches!(&result, Err(Error::Signature(m)) if m.starts_with(&expected)),
            "{:?}",
            result.err()
        );
    }

    /// An empty `gpgkeypath` entry names no file, so the read refuses it.
    /// The refusal names the configuration key and the empty entry.
    #[test]
    fn an_empty_keypath_entry_is_refused() {
        let result = keypath_files("gpgkeypath", &[String::new()]);
        assert!(
            matches!(
                &result,
                Err(Error::Signature(m)) if m.starts_with("gpgkeypath entry '' cannot be read: ")
            ),
            "{:?}",
            result.err()
        );
    }

    /// A keyring load refuses a remote name that [`valid_remote_name`]
    /// refuses, before it reads a keyring. A directory is at the path that
    /// the join of the name to the repository or to the remotes directory
    /// reaches, so a read of that path before the check gives
    /// [`Error::Signature`] with `is not a regular file`.
    #[test]
    fn a_remote_keyring_load_refuses_an_invalid_remote_name() {
        use crate::{CreateOptions, OpenOptions, RepoMode};

        let dir = scratch_dir();
        let root = dir.join("repo");
        let remotes = dir.join("remotes.d");
        std::fs::create_dir_all(&remotes).unwrap();
        std::fs::create_dir(dir.join("x.trustedkeys.gpg")).unwrap();
        let handle = ostrya_rt::block_on(async {
            Repo::create(&root, CreateOptions::new(RepoMode::Archive))
                .await
                .unwrap();
            OpenOptions::new()
                .remotes_dir(RemotesDir::path(&remotes))
                .open(&root)
                .await
                .unwrap()
        });
        let keyrings = GpgVerifier::for_remote_keyrings(None, "../x", &[]);
        let in_handle = GpgVerifier::for_remote_in(&handle, "../x", &[]);
        drop(handle);
        std::fs::remove_dir_all(&dir).unwrap();
        for result in [keyrings, in_handle] {
            assert!(
                matches!(
                    &result,
                    Err(Error::InvalidInput(m)) if m == "invalid remote name: ../x"
                ),
                "{:?}",
                result.err()
            );
        }
    }

    /// A remote keyring load reads the repository keyring before the
    /// `gpgkeypath` entries. A directory is at the path of the repository
    /// keyring, and the entry does not exist, so the refusal names the
    /// repository keyring.
    #[test]
    fn a_keypath_load_refuses_the_repository_keyring_first() {
        use crate::{CreateOptions, RepoMode};

        let dir = scratch_dir();
        let root = dir.join("repo");
        std::fs::create_dir_all(&dir).unwrap();
        let repo = ostrya_rt::block_on(async {
            Repo::create(&root, CreateOptions::new(RepoMode::Archive))
                .await
                .unwrap()
        });
        std::fs::create_dir(root.join("keypath-order.trustedkeys.gpg")).unwrap();
        let entry = dir.join("absent.gpg").to_string_lossy().into_owned();
        let result = GpgVerifier::for_remote_in(&repo, "keypath-order", &[entry]);
        drop(repo);
        std::fs::remove_dir_all(&dir).unwrap();
        assert!(
            matches!(
                &result,
                Err(Error::Signature(m))
                    if m == "the keyring 'keypath-order.trustedkeys.gpg' is not a regular file"
            ),
            "{:?}",
            result.err()
        );
    }

    /// A remote keyring load trusts the keys of a `gpgkeypath` entry. The
    /// repository holds no keyring of the remote, so the same load with no
    /// entry does not trust the key.
    #[test]
    fn a_keypath_load_trusts_the_keypath_entries() {
        use crate::{CreateOptions, RepoMode};
        use ostrya_sign::Verifier as _;

        if !gpg_available() {
            assert!(
                std::env::var_os("OSTRYA_REQUIRE_GNUPG").is_none(),
                "OSTRYA_REQUIRE_GNUPG is set and `gpg` is not available"
            );
            eprintln!("skipping: gpg not available");
            return;
        }
        let key = KeyFixture::new("Keypath <keypath@ostrya.example>");
        let payload = b"keypath payload";
        let signature = key.sign(payload);
        let dir = scratch_dir();
        let root = dir.join("repo");
        std::fs::create_dir_all(&dir).unwrap();
        let repo = ostrya_rt::block_on(async {
            Repo::create(&root, CreateOptions::new(RepoMode::Archive))
                .await
                .unwrap()
        });
        let keyring = dir.join("k.gpg");
        std::fs::write(&keyring, key.export(false)).unwrap();
        let entry = keyring.to_string_lossy().into_owned();
        let trusted = GpgVerifier::for_remote_in(&repo, "keypath-trust", &[entry])
            .unwrap()
            .verify(payload, std::slice::from_ref(&signature))
            .unwrap();
        let untrusted = GpgVerifier::for_remote_in(&repo, "keypath-trust", &[])
            .unwrap()
            .verify(payload, std::slice::from_ref(&signature))
            .unwrap();
        drop(repo);
        std::fs::remove_dir_all(&dir).unwrap();
        assert!(trusted.valid, "{:?}", trusted.signatures);
        assert!(!untrusted.valid, "{:?}", untrusted.signatures);
    }

    /// An import refuses an offered stream over [`MAX_KEYRING`] by its size,
    /// and the keyring of the remote does not change. The case runs with no
    /// keyring and with an empty keyring.
    #[test]
    fn an_import_over_the_keyring_ceiling_is_refused() {
        use crate::{CreateOptions, RepoMode};

        let dir = scratch_dir();
        let root = dir.join("repo");
        std::fs::create_dir_all(&dir).unwrap();
        let keyring = root.join("origin.trustedkeys.gpg");
        let oversized = vec![0u8; MAX_KEYRING as usize + 1];
        let refusals = ostrya_rt::block_on(async {
            let repo = Repo::create(&root, CreateOptions::new(RepoMode::Archive))
                .await
                .unwrap();
            let absent = repo.gpg_import_keys("origin", &[&oversized], &[]).await;
            let absent_after = std::fs::exists(&keyring).unwrap();
            std::fs::write(&keyring, b"").unwrap();
            let empty = repo.gpg_import_keys("origin", &[&oversized], &[]).await;
            let empty_after = std::fs::read(&keyring).unwrap();
            (absent, absent_after, empty, empty_after)
        });
        std::fs::remove_dir_all(&dir).unwrap();
        let (absent, absent_after, empty, empty_after) = refusals;
        let text = "signature: the keyring to import is over the 4194304-byte ceiling";
        assert_eq!(absent.unwrap_err().to_string(), text);
        assert!(!absent_after, "the refused import wrote a keyring");
        assert_eq!(empty.unwrap_err().to_string(), text);
        assert!(
            empty_after.is_empty(),
            "the refused import changed the keyring"
        );
    }

    /// An import refuses a remote name that [`valid_remote_name`] refuses,
    /// before it reads a keyring. A directory is at the path that the join of
    /// the name to the repository reaches, so a read of that path before the
    /// check gives a different error. The directory stays.
    #[test]
    fn an_import_refuses_an_invalid_remote_name() {
        use crate::{CreateOptions, RepoMode};

        let dir = scratch_dir();
        let root = dir.join("repo");
        std::fs::create_dir_all(&dir).unwrap();
        let decoy = dir.join("x.trustedkeys.gpg");
        std::fs::create_dir(&decoy).unwrap();
        let result = ostrya_rt::block_on(async {
            let repo = Repo::create(&root, CreateOptions::new(RepoMode::Archive))
                .await
                .unwrap();
            repo.gpg_import_keys("../x", &[], &[]).await
        });
        let decoy_stays = decoy.is_dir();
        std::fs::remove_dir_all(&dir).unwrap();
        assert!(
            matches!(&result, Err(Error::InvalidInput(m)) if m == "invalid remote name: ../x"),
            "{:?}",
            result.err()
        );
        assert!(decoy_stays, "the refused import changed the decoy");
    }

    /// A key listing refuses a remote name that [`valid_remote_name`]
    /// refuses, before it reads a keyring. A directory is at the path that
    /// the join of the name to the repository reaches, so a read of that
    /// path before the check gives a different error.
    #[test]
    fn a_key_listing_refuses_an_invalid_remote_name() {
        use crate::{CreateOptions, RepoMode};

        let dir = scratch_dir();
        let root = dir.join("repo");
        std::fs::create_dir_all(&dir).unwrap();
        let decoy = dir.join("x.trustedkeys.gpg");
        std::fs::create_dir(&decoy).unwrap();
        let result = ostrya_rt::block_on(async {
            let repo = Repo::create(&root, CreateOptions::new(RepoMode::Archive))
                .await
                .unwrap();
            repo.gpg_list_keys("../x").await
        });
        let decoy_stays = decoy.is_dir();
        std::fs::remove_dir_all(&dir).unwrap();
        assert!(
            matches!(&result, Err(Error::InvalidInput(m)) if m == "invalid remote name: ../x"),
            "{:?}",
            result.err()
        );
        assert!(decoy_stays, "the refused listing changed the decoy");
    }

    /// A key listing reads the keyring of the remotes directory when the
    /// repository holds no keyring of the remote, as the `ostree` command
    /// does. An empty repository keyring hides it. A handle with no remotes
    /// directory does not read it.
    #[test]
    fn a_key_listing_reads_the_keyring_of_the_remotes_directory() {
        use crate::{CreateOptions, OpenOptions, RepoMode};

        if !gpg_available() {
            assert!(
                std::env::var_os("OSTRYA_REQUIRE_GNUPG").is_none(),
                "OSTRYA_REQUIRE_GNUPG is set and `gpg` is not available"
            );
            eprintln!("skipping: gpg not available");
            return;
        }
        let key = KeyFixture::new("Listed <listed@ostrya.example>");
        let dir = scratch_dir();
        let root = dir.join("repo");
        let remotes = dir.join("remotes.d");
        std::fs::create_dir_all(&remotes).unwrap();
        std::fs::write(remotes.join("origin.trustedkeys.gpg"), key.export(false)).unwrap();
        let listings = ostrya_rt::block_on(async {
            Repo::create(&root, CreateOptions::new(RepoMode::Archive))
                .await
                .unwrap();
            let with_dir = OpenOptions::new()
                .remotes_dir(RemotesDir::path(&remotes))
                .open(&root)
                .await
                .unwrap();
            let without_dir = Repo::open(&root).await.unwrap();
            let fallback = with_dir.gpg_list_keys("origin").await.unwrap();
            let no_dir = without_dir.gpg_list_keys("origin").await.unwrap();
            std::fs::write(root.join("origin.trustedkeys.gpg"), b"").unwrap();
            let hidden = with_dir.gpg_list_keys("origin").await.unwrap();
            (fallback, no_dir, hidden)
        });
        std::fs::remove_dir_all(&dir).unwrap();
        let (fallback, no_dir, hidden) = listings;
        let fingerprints: Vec<&str> = fallback.iter().map(|k| k.fingerprint.as_str()).collect();
        assert_eq!(fingerprints, [key.fingerprint()]);
        assert!(no_dir.is_empty(), "{no_dir:?}");
        assert!(hidden.is_empty(), "{hidden:?}");
    }

    /// A key listing refuses a keyring of the remotes directory that cannot
    /// be read by its path. A directory is at the path of the keyring.
    #[test]
    fn a_key_listing_refuses_a_keyring_of_the_remotes_directory_by_its_path() {
        use crate::{CreateOptions, OpenOptions, RepoMode};

        let dir = scratch_dir();
        let root = dir.join("repo");
        let keyring = dir.join("remotes.d").join("origin.trustedkeys.gpg");
        std::fs::create_dir_all(&keyring).unwrap();
        let result = ostrya_rt::block_on(async {
            Repo::create(&root, CreateOptions::new(RepoMode::Archive))
                .await
                .unwrap();
            OpenOptions::new()
                .remotes_dir(RemotesDir::path(dir.join("remotes.d")))
                .open(&root)
                .await
                .unwrap()
                .gpg_list_keys("origin")
                .await
        });
        std::fs::remove_dir_all(&dir).unwrap();
        let expected = format!(
            "the keyring '{}' of the remotes directory is not a regular file",
            keyring.display()
        );
        assert!(
            matches!(&result, Err(Error::Signature(m)) if *m == expected),
            "{:?}",
            result.err().map(|e| e.to_string())
        );
    }

    /// Returns `true` if the `gpg` binary runs.
    ///
    /// The trust group cases in `verify::trust` and the receive signer cases
    /// in `receive::signer` build their fixtures with `gpg`. Each of these
    /// cases skips if the binary is absent. If `OSTRYA_REQUIRE_GNUPG` is set,
    /// the absence of the binary fails the case.
    pub(crate) fn gpg_available() -> bool {
        std::process::Command::new("gpg")
            .arg("--version")
            .output()
            .is_ok_and(|out| out.status.success())
    }

    /// A private GnuPG home with new ed25519 signing keys that have no
    /// passphrase, under the test scratch tree.
    ///
    /// Each `gpg` run names this directory with `--homedir`. So the GnuPG home
    /// and the agents of the user that runs the tests take no part. When the
    /// fixture drops, it stops the GnuPG daemons of the directory, removes
    /// their socket directory, and removes the directory.
    pub(crate) struct KeyFixture {
        pub(crate) dir: PathBuf,
    }

    impl KeyFixture {
        /// Creates a home directory with one key for `uid` that never expires.
        pub(crate) fn new(uid: &str) -> KeyFixture {
            let fixture = KeyFixture {
                dir: KeyFixture::make_dir(),
            };
            fixture.add_key(uid);
            fixture
        }

        /// Creates a new directory under the test scratch tree that only its
        /// owner can read. `gpg` requires this of a home directory.
        fn make_dir() -> PathBuf {
            use std::os::unix::fs::DirBuilderExt;
            let dir = scratch_dir();
            std::fs::DirBuilder::new().mode(0o700).create(&dir).unwrap();
            dir
        }

        /// Generates one more key, for `uid`, in the same home directory.
        pub(crate) fn add_key(&self, uid: &str) {
            self.generate(uid, "never");
        }

        /// Generates one key for `uid` with the lifetime `expiry`.
        fn generate(&self, uid: &str, expiry: &str) {
            let status = self
                .gpg()
                .args(["--pinentry-mode", "loopback", "--passphrase", ""])
                .args(["--quick-gen-key", uid, "ed25519", "sign", expiry])
                .status()
                .unwrap();
            assert!(status.success(), "gpg --quick-gen-key failed");
        }

        /// Returns one detached signature over `payload` by the first key of
        /// the home.
        pub(crate) fn sign(&self, payload: &[u8]) -> Vec<u8> {
            let file = self.dir.join("payload");
            std::fs::write(&file, payload).unwrap();
            let out = self
                .gpg()
                .args(["--pinentry-mode", "loopback", "--passphrase", ""])
                .args(["--detach-sign", "--output", "-", "--local-user"])
                .arg(format!("{}!", self.fingerprint()))
                .arg(&file)
                .output()
                .unwrap();
            assert!(out.status.success() && !out.stdout.is_empty());
            out.stdout
        }

        /// Returns the fingerprint of the first key of the home, in uppercase
        /// hex.
        pub(crate) fn fingerprint(&self) -> String {
            let out = self
                .gpg()
                .args(["--with-colons", "--list-keys"])
                .output()
                .unwrap();
            assert!(out.status.success(), "gpg --list-keys failed");
            String::from_utf8_lossy(&out.stdout)
                .lines()
                .filter_map(|line| line.strip_prefix("fpr:"))
                .filter_map(|rest| rest.split(':').nth(8).map(str::to_owned))
                .next()
                .unwrap()
        }

        /// Returns a `gpg` command for this home directory, in batch mode.
        fn gpg(&self) -> std::process::Command {
            let mut cmd = std::process::Command::new("gpg");
            cmd.arg("--homedir").arg(&self.dir).arg("--batch");
            cmd
        }

        /// Returns the exported public keyring, binary or ASCII-armored.
        pub(crate) fn export(&self, armored: bool) -> Vec<u8> {
            let mut cmd = self.gpg();
            cmd.arg("--export");
            if armored {
                cmd.arg("--armor");
            }
            let out = cmd.output().unwrap();
            assert!(out.status.success() && !out.stdout.is_empty());
            out.stdout
        }
    }

    impl Drop for KeyFixture {
        fn drop(&mut self) {
            remove_home_sockets(&self.dir);
            let _ = std::fs::remove_dir_all(&self.dir);
        }
    }
}