ferroday-cage 0.4.3

Run a command inside an unprivileged Linux sandbox: fresh namespaces, a root filesystem you supply or bootstrap from Debian, Alpine, or Gentoo, and a clean environment, established in pure Rust against the kernel
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
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
//! Integration tests for the Debian userland provisioner.

mod common;

use std::os::unix::fs::{MetadataExt, PermissionsExt};
use std::path::Path;

use common::deb_repo::{Pkg, Tar, ar, deb, write_repo};
use ferroday_cage::provision::debian::{Debian, DebianEvent, Plan, Pool, Repository};
use ferroday_cage::provision::{self, ProvisionError, Provisioned};

/// The pool the pool tests publish into: a fixed suite, component, and
/// architecture, so each test names only what it is varying.
fn trixie_pool(dir: &Path) -> Pool {
    Pool::at(dir)
        .suite("trixie")
        .component("main")
        .architecture("amd64")
}

/// Builds a `.deb` carrying a real `./control` member, which [`deb`] omits.
/// `Pool::publish` reads the control stanza to synthesize the pool index, so its
/// tests need a control-bearing package.
fn deb_with_control(control: &str, data_tar: &[u8]) -> Vec<u8> {
    let control_tar = Tar::new()
        .file("./control", 0o644, control.as_bytes())
        .finish();
    ar(&[
        ("debian-binary", b"2.0\n"),
        ("control.tar", &control_tar),
        ("data.tar", data_tar),
    ])
}

/// The environment variable that opts into the network-dependent bootstrap
/// tests. They fetch from a live Debian mirror and are off by default, so a
/// plain `cargo test --all-features` — including CI — does not reach the
/// network.
const NETWORK_GATE: &str = "FERRODAY_CAGE_DEBIAN_NETWORK_TEST";

/// The variable that asserts these tests ran, in the crate's usual shape: a
/// skip stays a skip by default, and this turns it into a failure so a release
/// check cannot come back green having exercised no repository at all.
const REQUIRE_GATE: &str = "FERRODAY_CAGE_REQUIRE_NETWORK_TEST";

/// Returns `false` and prints a skip note when the network tests are not
/// enabled.
fn network_enabled() -> bool {
    if std::env::var_os(NETWORK_GATE).is_some() {
        return true;
    }
    assert!(
        std::env::var_os(REQUIRE_GATE).is_none(),
        "{REQUIRE_GATE} is set and {NETWORK_GATE} is not, so the Debian bootstrap tests \
         would have been skipped",
    );
    eprintln!("skipping: set {NETWORK_GATE}=1 to run the Debian bootstrap tests");
    false
}

#[test]
fn hermetic_extract_only_bootstrap() {
    // A self-contained file:// repository, no network and no signature, so
    // the whole acquisition and extraction pipeline runs in CI.
    let dir = common::scratch_dir("debian-hermetic");
    let base = Tar::new()
        .dir("./usr", 0o755)
        .dir("./usr/bin", 0o755)
        .dir("./usr/sbin", 0o755)
        .file("./usr/bin/hello", 0o755, b"#!/bin/true\n")
        // A setgid helper owned by a non-root group, as the base set's
        // shadow helpers are: it extracts root-owned, keeping its mode.
        .file_owned("./usr/sbin/helper", 0o2755, 0, 42, b"x\n")
        .symlink("./bin", "usr/bin")
        .finish();
    let dependency = Tar::new()
        .dir("./usr", 0o755)
        .dir("./usr/lib", 0o755)
        .file("./usr/lib/libdep.so", 0o644, b"lib\n")
        .finish();
    let mirror = write_repo(
        &dir.join("repo"),
        "trixie",
        "amd64",
        &[
            Pkg::required("base", deb(&base), "libdep"),
            Pkg::ordinary("libdep", deb(&dependency)),
        ],
    );

    let rootfs = dir.join("rootfs");
    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .cache_dir(dir.join("cache"))
        .extract_only(true)
        .build()
        .expect("the builder validates");
    assert_eq!(
        provision::ensure(&rootfs, &mut debian).expect("the hermetic bootstrap runs"),
        Provisioned::Created,
    );

    assert!(rootfs.join("usr/bin/hello").is_file());
    // The dependency was resolved from the base package's Depends and
    // extracted, and the merged-usr symlink is in place.
    assert!(rootfs.join("usr/lib/libdep.so").is_file());
    assert!(rootfs.join("bin").is_symlink());
    let mode = std::fs::symlink_metadata(rootfs.join("usr/sbin/helper"))
        .unwrap()
        .permissions()
        .mode()
        & 0o7777;
    assert_eq!(mode, 0o2755, "the setgid bit is preserved");
}

#[test]
fn hermetic_resolve_reports_the_plan_without_downloading() {
    // resolve() fetches and verifies the release and index and resolves the
    // closure, but downloads no package. Proven by deleting the pool .debs
    // after building the repository: resolve() must still succeed, reading only
    // the release and the index, and report the archive's recorded digests.
    let dir = common::scratch_dir("debian-resolve");
    let repo = dir.join("repo");
    let base = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/bin/x", 0o755, b"ok\n")
        .finish();
    let dependency = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/lib/libdep.so", 0o644, b"lib\n")
        .finish();
    let base_deb = deb(&base);
    let mirror = write_repo(
        &repo,
        "trixie",
        "amd64",
        &[
            Pkg::required("base", base_deb.clone(), "libdep"),
            Pkg::ordinary("libdep", deb(&dependency)),
        ],
    );
    // Remove the pool so any download would fail; resolve() must not need it.
    std::fs::remove_file(repo.join("pool/base_1.0_amd64.deb")).unwrap();
    std::fs::remove_file(repo.join("pool/libdep_1.0_amd64.deb")).unwrap();

    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .build()
        .expect("the builder validates");
    let plan = debian
        .resolve()
        .expect("resolve reads only the release and index");

    assert_eq!(plan.suite, "trixie");
    assert_eq!(plan.architecture, "amd64");
    // The required base package pulls its dependency; the plan is sorted by name.
    let names: Vec<_> = plan.packages.iter().map(|p| p.name.as_str()).collect();
    assert_eq!(names, ["base", "libdep"]);

    let base_plan = plan
        .packages
        .iter()
        .find(|p| p.name == "base")
        .expect("base is in the plan");
    assert_eq!(base_plan.version, "1.0");
    assert_eq!(base_plan.architecture, "amd64");
    assert_eq!(base_plan.filename, "pool/base_1.0_amd64.deb");
    // The reported digest is the archive's, chained from the verified index.
    assert_eq!(base_plan.sha256, common::deb_repo::sha256_hex(&base_deb));

    // Resolution is deterministic: a second call yields an identical plan.
    assert_eq!(debian.resolve().expect("resolve again"), plan);
}

#[test]
fn hermetic_two_repositories_merge_and_fetch_origin_correctly() {
    // A primary mirror plus an additional trusted file:// repository shipping a
    // custom package. The custom package lives only in the second repository's
    // pool and depends on a library that lives only in the primary; a full
    // extraction succeeds only if each package is fetched from its own
    // repository's mirror, proving origin-correct acquisition and a merged
    // resolution that closes the custom package's dependency against the mirror.
    let dir = common::scratch_dir("debian-two-repos");

    let base = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/bin/x", 0o755, b"ok\n")
        .finish();
    let library = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/lib/libdep.so", 0o644, b"lib\n")
        .finish();
    let primary = write_repo(
        &dir.join("primary"),
        "trixie",
        "amd64",
        &[
            Pkg::required("base", deb(&base), "libdep"),
            Pkg::ordinary("libdep", deb(&library)),
        ],
    );

    let custom_tar = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/bin/custom", 0o755, b"#!/bin/true\n")
        .finish();
    // An ordinary package that depends on the primary's library, so resolving
    // it exercises a cross-repository dependency closure.
    let custom = Pkg::ordinary("custom", deb(&custom_tar)).depending_on("libdep");
    let feature = write_repo(&dir.join("feature"), "trixie", "amd64", &[custom]);

    let feature_repo = ferroday_cage::provision::debian::Repository::builder("trixie")
        .mirror(feature)
        .trust_unsigned(true)
        .name("feature")
        .build()
        .expect("the feature repository validates");

    let rootfs = dir.join("rootfs");
    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(primary)
        .trust_unsigned(true)
        .include(["custom"])
        .repository(feature_repo)
        .cache_dir(dir.join("cache"))
        .extract_only(true)
        .build()
        .expect("the builder validates");

    // The plan draws from both repositories: the base and its library from the
    // primary, the custom package from the feature repository.
    let plan = debian.resolve().expect("the merged plan resolves");
    let mut names: Vec<_> = plan.packages.iter().map(|p| p.name.as_str()).collect();
    names.sort_unstable();
    assert_eq!(names, ["base", "custom", "libdep"]);

    assert_eq!(
        provision::ensure(&rootfs, &mut debian).expect("the two-repository bootstrap runs"),
        Provisioned::Created,
    );
    // Every package's files are present: the custom package could only have been
    // fetched from the feature repository, its dependency and the base only from
    // the primary.
    assert!(rootfs.join("usr/bin/custom").is_file());
    assert!(rootfs.join("usr/lib/libdep.so").is_file());
    assert!(rootfs.join("usr/bin/x").is_file());
}

#[test]
fn hermetic_resolved_event_reports_the_bootstrap_plan() {
    // A full bootstrap emits DebianEvent::Resolved once the closure is resolved
    // and before the first download, carrying the same plan a standalone
    // resolve() reports. Captured through a progress sink over an extract-only
    // bootstrap, so no configuration wave is needed.
    let dir = common::scratch_dir("debian-resolved-event");
    let base = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/bin/x", 0o755, b"ok\n")
        .finish();
    let dependency = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/lib/libdep.so", 0o644, b"lib\n")
        .finish();
    let mirror = write_repo(
        &dir.join("repo"),
        "trixie",
        "amd64",
        &[
            Pkg::required("base", deb(&base), "libdep"),
            Pkg::ordinary("libdep", deb(&dependency)),
        ],
    );

    // The sink borrows `reported` for the run; the block scopes that borrow so
    // the plan can be read out afterward.
    let reported: std::cell::RefCell<Option<Plan>> = std::cell::RefCell::new(None);
    let rootfs = dir.join("rootfs");
    {
        let mut sink = |event: DebianEvent<'_>| {
            if let DebianEvent::Resolved { plan, .. } = event {
                *reported.borrow_mut() = Some(plan.clone());
            }
        };
        let mut debian = Debian::builder("trixie")
            .architecture("amd64")
            .mirror(mirror)
            .trust_unsigned(true)
            .cache_dir(dir.join("cache"))
            .extract_only(true)
            .build()
            .expect("the builder validates");
        provision::ensure(&rootfs, &mut debian.observe(&mut sink)).expect("the bootstrap runs");
    }

    let plan = reported
        .into_inner()
        .expect("a Resolved event carried the plan");
    assert_eq!(plan.suite, "trixie");
    assert_eq!(plan.architecture, "amd64");
    let names: Vec<_> = plan.packages.iter().map(|p| p.name.as_str()).collect();
    assert_eq!(names, ["base", "libdep"]);
}

#[test]
fn hermetic_published_pool_is_consumable_by_the_provisioner() {
    // Pool::publish writes a dists/-structured trusted pool from a freshly-built
    // .deb; the provisioner then resolves and extracts against it as an
    // additional repository. The round-trip proves the writer's output parses
    // through the reader, and the pool's bytes are fetchable and extractable.
    let dir = common::scratch_dir("debian-pool-writer");

    let base = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/bin/x", 0o755, b"ok\n")
        .finish();
    let library = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/lib/libdep.so", 0o644, b"lib\n")
        .finish();
    let primary = write_repo(
        &dir.join("primary"),
        "trixie",
        "amd64",
        &[
            Pkg::required("base", deb(&base), "libdep"),
            Pkg::ordinary("libdep", deb(&library)),
        ],
    );

    // A freshly-built custom package that depends on the primary's library.
    let custom_data = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/bin/custom", 0o755, b"#!/bin/true\n")
        .finish();
    let custom_deb = deb_with_control(
        "Package: custom\nVersion: 1.0\nArchitecture: amd64\nMaintainer: test\n\
         Depends: libdep\nDescription: a custom package\n",
        &custom_data,
    );
    let deb_path = dir.join("custom_1.0_amd64.deb");
    std::fs::write(&deb_path, &custom_deb).unwrap();

    let pool = dir.join("pool");
    trixie_pool(&pool)
        .publish([&deb_path])
        .expect("the pool publishes");

    let pool_repo = Repository::builder("trixie")
        .mirror(format!("file://{}", pool.display()))
        .trust_unsigned(true)
        .name("localpool")
        .build()
        .expect("the pool repository validates");

    let rootfs = dir.join("rootfs");
    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(primary)
        .trust_unsigned(true)
        .include(["custom"])
        .repository(pool_repo)
        .cache_dir(dir.join("cache"))
        .extract_only(true)
        .build()
        .expect("the builder validates");

    let plan = debian
        .resolve()
        .expect("resolves against the published pool");
    let mut names: Vec<_> = plan.packages.iter().map(|p| p.name.as_str()).collect();
    names.sort_unstable();
    assert_eq!(names, ["base", "custom", "libdep"]);

    // The custom entry carries the pool path the writer chose and the archive
    // digest of the .deb published there — read from its control member and
    // computed over its bytes.
    let custom = plan
        .packages
        .iter()
        .find(|p| p.name == "custom")
        .expect("the custom package resolved from the pool");
    assert_eq!(custom.version, "1.0");
    assert_eq!(custom.filename, "pool/main/c/custom/custom_1.0_amd64.deb");
    assert_eq!(custom.sha256, common::deb_repo::sha256_hex(&custom_deb));

    assert_eq!(
        provision::ensure(&rootfs, &mut debian).expect("the bootstrap runs"),
        Provisioned::Created,
    );
    assert!(rootfs.join("usr/bin/custom").is_file());
}

#[test]
fn hermetic_a_second_component_does_not_retire_the_first() {
    // A pool's release names the sections a reader may resolve, and a reader
    // consults it before it knows which digests to ask for. So publishing a
    // second component into one suite has to leave the first named: the index
    // an earlier publish wrote survives on disk either way, but a section the
    // release does not name cannot be resolved, and the by-hash copies are no
    // escape from that. The proof is the provisioner drawing a package out of
    // each component after both have been published.
    let dir = common::scratch_dir("debian-pool-two-components");

    let base = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/bin/x", 0o755, b"ok\n")
        .finish();
    let primary = write_repo(
        &dir.join("primary"),
        "trixie",
        "amd64",
        &[Pkg::required("base", deb(&base), "")],
    );

    // One package per component, each with contents of its own, so the two
    // indexes differ and a digest carried across a publish is meaningful.
    let build = |name: &str| {
        let data = Tar::new()
            .dir("./usr", 0o755)
            .file(&format!("./usr/bin/{name}"), 0o755, b"#!/bin/true\n")
            .finish();
        let bytes = deb_with_control(
            &format!(
                "Package: {name}\nVersion: 1.0\nArchitecture: amd64\nMaintainer: test\n\
                 Description: the {name} package\n"
            ),
            &data,
        );
        let path = dir.join(format!("{name}_1.0_amd64.deb"));
        std::fs::write(&path, bytes).unwrap();
        path
    };
    let in_main = build("mainpkg");
    let in_contrib = build("contribpkg");

    let pool = dir.join("pool");
    let publish = |component: &str, package: &Path| {
        Pool::at(&pool)
            .suite("trixie")
            .architecture("amd64")
            .component(component)
            .publish([package])
            .unwrap_or_else(|err| panic!("publishing into {component}: {err}"));
    };
    publish("main", &in_main);
    publish("contrib", &in_contrib);

    let release_text = || std::fs::read_to_string(pool.join("dists/trixie/Release")).unwrap();
    let release = release_text();
    assert!(
        release.contains("Components: contrib main\n"),
        "the second publish retired the first component: {release}",
    );

    // The digest a section is recorded under is what a reader verifies its
    // fetch against, so republishing one component must leave the other's
    // exactly as it was. Anything else breaks a reader that is midway through
    // resolving a component this publish has nothing to do with.
    let digest_of = |release: &str, section: &str| -> String {
        release
            .lines()
            .find(|line| line.ends_with(section))
            .unwrap_or_else(|| panic!("the release does not name {section}: {release}"))
            .split_whitespace()
            .next()
            .expect("a section line begins with its digest")
            .to_string()
    };
    let contrib_before = digest_of(&release, "contrib/binary-amd64/Packages");
    publish("main", &in_main);
    assert_eq!(
        digest_of(&release_text(), "contrib/binary-amd64/Packages"),
        contrib_before,
        "republishing main changed the digest contrib's index is recorded under",
    );

    // Reachability, end to end: resolution verifies each index it fetches
    // against the digest the release records, so drawing a package out of both
    // components is only possible if the release names both sections.
    let pool_repo = Repository::builder("trixie")
        .mirror(format!("file://{}", pool.display()))
        .components(["main", "contrib"])
        .trust_unsigned(true)
        .name("localpool")
        .build()
        .expect("the pool repository validates");

    let rootfs = dir.join("rootfs");
    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(primary)
        .trust_unsigned(true)
        .include(["mainpkg", "contribpkg"])
        .repository(pool_repo)
        .cache_dir(dir.join("cache"))
        .extract_only(true)
        .build()
        .expect("the builder validates");

    let plan = debian
        .resolve()
        .expect("both components resolve out of one pool");
    let mut names: Vec<_> = plan.packages.iter().map(|p| p.name.as_str()).collect();
    names.sort_unstable();
    assert_eq!(names, ["base", "contribpkg", "mainpkg"]);

    assert_eq!(
        provision::ensure(&rootfs, &mut debian).expect("the bootstrap runs"),
        Provisioned::Created,
    );
    // Each package came from its own component's pool directory.
    assert!(rootfs.join("usr/bin/mainpkg").is_file());
    assert!(rootfs.join("usr/bin/contribpkg").is_file());
}

#[test]
fn hermetic_empty_published_pool_is_a_valid_repository() {
    // A pool published with no .debs is a valid, empty repository: its Release
    // declares the architecture and component, so the provisioner's release
    // check passes and the empty package set contributes nothing. This is the
    // create-the-pool-up-front case before the first component has built.
    let dir = common::scratch_dir("debian-empty-pool");
    let base = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/bin/x", 0o755, b"ok\n")
        .finish();
    let primary = write_repo(
        &dir.join("primary"),
        "trixie",
        "amd64",
        &[Pkg::required("base", deb(&base), "")],
    );

    let pool = dir.join("pool");
    trixie_pool(&pool)
        .publish::<&Path>([])
        .expect("an empty pool publishes");

    let pool_repo = Repository::builder("trixie")
        .mirror(format!("file://{}", pool.display()))
        .trust_unsigned(true)
        .name("empty")
        .build()
        .expect("the empty pool repository validates");

    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(primary)
        .trust_unsigned(true)
        .repository(pool_repo)
        .build()
        .expect("the builder validates");
    let plan = debian
        .resolve()
        .expect("resolution succeeds with an empty pool declared");
    let names: Vec<_> = plan.packages.iter().map(|p| p.name.as_str()).collect();
    assert_eq!(names, ["base"], "the empty pool contributes nothing");
}

#[test]
fn hermetic_published_pool_keeps_the_highest_version_across_calls() {
    // Pool::publish is incremental and idempotent: publishing a higher version
    // supersedes a lower one, and re-publishing the lower one afterward does not
    // regress the index. The resolver draws the highest version, matching the
    // pool's own dedupe.
    let dir = common::scratch_dir("debian-pool-dedupe");
    let base = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/bin/x", 0o755, b"ok\n")
        .finish();
    let primary = write_repo(
        &dir.join("primary"),
        "trixie",
        "amd64",
        &[Pkg::required("base", deb(&base), "")],
    );

    let data = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/bin/tool", 0o755, b"v\n")
        .finish();
    let v1 = deb_with_control(
        "Package: tool\nVersion: 1.0\nArchitecture: amd64\nDescription: a tool\n",
        &data,
    );
    let v2 = deb_with_control(
        "Package: tool\nVersion: 2.0\nArchitecture: amd64\nDescription: a tool\n",
        &data,
    );
    let p1 = dir.join("tool_1.0.deb");
    let p2 = dir.join("tool_2.0.deb");
    std::fs::write(&p1, &v1).unwrap();
    std::fs::write(&p2, &v2).unwrap();

    let pool = dir.join("pool");
    trixie_pool(&pool).publish([&p1]).unwrap();
    trixie_pool(&pool).publish([&p2]).unwrap();
    // Re-publishing the older version must not win over the newer one.
    trixie_pool(&pool).publish([&p1]).unwrap();

    let pool_repo = Repository::builder("trixie")
        .mirror(format!("file://{}", pool.display()))
        .trust_unsigned(true)
        .name("pool")
        .build()
        .unwrap();
    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(primary)
        .trust_unsigned(true)
        .include(["tool"])
        .repository(pool_repo)
        .build()
        .unwrap();
    let plan = debian.resolve().expect("resolves");
    let tool = plan
        .packages
        .iter()
        .find(|p| p.name == "tool")
        .expect("the tool resolved from the pool");
    assert_eq!(
        tool.version, "2.0",
        "the highest version wins across publish calls",
    );
}

#[test]
fn hermetic_concurrent_publishes_keep_every_package() {
    // Publishing rebuilds the index from what the pool already holds, so
    // concurrent publishes that each started from the same prior index would
    // silently drop each other's packages. Every package a publish reported as
    // written must be in the final index.
    let dir = common::scratch_dir("debian-pool-concurrent");
    let data = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/bin/tool", 0o755, b"v\n")
        .finish();

    // One .deb per publisher, each a distinct package name, so a lost update
    // shows up as a missing name rather than a version regression.
    let names: Vec<String> = (0..8).map(|i| format!("component{i}")).collect();
    let paths: Vec<std::path::PathBuf> = names
        .iter()
        .map(|name| {
            let bytes = deb_with_control(
                &format!(
                    "Package: {name}\nVersion: 1.0\nArchitecture: amd64\nDescription: a part\n"
                ),
                &data,
            );
            let path = dir.join(format!("{name}.deb"));
            std::fs::write(&path, &bytes).unwrap();
            path
        })
        .collect();

    let pool = dir.join("pool");
    let publishers: Vec<_> = paths
        .into_iter()
        .map(|path| {
            let pool = pool.clone();
            std::thread::spawn(move || trixie_pool(&pool).publish([&path]))
        })
        .collect();
    for publisher in publishers {
        publisher
            .join()
            .unwrap()
            .expect("a concurrent publish succeeds");
    }

    let packages =
        std::fs::read_to_string(pool.join("dists/trixie/main/binary-amd64/Packages")).unwrap();
    for name in &names {
        assert!(
            packages.contains(&format!("Package: {name}\n")),
            "{name} was dropped from the index by a concurrent publish",
        );
    }
}

#[test]
fn hermetic_a_published_release_keeps_resolving_after_later_publishes() {
    // The property that lets a pipeline resolve against the pool while other
    // components publish into it: a reader holding a Release resolves the
    // indexes that Release described, however many publishes land afterward.
    // The Release declares Acquire-By-Hash, and the by-hash copies it names are
    // never rewritten or removed.
    let dir = common::scratch_dir("debian-pool-by-hash");
    let data = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/bin/tool", 0o755, b"v\n")
        .finish();
    let write_deb = |name: &str| {
        let bytes = deb_with_control(
            &format!("Package: {name}\nVersion: 1.0\nArchitecture: amd64\nDescription: a part\n"),
            &data,
        );
        let path = dir.join(format!("{name}.deb"));
        std::fs::write(&path, &bytes).unwrap();
        path
    };

    let pool = dir.join("pool");
    trixie_pool(&pool).publish([write_deb("first")]).unwrap();

    // What a reader would have read before the later publishes.
    let release_path = pool.join("dists/trixie/Release");
    let captured = std::fs::read_to_string(&release_path).unwrap();
    assert!(
        captured.contains("Acquire-By-Hash: yes"),
        "the release must direct readers at the immutable index copies",
    );
    let digests: Vec<String> = captured
        .lines()
        .filter(|line| line.starts_with(' '))
        .filter_map(|line| line.split_whitespace().next())
        .map(str::to_string)
        .collect();
    assert_eq!(
        digests.len(),
        2,
        "the release names Packages and Packages.gz"
    );

    for name in ["second", "third", "fourth"] {
        trixie_pool(&pool).publish([write_deb(name)]).unwrap();
    }

    // The captured Release is now stale, but everything it named still resolves
    // to the bytes it described.
    let by_hash = pool.join("dists/trixie/main/binary-amd64/by-hash/SHA256");
    for digest in &digests {
        let body = std::fs::read(by_hash.join(digest)).unwrap_or_else(|err| {
            panic!("the index {digest} named by a read Release is gone: {err}")
        });
        assert_eq!(
            common::deb_repo::sha256_hex(&body),
            *digest,
            "a by-hash index must hold the bytes its name claims",
        );
    }

    // And the current Release names the current index, which has everything.
    let packages =
        std::fs::read_to_string(pool.join("dists/trixie/main/binary-amd64/Packages")).unwrap();
    for name in ["first", "second", "third", "fourth"] {
        assert!(
            packages.contains(&format!("Package: {name}\n")),
            "{name} missing"
        );
    }
}

#[test]
fn hermetic_republishing_identical_bytes_does_not_rewrite_the_pool_file() {
    // Copying identical bytes over themselves would be the only routine rewrite
    // of a file a reader may be fetching, so an unchanged .deb is left alone.
    let dir = common::scratch_dir("debian-pool-idempotent");
    let data = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/bin/tool", 0o755, b"v\n")
        .finish();
    let bytes = deb_with_control(
        "Package: tool\nVersion: 1.0\nArchitecture: amd64\nDescription: a tool\n",
        &data,
    );
    let source = dir.join("tool.deb");
    std::fs::write(&source, &bytes).unwrap();

    let pool = dir.join("pool");
    trixie_pool(&pool).publish([&source]).unwrap();

    let published = pool.join("pool/main/t/tool/tool_1.0_amd64.deb");
    let first = std::fs::metadata(&published).unwrap();

    trixie_pool(&pool).publish([&source]).unwrap();

    let second = std::fs::metadata(&published).unwrap();
    assert_eq!(
        (first.ino(), first.modified().unwrap()),
        (second.ino(), second.modified().unwrap()),
        "republishing identical bytes replaced the pool file",
    );
    assert_eq!(std::fs::read(&published).unwrap(), bytes);
}

/// The `Filename`, `Size`, and `SHA256` the published index records for
/// `package`.
fn published_fields(pool: &Path, package: &str) -> (String, u64, String) {
    let packages =
        std::fs::read_to_string(pool.join("dists/trixie/main/binary-amd64/Packages")).unwrap();
    let stanza = packages
        .split("\n\n")
        .find(|block| {
            block
                .lines()
                .any(|line| line == format!("Package: {package}"))
        })
        .unwrap_or_else(|| panic!("{package} is not in the published index"));
    let field = |name: &str| {
        stanza
            .lines()
            .find_map(|line| line.strip_prefix(&format!("{name}: ")))
            .unwrap_or_else(|| panic!("the {package} stanza has no {name}"))
            .to_string()
    };
    (
        field("Filename"),
        field("Size").parse().expect("Size is a number"),
        field("SHA256"),
    )
}

#[test]
fn hermetic_rewriting_a_source_deb_does_not_change_the_pool() {
    // The pool's copy is its own inode, never an alias of the caller's file. A
    // clone shares storage and nothing else, and the streamed fallback shares
    // nothing at all, so a caller that rewrites its .deb in place cannot change
    // bytes a reader is resolving against. A hard link into the pool would be
    // cheaper still and would break exactly this.
    let dir = common::scratch_dir("debian-pool-independent");
    let bytes = deb_with_control(
        "Package: tool\nVersion: 1.0\nArchitecture: amd64\nDescription: a tool\n",
        &Tar::new()
            .dir("./usr", 0o755)
            .file("./usr/bin/tool", 0o755, b"v1\n")
            .finish(),
    );
    let source = dir.join("tool.deb");
    std::fs::write(&source, &bytes).unwrap();

    let pool = dir.join("pool");
    trixie_pool(&pool).publish([&source]).unwrap();

    let published = pool.join("pool/main/t/tool/tool_1.0_amd64.deb");
    assert_ne!(
        std::fs::metadata(&published).unwrap().ino(),
        std::fs::metadata(&source).unwrap().ino(),
        "the pool must hold its own inode, not an alias of the caller's",
    );

    // Rewrite the caller's .deb in place at a different length, then at the
    // same length: a shared inode would carry either straight into the pool.
    let rebuilt = deb_with_control(
        "Package: tool\nVersion: 1.0\nArchitecture: amd64\nDescription: a rebuilt tool\n",
        &Tar::new()
            .dir("./usr", 0o755)
            // Enough to span more tar blocks than the original, so the rewrite
            // genuinely changes the file's length.
            .file("./usr/bin/tool", 0o755, &vec![b'v'; 4096])
            .finish(),
    );
    assert_ne!(rebuilt.len(), bytes.len(), "the rewrite must change length");
    std::fs::write(&source, &rebuilt).unwrap();
    assert_eq!(
        std::fs::read(&published).unwrap(),
        bytes,
        "rewriting the source at a new length changed the pool's copy",
    );

    let mut flipped = bytes.clone();
    *flipped.last_mut().unwrap() ^= 0xff;
    std::fs::write(&source, &flipped).unwrap();
    assert_eq!(
        std::fs::read(&published).unwrap(),
        bytes,
        "rewriting the source in place changed the pool's copy",
    );

    // And the index still describes the file the pool holds.
    let (filename, size, digest) = published_fields(&pool, "tool");
    assert_eq!(filename, "pool/main/t/tool/tool_1.0_amd64.deb");
    assert_eq!(size, bytes.len() as u64);
    assert_eq!(digest, common::deb_repo::sha256_hex(&bytes));
}

#[test]
fn hermetic_republishing_changed_bytes_replaces_the_file_and_its_digest() {
    // The documented republish case: the archive layout stores one version at
    // one path, so different bytes at the same version replace what is there.
    // The index must follow the file rather than go on describing what the file
    // used to be — the recorded digest and the stored bytes are one claim.
    let dir = common::scratch_dir("debian-pool-republish-changed");
    let control = "Package: tool\nVersion: 1.0\nArchitecture: amd64\nDescription: a tool\n";
    let first = deb_with_control(
        control,
        &Tar::new()
            .dir("./usr", 0o755)
            .file("./usr/bin/tool", 0o755, b"v1\n")
            .finish(),
    );
    // Both payloads fit the same number of tar blocks, so the two .debs are the
    // same length and only their content differs. That is deliberate: it takes
    // the comparison past the length check and onto the digests, the path a
    // rebuild of one component actually takes.
    let second = deb_with_control(
        control,
        &Tar::new()
            .dir("./usr", 0o755)
            .file("./usr/bin/tool", 0o755, b"v2\n")
            .finish(),
    );

    let source = dir.join("tool.deb");
    let pool = dir.join("pool");
    std::fs::write(&source, &first).unwrap();
    trixie_pool(&pool).publish([&source]).unwrap();
    std::fs::write(&source, &second).unwrap();
    trixie_pool(&pool).publish([&source]).unwrap();

    let published = pool.join("pool/main/t/tool/tool_1.0_amd64.deb");
    assert_eq!(
        std::fs::read(&published).unwrap(),
        second,
        "the pool must hold the bytes most recently published",
    );
    let (_, size, digest) = published_fields(&pool, "tool");
    assert_eq!(size, second.len() as u64);
    assert_eq!(digest, common::deb_repo::sha256_hex(&second));
}

#[test]
fn hermetic_resolve_layer_omits_the_base_packages() {
    // A layered resolution treats the base's configured set as satisfied and
    // returns only the increment. The base's dpkg status lists `base` and
    // `libdep` installed; resolving an include that depends on `libdep` yields
    // just the include, while a full resolution of the same repository draws in
    // the base packages it stands on. The contrast is the whole point of the
    // assume-installed seed.
    let dir = common::scratch_dir("debian-resolve-layer");

    let base_data = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/bin/x", 0o755, b"ok\n")
        .finish();
    let libdep_data = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/lib/libdep.so", 0o644, b"lib\n")
        .finish();
    let tool_data = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/bin/tool", 0o755, b"#!/bin/true\n")
        .finish();
    let mirror = write_repo(
        &dir.join("repo"),
        "trixie",
        "amd64",
        &[
            Pkg::required("base", deb(&base_data), "libdep"),
            Pkg::ordinary("libdep", deb(&libdep_data)),
            Pkg::ordinary("tool", deb(&tool_data)).depending_on("libdep"),
        ],
    );

    // A base whose dpkg status database records the base packages configured.
    let base_dir = dir.join("base");
    let dpkg = base_dir.join("var/lib/dpkg");
    std::fs::create_dir_all(&dpkg).unwrap();
    std::fs::write(
        dpkg.join("status"),
        "Package: base\nStatus: install ok installed\nVersion: 1.0\n\n\
         Package: libdep\nStatus: install ok installed\nVersion: 1.0\n",
    )
    .unwrap();

    let mut layered = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror.clone())
        .trust_unsigned(true)
        .base_layer(&base_dir)
        .include(["tool"])
        .build()
        .expect("the builder validates");
    let plan = layered.resolve_layer().expect("the layer resolves");
    let names: Vec<_> = plan.packages.iter().map(|p| p.name.as_str()).collect();
    assert_eq!(
        names,
        ["tool"],
        "the increment is the include alone; the base's `libdep` dependency and \
         seed packages are assumed satisfied",
    );

    // The same repository resolved as a full bootstrap draws in the base seed and
    // the dependency the layer left to the base.
    let mut full = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .include(["tool"])
        .build()
        .expect("the builder validates");
    let mut full_names: Vec<_> = full
        .resolve()
        .expect("the full plan resolves")
        .packages
        .iter()
        .map(|p| p.name.clone())
        .collect();
    full_names.sort();
    assert_eq!(
        full_names,
        ["base", "libdep", "tool"],
        "a full bootstrap installs the base seed and the shared dependency",
    );
}

#[test]
fn hermetic_resolve_layer_requires_a_configured_base() {
    // A base layer that is not a configured bootstrap — no status database, or an
    // empty one from an extract-only tree — is refused rather than treated as an
    // empty base, which would draw the whole base system into the increment.
    let dir = common::scratch_dir("debian-layer-base-check");
    let base_data = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/bin/x", 0o755, b"ok\n")
        .finish();
    let mirror = write_repo(
        &dir.join("repo"),
        "trixie",
        "amd64",
        &[Pkg::required("base", deb(&base_data), "")],
    );

    // No status database at all.
    let missing = dir.join("no-base");
    std::fs::create_dir_all(&missing).unwrap();
    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .base_layer(&missing)
        .include(["base"])
        .build()
        .expect("the builder validates");
    assert!(
        debian.resolve_layer().is_err(),
        "a base with no dpkg status database is refused",
    );
}

#[test]
fn hermetic_stage_layer_empty_increment_stages_without_configuring() {
    // When every requested package is already in the base, the increment is
    // empty: stage_layer installs nothing, launches no dpkg wave, and returns a
    // build layer over an empty upper. This exercises the empty-increment path
    // hermetically — it runs no cage — so it needs no network or real dpkg; it
    // skips only where the overlay preflight reports the host cannot overlay.
    let dir = common::scratch_dir("debian-empty-layer");
    if let Some(blocker) = ferroday_cage::host::overlay_blocker(&dir) {
        eprintln!("skipping: overlay-rooted cages are unavailable: {blocker}");
        return;
    }
    let base_data = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/bin/x", 0o755, b"ok\n")
        .finish();
    let mirror = write_repo(
        &dir.join("repo"),
        "trixie",
        "amd64",
        &[Pkg::required("base", deb(&base_data), "")],
    );

    // A configured base carrying `base`, the only package the layer requests.
    let base_dir = dir.join("base");
    let dpkg = base_dir.join("var/lib/dpkg");
    std::fs::create_dir_all(&dpkg).unwrap();
    std::fs::write(
        dpkg.join("status"),
        "Package: base\nStatus: install ok installed\nVersion: 1.0\n",
    )
    .unwrap();

    let upper = dir.join("upper");
    let layer = {
        let mut debian = Debian::builder("trixie")
            .architecture("amd64")
            .mirror(mirror)
            .trust_unsigned(true)
            .base_layer(&base_dir)
            .include(["base"])
            .build()
            .expect("the builder validates");
        debian
            .stage_layer(&upper)
            .expect("an empty increment stages without configuring")
    };
    assert_eq!(layer.path(), upper.as_path());
    assert!(
        upper.is_dir(),
        "the upper exists even for an empty increment"
    );
    drop(layer);
    assert!(
        !upper.exists(),
        "the empty layer's upper is removed on drop"
    );
}

#[test]
fn hermetic_a_plan_is_the_increment_a_layered_build_installs() {
    // A plan set alongside a base layer describes the increment. The layered
    // path read no plan at all and `plan()` forbids `include()`, so the seed was
    // empty: `stage_layer` returned a build layer over an empty upper having
    // installed nothing, and `resolve_layer` reported that same empty closure.
    let dir = common::scratch_dir("debian-layer-plan");
    if let Some(blocker) = ferroday_cage::host::overlay_blocker(&dir) {
        eprintln!("skipping: overlay-rooted cages are unavailable: {blocker}");
        return;
    }
    let mirror = write_repo(
        &dir.join("repo"),
        "trixie",
        "amd64",
        &[
            Pkg::required("base", payload("base"), ""),
            Pkg::ordinary("extra", payload("extra")),
        ],
    );

    // A configured base carrying only `base`.
    let base_dir = dir.join("base");
    let dpkg = base_dir.join("var/lib/dpkg");
    std::fs::create_dir_all(&dpkg).unwrap();
    std::fs::write(
        dpkg.join("status"),
        "Package: base\nStatus: install ok installed\nVersion: 1.0\n",
    )
    .unwrap();

    let layered = |plan: Option<Plan>| {
        let mut builder = Debian::builder("trixie")
            .architecture("amd64")
            .mirror(mirror.clone())
            .trust_unsigned(true)
            .cache_dir(dir.join("cache"))
            .base_layer(&base_dir);
        builder = match plan {
            Some(plan) => builder.plan(plan),
            None => builder.include(["extra"]),
        };
        builder.build().expect("the builder validates")
    };

    let plan = layered(None)
        .resolve_layer()
        .expect("the increment resolves");
    assert_eq!(
        plan.packages
            .iter()
            .map(|package| package.name.clone())
            .collect::<Vec<_>>(),
        ["extra"],
        "the base already carries `base`, so the increment is `extra` alone",
    );

    // The plan is what the layered resolve answers with, rather than a closure
    // resolved from an empty seed.
    let answered = layered(Some(plan.clone()))
        .resolve_layer()
        .expect("a configured plan needs no archive to answer");
    assert_eq!(
        answered
            .packages
            .iter()
            .map(|package| package.name.clone())
            .collect::<Vec<_>>(),
        ["extra"],
    );

    // And it is what staging installs. The configure wave needs a real dpkg in
    // the base, which this fixture has none of, so the call fails there -- what
    // this asserts is that the increment's package was fetched, which an empty
    // layer would not have done.
    let watcher = Watching::default();
    let asked = std::sync::Arc::clone(&watcher.asked);
    let mut staging = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .cache_dir(dir.join("cache"))
        .base_layer(&base_dir)
        .plan(plan)
        .fetcher(Box::new(watcher))
        .build()
        .expect("the builder validates");
    let _ = staging.stage_layer(dir.join("upper"));
    let urls = asked.lock().unwrap().clone();
    assert!(
        urls.iter()
            .any(|url| url.contains("/pool/") && url.contains("extra")),
        "the plan's package was never fetched: {urls:?}",
    );
}

#[test]
fn hermetic_a_failed_stage_layer_disposes_of_the_upper_it_created() {
    // stage_layer creates the upper before it resolves, and a failure hands the
    // caller no BuildLayer to drop — so the upper it created has to go with the
    // failure, or a partly-installed increment (and the download cache beside
    // it) is orphaned with nothing that knows to remove it.
    //
    // The failure is an include the archive does not carry, which is refused
    // after the upper exists and before anything is downloaded.
    let dir = common::scratch_dir("debian-failed-layer");
    if let Some(blocker) = ferroday_cage::host::overlay_blocker(&dir) {
        eprintln!("skipping: overlay-rooted cages are unavailable: {blocker}");
        return;
    }
    let base_data = Tar::new().file("./usr/bin/x", 0o755, b"ok\n").finish();
    let mirror = write_repo(
        &dir.join("repo"),
        "trixie",
        "amd64",
        &[Pkg::required("base", deb(&base_data), "")],
    );

    let base_dir = dir.join("base");
    let dpkg = base_dir.join("var/lib/dpkg");
    std::fs::create_dir_all(&dpkg).unwrap();
    std::fs::write(
        dpkg.join("status"),
        "Package: base\nStatus: install ok installed\nVersion: 1.0\n",
    )
    .unwrap();

    let upper = dir.join("upper");
    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .base_layer(&base_dir)
        .include(["nonesuch"])
        .build()
        .expect("the builder validates");
    debian
        .stage_layer(&upper)
        .expect_err("an unknown include fails the staging");

    assert!(!upper.exists(), "a failed staging left its upper behind");
    assert!(
        !dir.join("upper.fcage-debs").exists(),
        "a failed staging left its package cache behind",
    );
    let mut left: Vec<String> = std::fs::read_dir(&dir)
        .unwrap()
        .map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned())
        .collect();
    left.sort();
    assert_eq!(left, ["base", "repo"]);
}

#[test]
fn hermetic_mirror_fallback_serves_a_missing_primary() {
    // The primary mirror points at a directory that does not exist, so every
    // fetch 404s there and falls through to the backstop, which serves the whole
    // repository. A successful bootstrap proves the ordered-URL failover.
    let dir = common::scratch_dir("debian-fallback");
    let payload = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/bin/x", 0o755, b"ok\n")
        .finish();
    let backstop = write_repo(
        &dir.join("backstop"),
        "trixie",
        "amd64",
        &[Pkg::required("base", deb(&payload), "")],
    );
    // A file:// URL to a path that was never created: every resource under it is
    // reported missing, the signal the mirror walk advances past.
    let missing = format!("file://{}", dir.join("does-not-exist").display());

    let rootfs = dir.join("rootfs");
    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(missing)
        .mirror_fallback(backstop)
        .trust_unsigned(true)
        .cache_dir(dir.join("cache"))
        .extract_only(true)
        .build()
        .expect("the builder validates");
    assert_eq!(
        provision::ensure(&rootfs, &mut debian).expect("the fallback bootstrap runs"),
        Provisioned::Created,
    );
    assert!(rootfs.join("usr/bin/x").is_file());
}

#[test]
fn an_archive_coordinate_the_repository_cannot_be_addressed_by_is_refused() {
    // The suite, the components, and the architecture become path segments of
    // every URL a repository is fetched through, and words of the `deb` line
    // written into the finished rootfs. A `..` segment climbs out of the mirror
    // root — for a file:// mirror, a local read — and a control character splits
    // the request line the value is interpolated into, or adds a source line the
    // caller never wrote. They pass the same check the pool holds its own
    // coordinates to, so an archive this crate writes and one it reads agree on
    // what an archive may be called.
    let refused = |what: &str, build: Result<Debian<'_>, _>| match build {
        Err(ferroday_cage::provision::debian::DebianError::Config { reason, .. }) => {
            assert!(reason.contains(what), "{what}: {reason}");
        }
        other => panic!("{what} should be refused, got {other:?}"),
    };
    let base = || {
        Debian::builder("trixie")
            .mirror("file:///srv/debs")
            .trust_unsigned(true)
    };

    for suite in ["../../etc", "/etc", "a//b", "with space", "a\r\nX: 1"] {
        refused(
            "suite",
            Debian::builder(suite)
                .mirror("file:///srv/debs")
                .trust_unsigned(true)
                .build(),
        );
    }
    refused(
        "component",
        base().components(["main", "../../etc"]).build(),
    );
    refused("architecture", base().architecture("../../etc").build());
    // An architecture is one directory name; the other two may name a subtree.
    refused("architecture", base().architecture("linux/amd64").build());
    Debian::builder("buster/updates")
        .mirror("file:///srv/debs")
        .trust_unsigned(true)
        .components(["main/debian-installer"])
        .build()
        .expect("a slashed suite and component are real layouts");

    // An additional repository is held to the same rule, at its own build.
    assert!(
        Repository::builder("../../etc")
            .mirror("file:///srv/debs")
            .trust_unsigned(true)
            .build()
            .is_err(),
        "an additional repository's suite is unchecked",
    );
}

#[test]
fn the_unsigned_path_is_refused_without_trust_unsigned() {
    // The same unsigned file:// repository, but without trust_unsigned: the
    // default path requires a signed InRelease, which the repository does not
    // carry, so provisioning fails rather than silently trusting the plain
    // Release. This is the default-secure property the other hermetic tests,
    // which all pass trust_unsigned(true), do not exercise.
    let dir = common::scratch_dir("debian-unsigned-default");
    let base = Tar::new()
        .dir("./usr", 0o755)
        .file("./usr/bin/x", 0o755, b"ok\n")
        .finish();
    let mirror = write_repo(
        &dir.join("repo"),
        "trixie",
        "amd64",
        &[Pkg::required("base", deb(&base), "")],
    );
    let rootfs = dir.join("rootfs");
    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        // No trust_unsigned: the archive signature must be present and verify.
        .cache_dir(dir.join("cache"))
        .extract_only(true)
        .build()
        .expect("the builder validates");
    assert!(
        provision::ensure(&rootfs, &mut debian).is_err(),
        "an unsigned repository must not provision without trust_unsigned",
    );
    assert!(!rootfs.exists(), "a refused bootstrap publishes nothing");
}

#[test]
fn hermetic_rejects_an_escaping_deb_entry() {
    // A package whose data.tar tries to write outside the root is refused by
    // the hardened extractor, and nothing is published.
    let dir = common::scratch_dir("debian-hostile");
    let evil = Tar::new().file("../escape", 0o644, b"pwned\n").finish();
    let mirror = write_repo(
        &dir.join("repo"),
        "trixie",
        "amd64",
        &[Pkg::required("evil", deb(&evil), "")],
    );
    let rootfs = dir.join("rootfs");
    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .cache_dir(dir.join("cache"))
        .extract_only(true)
        .build()
        .expect("the builder validates");
    let err = provision::ensure(&rootfs, &mut debian).unwrap_err();
    assert!(
        matches!(err, ProvisionError::EntryUnsafe { .. }),
        "an escaping entry is rejected: {err:?}"
    );
    assert!(!rootfs.exists(), "a failed bootstrap publishes nothing");
}

/// A control stanza for `name` at `version`, with the fields every `.deb`
/// carries and nothing else.
fn control_of(name: &str, version: &str) -> String {
    format!(
        "Package: {name}\nVersion: {version}\nArchitecture: amd64\nMaintainer: test\n\
         Description: a test package\n",
    )
}

/// The names of every file under `dir`, recursively, in no particular order.
fn files_under(dir: &Path) -> Vec<String> {
    let mut found = Vec::new();
    let Ok(entries) = std::fs::read_dir(dir) else {
        return found;
    };
    for entry in entries.flatten() {
        if entry.file_type().is_ok_and(|kind| kind.is_dir()) {
            found.extend(files_under(&entry.path()));
        } else {
            found.push(entry.file_name().to_string_lossy().into_owned());
        }
    }
    found
}

#[test]
fn hermetic_a_deb_cannot_choose_where_in_the_pool_it_is_written() {
    // The pool path a `.deb` is stored at is derived from the package's own
    // control stanza, and `Path::join` does not normalize: a `..` in the name
    // survives into the syscall and the kernel resolves it, so a publish would
    // create directories and write a file outside the pool root, as the
    // publishing user. The version reaches the same path through the file-name
    // component. Both are held to the alphabet Debian policy gives them before
    // any path is built from them.
    //
    // The pool is nested several levels inside the scratch directory so that a
    // regression here writes there rather than somewhere a test has no business
    // touching.
    let dir = common::scratch_dir("debian-pool-traversal");
    let pool = dir.join("a/b/c/d/e/pool");
    let data = Tar::new().file("./usr/bin/x", 0o755, b"ok\n").finish();

    let refused = |control: String, note: &str| {
        let path = dir.join("candidate.deb");
        std::fs::write(&path, deb_with_control(&control, &data)).unwrap();
        let reason = trixie_pool(&pool)
            .publish([&path])
            .expect_err(note)
            .to_string();
        assert!(
            reason.contains("Debian policy permits"),
            "{note} was refused for the wrong reason: {reason}",
        );
    };

    refused(control_of("../../../../escaped", "1.0"), "a climbing name");
    refused(control_of("..", "1.0"), "a name that is `..`");
    refused(control_of(".", "1.0"), "a name that is `.`");
    refused(control_of("evil/../../x", "1.0"), "a name with a separator");
    refused(control_of("Evil", "1.0"), "an upper-case name");
    refused(control_of("x", "1.0"), "a one-character name");
    refused(control_of("-lead", "1.0"), "a name starting with a dash");
    refused(control_of("tool", "1.0/../../evil"), "a slashed version");
    refused(control_of("tool", "1.0 2.0"), "a spaced version");
    refused(control_of("tool", ""), "an empty version");

    // Nothing was created along the way to any of those paths: the pool tree a
    // published package lives in was never opened.
    assert!(
        !pool.join("pool").exists(),
        "a refused publish created a pool tree",
    );
    let stray: Vec<String> = files_under(&dir)
        .into_iter()
        .filter(|name| name.contains("escaped") || name.contains("evil"))
        .collect();
    assert!(stray.is_empty(), "a refused publish wrote {stray:?}");

    // An ordinary package still publishes, epoch and all: the check is of the
    // alphabet, not of the punctuation a real version uses.
    let path = dir.join("ok.deb");
    let control = control_of("tool", "2:1.3-1~bpo12+1");
    std::fs::write(&path, deb_with_control(&control, &data)).unwrap();
    trixie_pool(&pool)
        .publish([&path])
        .expect("an ordinary package publishes");
    assert!(
        pool.join("pool/main/t/tool/tool_1.3-1~bpo12+1_amd64.deb")
            .is_file(),
        "the published file is not where the archive convention puts it",
    );
}

#[test]
fn hermetic_a_deb_cannot_write_its_own_index_entry() {
    // A pool entry is the control file verbatim with Filename, Size, and SHA256
    // appended, and a deb822 reader resolves a field by its first occurrence. A
    // control file carrying one of those would shadow the archive's value: the
    // recorded digest would be one the package chose rather than one taken from
    // the bytes on disk, and the recorded Filename could name another package's
    // file in the same pool.
    //
    // A control file of several paragraphs reaches the same place by another
    // route: the appended fields land on the last, which is then the paragraph
    // that reads back as the entry — under whatever name and version it
    // declares, while the dedupe that placed it keyed on the first. A pool is
    // consumed as [trusted=yes] and resolution is highest-version-wins across
    // repositories, so an entry a package wrote for itself supersedes the
    // primary archive's.
    let dir = common::scratch_dir("debian-pool-shadowing");
    let pool = dir.join("pool");
    let data = Tar::new().file("./usr/bin/x", 0o755, b"ok\n").finish();

    let refused = |control: String, expected: &str, note: &str| {
        let path = dir.join("candidate.deb");
        std::fs::write(&path, deb_with_control(&control, &data)).unwrap();
        let reason = trixie_pool(&pool)
            .publish([&path])
            .expect_err(note)
            .to_string();
        assert!(
            reason.contains(expected),
            "{note} was refused for the wrong reason: {reason}",
        );
    };

    for field in ["Filename", "Size", "SHA256"] {
        refused(
            format!(
                "{}{field}: pool/main/o/other/other_1.0_amd64.deb\n",
                control_of("tool", "1.0")
            ),
            field,
            &format!("a control file carrying {field}"),
        );
    }
    refused(
        format!(
            "{}\n{}",
            control_of("tool", "1.0"),
            control_of("libc6", "99.0"),
        ),
        "paragraphs",
        "a control file of two paragraphs",
    );

    assert!(
        !pool.join("pool").exists(),
        "a refused publish created a pool tree",
    );

    // The well-formed case still publishes, and the index it writes carries the
    // archive's own values: the digest of the bytes in the pool, and the path
    // the pool chose.
    let path = dir.join("ok.deb");
    let deb = deb_with_control(&control_of("tool", "1.0"), &data);
    std::fs::write(&path, &deb).unwrap();
    trixie_pool(&pool)
        .publish([&path])
        .expect("a well-formed package publishes");
    let index =
        std::fs::read_to_string(pool.join("dists/trixie/main/binary-amd64/Packages")).unwrap();
    assert!(
        index.contains(&format!("SHA256: {}", common::deb_repo::sha256_hex(&deb))),
        "the index does not record the digest of the published bytes: {index}",
    );
    assert!(
        index.contains("Filename: pool/main/t/tool/tool_1.0_amd64.deb"),
        "the index does not record the path the pool chose: {index}",
    );
}

#[test]
fn hermetic_a_failed_bootstrap_leaves_no_package_cache_behind() {
    // Without a cache directory the packages are downloaded into a sibling of
    // the staging tree — a sibling because an extracted package's entries land
    // under the staging root, so a cache beneath it could be overwritten before
    // it is read. Nothing else clears that sibling: the publication's own
    // cleanup removes the staging tree, and provision::remove removes the
    // destination and its lock. So a failure partway has to take it, or a failed
    // bootstrap leaves a base system's worth of .debs beside a rootfs that was
    // never published.
    //
    // The failure is an escaping archive entry, which happens after the download
    // has populated the cache.
    let dir = common::scratch_dir("debian-failed-cache");
    let evil = Tar::new().file("../escape", 0o644, b"pwned\n").finish();
    let mirror = write_repo(
        &dir.join("repo"),
        "trixie",
        "amd64",
        &[Pkg::required("evil", deb(&evil), "")],
    );

    let rootfs = dir.join("rootfs");
    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .extract_only(true)
        .build()
        .expect("the builder validates");
    provision::ensure(&rootfs, &mut debian).expect_err("an escaping entry fails the bootstrap");

    assert!(!rootfs.exists(), "a failed bootstrap publishes nothing");
    // The staging tree and the cache beside it, by the names the run derives.
    assert!(!dir.join(".rootfs.staging").exists(), "the staging tree");
    assert!(
        !dir.join(".rootfs.staging.fcage-debs").exists(),
        "the package cache",
    );
    // And nothing else either: what is left is the repository the test wrote
    // and the publication lock, which `provision::remove` clears.
    let mut left: Vec<String> = std::fs::read_dir(&dir)
        .unwrap()
        .map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned())
        .collect();
    left.sort();
    assert_eq!(left, ["repo", "rootfs.lock"]);
}

#[test]
fn hermetic_rejects_a_digest_mismatch() {
    // The index records a digest; a package whose bytes do not match it is
    // rejected rather than installed.
    let dir = common::scratch_dir("debian-mismatch");
    let repo = dir.join("repo");
    let good = Tar::new().file("./usr/bin/x", 0o755, b"ok\n").finish();
    write_repo(
        &repo,
        "trixie",
        "amd64",
        &[Pkg::required("base", deb(&good), "")],
    );
    // Corrupt the published package so its bytes no longer match the index,
    // without changing its length: the download is bounded by the size the
    // index records, and a longer body would be refused at that bound before
    // the digest was reached. This is the digest's own test.
    let pool_deb = repo.join("pool/base_1.0_amd64.deb");
    let mut bytes = std::fs::read(&pool_deb).unwrap();
    *bytes.last_mut().unwrap() ^= 0xff;
    std::fs::write(&pool_deb, &bytes).unwrap();

    let rootfs = dir.join("rootfs");
    let mirror = format!("file://{}", repo.display());
    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .cache_dir(dir.join("cache"))
        .extract_only(true)
        .build()
        .expect("the builder validates");
    let err = provision::ensure(&rootfs, &mut debian).unwrap_err();
    let ProvisionError::Other { source: inner, .. } = &err else {
        panic!("expected a boxed Debian error, got {err:?}");
    };
    assert!(
        inner.to_string().contains("digest"),
        "a digest mismatch is reported: {inner}"
    );
    assert!(!rootfs.exists());
}

#[test]
fn hermetic_stops_a_package_at_the_size_the_index_recorded() {
    // The index records what the package weighs, so a mirror serving more is
    // serving something else. The download stops at that bound rather than
    // spending disk on the whole of it and discovering the substitution at the
    // digest afterward.
    let dir = common::scratch_dir("debian-oversized");
    let repo = dir.join("repo");
    let good = Tar::new().file("./usr/bin/x", 0o755, b"ok\n").finish();
    write_repo(
        &repo,
        "trixie",
        "amd64",
        &[Pkg::required("base", deb(&good), "")],
    );
    // Grow the published package well past its recorded size.
    let pool_deb = repo.join("pool/base_1.0_amd64.deb");
    let mut bytes = std::fs::read(&pool_deb).unwrap();
    bytes.resize(bytes.len() + 4 * 1024 * 1024, b'x');
    std::fs::write(&pool_deb, &bytes).unwrap();

    let rootfs = dir.join("rootfs");
    let cache = dir.join("cache");
    let mirror = format!("file://{}", repo.display());
    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .cache_dir(&cache)
        .extract_only(true)
        .build()
        .expect("the builder validates");
    let err = provision::ensure(&rootfs, &mut debian).unwrap_err();
    let ProvisionError::Other { source: inner, .. } = &err else {
        panic!("expected a boxed Debian error, got {err:?}");
    };
    assert!(
        inner.to_string().contains("maximum size"),
        "the oversized body is refused at the bound: {inner}"
    );
    assert!(!rootfs.exists());
    // Nothing oversized was left in the cache, and nothing was published there.
    let cached: u64 = std::fs::read_dir(&cache)
        .into_iter()
        .flatten()
        .flatten()
        .map(|entry| entry.metadata().unwrap().len())
        .sum();
    assert!(
        cached < bytes.len() as u64,
        "the whole oversized body reached the cache",
    );
}

#[test]
fn extract_only_bootstraps_a_trixie_tree() {
    if !network_enabled() {
        return;
    }
    let rootfs = common::scratch_dir("debian-extract").join("rootfs");
    let mut debian = Debian::builder("trixie")
        .cache_dir(common::download_cache("debian"))
        .extract_only(true)
        .build()
        .expect("the builder validates");

    let outcome = provision::ensure(&rootfs, &mut debian).expect("the extract-only bootstrap runs");
    assert_eq!(outcome, Provisioned::Created);

    // The base system's files are laid out, merged-usr is in place, and the
    // dpkg database is initialized — but nothing is configured.
    assert!(rootfs.join("usr/bin/dpkg").is_file(), "dpkg is extracted");
    assert!(
        rootfs.join("bin").is_symlink(),
        "merged-usr /bin is a symlink"
    );
    assert!(
        Path::new(&rootfs.join("usr/bin/sh")).is_symlink(),
        "dash provides /usr/bin/sh"
    );
    // The dpkg database is initialized but empty: extract-only runs no
    // maintainer scripts, so nothing is configured.
    assert!(
        rootfs.join("var/lib/dpkg/status").is_file(),
        "the dpkg status database is initialized"
    );
    assert_eq!(
        std::fs::read_to_string(rootfs.join("var/lib/dpkg/status")).unwrap(),
        "",
        "extract-only leaves the status database empty"
    );
    // The setgid shadow helper is laid out (root-owned, as everything is
    // under the single-identity map) but not configured.
    assert!(rootfs.join("usr/sbin/unix_chkpwd").is_file());
}

#[test]
fn full_bootstrap_configures_a_runnable_trixie() {
    if !network_enabled() {
        return;
    }
    let Some(_) = common::fixture_rootfs() else {
        // The full bootstrap runs dpkg inside a cage, which needs user
        // namespaces; the fixture probe reports the skip.
        return;
    };
    let rootfs = common::scratch_dir("debian-full").join("rootfs");
    // The provisioner borrows the progress sink, so both are scoped and
    // dropped before the finished rootfs is exercised.
    {
        let mut progress = |event: DebianEvent<'_>| {
            if let DebianEvent::CommandOutput { bytes, .. } = event {
                use std::io::Write;
                let _ = std::io::stderr().write_all(bytes);
            }
        };
        let mut debian = Debian::builder("trixie")
            .cache_dir(common::download_cache("debian"))
            .build()
            .expect("the builder validates");
        provision::ensure(&rootfs, &mut debian.observe(&mut progress))
            .expect("the full bootstrap runs");
    }

    // The finished rootfs is apt-usable: the archive keyring is written as the
    // signed-by trust anchor, and the sources.list references it. The base
    // closure does not install debian-archive-keyring, so without this the
    // apt-get update below would have no key to verify the release against.
    let anchor = std::fs::read(rootfs.join("usr/share/keyrings/debian-archive-keyring.gpg"))
        .expect("the trust anchor is written");
    assert!(
        !anchor.is_empty(),
        "the trust anchor holds the archive keyring the release was verified against",
    );
    let sources = std::fs::read_to_string(rootfs.join("etc/apt/sources.list"))
        .expect("the sources.list is written");
    assert!(
        sources.contains("[signed-by=/usr/share/keyrings/debian-archive-keyring.gpg]"),
        "the sources.list is signed-by the trust anchor: {sources:?}"
    );

    // Every base package reached the installed state; run dpkg in the cage
    // to prove the userland is configured and executable.
    let status = ferroday_cage::Cage::builder()
        .rootfs(&rootfs)
        .command("/usr/bin/dpkg")
        .args(["--list"])
        .build()
        .expect("the cage builds")
        .run()
        .expect("dpkg runs in the finished rootfs");
    assert!(
        status.success(),
        "dpkg --list succeeds in the bootstrapped rootfs"
    );

    // The real proof of the trust anchor: apt-get update verifies the release
    // from inside the rootfs against the signed-by keyring. A missing or
    // untrusted anchor makes apt exit non-zero here.
    let status = ferroday_cage::Cage::builder()
        .rootfs(&rootfs)
        .network(ferroday_cage::Network::Host)
        .command("/usr/bin/apt-get")
        .args(["update"])
        .build()
        .expect("the cage builds")
        .run()
        .expect("apt-get runs in the finished rootfs");
    assert!(
        status.success(),
        "apt-get update verifies the release against the signed-by trust anchor"
    );
}

#[test]
fn full_bootstrap_applies_a_pre_configure_overlay() {
    if !network_enabled() {
        return;
    }
    let Some(_) = common::fixture_rootfs() else {
        return;
    };
    let scratch = common::scratch_dir("debian-overlay");

    // A rootfs-shaped overlay carrying one configuration file at a path no
    // package owns, so it survives configuration untouched and its presence
    // proves the overlay was laid during the bootstrap.
    let overlay = scratch.join("overlay");
    std::fs::create_dir_all(overlay.join("etc")).unwrap();
    std::fs::write(overlay.join("etc/fcage-overlay.conf"), b"laid-by-overlay\n").unwrap();

    let rootfs = scratch.join("rootfs");
    {
        let mut debian = Debian::builder("trixie")
            .cache_dir(common::download_cache("debian"))
            .pre_configure_overlay(&overlay)
            .build()
            .expect("the builder validates");
        provision::ensure(&rootfs, &mut debian).expect("the bootstrap with an overlay runs");
    }

    assert_eq!(
        std::fs::read_to_string(rootfs.join("etc/fcage-overlay.conf")).unwrap(),
        "laid-by-overlay\n",
        "the overlay file is present in the finished rootfs",
    );
}

/// The layered build: a base provisioned once, a component's increment staged
/// over it into a disposable overlay upper, and the base left pristine.
#[test]
fn layered_build_stages_only_the_increment_over_a_pristine_base() {
    if !network_enabled() {
        return;
    }
    let Some(_) = common::fixture_rootfs() else {
        // stage_layer runs dpkg in a cage, which needs user namespaces.
        return;
    };
    let scratch = common::scratch_dir("debian-layer");
    if let Some(blocker) = ferroday_cage::host::overlay_blocker(&scratch) {
        eprintln!("skipping: overlay-rooted cages are unavailable: {blocker}");
        return;
    }

    // Provision the shared base once, the ordinary way: a configured minbase.
    let base = scratch.join("base");
    {
        let mut debian = Debian::builder("trixie")
            .cache_dir(common::download_cache("debian"))
            .build()
            .expect("the builder validates");
        provision::ensure(&base, &mut debian).expect("the base bootstrap runs");
    }

    // Stage `hello` as an increment. Its only dependency, libc6, is in the base,
    // so the increment is `hello` alone.
    let upper = scratch.join("upper");
    let reported: std::cell::RefCell<Option<Plan>> = std::cell::RefCell::new(None);
    let layer = {
        let mut sink = |event: DebianEvent<'_>| match event {
            DebianEvent::Resolved { plan, .. } => *reported.borrow_mut() = Some(plan.clone()),
            DebianEvent::CommandOutput { bytes, .. } => {
                use std::io::Write;
                let _ = std::io::stderr().write_all(bytes);
            }
            _ => {}
        };
        let mut debian = Debian::builder("trixie")
            .base_layer(&base)
            .include(["hello"])
            .cache_dir(common::download_cache("debian"))
            .build()
            .expect("the builder validates");
        debian
            .observe(&mut sink)
            .stage_layer(&upper)
            .expect("the increment stages")
    };

    // The increment omits the base's packages: `hello` is present, and the
    // `libc6` it depends on — configured in the base — is not re-resolved.
    let plan = reported
        .into_inner()
        .expect("a Resolved event carried the plan");
    let names: Vec<_> = plan.packages.iter().map(|p| p.name.as_str()).collect();
    assert!(
        names.contains(&"hello"),
        "the increment installs hello: {names:?}"
    );
    assert!(
        !names.contains(&"libc6"),
        "the base's libc6 is assumed satisfied, not re-resolved: {names:?}",
    );

    // The upper holds only the increment's files and dpkg state. `hello`'s binary
    // landed there; the base's own files stayed in the read-only lower.
    assert!(
        upper.join("usr/bin/hello").is_file(),
        "the increment's binary is in the upper",
    );
    assert!(
        !upper.join("usr/bin/dpkg").exists(),
        "the base's files stay in the lower, not copied into the upper",
    );
    // The base's database is unchanged; the merged database dpkg copied up into
    // the upper records the increment installed.
    assert!(
        !std::fs::read_to_string(base.join("var/lib/dpkg/status"))
            .unwrap()
            .contains("Package: hello"),
        "the base's dpkg database is untouched",
    );
    assert!(
        std::fs::read_to_string(upper.join("var/lib/dpkg/status"))
            .unwrap()
            .contains("Package: hello"),
        "the increment's dpkg state landed in the upper",
    );

    // Build against the merged view: `hello` runs, proving the overlay root joins
    // the base's C library and the increment's binary into one view.
    let status = ferroday_cage::Cage::builder()
        .overlay_rootfs(&base, layer.path())
        .command("/usr/bin/hello")
        .build()
        .expect("the overlay build cage builds")
        .run()
        .expect("hello runs in the build root");
    assert!(
        status.success(),
        "hello runs against the merged base-plus-increment view",
    );

    // Dropping the layer discards the increment; the base stays runnable.
    drop(layer);
    assert!(!upper.exists(), "the layer's upper is removed on drop");
    assert!(
        base.join("usr/bin/dpkg").is_file(),
        "the base is left pristine and intact",
    );
}

/// The range-mapped layered build: the increment is configured under the same
/// subordinate map as its base, and disposal escalates through the map to remove
/// the subordinate-owned upper and the mode-`0` overlay work directory.
#[cfg(feature = "subid")]
#[test]
fn layered_build_under_a_range_map_disposes_of_a_subordinate_owned_upper() {
    if !network_enabled() {
        return;
    }
    let Some(_) = common::fixture_rootfs() else {
        return;
    };
    let scratch = common::scratch_dir("debian-layer-ranged");
    if let Some(blocker) = ferroday_cage::host::overlay_blocker(&scratch) {
        eprintln!("skipping: overlay-rooted cages are unavailable: {blocker}");
        return;
    }
    if ferroday_cage::host::range_map_blocker().is_some() {
        eprintln!("skipping: no delegate can establish a range map");
        return;
    }

    // The base and its layer share the subordinate identity map, as they must.
    let base = scratch.join("base");
    {
        let mut debian = Debian::builder("trixie")
            .identity_map(ferroday_cage::IdentityMap::Subordinate)
            .cache_dir(common::download_cache("debian"))
            .build()
            .expect("the builder validates");
        provision::ensure(&base, &mut debian).expect("the range-mapped base bootstrap runs");
    }

    let upper = scratch.join("upper");
    let layer = {
        let mut progress = |event: DebianEvent<'_>| {
            if let DebianEvent::CommandOutput { bytes, .. } = event {
                use std::io::Write;
                let _ = std::io::stderr().write_all(bytes);
            }
        };
        let mut debian = Debian::builder("trixie")
            .identity_map(ferroday_cage::IdentityMap::Subordinate)
            .base_layer(&base)
            .include(["hello"])
            .cache_dir(common::download_cache("debian"))
            .build()
            .expect("the builder validates");
        debian
            .observe(&mut progress)
            .stage_layer(&upper)
            .expect("the range-mapped increment stages")
    };

    // The increment configured under the map: hello's binary is in the upper,
    // owned by a real mapped id rather than the flattened caller id.
    assert!(
        upper.join("usr/bin/hello").is_file(),
        "the increment is in the upper"
    );

    // The build runs under the same map, seeing the merged view.
    let status = ferroday_cage::Cage::builder()
        .overlay_rootfs(&base, layer.path())
        .identity_map(ferroday_cage::IdentityMap::Subordinate)
        .command("/usr/bin/hello")
        .build()
        .expect("the overlay build cage builds")
        .run()
        .expect("hello runs in the range-mapped build root");
    assert!(
        status.success(),
        "hello runs in the range-mapped build root"
    );

    // Disposal escalates through the map: the subordinate-owned upper and the
    // mode-`0` overlay work directory beside it are both removed, where a plain
    // caller-side delete could touch neither.
    drop(layer);
    assert!(
        !upper.exists(),
        "the subordinate-owned upper is removed on drop through the identity map",
    );
    assert!(base.join("usr/bin/dpkg").is_file(), "the base is intact");
}

/// The range-mapped bootstrap: real ownership, no reconciliation artifacts,
/// and a rootfs that needs — and gets — the map-assisted removal.
#[cfg(feature = "subid")]
#[test]
fn full_bootstrap_under_a_range_map_carries_real_ownership() {
    if !network_enabled() {
        return;
    }
    let Some(_) = common::fixture_rootfs() else {
        return;
    };
    if ferroday_cage::host::range_map_blocker().is_some() {
        eprintln!("skipping: no delegate can establish a range map");
        return;
    }
    let scratch = common::scratch_dir("debian-full-ranged");
    let rootfs = scratch.join("rootfs");
    {
        let mut progress = |event: DebianEvent<'_>| {
            if let DebianEvent::CommandOutput { bytes, .. } = event {
                use std::io::Write;
                let _ = std::io::stderr().write_all(bytes);
            }
        };
        let mut debian = Debian::builder("trixie")
            .identity_map(ferroday_cage::IdentityMap::Subordinate)
            .cache_dir(common::download_cache("debian"))
            .build()
            .expect("the builder validates");
        provision::ensure(&rootfs, &mut debian.observe(&mut progress))
            .expect("the range-mapped bootstrap runs");
    }

    // No single-identity reconciliation happened: the statoverride seed is
    // absent, and base-passwd's shadow helper carries its real non-root
    // group — the base gid mapped through the subordinate allocation —
    // rather than the flattened root.
    assert!(
        !rootfs.join("var/lib/dpkg/statoverride").exists(),
        "a range-mapped bootstrap seeds no statoverride"
    );
    let meta = std::fs::metadata(rootfs.join("usr/sbin/unix_chkpwd"))
        .expect("the shadow helper is laid out");
    use std::os::unix::fs::MetadataExt;
    // Flattened ownership would leave the file with the caller's own gid;
    // the real shadow gid lands inside the subordinate allocation instead.
    assert_ne!(
        meta.gid(),
        rustix::process::getegid().as_raw(),
        "the shadow helper's group is a real mapped id, not the flattened caller gid",
    );

    // The finished rootfs is runnable under the same map.
    let status = ferroday_cage::Cage::builder()
        .rootfs(&rootfs)
        .command("/usr/bin/dpkg")
        .args(["--list"])
        .identity_map(ferroday_cage::IdentityMap::Subordinate)
        .build()
        .expect("the cage builds")
        .run()
        .expect("dpkg runs in the finished rootfs");
    assert!(status.success());

    // Real ownership can put parts of a tree beyond the plain caller — a
    // directory owned by a mapped non-root id refuses the unlinks inside
    // it. The base system's directories are root-owned, so this tree may
    // remove plainly; `provision::remove` covers either case, and the
    // escalated path itself is exercised by the identity tests.
    provision::remove(&rootfs).expect("the removal succeeds");
    assert!(!rootfs.exists());
}

/// A payload for a fixture package: one file under `/usr/bin` named for it.
fn payload(name: &str) -> Vec<u8> {
    deb(&Tar::new()
        .dir("./usr", 0o755)
        .dir("./usr/bin", 0o755)
        .file(&format!("./usr/bin/{name}"), 0o755, b"#!/bin/true\n")
        .finish())
}

#[test]
fn available_reports_the_names_the_archive_carries() {
    // The question a resolve cannot answer. A top-level include naming nothing
    // fails the whole resolve, so asking that way tells a caller whether every
    // name was there and never which were not.
    let dir = common::scratch_dir("debian-available");
    let mirror = write_repo(
        &dir.join("repo"),
        "trixie",
        "amd64",
        &[
            Pkg::required("base", payload("base"), ""),
            Pkg::ordinary("libdep", payload("libdep")),
        ],
    );

    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .build()
        .expect("the builder validates");
    let available = debian.available().expect("the index is read");

    assert!(available.contains("base"));
    assert!(available.contains("libdep"));
    assert!(!available.contains("not-in-this-archive"));
    // A real package is not a provider of itself.
    assert_eq!(available.providers("libdep").count(), 0);
}

#[test]
fn available_reports_a_virtual_name_nothing_is_named_for() {
    // The case that is the reason for the whole query: `awk` is no package's
    // name, so a resolve's closure can only say something satisfied it, and a
    // caller reporting on a package list wants to know the name resolves and
    // what satisfies it.
    let dir = common::scratch_dir("debian-available-virtual");
    let mirror = write_repo(
        &dir.join("repo"),
        "trixie",
        "amd64",
        &[
            Pkg::required("base", payload("base"), ""),
            Pkg::ordinary("mawk", payload("mawk")).providing("awk"),
            Pkg::ordinary("gawk", payload("gawk")).providing("awk"),
        ],
    );

    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .build()
        .expect("the builder validates");
    let available = debian.available().expect("the index is read");

    assert!(available.contains("awk"), "the virtual name resolves");
    let providers: Vec<&str> = available.providers("awk").collect();
    assert_eq!(
        providers,
        ["gawk", "mawk"],
        "sorted, so the order is stable"
    );
}

#[test]
fn available_merges_every_configured_repository() {
    let dir = common::scratch_dir("debian-available-merge");
    let primary = write_repo(
        &dir.join("primary"),
        "trixie",
        "amd64",
        &[Pkg::required("base", payload("base"), "")],
    );
    let extra = write_repo(
        &dir.join("extra"),
        "trixie",
        "amd64",
        &[Pkg::ordinary("only-here", payload("only-here"))],
    );

    let extra_repo = Repository::builder("trixie")
        .mirror(extra)
        .trust_unsigned(true)
        .name("extra")
        .build()
        .expect("the extra repository validates");
    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(primary)
        .trust_unsigned(true)
        .repository(extra_repo)
        .build()
        .expect("the builder validates");
    let available = debian.available().expect("the indexes are read and merged");

    assert!(available.contains("base"), "the primary's package");
    assert!(available.contains("only-here"), "the second's package");
}

#[test]
fn available_applies_the_architecture_filter() {
    // The query answers availability for the architecture that was asked for.
    // A package published only for another one cannot be installed here, so
    // reporting it as available would be a wrong answer rather than a generous
    // one; an `all` package is installable everywhere and is reported.
    let dir = common::scratch_dir("debian-available-arch");
    let mirror = write_repo(
        &dir.join("repo"),
        "trixie",
        "amd64",
        &[
            Pkg::required("base", payload("base"), ""),
            Pkg::ordinary("elsewhere", payload("elsewhere")).for_architecture("riscv64"),
            Pkg::ordinary("everywhere", payload("everywhere")).for_architecture("all"),
        ],
    );

    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .build()
        .expect("the builder validates");
    let available = debian.available().expect("the index is read");

    assert!(available.contains("base"));
    assert!(
        available.contains("everywhere"),
        "an `all` package installs here"
    );
    assert!(
        !available.contains("elsewhere"),
        "another architecture's does not"
    );
}

#[test]
fn available_and_resolve_agree_on_one_index() {
    // The two read the same merged index through the same path, so every name
    // a resolve selects has to be a name the query reports. Pinning it keeps
    // the projection from drifting away from what the resolver sees.
    let dir = common::scratch_dir("debian-available-agrees");
    let mirror = write_repo(
        &dir.join("repo"),
        "trixie",
        "amd64",
        &[
            Pkg::required("base", payload("base"), "libdep"),
            Pkg::ordinary("libdep", payload("libdep")),
            Pkg::ordinary("tool", payload("tool")).depending_on("libdep"),
        ],
    );

    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror.clone())
        .trust_unsigned(true)
        .include(["tool"])
        .build()
        .expect("the builder validates");
    let plan = debian.resolve().expect("the plan resolves");

    // A fresh builder, since `available` ignores the include set rather than
    // consuming it, and reading through the same one would prove less.
    let mut query = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .build()
        .expect("the builder validates");
    let available = query.available().expect("the index is read");

    assert!(
        !plan.packages.is_empty(),
        "the fixture resolves to something"
    );
    for package in &plan.packages {
        assert!(
            available.contains(&package.name),
            "{} is in the plan but not reported available",
            package.name,
        );
    }
}

#[test]
fn a_plan_records_the_archive_state_it_resolved_against() {
    // A plan naming a package set says what was selected and not what it was
    // selected from, and the same suite resolves to different versions a week
    // apart. The archive record is what makes the plan a description of a
    // moment in an archive's life.
    let dir = common::scratch_dir("debian-plan-archives");
    let repo = dir.join("repo");
    let mirror = write_repo(
        &repo,
        "trixie",
        "amd64",
        &[
            Pkg::required("base", payload("base"), "libdep"),
            Pkg::ordinary("libdep", payload("libdep")),
        ],
    );

    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror.clone())
        .trust_unsigned(true)
        .build()
        .expect("the builder validates");
    let plan = debian.resolve().expect("the plan resolves");

    assert_eq!(plan.archives.len(), 1);
    let archive = &plan.archives[0];
    assert_eq!(archive.mirror, mirror);
    assert_eq!(archive.suite, "trixie");
    assert_eq!(archive.components, ["main"]);
    // The digest is of the release body that was read, which for an unsigned
    // repository is the Release file itself.
    let release = std::fs::read(repo.join("dists/trixie/Release")).unwrap();
    assert_eq!(
        archive.release_sha256,
        common::deb_repo::sha256_hex(&release)
    );
    // Nothing verified it, so no key is named. An empty list is the record of
    // that, not an omission.
    assert!(archive.signed_by.is_empty(), "{:?}", archive.signed_by);
    // Every package resolved from the only archive there is.
    assert!(plan.packages.iter().all(|package| package.archive == 0));
}

#[test]
fn a_plan_records_the_mirror_that_served_rather_than_the_one_configured_first() {
    // A repository with a snapshot backstop resolves against whichever URL
    // answered. Recording the configured list would describe a choice rather
    // than the choice made, and a caller replaying the plan would go to a
    // mirror that never served it.
    let dir = common::scratch_dir("debian-plan-backstop");
    let backstop = write_repo(
        &dir.join("backstop"),
        "trixie",
        "amd64",
        &[Pkg::required("base", payload("base"), "")],
    );
    let missing = format!("file://{}", dir.join("does-not-exist").display());

    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(missing.clone())
        .mirror_fallback(backstop.clone())
        .trust_unsigned(true)
        .build()
        .expect("the builder validates");
    let plan = debian.resolve().expect("the backstop serves the resolve");

    assert_eq!(plan.archives.len(), 1);
    assert_eq!(plan.archives[0].mirror, backstop);
    assert_ne!(plan.archives[0].mirror, missing);
}

#[test]
fn a_plan_indexes_each_package_to_the_archive_its_version_came_from() {
    // Resolution is highest-version-wins across the merged repositories, so
    // with more than one configured the archive index is the only thing that
    // says where a package will actually be fetched from.
    let dir = common::scratch_dir("debian-plan-multirepo");
    let primary = write_repo(
        &dir.join("primary"),
        "trixie",
        "amd64",
        &[Pkg::required("base", payload("base"), "")],
    );
    let feature = write_repo(
        &dir.join("feature"),
        "trixie",
        "amd64",
        &[Pkg::ordinary("custom", payload("custom"))],
    );
    let feature_repo = Repository::builder("trixie")
        .mirror(feature.clone())
        .trust_unsigned(true)
        .name("feature")
        .build()
        .expect("the feature repository validates");

    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(primary.clone())
        .trust_unsigned(true)
        .include(["custom"])
        .repository(feature_repo)
        .build()
        .expect("the builder validates");
    let plan = debian.resolve().expect("the plan resolves");

    assert_eq!(plan.archives.len(), 2, "one record per repository");
    assert_eq!(plan.archives[0].mirror, primary, "the primary leads");
    assert_eq!(plan.archives[1].mirror, feature);

    let index_of = |name: &str| {
        plan.packages
            .iter()
            .find(|package| package.name == name)
            .unwrap_or_else(|| panic!("{name} is in the plan"))
            .archive
    };
    assert_eq!(index_of("base"), 0, "the primary's package");
    assert_eq!(index_of("custom"), 1, "the second repository's package");
    // And the index names a real archive, so a caller can follow it to a URL.
    assert_eq!(plan.archives[index_of("custom")].mirror, feature);
}

/// A fetcher that records every URL it is asked for and otherwise reads from
/// the `file://` mirrors the hermetic fixtures write.
///
/// The `.deb` fetch has to work for a pinned install to finish, so this wraps
/// the ordinary transport rather than replacing it: the assertion is about what
/// was *not* asked for.
#[derive(Default)]
struct Watching {
    asked: std::sync::Arc<std::sync::Mutex<Vec<String>>>,
}

impl ferroday_cage::provision::Fetch for Watching {
    fn fetch(
        &mut self,
        request: &ferroday_cage::provision::FetchRequest<'_>,
        sink: &mut dyn std::io::Write,
    ) -> Result<(), ferroday_cage::provision::FetchError> {
        self.asked
            .lock()
            .expect("the URL log is not poisoned")
            .push(request.url().to_string());
        ferroday_cage::provision::HttpFetch::new().fetch(request, sink)
    }
}

/// Builds a two-package hermetic repository and returns its scratch directory,
/// its mirror URL, and the plan a resolve against it produces.
fn resolved_fixture(name: &str) -> (std::path::PathBuf, String, Plan) {
    let dir = common::scratch_dir(name);
    let mirror = write_repo(
        &dir.join("repo"),
        "trixie",
        "amd64",
        &[
            Pkg::required("base", payload("base"), "libdep"),
            Pkg::ordinary("libdep", payload("libdep")),
        ],
    );
    let plan = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror.clone())
        .trust_unsigned(true)
        .build()
        .expect("the builder validates")
        .resolve()
        .expect("the plan resolves");
    (dir, mirror, plan)
}

#[test]
fn a_pinned_plan_installs_the_tree_the_resolve_that_made_it_would_have() {
    // The primitive a reproduce mode is missing: resolve once, install from
    // that, and get what the inline resolution would have laid down.
    let (dir, mirror, plan) = resolved_fixture("debian-pinned-same-tree");

    let inline_rootfs = dir.join("inline");
    let mut inline = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror.clone())
        .trust_unsigned(true)
        .cache_dir(dir.join("cache"))
        .extract_only(true)
        .build()
        .expect("the builder validates");
    provision::ensure(&inline_rootfs, &mut inline).expect("the inline bootstrap runs");

    let pinned_rootfs = dir.join("pinned");
    let mut pinned = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .cache_dir(dir.join("cache"))
        .extract_only(true)
        .plan(plan)
        .build()
        .expect("the pinned builder validates");
    provision::ensure(&pinned_rootfs, &mut pinned).expect("the pinned bootstrap runs");

    for entry in ["usr/bin/base", "usr/bin/libdep"] {
        assert!(
            pinned_rootfs.join(entry).is_file(),
            "{entry} is missing from the pinned tree",
        );
        assert_eq!(
            std::fs::read(inline_rootfs.join(entry)).unwrap(),
            std::fs::read(pinned_rootfs.join(entry)).unwrap(),
            "{entry} differs between the two trees",
        );
    }
}

#[test]
fn a_plan_kept_as_a_document_installs_what_the_plan_it_came_from_would_have() {
    // The reproduce case the same-process handoff does not cover: resolve on
    // one machine, keep the plan on disk, and replay it later. The document is
    // the whole interchange — nothing else travels — so a plan that survives it
    // must install byte for byte what the plan it was written from would have.
    let (dir, mirror, plan) = resolved_fixture("debian-plan-document");

    let kept = dir.join("trixie.plan");
    std::fs::write(&kept, plan.to_document().expect("the plan renders")).unwrap();
    let read = ferroday_cage::provision::debian::Plan::parse_document(
        &std::fs::read_to_string(&kept).unwrap(),
    )
    .expect("the kept document reads");
    assert_eq!(read, plan, "the document did not carry the plan intact");

    let inline_rootfs = dir.join("inline");
    let mut inline = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror.clone())
        .trust_unsigned(true)
        .cache_dir(dir.join("cache"))
        .extract_only(true)
        .plan(plan)
        .build()
        .expect("the builder validates");
    provision::ensure(&inline_rootfs, &mut inline).expect("the in-process plan installs");

    let replayed_rootfs = dir.join("replayed");
    let mut replayed = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .cache_dir(dir.join("cache"))
        .extract_only(true)
        .plan(read)
        .build()
        .expect("the replayed builder validates");
    provision::ensure(&replayed_rootfs, &mut replayed).expect("the kept plan installs");

    for entry in ["usr/bin/base", "usr/bin/libdep"] {
        assert_eq!(
            std::fs::read(inline_rootfs.join(entry)).unwrap(),
            std::fs::read(replayed_rootfs.join(entry)).unwrap(),
            "{entry} differs between the in-process plan and the kept one",
        );
    }
}

/// The guide prints a sample plan document. It is only worth printing if it is
/// what the library actually emits, so it is parsed and re-rendered here rather
/// than trusted: a change to the format that nobody carried into the guide
/// leaves a sample that reads plausibly and is wrong.
///
/// A locked-down sample only covers the cases it contains, so the sample is
/// itself checked for the ones this test exists to catch. The rendering of an
/// empty field is the first of them: `Signed-By: ` with a trailing space was
/// the defect that made this a test rather than a one-off check, and a sample
/// whose every archive is signed would have rendered identically before and
/// after that fix.
#[test]
fn the_guides_sample_document_is_what_the_library_emits() {
    const GUIDE: &str = include_str!("../../../docs/src/debian.md");

    let start = GUIDE
        .find("### Keeping a plan")
        .expect("the guide still has the plan-document section");
    let block = GUIDE[start..]
        .split_once("```text\n")
        .expect("the section still prints a sample")
        .1
        .split_once("```")
        .expect("the sample is fenced")
        .0;

    // An empty field, so the bare-colon rendering is exercised rather than
    // only described in the prose beside it.
    assert!(
        block.contains("\nSigned-By:\n"),
        "the sample has no unsigned archive, so it does not cover the empty-field \
         rendering this test was written for:\n{block}",
    );
    // And an archive a package actually refers to by index, so the sample
    // covers a plan with more than one repository.
    assert!(block.contains("\nArchive: 1\n"), "{block}");

    let plan = ferroday_cage::provision::debian::Plan::parse_document(block)
        .expect("the guide's sample reads");
    let rendered = plan.to_document().expect("and renders");
    // Every stanza ends in a blank line, so a document ends in two newlines. A
    // fenced block cannot show that last blank line, so it is the one
    // difference the comparison allows.
    assert_eq!(
        rendered.trim_end_matches('\n'),
        block.trim_end_matches('\n'),
        "the guide's sample is not what the library emits",
    );
    assert!(rendered.ends_with("\n\n"), "{rendered:?}");
}

#[test]
fn a_configured_plan_is_what_resolve_answers_with() {
    // `resolve` and `ensure` are documented as one code path, and the plan is
    // recommended as a build-cache key. Resolving afresh on a provisioner that
    // already carries a plan answered with a whole fresh closure while `ensure`
    // on the same value installed the plan's, so the key described a rootfs
    // nothing built.
    let (dir, mirror, plan) = resolved_fixture("debian-plan-resolve-agrees");
    let mut trimmed = plan.clone();
    trimmed.packages.truncate(1);

    let answered = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .cache_dir(dir.join("cache"))
        .extract_only(true)
        .plan(trimmed.clone())
        .build()
        .expect("the builder validates")
        .resolve()
        .expect("a configured plan needs no archive to answer");
    assert_eq!(
        answered
            .packages
            .iter()
            .map(|package| package.name.clone())
            .collect::<Vec<_>>(),
        trimmed
            .packages
            .iter()
            .map(|package| package.name.clone())
            .collect::<Vec<_>>(),
        "resolve answered with a closure the same value would not install",
    );
}

#[test]
fn a_pinned_install_fetches_no_release_and_no_index() {
    // The whole win, asserted through the fetcher seam: skipping the release
    // and the index is what removes a 9 MB download from a reproduce run, and
    // it is checkable without a network.
    let (dir, mirror, plan) = resolved_fixture("debian-pinned-no-index");

    let watcher = Watching::default();
    let asked = std::sync::Arc::clone(&watcher.asked);
    let mut pinned = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .cache_dir(dir.join("cache"))
        .extract_only(true)
        .plan(plan)
        .fetcher(Box::new(watcher))
        .build()
        .expect("the pinned builder validates");
    provision::ensure(dir.join("rootfs"), &mut pinned).expect("the pinned bootstrap runs");

    let urls = asked.lock().unwrap().clone();
    assert!(!urls.is_empty(), "the packages were still fetched");
    for url in &urls {
        assert!(
            !url.contains("/dists/"),
            "a pinned install touched archive metadata: {url}",
        );
        assert!(url.contains("/pool/"), "only packages are fetched: {url}");
    }
}

#[test]
fn a_pinned_install_survives_an_archive_that_has_moved_on() {
    // The point of pinning. The repository republishes with a newer version
    // and without the one the plan names; the plan still installs its own
    // versions, because it never consults the index that no longer offers them.
    let dir = common::scratch_dir("debian-pinned-moved-on");
    let repo = dir.join("repo");
    let mirror = write_repo(
        &repo,
        "trixie",
        "amd64",
        &[Pkg::required("base", payload("base"), "")],
    );
    let plan = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror.clone())
        .trust_unsigned(true)
        .build()
        .expect("the builder validates")
        .resolve()
        .expect("the plan resolves");
    let pinned_version = plan.packages[0].version.clone();

    // The archive publishes on: a new version, and the index no longer names
    // the old one. The pool file the plan points at is left in place, as a real
    // archive leaves a superseded .deb until it is culled.
    let mut newer = Pkg::required("base", payload("base-2"), "");
    newer.version = "2.0".to_string();
    write_repo(&repo, "trixie", "amd64", &[newer]);

    let rootfs = dir.join("rootfs");
    let mut pinned = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .cache_dir(dir.join("cache"))
        .extract_only(true)
        .plan(plan)
        .build()
        .expect("the pinned builder validates");
    provision::ensure(&rootfs, &mut pinned).expect("the pinned bootstrap runs");

    assert_eq!(pinned_version, "1.0");
    assert!(rootfs.join("usr/bin/base").is_file());
}

#[test]
fn a_pinned_install_whose_package_is_gone_fails_naming_the_package() {
    // The failure a caller has to be able to read: the plan is intact and the
    // archive no longer holds what it names, which is a different problem from
    // a resolution that found nothing.
    let (dir, mirror, plan) = resolved_fixture("debian-pinned-missing-deb");
    let gone = plan.packages[0].filename.clone();
    std::fs::remove_file(dir.join("repo").join(&gone)).expect("the pool file is removable");

    let mut pinned = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .cache_dir(dir.join("cache"))
        .extract_only(true)
        .plan(plan)
        .build()
        .expect("the pinned builder validates");
    let err = provision::ensure(dir.join("rootfs"), &mut pinned).unwrap_err();
    let ProvisionError::Other { source: inner, .. } = &err else {
        panic!("expected a boxed Debian error, got {err:?}");
    };
    let message = inner.to_string();
    assert!(
        message.contains(&gone),
        "the report names the package: {message}"
    );
}

#[test]
fn a_pinned_install_refuses_a_digest_mismatch_without_trying_elsewhere() {
    // The plan is the trust anchor, so the digest it records is the whole of
    // what a fetched .deb is held to. A mismatch means the bytes are not the
    // ones the plan was made from; another mirror cannot make that true.
    let (dir, mirror, plan) = resolved_fixture("debian-pinned-mismatch");
    let pool_deb = dir.join("repo").join(&plan.packages[0].filename);
    let mut bytes = std::fs::read(&pool_deb).unwrap();
    bytes.extend_from_slice(b"tampered");
    std::fs::write(&pool_deb, &bytes).unwrap();

    let mut pinned = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        // A backstop that would serve, so this proves the failure is terminal
        // rather than merely unrecoverable for want of an alternative.
        .mirror_fallback(format!("file://{}", dir.join("repo").display()))
        .trust_unsigned(true)
        .cache_dir(dir.join("cache"))
        .extract_only(true)
        .plan(plan)
        .build()
        .expect("the pinned builder validates");
    let err = provision::ensure(dir.join("rootfs"), &mut pinned).unwrap_err();
    let ProvisionError::Other { source: inner, .. } = &err else {
        panic!("expected a boxed Debian error, got {err:?}");
    };
    assert!(
        inner.to_string().contains("digest"),
        "a digest mismatch is reported: {inner}",
    );
}

#[test]
fn a_plan_refuses_every_setting_that_would_shape_a_resolution() {
    // Each refusal names what conflicts, rather than the build picking a silent
    // precedence between a plan and a setting that contradicts it.
    let (_dir, mirror, plan) = resolved_fixture("debian-pinned-conflicts");

    let base = |plan: Plan| {
        Debian::builder("trixie")
            .architecture("amd64")
            .mirror(mirror.clone())
            .trust_unsigned(true)
            .plan(plan)
    };
    let refusal =
        |builder: ferroday_cage::provision::debian::DebianBuilder<'_>| match builder.build() {
            Err(ferroday_cage::provision::debian::DebianError::Config { reason, .. }) => reason,
            other => panic!("expected a configuration refusal, got {other:?}"),
        };

    assert!(refusal(base(plan.clone()).include(["git"])).contains("include()"));
    assert!(refusal(base(plan.clone()).exclude(["git"])).contains("exclude()"));
    assert!(
        refusal(
            base(plan.clone()).base_priority(ferroday_cage::provision::debian::Priority::Important)
        )
        .contains("base_priority()"),
    );

    // A plan for another suite or architecture describes a different tree.
    let mut elsewhere = plan.clone();
    elsewhere.suite = "bookworm".to_string();
    assert!(refusal(base(elsewhere)).contains("bookworm"));
    let mut foreign = plan.clone();
    foreign.architecture = "riscv64".to_string();
    assert!(refusal(base(foreign)).contains("riscv64"));

    // And a plan resolved against more archives than there are repositories has
    // packages naming a mirror that is not configured.
    let mut wider = plan.clone();
    let extra = wider.archives[0].clone();
    wider.archives.push(extra);
    assert!(refusal(base(wider)).contains("2 archives"));

    // The archive count bounds a plan whose packages agree with its own archive
    // list, which is every plan a document produced. `Plan` is public and
    // `Clone`, though, so a caller can take one from `resolve()` and edit a
    // package's archive index past the end — and the index is what a bootstrap
    // slices the repository list with, so an unchecked one is a panic partway
    // through a download rather than a refusal at build time.
    let mut edited = plan.clone();
    edited.packages[0].archive = 7;
    let reason = refusal(base(edited));
    assert!(reason.contains("archive 7"), "{reason}");

    // Same route, and the fields that become a URL rather than an index. The
    // pool path is interpolated verbatim into the fetch URL, so an edited one
    // could ask a mirror for something above its root; the digest is compared
    // with a plain `==`, so one spelled any other way could only ever mismatch.
    let mut traversing = plan.clone();
    traversing.packages[0].filename = "../../../etc/passwd".to_string();
    let reason = refusal(base(traversing));
    assert!(reason.contains("asked for"), "{reason}");

    let mut shouted = plan;
    shouted.packages[0].sha256 = shouted.packages[0].sha256.to_uppercase();
    let reason = refusal(base(shouted));
    assert!(reason.contains("lowercase hex"), "{reason}");
}

/// A package at an explicit version, which [`Pkg::ordinary`] fixes at `1.0`.
fn at_version(name: &str, version: &str, deb: Vec<u8>) -> Pkg {
    Pkg {
        version: version.to_string(),
        ..Pkg::ordinary(name, deb)
    }
}

/// A payload at a fixed path carrying the given bytes, so two builds of one
/// package are told apart by what they installed rather than by where.
fn payload_holding(contents: &[u8]) -> Vec<u8> {
    deb(&Tar::new()
        .dir("./usr", 0o755)
        .dir("./usr/bin", 0o755)
        .file("./usr/bin/x", 0o755, contents)
        .finish())
}

#[test]
fn hermetic_a_pin_selects_its_version_where_the_archive_offers_a_higher_one() {
    // What a pin is for: the archive has published since the plan was taken and
    // still offers both versions, as a snapshot mirror does. The unpinned
    // resolution takes the newer one and the pinned resolution does not, from
    // the same archive in the same test.
    let dir = common::scratch_dir("debian-pin-selects");
    let repo = dir.join("repo");
    let old = payload_holding(b"one\n");
    let new = payload_holding(b"two\n");
    let mirror = write_repo(
        &repo,
        "trixie",
        "amd64",
        &[
            Pkg {
                priority: "required".to_string(),
                ..at_version("base", "1.0", old.clone())
            },
            Pkg {
                priority: "required".to_string(),
                ..at_version("base", "2.0", new)
            },
        ],
    );

    let build = || {
        Debian::builder("trixie")
            .architecture("amd64")
            .mirror(mirror.clone())
            .trust_unsigned(true)
            .cache_dir(dir.join("cache"))
            .extract_only(true)
    };

    // Unpinned, the archive's newest wins.
    let live = build().build().expect("the builder validates");
    let live = live_resolve(live);
    assert_eq!(live.packages[0].version, "2.0");

    // Pinned to the older one, the resolution holds there — and reports the
    // digest of the version it held to, not the one it passed over.
    let mut pin = live.clone();
    pin.packages[0].version = "1.0".to_string();
    pin.packages[0].sha256 = common::deb_repo::sha256_hex(&old);
    pin.packages[0].filename = "pool/base_1.0_amd64.deb".to_string();

    let mut debian = build().pin(pin).build().expect("the pin validates");
    let held = debian.resolve().expect("the pinned version is offered");
    assert_eq!(held.packages[0].version, "1.0");
    assert_eq!(held.packages[0].sha256, common::deb_repo::sha256_hex(&old));

    // And the bootstrap installs what the pin held to, not what the archive
    // would have chosen.
    let rootfs = dir.join("rootfs");
    assert_eq!(
        provision::ensure(&rootfs, &mut debian).expect("the pinned bootstrap runs"),
        Provisioned::Created,
    );
    assert_eq!(std::fs::read(rootfs.join("usr/bin/x")).unwrap(), b"one\n");
}

/// Resolves a bootstrap, taking it by value so a builder chain reads in one
/// expression.
fn live_resolve(mut debian: Debian<'_>) -> Plan {
    debian.resolve().expect("the resolution runs")
}

#[test]
fn hermetic_a_pin_the_archive_has_moved_past_names_what_it_offers() {
    // The archive replaced the version rather than keeping both, which is what
    // an ordinary suite does. The pin cannot be held, and the refusal says what
    // is there instead rather than only that something is missing.
    let dir = common::scratch_dir("debian-pin-moved");
    let repo = dir.join("repo");
    let mirror = write_repo(
        &repo,
        "trixie",
        "amd64",
        &[Pkg {
            priority: "required".to_string(),
            ..at_version("base", "1.0", payload_holding(b"one\n"))
        }],
    );
    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror.clone())
        .trust_unsigned(true)
        .build()
        .expect("the builder validates");
    let pin = debian.resolve().expect("the first resolution runs");

    // The archive publishes 2.0 over 1.0, and drops the package `gone`
    // entirely — the two failures a pin distinguishes.
    write_repo(
        &repo,
        "trixie",
        "amd64",
        &[Pkg {
            priority: "required".to_string(),
            ..at_version("base", "2.0", payload_holding(b"two\n"))
        }],
    );

    let mut dropped = pin.clone();
    let mut gone = dropped.packages[0].clone();
    gone.name = "gone".to_string();
    dropped.packages.push(gone);

    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .pin(dropped)
        .build()
        .expect("the pin validates against the configuration");

    let refusal = debian.resolve().expect_err("the pin cannot be held");
    let rendered = refusal.to_string();
    assert!(rendered.contains("base is pinned to 1.0"), "{rendered}");
    assert!(rendered.contains("offer 2.0"), "{rendered}");
    assert!(
        rendered.contains("gone is pinned to 1.0 and the archives offer no version of it"),
        "{rendered}",
    );
    // Reported in name order, so a diff across two archive publishes is stable.
    assert!(
        rendered.find("base is pinned").unwrap() < rendered.find("gone is pinned").unwrap(),
        "{rendered}",
    );
}

#[test]
fn hermetic_a_pin_holds_the_archive_half_and_lets_a_local_build_float() {
    // The shape a pin exists for. The archive half of a build is fixed by a
    // committed document; the half the build compiles for itself is published
    // to a local repository and pinned by nothing, because a compile that is
    // not byte-reproducible produces a different digest each time. Handing the
    // whole plan to plan() refuses that build; pinning only the archive half
    // resolves it.
    let dir = common::scratch_dir("debian-pin-local");
    let archive = write_repo(
        &dir.join("archive"),
        "trixie",
        "amd64",
        &[Pkg::required("base", payload_holding(b"base\n"), "")],
    );
    let local = dir.join("local");
    let built = write_repo(
        &local,
        "trixie",
        "amd64",
        &[at_version(
            "kernel",
            "6.12",
            payload_holding(b"first compile\n"),
        )],
    );

    let local_repo = Repository::builder("trixie")
        .mirror(built.clone())
        .trust_unsigned(true)
        .name("local")
        .build()
        .expect("the local repository validates");
    // A plan names every package to install, so the replay below composes with
    // no include(); the selecting builds carry one and the replay does not.
    let base = |repo: Repository| {
        Debian::builder("trixie")
            .architecture("amd64")
            .mirror(archive.clone())
            .trust_unsigned(true)
            .repository(repo)
            .cache_dir(dir.join("cache"))
            .extract_only(true)
    };
    let build = |repo: Repository| base(repo).include(["kernel"]);

    let mut debian = build(local_repo.clone())
        .build()
        .expect("the builder validates");
    let plan = debian.resolve().expect("the first resolution runs");
    assert_eq!(plan.packages.len(), 2, "{plan:?}");

    // The kernel is compiled again and differs in a byte, as a build that is
    // not byte-reproducible does.
    write_repo(
        &local,
        "trixie",
        "amd64",
        &[at_version(
            "kernel",
            "6.12",
            payload_holding(b"second compile\n"),
        )],
    );

    // The whole plan replayed verbatim fetches by the digest of the first
    // compile, which nothing now serves.
    let mut verbatim = base(local_repo.clone())
        .plan(plan.clone())
        .build()
        .expect("the plan validates against the configuration");
    let rootfs = dir.join("replayed");
    let refusal = provision::ensure(&rootfs, &mut verbatim)
        .expect_err("the recompiled kernel does not match the recorded digest");
    assert!(refusal.to_string().contains("recorded digest"), "{refusal}",);

    // The same plan with the locally built half dropped holds the archive and
    // resolves the kernel at whatever the local repository now serves.
    let mut pin = plan;
    pin.packages.retain(|package| package.name != "kernel");
    let mut debian = build(local_repo)
        .pin(pin)
        .build()
        .expect("the pin validates against the configuration");
    let rootfs = dir.join("pinned");
    assert_eq!(
        provision::ensure(&rootfs, &mut debian).expect("the pinned bootstrap runs"),
        Provisioned::Created,
    );
    assert_eq!(
        std::fs::read(rootfs.join("usr/bin/x")).unwrap(),
        b"second compile\n",
    );
}

#[test]
fn hermetic_one_version_published_twice_over_different_bytes_is_refused() {
    // The version matches and the bytes do not, which is the event a recorded
    // digest exists to catch. Nothing else in the resolution notices: the
    // archive's own index is internally consistent and its release signs it.
    let dir = common::scratch_dir("debian-pin-rebuilt");
    let repo = dir.join("repo");
    let mirror = write_repo(
        &repo,
        "trixie",
        "amd64",
        &[Pkg::required("base", payload_holding(b"one\n"), "")],
    );
    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror.clone())
        .trust_unsigned(true)
        .build()
        .expect("the builder validates");
    let pin = debian.resolve().expect("the first resolution runs");

    // Republished at the same version over different bytes.
    write_repo(
        &repo,
        "trixie",
        "amd64",
        &[Pkg::required("base", payload_holding(b"two\n"), "")],
    );

    let mut debian = Debian::builder("trixie")
        .architecture("amd64")
        .mirror(mirror)
        .trust_unsigned(true)
        .pin(pin)
        .build()
        .expect("the pin validates against the configuration");
    let refusal = debian
        .resolve()
        .expect_err("the archive records other bytes for the pinned version")
        .to_string();
    assert!(
        refusal.contains("base 1.0 is pinned to the digest"),
        "{refusal}"
    );
}

#[test]
fn hermetic_a_pin_composes_with_the_selection_a_plan_contradicts() {
    // The whole of what separates a pin from a plan: a plan names every package
    // to install, so include() has nothing to add and is refused; a pin names
    // the versions a selection resolves to and says nothing about what is
    // selected, so the same include() composes with it.
    let dir = common::scratch_dir("debian-pin-composes");
    let mirror = write_repo(
        &dir.join("repo"),
        "trixie",
        "amd64",
        &[
            Pkg::required("base", payload_holding(b"base\n"), ""),
            Pkg::ordinary("extra", payload_holding(b"extra\n")),
        ],
    );
    let build = || {
        Debian::builder("trixie")
            .architecture("amd64")
            .mirror(mirror.clone())
            .trust_unsigned(true)
    };
    let mut debian = build().build().expect("the builder validates");
    let plan = debian.resolve().expect("the base resolution runs");

    // The same plan, the same include, and opposite outcomes.
    let refusal = build()
        .include(["extra"])
        .plan(plan.clone())
        .build()
        .expect_err("a plan and an include contradict")
        .to_string();
    assert!(refusal.contains("include()"), "{refusal}");

    let mut debian = build()
        .include(["extra"])
        .pin(plan.clone())
        .build()
        .expect("a pin and an include compose");
    let resolved = debian.resolve().expect("the pinned resolution runs");
    let names: Vec<_> = resolved.packages.iter().map(|p| p.name.as_str()).collect();
    assert_eq!(names, ["base", "extra"]);

    // A plan and a pin at once are refused: a plan resolves nothing, so there
    // is no resolution left for a pin to hold.
    let refusal = build()
        .plan(plan.clone())
        .pin(plan.clone())
        .build()
        .expect_err("a plan and a pin contradict")
        .to_string();
    assert!(refusal.contains("drop one of the two"), "{refusal}");

    // A pin resolved for another coordinate is refused where it cannot mean
    // anything, exactly as a plan is.
    let mut foreign = plan;
    foreign.architecture = "riscv64".to_string();
    let refusal = build()
        .pin(foreign)
        .build()
        .expect_err("a pin for another architecture is refused")
        .to_string();
    assert!(refusal.contains("riscv64"), "{refusal}");
}