ostrya 0.7.0

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
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
//! The pull of the refs of a collection from the remotes that serve it.
//!
//! Each test serves remote repositories from in-process static file servers.
//! A server answers the ref listing with fixed bytes that the test builds
//! with `Repo::build_ref_listing`, whatever the query. A server with no such
//! bytes answers 404, as a static server does, and the pull reads its
//! summary.
//!
//! ostrya builds the remote repositories. A remote with the collection id
//! `org.example.Os` lists its refs in field 0, and a remote with another id
//! lists them in its collection map.

mod common;
#[path = "common/pull.rs"]
mod pull;

use std::os::fd::AsFd;
use std::path::Path;
use std::sync::{Arc, Mutex};

use common::{TmpDir, anyone, store_with_field2};
use ostrya::endpoint::EndpointOverrides;
use ostrya::{
    Checksum, CollectionPullEvent, CollectionPullOptions, CollectionPullStats, CollectionRef,
    CommitModifier, CommitModifierFlags, CommitOptions, CreateOptions, Ed25519Signer, Error,
    FsckOptions, MutableTree, ObjectType, PullFlags, PullOptions, RefSelector, Repo, RepoMode,
    SummaryOptions, TimestampCheck, Type, Value,
};
use ostrya_rt::block_on;
use pull::*;

/// The collection that the tests pull.
const ID: &str = "org.example.Os";
/// Another collection.
const OTHER: &str = "org.example.Other";
/// The ref that the tests pull.
const REF: &str = "os/stable";
/// A fixed ed25519 keypair. A destination trusts its public key.
const SECRET_B64: &str =
    "o74ME/dmhvDeYf64dDJQY8kX2piK0M/nyIRWVi30i6DCOzRsHVcvgYToz6zOb5OvK/v8nH6KfLR3dfdsn6ZSyQ==";
const PUBLIC_B64: &str = "wjs0bB1XL4GE6M+szm+Tryv7/Jx+iny0d3X3bJ+mUsk=";
/// A second keypair. It stands for a key that a destination does not trust.
const OTHER_SECRET_B64: &str =
    "5ILWxT+l9G/u3h0BptRpmSi35C9uog7YDdD+Fp1Xk+Hz52p0NlYh6xBA73kJEJKhKbbnjcE0rsWA5XA/K5Sq5Q==";
/// The path of the ref listing on a server.
const LISTING: &str = "_ostrya/refs/v1";

/// The bindings of a test commit.
#[derive(Clone, Copy)]
struct Bind<'a> {
    /// The `ostree.collection-binding`, or `None` for no key.
    collection: Option<&'a str>,
    /// The `ostree.ref-binding`, or `None` for no key.
    refs: Option<&'a [&'a str]>,
}

/// The bindings of a commit of `REF` in `ID`.
const BOUND: Bind<'static> = Bind {
    collection: Some(ID),
    refs: Some(&[REF]),
};

/// Returns a metadata entry `key` with the value `value` of type `ty`.
fn entry(key: &str, ty: &str, value: Value) -> Value {
    Value::Tuple(vec![
        Value::Str(key.to_owned()),
        Value::Variant(Box::new((Type::parse(ty).unwrap(), value))),
    ])
}

/// Creates an archive repository at `dir/name` with the collection id `own`.
async fn create(dir: &Path, name: &str, own: Option<&str>) -> Repo {
    Repo::create(
        &dir.join(name),
        CreateOptions {
            mode: RepoMode::Archive,
            collection_id: own.map(str::to_owned),
        },
    )
    .await
    .unwrap()
}

/// Commits a small tree with the marker `marker` into `repo`, with the
/// timestamp `timestamp` and the bindings `bind`. Writes the commit to the
/// ref `name` of the collection `collection`, or to the local ref `name` if
/// `collection` is `None`.
async fn commit(
    repo: &Repo,
    dir: &Path,
    marker: &str,
    collection: Option<&str>,
    name: &str,
    timestamp: u64,
    bind: Bind<'_>,
) -> Checksum {
    let sub = format!("src-{marker}");
    build_tree(&dir.join(&sub), marker.as_bytes());
    let txn = repo.transaction().await.unwrap();
    let mut mtree = MutableTree::new();
    let mut modifier = CommitModifier::new(
        CommitModifierFlags::SKIP_XATTRS | CommitModifierFlags::CANONICAL_PERMISSIONS,
    );
    let dfd = std::fs::File::open(dir).unwrap();
    txn.write_dfd_to_mtree(
        dfd.as_fd(),
        Path::new(&sub),
        &mut mtree,
        Some(&mut modifier),
    )
    .await
    .unwrap();
    let root = txn.write_mtree(&mut mtree).await.unwrap();
    let mut metadata = Vec::new();
    if let Some(id) = bind.collection {
        metadata.push(entry(
            "ostree.collection-binding",
            "s",
            Value::Str(id.to_owned()),
        ));
    }
    if let Some(refs) = bind.refs {
        let refs = refs.iter().map(|r| Value::Str((*r).to_owned())).collect();
        metadata.push(entry("ostree.ref-binding", "as", Value::Array(refs)));
    }
    let checksum = txn
        .write_commit(
            CommitOptions {
                subject: Some(marker.to_owned()),
                timestamp: Some(timestamp),
                metadata: Some(Value::Array(metadata)),
                ..CommitOptions::default()
            },
            &root,
        )
        .await
        .unwrap();
    match collection {
        Some(id) => txn.set_collection_ref(&CollectionRef::new(id, name), Some(&checksum)),
        None => txn.set_ref(name, Some(&checksum)),
    }
    txn.commit().await.unwrap();
    checksum
}

/// Writes the summary of `repo`.
async fn summarize(repo: &Repo) {
    repo.regenerate_summary(&SummaryOptions {
        last_modified: Some(FIXED_TS),
        ..SummaryOptions::default()
    })
    .await
    .unwrap();
}

/// Makes `server` answer each ref listing request with the listing of `repo`
/// for the names `names` in `ID`, or for each ref of `ID` if `names` is
/// empty.
async fn serve_listing(server: &RepoServer, repo: &Repo, names: &[&str]) {
    server.tamper("_ostrya/refs/v1", listing(repo, names).await);
}

/// Returns the ref listing of `repo` for the names `names` in `ID`, or for
/// each ref of `ID` and no local ref if `names` is empty.
async fn listing(repo: &Repo, names: &[&str]) -> Vec<u8> {
    let refs = if names.is_empty() {
        RefSelector::parse(["!**"]).unwrap()
    } else {
        RefSelector::parse(names.iter().copied()).unwrap()
    };
    let entries: Vec<String> = if names.is_empty() {
        vec![format!("{ID}/**")]
    } else {
        names.iter().map(|name| format!("{ID}/{name}")).collect()
    };
    let collections = RefSelector::parse(entries.iter().map(String::as_str)).unwrap();
    repo_listing(repo, &refs, Some(&collections)).await
}

/// Returns the ref listing of `repo` for the selectors `refs` and
/// `collections`.
async fn repo_listing(
    repo: &Repo,
    refs: &RefSelector,
    collections: Option<&RefSelector>,
) -> Vec<u8> {
    repo.build_ref_listing(refs, collections, &anyone())
        .await
        .unwrap()
        .bytes
}

/// Creates the destination repository at `dir/dest`, with the collection id
/// `own` and the remotes `remotes`: each a name, a URL, and extra keys.
async fn dest(dir: &Path, own: Option<&str>, remotes: &[(&str, &str, &str)]) -> Repo {
    let path = dir.join("dest");
    drop(create(dir, "dest", own).await);
    let config = path.join("config");
    let mut text = std::fs::read_to_string(&config).unwrap();
    for (name, url, extra) in remotes {
        text.push_str(&format!(
            "\n[remote \"{name}\"]\nurl={url}\ngpg-verify=false\ncollection-id={ID}\n{extra}"
        ));
    }
    std::fs::write(&config, text).unwrap();
    Repo::open(&path).await.unwrap()
}

/// Returns the options of a pull of `refs`, with no retry, and a sink for
/// its events.
fn options(refs: &[&str]) -> (CollectionPullOptions, Arc<Mutex<Vec<CollectionPullEvent>>>) {
    let events = Arc::new(Mutex::new(Vec::new()));
    let sink = Arc::clone(&events);
    let mut opts = CollectionPullOptions::default();
    opts.pull = PullOptions {
        refs: refs.iter().map(|r| (*r).to_owned()).collect(),
        overrides: EndpointOverrides {
            max_retries: Some(0),
            ..EndpointOverrides::default()
        },
        ..PullOptions::default()
    };
    opts.on_event = Some(Box::new(move |event| sink.lock().unwrap().push(event)));
    (opts, events)
}

/// Returns the commit of the collection ref `name` of `ID` in `repo`.
async fn mirror(repo: &Repo, name: &str) -> Option<Checksum> {
    repo.list_mirror_refs()
        .await
        .unwrap()
        .into_iter()
        .find(|(id, n, _)| id == ID && n == name)
        .map(|(_, _, commit)| commit)
}

/// Starts a server over `root` that answers each ref listing request with
/// `status`.
async fn listing_status(root: &Path, status: u16) -> RepoServer {
    let server = RepoServer::start(root, false).await;
    server.answer_status(LISTING, status);
    server
}

/// Appends `text` to the config of the repository `repo` at `dir/dest`, and
/// opens the repository again.
async fn reconfigure(dir: &Path, text: &str) -> Repo {
    let path = dir.join("dest/config");
    let mut config = std::fs::read_to_string(&path).unwrap();
    config.push_str(text);
    std::fs::write(&path, config).unwrap();
    Repo::open(&dir.join("dest")).await.unwrap()
}

/// Asserts that `repo` holds no ref and no commit object.
async fn assert_nothing_written(repo: &Repo) {
    assert!(repo.list_refs(None).await.unwrap().is_empty());
    assert!(repo.list_mirror_refs().await.unwrap().is_empty());
    assert!(repo.list_remote_refs().await.unwrap().is_empty());
}

/// Returns `true` if `server` received a request for an object.
fn objects_requested(server: &RepoServer) -> bool {
    server
        .seen()
        .iter()
        .any(|path| path.starts_with("objects/"))
}

// --- selection ---------------------------------------------------------------

/// The newest commit wins. The first remote serves no listing and lists the
/// ref in field 0 of its summary. The second remote serves a listing with
/// the ref in its collection map. The pull takes the objects from the second
/// remote alone, and writes the collection ref and no remote ref.
#[test]
fn the_newest_commit_wins_from_a_listing_or_a_summary() {
    block_on(async {
        let dir = TmpDir::new("pull-collection-newest");
        let a = create(dir.path(), "a", Some(ID)).await;
        commit(&a, dir.path(), "a", None, REF, FIXED_TS, BOUND).await;
        summarize(&a).await;
        let b = create(dir.path(), "b", Some(OTHER)).await;
        let newer = commit(&b, dir.path(), "b", Some(ID), REF, FIXED_TS + 10, BOUND).await;
        let server_a = RepoServer::start(&dir.path().join("a"), false).await;
        let server_b = RepoServer::start(&dir.path().join("b"), false).await;
        serve_listing(&server_b, &b, &[REF]).await;
        let dest = dest(
            dir.path(),
            None,
            &[("a", &server_a.url(), ""), ("b", &server_b.url(), "")],
        )
        .await;

        let (opts, events) = options(&[REF]);
        let pulled: CollectionPullStats =
            dest.pull_collection(ID, &["a", "b"], opts).await.unwrap();

        assert!(events.lock().unwrap().is_empty());
        assert_eq!(pulled.refs.len(), 1);
        assert_eq!(pulled.refs[0].name, REF);
        assert_eq!(pulled.refs[0].remote, "b");
        assert_eq!(pulled.refs[0].commit, newer);
        assert_eq!(pulled.refs[0].timestamp, FIXED_TS + 10);
        assert_eq!(mirror(&dest, REF).await, Some(newer));
        assert!(dest.list_refs(None).await.unwrap().is_empty());
        assert!(dest.list_remote_refs().await.unwrap().is_empty());
        assert!(dest.fsck(&FsckOptions::default()).await.unwrap().is_ok());
        assert_eq!(pulled.stats.content_fetched, 4);

        // The remote with no listing answered the probe from its summary,
        // and the pull asked it for no object.
        // With no summary policy, the probe reads no `summary.sig`.
        assert_eq!(server_a.seen(), [LISTING, "summary"]);
        assert!(objects_requested(&server_b));
    });
}

/// A tie of timestamps goes to the earlier remote of the list.
#[test]
fn a_tie_goes_to_the_earlier_remote() {
    block_on(async {
        let dir = TmpDir::new("pull-collection-tie");
        let a = create(dir.path(), "a", Some(ID)).await;
        let first = commit(&a, dir.path(), "a", None, REF, FIXED_TS, BOUND).await;
        summarize(&a).await;
        let b = create(dir.path(), "b", Some(ID)).await;
        commit(&b, dir.path(), "b", None, REF, FIXED_TS, BOUND).await;
        summarize(&b).await;
        let server_a = RepoServer::start(&dir.path().join("a"), false).await;
        let server_b = RepoServer::start(&dir.path().join("b"), false).await;
        let dest = dest(
            dir.path(),
            None,
            &[("a", &server_a.url(), ""), ("b", &server_b.url(), "")],
        )
        .await;

        let (opts, _) = options(&[REF]);
        let pulled = dest.pull_collection(ID, &["a", "b"], opts).await.unwrap();
        assert_eq!(pulled.refs[0].remote, "a");
        assert_eq!(mirror(&dest, REF).await, Some(first));
        assert!(!objects_requested(&server_b));
        // The pull from the selected remote uses the summary that the probe
        // read, and does not fetch it again.
        assert_eq!(server_a.requests_for("summary"), 1);
        assert_eq!(server_a.requests_for("summary.sig"), 0);
        assert_eq!(server_a.requests_for("config"), 1);
    });
}

/// A remote that answers the listing with a server error is skipped with an
/// event, and the pull takes the ref from the next remote. The skipped
/// remote holds a newer commit, and the pull reads no summary from it.
#[test]
fn a_failing_remote_is_skipped() {
    block_on(async {
        let dir = TmpDir::new("pull-collection-skip");
        let a = create(dir.path(), "a", Some(ID)).await;
        commit(&a, dir.path(), "a", None, REF, FIXED_TS + 10, BOUND).await;
        summarize(&a).await;
        let b = create(dir.path(), "b", Some(ID)).await;
        let tip = commit(&b, dir.path(), "b", None, REF, FIXED_TS, BOUND).await;
        summarize(&b).await;
        let server_a = listing_status(&dir.path().join("a"), 500).await;
        let server_b = RepoServer::start(&dir.path().join("b"), false).await;
        let dest = dest(
            dir.path(),
            None,
            &[("a", &server_a.url(), ""), ("b", &server_b.url(), "")],
        )
        .await;

        let (opts, events) = options(&[REF]);
        let pulled = dest.pull_collection(ID, &["a", "b"], opts).await.unwrap();
        assert_eq!(pulled.refs[0].remote, "b");
        assert_eq!(mirror(&dest, REF).await, Some(tip));
        let events = events.lock().unwrap();
        assert_eq!(events.len(), 1, "{events:?}");
        let CollectionPullEvent::RemoteSkipped {
            remote, message, ..
        } = &events[0]
        else {
            panic!("{events:?}");
        };
        assert_eq!(remote, "a");
        assert!(message.contains("500"), "{message}");
        assert!(events[0].to_string().starts_with("remote a skipped: "));
        assert_eq!(server_a.seen(), [LISTING]);
    });
}

/// A remote that refuses the listing request with a status from 400 to 499
/// serves no listing, and the pull reads its summary.
#[test]
fn a_client_error_on_the_listing_reads_the_summary() {
    block_on(async {
        for status in [400, 403] {
            let dir = TmpDir::new("pull-collection-client-error");
            let a = create(dir.path(), "a", Some(ID)).await;
            let tip = commit(&a, dir.path(), "a", None, REF, FIXED_TS, BOUND).await;
            summarize(&a).await;
            let server_a = listing_status(&dir.path().join("a"), status).await;
            let dest = dest(dir.path(), None, &[("a", &server_a.url(), "")]).await;

            let (opts, events) = options(&[REF]);
            let pulled = dest.pull_collection(ID, &["a"], opts).await.unwrap();
            assert_eq!(pulled.refs[0].remote, "a", "{status}");
            assert_eq!(mirror(&dest, REF).await, Some(tip), "{status}");
            assert!(events.lock().unwrap().is_empty(), "{status}");
            assert_eq!(server_a.statuses_for(LISTING), [status]);
            assert_eq!(server_a.requests_for("summary"), 1, "{status}");
        }
    });
}

/// A remote whose summary fails the summary policy of the remote is
/// skipped: a summary with no `summary.sig`, and a summary that an untrusted
/// key signed. The remote serves a listing, and the pull does not read it,
/// because no signature covers it.
#[test]
fn a_summary_that_fails_the_policy_skips_the_remote() {
    block_on(async {
        for signer in [None, Some(OTHER_SECRET_B64)] {
            let dir = TmpDir::new("pull-collection-summary-policy");
            let a = create(dir.path(), "a", Some(ID)).await;
            commit(&a, dir.path(), "a", None, REF, FIXED_TS + 10, BOUND).await;
            summarize(&a).await;
            if let Some(secret) = signer {
                let signer = Ed25519Signer::from_base64(secret).unwrap();
                a.sign_summary(&signer).await.unwrap();
            }
            let b = create(dir.path(), "b", Some(ID)).await;
            let tip = commit(&b, dir.path(), "b", None, REF, FIXED_TS, BOUND).await;
            summarize(&b).await;
            let server_a = RepoServer::start(&dir.path().join("a"), false).await;
            serve_listing(&server_a, &a, &[REF]).await;
            let server_b = RepoServer::start(&dir.path().join("b"), false).await;
            let policy =
                format!("sign-verify-summary=ed25519\nverification-ed25519-key={PUBLIC_B64}\n");
            let dest = dest(
                dir.path(),
                None,
                &[("a", &server_a.url(), &policy), ("b", &server_b.url(), "")],
            )
            .await;

            let (opts, events) = options(&[REF]);
            let pulled = dest.pull_collection(ID, &["a", "b"], opts).await.unwrap();
            assert_eq!(pulled.refs[0].remote, "b");
            assert_eq!(mirror(&dest, REF).await, Some(tip));
            let events = events.lock().unwrap();
            assert!(
                matches!(&events[..], [CollectionPullEvent::RemoteSkipped { remote, .. }] if remote == "a"),
                "{events:?}"
            );
            assert!(!objects_requested(&server_a));
            assert_eq!(server_a.requests_for(LISTING), 0);
        }
    });
}

/// Under a summary policy, the pull reads the refs from the verified
/// summary, also when the remote serves a listing. The listing names a newer
/// commit of another repository, and the pull takes the commit of the
/// summary. The pull from the selected remote reads the summary and its
/// signature one time, in the probe.
#[test]
fn a_summary_policy_takes_the_refs_from_the_verified_summary() {
    block_on(async {
        for with_listing in [false, true] {
            let dir = TmpDir::new("pull-collection-signed-summary");
            let a = create(dir.path(), "a", Some(ID)).await;
            let tip = commit(&a, dir.path(), "a", None, REF, FIXED_TS, BOUND).await;
            summarize(&a).await;
            let signer = Ed25519Signer::from_base64(SECRET_B64).unwrap();
            a.sign_summary(&signer).await.unwrap();
            let decoy = create(dir.path(), "decoy", Some(ID)).await;
            commit(&decoy, dir.path(), "d", None, REF, FIXED_TS + 10, BOUND).await;
            let server_a = RepoServer::start(&dir.path().join("a"), false).await;
            if with_listing {
                serve_listing(&server_a, &decoy, &[REF]).await;
            }
            let policy =
                format!("sign-verify-summary=ed25519\nverification-ed25519-key={PUBLIC_B64}\n");
            let dest = dest(dir.path(), None, &[("a", &server_a.url(), &policy)]).await;

            let (opts, events) = options(&[REF]);
            let pulled = dest.pull_collection(ID, &["a"], opts).await.unwrap();
            assert!(events.lock().unwrap().is_empty());
            assert_eq!(pulled.refs[0].commit, tip, "{with_listing}");
            assert_eq!(pulled.refs[0].timestamp, FIXED_TS);
            assert_eq!(mirror(&dest, REF).await, Some(tip));
            assert_eq!(server_a.requests_for(LISTING), 0);
            assert_eq!(server_a.requests_for("summary"), 1);
            assert_eq!(server_a.requests_for("summary.sig"), 1);
        }
    });
}

/// A ref that no remote lists fails the pull before an object request. A
/// pull in which each remote fails fails too.
#[test]
fn a_ref_that_no_remote_lists_fails_the_pull() {
    block_on(async {
        let dir = TmpDir::new("pull-collection-missing");
        let a = create(dir.path(), "a", Some(ID)).await;
        commit(&a, dir.path(), "a", None, REF, FIXED_TS, BOUND).await;
        summarize(&a).await;
        let server_a = RepoServer::start(&dir.path().join("a"), false).await;
        let server_dead = listing_status(dir.path(), 500).await;
        let dest = dest(
            dir.path(),
            None,
            &[("a", &server_a.url(), ""), ("dead", &server_dead.url(), "")],
        )
        .await;

        let (opts, _) = options(&[REF, "os/absent"]);
        let err = dest
            .pull_collection(ID, &["a", "dead"], opts)
            .await
            .unwrap_err();
        assert!(
            matches!(&err, Error::Pull(m) if m.contains("os/absent") && m.contains(ID)),
            "{err:?}"
        );
        assert!(!objects_requested(&server_a));
        assert_nothing_written(&dest).await;

        let (opts, events) = options(&[REF]);
        let err = dest.pull_collection(ID, &["dead"], opts).await.unwrap_err();
        assert!(matches!(err, Error::Pull(_)), "{err:?}");
        assert_eq!(events.lock().unwrap().len(), 1);
    });
}

/// With no ref name, the pull takes each ref of the collection that a remote
/// lists, and no ref of another collection. The listing request selects no
/// local ref, so a remote of another collection sends none.
#[test]
fn no_ref_name_pulls_each_ref_of_the_collection() {
    block_on(async {
        let dir = TmpDir::new("pull-collection-all");
        let b = create(dir.path(), "b", Some(OTHER)).await;
        let stable = commit(&b, dir.path(), "s", Some(ID), REF, FIXED_TS, BOUND).await;
        let next_bind = Bind {
            collection: Some(ID),
            refs: Some(&["os/next"]),
        };
        let next = commit(
            &b,
            dir.path(),
            "n",
            Some(ID),
            "os/next",
            FIXED_TS,
            next_bind,
        )
        .await;
        let other_bind = Bind {
            collection: Some(OTHER),
            refs: Some(&["other"]),
        };
        commit(&b, dir.path(), "o", None, "other", FIXED_TS, other_bind).await;
        let server_b = RepoServer::start(&dir.path().join("b"), false).await;
        serve_listing(&server_b, &b, &[]).await;
        let dest = dest(dir.path(), None, &[("b", &server_b.url(), "")]).await;

        let (opts, _) = options(&[]);
        let pulled = dest.pull_collection(ID, &["b"], opts).await.unwrap();
        let names: Vec<&str> = pulled.refs.iter().map(|r| r.name.as_str()).collect();
        assert_eq!(names, ["os/next", REF]);
        assert_eq!(mirror(&dest, REF).await, Some(stable));
        assert_eq!(mirror(&dest, "os/next").await, Some(next));
        assert_eq!(dest.list_mirror_refs().await.unwrap().len(), 2);
        assert!(dest.list_refs(None).await.unwrap().is_empty());
        assert_eq!(
            server_b.queries_for(LISTING),
            ["pattern=%21%2A%2A&collection=org.example.Os%2F%2A%2A"]
        );
    });
}

/// With no ref name, a remote whose own collection id is the pulled id
/// lists the refs in field 0. The pull asks it a second time, for each local
/// ref and no collection ref.
#[test]
fn no_ref_name_asks_a_remote_of_the_collection_for_its_local_refs() {
    block_on(async {
        let dir = TmpDir::new("pull-collection-all-own");
        let a = create(dir.path(), "a", Some(ID)).await;
        let stable = commit(&a, dir.path(), "a", None, REF, FIXED_TS, BOUND).await;
        let server_a = RepoServer::start(&dir.path().join("a"), false).await;
        let refs = RefSelector::parse(std::iter::empty()).unwrap();
        let every = repo_listing(&a, &refs, None).await;
        server_a.tamper(LISTING, every);
        let dest = dest(dir.path(), None, &[("a", &server_a.url(), "")]).await;

        let (opts, _) = options(&[]);
        let pulled = dest.pull_collection(ID, &["a"], opts).await.unwrap();
        assert_eq!(pulled.refs.len(), 1);
        assert_eq!(mirror(&dest, REF).await, Some(stable));
        assert_eq!(
            server_a.queries_for(LISTING),
            ["pattern=%21%2A%2A&collection=org.example.Os%2F%2A%2A", ""]
        );
    });
}

/// With no ref name, a pull in which no remote lists a ref of the collection
/// fails before an object request, and writes nothing.
#[test]
fn no_ref_name_and_no_listed_ref_fails_the_pull() {
    block_on(async {
        let dir = TmpDir::new("pull-collection-none");
        let b = create(dir.path(), "b", Some(OTHER)).await;
        let other_bind = Bind {
            collection: Some(OTHER),
            refs: Some(&["other"]),
        };
        commit(&b, dir.path(), "o", None, "other", FIXED_TS, other_bind).await;
        summarize(&b).await;
        let server_b = RepoServer::start(&dir.path().join("b"), false).await;
        let dest = dest(dir.path(), None, &[("b", &server_b.url(), "")]).await;

        let (opts, events) = options(&[]);
        let err = dest.pull_collection(ID, &["b"], opts).await.unwrap_err();
        assert!(
            matches!(&err, Error::Pull(m) if m == &format!("no remote lists a ref of collection '{ID}'")),
            "{err:?}"
        );
        assert!(events.lock().unwrap().is_empty());
        assert!(!objects_requested(&server_b));
        assert_nothing_written(&dest).await;
    });
}

// --- checks of the commit ----------------------------------------------------

/// The pull refuses a commit bound to another collection, a commit with no
/// collection binding, and a commit whose ref binding does not hold the ref.
/// A commit with no ref binding passes.
#[test]
fn the_bindings_of_each_commit_are_checked() {
    block_on(async {
        let cases: [(&str, Bind<'static>, bool); 4] = [
            (
                "other collection",
                Bind {
                    collection: Some(OTHER),
                    refs: Some(&[REF]),
                },
                false,
            ),
            (
                "no collection binding",
                Bind {
                    collection: None,
                    refs: Some(&[REF]),
                },
                false,
            ),
            (
                "other ref",
                Bind {
                    collection: Some(ID),
                    refs: Some(&["os/next"]),
                },
                false,
            ),
            (
                "no ref binding",
                Bind {
                    collection: Some(ID),
                    refs: None,
                },
                true,
            ),
        ];
        for (tag, bind, passes) in cases {
            let dir = TmpDir::new("pull-collection-bindings");
            let a = create(dir.path(), "a", Some(ID)).await;
            let tip = commit(&a, dir.path(), "a", None, REF, FIXED_TS, bind).await;
            summarize(&a).await;
            let server_a = RepoServer::start(&dir.path().join("a"), false).await;
            let dest = dest(dir.path(), None, &[("a", &server_a.url(), "")]).await;

            let (opts, _) = options(&[REF]);
            let pulled = dest.pull_collection(ID, &["a"], opts).await;
            if passes {
                pulled.unwrap();
                assert_eq!(mirror(&dest, REF).await, Some(tip), "{tag}");
                continue;
            }
            let err = pulled.unwrap_err();
            assert!(matches!(err, Error::Pull(_)), "{tag}: {err:?}");
            assert_nothing_written(&dest).await;
        }
    });
}

/// A commit whose timestamp differs from the timestamp that the remote
/// listed fails the pull.
#[test]
fn a_commit_with_another_timestamp_than_listed_fails() {
    block_on(async {
        let dir = TmpDir::new("pull-collection-listed-timestamp");
        let b = create(dir.path(), "b", Some(OTHER)).await;
        commit(&b, dir.path(), "b", Some(ID), REF, FIXED_TS, BOUND).await;
        let server_b = RepoServer::start(&dir.path().join("b"), false).await;
        // The listing states one second more than the commit holds.
        let mut bytes = listing(&b, &[REF]).await;
        let real = FIXED_TS.to_be_bytes();
        let at = bytes
            .windows(8)
            .position(|w| w == real)
            .expect("the listing holds the timestamp");
        bytes[at..at + 8].copy_from_slice(&(FIXED_TS + 1).to_be_bytes());
        server_b.tamper("_ostrya/refs/v1", bytes);
        let dest = dest(dir.path(), None, &[("b", &server_b.url(), "")]).await;

        let (opts, _) = options(&[REF]);
        let err = dest.pull_collection(ID, &["b"], opts).await.unwrap_err();
        assert!(
            matches!(&err, Error::Pull(m) if m.contains("timestamp")),
            "{err:?}"
        );
        assert_nothing_written(&dest).await;
    });
}

/// The keys of the selected remote apply. The newer remote requires a
/// signature, and its unsigned commit fails the pull. With an older commit
/// there, the other remote is selected, and its policy passes the commit.
#[test]
fn the_keys_of_the_selected_remote_apply() {
    block_on(async {
        for newer_signed_remote in [true, false] {
            let dir = TmpDir::new("pull-collection-keys");
            let a = create(dir.path(), "a", Some(ID)).await;
            let a_ts = if newer_signed_remote {
                FIXED_TS
            } else {
                FIXED_TS + 10
            };
            let a_tip = commit(&a, dir.path(), "a", None, REF, a_ts, BOUND).await;
            summarize(&a).await;
            let b = create(dir.path(), "b", Some(ID)).await;
            commit(&b, dir.path(), "b", None, REF, FIXED_TS + 5, BOUND).await;
            summarize(&b).await;
            let server_a = RepoServer::start(&dir.path().join("a"), false).await;
            let server_b = RepoServer::start(&dir.path().join("b"), false).await;
            let policy = format!("sign-verify=ed25519\nverification-ed25519-key={PUBLIC_B64}\n");
            let dest = dest(
                dir.path(),
                None,
                &[("a", &server_a.url(), ""), ("b", &server_b.url(), &policy)],
            )
            .await;

            let (opts, _) = options(&[REF]);
            let pulled = dest.pull_collection(ID, &["a", "b"], opts).await;
            if newer_signed_remote {
                let err = pulled.unwrap_err();
                assert!(matches!(err, Error::Signature(_)), "{err:?}");
                assert_nothing_written(&dest).await;
            } else {
                assert_eq!(pulled.unwrap().refs[0].remote, "a");
                assert_eq!(mirror(&dest, REF).await, Some(a_tip));
            }
        }
    });
}

/// The trust group option applies to the pull from each remote. The group
/// trusts one key, and the remote section names no key. An unsigned commit
/// fails the pull, and a commit of the key of the group passes.
#[test]
fn a_collection_pull_takes_the_trust_group_option() {
    block_on(async {
        let dir = TmpDir::new("pull-collection-trust-group");
        let a = create(dir.path(), "a", Some(ID)).await;
        let tip = commit(&a, dir.path(), "a", None, REF, FIXED_TS, BOUND).await;
        summarize(&a).await;
        let server = RepoServer::start(&dir.path().join("a"), false).await;
        let group = format!(
            "[ex-ostrya trust \"t\"]\nsign-verify=ed25519\nverification-ed25519-key={PUBLIC_B64}\n"
        );
        let dest = dest(dir.path(), None, &[("a", &server.url(), &group)]).await;
        let pull = || {
            let (mut opts, _) = options(&[REF]);
            opts.pull.trust_group = Some("t".to_owned());
            dest.pull_collection(ID, &["a"], opts)
        };

        let err = pull().await.unwrap_err();
        assert!(
            matches!(&err, Error::Signature(m) if m.contains("carries no signature")),
            "{err:?}"
        );
        assert_nothing_written(&dest).await;

        a.sign_commit(&tip, &Ed25519Signer::from_base64(SECRET_B64).unwrap())
            .await
            .unwrap();
        let pulled = pull().await.unwrap();
        assert_eq!(pulled.refs[0].remote, "a");
        assert_eq!(mirror(&dest, REF).await, Some(tip));
    });
}

// --- the place of the refs ---------------------------------------------------

/// Into a repository whose own collection id is the pulled id, the pull
/// writes the local ref, and the timestamp check reads that ref.
#[test]
fn the_own_collection_writes_the_local_ref() {
    block_on(async {
        let dir = TmpDir::new("pull-collection-local");
        let a = create(dir.path(), "a", Some(ID)).await;
        let tip = commit(&a, dir.path(), "a", None, REF, FIXED_TS, BOUND).await;
        summarize(&a).await;
        let server_a = RepoServer::start(&dir.path().join("a"), false).await;
        let dest = dest(dir.path(), Some(ID), &[("a", &server_a.url(), "")]).await;

        let (mut opts, _) = options(&[REF]);
        opts.pull.timestamp_check = TimestampCheck::CurrentRef;
        dest.pull_collection(ID, &["a"], opts).await.unwrap();
        assert_eq!(dest.resolve_ref_tip(REF).await.unwrap(), Some(tip));
        assert!(dest.list_mirror_refs().await.unwrap().is_empty());

        // An older commit on the remote now fails the timestamp check against
        // the local ref.
        let older = create(dir.path(), "older", Some(ID)).await;
        commit(&older, dir.path(), "older", None, REF, FIXED_TS - 10, BOUND).await;
        summarize(&older).await;
        let server_older = RepoServer::start(&dir.path().join("older"), false).await;
        let path = dir.path().join("dest/config");
        let mut text = std::fs::read_to_string(&path).unwrap();
        text.push_str(&format!(
            "\n[remote \"older\"]\nurl={}\ngpg-verify=false\n",
            server_older.url()
        ));
        std::fs::write(&path, text).unwrap();
        let dest = Repo::open(&dir.path().join("dest")).await.unwrap();
        let (mut opts, _) = options(&[REF]);
        opts.pull.timestamp_check = TimestampCheck::CurrentRef;
        let err = dest
            .pull_collection(ID, &["older"], opts)
            .await
            .unwrap_err();
        assert!(
            matches!(&err, Error::Pull(m) if m.contains("chronologically older")),
            "{err:?}"
        );
        assert_eq!(dest.resolve_ref_tip(REF).await.unwrap(), Some(tip));
    });
}

/// Into a repository of another collection, the timestamp check reads the
/// collection ref that the pull writes.
#[test]
fn the_timestamp_check_reads_the_collection_ref() {
    block_on(async {
        let dir = TmpDir::new("pull-collection-mirror-timestamp");
        let a = create(dir.path(), "a", Some(ID)).await;
        let tip = commit(&a, dir.path(), "a", None, REF, FIXED_TS, BOUND).await;
        summarize(&a).await;
        let older = create(dir.path(), "older", Some(ID)).await;
        commit(&older, dir.path(), "older", None, REF, FIXED_TS - 10, BOUND).await;
        summarize(&older).await;
        let server_a = RepoServer::start(&dir.path().join("a"), false).await;
        let server_older = RepoServer::start(&dir.path().join("older"), false).await;
        let dest = dest(
            dir.path(),
            Some(OTHER),
            &[
                ("a", &server_a.url(), ""),
                ("older", &server_older.url(), ""),
            ],
        )
        .await;

        let (mut opts, _) = options(&[REF]);
        opts.pull.timestamp_check = TimestampCheck::CurrentRef;
        dest.pull_collection(ID, &["a"], opts).await.unwrap();
        assert_eq!(mirror(&dest, REF).await, Some(tip));

        let (mut opts, _) = options(&[REF]);
        opts.pull.timestamp_check = TimestampCheck::CurrentRef;
        let err = dest
            .pull_collection(ID, &["older"], opts)
            .await
            .unwrap_err();
        assert!(
            matches!(&err, Error::Pull(m) if m.contains("chronologically older")),
            "{err:?}"
        );
        assert_eq!(mirror(&dest, REF).await, Some(tip));
        assert!(dest.list_refs(None).await.unwrap().is_empty());
    });
}

// --- detached metadata -------------------------------------------------------

/// With `keep-detached-related=false` in the destination, the pull drops
/// `ostrya.related` from the detached metadata that it stores, and stores
/// each other property.
#[test]
fn the_switch_drops_the_related_key_of_a_collection_pull() {
    block_on(async {
        let dir = TmpDir::new("pull-collection-keep-related");
        let a = create(dir.path(), "a", Some(ID)).await;
        let base = commit(&a, dir.path(), "base", None, "os/base", FIXED_TS, BOUND).await;
        let tip = commit(&a, dir.path(), "a", None, REF, FIXED_TS, BOUND).await;
        let kept = Value::Array(vec![entry("test.kept", "s", Value::Str("a".to_owned()))]);
        a.write_commit_detached_metadata(&tip, Some(&kept))
            .await
            .unwrap();
        a.add_detached_related(&tip, "layer/base", &base)
            .await
            .unwrap();
        summarize(&a).await;
        let server_a = RepoServer::start(&dir.path().join("a"), false).await;
        drop(dest(dir.path(), None, &[("a", &server_a.url(), "")]).await);
        let dest = reconfigure(dir.path(), "\n[ex-ostrya]\nkeep-detached-related=false\n").await;
        assert!(!dest.config().keep_detached_related().unwrap());

        let (opts, _) = options(&[REF]);
        dest.pull_collection(ID, &["a"], opts).await.unwrap();

        assert_eq!(mirror(&dest, REF).await, Some(tip));
        let stored = dest
            .read_commit_detached_metadata(&tip)
            .await
            .unwrap()
            .unwrap();
        assert!(stored.dict_get("test.kept").is_some(), "{stored:?}");
        assert!(stored.dict_get("ostrya.related").is_none(), "{stored:?}");
    });
}

// --- dependencies ------------------------------------------------------------

/// A collection pull fetches the dependency that field 2 of the pulled commit
/// names, complete, and writes no ref for it. The dependency gets no binding
/// check.
#[test]
fn a_collection_pull_fetches_the_dependency() {
    block_on(async {
        let dir = TmpDir::new("pull-collection-dependency");
        let a = create(dir.path(), "a", Some(ID)).await;
        let unbound = Bind {
            collection: None,
            refs: None,
        };
        let dep = commit(&a, dir.path(), "dep", None, "os/dep", FIXED_TS, unbound).await;
        let plain = commit(&a, dir.path(), "a", None, REF, FIXED_TS, BOUND).await;
        let tip = store_with_field2(&a, &plain, &[("boot/uki", dep.as_bytes())]).await;
        let txn = a.transaction().await.unwrap();
        txn.set_ref(REF, Some(&tip));
        txn.commit().await.unwrap();
        summarize(&a).await;
        let server_a = RepoServer::start(&dir.path().join("a"), false).await;
        let dest = dest(dir.path(), None, &[("a", &server_a.url(), "")]).await;

        let (opts, _) = options(&[REF]);
        dest.pull_collection(ID, &["a"], opts).await.unwrap();

        assert_eq!(mirror(&dest, REF).await, Some(tip));
        assert_eq!(dest.list_mirror_refs().await.unwrap().len(), 1);
        assert!(dest.list_refs(None).await.unwrap().is_empty());
        assert!(dest.list_remote_refs().await.unwrap().is_empty());
        assert!(dest.has_object(ObjectType::Commit, &dep).await.unwrap());
        assert_eq!(
            dest.commit_state(&dep).await.unwrap(),
            ostrya::CommitState::Normal
        );
        for name in &a.traverse_commit(&dep, 0).await.unwrap() {
            assert!(
                dest.has_object(name.ty, &name.checksum).await.unwrap(),
                "{name} missing from the destination"
            );
        }
        assert!(dest.fsck(&FsckOptions::default()).await.unwrap().is_ok());
    });
}

// --- refusals ----------------------------------------------------------------

/// The pull refuses each option that it cannot carry before its first
/// request, and writes nothing.
#[test]
fn refused_options_send_no_request() {
    block_on(async {
        let dir = TmpDir::new("pull-collection-refusals");
        let a = create(dir.path(), "a", Some(ID)).await;
        commit(&a, dir.path(), "a", None, REF, FIXED_TS, BOUND).await;
        summarize(&a).await;
        let server_a = RepoServer::start(&dir.path().join("a"), false).await;
        let dest = dest(dir.path(), None, &[("a", &server_a.url(), "")]).await;

        let long = "a.b".repeat(100);
        let base = || PullOptions {
            refs: vec![REF.to_owned()],
            ..PullOptions::default()
        };
        let cases: Vec<(&str, &str, Vec<&str>, PullOptions)> = vec![
            ("invalid id", "fresh", vec!["a"], base()),
            ("long id", &long, vec!["a"], base()),
            ("no remote", ID, vec![], base()),
            (
                "collection_id",
                ID,
                vec!["a"],
                PullOptions {
                    collection_id: Some(ID.to_owned()),
                    ..base()
                },
            ),
            (
                "remote",
                ID,
                vec!["a"],
                PullOptions {
                    remote: Some("a".to_owned()),
                    ..base()
                },
            ),
            (
                "mirror",
                ID,
                vec!["a"],
                PullOptions {
                    flags: PullFlags::MIRROR,
                    ..base()
                },
            ),
            (
                "static deltas",
                ID,
                vec!["a"],
                PullOptions {
                    static_deltas: Some(Vec::new()),
                    ..base()
                },
            ),
            (
                "url",
                ID,
                vec!["a"],
                PullOptions {
                    overrides: EndpointOverrides {
                        url: Some(server_a.url()),
                        ..EndpointOverrides::default()
                    },
                    ..base()
                },
            ),
            (
                "disable bindings",
                ID,
                vec!["a"],
                PullOptions {
                    flags: PullFlags::DISABLE_VERIFY_BINDINGS,
                    ..base()
                },
            ),
            (
                "ref name",
                ID,
                vec!["a"],
                PullOptions {
                    refs: vec!["a:b".to_owned()],
                    ..base()
                },
            ),
            (
                "long ref name",
                ID,
                vec!["a"],
                PullOptions {
                    refs: vec![REF.to_owned(), "x".repeat(4090)],
                    ..base()
                },
            ),
            ("unknown remote", ID, vec!["a", "nowhere"], base()),
        ];
        for (tag, id, remotes, pull) in cases {
            let mut opts = CollectionPullOptions::default();
            opts.pull = pull;
            let err = dest.pull_collection(id, &remotes, opts).await.unwrap_err();
            let expected = match tag {
                "collection_id" => matches!(err, Error::Unsupported(_)),
                "ref name" => matches!(err, Error::InvalidRefspec(_)),
                "unknown remote" => true,
                _ => matches!(err, Error::InvalidInput(_)),
            };
            assert!(expected, "{tag}: {err:?}");
            assert!(server_a.seen().is_empty(), "{tag}: {:?}", server_a.seen());
            assert_nothing_written(&dest).await;
        }
    });
}

/// An empty ssh command or send command of a later remote fails the pull
/// before the request to the first remote, and the pull starts no ssh
/// client.
#[test]
fn an_empty_ssh_command_fails_before_the_first_request() {
    block_on(async {
        for key in ["ssh-command", "send-command"] {
            let dir = TmpDir::new("pull-collection-empty-ssh");
            let a = create(dir.path(), "a", Some(ID)).await;
            commit(&a, dir.path(), "a", None, REF, FIXED_TS, BOUND).await;
            summarize(&a).await;
            let server_a = RepoServer::start(&dir.path().join("a"), false).await;
            drop(dest(dir.path(), None, &[("a", &server_a.url(), "")]).await);
            let dest = reconfigure(
                dir.path(),
                &format!(
                    "\n[remote \"b\"]\npull-url=ssh://localhost/nowhere\ngpg-verify=false\n\
                     collection-id={ID}\n{key}=\n"
                ),
            )
            .await;

            let (opts, events) = options(&[REF]);
            let err = dest
                .pull_collection(ID, &["a", "b"], opts)
                .await
                .unwrap_err();
            assert!(
                matches!(&err, Error::Remote(_)) && err.to_string().contains("is empty"),
                "{key}: {err:?}"
            );
            assert!(events.lock().unwrap().is_empty());
            assert!(server_a.seen().is_empty(), "{key}: {:?}", server_a.seen());
            assert_nothing_written(&dest).await;
        }
    });
}

// --- interoperability with the `ostree` command ------------------------------

/// The pull reads the summaries that the `ostree` command writes: field 0 of
/// a repository whose collection id is the pulled id, and the collection map
/// of a repository with another id. The tool then lists the pulled
/// collection refs and passes fsck on the destination.
#[test]
fn the_pull_reads_the_summaries_of_the_tool() {
    if !common::ostree_available() {
        eprintln!("skipped: `ostree` is not installed");
        return;
    }
    block_on(async {
        let dir = TmpDir::new("pull-collection-tool");
        let root = dir.path();
        let repo = |name: &str| format!("--repo={}", root.join(name).display());
        let tree = |name: &str, text: &str| {
            let path = root.join(name);
            std::fs::create_dir_all(path.join("dir")).unwrap();
            std::fs::write(path.join("a"), text).unwrap();
            std::fs::write(path.join("dir/b"), b"beta\n").unwrap();
            path.display().to_string()
        };
        let checksum = |out: Vec<u8>| String::from_utf8(out).unwrap().trim().to_owned();
        let collection = format!("--collection-id={ID}");
        let other = format!("--collection-id={OTHER}");

        // `a` has the collection id, and lists the refs in field 0.
        ostree(&[&repo("a"), "init", "--mode=archive", &collection]);
        let older = checksum(ostree(&[
            &repo("a"),
            "commit",
            &format!("--branch={REF}"),
            "--timestamp=@1700000000",
            "-s",
            "older",
            &tree("t1", "one\n"),
        ]));
        let next = checksum(ostree(&[
            &repo("a"),
            "commit",
            "--branch=os/next",
            "--timestamp=@1700000000",
            "-s",
            "next",
            &tree("t2", "two\n"),
        ]));
        ostree(&[&repo("a"), "summary", "-u"]);

        // `b` has another id, and lists the newer commit in its collection
        // map. The commit comes from a scratch repository of the collection,
        // so it is bound to the collection.
        ostree(&[&repo("scratch"), "init", "--mode=archive", &collection]);
        let newer = checksum(ostree(&[
            &repo("scratch"),
            "commit",
            &format!("--branch={REF}"),
            "--timestamp=@1700000100",
            "-s",
            "newer",
            &tree("t3", "three\n"),
        ]));
        ostree(&[&repo("b"), "init", "--mode=archive", &other]);
        let scratch = root.join("scratch").display().to_string();
        ostree(&[&repo("b"), "pull-local", &scratch, REF]);
        ostree(&[
            &repo("b"),
            "refs",
            "--collections",
            &format!("--create={ID}:{REF}"),
            &newer,
        ]);
        ostree(&[&repo("b"), "refs", "--delete", REF]);
        ostree(&[&repo("b"), "summary", "-u"]);

        let server_a = RepoServer::start(&root.join("a"), false).await;
        let server_b = RepoServer::start(&root.join("b"), false).await;
        let dest = dest(
            root,
            None,
            &[("a", &server_a.url(), ""), ("b", &server_b.url(), "")],
        )
        .await;
        let (opts, events) = options(&[REF, "os/next"]);
        let pulled = dest.pull_collection(ID, &["a", "b"], opts).await.unwrap();
        assert!(events.lock().unwrap().is_empty());
        let selected: Vec<(&str, &str)> = pulled
            .refs
            .iter()
            .map(|r| (r.name.as_str(), r.remote.as_str()))
            .collect();
        assert_eq!(selected, [(REF, "b"), ("os/next", "a")]);
        assert_eq!(mirror(&dest, REF).await.unwrap().to_hex(), newer);
        assert_eq!(mirror(&dest, "os/next").await.unwrap().to_hex(), next);
        assert_ne!(older, newer);

        let listed = String::from_utf8(ostree(&[&repo("dest"), "refs", "--collections"])).unwrap();
        let mut lines: Vec<&str> = listed.lines().collect();
        lines.sort_unstable();
        assert_eq!(
            lines,
            [
                format!("({ID}, os/next)").as_str(),
                format!("({ID}, {REF})").as_str()
            ]
        );
        common::tool_fsck(&root.join("dest"));
    });
}

// --- bucket repositories -----------------------------------------------------

/// The pull of collection refs from bucket repositories at `s3://`
/// addresses.
///
/// Most cases serve the bucket `bkt` from the S3 stand-in of
/// `common/s3.rs`, which accepts each signature and keeps each request. Two
/// cases serve it from `rclone serve s3`, which checks each signature. A
/// case copies the files of a local `archive` repository, its summary
/// included, into the bucket.
///
/// `Repo::pull_collection` reads the key pair from the environment of the
/// process. So each test runs its case in a child process of this test
/// binary, which gets the key pair, the two shared AWS files at absent
/// paths, no other AWS variable, and no proxy variable. The rclone cases
/// skip when `rclone` is absent, and `OSTRYA_REQUIRE_S3` turns the skip into
/// a failure.
#[cfg(feature = "s3")]
mod s3_remote {
    use std::path::Path;
    use std::process::Command;
    use std::sync::atomic::{AtomicU64, Ordering};
    use std::time::Duration;

    use ostrya::PullProgress;

    use super::*;
    use crate::common::s3::{ACCESS_KEY, Rclone, SECRET_KEY, StandIn, rclone_available};

    /// The variable that names the case of the child process.
    const CASE: &str = "OSTRYA_TEST_S3_CASE";

    /// The proxy variables that the fetcher reads. The child process gets
    /// none of them, so it connects to the server directly.
    const PROXY_VARIABLES: [&str; 7] = [
        "http_proxy",
        "HTTP_PROXY",
        "https_proxy",
        "HTTPS_PROXY",
        "all_proxy",
        "ALL_PROXY",
        "no_proxy",
    ];

    /// The request target of a key of the bucket repository `s3://bkt/repo`
    /// on the stand-in.
    fn target(key: &str) -> String {
        format!("/bkt/repo/{key}")
    }

    /// Runs the case `case` in a child process with the secret key `secret`.
    fn run_case(case: &str, secret: &str) {
        let scratch = TmpDir::new("pull-collection-s3-env");
        let mut child = Command::new(std::env::current_exe().unwrap());
        child
            .args([
                "s3_remote::subprocess",
                "--exact",
                "--ignored",
                "--nocapture",
            ])
            .env(CASE, case)
            .env("AWS_ACCESS_KEY_ID", ACCESS_KEY)
            .env("AWS_SECRET_ACCESS_KEY", secret)
            .env("AWS_CONFIG_FILE", scratch.path().join("absent-aws-config"))
            .env(
                "AWS_SHARED_CREDENTIALS_FILE",
                scratch.path().join("absent-aws-credentials"),
            );
        for (name, _) in std::env::vars_os() {
            let text = name.to_string_lossy();
            if text.starts_with("AWS_") && !child.get_envs().any(|(set, _)| set == name) {
                child.env_remove(&name);
            }
        }
        for name in PROXY_VARIABLES {
            child.env_remove(name);
        }
        let output = child.output().expect("re-execute this test binary");
        let stdout = String::from_utf8_lossy(&output.stdout);
        assert!(
            output.status.success() && stdout.contains("1 passed"),
            "the child of the case {case} reported {}:\n{stdout}{}",
            output.status,
            String::from_utf8_lossy(&output.stderr),
        );
    }

    /// Runs a case of a test of this module.
    ///
    /// This test does its work only when a test of this module runs this
    /// binary again with the variables set.
    #[test]
    #[ignore = "helper process for the tests of bucket repositories"]
    fn subprocess() {
        let Some(case) = std::env::var_os(CASE) else {
            return;
        };
        block_on(async {
            match case.to_str().unwrap() {
                "collection-map" => collection_map().await,
                "field-0" => field_0().await,
                "live-count" => live_count().await,
                "across-transports" => across_transports().await,
                "no-summary" => no_summary().await,
                "summary-policy" => summary_policy().await,
                "stale-summary" => stale_summary().await,
                "absent-bucket" => absent_bucket().await,
                "rclone" => rclone(false).await,
                "rclone-wrong-secret" => rclone(true).await,
                other => panic!("unknown case {other}"),
            }
        });
    }

    /// Stores each file under the directory `dir` in `server`, at its path
    /// under `dir` with the prefix `repo/`.
    fn store_tree(server: &StandIn, dir: &Path) {
        let mut pending = vec![dir.to_path_buf()];
        while let Some(at) = pending.pop() {
            for entry in std::fs::read_dir(&at).unwrap() {
                let path = entry.unwrap().path();
                if path.is_dir() {
                    pending.push(path);
                } else {
                    let key = path.strip_prefix(dir).unwrap().to_str().unwrap();
                    let bytes = std::fs::read(&path).unwrap();
                    server.state().store(&format!("repo/{key}"), &bytes);
                }
            }
        }
    }

    /// Returns the keys of a remote at the address `address` on the server
    /// at `url`.
    fn bucket(url: &str, address: &str) -> String {
        format!("pull-url={address}\ns3-endpoint={url}\n")
    }

    /// Creates the destination repository at `dir/name`, with the collection
    /// id `own` and the remotes `remotes`: each a name and its keys.
    async fn bucket_dest(
        dir: &Path,
        name: &str,
        own: Option<&str>,
        remotes: &[(&str, &str)],
    ) -> Repo {
        drop(create(dir, name, own).await);
        let config = dir.join(name).join("config");
        let mut text = std::fs::read_to_string(&config).unwrap();
        for (remote, keys) in remotes {
            text.push_str(&format!(
                "\n[remote \"{remote}\"]\ngpg-verify=false\ncollection-id={ID}\n{keys}"
            ));
        }
        std::fs::write(&config, text).unwrap();
        Repo::open(&dir.join(name)).await.unwrap()
    }

    /// Returns the options of [`options`] that allow the `http://`
    /// endpoint of the server.
    fn bucket_options(
        refs: &[&str],
    ) -> (CollectionPullOptions, Arc<Mutex<Vec<CollectionPullEvent>>>) {
        let (mut opts, events) = options(refs);
        opts.pull.overrides.allow_cleartext_credentials = true;
        (opts, events)
    }

    /// Asserts that each request to `server` is signed, and that no request
    /// asks for the ref listing.
    fn assert_signed_with_no_listing(server: &StandIn) {
        let state = server.state();
        assert!(state.authorizations.iter().all(Option::is_some));
        assert!(
            !state
                .requests
                .iter()
                .any(|(_, target)| target.contains(LISTING)),
            "{:?}",
            state.requests
        );
    }

    /// Returns the number of requests to `server` for the key `key` of the
    /// bucket repository.
    fn requests_for(server: &StandIn, key: &str) -> usize {
        let wanted = target(key);
        server
            .state()
            .requests
            .iter()
            .filter(|(_, target)| *target == wanted)
            .count()
    }

    /// Returns `true` if `server` received a request for an object of the
    /// bucket repository.
    fn bucket_objects_requested(server: &StandIn) -> bool {
        let objects = target("objects/");
        server
            .state()
            .requests
            .iter()
            .any(|(_, target)| target.starts_with(&objects))
    }

    /// Returns the single skip event of `events`, as the remote and the
    /// message.
    fn skipped(events: &Mutex<Vec<CollectionPullEvent>>) -> (String, String) {
        let events = events.lock().unwrap();
        match &events[..] {
            [
                CollectionPullEvent::RemoteSkipped {
                    remote, message, ..
                },
            ] => (remote.clone(), message.clone()),
            other => panic!("{other:?}"),
        }
    }

    /// A bucket repository with no collection id holds the ref of the
    /// collection under `refs/mirrors/ID/`, and its summary lists it in the
    /// collection map. A pull of the named ref and a pull of each ref read
    /// the summary alone, then the `config`, and write the collection ref.
    #[test]
    fn a_bucket_serves_the_refs_of_its_collection_map() {
        run_case("collection-map", SECRET_KEY);
    }

    async fn collection_map() {
        let dir = TmpDir::new("pull-collection-s3-map");
        let source = create(dir.path(), "source", None).await;
        let tip = commit(&source, dir.path(), "a", Some(ID), REF, FIXED_TS, BOUND).await;
        summarize(&source).await;
        for refs in [&[REF][..], &[]] {
            let server = StandIn::start();
            store_tree(&server, &dir.path().join("source"));
            let keys = bucket(&server.endpoint(), "s3://bkt/repo");
            let name = format!("dest-{}", refs.len());
            let dest = bucket_dest(dir.path(), &name, None, &[("bkt", &keys)]).await;
            let (opts, events) = bucket_options(refs);
            let pulled = dest.pull_collection(ID, &["bkt"], opts).await.unwrap();
            assert!(events.lock().unwrap().is_empty());
            assert_eq!(pulled.refs.len(), 1);
            assert_eq!(pulled.refs[0].remote, "bkt");
            assert_eq!(pulled.refs[0].commit, tip);
            assert_eq!(pulled.refs[0].timestamp, FIXED_TS);
            assert_eq!(mirror(&dest, REF).await, Some(tip));
            assert!(dest.list_refs(None).await.unwrap().is_empty());
            assert!(dest.fsck(&FsckOptions::default()).await.unwrap().is_ok());
            let requests = server.state().requests.clone();
            assert_eq!(
                requests[..2],
                [
                    ("GET".to_owned(), target("summary")),
                    ("GET".to_owned(), target("config")),
                ]
            );
            assert_eq!(requests_for(&server, "summary.sig"), 0);
            assert_eq!(requests_for(&server, "summary"), 1);
            assert_signed_with_no_listing(&server);
        }
    }

    /// A bucket repository whose summary states the collection id lists the
    /// refs of the collection in field 0. A destination of the same
    /// collection writes the local ref.
    #[test]
    fn a_bucket_of_the_collection_serves_field_0() {
        run_case("field-0", SECRET_KEY);
    }

    async fn field_0() {
        let dir = TmpDir::new("pull-collection-s3-field-0");
        let source = create(dir.path(), "source", Some(ID)).await;
        let tip = commit(&source, dir.path(), "a", None, REF, FIXED_TS, BOUND).await;
        summarize(&source).await;
        let server = StandIn::start();
        store_tree(&server, &dir.path().join("source"));
        let keys = bucket(&server.endpoint(), "s3://bkt/repo");
        let dest = bucket_dest(dir.path(), "dest", Some(ID), &[("bkt", &keys)]).await;
        let (opts, events) = bucket_options(&[REF]);
        let pulled = dest.pull_collection(ID, &["bkt"], opts).await.unwrap();
        assert!(events.lock().unwrap().is_empty());
        assert_eq!(pulled.refs[0].commit, tip);
        assert_eq!(dest.resolve_rev(REF, false).await.unwrap(), Some(tip));
        assert!(dest.list_mirror_refs().await.unwrap().is_empty());
        assert_signed_with_no_listing(&server);
    }

    /// The live count of a pull from a bucket repository includes the bytes
    /// of `summary` and `summary.sig` until the pull reads the `config`, as
    /// over HTTP. The count of the stats starts after the root files.
    #[test]
    fn the_live_count_of_a_bucket_pull_includes_the_summary() {
        run_case("live-count", SECRET_KEY);
    }

    async fn live_count() {
        let dir = TmpDir::new("pull-collection-s3-live-count");
        let source = create(dir.path(), "source", Some(ID)).await;
        let tip = commit(&source, dir.path(), "a", None, REF, FIXED_TS, BOUND).await;
        summarize(&source).await;
        let signer = Ed25519Signer::from_base64(SECRET_B64).unwrap();
        source.sign_summary(&signer).await.unwrap();
        let server = StandIn::start();
        store_tree(&server, &dir.path().join("source"));
        let keys = bucket(&server.endpoint(), "s3://bkt/repo");
        let dest = bucket_dest(dir.path(), "dest", Some(ID), &[("bkt", &keys)]).await;
        let progress = PullProgress::new();
        let at_config = Arc::new(AtomicU64::new(u64::MAX));
        let root = {
            let mut state = server.state();
            let (seen, stored) = (progress.clone(), Arc::clone(&at_config));
            state.before_key = Some((
                "GET",
                "repo/config".to_owned(),
                Box::new(move |_| {
                    stored.store(seen.snapshot().bytes_transferred, Ordering::Relaxed);
                }),
            ));
            state.objects["repo/summary"].bytes.len()
                + state.objects["repo/summary.sig"].bytes.len()
        };
        let stats = dest
            .pull(
                "bkt",
                PullOptions {
                    refs: vec![REF.to_owned()],
                    progress: Some(progress.clone()),
                    overrides: EndpointOverrides {
                        allow_cleartext_credentials: true,
                        ..EndpointOverrides::default()
                    },
                    ..PullOptions::default()
                },
            )
            .await
            .unwrap();
        assert_eq!(
            dest.resolve_rev(&format!("bkt:{REF}"), false)
                .await
                .unwrap(),
            Some(tip)
        );
        assert_eq!(at_config.load(Ordering::Relaxed), root as u64);
        assert_eq!(
            progress.snapshot().bytes_transferred,
            stats.bytes_transferred
        );
        assert_signed_with_no_listing(&server);
    }

    /// An HTTP remote and a bucket repository serve the collection. The
    /// newest commit wins across the two transports, and the pull takes the
    /// objects from the selected remote alone.
    #[test]
    fn the_newest_commit_wins_across_http_and_s3() {
        run_case("across-transports", SECRET_KEY);
    }

    async fn across_transports() {
        for bucket_newer in [true, false] {
            let dir = TmpDir::new("pull-collection-s3-across");
            let (http_ts, bucket_ts) = if bucket_newer {
                (FIXED_TS, FIXED_TS + 10)
            } else {
                (FIXED_TS + 10, FIXED_TS)
            };
            let a = create(dir.path(), "a", Some(ID)).await;
            let http_tip = commit(&a, dir.path(), "a", None, REF, http_ts, BOUND).await;
            summarize(&a).await;
            let source = create(dir.path(), "source", None).await;
            let bucket_tip =
                commit(&source, dir.path(), "b", Some(ID), REF, bucket_ts, BOUND).await;
            summarize(&source).await;
            let server_a = RepoServer::start(&dir.path().join("a"), false).await;
            let server = StandIn::start();
            store_tree(&server, &dir.path().join("source"));
            let http = format!("url={}\n", server_a.url());
            let keys = bucket(&server.endpoint(), "s3://bkt/repo");
            let dest = bucket_dest(dir.path(), "dest", None, &[("a", &http), ("bkt", &keys)]).await;
            let (opts, events) = bucket_options(&[REF]);
            let pulled = dest.pull_collection(ID, &["a", "bkt"], opts).await.unwrap();
            assert!(events.lock().unwrap().is_empty());
            let (winner, tip) = if bucket_newer {
                ("bkt", bucket_tip)
            } else {
                ("a", http_tip)
            };
            assert_eq!(pulled.refs[0].remote, winner);
            assert_eq!(mirror(&dest, REF).await, Some(tip));
            assert_eq!(objects_requested(&server_a), !bucket_newer);
            assert_eq!(bucket_objects_requested(&server), bucket_newer);
            assert_signed_with_no_listing(&server);
        }
    }

    /// A bucket repository with no `summary` fails the probe, and the next
    /// remote serves the ref. A bucket repository alone with no `summary`
    /// fails the pull before an object request, and the pull writes nothing.
    #[test]
    fn a_bucket_with_no_summary_is_skipped() {
        run_case("no-summary", SECRET_KEY);
    }

    async fn no_summary() {
        let dir = TmpDir::new("pull-collection-s3-no-summary");
        let source = create(dir.path(), "source", None).await;
        commit(
            &source,
            dir.path(),
            "b",
            Some(ID),
            REF,
            FIXED_TS + 10,
            BOUND,
        )
        .await;
        let a = create(dir.path(), "a", Some(ID)).await;
        let tip = commit(&a, dir.path(), "a", None, REF, FIXED_TS, BOUND).await;
        summarize(&a).await;
        let server_a = RepoServer::start(&dir.path().join("a"), false).await;
        let server = StandIn::start();
        store_tree(&server, &dir.path().join("source"));
        let http = format!("url={}\n", server_a.url());
        let keys = bucket(&server.endpoint(), "s3://bkt/repo");

        let dest = bucket_dest(dir.path(), "dest", None, &[("bkt", &keys), ("a", &http)]).await;
        let (opts, events) = bucket_options(&[REF]);
        let pulled = dest.pull_collection(ID, &["bkt", "a"], opts).await.unwrap();
        assert_eq!(pulled.refs[0].remote, "a");
        assert_eq!(mirror(&dest, REF).await, Some(tip));
        let (remote, message) = skipped(&events);
        assert_eq!(remote, "bkt");
        assert!(
            message.contains("the bucket repository has no summary"),
            "{message}"
        );

        let alone = bucket_dest(dir.path(), "alone", None, &[("bkt", &keys)]).await;
        let (opts, events) = bucket_options(&[REF]);
        let err = alone.pull_collection(ID, &["bkt"], opts).await.unwrap_err();
        assert!(matches!(err, Error::Pull(_)), "{err:?}");
        assert_eq!(skipped(&events).0, "bkt");
        assert_nothing_written(&alone).await;
        assert!(!bucket_objects_requested(&server));
        assert_signed_with_no_listing(&server);
    }

    /// Under a summary policy, the pull reads `summary.sig` and `summary`
    /// through the bucket client, and verifies the summary. The two requests
    /// are in flight together: in the first pull, the server waits before it
    /// answers `summary.sig`, and logs `summary` first. The pull asks for
    /// `summary.sig` first: in the second pull, with a fetch limit of one,
    /// the server logs `summary.sig` first. A summary that an untrusted key
    /// signed, and a summary with no `summary.sig`, skip the bucket
    /// repository.
    #[test]
    fn a_summary_policy_verifies_the_summary_of_a_bucket() {
        run_case("summary-policy", SECRET_KEY);
    }

    async fn summary_policy() {
        for signer in [Some(SECRET_B64), Some(OTHER_SECRET_B64), None] {
            let dir = TmpDir::new("pull-collection-s3-policy");
            let source = create(dir.path(), "source", None).await;
            let tip = commit(&source, dir.path(), "a", Some(ID), REF, FIXED_TS, BOUND).await;
            summarize(&source).await;
            if let Some(secret) = signer {
                let signer = Ed25519Signer::from_base64(secret).unwrap();
                source.sign_summary(&signer).await.unwrap();
            }
            let server = StandIn::start();
            store_tree(&server, &dir.path().join("source"));
            if signer == Some(SECRET_B64) {
                server.state().delay = Some((
                    "GET",
                    "repo/summary.sig".to_owned(),
                    Duration::from_millis(500),
                ));
            }
            let keys = format!(
                "{}sign-verify-summary=ed25519\nverification-ed25519-key={PUBLIC_B64}\n",
                bucket(&server.endpoint(), "s3://bkt/repo")
            );
            let dest = bucket_dest(dir.path(), "dest", None, &[("bkt", &keys)]).await;
            let (mut opts, events) = bucket_options(&[REF]);
            if signer == Some(OTHER_SECRET_B64) {
                opts.pull.max_outstanding_fetches = Some(1);
            }
            let pulled = dest.pull_collection(ID, &["bkt"], opts).await;
            let requests = server.state().requests.clone();
            let summary = ("GET".to_owned(), target("summary"));
            let signature = ("GET".to_owned(), target("summary.sig"));
            let mut first = requests[..2].to_vec();
            if signer == Some(SECRET_B64) {
                assert_eq!(first, [summary, signature]);
            } else if signer == Some(OTHER_SECRET_B64) {
                assert_eq!(first, [signature, summary]);
            } else {
                first.sort();
                assert_eq!(first, [summary, signature]);
            }
            if signer == Some(SECRET_B64) {
                let pulled = pulled.unwrap();
                assert!(events.lock().unwrap().is_empty());
                assert_eq!(pulled.refs[0].commit, tip);
                assert_eq!(mirror(&dest, REF).await, Some(tip));
                assert_eq!(requests_for(&server, "summary.sig"), 1);
                assert_eq!(requests_for(&server, "summary"), 1);
            } else {
                assert!(matches!(pulled, Err(Error::Pull(_))), "{pulled:?}");
                assert_eq!(skipped(&events).0, "bkt");
                assert_nothing_written(&dest).await;
                assert!(!bucket_objects_requested(&server));
            }
            assert_signed_with_no_listing(&server);
        }
    }

    /// A ref file of the bucket repository that names a newer commit than
    /// its summary: the pull takes the commit of the summary, and reads no
    /// ref file.
    #[test]
    fn a_stale_summary_gives_the_commit_of_the_summary() {
        run_case("stale-summary", SECRET_KEY);
    }

    async fn stale_summary() {
        let dir = TmpDir::new("pull-collection-s3-stale");
        let source = create(dir.path(), "source", None).await;
        let old = commit(&source, dir.path(), "a", Some(ID), REF, FIXED_TS, BOUND).await;
        summarize(&source).await;
        let new = commit(
            &source,
            dir.path(),
            "b",
            Some(ID),
            REF,
            FIXED_TS + 10,
            BOUND,
        )
        .await;
        let server = StandIn::start();
        store_tree(&server, &dir.path().join("source"));
        assert_eq!(
            server.state().objects[&format!("repo/refs/mirrors/{ID}/{REF}")].bytes,
            format!("{new}\n").into_bytes()
        );
        let keys = bucket(&server.endpoint(), "s3://bkt/repo");
        let dest = bucket_dest(dir.path(), "dest", None, &[("bkt", &keys)]).await;
        let (opts, _) = bucket_options(&[REF]);
        let pulled = dest.pull_collection(ID, &["bkt"], opts).await.unwrap();
        assert_eq!(pulled.refs[0].commit, old);
        assert_eq!(mirror(&dest, REF).await, Some(old));
        let refs = target("refs/");
        assert!(
            !server
                .state()
                .requests
                .iter()
                .any(|(_, target)| target.starts_with(&refs))
        );
    }

    /// A remote whose bucket the server does not hold is skipped with the S3
    /// code `NoSuchBucket`, which the bucket client reads, and the next
    /// remote serves the ref.
    #[test]
    fn an_absent_bucket_is_skipped_with_the_s3_error() {
        run_case("absent-bucket", SECRET_KEY);
    }

    async fn absent_bucket() {
        let dir = TmpDir::new("pull-collection-s3-absent");
        let source = create(dir.path(), "source", None).await;
        let tip = commit(&source, dir.path(), "a", Some(ID), REF, FIXED_TS, BOUND).await;
        summarize(&source).await;
        let server = StandIn::start();
        store_tree(&server, &dir.path().join("source"));
        let gone = bucket(&server.endpoint(), "s3://other/repo");
        let keys = bucket(&server.endpoint(), "s3://bkt/repo");
        let dest = bucket_dest(dir.path(), "dest", None, &[("gone", &gone), ("bkt", &keys)]).await;
        let (opts, events) = bucket_options(&[REF]);
        let pulled = dest
            .pull_collection(ID, &["gone", "bkt"], opts)
            .await
            .unwrap();
        assert_eq!(pulled.refs[0].remote, "bkt");
        assert_eq!(mirror(&dest, REF).await, Some(tip));
        let (remote, message) = skipped(&events);
        assert_eq!(remote, "gone");
        assert!(message.contains("NoSuchBucket"), "{message}");
        assert_signed_with_no_listing(&server);
    }

    /// `rclone serve s3` checks the signature of each request. The pull
    /// reads the collection map of the summary of a bucket directory, and
    /// writes the collection ref. A wrong secret key skips the remote with
    /// the S3 code `SignatureDoesNotMatch`, and the pull writes nothing.
    #[test]
    fn a_bucket_on_rclone_serves_the_refs_of_a_collection() {
        if !rclone_available("a_bucket_on_rclone_serves_the_refs_of_a_collection") {
            return;
        }
        run_case("rclone", SECRET_KEY);
        run_case("rclone-wrong-secret", "wrong-secret-key");
    }

    async fn rclone(wrong_secret: bool) {
        let dir = TmpDir::new("pull-collection-s3-rclone");
        let source = create(dir.path(), "source", None).await;
        let tip = commit(&source, dir.path(), "a", Some(ID), REF, FIXED_TS, BOUND).await;
        summarize(&source).await;
        drop(source);
        // rclone keeps the listing of each directory, so the files of the
        // bucket directory are in place before the server starts.
        let root = dir.path().join("root");
        std::fs::create_dir_all(root.join("bkt")).unwrap();
        let status = Command::new("cp")
            .arg("-r")
            .arg(dir.path().join("source"))
            .arg(root.join("bkt").join("repo"))
            .status()
            .unwrap();
        assert!(status.success(), "cp -r");
        let server = Rclone::start(dir.path(), &root);
        let keys = bucket(&server.endpoint(), "s3://bkt/repo");
        let dest = bucket_dest(dir.path(), "dest", None, &[("bkt", &keys)]).await;
        let (opts, events) = bucket_options(&[REF]);
        let pulled = dest.pull_collection(ID, &["bkt"], opts).await;
        if wrong_secret {
            assert!(matches!(pulled, Err(Error::Pull(_))), "{pulled:?}");
            let (remote, message) = skipped(&events);
            assert_eq!(remote, "bkt");
            assert!(message.contains("SignatureDoesNotMatch"), "{message}");
            assert_nothing_written(&dest).await;
        } else {
            let pulled = pulled.unwrap();
            assert!(events.lock().unwrap().is_empty());
            assert_eq!(pulled.refs[0].commit, tip);
            assert_eq!(mirror(&dest, REF).await, Some(tip));
            assert!(dest.fsck(&FsckOptions::default()).await.unwrap().is_ok());
        }
    }
}