structured-xades 0.21.3

XAdES mechanisms over caller-owned XML and explicit resource accounting
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
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
//! Direct-incorporation structure tests; these do not assert cryptographic trust.

use structured_cms::timestamp::CheckContext;

#[path = "support/timestamp_oracle.rs"]
mod timestamp_oracle;

#[path = "support/token_profiles.rs"]
mod token_profiles;

#[path = "support/lifecycle.rs"]
mod lifecycle;

#[path = "support/independent_corpus.rs"]
pub mod independent_corpus;

#[path = "support/baseline.rs"]
mod baseline_checks;
#[path = "support/credential.rs"]
mod credential_checks;
#[path = "support/xml_digest.rs"]
mod xml_digest;

#[path = "support/renewed_check.rs"]
mod renewed_check;

#[path = "support/hash_uses.rs"]
mod hash_checks;

#[path = "support/scalar.rs"]
mod scalar_checks;

#[path = "support/attributes.rs"]
mod attribute_checks;

#[path = "support/reference_fields.rs"]
mod reference_fields;

#[path = "support/digest_class.rs"]
mod digest_class;

#[path = "support/query_boundary.rs"]
mod query_boundary;

#[path = "support/reference_timestamps.rs"]
mod reference_timestamps;

use std::{
    cell::{Cell, RefCell},
    cmp::Ordering,
    sync::Arc,
};
use structured_asn1::digest::{Digest, Resolution as DigestResolution};
use structured_asn1::memory::{Memory, MemoryAccount, MemoryError, OwnedVec, Work};
use structured_cms::stream::{self, Receiver, Sink};
use structured_xades::{
    binary::{self, BinaryXml, Location},
    binding::{
        self, BindingError, Coverage, Reference, SIGNED_PROPERTIES, SignatureXml, Status, Target,
    },
    certificate, commitment,
    container::{self, ContainerError, DS, XADES},
    counter, format, header, identifier, material, policy, prepare, properties,
    reference::{
        Boolean, Completeness, Descriptor, LexicalSource, ObjectLocation, Parent, ReferenceXml,
        Replay, ReplayOutcome, Resolution, VisitError,
    },
    role,
    timestamp::{self, Input, InputError, Role, TimestampXml, XADES_141},
    unsigned, validation,
    xml::{Access, AccessError, Name, Node, Snapshot, Xml},
};

#[derive(Clone, Copy)]
struct Record {
    parent: Option<usize>,
    value: Node<'static>,
}

struct Edges {
    first: Vec<Option<usize>>,
    next: Vec<Option<usize>>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct KeyProjection {
    key: usize,
    parameters: Option<(usize, usize)>,
}

impl KeyProjection {
    fn of(key: structured_pkix::path::WorkingKey<'_>) -> Self {
        Self {
            key: key.key().as_bytes().as_ptr() as usize,
            parameters: key
                .parameters()
                .map(|bytes| (bytes.as_ptr() as usize, bytes.len())),
        }
    }
}

enum Decoding {
    Ready,
    Refuse,
}

struct Tree {
    document: u8,
    nodes: Vec<Record>,
    signature_status: Status,
    references: Vec<Reference<'static, usize>>,
    target: Target<usize>,
    target_routes: Vec<(usize, Target<usize>)>,
    generation: Cell<u8>,
    streamed: Cell<usize>,
    canonical_routes: Vec<(usize, Vec<u8>)>,
    reference_routes: Vec<(usize, Vec<u8>)>,
    ignore_refusal: bool,
    incomplete: bool,
    binary: Vec<u8>,
    binary_routes: Vec<(usize, Vec<u8>)>,
    text_routes: Vec<(usize, &'static str)>,
    calendar_routes: Vec<(usize, properties::CalendarFact<'static>)>,
    binary_depth: u32,
    binary_parser: RefCell<Option<structured_asn1::Parser>>,
    mutate_binary: bool,
    decoding: Decoding,
    binary_encoding: Option<&'static str>,
    key_association: certificate::KeyAssociation,
    key_projection: Cell<Option<KeyProjection>>,
    digest_algorithm: Option<&'static str>,
    digest_algorithms: Vec<(usize, &'static str)>,
    object_reference: Option<&'static str>,
    object_mime: Option<&'static str>,
    identifier_qualifier: Option<&'static str>,
    validation_uri: Option<&'static str>,
    counter_evidence: Option<CounterEvidence>,
    nested_counters: Vec<CounterEvidence>,
    indexed: Option<Edges>,
    include_routes: Vec<(usize, usize, Boolean)>,
    reference_completeness: Completeness<usize>,
    reference_locations: Vec<(usize, ObjectLocation)>,
    unavailable_reference: Option<usize>,
    decoded_pointer: Cell<usize>,
    cached_binary: RefCell<Option<OwnedVec<u8>>>,
    date_time: properties::DateTimeFact,
    date_time_calls: Cell<usize>,
    hash_uses: Vec<structured_xades::hashes::HashUse<usize>>,
    hash_selection: hash_checks::Selection,
    hash_completeness: structured_xades::hashes::HashCompleteness<usize>,
    hash_calls: Cell<usize>,
    hash_visits: Cell<usize>,
    hash_refusal: hash_checks::RefusalMutation,
    scalar_routes: Vec<scalar_checks::Route>,
    scalar_calls: Cell<usize>,
    uri_routes: Vec<scalar_checks::UriRoute>,
    attribute_routes: Vec<attribute_checks::Route>,
    qname_routes: Vec<attribute_checks::TypeRoute>,
    attribute_calls: Cell<usize>,
}

struct CounterEvidence {
    child: usize,
    status: Status,
    references: Vec<Reference<'static, usize>>,
}

#[derive(Debug)]
enum Cause {
    Source,
    Resource(MemoryError),
    Incomplete,
    Unavailable,
}

fn element(parent: Option<usize>, namespace: &'static str, local: &'static str) -> Record {
    Record {
        parent,
        value: Node::Element(Name { namespace, local }),
    }
}

impl Tree {
    fn element_child(&self, parent: usize, namespace: &str, local: &str) -> usize {
        self.nodes.iter().position(|record|record.parent==Some(parent)&&matches!(record.value,Node::Element(name) if name.namespace==namespace&&name.local==local)).expect("fixture child")
    }
    fn reference_location<W: Work>(&self, node: usize, work: &W) -> Result<ObjectLocation, Cause> {
        work.charge_work(self.reference_locations.len() as u64)
            .map_err(Cause::Resource)?;
        Ok(self
            .reference_locations
            .iter()
            .find(|entry| entry.0 == node)
            .map_or(ObjectLocation::Unknown, |entry| entry.1))
    }
    // Build the synthetic DOM's links before starting an operation. Wide/deep
    // tests exercise the common walker, not quadratic array rescans under Miri.
    fn index_edges(&mut self) {
        let mut first = vec![None; self.nodes.len()];
        let mut last: Vec<Option<usize>> = vec![None; self.nodes.len()];
        let mut next = vec![None; self.nodes.len()];
        for (node, record) in self.nodes.iter().enumerate() {
            if let Some(parent) = record.parent {
                if let Some(previous) = last[parent] {
                    next[previous] = Some(node);
                } else {
                    first[parent] = Some(node);
                }
                last[parent] = Some(node);
            }
        }
        self.indexed = Some(Edges { first, next });
    }
    fn counter_receipt<W: Work>(
        &self,
        signature: usize,
        work: &W,
    ) -> Result<Option<&CounterEvidence>, Cause> {
        work.charge_work(self.nested_counters.len() as u64 + 1)
            .map_err(Cause::Resource)?;
        if let Some(counter) = &self.counter_evidence
            && signature == counter.child
        {
            return Ok(Some(counter));
        }
        Ok(self
            .nested_counters
            .iter()
            .find(|counter| counter.child == signature))
    }

    fn mime(&mut self, parent: usize) -> usize {
        let node = self.nodes.len();
        self.nodes.push(element(Some(parent), XADES, "MimeType"));
        self.nodes.push(Record {
            parent: Some(node),
            value: Node::Text("text/plain"),
        });
        node
    }

    fn normal() -> Self {
        Self {
            document: 9,
            signature_status: Status::Valid,
            generation: Cell::new(1),
            streamed: Cell::new(0),
            canonical_routes: Vec::new(),
            reference_routes: Vec::new(),
            ignore_refusal: false,
            incomplete: false,
            binary: Vec::new(),
            binary_routes: Vec::new(),
            text_routes: Vec::new(),
            calendar_routes: Vec::new(),
            binary_depth: 0,
            binary_parser: RefCell::new(None),
            mutate_binary: false,
            decoding: Decoding::Ready,
            binary_encoding: None,
            key_association: certificate::KeyAssociation::Unproven,
            key_projection: Cell::new(None),
            digest_algorithm: Some("urn:test:checksum"),
            digest_algorithms: Vec::new(),
            object_reference: None,
            object_mime: None,
            identifier_qualifier: None,
            validation_uri: None,
            counter_evidence: None,
            nested_counters: Vec::new(),
            indexed: None,
            include_routes: Vec::new(),
            reference_completeness: Completeness::Complete,
            reference_locations: Vec::new(),
            unavailable_reference: None,
            decoded_pointer: Cell::new(0),
            cached_binary: RefCell::new(None),
            date_time: properties::DateTimeFact::Unsupported,
            date_time_calls: Cell::new(0),
            hash_uses: Vec::new(),
            hash_selection: hash_checks::Selection::Single,
            hash_completeness: structured_xades::hashes::HashCompleteness::Unprocessed(0),
            hash_calls: Cell::new(0),
            hash_visits: Cell::new(0),
            hash_refusal: hash_checks::RefusalMutation::Keep,
            scalar_routes: Vec::new(),
            scalar_calls: Cell::new(0),
            uri_routes: Vec::new(),
            attribute_routes: Vec::new(),
            qname_routes: Vec::new(),
            attribute_calls: Cell::new(0),
            references: vec![
                Reference {
                    node: 10,
                    target: None,
                    kind: None,
                    status: Status::Valid,
                    coverage: Coverage::Transformed,
                },
                Reference {
                    node: 11,
                    target: Some(3),
                    kind: Some(SIGNED_PROPERTIES),
                    status: Status::Valid,
                    coverage: Coverage::ElementContent,
                },
            ],
            target: Target::LocalBareName(0),
            target_routes: Vec::new(),
            nodes: vec![
                element(None, DS, "Signature"),
                element(Some(0), DS, "Object"),
                element(Some(1), XADES, "QualifyingProperties"),
                element(Some(2), XADES, "SignedProperties"),
                element(Some(3), XADES, "SignedSignatureProperties"),
                element(Some(4), XADES, "SigningTime"),
                element(Some(2), XADES, "UnsignedProperties"),
                element(Some(6), XADES, "UnsignedSignatureProperties"),
                element(Some(7), XADES, "SignatureTimeStamp"),
                element(Some(0), DS, "SignedInfo"),
                element(Some(9), DS, "Reference"),
                element(Some(9), DS, "Reference"),
                element(Some(0), DS, "SignatureValue"),
                element(Some(0), DS, "KeyInfo"),
            ],
        }
    }

    fn check(&self, snapshot: Snapshot<u8, u8>, node: usize) -> Result<(), Cause> {
        if snapshot != self.snapshot() || node >= self.nodes.len() {
            return Err(Cause::Source);
        }
        Ok(())
    }

    fn stamped(role: Role) -> (Self, usize, usize) {
        let mut xml = Self::normal();
        let node = if role == Role::Signature {
            8
        } else {
            let index = xml.nodes.len();
            xml.nodes
                .push(element(Some(7), XADES_141, "ArchiveTimeStamp"));
            index
        };
        let method = xml.nodes.len();
        xml.nodes
            .push(element(Some(node), DS, "CanonicalizationMethod"));
        xml.nodes
            .push(element(Some(node), XADES, "EncapsulatedTimeStamp"));
        (xml, node, method)
    }

    fn emit<W: Work>(
        &self,
        bytes: &[u8],
        receiver: &mut impl Receiver,
        work: &W,
    ) -> Result<(), Cause> {
        work.charge_work(u64::try_from(bytes.len()).unwrap())
            .map_err(Cause::Resource)?;
        self.streamed.set(self.streamed.get() + 1);
        let wrote = receiver.write(bytes);
        if self.ignore_refusal {
            let _ = receiver.write(bytes);
        } else {
            wrote.map_err(|_| Cause::Source)?;
        }
        if self.incomplete {
            return Err(Cause::Incomplete);
        }
        Ok(())
    }
}

impl TimestampXml for Tree {
    fn canonicalize<W: Work>(
        &self,
        snapshot: Snapshot<u8, u8>,
        node: usize,
        method: usize,
        receiver: &mut impl Receiver,
        execution: &W,
    ) -> Result<(), Cause> {
        self.check(snapshot, node)?;
        self.check(snapshot, method)?;
        execution
            .charge_work(self.canonical_routes.len() as u64)
            .map_err(Cause::Resource)?;
        if let Some((_, bytes)) = self.canonical_routes.iter().find(|entry| entry.0 == node) {
            return self.emit(bytes, receiver, execution);
        }
        let node = u8::try_from(node).map_err(|_| Cause::Source)?;
        let method = u8::try_from(method).map_err(|_| Cause::Source)?;
        // Synthetic framing tests common input order, not XML C14N equivalence.
        self.emit(&[b'E', node, b'M', method], receiver, execution)
    }
}

impl ReferenceXml for Tree {
    fn visit_authenticated_references<VE, W: Work>(
        &self,
        snapshot: Snapshot<u8, u8>,
        signature: usize,
        execution: &W,
        mut visitor: impl for<'view> FnMut(Descriptor<'view, usize>) -> Result<(), VE>,
    ) -> Result<Completeness<usize>, VisitError<Cause, VE>> {
        self.check(snapshot, signature)
            .map_err(VisitError::Processing)?;
        for reference in &self.references {
            execution
                .charge_work(1)
                .map_err(|cause| VisitError::Processing(Cause::Resource(cause)))?;
            let container = self.nodes[reference.node]
                .parent
                .ok_or(VisitError::Processing(Cause::Source))?;
            let parent = if container == 9 {
                Parent::SignedInfo(container)
            } else {
                Parent::Manifest(container)
            };
            visitor(Descriptor {
                reference: *reference,
                parent,
                object_location: self
                    .reference_location(reference.node, execution)
                    .map_err(VisitError::Processing)?,
            })
            .map_err(VisitError::Visitor)?;
        }
        Ok(self.reference_completeness)
    }

    fn resolve_reference<R, W: Work>(
        &self,
        snapshot: Snapshot<u8, u8>,
        signature: usize,
        property: usize,
        source: LexicalSource<'_>,
        execution: &W,
        visitor: impl for<'view> FnOnce(Descriptor<'view, usize>) -> R,
    ) -> Result<Resolution<R>, Cause> {
        self.check(snapshot, signature)?;
        self.check(snapshot, property)?;
        execution.charge_work(1).map_err(Cause::Resource)?;
        if let LexicalSource::Attribute(attribute) = source
            && (!attribute.namespace.is_empty()
                || !matches!(attribute.local, "URI" | "ObjectReference"))
        {
            return Err(Cause::Source);
        }
        // Synthetic resolver routing; URI grammar belongs to the real XML owner.
        execution
            .charge_work(self.include_routes.len() as u64)
            .map_err(Cause::Resource)?;
        let routed = self
            .include_routes
            .iter()
            .find(|entry| entry.0 == property)
            .map(|entry| entry.1);
        let target = self.object_reference;
        let ordinal = if let Some(ordinal) = routed {
            ordinal
        } else if let Some(target) = target {
            match target {
                "#direct" => 0,
                "#manifest" => 2,
                "#bad-digest" => 3,
                "#foreign" => return Ok(Resolution::Unauthenticated),
                "#unsupported" => return Ok(Resolution::Unsupported),
                _ => return Ok(Resolution::Unresolved),
            }
        } else {
            return Ok(Resolution::Missing);
        };
        let reference = *self.references.get(ordinal).ok_or(Cause::Source)?;
        let container = self.nodes[reference.node].parent.ok_or(Cause::Source)?;
        let parent = if container == 9 {
            Parent::SignedInfo(container)
        } else {
            Parent::Manifest(container)
        };
        Ok(Resolution::Resolved(visitor(Descriptor {
            reference,
            parent,
            object_location: self.reference_location(reference.node, execution)?,
        })))
    }

    fn boolean_attribute<W: Work>(
        &self,
        snapshot: Snapshot<u8, u8>,
        node: usize,
        attribute: Name<'_>,
        execution: &W,
    ) -> Result<Boolean, Cause> {
        self.check(snapshot, node)?;
        execution.charge_work(1).map_err(Cause::Resource)?;
        if attribute
            != (Name {
                namespace: "",
                local: "referencedData",
            })
        {
            return Err(Cause::Source);
        }
        execution
            .charge_work(self.include_routes.len() as u64)
            .map_err(Cause::Resource)?;
        if let Some(entry) = self.include_routes.iter().find(|entry| entry.0 == node) {
            return Ok(entry.2);
        }
        Ok(match self.validation_uri {
            None => Boolean::Absent,
            Some("true" | "1") => Boolean::Value(true),
            Some("false" | "0") => Boolean::Value(false),
            Some(_) => Boolean::Malformed,
        })
    }

    fn replay_reference<W: Work>(
        &self,
        snapshot: Snapshot<u8, u8>,
        signature: usize,
        reference: usize,
        mode: Replay<usize>,
        receiver: &mut impl Receiver,
        execution: &W,
    ) -> Result<ReplayOutcome<Cause>, Cause> {
        self.check(snapshot, signature)?;
        self.check(snapshot, reference)?;
        let ordinal = self
            .references
            .iter()
            .position(|entry| entry.node == reference)
            .ok_or(Cause::Source)?;
        let ordinal = u8::try_from(ordinal).map_err(|_| Cause::Source)?;
        if self.unavailable_reference == Some(reference) {
            return Ok(ReplayOutcome::Unavailable(Cause::Unavailable));
        }
        execution
            .charge_work(self.reference_routes.len() as u64)
            .map_err(Cause::Resource)?;
        if let Some((_, bytes)) = self
            .reference_routes
            .iter()
            .find(|entry| entry.0 == reference)
        {
            self.emit(bytes, receiver, execution)?;
            return Ok(ReplayOutcome::Complete);
        }
        match mode {
            Replay::Timestamp(method) => {
                self.check(snapshot, method)?;
                let method = u8::try_from(method).map_err(|_| Cause::Source)?;
                self.emit(&[b'R', ordinal, b'M', method], receiver, execution)
            }
            Replay::ReferenceOutput => self.emit(&[b'D', ordinal], receiver, execution),
        }?;
        Ok(ReplayOutcome::Complete)
    }
}

#[derive(Debug)]
enum SinkError {
    Account,
    Resource(MemoryError),
    Refused,
}

struct Collect {
    bytes: Option<OwnedVec<u8>>,
    refuse: bool,
}

impl Collect {
    fn new(memory: &Memory) -> Self {
        Self {
            bytes: Some(memory.vector()),
            refuse: false,
        }
    }
    fn bytes(&self) -> &[u8] {
        self.bytes.as_ref().unwrap()
    }
}

impl Sink for Collect {
    type Error = SinkError;
    fn write<W: Work>(&mut self, bytes: &[u8], execution: &W) -> Result<(), Self::Error> {
        if self.refuse {
            return Err(SinkError::Refused);
        }
        let retained = self.bytes.take().unwrap();
        match retained.with_execution(execution) {
            Ok(mut scoped) => {
                let copied = scoped.try_extend_from_slice(bytes);
                self.bytes = Some(scoped.into_owned());
                copied.map_err(SinkError::Resource)
            }
            Err(retained) => {
                self.bytes = Some(retained);
                Err(SinkError::Account)
            }
        }
    }
}

fn funded() -> Memory {
    Memory::new(Arc::new(MemoryAccount::new(65_536, 300_000)))
}

impl SignatureXml for Tree {
    fn signature_status<W: Work>(
        &self,
        snapshot: Snapshot<u8, u8>,
        signature: usize,
        execution: &W,
    ) -> Result<Status, Cause> {
        self.check(snapshot, signature)?;
        if let Some(counter) = self.counter_receipt(signature, execution)? {
            return Ok(counter.status);
        }
        Ok(self.signature_status)
    }

    fn reference_count<W: Work>(
        &self,
        snapshot: Snapshot<u8, u8>,
        signature: usize,
        execution: &W,
    ) -> Result<usize, Cause> {
        self.check(snapshot, signature)?;
        if let Some(counter) = self.counter_receipt(signature, execution)? {
            return Ok(counter.references.len());
        }
        Ok(self.references.len())
    }

    fn reference<R, W: Work>(
        &self,
        snapshot: Snapshot<u8, u8>,
        signature: usize,
        ordinal: usize,
        execution: &W,
        visitor: impl for<'view> FnOnce(Reference<'view, usize>) -> R,
    ) -> Result<R, Cause> {
        self.check(snapshot, signature)?;
        if let Some(counter) = self.counter_receipt(signature, execution)? {
            let reference = counter.references.get(ordinal).ok_or(Cause::Source)?;
            return Ok(visitor(*reference));
        }
        let reference = self.references.get(ordinal).ok_or(Cause::Source)?;
        Ok(visitor(*reference))
    }

    fn qualifying_target<W: Work>(
        &self,
        snapshot: Snapshot<u8, u8>,
        qualifying: usize,
        execution: &W,
    ) -> Result<Target<usize>, Cause> {
        self.check(snapshot, qualifying)?;
        for (node, target) in &self.target_routes {
            execution.charge_work(1).map_err(Cause::Resource)?;
            if *node == qualifying {
                return Ok(*target);
            }
        }
        if self.nested_counters.is_empty() && self.counter_evidence.is_none() {
            return Ok(self.target);
        }
        // Synthetic original evidence can represent selected child signatures
        // independently of the root receipt, without changing XML generation.
        execution.charge_work(2).map_err(Cause::Resource)?;
        if let Some(object) = self.nodes[qualifying].parent
            && let Some(signature) = self.nodes[object].parent
            && self.counter_receipt(signature, execution)?.is_some()
        {
            return Ok(Target::LocalBareName(signature));
        }
        Ok(self.target)
    }
}

impl Xml for Tree {
    type Id = usize;
    type Document = u8;
    type Generation = u8;
    type Error = Cause;

    fn snapshot(&self) -> Snapshot<u8, u8> {
        Snapshot {
            document: self.document,
            generation: self.generation.get(),
        }
    }

    fn inspect<R, W: Work>(
        &self,
        snapshot: Snapshot<u8, u8>,
        node: usize,
        _execution: &W,
        visitor: impl for<'view> FnOnce(Node<'view>) -> R,
    ) -> Result<R, Cause> {
        self.check(snapshot, node)?;
        Ok(visitor(self.nodes[node].value))
    }

    fn attribute<R, W: Work>(
        &self,
        snapshot: Snapshot<u8, u8>,
        node: usize,
        name: Name<'_>,
        execution: &W,
        visitor: impl for<'view> FnOnce(Option<&'view str>) -> R,
    ) -> Result<R, Cause> {
        self.check(snapshot, node)?;
        Ok(visitor(
            if name.namespace.is_empty() && name.local == "Qualifier" {
                self.identifier_qualifier
            } else if name.namespace.is_empty() && name.local == "MimeType" {
                self.object_mime
            } else if name.namespace.is_empty() && name.local == "Encoding" {
                self.binary_encoding
            } else if name.namespace.is_empty() && name.local == "Algorithm" {
                execution
                    .charge_work(self.digest_algorithms.len() as u64)
                    .map_err(Cause::Resource)?;
                self.digest_algorithms
                    .iter()
                    .find(|entry| entry.0 == node)
                    .map_or(self.digest_algorithm, |entry| Some(entry.1))
            } else if name.namespace.is_empty() && name.local == "ObjectReference" {
                self.object_reference
            } else if name.namespace.is_empty() && name.local == "URI" {
                self.validation_uri
            } else {
                None
            },
        ))
    }

    fn first_child<W: Work>(
        &self,
        snapshot: Snapshot<u8, u8>,
        parent: usize,
        execution: &W,
    ) -> Result<Option<usize>, Cause> {
        self.check(snapshot, parent)?;
        if let Some(index) = &self.indexed {
            if index.first.len() != self.nodes.len() {
                return Err(Cause::Source);
            }
            execution.charge_work(1).map_err(Cause::Resource)?;
            return Ok(index.first[parent]);
        }
        for index in 0..self.nodes.len() {
            execution.charge_work(1).map_err(Cause::Resource)?;
            if self.nodes[index].parent == Some(parent) {
                return Ok(Some(index));
            }
        }
        Ok(None)
    }

    fn next_sibling<W: Work>(
        &self,
        snapshot: Snapshot<u8, u8>,
        parent: usize,
        node: usize,
        execution: &W,
    ) -> Result<Option<usize>, Cause> {
        self.check(snapshot, node)?;
        if self.nodes[node].parent != Some(parent) {
            return Err(Cause::Source);
        }
        if let Some(index) = &self.indexed {
            if index.next.len() != self.nodes.len() {
                return Err(Cause::Source);
            }
            execution.charge_work(1).map_err(Cause::Resource)?;
            return Ok(index.next[node]);
        }
        for index in node + 1..self.nodes.len() {
            execution.charge_work(1).map_err(Cause::Resource)?;
            if self.nodes[index].parent == Some(parent) {
                return Ok(Some(index));
            }
        }
        Ok(None)
    }
}

impl properties::DateTimeXml for Tree {
    fn date_time<W: Work>(
        &self,
        snapshot: Snapshot<u8, u8>,
        node: usize,
        execution: &W,
    ) -> Result<properties::DateTimeFact, Cause> {
        // Metadata projection oracle only. Actual XSD lexical qualification is
        // the XML owner's public scalar suite, not this synthetic tree.
        self.check(snapshot, node)?;
        self.date_time_calls.set(self.date_time_calls.get() + 1);
        execution.charge_work(1).map_err(Cause::Resource)?;
        Ok(self.date_time)
    }
}

impl certificate::CertificateXml for Tree {
    fn key_association<W: Work>(
        &self,
        snapshot: Snapshot<u8, u8>,
        signature: usize,
        _certificate: &structured_pkix::Certificate<'_>,
        key: structured_pkix::path::WorkingKey<'_>,
        execution: &W,
    ) -> Result<certificate::KeyAssociation, Cause> {
        // Explicit synthetic owner receipt, not real crypto/key qualification.
        self.check(snapshot, signature)?;
        execution.charge_work(1).map_err(Cause::Resource)?;
        self.key_projection.set(Some(KeyProjection::of(key)));
        Ok(self.key_association)
    }
}

impl binary::OwnedBinaryXml for Tree {
    fn decode_owned<W: Work>(
        &self,
        snapshot: Snapshot<u8, u8>,
        node: usize,
        execution: &W,
    ) -> Result<OwnedVec<u8>, Cause> {
        self.check(snapshot, node)?;
        execution.charge_work(1).map_err(Cause::Resource)?;
        if matches!(self.decoding, Decoding::Refuse) {
            return Err(Cause::Source);
        }
        if let Some(buffer) = self.cached_binary.borrow_mut().take() {
            return Ok(buffer);
        }
        // Fixture source already has decoded octets. A real adapter must admit
        // and reuse its original lexical/base64 kernel, not this fixture copy.
        let mut decoded = execution.vector();
        execution
            .charge_work(self.binary_routes.len() as u64)
            .map_err(Cause::Resource)?;
        let bytes = match self
            .binary_routes
            .iter()
            .find(|(original, _)| *original == node)
        {
            Some((_, bytes)) => bytes,
            None => &self.binary,
        };
        decoded
            .try_extend_from_slice(bytes)
            .map_err(Cause::Resource)?;
        self.decoded_pointer.set(decoded.as_ptr() as usize);
        if self.mutate_binary {
            self.generation.set(self.generation.get() + 1);
        }
        Ok(decoded.into_owned())
    }

    fn decode_binary_owned(
        &self,
        snapshot: Snapshot<u8, u8>,
        node: usize,
        operation: &structured_asn1::Reader<'_>,
    ) -> Result<binary::OwnedDecoded, Cause> {
        let bytes =
            binary::OwnedBinaryXml::decode_owned(self, snapshot, node, &operation.execution())?;
        // Synthetic fresh source, as in with_binary. Cached real sources must
        // move their original Parser too, rather than use this fixture constructor.
        let parser = match self.binary_parser.borrow_mut().take() {
            Some(original) => original,
            None => structured_asn1::Parser::ber(operation.memory().clone()),
        };
        Ok(binary::OwnedDecoded {
            parser,
            bytes,
            base: 0,
            depth: self.binary_depth,
        })
    }
}

impl BinaryXml for Tree {
    fn with_octets<R, C, W: Work>(
        &self,
        snapshot: Snapshot<u8, u8>,
        node: usize,
        execution: &W,
        visitor: impl for<'source> FnOnce(&'source [u8]) -> Result<R, C>,
    ) -> Result<Result<R, C>, Cause> {
        let decoded = binary::OwnedBinaryXml::decode_owned(self, snapshot, node, execution)?;
        let result = visitor(&decoded);
        Ok(result)
    }

    fn with_binary<R, C>(
        &self,
        snapshot: Snapshot<u8, u8>,
        node: usize,
        operation: &structured_asn1::Reader<'_>,
        visitor: impl for<'source> FnOnce(binary::Decoded<'source>) -> Result<R, C>,
    ) -> Result<Result<R, C>, Cause> {
        self.with_octets(snapshot, node, &operation.execution(), |decoded| {
            let parser = structured_asn1::Parser::ber(operation.memory().clone());
            visitor(binary::Decoded {
                parser: &parser,
                bytes: decoded,
                base: 0,
                depth: self.binary_depth,
            })
        })
    }
}

fn memory(work: usize) -> Memory {
    Memory::new(Arc::new(MemoryAccount::new(0, work)))
}

#[test]
fn original_containers_and_snapshot_survive_without_dom_or_string_copies() {
    // Structural selection preserves every original role; it makes no trust claim.
    let xml = Tree::normal();
    let memory = memory(10000);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let found = container::locate(&access, 0).unwrap();
    assert_eq!(found.snapshot(), xml.snapshot());
    assert_eq!(found.signature(), 0);
    assert_eq!(found.object(), 1);
    assert_eq!(found.qualifying(), 2);
    assert_eq!(found.signed(), 3);
    assert_eq!(found.unsigned(), Some(6));
}

#[test]
fn nested_signature_properties_cannot_substitute_for_selected_signature() {
    // A descendant search would accept a wrapping attack's different Signature.
    let mut xml = Tree::normal();
    let nested = xml.nodes.len();
    xml.nodes.push(element(Some(1), DS, "Signature"));
    let nested_object = xml.nodes.len();
    xml.nodes.push(element(Some(nested), DS, "Object"));
    xml.nodes[2].parent = Some(nested_object);
    let memory = memory(10000);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        container::locate(&access, 0),
        Err(ContainerError::Missing {
            parent: 0,
            local: "QualifyingProperties"
        })
    ));
}

#[test]
fn duplicate_qualifying_properties_in_another_object_are_not_first_match_success() {
    // The single-container rule applies across all direct ds:Object children.
    let mut xml = Tree::normal();
    let object = xml.nodes.len();
    xml.nodes.push(element(Some(0), DS, "Object"));
    let duplicate = xml.nodes.len();
    xml.nodes
        .push(element(Some(object), XADES, "QualifyingProperties"));
    let memory = memory(10000);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        container::locate(&access, 0),
        Err(ContainerError::Duplicate { node }) if node == duplicate
    ));
}

#[test]
fn baseline_direct_properties_cannot_hide_an_indirect_reference_in_another_object() {
    // EN6.3 requires direct-only incorporation at every current baseline level.
    // A valid direct SignedProperties cannot conceal a second indirect branch.
    for same_object in [true, false] {
        let mut xml = Tree::normal();
        let object = if same_object {
            1
        } else {
            let object = xml.nodes.len();
            xml.nodes.push(element(Some(0), DS, "Object"));
            object
        };
        let reference = xml.nodes.len();
        xml.nodes.push(element(
            Some(object),
            XADES,
            "QualifyingPropertiesReference",
        ));
        let memory = funded();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        assert!(
            matches!(container::locate(&access, 0), Err(ContainerError::Unexpected { node }) if node == reference)
        );
        assert_eq!(xml.streamed.get(), 0);
    }
}

#[test]
fn mixed_content_of_other_objects_is_not_a_property_schema_violation() {
    // ds:Object's opaque application payload is allowed to contain character data.
    let mut xml = Tree::normal();
    let object = xml.nodes.len();
    xml.nodes.push(element(Some(0), DS, "Object"));
    xml.nodes.push(Record {
        parent: Some(object),
        value: Node::Text("opaque application payload"),
    });
    let memory = memory(10000);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert_eq!(container::locate(&access, 0).unwrap().qualifying(), 2);
}

#[test]
fn namespace_spelling_cannot_impersonate_xades() {
    // Namespace identity is exact even when a URI looks similar to the real one.
    let mut xml = Tree::normal();
    xml.nodes[2].value = Node::Element(Name {
        namespace: "HTTP://uri.etsi.org/01903/v1.3.2#",
        local: "QualifyingProperties",
    });
    let memory = memory(10000);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        container::locate(&access, 0),
        Err(ContainerError::Missing { .. })
    ));
}

#[test]
fn signed_after_unsigned_is_rejected_and_empty_signed_is_not_a_valid_layout() {
    // XSD sequence order and the normative nonempty rule are separate constraints.
    let mut xml = Tree::normal();
    xml.nodes[3].value = Node::Element(Name {
        namespace: XADES,
        local: "UnsignedProperties",
    });
    xml.nodes[6].value = Node::Element(Name {
        namespace: XADES,
        local: "SignedProperties",
    });
    let memory = memory(10000);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        container::locate(&access, 0),
        Err(ContainerError::Order { node: 6 })
    ));

    let mut xml = Tree::normal();
    xml.nodes[4].parent = Some(1);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        container::locate(&access, 0),
        Err(ContainerError::Empty { node: 3 })
    ));
}

#[test]
fn original_resource_cause_is_not_missing_properties() {
    // Locating a container cannot turn work exhaustion into document absence.
    let xml = Tree::normal();
    let memory = memory(0);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        container::locate(&access, 0),
        Err(ContainerError::Access(AccessError::Resource {
            node: 0,
            cause: MemoryError::WorkExceeded
        }))
    ));

    // Initial container traversal fits; the later QName comparison must refuse.
    let memory = self::memory(100);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        container::locate(&access, 0),
        Err(ContainerError::Access(AccessError::Resource {
            node: 2,
            cause: MemoryError::WorkExceeded
        }))
    ));

    // Exactly the signature QName and dispatch fit; owner scanning then refuses.
    let memory = self::memory(45);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        container::locate(&access, 0),
        Err(ContainerError::Child(
            structured_xades::xml::ChildError::Access(AccessError::Xml(Cause::Resource(
                MemoryError::WorkExceeded
            )))
        ))
    ));
}

#[test]
fn selected_binding_uses_exact_reference_target_type_and_generation() {
    // The result keeps the exact selected signature and its SignedInfo ordinal.
    let xml = Tree::normal();
    let memory = memory(10000);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    assert_eq!(bound.containers().signature(), 0);
    assert_eq!(bound.containers().signed(), 3);
    assert_eq!(bound.reference(), 11);
    assert_eq!(bound.ordinal(), 1);
    assert_eq!(bound.containers().snapshot(), xml.snapshot());
}

#[test]
fn binding_does_not_substitute_another_signature_or_unresolved_target() {
    // A successful reference somewhere else cannot authenticate the chosen node.
    let mut xml = Tree::normal();
    xml.target = Target::LocalBareName(8);
    let memory = memory(10000);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    assert!(matches!(
        binding::bind(&access, &layout),
        Err(BindingError::Target { node: 2 })
    ));

    xml.target = Target::Other;
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        binding::bind(&access, &layout),
        Err(BindingError::Target { node: 2 })
    ));

    xml.target = Target::LocalBareName(0);
    xml.signature_status = Status::Invalid;
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        binding::bind(&access, &layout),
        Err(BindingError::Signature {
            node: 0,
            status: Status::Invalid
        })
    ));
}

#[test]
fn matching_type_without_complete_original_coverage_cannot_be_bound() {
    // Valid digest over an arbitrary filtered node-set does not cover the property.
    let mut xml = Tree::normal();
    xml.references[1].coverage = Coverage::Transformed;
    let memory = memory(10000);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    assert!(matches!(
        binding::bind(&access, &layout),
        Err(BindingError::Reference { node: 11 })
    ));
}

#[test]
fn repeated_valid_reference_to_the_same_properties_does_not_invent_cardinality_rule() {
    // EN 319132-1 §4.4.2 requires a protecting reference, not at most one reference.
    let mut xml = Tree::normal();
    let mut repeated = xml.references[1];
    repeated.node = 12;
    xml.references.push(repeated);
    let memory = memory(10000);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    assert_eq!(binding::bind(&access, &layout).unwrap().reference(), 11);
}

#[test]
fn signature_timestamp_covers_original_signature_value_element_only() {
    // Core selects the element for the owner, never decoded signature octets.
    let (xml, node, method) = Tree::stamped(Role::Signature);
    let memory = funded();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let stamp = timestamp::read(&access, &bound, node).unwrap();
    assert_eq!(stamp.role(), Role::Signature);
    assert_eq!(stamp.method(), method);
    assert_eq!(stamp.tokens(), &[method + 1]);
    let mut input = Input::prepare(&xml, &bound, &stamp, &memory).unwrap();
    assert_eq!(input.location(), (xml.snapshot(), node));
    let mut sink = Collect::new(&memory);
    stream::run(&mut input, &mut sink, &memory, "logical.timestamp.input", 0).unwrap();
    assert_eq!(
        sink.bytes(),
        &[b'E', 12, b'M', u8::try_from(method).unwrap()]
    );
    assert_eq!(xml.streamed.get(), 1);
}

#[test]
fn timestamp_md5_imprint_is_a_baseline_refusal_before_original_input_replay() {
    // EN319132-1 §6.2.1 prohibits MD5 even when the CMS signature itself uses
    // an allowed digest. A generic Unsupported result after reading XML would
    // miss the baseline rule and perform observable work before its refusal.
    use structured_cms::{ess::Version, timestamp::ProfileSource};
    assert_timestamp_digest_prohibition(
        timestamp_oracle::fixture_md5_imprint(),
        timestamp::DigestUse::Imprint,
        ProfileSource::Information,
    );
    assert_timestamp_digest_prohibition(
        timestamp_oracle::fixture_md5_signer(),
        timestamp::DigestUse::Signer,
        ProfileSource::Envelope,
    );
    for index in [0, 1] {
        assert_timestamp_digest_prohibition(
            timestamp_oracle::fixture_md5_ess(index),
            timestamp::DigestUse::Certificate {
                version: Version::V2,
                index,
            },
            ProfileSource::Envelope,
        );
    }
}

fn assert_timestamp_digest_prohibition(
    (provider, token, certificate_bytes): (timestamp_oracle::Oracle, Vec<u8>, Vec<u8>),
    usage: timestamp::DigestUse,
    domain: structured_cms::timestamp::ProfileSource,
) {
    // Both original source domains must survive the same complete-check bridge,
    // including a later signed restriction after the allowed first identity.
    use structured_cms::timestamp::CheckContext;
    use structured_pkix::{
        path::WorkingKey,
        temporal::{Instant, LeapTable},
    };
    let (mut xml, node, _) = Tree::stamped(Role::Signature);
    xml.binary = token;
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let certificate = structured_pkix::Certificate::read(&parser, &certificate_bytes, 400).unwrap();
    let utc = LeapTable::new(0, 2_000_000_000, &[]).unwrap();
    let context = CheckContext {
        certificate: &certificate,
        certificate_parent: &operation,
        key: WorkingKey::standing(certificate.tbs().public_key()),
        requested: None,
        utc: &utc,
        reference: Some(Instant::new(1_800_000_000, 0).unwrap()),
        window: None,
        policy_accuracy: None,
    };
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let stamp = timestamp::read(&access, &bound, node).unwrap();
    let check = timestamp::Check {
        xml: &xml,
        provider: &provider,
        context: &context,
    };
    let answer = check.retain(&bound, &stamp, 0, &operation, |_, _| Ok::<_, ()>(false));
    let Err(timestamp::CheckError::Token(binary::Error::Callback {
        location,
        cause:
            binary::TokenError::Callback(structured_cms::timestamp::ProfileCheckError::Profile {
                source,
                context: map,
            }),
    })) = answer
    else {
        panic!("MD5 refusal lost its original profile class and source")
    };
    assert_eq!(location.node, stamp.tokens()[0]);
    assert_eq!(location.snapshot, xml.snapshot());
    assert_eq!(source.source, domain);
    let timestamp::DigestError::Prohibited {
        usage: actual,
        offset,
    } = source.cause
    else {
        panic!("MD5 became a resource or provider refusal")
    };
    assert_eq!(actual, usage);
    let physical = if domain == structured_cms::timestamp::ProfileSource::Information {
        map.project_offset(offset, &memory).unwrap()
    } else {
        offset
    };
    assert!(physical < xml.binary.len() as u64);
    assert_eq!(xml.streamed.get(), 0, "prohibited imprint replayed XML");
    assert_eq!(provider.verified.get(), 0);
}

#[test]
fn original_signature_and_archive_inputs_drive_real_timestamp_signature_and_retained_evidence() {
    // This proves the actual common RFC3161 complete checker receives our
    // prepared original input, verifies ECDSA/ESS/imprint and retains evidence.
    // Framing is synthetic: this does not qualify real XML C14N or B profiles.
    use structured_cms::timestamp::CheckContext;
    use structured_pkix::{
        path::WorkingKey,
        temporal::{Instant, LeapTable},
    };
    for role in [Role::Signature, Role::Archive] {
        for agrees in [true, false] {
            let memory = Memory::new(Arc::new(MemoryAccount::new(1_000_000, 10_000_000)));
            let located = {
                let (mut xml, node, method) = Tree::stamped(role);
                let m = u8::try_from(method).unwrap();
                let expected = if role == Role::Signature {
                    vec![b'E', 12, b'M', m]
                } else {
                    vec![
                        b'R', 0, b'M', m, b'R', 1, b'M', m, b'E', 9, b'M', m, b'E', 12, b'M', m,
                        b'E', 13, b'M', m, b'E', 8, b'M', m,
                    ]
                };
                let covered = if agrees {
                    expected.as_slice()
                } else {
                    b"unrelated original input"
                };
                let (provider, token, certificate_bytes) = if role == Role::Signature && agrees {
                    // The baseline forbids actual MD5 use, not an unused
                    // advertised entry in the generic CMS algorithm catalogue.
                    timestamp_oracle::fixture_unused_md5(covered)
                } else {
                    timestamp_oracle::fixture(covered)
                };
                {
                    let inspection = structured_asn1::Parser::der(memory.clone());
                    let decoded =
                        structured_cms::timestamp::Token::read(&inspection, &token, 0).unwrap();
                    assert!(decoded.faults().is_empty(), "{:?}", decoded.faults());
                    assert!(
                        decoded.message().faults().is_empty(),
                        "{:?}",
                        decoded.message().faults()
                    );
                    assert!(
                        decoded.signer().faults().is_empty(),
                        "{:?}",
                        decoded.signer().faults()
                    );
                }
                xml.binary = token;
                let parser = structured_asn1::Parser::der(memory.clone());
                let operation = parser.read(&[], 900).unwrap();
                let cert_parser = structured_asn1::Parser::der(memory.clone());
                let certificate =
                    structured_pkix::Certificate::read(&cert_parser, &certificate_bytes, 400)
                        .unwrap();
                let certificate_parent = cert_parser.read(&[], 400).unwrap();
                let utc = LeapTable::new(0, 2_000_000_000, &[]).unwrap();
                let context = CheckContext {
                    certificate: &certificate,
                    certificate_parent: &certificate_parent,
                    key: WorkingKey::standing(certificate.tbs().public_key()),
                    requested: None,
                    utc: &utc,
                    reference: Some(Instant::new(1_800_000_000, 0).unwrap()),
                    window: None,
                    policy_accuracy: None,
                };
                let access = Access::new(&xml, xml.snapshot(), &memory);
                let layout = container::locate(&access, 0).unwrap();
                let bound = binding::bind(&access, &layout).unwrap();
                let stamp = timestamp::read(&access, &bound, node).unwrap();
                let check = timestamp::Check {
                    xml: &xml,
                    provider: &provider,
                    context: &context,
                };
                let located = check
                    .retain(&bound, &stamp, 0, &operation, |_, _| Ok::<_, ()>(false))
                    .unwrap();
                assert_eq!(located.is_some(), agrees);
                if let Some(verified) = &located {
                    assert_eq!(verified.token().node, stamp.tokens()[0]);
                    assert_eq!(verified.token().snapshot, xml.snapshot());
                    assert_eq!(verified.property(), stamp.node());
                    assert_eq!(verified.signature(), layout.signature());
                    assert_eq!(verified.purpose(), role.purpose());
                    assert_verified_element_coverage(&xml, &bound, &stamp, verified, &memory);
                }
                assert_eq!(provider.verified.get(), 1);
                located
            };
            if agrees {
                assert!(memory.used() > 0);
                // The privately bound XML identity and common evidence survive
                // every XML/parser/provider source; endpoints reuse common UTC.
                let utc = LeapTable::new(0, 2_000_000_000, &[]).unwrap();
                let interval = located.as_ref().unwrap().interval(&utc, &memory).unwrap();
                assert_eq!(interval.earliest, Instant::new(1_767_225_600, 0).unwrap());
                assert_eq!(interval.latest, interval.earliest);
            }
            drop(located);
            assert_eq!(memory.used(), 0);
        }
    }
}

fn assert_verified_element_coverage(
    xml: &Tree,
    bound: &binding::Selected<Tree>,
    stamp: &timestamp::Timestamp<Tree>,
    verified: &timestamp::VerifiedTimestamp<Tree>,
    memory: &Memory,
) {
    let access = Access::new(xml, xml.snapshot(), memory);
    let input = Input::prepare(xml, bound, stamp, memory).unwrap();
    let coverage = input.element_coverage(memory).unwrap();
    let signature = bound.containers().signature();
    let value = xml.element_child(signature, DS, "SignatureValue");
    let key = xml.element_child(signature, DS, "KeyInfo");
    assert!(coverage.covers(verified, value, &access).unwrap());
    // Retained timestamp evidence cannot spend its earlier account when a
    // later coverage query has no work, even for the same original identities.
    let denied = Memory::new(Arc::new(MemoryAccount::new(0, 0)));
    let denied_access = Access::new(xml, xml.snapshot(), &denied);
    assert!(matches!(
        coverage.covers(verified, value, &denied_access),
        Err(structured_ltv::material::CoverageError::Scope(_))
    ));
    assert_eq!(
        coverage.covers(verified, key, &access).unwrap(),
        stamp.role() == Role::Archive
    );
    assert!(
        !coverage
            .covers(verified, stamp.tokens()[0], &access)
            .unwrap()
    );
    let foreign = Access::new(
        xml,
        Snapshot {
            document: xml.document + 1,
            generation: xml.generation.get(),
        },
        memory,
    );
    assert!(matches!(
        coverage.covers(verified, value, &foreign),
        Err(structured_ltv::material::CoverageError::Foreign { .. })
    ));
}

#[test]
fn archive_element_coverage_excludes_its_own_and_later_material_and_non_elements() {
    // The prepared input set is the SAME sequence as replay. Earlier material
    // is included, but the archive token cannot authenticate its own material
    // or a later property merely because both share an unsigned container.
    let (mut xml, archive, _) = Tree::stamped(Role::Archive);
    let earlier = xml.element_child(0, DS, "SignatureValue");
    let later = xml.nodes.len();
    xml.nodes.push(element(Some(7), XADES, "AnyValidationData"));
    let token = xml.element_child(archive, XADES, "EncapsulatedTimeStamp");
    let ignored = xml.nodes.len();
    xml.nodes.push(Record {
        parent: Some(earlier),
        value: Node::Other,
    });
    let memory = funded();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let stamp = timestamp::read(&access, &bound, archive).unwrap();
    let input = Input::prepare(&xml, &bound, &stamp, &memory).unwrap();
    let held = memory.used();
    let coverage = input.element_coverage(&memory).unwrap();
    assert!(coverage.selects(earlier, &access).unwrap());
    assert!(!coverage.selects(archive, &access).unwrap());
    assert!(!coverage.selects(token, &access).unwrap());
    assert!(!coverage.selects(later, &access).unwrap());
    assert!(!coverage.selects(ignored, &access).unwrap());
    let zero = Memory::new(Arc::new(MemoryAccount::new(0, 0)));
    let denied = Access::new(&xml, xml.snapshot(), &zero);
    assert!(matches!(
        coverage.selects(earlier, &denied),
        Err(structured_ltv::material::CoverageError::Scope(
            structured_ltv::source::ScopeError::Resource { .. }
        ))
    ));
    xml.generation.set(2);
    assert!(matches!(
        coverage.selects(earlier, &access),
        Err(structured_ltv::material::CoverageError::Scope(
            structured_ltv::source::ScopeError::Changed { .. }
        ))
    ));
    drop(coverage);
    assert_eq!(memory.used(), held);
}

#[test]
fn timestamp_element_coverage_requires_explicit_successful_whole_target_receipt() {
    // Authenticated reference instructions alone do not prove their targets'
    // original element contents survived arbitrary transforms or were checked.
    for (coverage, status, expected) in [
        (Coverage::Transformed, Status::Valid, false),
        (Coverage::ElementContent, Status::Unchecked, false),
        (Coverage::ElementContent, Status::Valid, true),
    ] {
        let (mut xml, archive, _) = Tree::stamped(Role::Archive);
        let envelope = xml.nodes.len();
        xml.nodes.push(element(None, "urn:test", "Envelope"));
        xml.nodes[0].parent = Some(envelope);
        let payload = xml.nodes.len();
        xml.nodes
            .push(element(Some(envelope), "urn:test", "DetachedFromSignature"));
        let inner = xml.nodes.len();
        xml.nodes.push(element(Some(payload), "urn:test", "Binary"));
        xml.references[0].target = Some(payload);
        xml.references[0].coverage = coverage;
        xml.references[0].status = status;
        let memory = funded();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        let layout = container::locate(&access, 0).unwrap();
        let bound = binding::bind(&access, &layout).unwrap();
        let stamp = timestamp::read(&access, &bound, archive).unwrap();
        let input = Input::prepare(&xml, &bound, &stamp, &memory).unwrap();
        let selection = input.element_coverage(&memory).unwrap();
        assert_eq!(selection.selects(payload, &access).unwrap(), expected);
        assert_eq!(selection.selects(inner, &access).unwrap(), expected);
    }
}

#[test]
fn timestamp_reference_document_root_is_a_traversal_boundary_not_a_covered_element() {
    // The XML owner's DocumentNode may be its virtual document root. This is
    // a receipt-shape fixture, not a claim of a valid self-referencing signature.
    let (mut xml, archive, _) = Tree::stamped(Role::Archive);
    let root = xml.nodes.len();
    xml.nodes.push(Record {
        parent: None,
        value: Node::Other,
    });
    xml.nodes[0].parent = Some(root);
    let payload = xml.nodes.len();
    xml.nodes.push(element(Some(root), "urn:test", "Payload"));
    xml.references[0].target = Some(root);
    xml.references[0].coverage = Coverage::ElementContent;
    let memory = funded();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let stamp = timestamp::read(&access, &bound, archive).unwrap();
    let input = Input::prepare(&xml, &bound, &stamp, &memory).unwrap();
    let selection = input.element_coverage(&memory).unwrap();
    assert!(!selection.selects(root, &access).unwrap());
    assert!(selection.selects(payload, &access).unwrap());
}

#[test]
fn archive_element_coverage_uses_iterative_frames_for_deep_original_payloads() {
    // Other Object payloads can be deeply nested without recursive traversal.
    // A refused partial index releases both identity storage and active frames.
    let (mut xml, archive, _) = Tree::stamped(Role::Archive);
    let object = xml.nodes.len();
    xml.nodes.push(element(Some(0), DS, "Object"));
    let mut leaf = object;
    for _ in 0..512 {
        let parent = leaf;
        leaf = xml.nodes.len();
        xml.nodes.push(element(Some(parent), "urn:test", "Nested"));
    }
    xml.index_edges();
    let memory = Memory::new(Arc::new(MemoryAccount::new(1_048_576, 10_000_000)));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let stamp = timestamp::read(&access, &bound, archive).unwrap();
    let input = Input::prepare(&xml, &bound, &stamp, &memory).unwrap();
    let held = memory.used();
    let coverage = input.element_coverage(&memory).unwrap();
    assert!(coverage.selects(object, &access).unwrap());
    assert!(coverage.selects(leaf, &access).unwrap());
    drop(coverage);
    assert_eq!(memory.used(), held);
    let small = Memory::new(Arc::new(MemoryAccount::new(1024, 10_000_000)));
    assert!(input.element_coverage(&small).is_err());
    assert_eq!(small.used(), 0);
    assert_eq!(memory.used(), held);
}

fn token_pipeline_tree() -> (Tree, usize, usize) {
    let mut xml = preparation_tree();
    let first = xml
        .nodes
        .iter()
        .position(|record| {
            record.parent == Some(8)
                && matches!(
                    record.value,
                    Node::Element(Name {
                        local: "EncapsulatedTimeStamp",
                        ..
                    })
                )
        })
        .unwrap();
    let archive = xml.nodes.len();
    xml.nodes
        .push(element(Some(7), XADES_141, "ArchiveTimeStamp"));
    xml.nodes
        .push(element(Some(archive), DS, "CanonicalizationMethod"));
    let second = xml.nodes.len();
    xml.nodes
        .push(element(Some(archive), XADES, "EncapsulatedTimeStamp"));
    (xml, first, second)
}

fn inspection_token() -> Vec<u8> {
    inspection_token_with_choices(&[], 3)
}

fn inspection_token_with_choices(revocations: &[u8], version: u8) -> Vec<u8> {
    inspection_token_with_carriage(revocations, &[], version)
}

fn inspection_token_with_carriage(revocations: &[u8], certificates: &[u8], version: u8) -> Vec<u8> {
    // A schema-only CMS token. Its carried empty TSTInfo is deliberately NOT
    // usable evidence; this fixture exercises source retention, not crypto.
    use structured_cms::oid;
    use timestamp_oracle::tlv;
    let algorithm = tlv(0x30, &tlv(6, &[0x60, 0x86, 0x48, 1, 0x65, 3, 4, 2, 1]));
    let mut signer = vec![2, 1, 3, 0x80, 1, 7];
    signer.extend(&algorithm);
    signer.extend(tlv(0x30, &tlv(6, &[0x2a, 0x86, 0x48, 0xce, 0x3d, 4, 3, 2])));
    signer.extend([4, 1, 1]);
    let mut content = tlv(6, oid::TST_INFO.as_bytes());
    content.extend(tlv(0xa0, &tlv(4, &[0x30, 0])));
    let mut body = vec![2, 1, version];
    body.extend(tlv(0x31, &algorithm));
    body.extend(tlv(0x30, &content));
    if !certificates.is_empty() {
        body.extend(tlv(0xa0, certificates));
    }
    if !revocations.is_empty() {
        body.extend(tlv(0xa1, revocations));
    }
    body.extend(tlv(0x31, &tlv(0x30, &signer)));
    let mut envelope = tlv(6, oid::SIGNED_DATA.as_bytes());
    envelope.extend(tlv(0xa0, &tlv(0x30, &body)));
    tlv(0x30, &envelope)
}

#[test]
fn material_index_combines_xml_and_token_pools_without_losing_original_placement() {
    // Equal DER carried in three storage roles keeps five distinct actual
    // occurrences. Source-pool tags prevent token ordinal 0 aliasing XML 0.
    let certificate = candidate_certificate();
    let (mut xml, first, second) = token_pipeline_tree();
    let header = xml
        .nodes
        .iter()
        .position(|record| {
            matches!(
                record.value,
                Node::Element(Name {
                    namespace: DS,
                    local: "X509Certificate"
                })
            )
        })
        .unwrap();
    xml.binary_routes.push((header, certificate.clone()));
    xml.binary = inspection_token_with_carriage(
        &carried_ocsp_choice(&ocsp_material_with_certificate(&certificate)),
        &certificate,
        5,
    );
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Archival, None).unwrap();
    let held = memory.used();
    material::with_values(&xml, &prepared, &operation, |query, values| {
        timestamp::with_tokens(&xml, &prepared, query, timestamp::TokenProfile::Etsi, |current, tokens| {
            let carried = tokens.material_in(current).unwrap();
            let execution = current.execution();
            let access = Access::new(&xml, xml.snapshot(), &execution);
            // Source-pair identity is new work even though both parsed views
            // are already retained. Refuse before constructing any index.
            for budget in [0, 1] {
                let limited = Memory::new(Arc::new(MemoryAccount::new(0, budget)));
                let lower = Access::new(&xml, xml.snapshot(), &limited);
                assert!(matches!(
                    material::index_with_tokens(&values, &carried, &lower),
                    Err(structured_ltv::material::IndexError::Resource { cause: MemoryError::WorkExceeded, .. })
                ));
                assert_eq!(limited.used(), 0);
            }
            let index = material::index_with_tokens(&values, &carried, &access).unwrap();
            let material::Value::Certificate(certificate) = &values.values()[0].value else { panic!("original header certificate") };
            let copies = index.certificate(certificate, &access).unwrap().collect::<Result<Vec<_>, _>>().unwrap();
            assert_eq!(copies.len(), 5);
            assert_eq!(copies[0].source(), structured_ltv::material::ArtifactSource::Material(0));
            for token in 0..2 {
                assert_eq!(copies[1 + token].source(), structured_ltv::material::ArtifactSource::TokenCertificate { token, certificate: 0 });
                assert_eq!(copies[3 + token].source(), structured_ltv::material::ArtifactSource::TokenRevocation(structured_ltv::material::CarriedOrigin { token, choice: 0 }));
                assert_eq!(copies[1 + token].embedded_certificate(), None);
                assert_eq!(copies[3 + token].embedded_certificate(), Some(0));
                let node = if token == 0 { first } else { second };
                for copy in [copies[1 + token], copies[3 + token]] {
                    assert_eq!(copy.location().node, node);
                    assert!(matches!(copy.placement(), material::Placement::TimestampToken { property, token: actual, role } if actual == node && property == tokens.sources()[token].property() && role == tokens.sources()[token].role()));
                }
            }
            let statuses = index.ocsp(&carried.basic_responses()[0].value, &access).unwrap().collect::<Result<Vec<_>, _>>().unwrap();
            assert_eq!(statuses.len(), 2);
            assert_eq!(statuses[0].location().node, first);
            assert_eq!(statuses[1].location().node, second);
            // Calibrate actual XML-only index capacity, then prove that exact
            // capacity cannot produce a successful truncated combined index.
            drop(index);
            let before = memory.used();
            let baseline = material::index(&values, &access).unwrap();
            let capacity = memory.used() - before;
            drop(baseline);
            let limited = Memory::new(Arc::new(MemoryAccount::new(capacity, 1_048_576)));
            let lower = Access::new(&xml, xml.snapshot(), &limited);
            drop(material::index(&values, &lower).unwrap());
            assert_eq!(limited.used(), 0);
            let Err(error) = material::index_with_tokens(&values, &carried, &lower) else { panic!("late index growth must refuse") };
            assert!(matches!(error, structured_ltv::material::IndexError::Resource { .. }));
            assert_eq!(limited.used(), 0);
            Ok::<_, ()>(())
        }).unwrap();
        Ok::<_, ()>(())
    }).unwrap();
    assert_eq!(memory.used(), held);
    drop(prepared);
    assert_eq!(memory.used(), 0);
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum ArchiveOrder {
    Late,
    Early,
    BaseEarly,
}

fn archive_material_fixture(
    order: ArchiveOrder,
) -> (Tree, usize, timestamp_oracle::Oracle, Vec<u8>, Vec<u8>) {
    // Synthetic XML framing isolates exact original archive selection; actual
    // P256/ESS/imprint verification below does not qualify XML C14N itself.
    struct Collect(Vec<u8>);
    impl Receiver for Collect {
        fn write(&mut self, bytes: &[u8]) -> Result<(), stream::Refused> {
            self.0.extend_from_slice(bytes);
            Ok(())
        }
    }
    let mut xml = preparation_tree();
    let early = order != ArchiveOrder::Late;
    let deferred = if early {
        append_crl_pair(&mut xml, order == ArchiveOrder::BaseEarly)
    } else {
        None
    };
    let archive = xml.nodes.len();
    xml.nodes
        .push(element(Some(7), XADES_141, "ArchiveTimeStamp"));
    xml.nodes
        .push(element(Some(archive), DS, "CanonicalizationMethod"));
    xml.nodes
        .push(element(Some(archive), XADES, "EncapsulatedTimeStamp"));
    if !early {
        assert!(append_crl_pair(&mut xml, false).is_none());
    }
    if let Some(delta) = deferred {
        let general = xml.nodes.len();
        xml.nodes
            .push(element(Some(7), XADES_141, "AnyValidationData"));
        let revocation = xml.nodes.len();
        xml.nodes
            .push(element(Some(general), XADES, "RevocationValues"));
        let group = xml.nodes.len();
        xml.nodes
            .push(element(Some(revocation), XADES, "CRLValues"));
        append_crl_value(&mut xml, group, delta);
    }
    let header = xml
        .nodes
        .iter()
        .position(|record| {
            matches!(
                record.value,
                Node::Element(Name {
                    namespace: DS,
                    local: "X509Certificate"
                })
            )
        })
        .unwrap();
    xml.binary_routes.push((header, candidate_certificate()));
    let (_, _, later_certificate, root) = timestamp_oracle::fixture_with_root(b"setup");
    let late = xml.nodes.len();
    xml.nodes.push(element(Some(7), XADES, "CertificateValues"));
    let late_value = xml.nodes.len();
    xml.nodes
        .push(element(Some(late), XADES, "EncapsulatedX509Certificate"));
    xml.binary_routes
        .push((late_value, later_certificate.clone()));
    if early {
        let value = xml.nodes.len();
        xml.nodes
            .push(element(xml.nodes[header].parent, DS, "X509Certificate"));
        xml.binary_routes.push((value, later_certificate));
        let value = xml.nodes.len();
        xml.nodes
            .push(element(xml.nodes[header].parent, DS, "X509Certificate"));
        xml.binary_routes.push((value, root));
    }
    let work = funded();
    let access = Access::new(&xml, xml.snapshot(), &work);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let stamp = timestamp::read(&access, &bound, archive).unwrap();
    let mut input = Input::prepare(&xml, &bound, &stamp, &work).unwrap();
    let mut octets = Collect(Vec::new());
    stream::Source::stream(&mut input, &mut octets, &work).unwrap();
    drop(input);
    let (provider, token, certificate, root) = timestamp_oracle::fixture_with_root(&octets.0);
    xml.binary = token;
    (xml, archive, provider, certificate, root)
}

fn append_crl_pair(xml: &mut Tree, defer_delta: bool) -> Option<Vec<u8>> {
    let property = xml.nodes.len();
    xml.nodes.push(element(Some(7), XADES, "RevocationValues"));
    let group = xml.nodes.len();
    xml.nodes.push(element(Some(property), XADES, "CRLValues"));
    let (base, delta) = timestamp_oracle::crl_pair();
    append_crl_value(xml, group, base);
    let deferred = if defer_delta {
        Some(delta)
    } else {
        append_crl_value(xml, group, delta);
        None
    };
    let group = xml.nodes.len();
    xml.nodes.push(element(Some(property), XADES, "OCSPValues"));
    let value = xml.nodes.len();
    xml.nodes
        .push(element(Some(group), XADES, "EncapsulatedOCSPValue"));
    xml.binary_routes
        .push((value, timestamp_oracle::ocsp_response(2)));
    deferred
}

fn append_crl_value(xml: &mut Tree, group: usize, bytes: Vec<u8>) {
    let value = xml.nodes.len();
    xml.nodes
        .push(element(Some(group), XADES, "EncapsulatedCRLValue"));
    xml.binary_routes.push((value, bytes));
}

#[test]
fn an_archive_tokens_own_certificate_cannot_complete_its_prior_material() {
    // Availability in the token being checked is too late to be part of its
    // own imprint. An earlier token here intentionally carries no certificate;
    // otherwise that legitimately covered earlier copy could satisfy lookup.
    use structured_cms::timestamp::CheckContext;
    use structured_pkix::{
        path::WorkingKey,
        temporal::{Instant, LeapTable},
    };
    let (mut xml, archive, provider, certificate_bytes, _) =
        archive_material_fixture(ArchiveOrder::Late);
    let prior = xml
        .nodes
        .iter()
        .position(|record| {
            record.parent == Some(8)
                && matches!(
                    record.value,
                    Node::Element(Name {
                        local: "EncapsulatedTimeStamp",
                        ..
                    })
                )
        })
        .unwrap();
    xml.binary_routes.push((
        prior,
        timestamp_oracle::fixture(b"unrelated earlier imprint").1,
    ));
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let certificate = structured_pkix::Certificate::read(&parser, &certificate_bytes, 400).unwrap();
    let utc = LeapTable::new(0, 2_000_000_000, &[]).unwrap();
    let context = CheckContext {
        certificate: &certificate,
        certificate_parent: &operation,
        key: WorkingKey::standing(certificate.tbs().public_key()),
        requested: None,
        utc: &utc,
        reference: Some(Instant::new(1_800_000_000, 0).unwrap()),
        window: None,
        policy_accuracy: None,
    };
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Archival, None).unwrap();
    let stamp = prepared
        .timestamps()
        .iter()
        .find(|stamp| stamp.node() == archive)
        .unwrap();
    let check = timestamp::Check {
        xml: &xml,
        provider: &provider,
        context: &context,
    };
    let evidence = check
        .retain(prepared.bound(), stamp, 0, &operation, |_, _| {
            Ok::<_, ()>(false)
        })
        .unwrap()
        .unwrap();
    let input = Input::prepare(&xml, prepared.bound(), stamp, &memory).unwrap();
    let coverage = input.element_coverage(&memory).unwrap();
    material::with_values(&xml,&prepared,&operation,|query,values| {
        timestamp::with_tokens(&xml,&prepared,query,timestamp::TokenProfile::Etsi,|query,tokens| {
            let carried=tokens.material_in(query).unwrap();
            let work=query.execution();let access=Access::new(&xml,xml.snapshot(),&work);
            let index=material::index_with_tokens(&values,&carried,&access).unwrap();
            let mut own=0;
            for copy in index.certificate(&certificate,&access).unwrap() {
                let copy=copy.unwrap();
                if matches!(copy.source(),structured_ltv::material::ArtifactSource::TokenCertificate {..}) {
                    assert!(matches!(copy.placement(),material::Placement::TimestampToken {property,..} if property==archive));
                    own+=1;
                }
            }
            assert_eq!(own,1);
            assert!(matches!(index.archived(structured_ltv::material::ArtifactRef::Certificate(&certificate),&coverage,&evidence,&access).unwrap(),material::Archived::Uncovered));
            Ok::<_,()>(())
        }).unwrap();
        Ok::<_,()>(())
    }).unwrap();
    assert_eq!(provider.verified.get(), 1);
}

#[test]
fn archive_artifact_lookup_requires_a_covered_copy_and_exact_original_identity() {
    // A later copy is not material present before this archive. Adding an early
    // duplicate can satisfy availability without changing the required bytes;
    // unknown DER and a foreign source/current resource refusal stay distinct.
    use structured_pkix::{
        path::WorkingKey,
        temporal::{Instant, LeapTable},
    };
    for order in [
        ArchiveOrder::Late,
        ArchiveOrder::Early,
        ArchiveOrder::BaseEarly,
    ] {
        let early = order != ArchiveOrder::Late;
        let (xml, archive, provider, certificate_bytes, root_bytes) =
            archive_material_fixture(order);
        let memory = funded();
        let parser = structured_asn1::Parser::der(memory.clone());
        let operation = parser.read(&[], 900).unwrap();
        let certificate =
            structured_pkix::Certificate::read(&parser, &certificate_bytes, 400).unwrap();
        let root = structured_pkix::Certificate::read(&parser, &root_bytes, 2000).unwrap();
        let parent = parser.read(&[], 400).unwrap();
        let utc = LeapTable::new(0, 2_000_000_000, &[]).unwrap();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        let prepared =
            prepare::prepare(&access, 0, unsigned::Requirements::Archival, None).unwrap();
        let stamp = prepared
            .timestamps()
            .iter()
            .find(|stamp| stamp.node() == archive)
            .unwrap();
        let actor = {
            let context = CheckContext {
                certificate: &certificate,
                certificate_parent: &parent,
                key: WorkingKey::standing(certificate.tbs().public_key()),
                requested: None,
                utc: &utc,
                reference: Some(Instant::new(1_800_000_000, 0).unwrap()),
                window: None,
                policy_accuracy: None,
            };
            let check = timestamp::Check {
                xml: &xml,
                provider: &provider,
                context: &context,
            };
            check
                .retain_actor(prepared.bound(), stamp, 0, &operation, |_, _| {
                    Ok::<_, ()>(false)
                })
                .unwrap()
                .unwrap()
        };
        // The successful actor retains the actual certificate/key, not the
        // temporary Check/CheckContext used to perform this one verification.
        assert!(core::ptr::eq(actor.certificate(), &raw const certificate));
        assert_eq!(
            actor.key().key().as_bytes().as_ptr(),
            certificate.tbs().public_key().as_bytes().as_ptr()
        );
        let path = assert_timestamp_path(&actor, &root, &operation, &utc);
        let evidence = actor.into_verified();
        let input = Input::prepare(&xml, prepared.bound(), stamp, &memory).unwrap();
        let coverage = input.element_coverage(&memory).unwrap();
        let mut missing = certificate_bytes.clone();
        *missing.last_mut().unwrap() ^= 1;
        let missing = structured_pkix::Certificate::read(&parser, &missing, 1600).unwrap();
        material::with_values(&xml,&prepared,&operation,|query,view| {
            let work=query.execution();let access=Access::new(&xml,xml.snapshot(),&work);
            let index=material::index(&view,&access).unwrap();
            assert_crl_archive(&view,&index,&coverage,&evidence,&access,(&certificate,&root,order),&xml);
            assert_ocsp_archive(&view,&index,&coverage,&evidence,&access,(&certificate,&root,early,&path),&xml);
            let artifact=structured_ltv::material::ArtifactRef::Certificate(&certificate);
            let census=index.archived_path(&path,&coverage,&evidence,&access).unwrap();
            assert!(core::ptr::eq(census.path(), &raw const path));
            assert_eq!(census.archive(),evidence.token());
            assert_eq!(census.all_certificates_covered(),early);
            assert_eq!(census.entries().len(),2);
            assert_eq!(census.entries()[0].role,structured_ltv::material::PathCertificate::Anchor);
            if early {assert!(matches!(census.entries()[0].availability,material::Archived::Present(_)));}
            else {assert!(matches!(census.entries()[0].availability,material::Archived::Missing));}
            assert_eq!(census.entries()[1].role,structured_ltv::material::PathCertificate::Member(0));
            if early {assert!(matches!(census.entries()[1].availability,material::Archived::Present(_)));}
            else {assert!(matches!(census.entries()[1].availability,material::Archived::Uncovered));}
            let found=index.archived(artifact,&coverage,&evidence,&access).unwrap();
            if early {assert!(matches!(found,material::Archived::Present(copy) if matches!(copy.placement(),material::Placement::KeyInfo(_))));}
            else {assert!(matches!(found,material::Archived::Uncovered));}
            assert!(matches!(index.archived(structured_ltv::material::ArtifactRef::Certificate(&missing),&coverage,&evidence,&access).unwrap(),material::Archived::Missing));
            let denied=Memory::new(Arc::new(MemoryAccount::new(0,0)));
            let denied=Access::new(&xml,xml.snapshot(),&denied);
            assert!(matches!(index.archived(artifact,&coverage,&evidence,&denied),Err(structured_ltv::material::ArchiveError::Index(structured_ltv::material::IndexError::Scope(_) | structured_ltv::material::IndexError::Resource {..}))));
            let no_capacity=Memory::new(Arc::new(MemoryAccount::new(0,1<<24)));
            let no_capacity_access=Access::new(&xml,xml.snapshot(),&no_capacity);
            assert!(matches!(index.archived_path(&path,&coverage,&evidence,&no_capacity_access),Err(structured_ltv::material::ArchiveError::Index(structured_ltv::material::IndexError::Resource {..}))));
            assert_eq!(no_capacity.used(),0);
            // Retry is a NEW operation with the unchanged test limits. It must
            // not borrow the nearly exhausted fuel of the preceding independent
            // archive/status assertions; the retained index stays on its source.
            // Failed allocation cannot turn that retry into an empty census.
            let retry=funded();
            let retry_access=Access::new(&xml,xml.snapshot(),&retry);
            assert_eq!(index.archived_path(&path,&coverage,&evidence,&retry_access).unwrap().entries().len(),2);
            let foreign=Access::new(&xml,Snapshot {document:xml.document+1,generation:xml.generation.get()},&work);
            assert!(matches!(index.archived(artifact,&coverage,&evidence,&foreign),Err(structured_ltv::material::ArchiveError::Index(structured_ltv::material::IndexError::Foreign {..}))));
            Ok::<_,()>(())
        }).unwrap();
        assert_eq!(provider.verified.get(), 1);
    }
}

fn assert_crl_archive<'source, W: Work + 'source, C: Work>(
    view: &material::View<'source, usize, u8, u8>,
    index: &material::Index<'source, usize, u8, u8, W>,
    coverage: &structured_ltv::material::Coverage<usize, u8, u8, C, structured_xades::Xades>,
    evidence: &structured_ltv::timestamp::Verified<usize, u8, u8, structured_xades::Xades>,
    access: &Access<'_, Tree, W>,
    actor: (
        &'source structured_pkix::Certificate<'source>,
        &'source structured_pkix::Certificate<'source>,
        ArchiveOrder,
    ),
    xml: &Tree,
) {
    // Actual issuer signatures and common complete/delta semantics select the
    // original lists. Retaining only the updating delta must never suffice.
    use structured_ltv::material::status::{self, StatusPart, StatusSource};
    use structured_pkix::crl::{
        Authentication, Coverage, DeltaBinding, DeltaEvidence, Evaluation, SourceOrigin,
    };
    let (target, issuer, order) = actor;
    let early = order != ArchiveOrder::Late;
    let lists: Vec<_> = view
        .values()
        .iter()
        .filter_map(|value| match &value.value {
            material::Value::Crl(crl) => Some(crl),
            _ => None,
        })
        .collect();
    assert_eq!(lists.len(), 2);
    let verifier = timestamp_oracle::PathOracle {
        calls: Cell::new(0),
    };
    let key = structured_pkix::path::WorkingKey::standing(issuer.tbs().public_key());
    let Authentication::Authenticated(base) = lists[0]
        .authenticate(issuer, key, &verifier, access.execution())
        .unwrap()
    else {
        panic!("actual base signature")
    };
    let Authentication::Authenticated(delta) = lists[1]
        .authenticate(issuer, key, &verifier, access.execution())
        .unwrap()
    else {
        panic!("actual delta signature")
    };
    let DeltaBinding::Bound(pair) = base.bind_delta(&delta, access.execution()).unwrap() else {
        panic!("same original issuer and scope")
    };
    let utc = structured_pkix::temporal::LeapTable::new(0, 2_000_000_000, &[]).unwrap();
    let time = Evaluation {
        now: structured_pkix::temporal::Instant::new(1_767_225_600, 0)
            .unwrap()
            .into(),
        utc: &utc,
        execution: access.execution(),
        origin: SourceOrigin::Cached,
    };
    let DeltaEvidence::Facts(facts) = pair.evaluate(target, &time).unwrap() else {
        panic!("applicable actual pair")
    };
    let qualified = Coverage::new(facts.absence().unwrap(), access.execution()).unwrap();
    assert!(core::ptr::eq(qualified.target(), target));
    assert_eq!(qualified.evaluated_at(), time.now);
    assert!(qualified.reasons().is_complete());
    let path = assert_crl_target(&qualified, issuer, &utc, access.execution());
    let report = status::archived(
        StatusSource::Crl(&qualified),
        index,
        coverage,
        evidence,
        access,
    )
    .unwrap();
    assert_eq!(report.archive(), evidence.token());
    assert!(
        matches!(report.source(),StatusSource::Crl(original) if core::ptr::eq(original,&raw const qualified))
    );
    assert_eq!(report.entries().len(), 4);
    assert_eq!(report.entries()[0].part, StatusPart::CompleteCrl);
    assert_eq!(report.entries()[1].part, StatusPart::DeltaCrl);
    for entry in report.entries() {
        assert_eq!(entry.source, 0);
        let covered = if entry.part == StatusPart::DeltaCrl {
            order == ArchiveOrder::Early
        } else {
            early
        };
        assert_eq!(
            matches!(entry.availability, material::Archived::Present(_)),
            covered
        );
    }
    assert_eq!(report.all_artifacts_covered(), order == ArchiveOrder::Early);
    assert_crl_path_archive(
        &path,
        &qualified,
        (&utc, order, xml),
        index,
        coverage,
        evidence,
        access,
    );
    assert_crl_archive_refusal(&qualified, index, coverage, evidence, access, xml);
    assert_eq!(verifier.calls.get(), 2);
}

fn assert_crl_archive_refusal<W: Work, C: Work>(
    qualified: &structured_pkix::crl::Coverage<'_, '_>,
    index: &material::Index<'_, usize, u8, u8, W>,
    coverage: &structured_ltv::material::Coverage<usize, u8, u8, C, structured_xades::Xades>,
    evidence: &structured_ltv::timestamp::Verified<usize, u8, u8, structured_xades::Xades>,
    access: &Access<'_, Tree, W>,
    xml: &Tree,
) {
    // Refused report allocation must remain terminal and release partial data.
    let denied = Memory::new(Arc::new(MemoryAccount::new(0, 1 << 24)));
    let denied_access = Access::new(xml, access.snapshot(), &denied);
    assert!(matches!(
        structured_ltv::material::status::archived(
            structured_ltv::material::status::StatusSource::Crl(qualified),
            index,
            coverage,
            evidence,
            &denied_access
        ),
        Err(structured_ltv::material::ArchiveError::Index(
            structured_ltv::material::IndexError::Resource { .. }
        ))
    ));
    assert_eq!(denied.used(), 0);
}

fn assert_crl_path_archive<'source, W: Work, C: Work>(
    path: &structured_ltv::pki::CheckedPath<'source>,
    qualified: &structured_pkix::crl::Coverage<'_, 'source>,
    expected: (
        &structured_pkix::temporal::LeapTable<'_>,
        ArchiveOrder,
        &Tree,
    ),
    index: &material::Index<'_, usize, u8, u8, W>,
    coverage: &structured_ltv::material::Coverage<usize, u8, u8, C, structured_xades::Xades>,
    evidence: &structured_ltv::timestamp::Verified<usize, u8, u8, structured_xades::Xades>,
    access: &Access<'_, Tree, W>,
) {
    // A complete actor census must use one actual path and all original status
    // contributors. Late delta material cannot disappear in a combined report.
    use structured_ltv::pki::{CrlAuthorities, CrlAuthority, CrlCoverageBinding, MemberStatus};
    let authorities = [CrlAuthorities::Delta {
        base: CrlAuthority::Anchor,
        delta: CrlAuthority::Anchor,
    }];
    let CrlCoverageBinding::Bound(bound) = path
        .bind_crl_coverage(0, qualified, &authorities, expected.0, access.execution())
        .unwrap()
    else {
        panic!("same actual complete/delta authorities")
    };
    let entries = [MemberStatus::Crl(&bound)];
    let inputs = path
        .status_census(qualified.evaluated_at(), &entries, access.execution())
        .unwrap();
    xml_digest::assert_participant_status_closure(path, &inputs);
    let whole =
        structured_ltv::material::status::archived_path(&inputs, index, coverage, evidence, access)
            .unwrap();
    assert_unusable_crl_readings(path, qualified, expected, index, coverage, evidence, access);
    assert!(core::ptr::eq(whole.inputs(), &raw const inputs));
    assert!(core::ptr::eq(whole.certificates().path(), path));
    assert_eq!(whole.certificates().entries().len(), 2);
    assert_eq!(whole.statuses().len(), 1);
    assert_eq!(whole.statuses()[0].entries().len(), 4);
    assert_eq!(
        whole.all_artifacts_covered(),
        expected.1 == ArchiveOrder::Early
    );
    {
        // The authority closure and archive use the same actual path/status
        // inputs. Complete/delta artifacts remain four separate obligations.
        use structured_ltv::pki::{AuthorityNode, close_authorities};
        let nodes = [AuthorityNode {
            statuses: &inputs,
            responders: &[],
        }];
        let roots = [&inputs];
        let closed = close_authorities(&roots, &nodes, access.execution()).unwrap();
        let report = structured_ltv::material::status::archived_authorities(
            &closed, index, coverage, evidence, access,
        )
        .unwrap();
        assert!(core::ptr::eq(report.inputs(), &raw const closed));
        assert_eq!(report.paths().len(), 1);
        assert!(core::ptr::eq(report.paths()[0].inputs(), &raw const inputs));
        assert_eq!(report.paths()[0].statuses()[0].entries().len(), 4);
        assert_eq!(
            report.all_artifacts_covered(),
            expected.1 == ArchiveOrder::Early
        );
    }
    let before = access.execution().memory().used();
    let probe = index
        .archived_path(path, coverage, evidence, access)
        .unwrap();
    let certificate_capacity = access.execution().memory().used() - before;
    drop(probe);
    assert_eq!(access.execution().memory().used(), before);
    // Admit exactly the certificate report, then refuse the status catalogue.
    // The parent report's capacity must be returned with no successful prefix.
    let limited = Memory::new(Arc::new(MemoryAccount::new(certificate_capacity, 1 << 24)));
    let limited_access = Access::new(expected.2, access.snapshot(), &limited);
    assert!(matches!(
        structured_ltv::material::status::archived_path(
            &inputs,
            index,
            coverage,
            evidence,
            &limited_access
        ),
        Err(structured_ltv::material::ArchiveError::Index(
            structured_ltv::material::IndexError::Resource { .. }
        ))
    ));
    assert_eq!(limited.used(), 0);
}

fn assert_crl_target<'a, W: Work + 'a>(
    coverage: &structured_pkix::crl::Coverage<'a, 'a>,
    issuer: &'a structured_pkix::Certificate<'a>,
    utc: &structured_pkix::temporal::LeapTable<'_>,
    work: &W,
) -> structured_ltv::pki::CheckedPath<'a> {
    // Shared absence already checked signature/scope/entries/time. Association
    // must additionally bind the actual target and each complete/delta signer
    // to this checked path's anchor, without checking CRL signatures again.
    use structured_ltv::pki::{
        self, CrlAuthorities, CrlAuthority, CrlBinding, CrlBindingError, CrlBindingFailure,
        PathOutcome,
    };
    use structured_pkix::path::{Anchor, Interval, ValidationBounds, ValidationContext};
    let verifier = timestamp_oracle::PathOracle {
        calls: Cell::new(0),
    };
    let context = ValidationContext {
        execution: work,
        interval: Interval::at_instant(coverage.evaluated_at().start()),
        policy: structured_pkix::path::Policy::default(),
        bounds: ValidationBounds::default(),
        verifier: &verifier,
        watch: &(),
        recognizer: &(),
    };
    let PathOutcome::Accepted(path) = pki::check_path(
        &[coverage.target()],
        Anchor::from_certificate(issuer),
        &context,
    )
    .unwrap() else {
        panic!("actual target chain")
    };
    let evidence = &coverage.sources()[0];
    let authorities = || CrlAuthorities::Delta {
        base: CrlAuthority::Anchor,
        delta: CrlAuthority::Anchor,
    };
    let CrlBinding::Bound(bound) = path
        .bind_crl(0, evidence, authorities(), utc, work)
        .unwrap()
    else {
        panic!("both actual original lists are signed by the selected anchor")
    };
    assert!(core::ptr::eq(bound.path(), &raw const path));
    assert_eq!(bound.member(), 0);
    assert!(core::ptr::eq(bound.evidence().target(), coverage.target()));
    assert!(matches!(bound.authorities(), CrlAuthorities::Delta { .. }));
    assert!(matches!(
        path.bind_crl(1, evidence, authorities(), utc, work),
        Err(CrlBindingError::MemberIndex { index: 1 })
    ));
    assert!(matches!(
        path.bind_crl(
            0,
            evidence,
            CrlAuthorities::Complete(CrlAuthority::Anchor),
            utc,
            work
        ),
        Err(CrlBindingError::SourceKind)
    ));
    assert!(matches!(
        path.bind_crl(
            0,
            evidence,
            CrlAuthorities::Delta {
                base: CrlAuthority::Anchor,
                delta: CrlAuthority::Path(&path)
            },
            utc,
            work
        )
        .unwrap(),
        CrlBinding::Rejected(CrlBindingFailure::Signer)
    ));
    let denied = Memory::new(Arc::new(MemoryAccount::new(0, 0)));
    assert!(matches!(
        path.bind_crl(0, evidence, authorities(), utc, &denied),
        Err(CrlBindingError::Resource(_))
    ));
    assert_eq!(verifier.calls.get(), 1);
    path
}

#[test]
fn crl_authority_paths_require_same_anchor_and_full_evaluation_reading() {
    // A valid certificate path and a valid CRL signature under its actual key
    // are insufficient if that path uses a different anchor. Correct same-root
    // authorities must also cover the original status-evaluation reading.
    use structured_ltv::pki::{
        self, CrlAuthorities, CrlAuthority, CrlBinding, CrlBindingFailure, PathOutcome,
    };
    use structured_pkix::{
        Certificate, crl,
        path::{Anchor, Interval, ValidationBounds, ValidationContext},
        temporal::{Instant, LeapTable},
    };
    #[cfg(miri)]
    eprintln!("CRL association: generating and verifying original target path");
    xml_digest::with_checked_path(|target_path, _, original_root, work| {
        #[cfg(miri)]
        eprintln!("CRL association: target path accepted");
        let target = target_path.members()[0].certificate();
        let bytes = timestamp_oracle::complete_crl_for(5);
        let parser = structured_asn1::Parser::der(work.clone());
        let list = crl::CertificateList::read(&parser, &bytes, 2000).unwrap();
        let utc = LeapTable::new(0, 2_000_000_000, &[]).unwrap();
        let now = Instant::new(1_767_225_600, 0).unwrap();
        for root_seed in [2, 4] {
            #[cfg(miri)]
            eprintln!("CRL association: creating authority certified by root{root_seed}");
            let (signer_bytes, anchor_bytes) =
                timestamp_oracle::crl_authority_with_root(root_seed, original_root);
            let signer = Certificate::read(&parser, &signer_bytes, 3000).unwrap();
            let anchor = Certificate::read(&parser, &anchor_bytes, 4000).unwrap();
            let verifier = timestamp_oracle::PathOracle {
                calls: Cell::new(0),
            };
            let key = structured_pkix::path::WorkingKey::standing(signer.tbs().public_key());
            let crl::Authentication::Authenticated(source) =
                list.authenticate(&signer, key, &verifier, work).unwrap()
            else {
                panic!("real CRL signature")
            };
            let time = crl::Evaluation {
                now: now.into(),
                utc: &utc,
                execution: work,
                origin: crl::SourceOrigin::Cached,
            };
            let crl::CompleteEvidence::Facts(facts) = source.evaluate(target, &time).unwrap()
            else {
                panic!("actual complete CRL scope")
            };
            let evidence = facts.absence().unwrap();
            let context = ValidationContext {
                execution: work,
                interval: Interval::at_instant(now),
                policy: structured_pkix::path::Policy::default(),
                bounds: ValidationBounds::default(),
                verifier: &verifier,
                watch: &(),
                recognizer: &(),
            };
            let signer_path =
                match pki::check_path(&[&signer], Anchor::from_certificate(&anchor), &context)
                    .unwrap()
                {
                    PathOutcome::Accepted(path) => path,
                    PathOutcome::Rejected(reason) => {
                        panic!("actual CRL signer path root{root_seed}: {reason:?}")
                    }
                    PathOutcome::Incomplete(reason) => panic!("actual CRL signer path: {reason:?}"),
                };
            let result = target_path
                .bind_crl(
                    0,
                    &evidence,
                    CrlAuthorities::Complete(CrlAuthority::Path(&signer_path)),
                    &utc,
                    work,
                )
                .unwrap();
            if root_seed == 4 {
                assert!(matches!(
                    result,
                    CrlBinding::Rejected(CrlBindingFailure::Anchor)
                ));
                assert_eq!(verifier.calls.get(), 2);
                continue;
            }
            let CrlBinding::Bound(bound) = result else {
                panic!("same-anchor original authority")
            };
            assert!(core::ptr::eq(bound.evidence(), &raw const evidence));
            let signer_status = crl_signer_status(&source, &signer, &time);
            assert_crl_authority_closure(
                target_path,
                &signer_path,
                &evidence,
                &signer_status,
                &utc,
                work,
            );
            assert_crl_time_reading(target_path, &evidence, &signer_path, &utc, work);
            assert_eq!(verifier.calls.get(), 2);
        }
    });
}

fn crl_signer_status<'r, 'a, W: Work>(
    source: &structured_pkix::crl::Authenticated<'r, 'a>,
    signer: &'r structured_pkix::Certificate<'a>,
    time: &structured_pkix::crl::Evaluation<'_, W>,
) -> structured_pkix::crl::AbsenceEvidence<'r, 'a> {
    // Reuse the already authenticated original list to evaluate its actual
    // signing certificate; there is no second parse or signature verification.
    let structured_pkix::crl::CompleteEvidence::Facts(facts) =
        source.evaluate(signer, time).unwrap()
    else {
        panic!("actual signer status scope")
    };
    facts.absence().unwrap()
}

fn assert_crl_authority_closure<'a, W: Work>(
    target: &structured_ltv::pki::CheckedPath<'a>,
    signer: &structured_ltv::pki::CheckedPath<'a>,
    target_status: &structured_pkix::crl::AbsenceEvidence<'_, 'a>,
    signer_status: &structured_pkix::crl::AbsenceEvidence<'_, 'a>,
    utc: &structured_pkix::temporal::LeapTable<'_>,
    work: &W,
) {
    // Both actual certificates use the same authenticated CRL. The signer
    // path's self-dependency must terminate by receipt identity, not recurse
    // or replay verification; omitting that signer census is not completion.
    use structured_ltv::pki::{
        AuthorityClosureError, AuthorityNode, AuthorityOrigin, CrlAuthorities, CrlAuthority,
        CrlCoverageBinding, MemberStatus, close_authorities,
    };
    use structured_pkix::crl::Coverage;
    let target_sources = Coverage::new(*target_status, work).unwrap();
    let signer_sources = Coverage::new(*signer_status, work).unwrap();
    let authorities = [CrlAuthorities::Complete(CrlAuthority::Path(signer))];
    let CrlCoverageBinding::Bound(target_bound) = target
        .bind_crl_coverage(0, &target_sources, &authorities, utc, work)
        .unwrap()
    else {
        panic!("actual target CRL authority")
    };
    let CrlCoverageBinding::Bound(signer_bound) = signer
        .bind_crl_coverage(0, &signer_sources, &authorities, utc, work)
        .unwrap()
    else {
        panic!("actual signer CRL authority")
    };
    let target_entries = [MemberStatus::Crl(&target_bound)];
    let signer_entries = [MemberStatus::Crl(&signer_bound)];
    let parent = target
        .status_census(target_sources.evaluated_at(), &target_entries, work)
        .unwrap();
    let child = signer
        .status_census(signer_sources.evaluated_at(), &signer_entries, work)
        .unwrap();
    let nodes = [
        AuthorityNode {
            statuses: &parent,
            responders: &[],
        },
        AuthorityNode {
            statuses: &child,
            responders: &[],
        },
    ];
    xml_digest::assert_participant_nodes(target, &parent, &nodes);
    let roots = [&parent];
    let before = work.memory().used();
    {
        let closure = close_authorities(&roots, &nodes, work).unwrap();
        assert_eq!(closure.nodes().len(), 2);
        assert!(core::ptr::eq(closure.nodes()[1].statuses.path(), signer));
    }
    assert_eq!(work.memory().used(), before);
    assert!(matches!(
        close_authorities(&roots, &nodes[..1], work),
        Err(AuthorityClosureError::MissingPath {
            origin: AuthorityOrigin::Crl {
                node: 0,
                member: 0,
                source: 0,
                delta: false
            }
        })
    ));
    assert_eq!(work.memory().used(), before);
}

fn assert_crl_time_reading<'a, W: Work + 'a>(
    target_path: &structured_ltv::pki::CheckedPath<'a>,
    evidence: &structured_pkix::crl::AbsenceEvidence<'_, 'a>,
    signer_path: &structured_ltv::pki::CheckedPath<'a>,
    utc: &structured_pkix::temporal::LeapTable<'_>,
    work: &W,
) {
    // Missing UTC coverage is a processing uncertainty; a later valid path is
    // a completed interval mismatch. Neither can assert authority at the
    // original status-reading instant, and the original CRL is not reverified.
    use structured_ltv::pki::{
        self, CrlAuthorities, CrlAuthority, CrlBinding, CrlBindingError, CrlBindingFailure,
        PathOutcome,
    };
    use structured_pkix::{
        path::{Interval, ValidationBounds, ValidationContext},
        temporal::LeapTable,
    };
    assert_crl_foreign_status(target_path, evidence, signer_path, utc, work);
    let now = evidence.evaluated_at().start();
    let unknown = LeapTable::new(now.seconds() + 1, 2_000_000_000, &[]).unwrap();
    assert!(matches!(
        target_path.bind_crl(
            0,
            evidence,
            CrlAuthorities::Complete(CrlAuthority::Path(signer_path)),
            &unknown,
            work
        ),
        Err(CrlBindingError::Time(_))
    ));
    let verifier = timestamp_oracle::PathOracle {
        calls: Cell::new(0),
    };
    let later = ValidationContext {
        execution: work,
        interval: Interval::at(now.seconds() + 1),
        policy: structured_pkix::path::Policy::default(),
        bounds: ValidationBounds::default(),
        verifier: &verifier,
        watch: &(),
        recognizer: &(),
    };
    let signer = signer_path.members().last().unwrap().certificate();
    let PathOutcome::Accepted(later_path) =
        pki::check_path(&[signer], signer_path.anchor(), &later).unwrap()
    else {
        panic!("same signer valid a second later")
    };
    assert!(matches!(
        target_path
            .bind_crl(
                0,
                evidence,
                CrlAuthorities::Complete(CrlAuthority::Path(&later_path)),
                utc,
                work
            )
            .unwrap(),
        CrlBinding::Rejected(CrlBindingFailure::Interval)
    ));
    assert_eq!(verifier.calls.get(), 1);
}

fn assert_crl_foreign_status<'a, W: Work>(
    target_path: &structured_ltv::pki::CheckedPath<'a>,
    evidence: &structured_pkix::crl::AbsenceEvidence<'_, 'a>,
    signer_path: &structured_ltv::pki::CheckedPath<'a>,
    utc: &structured_pkix::temporal::LeapTable<'_>,
    work: &W,
) {
    // Equal member counts cannot relabel another actor's original status.
    // Reuse the already authenticated source and checked authority path.
    use structured_ltv::pki::{
        CrlAuthorities, CrlAuthority, CrlCoverageBinding, MemberStatus, StatusCensusError,
    };
    let coverage = structured_pkix::crl::Coverage::new(*evidence, work).unwrap();
    let authorities = [CrlAuthorities::Complete(CrlAuthority::Path(signer_path))];
    let CrlCoverageBinding::Bound(bound) = target_path
        .bind_crl_coverage(0, &coverage, &authorities, utc, work)
        .unwrap()
    else {
        panic!("same actual original target and signer")
    };
    let entries = [MemberStatus::Crl(&bound)];
    assert!(matches!(
        signer_path.status_census(coverage.evaluated_at(), &entries, work),
        Err(StatusCensusError::Member { index: 0 })
    ));
}

fn assert_unusable_crl_readings<'source, W: Work, C: Work>(
    path: &structured_ltv::pki::CheckedPath<'source>,
    qualified: &structured_pkix::crl::Coverage<'_, 'source>,
    expected: (
        &structured_pkix::temporal::LeapTable<'_>,
        ArchiveOrder,
        &Tree,
    ),
    index: &material::Index<'_, usize, u8, u8, W>,
    coverage: &structured_ltv::material::Coverage<usize, u8, u8, C, structured_xades::Xades>,
    evidence: &structured_ltv::timestamp::Verified<usize, u8, u8, structured_xades::Xades>,
    access: &Access<'_, Tree, W>,
) {
    // Actual stale processing facts cannot qualify absence, but still retain
    // their exact target/time, original whole lists and signing authorities.
    use structured_ltv::pki::CrlReading;
    use structured_pkix::{crl, temporal::Instant};
    let crl::EvidenceSource::Delta { base, delta } = qualified.sources()[0].source() else {
        panic!("original pair")
    };
    let time = crl::Evaluation {
        now: Instant::new(1_925_000_000, 0).unwrap().into(),
        utc: expected.0,
        execution: access.execution(),
        origin: crl::SourceOrigin::Cached,
    };
    let crl::CompleteEvidence::Facts(complete) = base.evaluate(qualified.target(), &time).unwrap()
    else {
        panic!("same applicable original base")
    };
    assert_eq!(complete.freshness(), crl::Freshness::Stale);
    assert!(complete.absence().is_none());
    assert_crl_reading_archive(
        path,
        CrlReading::Complete(&complete),
        (expected.0, expected.1 != ArchiveOrder::Late, 2),
        index,
        coverage,
        evidence,
        access,
    );
    let crl::DeltaBinding::Bound(pair) = base.bind_delta(&delta, access.execution()).unwrap()
    else {
        panic!("same actual base/delta pair")
    };
    let crl::DeltaEvidence::Facts(combined) = pair.evaluate(qualified.target(), &time).unwrap()
    else {
        panic!("same applicable original pair")
    };
    assert_eq!(combined.delta_freshness(), crl::Freshness::Stale);
    assert!(combined.absence().is_none());
    assert_crl_reading_archive(
        path,
        CrlReading::Delta(&combined),
        (expected.0, expected.1 == ArchiveOrder::Early, 4),
        index,
        coverage,
        evidence,
        access,
    );
    assert_revoked_crl_reading_archive(
        path, qualified, expected.0, index, coverage, evidence, access,
    );
}

fn assert_revoked_crl_reading_archive<'source, W: Work, C: Work>(
    path: &structured_ltv::pki::CheckedPath<'source>,
    qualified: &structured_pkix::crl::Coverage<'_, 'source>,
    utc: &structured_pkix::temporal::LeapTable<'_>,
    index: &material::Index<'_, usize, u8, u8, W>,
    coverage: &structured_ltv::material::Coverage<usize, u8, u8, C, structured_xades::Xades>,
    evidence: &structured_ltv::timestamp::Verified<usize, u8, u8, structured_xades::Xades>,
    access: &Access<'_, Tree, W>,
) {
    // A current CRL with an actual revoked entry is not absence. Its missing
    // original encoding cannot be substituted by the different carried CRL
    // having the same issuer/update fields but no revoked serial.
    use structured_pkix::crl;
    let crl::EvidenceSource::Delta { base, .. } = qualified.sources()[0].source() else {
        panic!("original issuer")
    };
    let work = access.execution();
    let bytes = timestamp_oracle::revoking_crl_for(2, 1);
    let parser = structured_asn1::Parser::der(work.memory().clone());
    let list = crl::CertificateList::read(&parser, &bytes, 12000).unwrap();
    let verifier = timestamp_oracle::PathOracle {
        calls: Cell::new(0),
    };
    let crl::Authentication::Authenticated(source) = list
        .authenticate(base.issuer(), base.key(), &verifier, work)
        .unwrap()
    else {
        panic!("actual revoked-list signature")
    };
    let time = crl::Evaluation {
        now: qualified.evaluated_at(),
        utc,
        execution: work,
        origin: crl::SourceOrigin::Cached,
    };
    let crl::CompleteEvidence::Facts(facts) = source.evaluate(qualified.target(), &time).unwrap()
    else {
        panic!("same actual applicable target")
    };
    assert_eq!(facts.freshness(), crl::Freshness::Current);
    assert!(matches!(facts.entry(), crl::EntryLookup::Found(_)));
    assert!(facts.absence().is_none());
    assert_crl_reading_archive(
        path,
        structured_ltv::pki::CrlReading::Complete(&facts),
        (utc, false, 2),
        index,
        coverage,
        evidence,
        access,
    );
    let crl::CompleteEvidence::Facts(other) = source.evaluate(base.issuer(), &time).unwrap() else {
        panic!("same-named original root target")
    };
    assert!(matches!(
        path.bind_crl_reading(
            0,
            structured_ltv::pki::CrlReading::Complete(&other),
            structured_ltv::pki::CrlAuthorities::Complete(
                structured_ltv::pki::CrlAuthority::Anchor
            ),
            utc,
            work
        )
        .unwrap(),
        structured_ltv::pki::CrlReadingBinding::Rejected(
            structured_ltv::pki::CrlBindingFailure::Target
        )
    ));
    assert_eq!(verifier.calls.get(), 1);
}

fn assert_crl_reading_archive<'source, W: Work, C: Work>(
    path: &structured_ltv::pki::CheckedPath<'source>,
    reading: structured_ltv::pki::CrlReading<'_, '_, 'source>,
    expected: (&structured_pkix::temporal::LeapTable<'_>, bool, usize),
    index: &material::Index<'_, usize, u8, u8, W>,
    coverage: &structured_ltv::material::Coverage<usize, u8, u8, C, structured_xades::Xades>,
    evidence: &structured_ltv::timestamp::Verified<usize, u8, u8, structured_xades::Xades>,
    access: &Access<'_, Tree, W>,
) {
    // Archive availability and negative status facts are independent. The
    // source/authority binder is the same for complete and paired readings.
    use structured_ltv::pki::{
        AuthorityNode, CrlAuthorities, CrlAuthority, CrlReading, CrlReadingBinding, MemberStatus,
        close_authorities,
    };
    let authorities = match reading {
        CrlReading::Complete(_) => CrlAuthorities::Complete(CrlAuthority::Anchor),
        CrlReading::Delta(_) => CrlAuthorities::Delta {
            base: CrlAuthority::Anchor,
            delta: CrlAuthority::Anchor,
        },
    };
    let work = access.execution();
    let wrong_kind = match reading {
        CrlReading::Complete(_) => CrlAuthorities::Delta {
            base: CrlAuthority::Anchor,
            delta: CrlAuthority::Anchor,
        },
        CrlReading::Delta(_) => CrlAuthorities::Complete(CrlAuthority::Anchor),
    };
    assert!(matches!(
        path.bind_crl_reading(0, reading, wrong_kind, expected.0, work),
        Err(structured_ltv::pki::CrlBindingError::SourceKind)
    ));
    assert!(matches!(
        path.bind_crl_reading(1, reading, authorities, expected.0, work),
        Err(structured_ltv::pki::CrlBindingError::MemberIndex { index: 1 })
    ));
    let CrlReadingBinding::Bound(bound) = path
        .bind_crl_reading(0, reading, authorities, expected.0, work)
        .unwrap()
    else {
        panic!("same actual negative result and authorities")
    };
    assert!(core::ptr::eq(bound.reading().target(), reading.target()));
    let entries = [MemberStatus::CrlReading(&bound)];
    let inputs = path
        .status_census(reading.evaluated_at(), &entries, work)
        .unwrap();
    let nodes = [AuthorityNode {
        statuses: &inputs,
        responders: &[],
    }];
    let roots = [&inputs];
    let closed = close_authorities(&roots, &nodes, work).unwrap();
    let report = structured_ltv::material::status::archived_authorities(
        &closed, index, coverage, evidence, access,
    )
    .unwrap();
    assert_eq!(report.all_artifacts_covered(), expected.1);
    assert_eq!(report.paths()[0].statuses()[0].entries().len(), expected.2);
    assert!(matches!(
        report.paths()[0].statuses()[0].source(),
        structured_ltv::material::status::StatusSource::CrlReading(_)
    ));
    assert_eq!(
        report.paths()[0].statuses()[0].source().evaluated_at(),
        reading.evaluated_at()
    );
    let empty = Memory::new(Arc::new(MemoryAccount::new(0, 0)));
    assert!(matches!(
        path.bind_crl_reading(0, reading, authorities, expected.0, &empty),
        Err(structured_ltv::pki::CrlBindingError::Resource(_))
    ));
}

fn assert_ocsp_archive<'source, W: Work + 'source, C: Work>(
    view: &material::View<'source, usize, u8, u8>,
    index: &material::Index<'source, usize, u8, u8, W>,
    coverage: &structured_ltv::material::Coverage<usize, u8, u8, C, structured_xades::Xades>,
    evidence: &structured_ltv::timestamp::Verified<usize, u8, u8, structured_xades::Xades>,
    access: &Access<'_, Tree, W>,
    actor: (
        &'source structured_pkix::Certificate<'source>,
        &'source structured_pkix::Certificate<'source>,
        bool,
        &'source structured_ltv::pki::CheckedPath<'source>,
    ),
    xml: &Tree,
) {
    // The actual whole response, signer binding, issuer scope and CertID
    // matching run through the common kernel; the archive needs the envelope.
    use structured_ltv::material::status::{self, StatusPart, StatusSource};
    // This independent OCSP query keeps the same per-operation limits instead
    // of spending fuel left by the preceding complete/delta CRL assertions.
    let operation = funded();
    let access = &Access::new(xml, access.snapshot(), &operation);
    let (target, issuer, early, path) = actor;
    assert_eq!(view.basic_responses().len(), 1);
    let response = view.basic_responses()[0].response();
    let qualified = checked_ocsp(response, target, issuer, access.execution(), false);
    assert_ocsp_target(path, &qualified, access.execution());
    let configured = checked_ocsp(response, target, issuer, access.execution(), true);
    assert_ocsp_target(path, &configured, access.execution());
    assert!(configured.evidence().source().issuing_key().is_none());
    if early {
        assert_no_check_archive(path, index, coverage, evidence, access);
    }
    let report = status::archived(
        StatusSource::Ocsp(&qualified),
        index,
        coverage,
        evidence,
        access,
    )
    .unwrap();
    assert!(core::ptr::eq(report.source().target(), target));
    assert_eq!(report.entries().len(), 2);
    assert_eq!(report.entries()[0].part, StatusPart::OcspResponse);
    assert_eq!(report.entries()[1].part, StatusPart::SigningCertificate);
    assert_eq!(report.all_artifacts_covered(), early);
    let denied = Memory::new(Arc::new(MemoryAccount::new(0, 1 << 24)));
    let denied_access = Access::new(xml, access.snapshot(), &denied);
    assert!(matches!(
        status::archived(
            StatusSource::Ocsp(&qualified),
            index,
            coverage,
            evidence,
            &denied_access
        ),
        Err(structured_ltv::material::ArchiveError::Index(
            structured_ltv::material::IndexError::Resource { .. }
        ))
    ));
    assert_eq!(denied.used(), 0);
    for entry in report.entries() {
        assert_eq!(
            matches!(entry.availability, material::Archived::Present(_)),
            early
        );
    }
}

fn assert_no_check_archive<W: Work, C: Work, Q: Work>(
    target: &structured_ltv::pki::CheckedPath<'_>,
    index: &material::Index<'_, usize, u8, u8, W>,
    coverage: &structured_ltv::material::Coverage<usize, u8, u8, C, structured_xades::Xades>,
    evidence: &structured_ltv::timestamp::Verified<usize, u8, u8, structured_xades::Xades>,
    access: &Access<'_, Tree, Q>,
) {
    // An actually exempt responder needs its original certificate in archive
    // material, but no fabricated status. Here that certificate is absent from
    // XML: skipping its revocation lookup must not make availability positive.
    use structured_ltv::pki::{
        self, AuthorityNode, MemberStatus, OcspBinding, PathOutcome, ResponderBinding,
        close_authorities,
    };
    use structured_pkix::{
        Certificate, ocsp,
        path::{Anchor, Interval, ValidationBounds, ValidationContext, WorkingKey},
        temporal::{Instant, LeapTable},
    };
    let work = access.execution();
    let parser = structured_asn1::Parser::der(work.memory().clone());
    let (bytes, root_bytes) = timestamp_oracle::ocsp_delegate_no_check(2);
    let delegate = Certificate::read(&parser, &bytes, 9000).unwrap();
    let root = Certificate::read(&parser, &root_bytes, 10000).unwrap();
    let verifier = timestamp_oracle::PathOracle {
        calls: Cell::new(0),
    };
    let utc = LeapTable::new(0, 2_000_000_000, &[]).unwrap();
    let now = Instant::new(1_767_225_600, 0).unwrap();
    let context = ValidationContext {
        execution: work,
        interval: Interval::at_instant(now),
        policy: structured_pkix::path::Policy::default(),
        bounds: ValidationBounds::default(),
        verifier: &verifier,
        watch: &(),
        recognizer: &(),
    };
    let ocsp::Delegation::Accepted(local) = ocsp::check_delegate(
        &delegate,
        &root,
        WorkingKey::standing(root.tbs().public_key()),
        &context,
    )
    .unwrap() else {
        panic!("actual noCheck delegation")
    };
    let bytes = timestamp_oracle::ocsp_response_signed(2, 5);
    let query = parser.read(&[], 11000).unwrap();
    let ocsp::Response::Successful(envelope) =
        ocsp::Response::read(&parser, &bytes, 11000).unwrap()
    else {
        panic!("actual whole response")
    };
    let response = envelope.basic_in(&query).unwrap().unwrap();
    let scoped = authenticate_delegate(&response, &local, &verifier, work);
    let request = ocsp::Request {
        target: target.members()[0].certificate(),
        nonce: None,
        intended_signer: Some(&delegate),
    };
    let time = ocsp::Evaluation {
        now: now.into(),
        utc: &utc,
        execution: work,
    };
    let read = scoped
        .evaluate_with_target(&request, &time, &verifier)
        .unwrap();
    let facts = read.evidence().unwrap();
    let OcspBinding::Bound(bound) = target.bind_ocsp(0, &facts, work).unwrap() else {
        panic!("original target")
    };
    let PathOutcome::Accepted(path) =
        pki::check_path(&[&delegate], Anchor::from_certificate(&root), &context).unwrap()
    else {
        panic!("actual full responder path")
    };
    let ResponderBinding::Bound(responder) = bound.bind_responder_path(&path, &utc, work).unwrap()
    else {
        panic!("original root/time")
    };
    let entries = [MemberStatus::ResponderNoCheck(&responder)];
    let inputs = path
        .status_census(facts.evaluated_at(), &entries, work)
        .unwrap();
    let nodes = [AuthorityNode {
        statuses: &inputs,
        responders: &[],
    }];
    let roots = [&inputs];
    let closed = close_authorities(&roots, &nodes, work).unwrap();
    let before = verifier.calls.get();
    let material = structured_ltv::material::status::archived_authorities(
        &closed, index, coverage, evidence, access,
    )
    .unwrap();
    assert!(!material.all_artifacts_covered());
    assert_eq!(material.paths().len(), 1);
    assert!(material.paths()[0].statuses().is_empty());
    assert_eq!(material.paths()[0].certificates().entries().len(), 2);
    assert!(matches!(
        material.paths()[0].certificates().entries()[1].availability,
        material::Archived::Missing
    ));
    assert_eq!(verifier.calls.get(), before);
}

fn checked_ocsp<'a, W: Work + 'a>(
    response: &'a structured_pkix::ocsp::BasicResponse<'a>,
    target: &'a structured_pkix::Certificate<'a>,
    issuer: &'a structured_pkix::Certificate<'a>,
    execution: &W,
    configured: bool,
) -> structured_pkix::ocsp::BoundEvidence<'a, 'a, 'a, 'a> {
    // The result must survive this temporary provider/request/UTC/context.
    use structured_pkix::ocsp::{self, AuthorityBinding, SignatureAuthentication};
    let verifier = timestamp_oracle::PathOracle {
        calls: Cell::new(0),
    };
    let key = structured_pkix::path::WorkingKey::standing(issuer.tbs().public_key());
    let SignatureAuthentication::Signed(signed) = response
        .authenticate_signature(issuer, key, &verifier, &verifier, execution)
        .unwrap()
    else {
        panic!("actual OCSP issuer signature")
    };
    let scoped = if configured {
        signed.trust_for_issuer(issuer)
    } else {
        let AuthorityBinding::Bound(scoped) = signed.bind_issuer(issuer, key, execution).unwrap()
        else {
            panic!("actual target issuer")
        };
        scoped
    };
    let utc = structured_pkix::temporal::LeapTable::new(0, 2_000_000_000, &[]).unwrap();
    let time = ocsp::Evaluation {
        now: structured_pkix::temporal::Instant::new(1_767_225_600, 0)
            .unwrap()
            .into(),
        utc: &utc,
        execution,
    };
    let request = ocsp::Request {
        target,
        nonce: None,
        intended_signer: Some(issuer),
    };
    let reading = scoped
        .evaluate_with_target(&request, &time, &verifier)
        .unwrap();
    let qualified = reading.evidence().expect("actual requested certificate");
    assert!(core::ptr::eq(qualified.target(), target));
    assert_eq!(
        qualified.evidence().assertion().status(),
        ocsp::Status::Good
    );
    assert_eq!(verifier.calls.get(), 1);
    qualified
}

#[test]
fn delegated_ocsp_requires_original_root_signer_and_full_status_reading() {
    // A real locally delegated OCSP signer is not a full ancestor-path proof.
    // Bind only the exact original signer under the target root at the original
    // evaluation Reading; preserve unresolved revocation and reject relabeling.
    assert_delegated_ocsp_path(false);
    assert_delegated_ocsp_path(true);
}

fn assert_delegated_ocsp_path(no_check: bool) {
    // Both actual extension branches preserve the common revocation fact.
    // noCheck never exempts the responder's original-root path/time binding.
    use structured_ltv::pki::{self, OcspBinding, PathOutcome, ResponderBinding, ResponderFailure};
    use structured_pkix::{
        Certificate, ocsp,
        path::{Anchor, Interval, ValidationBounds, ValidationContext, WorkingKey},
        temporal::{Instant, LeapTable},
    };
    xml_digest::with_checked_path(|target_path, _, _, work| {
        let parser = structured_asn1::Parser::der(work.clone());
        let (delegate_bytes, root_bytes) = if no_check {
            timestamp_oracle::ocsp_delegate_no_check(2)
        } else {
            timestamp_oracle::ocsp_delegate(2)
        };
        let delegate = Certificate::read(&parser, &delegate_bytes, 3000).unwrap();
        let root = Certificate::read(&parser, &root_bytes, 4000).unwrap();
        let verifier = timestamp_oracle::PathOracle {
            calls: Cell::new(0),
        };
        let now = Instant::new(1_767_225_600, 0).unwrap();
        let utc = LeapTable::new(0, 2_000_000_000, &[]).unwrap();
        let context = ValidationContext {
            execution: work,
            interval: Interval::at_instant(now),
            policy: structured_pkix::path::Policy::default(),
            bounds: ValidationBounds::default(),
            verifier: &verifier,
            watch: &(),
            recognizer: &(),
        };
        let ocsp::Delegation::Accepted(local) = ocsp::check_delegate(
            &delegate,
            &root,
            WorkingKey::standing(root.tbs().public_key()),
            &context,
        )
        .unwrap() else {
            panic!("real direct OCSP delegation")
        };
        let bytes = timestamp_oracle::ocsp_response_signed(2, 5);
        let ocsp::Response::Successful(envelope) =
            ocsp::Response::read(&parser, &bytes, 5000).unwrap()
        else {
            panic!("original response envelope")
        };
        let query = parser.read(&[], 5000).unwrap();
        let response = envelope.basic_in(&query).unwrap().unwrap();
        let scoped = authenticate_delegate(&response, &local, &verifier, work);
        let request = ocsp::Request {
            target: target_path.members()[0].certificate(),
            nonce: None,
            intended_signer: Some(&delegate),
        };
        let time = ocsp::Evaluation {
            now: now.into(),
            utc: &utc,
            execution: work,
        };
        let reading = scoped
            .evaluate_with_target(&request, &time, &verifier)
            .unwrap();
        let facts = reading.evidence().unwrap();
        let OcspBinding::Bound(target) = target_path.bind_ocsp(0, &facts, work).unwrap() else {
            panic!("actual target and issuing key")
        };
        let PathOutcome::Accepted(path) =
            pki::check_path(&[&delegate], Anchor::from_certificate(&root), &context).unwrap()
        else {
            panic!("full responder path")
        };
        let before = verifier.calls.get();
        let ResponderBinding::Bound(bound) = target.bind_responder_path(&path, &utc, work).unwrap()
        else {
            panic!("same original root/signer/time")
        };
        assert!(core::ptr::eq(bound.path(), &raw const path));
        assert!(core::ptr::eq(bound.target(), &raw const target));
        assert_responder_census(&bound, work);
        assert_eq!(
            bound.revocation(),
            if no_check {
                ocsp::ResponderRevocation::NoCheck
            } else {
                ocsp::ResponderRevocation::Unresolved
            }
        );
        assert!(matches!(
            target.bind_responder_path(target_path, &utc, work).unwrap(),
            ResponderBinding::Rejected(ResponderFailure::Signer)
        ));
        assert_responder_boundaries(&target, &path, &utc, work, &verifier);
        assert_eq!(verifier.calls.get(), before + 2);
        assert_responder_fine_reading(target_path, &path, &scoped, &utc, work, &verifier);
    });
}

fn authenticate_delegate<'a, W: Work>(
    response: &'a structured_pkix::ocsp::BasicResponse<'a>,
    local: &structured_pkix::ocsp::DelegateFacts<'a>,
    verifier: &timestamp_oracle::PathOracle,
    work: &W,
) -> structured_pkix::ocsp::ScopedResponse<'a, 'a> {
    // Preserve the same actual signer/key and common local-delegation receipt.
    use structured_pkix::ocsp::{AuthorityBinding, SignatureAuthentication};
    let SignatureAuthentication::Signed(signed) = response
        .authenticate_signature(local.responder(), local.key(), verifier, verifier, work)
        .unwrap()
    else {
        panic!("actual delegated signature")
    };
    let AuthorityBinding::Bound(scoped) = signed.bind_delegate(*local, work).unwrap() else {
        panic!("exact original signer")
    };
    scoped
}

fn assert_responder_census<W: Work>(
    bound: &structured_ltv::pki::ResponderPath<'_, '_, '_, '_>,
    work: &W,
) {
    // Only the actual shared noCheck fact exempts the exact responder target.
    // An unresolved delegate, missing entry or other actor is not a complete
    // status census; noCheck never turns into a caller-written Good flag.
    use structured_ltv::pki::{MemberStatus, StatusCensusError};
    use structured_pkix::ocsp::ResponderRevocation;
    let entries = [MemberStatus::ResponderNoCheck(bound)];
    let at = bound.target().evidence().evaluated_at();
    let result = bound.path().status_census(at, &entries, work);
    if bound.revocation() == ResponderRevocation::Unresolved {
        assert!(matches!(
            result,
            Err(StatusCensusError::Exemption { index: 0 })
        ));
        return;
    }
    let census = result.unwrap();
    assert!(core::ptr::eq(census.path(), bound.path()));
    assert!(matches!(
        census.entries(),
        [MemberStatus::ResponderNoCheck(_)]
    ));
    assert_authority_closure(bound, &census, work);
    assert!(matches!(
        bound.target().path().status_census(at, &entries, work),
        Err(StatusCensusError::Member { index: 0 })
    ));
    assert!(matches!(
        bound.path().status_census(at, &[], work),
        Err(StatusCensusError::Count)
    ));
    let denied = Memory::new(Arc::new(MemoryAccount::new(0, 0)));
    assert!(matches!(
        bound.path().status_census(at, &entries, &denied),
        Err(StatusCensusError::Resource(_))
    ));
}

fn assert_authority_closure<W: Work>(
    responder: &structured_ltv::pki::ResponderPath<'_, '_, '_, '_>,
    child: &structured_ltv::pki::PathStatuses<'_, '_, '_>,
    work: &W,
) {
    // Closure derives the actual delegated authority, not a successful subset
    // chosen by the caller. A repeated root shares its exact dependency once;
    // missing responder/path, duplicate nodes and resource failure stay typed.
    use structured_ltv::pki::{
        AuthorityClosureError, AuthorityNode, AuthorityOrigin, MemberStatus, close_authorities,
    };
    let entries = [MemberStatus::Ocsp(responder.target())];
    let parent = responder
        .target()
        .path()
        .status_census(child.evaluated_at(), &entries, work)
        .unwrap();
    let bindings = [responder];
    let nodes = [
        AuthorityNode {
            statuses: &parent,
            responders: &bindings,
        },
        AuthorityNode {
            statuses: child,
            responders: &[],
        },
    ];
    let roots = [&parent, &parent];
    let before = work.memory().used();
    {
        let closed = {
            // Returned accounted metadata must outlive the short current
            // operation, while borrowing only the original external receipts.
            let operation = structured_asn1::Parser::der(work.memory().clone());
            close_authorities(&roots, &nodes, &operation.execution_in(work.memory())).unwrap()
        };
        assert_eq!(closed.roots().len(), 2);
        assert_eq!(closed.nodes().len(), 2);
        assert!(core::ptr::eq(closed.nodes()[0], &raw const nodes[0]));
        assert!(core::ptr::eq(closed.nodes()[1], &raw const nodes[1]));
        assert_root_dependencies(&closed, &nodes, work);
        assert!(work.memory().used() > before);
    }
    assert_eq!(work.memory().used(), before);
    assert!(matches!(
        close_authorities(&roots, &nodes[..1], work),
        Err(AuthorityClosureError::MissingPath {
            origin: AuthorityOrigin::Responder { node: 0, member: 0 }
        })
    ));
    let missing = [AuthorityNode {
        statuses: &parent,
        responders: &[],
    }];
    assert!(matches!(
        close_authorities(&roots, &missing, work),
        Err(AuthorityClosureError::MissingResponder {
            origin: AuthorityOrigin::Responder { node: 0, member: 0 }
        })
    ));
    let duplicate = [
        AuthorityNode {
            statuses: &parent,
            responders: &[],
        },
        AuthorityNode {
            statuses: &parent,
            responders: &[],
        },
    ];
    assert!(matches!(
        close_authorities(&roots, &duplicate, work),
        Err(AuthorityClosureError::DuplicateNode { index: 1 })
    ));
    let empty = Memory::new(Arc::new(MemoryAccount::new(0, 1 << 24)));
    assert!(matches!(
        close_authorities(&roots, &nodes, &empty),
        Err(AuthorityClosureError::Resource(_))
    ));
    assert_eq!(empty.used(), 0);
    let denied = Memory::new(Arc::new(MemoryAccount::new(1 << 24, 0)));
    assert!(matches!(
        close_authorities(&roots, &nodes, &denied),
        Err(AuthorityClosureError::Resource(_))
    ));
    assert_eq!(denied.used(), 0);
    let local = structured_asn1::Parser::new(
        structured_asn1::Encoding::Der,
        structured_asn1::Limits {
            max_work: 0,
            ..Default::default()
        },
        work.memory().clone(),
    );
    assert!(matches!(
        close_authorities(&roots, &nodes, &local.execution_in(work.memory())),
        Err(AuthorityClosureError::Resource(MemoryError::WorkExceeded))
    ));
    assert_eq!(work.memory().used(), before);
}

fn assert_root_dependencies<W: Work>(
    closed: &structured_ltv::pki::AuthorityClosure<'_, '_, '_>,
    nodes: &[structured_ltv::pki::AuthorityNode<'_, '_, '_>],
    work: &W,
) {
    // Duplicate actor roots independently reach the actual OCSP delegate.
    // Preserve both associations, not merely the first or a purpose union.
    let mut visits = [[0usize; 2]; 2];
    closed
        .visit_roots(work, |root, node| {
            let ordinal = if core::ptr::eq(node, &raw const nodes[0]) {
                0
            } else {
                assert!(core::ptr::eq(node, &raw const nodes[1]));
                1
            };
            visits[root][ordinal] += 1;
            Ok::<_, ()>(())
        })
        .unwrap();
    assert_eq!(visits, [[1, 1], [1, 1]]);
}

fn assert_responder_fine_reading<'a, W: Work>(
    target_path: &structured_ltv::pki::CheckedPath<'a>,
    path: &structured_ltv::pki::CheckedPath<'a>,
    scoped: &structured_pkix::ocsp::ScopedResponse<'_, 'a>,
    utc: &structured_pkix::temporal::LeapTable<'_>,
    work: &W,
    verifier: &timestamp_oracle::PathOracle,
) {
    // Subnanosecond readings need both bounding nanoseconds. A path checked
    // only at the lower label cannot silently supply the upper endpoint.
    use structured_ltv::pki::{self, OcspBinding, PathOutcome, ResponderBinding, ResponderFailure};
    use structured_pkix::{
        ocsp,
        path::{Interval, ValidationBounds, ValidationContext},
        temporal,
    };
    let parser = structured_asn1::Parser::der(work.memory().clone());
    let mut source = parser
        .read(b"\x18\x1a20260101000000.0000000001Z", 8000)
        .unwrap();
    let element = source.next_element().unwrap().unwrap();
    let now = temporal::read_generalized(&source, &element, "evaluation").unwrap();
    assert!(now.has_subnanosecond_remainder());
    let request = ocsp::Request {
        target: target_path.members()[0].certificate(),
        nonce: None,
        intended_signer: Some(scoped.signed().signer()),
    };
    let time = ocsp::Evaluation {
        now,
        utc,
        execution: work,
    };
    let reading = scoped
        .evaluate_with_target(&request, &time, verifier)
        .unwrap();
    let facts = reading.evidence().unwrap();
    let OcspBinding::Bound(bound) = target_path.bind_ocsp(0, &facts, work).unwrap() else {
        panic!("same target")
    };
    assert!(matches!(
        bound.bind_responder_path(path, utc, work).unwrap(),
        ResponderBinding::Rejected(ResponderFailure::Interval)
    ));
    let context = ValidationContext {
        execution: work,
        interval: Interval::between_instants(now.start(), now.upper_bound(utc).unwrap()),
        policy: structured_pkix::path::Policy::default(),
        bounds: ValidationBounds::default(),
        verifier,
        watch: &(),
        recognizer: &(),
    };
    let PathOutcome::Accepted(full) =
        pki::check_path(&[path.members()[0].certificate()], path.anchor(), &context).unwrap()
    else {
        panic!("full nanosecond envelope")
    };
    let before = verifier.calls.get();
    assert!(matches!(
        bound.bind_responder_path(&full, utc, work).unwrap(),
        ResponderBinding::Bound(_)
    ));
    assert_eq!(verifier.calls.get(), before);
}

fn assert_responder_boundaries<'a, W: Work>(
    target: &structured_ltv::pki::OcspTarget<'_, '_, 'a>,
    path: &structured_ltv::pki::CheckedPath<'a>,
    utc: &structured_pkix::temporal::LeapTable<'_>,
    work: &W,
    verifier: &timestamp_oracle::PathOracle,
) {
    // Valid alternative chains do not prove the original root/time association.
    // Work/UTC refusal stays terminal, and binding never repeats signatures.
    use structured_ltv::pki::{
        self, PathOutcome, ResponderBinding, ResponderError, ResponderFailure,
    };
    use structured_pkix::{
        Certificate,
        path::{Anchor, Interval, ValidationBounds, ValidationContext},
        temporal::LeapTable,
    };
    let at = target.evidence().evaluated_at().start();
    let denied = Memory::new(Arc::new(MemoryAccount::new(0, 0)));
    assert!(matches!(
        target.bind_responder_path(path, utc, &denied),
        Err(ResponderError::Resource(_))
    ));
    let unknown = LeapTable::new(at.seconds() + 1, 2_000_000_000, &[]).unwrap();
    assert!(matches!(
        target.bind_responder_path(path, &unknown, work),
        Err(ResponderError::Time(_))
    ));
    let context = ValidationContext {
        execution: work,
        interval: Interval::at(at.seconds() + 1),
        policy: structured_pkix::path::Policy::default(),
        bounds: ValidationBounds::default(),
        verifier,
        watch: &(),
        recognizer: &(),
    };
    let PathOutcome::Accepted(later) =
        pki::check_path(&[path.members()[0].certificate()], path.anchor(), &context).unwrap()
    else {
        panic!("same signer later valid")
    };
    assert!(matches!(
        target.bind_responder_path(&later, utc, work).unwrap(),
        ResponderBinding::Rejected(ResponderFailure::Interval)
    ));
    let (delegate_bytes, root_bytes) = timestamp_oracle::ocsp_delegate(4);
    let parser = structured_asn1::Parser::der(work.memory().clone());
    let delegate = Certificate::read(&parser, &delegate_bytes, 6000).unwrap();
    let root = Certificate::read(&parser, &root_bytes, 7000).unwrap();
    let PathOutcome::Accepted(other) =
        pki::check_path(&[&delegate], Anchor::from_certificate(&root), &context).unwrap()
    else {
        panic!("other root actual valid path")
    };
    assert!(matches!(
        target.bind_responder_path(&other, utc, work).unwrap(),
        ResponderBinding::Rejected(ResponderFailure::Anchor)
    ));
}

fn assert_ocsp_target<W: Work>(
    path: &structured_ltv::pki::CheckedPath<'_>,
    evidence: &structured_pkix::ocsp::BoundEvidence<'_, '_, '_, '_>,
    execution: &W,
) {
    use structured_ltv::pki::{OcspBinding, OcspBindingError};
    let OcspBinding::Bound(bound) = path.bind_ocsp(0, evidence, execution).unwrap() else {
        panic!("same actual target and issuing key")
    };
    let utc = structured_pkix::temporal::LeapTable::new(0, 2_000_000_000, &[]).unwrap();
    assert!(matches!(
        bound.bind_responder_path(path, &utc, execution),
        Err(structured_ltv::pki::ResponderError::SourceKind)
    ));
    assert!(core::ptr::eq(bound.path(), path));
    assert!(core::ptr::eq(bound.evidence(), evidence));
    assert_eq!(bound.member(), 0);
    assert_eq!(bound.authority(), evidence.evidence().source().source());
    assert!(matches!(
        path.bind_ocsp(1, evidence, execution),
        Err(OcspBindingError::MemberIndex { index: 1 })
    ));
    let denied = Memory::new(Arc::new(MemoryAccount::new(0, 0)));
    assert!(matches!(
        path.bind_ocsp(0, evidence, &denied),
        Err(OcspBindingError::Resource(_))
    ));
}

#[test]
fn ocsp_name_and_serial_cannot_substitute_another_issuer_or_path_target() {
    // The server signature and CertID can be valid under a caller-selected
    // configured-responder scope while the caller supplied the WRONG target
    // issuer. Root composition must bind it to the actual checked path key.
    use structured_ltv::pki::{OcspBinding, OcspBindingFailure};
    use structured_pkix::{
        ocsp,
        path::WorkingKey,
        temporal::{Instant, LeapTable},
    };
    xml_digest::with_checked_path(|path, _, _, memory| {
        let target = path.members()[0].certificate();
        let signer = path.anchor().certificate().unwrap();
        let bytes = timestamp_oracle::ocsp_response(3);
        let other_bytes = timestamp_oracle::other_issuer();
        let parser = structured_asn1::Parser::der(memory.clone());
        let query = parser.read(&[], 3000).unwrap();
        let other = structured_pkix::Certificate::read(&parser, &other_bytes, 4000).unwrap();
        let ocsp::Response::Successful(payload) =
            ocsp::Response::read(&parser, &bytes, 5000).unwrap()
        else {
            panic!("successful whole response")
        };
        let response = payload.basic_in(&query).unwrap().unwrap();
        let verifier = timestamp_oracle::PathOracle {
            calls: Cell::new(0),
        };
        let ocsp::SignatureAuthentication::Signed(authenticated) = response
            .authenticate_signature(
                signer,
                WorkingKey::standing(signer.tbs().public_key()),
                &verifier,
                &verifier,
                memory,
            )
            .unwrap()
        else {
            panic!("actual configured server signature")
        };
        let scoped = authenticated.trust_for_issuer(&other);
        let utc = LeapTable::new(0, 2_000_000_000, &[]).unwrap();
        let time = ocsp::Evaluation {
            now: Instant::new(1_767_225_600, 0).unwrap().into(),
            utc: &utc,
            execution: memory,
        };
        for (selected, failure) in [
            (target, OcspBindingFailure::Issuer),
            (&other, OcspBindingFailure::Target),
        ] {
            let request = ocsp::Request {
                target: selected,
                nonce: None,
                intended_signer: Some(signer),
            };
            let reading = scoped
                .evaluate_with_target(&request, &time, &verifier)
                .unwrap();
            let facts = reading.evidence().unwrap();
            assert_eq!(facts.evidence().assertion().status(), ocsp::Status::Good);
            assert!(
                matches!(path.bind_ocsp(0,&facts,memory).unwrap(),OcspBinding::Rejected(actual) if actual==failure)
            );
        }
        assert_eq!(verifier.calls.get(), 1);
    });
}

fn assert_timestamp_path<'a, N: Copy, D: Copy, G: Copy>(
    actor: &structured_ltv::timestamp::CheckedActor<'a, 'a, N, D, G, structured_xades::Xades>,
    root: &'a structured_pkix::Certificate<'a>,
    operation: &'a structured_asn1::Reader<'a>,
    utc: &structured_pkix::temporal::LeapTable<'_>,
) -> structured_ltv::pki::CheckedPath<'a> {
    // The actual TSA certificate must be the target of the checked path, over
    // the entire generation interval. A later valid chain is insufficient.
    use structured_ltv::pki::{
        self, PathOutcome, TimestampBinding, TimestampBindingError, TimestampBindingFailure,
    };
    use structured_pkix::path::{Anchor, Interval, ValidationBounds, ValidationContext};
    let verifier = timestamp_oracle::PathOracle {
        calls: core::cell::Cell::new(0),
    };
    let work = operation.execution();
    let context = ValidationContext {
        execution: &work,
        interval: Interval::at(1_767_225_600),
        policy: structured_pkix::path::Policy::default(),
        bounds: ValidationBounds::default(),
        verifier: &verifier,
        watch: &(),
        recognizer: &(),
    };
    let PathOutcome::Accepted(path) = pki::check_path(
        &[actor.certificate()],
        Anchor::from_certificate(root),
        &context,
    )
    .unwrap() else {
        panic!("actual TSA chain must validate")
    };
    let TimestampBinding::Bound(bound) = path.bind_timestamp(actor, utc, &work).unwrap() else {
        panic!("actual token signer and checked path must bind")
    };
    assert!(core::ptr::eq(bound.actor(), actor));
    assert!(core::ptr::eq(bound.path(), &raw const path));
    assert_eq!(
        bound.generation().earliest,
        structured_pkix::temporal::Instant::new(1_767_225_600, 0).unwrap()
    );
    assert_eq!(verifier.calls.get(), 1);
    let denied = Memory::new(Arc::new(MemoryAccount::new(0, 0)));
    assert!(matches!(
        path.bind_timestamp(actor, utc, &denied),
        Err(TimestampBindingError::Resource(_))
    ));
    let later = ValidationContext {
        interval: Interval::at(1_767_225_601),
        ..context
    };
    let PathOutcome::Accepted(later_path) = pki::check_path(
        &[actor.certificate()],
        Anchor::from_certificate(root),
        &later,
    )
    .unwrap() else {
        panic!("same chain remains valid one second later")
    };
    assert!(matches!(
        later_path.bind_timestamp(actor, utc, &work).unwrap(),
        TimestampBinding::Rejected(TimestampBindingFailure::Interval)
    ));
    assert_eq!(verifier.calls.get(), 2);
    path
}

fn carried_ocsp_choice(response: &[u8]) -> Vec<u8> {
    let mut fields = timestamp_oracle::tlv(6, structured_cms::oid::OCSP_RESPONSE.as_bytes());
    fields.extend(response);
    timestamp_oracle::tlv(0xa1, &fields)
}

#[test]
fn timestamp_carried_material_keeps_token_pool_identity_and_embedded_ocsp_certificates() {
    // Both timestamp sources retain their actual choice ordinals. This is
    // original-source availability, not OCSP signature/status authentication.
    let certificate = candidate_certificate();
    // DER SET OF order compares whole original TLVs: the shorter opaque
    // context-tagged member precedes the long OCSP member.
    let mut choices = timestamp_oracle::tlv(0xa1, &[6, 1, 42, 5, 0]);
    choices.extend(carried_ocsp_choice(&ocsp_material_with_certificate(
        &certificate,
    )));
    let (mut xml, _, _) = token_pipeline_tree();
    xml.binary = inspection_token_with_choices(&choices, 5);
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Archival, None).unwrap();
    let held = memory.used();
    timestamp::with_tokens(
        &xml,
        &prepared,
        &operation,
        timestamp::TokenProfile::Etsi,
        |query, view| {
            let material = view.material_in(query).unwrap();
            assert!(material.crls().is_empty());
            assert_eq!(material.responses().len(), 2);
            assert_eq!(material.basic_responses().len(), 2);
            assert_eq!(material.other().len(), 2);
            for ordinal in 0..2 {
                let basic = &material.basic_responses()[ordinal];
                assert_eq!(
                    basic.origin,
                    structured_ltv::material::CarriedOrigin {
                        token: ordinal,
                        choice: 1
                    }
                );
                assert_eq!(
                    basic.value.as_bytes().as_ptr(),
                    material.responses()[ordinal].value.payload().as_ptr()
                );
                assert_eq!(basic.value.certificates()[0].as_bytes(), certificate);
                assert_eq!(
                    material.other()[ordinal].origin,
                    structured_ltv::material::CarriedOrigin {
                        token: ordinal,
                        choice: 0
                    }
                );
                assert_eq!(material.other()[ordinal].value.kind.as_bytes(), &[42]);
                assert_eq!(material.other()[ordinal].value.value.as_bytes(), &[5, 0]);
            }
            Ok::<_, ()>(())
        },
    )
    .unwrap();
    assert_eq!(memory.used(), held);
    drop(prepared);
    assert_eq!(memory.used(), 0);
}

#[test]
fn timestamp_carried_material_late_known_failure_drops_all_partial_catalogues() {
    // Earlier usable syntax cannot hide a late unsigned OCSP protocol failure;
    // original token/choice and XML location survive the entire scope's drop.
    let (mut xml, _, second) = token_pipeline_tree();
    xml.binary = inspection_token_with_choices(
        &carried_ocsp_choice(&ocsp_material_with_certificate(&candidate_certificate())),
        5,
    );
    xml.binary_routes.push((
        second,
        inspection_token_with_choices(&carried_ocsp_choice(&[0x30, 3, 10, 1, 3]), 5),
    ));
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Archival, None).unwrap();
    let held = memory.used();
    let result = timestamp::with_tokens(&xml, &prepared, &operation, timestamp::TokenProfile::Etsi, |query, view| {
        let Err(error) = view.material_in(query) else { panic!("late invalid status must refuse") };
        assert_eq!(error.location.node, second);
        assert_eq!(error.origin, structured_ltv::material::CarriedOrigin { token: 1, choice: 0 });
        assert!(matches!(&error.cause, timestamp::CarriedCause::Cms(cause) if cause.code() == structured_cms::ErrorCode::MalformedValue));
        Ok::<_, ()>(error)
    }).unwrap();
    assert_eq!(result.location.snapshot, xml.snapshot());
    drop(result);
    assert_eq!(memory.used(), held);
}

#[test]
fn timestamp_carried_material_empty_set_still_pays_current_metadata_admission() {
    // Source parsing is no work credit for a later narrower operation. Even
    // empty revocation sets must not create successful unfunded traversals.
    let (mut xml, first, _) = token_pipeline_tree();
    xml.binary = inspection_token();
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Archival, None).unwrap();
    timestamp::with_tokens(&xml, &prepared, &operation, timestamp::TokenProfile::Etsi, |_, view| {
        let denied = structured_asn1::Parser::new(
            structured_asn1::Encoding::Der,
            structured_asn1::Limits {
                max_work: 0,
                ..structured_asn1::Limits::default()
            },
            memory.clone(),
        );
        let query = denied.read(&[], 0).unwrap();
        let Err(error) = view.material_in(&query) else {
            panic!("unfunded metadata must refuse")
        };
        assert_eq!(error.location.node, first);
        assert!(
            matches!(error.cause, timestamp::CarriedCause::Encoding(cause) if cause.is_resource())
        );
        Ok::<_, ()>(())
    })
    .unwrap();
}

#[test]
fn timestamp_carried_crl_uses_original_choice_bytes_and_actual_enclosing_depth() {
    // CMS CRL choices have no extra OCTET STRING wrapper. Reading must keep
    // the original whole CertificateList and actual nested source coordinates.
    use timestamp_oracle::tlv;
    let algorithm = [48, 3, 6, 1, 42];
    let name = [48, 12, 49, 10, 48, 8, 6, 3, 0x55, 4, 3, 12, 1, b'a'];
    let tbs = tlv(
        48,
        &[algorithm.as_slice(), &name, &tlv(23, b"261012120000Z")].concat(),
    );
    let crl = tlv(48, &[&tbs, algorithm.as_slice(), &[3, 2, 0, 8]].concat());
    let (mut xml, _, _) = token_pipeline_tree();
    xml.binary = inspection_token_with_choices(&crl, 3);
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Archival, None).unwrap();
    let held = memory.used();
    timestamp::with_tokens(
        &xml,
        &prepared,
        &operation,
        timestamp::TokenProfile::Etsi,
        |query, view| {
            let material = view.material_in(query).unwrap();
            assert_eq!(material.crls().len(), 2);
            assert!(material.responses().is_empty());
            assert!(material.basic_responses().is_empty());
            assert!(material.other().is_empty());
            for ordinal in 0..2 {
                let value = &material.crls()[ordinal];
                let original = view.readings()[ordinal].token().message().revocations()[0];
                assert_eq!(
                    value.origin,
                    structured_ltv::material::CarriedOrigin {
                        token: ordinal,
                        choice: 0
                    }
                );
                assert_eq!(value.value.as_bytes(), crl);
                assert_eq!(
                    value.value.as_bytes().as_ptr(),
                    original.as_bytes().as_ptr()
                );
                assert_eq!(value.value.span(), original.span());
            }
            Ok::<_, ()>(())
        },
    )
    .unwrap();
    assert_eq!(memory.used(), held);
}

#[test]
fn timestamp_carried_material_new_operation_cannot_bypass_storage_or_original_depth() {
    // Retained source capacity is not moved to the new account. A later
    // decoder must pay its own catalogues and respect the original CMS depth.
    let (mut xml, first, _) = token_pipeline_tree();
    xml.binary = inspection_token_with_choices(
        &carried_ocsp_choice(&ocsp_material_with_certificate(&candidate_certificate())),
        5,
    );
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Archival, None).unwrap();
    timestamp::with_tokens(
        &xml,
        &prepared,
        &operation,
        timestamp::TokenProfile::Etsi,
        |_, view| {
            for (capacity, depth) in [(0, 64), (1_048_576, 3)] {
                let account = Memory::new(Arc::new(MemoryAccount::new(capacity, 1_048_576)));
                let current = structured_asn1::Parser::new(
                    structured_asn1::Encoding::Der,
                    structured_asn1::Limits {
                        max_depth: depth,
                        ..structured_asn1::Limits::default()
                    },
                    account.clone(),
                );
                let query = current.read(&[], 0).unwrap();
                let held = memory.used();
                let Err(error) = view.material_in(&query) else {
                    panic!("refusal must precede usable material")
                };
                assert_eq!(error.location.node, first);
                assert_eq!(
                    error.origin,
                    structured_ltv::material::CarriedOrigin {
                        token: 0,
                        choice: 0
                    }
                );
                assert!(match &error.cause {
                    timestamp::CarriedCause::Cms(cause) => cause.is_resource(),
                    timestamp::CarriedCause::Pkix(cause) => cause.is_resource(),
                    timestamp::CarriedCause::Encoding(cause) => cause.is_resource(),
                    timestamp::CarriedCause::Resource(_) => true,
                });
                drop(error);
                assert_eq!(account.used(), 0);
                assert_eq!(memory.used(), held);
            }
            Ok::<_, ()>(())
        },
    )
    .unwrap();
}

#[test]
fn timestamp_source_set_freezes_ownership_before_borrowing_original_tokens_and_readers() {
    // Original buffers/parser contexts coexist without self-reference, token
    // clones, a second decoder or borrowing movable source-vector entries.
    let (mut xml, first, second) = token_pipeline_tree();
    xml.binary = inspection_token();
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Archival, None).unwrap();
    let held = memory.used();
    timestamp::with_tokens(
        &xml,
        &prepared,
        &operation,
        timestamp::TokenProfile::Etsi,
        |_, view| {
            assert_eq!(view.sources().len(), 2);
            assert_eq!(view.readings().len(), 2);
            for (index, (source, reading)) in view.sources().iter().zip(view.readings()).enumerate()
            {
                assert_eq!(source.location().node, [first, second][index]);
                assert_eq!(source.location().snapshot, xml.snapshot());
                assert_eq!(source.stamp_index(), index);
                assert_eq!(source.token_index(), 0);
                assert_eq!(source.property(), prepared.timestamps()[index].node());
                assert_eq!(source.role(), [Role::Signature, Role::Archive][index]);
                assert!(reading.reader().is_empty());
                assert_eq!(reading.reader().depth(), 0);
                assert_eq!(reading.token().message().signers().len(), 1);
                let bytes = reading.token().message().as_bytes();
                assert!(bytes.as_ptr() >= source.bytes().as_ptr());
                assert!(bytes.as_ptr_range().end <= source.bytes().as_ptr_range().end);
            }
            Ok::<_, ()>(())
        },
    )
    .unwrap();
    assert_eq!(memory.used(), held);
    drop(prepared);
    assert_eq!(memory.used(), 0);
}

#[test]
fn timestamp_source_set_runs_complete_common_checks_and_retains_independent_evidence_without_redecode()
 {
    // This tests the real P256/ESS/imprint common checker over the frozen source
    // set, not full XML C14N, signer trust or a completed XAdES baseline level.
    use structured_cms::timestamp::CheckContext;
    use structured_pkix::{
        path::WorkingKey,
        temporal::{Instant, LeapTable},
    };
    let memory = Memory::new(Arc::new(MemoryAccount::new(1_000_000, 10_000_000)));
    let (proofs, node) = {
        let mut xml = preparation_tree();
        let method = xml.element_child(8, DS, "CanonicalizationMethod");
        let node = xml.element_child(8, XADES, "EncapsulatedTimeStamp");
        let value = xml.element_child(0, DS, "SignatureValue");
        let expected = [
            b'E',
            u8::try_from(value).unwrap(),
            b'M',
            u8::try_from(method).unwrap(),
        ];
        let (provider, token, certificate_bytes) =
            timestamp_oracle::fixture_with_certificate(&expected);
        xml.binary = token;
        let parser = structured_asn1::Parser::der(memory.clone());
        let operation = parser.read(&[], 900).unwrap();
        let cert_parser = structured_asn1::Parser::der(memory.clone());
        let certificate =
            structured_pkix::Certificate::read(&cert_parser, &certificate_bytes, 400).unwrap();
        let certificate_parent = cert_parser.read(&[], 400).unwrap();
        let utc = LeapTable::new(0, 2_000_000_000, &[]).unwrap();
        let context = CheckContext {
            certificate: &certificate,
            certificate_parent: &certificate_parent,
            key: WorkingKey::standing(certificate.tbs().public_key()),
            requested: None,
            utc: &utc,
            reference: Some(Instant::new(1_800_000_000, 0).unwrap()),
            window: None,
            policy_accuracy: None,
        };
        let access = Access::new(&xml, xml.snapshot(), &memory);
        let prepared =
            prepare::prepare(&access, 0, unsigned::Requirements::Timestamped, None).unwrap();
        let proofs = timestamp::with_tokens(
            &xml,
            &prepared,
            &operation,
            timestamp::TokenProfile::Etsi,
            |query, view| {
                let execution = query.execution();
                let mut proofs = execution.vector();
                for (source, reading) in view.sources().iter().zip(view.readings()) {
                    assert_eq!(reading.token().message().certificates().len(), 1);
                    let stamp = &prepared.timestamps()[source.stamp_index()];
                    let mut input = Input::prepare(
                        &xml,
                        prepared.bound(),
                        stamp,
                        &reading.reader().execution(),
                    )
                    .unwrap();
                    let checked = reading
                        .token()
                        .check(&provider, reading.reader(), &context, &mut input, |_, _| {
                            Ok::<_, ()>(false)
                        })
                        .unwrap();
                    let evidence = checked
                        .into_evidence()
                        .expect("complete real timestamp check");
                    proofs
                        .try_push(binary::Located {
                            location: source.location(),
                            value: evidence,
                        })
                        .unwrap();
                }
                Ok::<_, ()>(proofs.into_owned())
            },
        )
        .unwrap();
        assert_eq!(provider.verified.get(), 1);
        (proofs, node)
    };
    assert_eq!(proofs.len(), 1);
    assert_eq!(proofs[0].location.node, node);
    assert_eq!(
        proofs[0].value.facts().gen_time().start(),
        Instant::new(1_767_225_600, 0).unwrap()
    );
    assert!(memory.used() > 0);
    drop(proofs);
    assert_eq!(memory.used(), 0);
}

#[test]
fn timestamp_source_set_never_exposes_a_parsed_prefix_when_a_later_token_is_broken() {
    // The early token parses, but a late CMS failure must precede the callback
    // and release all temporary source and parsed-catalogue capacity.
    let (mut xml, _, second) = token_pipeline_tree();
    xml.binary = inspection_token();
    xml.binary_routes.push((second, vec![0x30, 0]));
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Archival, None).unwrap();
    let held = memory.used();
    let called = Cell::new(false);
    let result = timestamp::with_tokens(
        &xml,
        &prepared,
        &operation,
        timestamp::TokenProfile::Etsi,
        |_, _| {
            called.set(true);
            Ok::<_, ()>(())
        },
    );
    assert!(
        matches!(result,Err(timestamp::TokenPipelineError::Token{location,..}) if location.node==second)
    );
    assert!(!called.get());
    // The original ASN.1 error legitimately retains its own diagnostic path;
    // release it before checking that every temporary allocation was returned.
    drop(result);
    assert_eq!(memory.used(), held);
}

#[test]
fn timestamp_source_set_preserves_visitor_cause_current_budget_and_generation_rollback() {
    // No fresh source read may escape the current operation. A source mutation
    // drops the visitor's retained result; its explicit error is otherwise intact.
    for mutate in [false, true] {
        let (mut xml, _, _) = token_pipeline_tree();
        xml.binary = inspection_token();
        let memory = funded();
        let parser = structured_asn1::Parser::der(memory.clone());
        let operation = parser.read(&[], 900).unwrap();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        let prepared =
            prepare::prepare(&access, 0, unsigned::Requirements::Archival, None).unwrap();
        let held = memory.used();
        let result = timestamp::with_tokens(
            &xml,
            &prepared,
            &operation,
            timestamp::TokenProfile::Etsi,
            |_, _| {
                if !mutate {
                    return Err("visitor refused");
                }
                let mut owned = memory.vector();
                owned.try_push(7u8).unwrap();
                xml.generation.set(2);
                Ok(owned)
            },
        );
        if mutate {
            assert!(matches!(
                result,
                Err(timestamp::TokenPipelineError::Access(
                    AccessError::Changed { .. }
                ))
            ));
        } else {
            assert!(matches!(
                result,
                Err(timestamp::TokenPipelineError::Visitor("visitor refused"))
            ));
        }
        assert_eq!(memory.used(), held);
    }
    let (mut xml, _, _) = token_pipeline_tree();
    xml.binary = inspection_token();
    let memory = funded();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Archival, None).unwrap();
    let parser = structured_asn1::Parser::new(
        structured_asn1::Encoding::Der,
        structured_asn1::Limits {
            max_work: 0,
            ..Default::default()
        },
        memory.clone(),
    );
    let operation = parser.read(&[], 900).unwrap();
    assert!(matches!(
        timestamp::with_tokens(
            &xml,
            &prepared,
            &operation,
            timestamp::TokenProfile::Etsi,
            |_, _| Ok::<_, ()>(())
        ),
        Err(timestamp::TokenPipelineError::Access(
            AccessError::Resource { .. }
        ))
    ));
    assert_eq!(xml.decoded_pointer.get(), 0);
}

#[test]
fn archive_preserves_reference_order_prior_properties_and_other_objects() {
    // Exact EN319132-1 §5.5.2.3 concatenation; current/later properties excluded.
    let (mut xml, node, method) = Tree::stamped(Role::Archive);
    let object = xml.nodes.len();
    xml.nodes.push(element(Some(0), DS, "Object"));
    xml.nodes.push(Record {
        parent: Some(object),
        value: Node::Text("application payload"),
    });
    xml.nodes.push(element(Some(7), XADES, "AnyValidationData"));
    let memory = funded();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let stamp = timestamp::read(&access, &bound, node).unwrap();
    let mut input = Input::prepare(&xml, &bound, &stamp, &memory).unwrap();
    let mut sink = Collect::new(&memory);
    stream::run(&mut input, &mut sink, &memory, "logical.archive.input", 0).unwrap();
    let method = u8::try_from(method).unwrap();
    assert_eq!(
        sink.bytes(),
        &[
            b'R',
            0,
            b'M',
            method,
            b'R',
            1,
            b'M',
            method,
            b'E',
            9,
            b'M',
            method,
            b'E',
            12,
            b'M',
            method,
            b'E',
            13,
            b'M',
            method,
            b'E',
            8,
            b'M',
            method,
            b'E',
            u8::try_from(object).unwrap(),
            b'M',
            method,
        ]
    );
    assert_eq!(xml.streamed.get(), 7);
}

#[test]
fn unavailable_original_reference_is_incomplete_timestamp_input_not_a_processing_error_or_empty_success()
 {
    // Only the owner's typed absent-source outcome is unavailable. An opaque
    // acquisition failure must retain its real error class and original node.
    for unavailable in [false, true] {
        let (mut xml, node, _) = Tree::stamped(Role::Archive);
        if unavailable {
            xml.unavailable_reference = Some(10);
        } else {
            xml.incomplete = true;
        }
        let memory = funded();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        let layout = container::locate(&access, 0).unwrap();
        let bound = binding::bind(&access, &layout).unwrap();
        let stamp = timestamp::read(&access, &bound, node).unwrap();
        let mut input = Input::prepare(&xml, &bound, &stamp, &memory).unwrap();
        let mut sink = Collect::new(&memory);
        let result = stream::run(&mut input, &mut sink, &memory, "virtual.archive.input", 0);
        if unavailable {
            assert!(matches!(
                result,
                Err(stream::StreamError::Source(InputError::Unavailable {
                    node: 10,
                    cause: Cause::Unavailable
                }))
            ));
            assert_eq!(sink.bytes(), []);
            assert_eq!(xml.streamed.get(), 0);
        } else {
            assert!(matches!(
                result,
                Err(stream::StreamError::Source(InputError::Access(
                    AccessError::Xml(Cause::Incomplete)
                )))
            ));
            assert_eq!(xml.streamed.get(), 1);
        }
    }
}

#[test]
fn timestamp_method_and_rfc3161_cardinality_are_not_defaulted_or_dropped() {
    // Missing method does not invent C14N; SignatureTimeStamp cannot hide a token.
    let (mut xml, node, method) = Tree::stamped(Role::Signature);
    xml.nodes[method].parent = Some(1);
    let memory = funded();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let bound = binding::bind(&access, &container::locate(&access, 0).unwrap()).unwrap();
    assert!(matches!(
        timestamp::read(&access, &bound, node),
        Err(InputError::Missing {
            local: "CanonicalizationMethod",
            ..
        })
    ));
    xml.nodes[method].parent = Some(node);
    let extra = xml.nodes.len();
    xml.nodes
        .push(element(Some(node), XADES, "EncapsulatedTimeStamp"));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(timestamp::read(&access, &bound, node), Err(InputError::Duplicate { node }) if node == extra)
    );
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn timestamp_preparation_funds_signature_identity_before_xml_traversal() {
    use structured_asn1::{Encoding, Limits, Parser};

    // The retained stamp/binding cannot pay for a new operation's identity
    // comparison. Refusal belongs to the stamp before any child traversal.
    for role in [Role::Signature, Role::Archive] {
        let (xml, node, _) = Tree::stamped(role);
        let memory = funded();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        let bound = binding::bind(&access, &container::locate(&access, 0).unwrap()).unwrap();
        let stamp = timestamp::read(&access, &bound, node).unwrap();
        for budget in [0, 1] {
            let current = Memory::new(Arc::new(MemoryAccount::new(0, budget)));
            assert!(matches!(
                Input::prepare(&xml, &bound, &stamp, &current),
                Err(InputError::Resource { node: at, cause: MemoryError::WorkExceeded }) if at == node
            ));
            assert_eq!(current.used(), 0);
        }
        let parser = Parser::new(
            Encoding::Der,
            Limits {
                max_work: 1,
                ..Limits::default()
            },
            funded(),
        );
        let reader = parser.read(&[], 0).unwrap();
        assert!(matches!(
            Input::prepare(&xml, &bound, &stamp, &reader.execution()),
            Err(InputError::Resource { node: at, cause: MemoryError::WorkExceeded }) if at == node
        ));
        assert_eq!(xml.streamed.get(), 0);
    }
}

#[test]
fn timestamp_membership_pays_after_child_lookup_and_before_field_decoding() {
    // XML traversal has already succeeded when the last unit remains. It
    // cannot fund the two-operand membership comparison or begin decoding.
    let (xml, node, _) = Tree::stamped(Role::Signature);
    let original = funded();
    let access = Access::new(&xml, xml.snapshot(), &original);
    let bound = binding::bind(&access, &container::locate(&access, 0).unwrap()).unwrap();
    let current = funded();
    let observed = query_boundary::Boundary::after_inspection(&xml, node, &current, 1);
    let access = Access::new(&observed, xml.snapshot(), &current);
    assert!(matches!(timestamp::read(&access, &bound, node),
        Err(InputError::Resource { node: at, cause: MemoryError::WorkExceeded }) if at == node));
    assert_eq!(current.work_remaining(), 1);
    assert_eq!(current.used(), 0);
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn signed_timestamp_membership_preserves_current_refusal_after_catalogue_admission() {
    // The catalogue metadata query fits, but the following identifier scan
    // must not use retained parsing credit or turn refusal into field access.
    let (mut xml, _) = certificate_tree();
    let objects = xml.nodes.len();
    xml.nodes
        .push(element(Some(3), XADES, "SignedDataObjectProperties"));
    let node = xml.nodes.len();
    xml.nodes
        .push(element(Some(objects), XADES, "AllDataObjectsTimeStamp"));
    xml.nodes
        .push(element(Some(node), DS, "CanonicalizationMethod"));
    xml.nodes
        .push(element(Some(node), XADES, "EncapsulatedTimeStamp"));
    let original = funded();
    let signed = signed_roles(&xml, &original).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &original);
    let bound = binding::bind(&access, &container::locate(&access, 0).unwrap()).unwrap();
    let current = funded();
    let observed = query_boundary::Boundary::exhaust(&xml, 2, &current, 1);
    let access = Access::new(&observed, xml.snapshot(), &current);
    assert!(
        matches!(timestamp::read_all_data_objects(&access, &bound, &signed, node),
        Err(InputError::Resource { node: at, cause: MemoryError::WorkExceeded }) if at == node)
    );
    assert_eq!(current.work_remaining(), 1);
    assert_eq!(current.used(), 0);
}

#[test]
fn stale_generation_and_capacity_refusal_precede_timestamp_side_effects() {
    // Plans cannot escape the original generation, and no C14N begins on refusal.
    let (xml, node, _) = Tree::stamped(Role::Archive);
    let memory = funded();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let bound = binding::bind(&access, &container::locate(&access, 0).unwrap()).unwrap();
    let stamp = timestamp::read(&access, &bound, node).unwrap();
    let empty = self::memory(300_000);
    assert!(matches!(
        Input::prepare(&xml, &bound, &stamp, &empty),
        Err(InputError::Resource {
            cause: MemoryError::LimitExceeded,
            ..
        })
    ));
    assert_eq!(xml.streamed.get(), 0);
    let mut input = Input::prepare(&xml, &bound, &stamp, &memory).unwrap();
    xml.generation.set(2);
    let mut sink = Collect::new(&memory);
    assert!(matches!(
        stream::run(&mut input, &mut sink, &memory, "logical.archive.input", 0),
        Err(stream::StreamError::Source(InputError::Access(
            AccessError::Changed { .. }
        )))
    ));
    assert_eq!(sink.bytes(), []);
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn sticky_sink_refusal_stops_later_archive_components_even_if_owner_ignores_it() {
    // The existing CMS driver holds the cause; input stops additional XML calls.
    let (mut xml, node, _) = Tree::stamped(Role::Archive);
    xml.ignore_refusal = true;
    let memory = funded();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let bound = binding::bind(&access, &container::locate(&access, 0).unwrap()).unwrap();
    let stamp = timestamp::read(&access, &bound, node).unwrap();
    let mut input = Input::prepare(&xml, &bound, &stamp, &memory).unwrap();
    let mut sink = Collect::new(&memory);
    sink.refuse = true;
    assert!(matches!(
        stream::run(&mut input, &mut sink, &memory, "logical.archive.input", 0),
        Err(stream::StreamError::Callback(SinkError::Refused))
    ));
    assert_eq!(xml.streamed.get(), 1);
    assert_eq!(sink.bytes(), []);
}

#[test]
fn incomplete_xml_source_is_not_a_completed_timestamp_input() {
    // A prefix reaching the hash sink does not erase the source's exact cause.
    let (mut xml, node, _) = Tree::stamped(Role::Signature);
    xml.incomplete = true;
    let memory = funded();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let bound = binding::bind(&access, &container::locate(&access, 0).unwrap()).unwrap();
    let stamp = timestamp::read(&access, &bound, node).unwrap();
    let mut input = Input::prepare(&xml, &bound, &stamp, &memory).unwrap();
    let mut sink = Collect::new(&memory);
    assert!(matches!(
        stream::run(&mut input, &mut sink, &memory, "logical.timestamp.input", 0),
        Err(stream::StreamError::Source(InputError::Access(
            AccessError::Xml(Cause::Incomplete)
        )))
    ));
    assert_eq!(sink.bytes().len(), 4);
}

fn binary_token() -> Vec<u8> {
    // Synthetic readable CMS/TSTInfo, deliberately not a cryptographic proof.
    fn tlv(tag: u8, contents: &[u8]) -> Vec<u8> {
        assert!(contents.len() < 128);
        let mut value = vec![tag, u8::try_from(contents.len()).unwrap()];
        value.extend_from_slice(contents);
        value
    }
    let mut info = vec![
        2, 1, 1, 6, 1, 43, 0x30, 9, 0x30, 3, 6, 1, 42, 4, 2, 97, 98, 2, 1, 7,
    ];
    info.extend(tlv(0x18, b"20260101000000Z"));
    let info = tlv(0x30, &info);
    let mut content = tlv(6, structured_cms::oid::TST_INFO.as_bytes());
    content.extend(tlv(0xa0, &tlv(4, &info)));
    let signer = tlv(
        0x30,
        &[
            2, 1, 3, 0x80, 1, 7, 0x30, 3, 6, 1, 42, 0x30, 3, 6, 1, 43, 4, 1, 8,
        ],
    );
    let mut message = vec![2, 1, 3, 0x31, 0];
    message.extend(tlv(0x30, &content));
    message.extend(tlv(0x31, &signer));
    let mut envelope = tlv(6, structured_cms::oid::SIGNED_DATA.as_bytes());
    envelope.extend(tlv(0xa0, &tlv(0x30, &message)));
    tlv(0x30, &envelope)
}

#[test]
fn scoped_binary_token_uses_common_info_and_retains_only_paid_results() {
    // No borrowed token or decoded scratch escapes; the selected result's
    // independent capacity survives both scoped producing parsers.
    let memory = funded();
    let retained = {
        let mut xml = Tree::normal();
        xml.binary = binary_token();
        let parser = structured_asn1::Parser::ber(memory.clone());
        let operation = parser.read(&[], 900).unwrap();
        let location = Location {
            snapshot: xml.snapshot(),
            node: 8,
        };
        binary::with_token(&xml, location, &operation, |token, reader| {
            let result = token
                .with_info(reader, |info, parent, _| {
                    assert_eq!(info.serial().value().as_u64(), Some(7));
                    assert!(!info.has_faults());
                    let execution = parent.execution();
                    let mut result = execution.vector();
                    result.try_extend_from_slice(info.policy().as_bytes())?;
                    Ok::<_, MemoryError>(result.into_owned())
                })
                .unwrap();
            Ok::<_, ()>(result)
        })
        .unwrap()
    };
    assert_eq!(&*retained, &[43]);
    assert!(memory.used() > 0);
    drop(retained);
    assert_eq!(memory.used(), 0);
}

#[test]
fn binary_callback_and_processing_errors_keep_xml_and_decoded_coordinates() {
    // A decoded ASN offset is not physical XML position 900, and callback
    // failure is never rewritten into syntax or a timestamp mismatch.
    let memory = funded();
    let parser = structured_asn1::Parser::ber(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let mut xml = Tree::normal();
    xml.binary = vec![0x30, 0];
    let location = Location {
        snapshot: xml.snapshot(),
        node: 8,
    };
    match binary::with_token(&xml, location, &operation, |_, _| Ok::<_, u8>(())) {
        Err(binary::Error::Callback {
            location: actual,
            cause: binary::TokenError::Processing(cause),
        }) => {
            assert_eq!(actual, location);
            assert!(cause.offset() < 900);
        }
        other => panic!("unexpected {other:?}"),
    }
    xml.binary = binary_token();
    assert!(matches!(
        binary::with_token(&xml, location, &operation, |_, _| Err::<(), _>(17_u8)),
        Err(binary::Error::Callback {
            cause: binary::TokenError::Callback(17),
            ..
        })
    ));
    assert_eq!(memory.used(), 0);
}

#[test]
fn binary_scopes_reject_stale_generation_and_fund_current_local_copy() {
    // Scratch copy cannot bypass the operation's local work ceiling. A stale
    // successful callback cannot publish retained output or updated evidence.
    let memory = funded();
    let limits = structured_asn1::Limits {
        max_work: 3,
        ..structured_asn1::Limits::default()
    };
    let parser =
        structured_asn1::Parser::new(structured_asn1::Encoding::Ber, limits, memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let mut xml = Tree::normal();
    xml.binary = binary_token();
    let location = Location {
        snapshot: xml.snapshot(),
        node: 8,
    };
    let called = Cell::new(false);
    assert!(matches!(
        binary::with_token(&xml, location, &operation, |_, _| {
            called.set(true);
            Ok::<_, ()>(())
        }),
        Err(binary::Error::Access {
            cause: AccessError::Xml(Cause::Resource(MemoryError::WorkExceeded)),
            ..
        })
    ));
    assert!(!called.get());
    assert_eq!(memory.work_used(), 3);
    assert_eq!(memory.used(), 0);
    let parser = structured_asn1::Parser::ber(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    xml.mutate_binary = true;
    assert!(matches!(
        binary::with_token(&xml, location, &operation, |_, _| Ok::<_, ()>(())),
        Err(binary::Error::Access {
            cause: AccessError::Changed { node: 8 },
            ..
        })
    ));
    assert_eq!(memory.used(), 0);
}

#[test]
fn binary_reader_creation_refusal_preserves_typed_depth_cause_before_visitor() {
    // Common Reader creation preserves the ASN resource refusal before the
    // token visitor, rather than reporting base64 syntax or missing timestamp.
    let memory = funded();
    let limits = structured_asn1::Limits {
        max_depth: 0,
        ..structured_asn1::Limits::default()
    };
    let parser =
        structured_asn1::Parser::new(structured_asn1::Encoding::Ber, limits, memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let mut xml = Tree::normal();
    xml.binary = binary_token();
    // Emulate an existing decoded window at ASN depth1; a fresh root is depth0.
    xml.binary_depth = 1;
    let location = Location {
        snapshot: xml.snapshot(),
        node: 8,
    };
    let called = Cell::new(false);
    let result = binary::with_token(&xml, location, &operation, |_, _| {
        called.set(true);
        Ok::<_, ()>(())
    });
    match result {
        Err(binary::Error::ReaderCreation {
            cause,
            location: actual,
        }) => {
            assert_eq!(actual, location);
            assert_eq!(
                cause.code(),
                structured_asn1::ErrorCode::LimitExceeded(structured_asn1::Limit::Depth)
            );
            assert_eq!(cause.offset(), 0);
        }
        other => panic!("unexpected {other:?}"),
    }
    assert!(!called.get());
    assert_eq!(memory.used(), 0);
}

fn signed_roles(
    xml: &Tree,
    memory: &Memory,
) -> properties::PropertyResult<properties::Reading<Tree>, Tree> {
    let access = Access::new(xml, xml.snapshot(), memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    properties::read(&access, &bound)
}

#[test]
fn signing_time_requires_a_qualified_explicit_zone_without_canonical_z_or_epoch_conversion() {
    // The owner's complete scalar receipt is a CLAIM, not a timestamp. Keep
    // original source/offset and never require a narrowed POSIX/calendar value.
    use properties::{ClaimedTimeBinding, ClaimedTimeFailure, DateTimeFact};
    for timezone in [-840, -60, 0, 60, 840] {
        let memory = funded();
        let mut xml = preparation_tree();
        xml.date_time = DateTimeFact::Valid {
            timezone_minutes: Some(timezone),
        };
        let signed = signed_roles(&xml, &memory).unwrap();
        let before = memory.used();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        let ClaimedTimeBinding::Bound(claim) = properties::check_time(&signed, &access).unwrap()
        else {
            panic!("complete original zoned scalar")
        };
        assert_eq!(claim.snapshot(), signed.snapshot());
        assert_eq!(claim.signature(), signed.signature());
        assert_eq!(claim.node(), signed.signing_time());
        assert_eq!(claim.timezone_minutes(), timezone);
        assert_eq!(xml.date_time_calls.get(), 1);
        assert_eq!(memory.used(), before);
        assert_eq!(xml.streamed.get(), 0);
    }
    for (fact, expected) in [
        (DateTimeFact::Malformed, ClaimedTimeFailure::Malformed),
        (DateTimeFact::Unsupported, ClaimedTimeFailure::Unsupported),
        (
            DateTimeFact::Valid {
                timezone_minutes: None,
            },
            ClaimedTimeFailure::TimezoneUnstated,
        ),
    ] {
        let mut xml = preparation_tree();
        xml.date_time = fact;
        let memory = funded();
        let signed = signed_roles(&xml, &memory).unwrap();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        assert!(matches!(properties::check_time(&signed, &access).unwrap(),
            ClaimedTimeBinding::Unproven { node, cause }
            if node == signed.signing_time() && cause == expected));
        assert_eq!(xml.date_time_calls.get(), 1);
    }
}

#[test]
fn signing_time_uses_current_admission_and_preserves_source_and_owner_refusals() {
    // Retaining SignedProperties does not pay a later scalar query. Zero work
    // refuses before owner dispatch; an owner failure stays its original cause.
    use properties::{ClaimedTimeBinding, ClaimedTimeError, DateTimeFact};
    let mut xml = preparation_tree();
    xml.date_time = DateTimeFact::Valid {
        timezone_minutes: Some(0),
    };
    let original = funded();
    let signed = signed_roles(&xml, &original).unwrap();
    let before = original.used();
    let denied = memory(0);
    let access = Access::new(&xml, xml.snapshot(), &denied);
    assert!(matches!(
        properties::check_time(&signed, &access),
        Err(ClaimedTimeError::Access(AccessError::Resource {
            cause: MemoryError::WorkExceeded,
            ..
        }))
    ));
    assert_eq!(xml.date_time_calls.get(), 0);
    let owner_denied = memory(1);
    let access = Access::new(&xml, xml.snapshot(), &owner_denied);
    assert!(matches!(
        properties::check_time(&signed, &access),
        Err(ClaimedTimeError::Access(AccessError::Xml(Cause::Resource(
            MemoryError::WorkExceeded
        ))))
    ));
    assert_eq!(xml.date_time_calls.get(), 1);
    let exact = memory(2);
    let access = Access::new(&xml, xml.snapshot(), &exact);
    assert!(matches!(
        properties::check_time(&signed, &access).unwrap(),
        ClaimedTimeBinding::Bound(_)
    ));
    assert_eq!(exact.used(), 0);
    assert_eq!(exact.work_used(), 2);
    let calls = xml.date_time_calls.get();
    xml.generation.set(xml.generation.get() + 1);
    let access = Access::new(&xml, xml.snapshot(), &original);
    assert!(matches!(properties::check_time(&signed, &access),
        Err(ClaimedTimeError::Foreign { node }) if node == signed.signing_time()));
    assert_eq!(xml.date_time_calls.get(), calls);
    assert_eq!(original.used(), before);
}

#[test]
fn signed_roles_require_current_baseline_properties_and_preserve_foreign_extensions() {
    // Schema-optional fields can be mandatory in the selected baseline. An
    // extension is retained for explicit checking, never treated as a certificate.
    let mut xml = Tree::normal();
    let memory = funded();
    assert!(matches!(
        signed_roles(&xml, &memory),
        Err(properties::Error::Missing {
            parent: 4,
            local: "SigningCertificateV2"
        })
    ));
    let cert = xml.nodes.len();
    xml.nodes
        .push(element(Some(4), XADES, "SigningCertificateV2"));
    let extra = xml.nodes.len();
    xml.nodes.push(element(Some(4), "urn:extension", "Claim"));
    let reading = signed_roles(&xml, &memory).unwrap();
    assert_eq!(reading.parent(), 4);
    assert_eq!(reading.signing_time(), 5);
    assert_eq!(reading.signing_certificate(), cert);
    assert_eq!(reading.snapshot(), xml.snapshot());
    assert_eq!(reading.signature(), 0);
    assert_eq!(reading.policy(), None);
    assert_eq!(reading.place(), None);
    assert_eq!(reading.role(), None);
    assert_eq!(reading.extensions(), &[extra]);
    assert!(memory.used() > 0);
    drop(reading);
    assert_eq!(memory.used(), 0);
}

#[test]
fn signed_roles_reject_legacy_duplicate_and_out_of_order_nodes() {
    // Coverage does not override the baseline's forbidden legacy forms or
    // sequence/singleton rules, even when their signed bytes are authentic.
    let memory = funded();
    let mut xml = Tree::normal();
    let cert = xml.nodes.len();
    xml.nodes
        .push(element(Some(4), XADES, "SigningCertificate"));
    assert!(
        matches!(signed_roles(&xml, &memory), Err(properties::Error::Forbidden { node }) if node == cert)
    );
    xml.nodes[cert] = element(Some(4), XADES, "SigningCertificateV2");
    let repeated = xml.nodes.len();
    xml.nodes
        .push(element(Some(4), XADES, "SigningCertificateV2"));
    assert!(
        matches!(signed_roles(&xml, &memory), Err(properties::Error::Duplicate { node }) if node == repeated)
    );
    xml.nodes[repeated] = element(Some(4), XADES, "SignaturePolicyIdentifier");
    xml.nodes[cert] = element(Some(4), XADES, "SignerRoleV2");
    assert!(
        matches!(signed_roles(&xml, &memory), Err(properties::Error::Order { node }) if node == repeated)
    );
    assert_eq!(memory.used(), 0);
}

#[test]
fn signed_role_wildcard_does_not_admit_absent_namespace() {
    // XSD 1.0 ##other excludes both the target namespace and absent namespace;
    // an unqualified property must not hide in an unchecked extension catalogue.
    let memory = funded();
    let mut xml = Tree::normal();
    xml.nodes
        .push(element(Some(4), XADES, "SigningCertificateV2"));
    let unqualified = xml.nodes.len();
    xml.nodes.push(element(Some(4), "", "SigningCertificateV2"));
    assert!(
        matches!(signed_roles(&xml, &memory), Err(properties::Error::Unexpected { node }) if node == unqualified)
    );
    assert_eq!(memory.used(), 0);
}

#[test]
fn digest_value_octets_do_not_acquire_a_fictitious_asn_depth() {
    // The same owner decode kernel supports arbitrary hash octets under a
    // zero ASN depth ceiling. Only actual embedded ASN.1 needs nested depth.
    let memory = funded();
    let limits = structured_asn1::Limits {
        max_depth: 0,
        ..structured_asn1::Limits::default()
    };
    let parser =
        structured_asn1::Parser::new(structured_asn1::Encoding::Ber, limits, memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let mut xml = Tree::normal();
    xml.binary = vec![0xff, 0x80, 0x00];
    let location = Location {
        snapshot: xml.snapshot(),
        node: 8,
    };
    let result = binary::with_octets(&xml, location, &operation.execution(), |bytes| {
        assert_eq!(bytes, &[0xff, 0x80, 0]);
        Ok::<_, ()>(bytes.len())
    })
    .unwrap();
    assert_eq!(result, 3);
    assert_eq!(memory.used(), 0);
}

fn certificate_tree() -> (Tree, [usize; 6]) {
    let mut xml = Tree::normal();
    let property = xml.nodes.len();
    xml.nodes
        .push(element(Some(4), XADES, "SigningCertificateV2"));
    let cert = xml.nodes.len();
    xml.nodes.push(element(Some(property), XADES, "Cert"));
    let digest = xml.nodes.len();
    xml.nodes.push(element(Some(cert), XADES, "CertDigest"));
    let method = xml.nodes.len();
    xml.nodes.push(element(Some(digest), DS, "DigestMethod"));
    let value = xml.nodes.len();
    xml.nodes.push(element(Some(digest), DS, "DigestValue"));
    let issuer = xml.nodes.len();
    xml.nodes.push(element(Some(cert), XADES, "IssuerSerialV2"));
    (xml, [property, cert, digest, method, value, issuer])
}

fn header_tree() -> (Tree, usize, usize, usize) {
    let (mut xml, _) = certificate_tree();
    let data = xml.nodes.len();
    xml.nodes.push(element(Some(13), DS, "X509Data"));
    let cert = xml.nodes.len();
    xml.nodes.push(element(Some(data), DS, "X509Certificate"));
    let method = xml.nodes.len();
    xml.nodes
        .push(element(Some(9), DS, "CanonicalizationMethod"));
    xml.digest_algorithm = Some("http://www.w3.org/2006/12/xml-c14n11");
    (xml, data, cert, method)
}

fn preparation_tree() -> Tree {
    let (mut xml, _, _, _) = header_tree();
    let objects = xml.nodes.len();
    xml.nodes
        .push(element(Some(3), XADES, "SignedDataObjectProperties"));
    let format = xml.nodes.len();
    xml.nodes
        .push(element(Some(objects), XADES, "DataObjectFormat"));
    xml.mime(format);
    xml.object_reference = Some("#direct");
    xml.nodes
        .push(element(Some(8), DS, "CanonicalizationMethod"));
    xml.nodes
        .push(element(Some(8), XADES, "EncapsulatedTimeStamp"));
    xml
}

#[test]
fn preparation_ownership_depends_on_original_identities_not_the_xml_view_type() {
    // A scoped owner may be dropped after HRTB inspection. Retained preparation
    // owns node identities/capacity, so equal identity domains must not pin its
    // nominal XML adapter type or require copying the prepared catalogues.
    fn identities_only<
        X: Xml,
        Y: Xml<Id = X::Id, Document = X::Document, Generation = X::Generation>,
    >(
        prepared: prepare::Prepared<X>,
    ) -> prepare::Prepared<Y> {
        prepared
    }
    let memory = funded();
    let xml = preparation_tree();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Timestamped, None).unwrap();
    let pointer = prepared.formats().as_ptr();
    let held = memory.used();
    let moved = identities_only::<Tree, Tree>(prepared);
    assert_eq!(moved.formats().as_ptr(), pointer);
    assert_eq!(moved.bound().containers().snapshot(), xml.snapshot());
    assert_eq!(memory.used(), held);
    drop(moved);
    assert_eq!(memory.used(), 0);
}

#[test]
fn selected_source_preparation_connects_header_properties_formats_and_timestamp_models() {
    // One entrypoint preserves original stage models and their charges. This
    // is source preparation, not verified SigningTime/MIME/certificate/TSA facts.
    let memory = funded();
    let prepared = {
        let xml = preparation_tree();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        let prepared =
            prepare::prepare(&access, 0, unsigned::Requirements::Timestamped, None).unwrap();
        assert_eq!(prepared.bound().containers().signature(), 0);
        assert_eq!(prepared.signed().signature(), 0);
        assert_eq!(prepared.header().key_info(), 13);
        assert_eq!(prepared.certificates().entries().len(), 1);
        assert_eq!(prepared.formats().len(), 1);
        assert_eq!(prepared.formats()[0].reference(), 10);
        assert!(prepared.roles().is_none());
        assert_eq!(prepared.unsigned().entries().len(), 1);
        assert_eq!(prepared.timestamps().len(), 1);
        assert_eq!(prepared.timestamps()[0].node(), 8);
        assert!(prepared.material().is_empty());
        assert!(prepared.validation().is_empty());
        assert_eq!(xml.streamed.get(), 0);
        assert_eq!(xml.decoded_pointer.get(), 0);
        prepared
    };
    assert!(memory.used() > 0);
    drop(prepared);
    assert_eq!(memory.used(), 0);
}

fn append_timestamp(
    xml: &mut Tree,
    parent: usize,
    role: Role,
    count: usize,
) -> (usize, Vec<usize>) {
    let property = xml.nodes.len();
    let (namespace, local) = match role {
        Role::Signature => (XADES, "SignatureTimeStamp"),
        Role::Archive => (XADES_141, "ArchiveTimeStamp"),
        Role::SignatureAndReferences => (XADES_141, "SigAndRefsTimeStampV2"),
        Role::ReferencesOnly => (XADES_141, "RefsOnlyTimeStampV2"),
        Role::AllDataObjects => (XADES, "AllDataObjectsTimeStamp"),
        Role::IndividualDataObjects => panic!("this fixture has no Include instructions"),
    };
    xml.nodes.push(element(Some(parent), namespace, local));
    xml.nodes
        .push(element(Some(property), DS, "CanonicalizationMethod"));
    let mut tokens = Vec::new();
    for _ in 0..count {
        tokens.push(xml.nodes.len());
        xml.nodes
            .push(element(Some(property), XADES, "EncapsulatedTimeStamp"));
    }
    (property, tokens)
}

#[test]
fn complete_timestamp_requirements_keep_own_archive_and_later_tokens() {
    // LT covers ALL incorporated tokens; the same selector's archive scope
    // excludes its own/later unsigned tokens without dropping signed ones.
    use structured_ltv::timestamp::{TimestampPathBinding, TimestampPathFailure, TimestampScope};
    use timestamp::TimestampSelection;
    let work = funded();
    let mut xml = preparation_tree();
    let (earlier, earlier_tokens) = append_timestamp(&mut xml, 7, Role::Archive, 2);
    let (archive, own) = append_timestamp(&mut xml, 7, Role::Archive, 2);
    let (later, later_tokens) = append_timestamp(&mut xml, 7, Role::Signature, 1);
    let access = Access::new(&xml, xml.snapshot(), &work);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Archival, None).unwrap();
    let at = prepared
        .timestamps()
        .iter()
        .position(|stamp| stamp.node() == archive)
        .unwrap();
    let first = &prepared.timestamps()[0];
    let before = work.used();
    let all = timestamp::requirements(&access, &prepared, TimestampSelection::All).unwrap();
    assert_eq!(
        all.scope(),
        TimestampScope::All(Location {
            snapshot: xml.snapshot(),
            node: 0
        })
    );
    let expected = [
        (first.node(), first.tokens()[0], Role::Signature.purpose()),
        (earlier, earlier_tokens[0], Role::Archive.purpose()),
        (earlier, earlier_tokens[1], Role::Archive.purpose()),
        (archive, own[0], Role::Archive.purpose()),
        (archive, own[1], Role::Archive.purpose()),
        (later, later_tokens[0], Role::Signature.purpose()),
    ];
    let actual: Vec<_> = all
        .entries()
        .iter()
        .map(|entry| (entry.property, entry.token, entry.purpose))
        .collect();
    assert_eq!(actual, expected);
    assert!(matches!(
        all.bind_paths(&[], &access).unwrap(),
        TimestampPathBinding::Unproven(TimestampPathFailure::Count)
    ));
    let prior =
        timestamp::requirements(&access, &prepared, TimestampSelection::BeforeArchive(at)).unwrap();
    assert_eq!(
        prior.scope(),
        TimestampScope::BeforeArchive(Location {
            snapshot: xml.snapshot(),
            node: archive
        })
    );
    let actual: Vec<_> = prior
        .entries()
        .iter()
        .map(|entry| (entry.property, entry.token, entry.purpose))
        .collect();
    assert_eq!(actual, expected[..3]);
    drop(prior);
    drop(all);
    assert_eq!(work.used(), before);
    assert_eq!(xml.streamed.get(), 0);
    for limit in [
        Memory::new(Arc::new(MemoryAccount::new(0, 1 << 24))),
        Memory::new(Arc::new(MemoryAccount::new(1 << 20, 0))),
    ] {
        let denied = Access::new(&xml, xml.snapshot(), &limit);
        assert!(matches!(
            timestamp::requirements(&denied, &prepared, TimestampSelection::All),
            Err(timestamp::RequirementError::Input(
                InputError::Resource { .. } | InputError::Access(AccessError::Resource { .. })
            ))
        ));
        assert_eq!(limit.used(), 0);
    }
    xml.generation.set(xml.generation.get() + 1);
    let stale = Access::new(&xml, xml.snapshot(), &work);
    assert!(matches!(
        timestamp::requirements(&stale, &prepared, TimestampSelection::All),
        Err(timestamp::RequirementError::Input(
            InputError::Foreign { .. }
        ))
    ));
}

#[test]
fn archive_prior_timestamp_census_includes_signed_tokens_and_every_earlier_archive_token() {
    // Discovery uses complete original preparation, not a supplied successful
    // token subset. Current/later unsigned tokens cannot be their own antecedents.
    let work = funded();
    let mut xml = preparation_tree();
    let (first, first_tokens) = append_timestamp(&mut xml, 7, Role::Archive, 2);
    let (current, own_tokens) = append_timestamp(&mut xml, 7, Role::Archive, 2);
    let (_, later_tokens) = append_timestamp(&mut xml, 7, Role::Signature, 1);
    let objects = xml
        .nodes
        .iter()
        .position(|record| {
            matches!(
                record.value,
                Node::Element(Name {
                    namespace: XADES,
                    local: "SignedDataObjectProperties"
                })
            )
        })
        .unwrap();
    let (signed, signed_tokens) = append_timestamp(&mut xml, objects, Role::AllDataObjects, 1);
    let access = Access::new(&xml, xml.snapshot(), &work);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Archival, None).unwrap();
    let at = prepared
        .timestamps()
        .iter()
        .position(|stamp| stamp.node() == current)
        .unwrap();
    let original_signature = &prepared.timestamps()[0];
    let expected = [
        (
            original_signature.node(),
            original_signature.tokens()[0],
            Role::Signature.purpose(),
        ),
        (first, first_tokens[0], Role::Archive.purpose()),
        (first, first_tokens[1], Role::Archive.purpose()),
        (signed, signed_tokens[0], Role::AllDataObjects.purpose()),
    ];
    let before = work.used();
    let required = timestamp::requirements(
        &access,
        &prepared,
        timestamp::TimestampSelection::BeforeArchive(at),
    )
    .unwrap();
    assert_eq!(
        required.signature(),
        prepared.bound().containers().signature()
    );
    assert_eq!(
        required.scope(),
        structured_ltv::timestamp::TimestampScope::BeforeArchive(Location {
            snapshot: xml.snapshot(),
            node: current
        })
    );
    let actual: Vec<_> = required
        .entries()
        .iter()
        .map(|entry| (entry.property, entry.token, entry.purpose))
        .collect();
    assert_eq!(actual, expected);
    assert!(required.entries().iter().all(|entry| !own_tokens.contains(&entry.token) && !later_tokens.contains(&entry.token)));
    assert_eq!(xml.streamed.get(), 0);
    assert!(work.used() > before);
    drop(required);
    assert_eq!(work.used(), before);
    assert_prior_timestamp_refusals(&xml, &prepared, at);
    drop(prepared);
    assert_eq!(work.used(), 0);
}

fn assert_prior_timestamp_refusals(xml: &Tree, prepared: &prepare::Prepared<Tree>, at: usize) {
    // Caller misuse, storage/work exhaustion and stale generation never yield
    // an empty successful set or trigger token acquisition/provider side effects.
    use timestamp::{RequirementError, TimestampSelection};
    let work = funded();
    let access = Access::new(xml, xml.snapshot(), &work);
    assert!(matches!(
        timestamp::requirements(
            &access,
            prepared,
            TimestampSelection::BeforeArchive(prepared.timestamps().len())
        ),
        Err(RequirementError::Index { .. })
    ));
    assert!(matches!(
        timestamp::requirements(&access, prepared, TimestampSelection::BeforeArchive(0)),
        Err(RequirementError::Role { .. })
    ));
    assert_eq!(work.used(), 0);
    for limit in [
        Memory::new(Arc::new(MemoryAccount::new(0, 1 << 24))),
        Memory::new(Arc::new(MemoryAccount::new(1 << 20, 0))),
    ] {
        let access = Access::new(xml, xml.snapshot(), &limit);
        assert!(matches!(
            timestamp::requirements(&access, prepared, TimestampSelection::BeforeArchive(at)),
            Err(RequirementError::Input(
                InputError::Resource { .. } | InputError::Access(AccessError::Resource { .. })
            ))
        ));
        assert_eq!(limit.used(), 0);
    }
    xml.generation.set(xml.generation.get() + 1);
    let access = Access::new(xml, xml.snapshot(), &work);
    assert!(matches!(
        timestamp::requirements(&access, prepared, TimestampSelection::BeforeArchive(at)),
        Err(RequirementError::Input(InputError::Foreign { .. }))
    ));
    assert_eq!(work.used(), 0);
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn prior_timestamp_paths_require_each_actual_original_token_not_one_repeated_success() {
    // A real common token check plus actual TSA path can satisfy its own
    // antecedent. It cannot cover a different, still unchecked archive token.
    use structured_pkix::{
        path::WorkingKey,
        temporal::{Instant, LeapTable},
    };
    xml_digest::with_checked_path(|path, _, _, work| {
        let mut xml = preparation_tree();
        append_timestamp(&mut xml, 7, Role::Archive, 1);
        append_timestamp(&mut xml, 7, Role::Archive, 1);
        let access = Access::new(&xml, xml.snapshot(), work);
        let prepared =
            prepare::prepare(&access, 0, unsigned::Requirements::Archival, None).unwrap();
        let stamp = &prepared.timestamps()[0];
        let method = u8::try_from(stamp.method()).unwrap();
        let (provider, token, cert_bytes, _) =
            timestamp_oracle::fixture_with_root(&[b'E', 12, b'M', method]);
        xml.binary = token;
        let parser = structured_asn1::Parser::der(work.clone());
        let certificate = structured_pkix::Certificate::read(&parser, &cert_bytes, 400).unwrap();
        let parent = parser.read(&[], 400).unwrap();
        let operation = parser.read(&[], 1200).unwrap();
        let utc = LeapTable::new(0, 2_000_000_000, &[]).unwrap();
        let context = structured_cms::timestamp::CheckContext {
            certificate: &certificate,
            certificate_parent: &parent,
            key: WorkingKey::standing(certificate.tbs().public_key()),
            requested: None,
            utc: &utc,
            reference: Some(Instant::new(1_800_000_000, 0).unwrap()),
            window: None,
            policy_accuracy: None,
        };
        let check = timestamp::Check {
            xml: &xml,
            provider: &provider,
            context: &context,
        };
        let actor = check
            .retain_actor(prepared.bound(), stamp, 0, &operation, |_, _| {
                Ok::<_, ()>(false)
            })
            .unwrap()
            .expect("actual complete timestamp math and source binding");
        let structured_ltv::pki::TimestampBinding::Bound(bound) =
            path.bind_timestamp(&actor, &utc, work).unwrap()
        else {
            panic!("same original TSA certificate/key and complete generation interval")
        };
        assert_prior_path_receipts(&xml, &prepared, &bound, work);
        assert_eq!(provider.verified.get(), 1);
    });
}

fn assert_prior_path_receipts(
    xml: &Tree,
    prepared: &prepare::Prepared<Tree>,
    path: &structured_ltv::pki::TimestampPath<'_, '_, '_, usize, u8, u8, structured_xades::Xades>,
    work: &Memory,
) {
    use structured_ltv::source::ScopeError;
    use structured_ltv::timestamp::{ObligationError, TimestampPathBinding, TimestampPathFailure};
    use timestamp::TimestampSelection;
    let access = Access::new(xml, xml.snapshot(), work);
    let archives: Vec<_> = prepared
        .timestamps()
        .iter()
        .enumerate()
        .filter_map(|(index, stamp)| (stamp.role() == Role::Archive).then_some(index))
        .collect();
    let first = timestamp::requirements(
        &access,
        prepared,
        TimestampSelection::BeforeArchive(archives[0]),
    )
    .unwrap();
    let paths = [path];
    let before = work.used();
    let TimestampPathBinding::Bound(bound) = first.bind_paths(&paths, &access).unwrap() else {
        panic!("complete actual original antecedent")
    };
    assert!(core::ptr::eq(bound.required(), &raw const first));
    assert_eq!(bound.paths().as_ptr(), paths.as_ptr());
    assert_eq!(work.used(), before);
    // Dispatch/metadata fit, but comparing the three original identity pairs
    // must also be admitted. One step per node is not its HashWork bound.
    let limited = Memory::new(Arc::new(MemoryAccount::new(0, 8)));
    let limited_access = Access::new(xml, xml.snapshot(), &limited);
    assert!(matches!(
        first.bind_paths(&paths, &limited_access),
        Err(ObligationError::Scope(ScopeError::Resource {
            cause: MemoryError::WorkExceeded,
            ..
        }))
    ));
    assert_eq!(limited.used(), 0);
    assert!(matches!(
        first.bind_paths(&[], &access).unwrap(),
        TimestampPathBinding::Unproven(TimestampPathFailure::Count)
    ));
    let second = timestamp::requirements(
        &access,
        prepared,
        TimestampSelection::BeforeArchive(archives[1]),
    )
    .unwrap();
    let repeats = [path, path];
    assert!(matches!(
        second.bind_paths(&repeats, &access).unwrap(),
        TimestampPathBinding::Unproven(TimestampPathFailure::Source { index: 1 })
    ));
    let denied = Memory::new(Arc::new(MemoryAccount::new(0, 0)));
    let denied_access = Access::new(xml, xml.snapshot(), &denied);
    assert!(matches!(
        first.bind_paths(&paths, &denied_access),
        Err(ObligationError::Scope(ScopeError::Resource { .. }))
    ));
    xml.generation.set(xml.generation.get() + 1);
    let stale = Access::new(xml, xml.snapshot(), work);
    assert!(matches!(
        first.bind_paths(&paths, &stale),
        Err(ObligationError::Foreign { .. })
    ));
}

#[test]
fn selected_source_preparation_never_leaves_signed_commitment_unprocessed() {
    // The cohesive entrypoint must reject malformed optional commitments and
    // retain valid associations before any timestamp/certificate execution.
    let memory = funded();
    let mut xml = preparation_tree();
    let parent = xml
        .nodes
        .iter()
        .position(|record| {
            matches!(
                record.value,
                Node::Element(Name {
                    namespace: XADES,
                    local: "SignedDataObjectProperties"
                })
            )
        })
        .unwrap();
    let node = xml.nodes.len();
    xml.nodes
        .push(element(Some(parent), XADES, "CommitmentTypeIndication"));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(prepare::prepare(&access, 0, unsigned::Requirements::Timestamped, None),
        Err(prepare::Error::Commitment(commitment::AssociationError::Property(properties::Error::Missing { parent, local: "CommitmentTypeId" }))) if parent == node)
    );
    assert_eq!(memory.used(), 0);
    let kind = xml.nodes.len();
    xml.nodes
        .push(element(Some(node), XADES, "CommitmentTypeId"));
    let identifier = xml.nodes.len();
    xml.nodes.push(element(Some(kind), XADES, "Identifier"));
    xml.nodes.push(Record {
        parent: Some(identifier),
        value: Node::Text("https://example.test/commitment"),
    });
    xml.nodes
        .push(element(Some(node), XADES, "AllSignedDataObjects"));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Timestamped, None).unwrap();
    assert_eq!(prepared.commitments().len(), 1);
    assert_eq!(prepared.commitments()[0].original().node(), node);
    assert_eq!(prepared.commitments()[0].objects().len(), 1);
    assert_eq!(prepared.commitments()[0].objects()[0].reference, 10);
    assert_eq!(xml.streamed.get(), 0);
    drop(prepared);
    assert_eq!(memory.used(), 0);
}

#[test]
fn preparation_refuses_policy_store_without_a_signed_explicit_policy_hash() {
    // EN6.3(m): unsigned policy bytes cannot qualify a signature with absent or
    // implied policy. Refusal must precede base64 decode/source/provider work.
    let memory = funded();
    let mut xml = preparation_tree();
    let store = xml.nodes.len();
    xml.nodes
        .push(element(Some(7), XADES_141, "SignaturePolicyStore"));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(prepare::prepare(&access, 0, unsigned::Requirements::Timestamped, None),
        Err(prepare::Error::Property(properties::Error::Forbidden { node })) if node == store)
    );
    assert_eq!(memory.used(), 0);
    assert_eq!(xml.streamed.get(), 0);
    assert_eq!(xml.decoded_pointer.get(), 0);
    let declaration = xml.nodes.len();
    xml.nodes
        .push(element(Some(4), XADES, "SignaturePolicyIdentifier"));
    xml.nodes
        .push(element(Some(declaration), XADES, "SignaturePolicyImplied"));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(prepare::prepare(&access, 0, unsigned::Requirements::Timestamped, None),
        Err(prepare::Error::Property(properties::Error::Forbidden { node })) if node == store)
    );
    assert_eq!(memory.used(), 0);
    assert_eq!(xml.decoded_pointer.get(), 0);
}

#[test]
fn selected_source_preparation_refuses_partial_catalogue_and_rolls_back_all_storage() {
    // An incomplete authenticated Manifest catalogue cannot become a successful
    // Prepared value, even though the original signature binding was valid.
    let memory = funded();
    let mut xml = preparation_tree();
    xml.reference_completeness = Completeness::Unchecked(1);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        prepare::prepare(&access, 0, unsigned::Requirements::Basic, None),
        Err(prepare::Error::Format(format::CoverageError::Incomplete {
            node: 1
        }))
    ));
    assert_eq!(memory.used(), 0);
    xml.reference_completeness = Completeness::Complete;
    let no_storage = Memory::new(Arc::new(MemoryAccount::new(0, 300_000)));
    let access = Access::new(&xml, xml.snapshot(), &no_storage);
    assert!(matches!(
        prepare::prepare(&access, 0, unsigned::Requirements::Basic, None),
        Err(prepare::Error::Property(properties::Error::Resource { .. }))
    ));
    assert_eq!(no_storage.used(), 0);
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn preparation_never_ignores_an_empty_renewed_digest_property() {
    // Optional-but-present archive input must satisfy its schema. Preserving
    // its outer name alone must not let missing methods/digests disappear.
    let mut xml = preparation_tree();
    let node = xml.nodes.len();
    xml.nodes
        .push(element(Some(7), XADES_141, "RenewedDigestsV2"));
    let memory = funded();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(prepare::prepare(&access, 0, unsigned::Requirements::Basic, None),
        Err(prepare::Error::Property(properties::Error::Missing { parent, local: "CanonicalizationMethod" })) if parent == node)
    );
    assert_eq!(memory.used(), 0);
}

fn renewed_tree() -> (Tree, usize, usize, usize, usize, usize) {
    let mut xml = preparation_tree();
    let node = xml.nodes.len();
    xml.nodes
        .push(element(Some(7), XADES_141, "RenewedDigestsV2"));
    let canonicalization = xml.nodes.len();
    xml.nodes
        .push(element(Some(node), DS, "CanonicalizationMethod"));
    let digest = xml.nodes.len();
    xml.nodes.push(element(Some(node), DS, "DigestMethod"));
    let pair = xml.nodes.len();
    xml.nodes
        .push(element(Some(node), XADES_141, "RecomputedDigestValue"));
    let object = xml.nodes.len();
    xml.nodes
        .push(element(Some(pair), XADES_141, "NewSDODigestValue"));
    let reference = xml.nodes.len();
    xml.nodes
        .push(element(Some(pair), XADES_141, "OriginalRefDigest"));
    (xml, node, canonicalization, digest, object, reference)
}

#[test]
fn renewed_digest_preparation_retains_both_original_roles_without_hashing_or_promotion() {
    // Reference-instruction and detached-object digests are different inputs.
    // Ordered pairs must survive preparation without replay, acquisition or
    // treating their mere presence as authenticated Manifest/digest agreement.
    let (mut xml, node, method, digest, object, reference) = renewed_tree();
    let second = xml.nodes.len();
    xml.nodes
        .push(element(Some(node), XADES_141, "RecomputedDigestValue"));
    xml.nodes
        .push(element(Some(second), XADES_141, "NewSDODigestValue"));
    xml.nodes
        .push(element(Some(second), XADES_141, "OriginalRefDigest"));
    let memory = funded();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let value = prepare::prepare(&access, 0, unsigned::Requirements::Basic, None).unwrap();
    assert_eq!(value.renewed().len(), 1);
    let renewed = &value.renewed()[0];
    assert_eq!(renewed.snapshot(), xml.snapshot());
    assert_eq!(renewed.signature(), 0);
    assert_eq!(renewed.node(), node);
    assert_eq!(renewed.canonicalization(), method);
    assert_eq!(renewed.digest(), digest);
    assert_eq!(renewed.pairs().len(), 2);
    assert_eq!(renewed.pairs()[0].object, object);
    assert_eq!(renewed.pairs()[0].reference, reference);
    assert_eq!(renewed.pairs()[1].node, second);
    assert_eq!(renewed.pairs()[1].object, second + 1);
    assert_eq!(renewed.pairs()[1].reference, second + 2);
    assert_eq!(xml.streamed.get(), 0);
    assert_eq!(xml.decoded_pointer.get(), 0);
    drop(value);
    assert_eq!(memory.used(), 0);
}

#[test]
fn renewed_digest_shape_rejects_swapped_extra_missing_and_complex_digest_values() {
    // All optional-but-present fields follow the 1.4.1 schema. Never confuse
    // the two digest roles or flatten nested elements into a base64 value.
    for kind in 0..4 {
        let (mut xml, node, _, _, object, reference) = renewed_tree();
        let bad = match kind {
            0 => {
                xml.nodes.swap(object, reference);
                object
            }
            1 => {
                let bad = xml.nodes.len();
                xml.nodes
                    .push(element(Some(object - 1), XADES_141, "OriginalRefDigest"));
                bad
            }
            2 => {
                xml.nodes.pop();
                object - 1
            }
            _ => {
                let bad = xml.nodes.len();
                xml.nodes
                    .push(element(Some(object), "urn:payload", "nested"));
                bad
            }
        };
        let memory = funded();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        let result = prepare::prepare(&access, 0, unsigned::Requirements::Basic, None);
        if kind == 2 {
            assert!(
                matches!(result, Err(prepare::Error::Property(properties::Error::Missing { parent, local: "OriginalRefDigest" })) if parent == bad)
            );
        } else {
            assert!(
                matches!(result, Err(prepare::Error::Property(properties::Error::Unexpected { node })) if node == bad)
            );
        }
        assert_eq!(memory.used(), 0);
        assert_eq!(xml.streamed.get(), 0);
        assert!(node < xml.nodes.len());
    }
}

#[test]
fn renewed_digest_catalogue_uses_current_work_and_releases_partial_capacity() {
    // Retained unsigned metadata supplies no credit for a later query. Stale
    // sources and current storage/work refusal must not produce an empty set.
    let (xml, _, _, _, _, _) = renewed_tree();
    let storage = funded();
    let access = Access::new(&xml, xml.snapshot(), &storage);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let inventory = unsigned::read(&access, &bound, unsigned::Requirements::Basic).unwrap();
    let index = inventory
        .entries()
        .iter()
        .position(|e| e.role == unsigned::Role::RenewedDigests)
        .unwrap();
    let execution = Memory::new(Arc::new(MemoryAccount::new(0, 100_000)));
    let query = Access::new(&xml, xml.snapshot(), &execution);
    assert!(matches!(
        structured_xades::renewed::read(&query, &inventory, index),
        Err(properties::Error::Resource { .. })
    ));
    assert_eq!(execution.used(), 0);
    let execution = Memory::new(Arc::new(MemoryAccount::new(100_000, 0)));
    let query = Access::new(&xml, xml.snapshot(), &execution);
    assert!(matches!(
        structured_xades::renewed::read(&query, &inventory, index),
        Err(properties::Error::Access(AccessError::Resource { .. }))
    ));
    xml.generation.set(2);
    let query = Access::new(&xml, xml.snapshot(), &storage);
    assert!(matches!(
        structured_xades::renewed::read(&query, &inventory, index),
        Err(properties::Error::Access(AccessError::Changed { .. }))
    ));
    drop(inventory);
    assert_eq!(storage.used(), 0);
}

#[test]
fn selected_source_preparation_checks_late_timestamp_shape_without_hashing_or_dropping_it() {
    // Valid early properties do not excuse a malformed later timestamp. The
    // complete preparation fails before source/hash/provider callbacks.
    let memory = funded();
    let mut xml = preparation_tree();
    xml.nodes.pop();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        prepare::prepare(&access, 0, unsigned::Requirements::Timestamped, None),
        Err(prepare::Error::Timestamp(InputError::Missing {
            node: 8,
            local: "EncapsulatedTimeStamp"
        }))
    ));
    assert_eq!(memory.used(), 0);
    assert_eq!(xml.streamed.get(), 0);
    assert_eq!(xml.decoded_pointer.get(), 0);
}

fn role_tree() -> (Tree, usize, usize, usize) {
    let (mut xml, _) = certificate_tree();
    let node = xml.nodes.len();
    xml.nodes.push(element(Some(4), XADES, "SignerRoleV2"));
    let claims = xml.nodes.len();
    xml.nodes.push(element(Some(node), XADES, "ClaimedRoles"));
    xml.nodes.push(element(Some(claims), XADES, "ClaimedRole"));
    let certificates = xml.nodes.len();
    xml.nodes
        .push(element(Some(node), XADES, "CertifiedRolesV2"));
    let certified = xml.nodes.len();
    xml.nodes
        .push(element(Some(certificates), XADES, "CertifiedRole"));
    xml.nodes
        .push(element(Some(certified), XADES, "X509AttributeCertificate"));
    let assertions = xml.nodes.len();
    xml.nodes
        .push(element(Some(node), XADES, "SignedAssertions"));
    xml.nodes
        .push(element(Some(assertions), XADES, "SignedAssertion"));
    (xml, node, certified, assertions)
}

#[test]
fn signed_role_catalogue_keeps_all_original_credential_kinds_without_trust_promotion() {
    // Role claims, an X509 attribute certificate and signed assertion retain
    // independent syntax/provenance, not a single fabricated authenticated role.
    let memory = funded();
    let (xml, node, _, _) = role_tree();
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let reading = role::read(&access, &signed).unwrap().unwrap();
    assert_eq!(reading.snapshot(), xml.snapshot());
    assert_eq!(reading.signature(), 0);
    assert_eq!(reading.node(), node);
    assert_eq!(reading.entries().len(), 3);
    assert_eq!(reading.entries()[0].kind, role::Kind::Claimed);
    assert_eq!(reading.entries()[1].kind, role::Kind::AttributeCertificate);
    assert_eq!(reading.entries()[2].kind, role::Kind::Assertion);
    let held = memory.used();
    drop(reading);
    assert!(memory.used() < held);
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn signed_role_groups_preserve_order_nonempty_members_and_exclusive_certificate_choice() {
    // One valid credential cannot hide a second child in its exclusive choice.
    let memory = funded();
    let (mut xml, _, certified, assertions) = role_tree();
    let extra = xml.nodes.len();
    xml.nodes
        .push(element(Some(certified), XADES, "OtherAttributeCertificate"));
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(role::read(&access, &signed), Err(properties::Error::Unexpected { node }) if node == extra)
    );
    xml.nodes.pop();
    xml.nodes[assertions + 1].parent = Some(1);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(role::read(&access, &signed), Err(properties::Error::Missing { parent, .. }) if parent == assertions)
    );
}

#[test]
fn role_credential_presence_conditions_attribute_material_without_authenticating_it() {
    // §6.3(r/s/w) forbids attribute material with no independent credential.
    // Passing this presence test is not validation of the assertion/certificate.
    let memory = funded();
    let (mut xml, _, _, _) = role_tree();
    let material = xml.nodes.len();
    xml.nodes
        .push(element(Some(7), XADES, "AttributeRevocationValues"));
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let inventory = unsigned::read(&access, &bound, unsigned::Requirements::LongTerm).unwrap();
    assert!(
        matches!(unsigned::check_credential_material(&access, &inventory, None), Err(properties::Error::Forbidden { node }) if node == material)
    );
    let roles = role::read(&access, &signed).unwrap().unwrap();
    // Credential presence is a new retained-source query: reject an unfunded
    // signature equality before traversing roles or inventory entries.
    let short = Memory::new(Arc::new(MemoryAccount::new(0, 1)));
    let lower = Access::new(&xml, xml.snapshot(), &short);
    assert!(matches!(
        unsigned::check_credential_material(&lower, &inventory, Some(&roles)),
        Err(properties::Error::Access(AccessError::Resource {
            cause: MemoryError::WorkExceeded,
            ..
        }))
    ));
    assert_eq!(short.work_remaining(), 1);
    unsigned::check_credential_material(&access, &inventory, Some(&roles)).unwrap();
    let zero = self::memory(0);
    let access = Access::new(&xml, xml.snapshot(), &zero);
    assert!(matches!(
        unsigned::check_credential_material(&access, &inventory, Some(&roles)),
        Err(properties::Error::Access(AccessError::Resource { .. }))
    ));
}

#[test]
fn unsigned_inventory_preserves_profile_requirements_without_claiming_timestamp_evidence() {
    // Table2 has required container presence; it does not make an empty token
    // cryptographically valid or make a material count prove B-LT completeness.
    let memory = funded();
    let mut xml = Tree::normal();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    for requirements in [
        unsigned::Requirements::Basic,
        unsigned::Requirements::Timestamped,
        unsigned::Requirements::LongTerm,
    ] {
        let inventory = unsigned::read(&access, &bound, requirements).unwrap();
        assert_eq!(inventory.snapshot(), xml.snapshot());
        assert_eq!(inventory.signature(), 0);
        assert_eq!(inventory.parent(), Some(7));
        assert_eq!(inventory.entries().len(), 1);
        assert_eq!(
            inventory.entries()[0].role,
            unsigned::Role::SignatureTimestamp
        );
    }
    assert!(matches!(
        unsigned::read(&access, &bound, unsigned::Requirements::Archival),
        Err(properties::Error::Missing {
            parent: 0,
            local: "ArchiveTimeStamp"
        })
    ));
    let archive = xml.nodes.len();
    xml.nodes
        .push(element(Some(7), XADES_141, "ArchiveTimeStamp"));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let inventory = unsigned::read(&access, &bound, unsigned::Requirements::Archival).unwrap();
    assert_eq!(inventory.entries()[1].node, archive);
    assert_eq!(inventory.entries()[1].role, unsigned::Role::Archive);
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn unsigned_reference_only_forms_are_not_long_term_validation_material() {
    // Current baseline forbids legacy spellings at all levels. V2 reference-only
    // forms remain readable for B-B/B-T but cannot substitute for LT/LTA values.
    let memory = funded();
    let mut xml = Tree::normal();
    let reference = xml.nodes.len();
    xml.nodes
        .push(element(Some(7), XADES_141, "CompleteCertificateRefsV2"));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let inventory = unsigned::read(&access, &bound, unsigned::Requirements::Timestamped).unwrap();
    assert_eq!(
        inventory.entries()[1].role,
        unsigned::Role::CertificateReferences
    );
    for requirements in [
        unsigned::Requirements::LongTerm,
        unsigned::Requirements::Archival,
    ] {
        assert!(
            matches!(unsigned::read(&access, &bound, requirements), Err(properties::Error::Forbidden { node }) if node == reference)
        );
    }
    xml.nodes[reference].value = Node::Element(Name {
        namespace: XADES,
        local: "CompleteCertificateRefs",
    });
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(unsigned::read(&access, &bound, unsigned::Requirements::Basic), Err(properties::Error::Forbidden { node }) if node == reference)
    );
}

#[test]
fn unsigned_catalogue_rejects_duplicate_material_and_preserves_foreign_extensions_and_data() {
    // AnyValidationData is repeatable; CertificateValues is singleton. Unknown
    // foreign properties stay explicit, while absent-namespace nodes are invalid.
    let memory = funded();
    let mut xml = Tree::normal();
    let entry = xml.nodes.len();
    xml.nodes.push(element(Some(7), XADES, "CertificateValues"));
    let extra = xml.nodes.len();
    xml.nodes.push(element(Some(7), XADES, "CertificateValues"));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let before = memory.used();
    assert!(
        matches!(unsigned::read(&access, &bound, unsigned::Requirements::Basic), Err(properties::Error::Duplicate { node }) if node == extra)
    );
    assert_eq!(memory.used(), before);
    xml.nodes[entry].value = Node::Element(Name {
        namespace: XADES_141,
        local: "AnyValidationData",
    });
    xml.nodes[extra].value = Node::Element(Name {
        namespace: XADES_141,
        local: "AnyValidationData",
    });
    let foreign = xml.nodes.len();
    xml.nodes
        .push(element(Some(7), "urn:foreign", "FutureProperty"));
    let data = xml.nodes.len();
    xml.nodes
        .push(element(Some(6), XADES, "UnsignedDataObjectProperties"));
    xml.nodes
        .push(element(Some(data), XADES, "UnsignedDataObjectProperty"));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let inventory = unsigned::read(&access, &bound, unsigned::Requirements::Basic).unwrap();
    assert_eq!(inventory.entries().len(), 4);
    assert_eq!(inventory.entries()[3].role, unsigned::Role::Extension);
    assert_eq!(inventory.data_objects(), Some(data));
    xml.nodes[foreign].value = Node::Element(Name {
        namespace: "",
        local: "FutureProperty",
    });
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(unsigned::read(&access, &bound, unsigned::Requirements::Basic), Err(properties::Error::Unexpected { node }) if node == foreign)
    );
}

#[test]
fn unsigned_inventory_capacity_refusal_releases_partial_catalogue() {
    // A failed catalogue cannot be used as an empty successful material set.
    let memory = funded();
    let xml = Tree::normal();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let no_storage = Memory::new(Arc::new(MemoryAccount::new(0, 300_000)));
    let access = Access::new(&xml, xml.snapshot(), &no_storage);
    assert!(matches!(
        unsigned::read(&access, &bound, unsigned::Requirements::Basic),
        Err(properties::Error::Resource { node: 8, .. })
    ));
    assert_eq!(no_storage.used(), 0);
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn baseline_header_requires_original_x509_data_and_selected_certificate_bytes() {
    // EN6.3(a) requires actual signing-certificate inclusion. A nonempty
    // X509Data, certificate count or key-association receipt cannot substitute
    // for comparing the candidate's complete original DER.
    let memory = funded();
    let bytes = candidate_certificate();
    let parser = structured_asn1::Parser::der(memory.clone());
    let candidate = structured_pkix::Certificate::read(&parser, &bytes, 900).unwrap();
    let (mut xml, data, cert, method) = header_tree();
    xml.binary = bytes.clone();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let reading = header::read(&access, &bound).unwrap();
    assert_eq!(reading.snapshot(), xml.snapshot());
    assert_eq!(reading.signature(), 0);
    assert_eq!(reading.key_info(), 13);
    assert_eq!(reading.x509_data(), data);
    assert_eq!(reading.method(), method);
    assert_eq!(reading.certificates(), &[cert]);
    let location = header::included(&xml, &reading, &candidate, &memory)
        .unwrap()
        .unwrap();
    assert_eq!(location.node, cert);
    assert_eq!(location.snapshot, xml.snapshot());
    xml.binary[bytes.len() - 1] ^= 1;
    assert!(
        header::included(&xml, &reading, &candidate, &memory)
            .unwrap()
            .is_none()
    );
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn baseline_header_rejects_missing_or_duplicate_roles_and_unknown_canonicalization() {
    // The baseline fixes one X509Data, at least one certificate and six
    // canonicalization identities, independently of a permissive XML provider.
    let memory = funded();
    let (mut xml, data, cert, method) = header_tree();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    xml.nodes[cert].value = Node::Element(Name {
        namespace: DS,
        local: "X509SubjectName",
    });
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(header::read(&access, &bound), Err(properties::Error::Missing { parent, local: "X509Certificate" }) if parent == data)
    );
    xml.nodes[cert].value = Node::Element(Name {
        namespace: DS,
        local: "X509Certificate",
    });
    let duplicate = xml.nodes.len();
    xml.nodes.push(element(Some(13), DS, "X509Data"));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(header::read(&access, &bound), Err(properties::Error::Duplicate { node }) if node == duplicate)
    );
    xml.nodes.pop();
    for algorithm in [None, Some("urn:unknown-canonicalizer")] {
        xml.digest_algorithm = algorithm;
        let access = Access::new(&xml, xml.snapshot(), &memory);
        assert!(
            matches!(header::read(&access, &bound), Err(properties::Error::Unexpected { node }) if node == method)
        );
    }
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn baseline_header_keeps_resource_and_generation_failure_separate_from_missing_certificate() {
    // All header catalogue capacity and certificate scans belong to the current
    // operation. Failure cannot return a partial successful header or mismatch.
    let memory = funded();
    let (xml, _, _, _) = header_tree();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let zero = self::memory(0);
    let access = Access::new(&xml, xml.snapshot(), &zero);
    assert!(matches!(
        header::read(&access, &bound),
        Err(properties::Error::Child(
            structured_xades::xml::ChildError::Access(AccessError::Resource {
                cause: MemoryError::WorkExceeded,
                ..
            })
        ))
    ));
    let no_storage = Memory::new(Arc::new(MemoryAccount::new(0, 300_000)));
    let access = Access::new(&xml, xml.snapshot(), &no_storage);
    assert!(matches!(
        header::read(&access, &bound),
        Err(properties::Error::Resource { .. })
    ));
    assert_eq!(no_storage.used(), 0);
    xml.generation.set(2);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        header::read(&access, &bound),
        Err(properties::Error::Access(AccessError::Changed { node: 0 }))
    ));
}

#[test]
fn signing_certificate_references_preserve_all_original_roles_and_owned_capacity() {
    // First reference remains first: an acquisition/issuer hint is not a digest
    // binding and this schema reading cannot promote it into path acceptance.
    let memory = funded();
    let (xml, [property, cert, _, method, value, issuer]) = certificate_tree();
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let refs = certificate::read(&access, &signed).unwrap();
    assert_eq!(refs.snapshot(), xml.snapshot());
    assert_eq!(refs.signature(), 0);
    assert_eq!(refs.node(), property);
    assert_eq!(refs.entries().len(), 1);
    let reference = refs.entries()[0];
    assert_eq!(reference.node(), cert);
    assert_eq!(reference.method(), method);
    assert_eq!(reference.digest(), value);
    assert_eq!(reference.issuer_serial(), Some(issuer));
    assert!(memory.used() > 0);
    drop(refs);
    assert_eq!(memory.used(), 0);
}

#[test]
fn certificate_reference_schema_never_borrows_fields_from_adjacent_roles() {
    // Exact expanded names and direct order are required. Empty or incomplete
    // certificate entries cannot borrow digest fields from another subtree.
    let memory = funded();
    let (mut xml, [property, cert, digest, _, value, _]) = certificate_tree();
    xml.nodes[value] = element(Some(digest), XADES, "DigestValue");
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(certificate::read(&access, &signed), Err(properties::Error::Unexpected { node }) if node == value)
    );
    xml.nodes[value] = element(Some(cert), DS, "DigestValue");
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(certificate::read(&access, &signed), Err(properties::Error::Unexpected { node }) if node == value)
    );
    xml.nodes[cert].parent = None;
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(certificate::read(&access, &signed), Err(properties::Error::Missing { parent, local: "Cert" }) if parent == property)
    );
    assert_eq!(memory.used(), 0);
}

#[test]
fn certificate_reference_retention_refusal_is_not_an_empty_success() {
    // Header traversal needs no storage, but publishing the actual catalogue
    // reserves its complete capacity first and preserves an admission error.
    let memory = memory(100_000);
    let (xml, _) = certificate_tree();
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        certificate::read(&access, &signed),
        Err(properties::Error::Resource { .. })
    ));
    assert_eq!(memory.used(), 0);
}

fn candidate_certificate() -> Vec<u8> {
    // Synthetic readable certificate, not a signature or trust fixture.
    fn tlv(tag: u8, bytes: &[u8]) -> Vec<u8> {
        assert!(bytes.len() < 128);
        let mut value = vec![tag, u8::try_from(bytes.len()).unwrap()];
        value.extend_from_slice(bytes);
        value
    }
    let algorithm = [0x30, 3, 6, 1, 42];
    let name = [0x30, 12, 0x31, 10, 0x30, 8, 6, 3, 0x55, 4, 3, 0x0c, 1, b'a'];
    let mut tbs = vec![0xa0, 3, 2, 1, 2, 2, 1, 1];
    tbs.extend(algorithm);
    tbs.extend(name);
    let mut validity = tlv(0x17, b"260101000000Z");
    validity.extend(tlv(0x17, b"270101000000Z"));
    tbs.extend(tlv(0x30, &validity));
    tbs.extend(name);
    let mut key = algorithm.to_vec();
    key.extend([3, 2, 0, 1]);
    tbs.extend(tlv(0x30, &key));
    let mut cert = tlv(0x30, &tbs);
    cert.extend(algorithm);
    cert.extend([3, 2, 0, 8]);
    tlv(0x30, &cert)
}

#[derive(Clone, Copy)]
enum DigestMode {
    Available,
    Unsupported,
    Malformed,
    Refused,
}

struct WholeCertificate {
    mode: DigestMode,
    method: usize,
    calls: Cell<usize>,
    pointer: Cell<usize>,
}

impl certificate::DigestProvider<'_, Tree> for WholeCertificate {
    fn classify(&self, (): &()) -> structured_xades::hashes::HashClass {
        // Scripted non-MD5 capability for byte-flow controls only; this
        // checksum oracle is explicitly not cryptographic qualification.
        structured_xades::hashes::HashClass::OtherKnown
    }

    fn resolve<W: Work>(
        &self,
        access: &Access<'_, Tree, W>,
        method: usize,
    ) -> Result<DigestResolution<()>, MemoryError> {
        // Test-only resolver: it checks source selection, not real URI policy.
        assert_eq!(method, self.method);
        assert!(
            access
                .has_name(
                    method,
                    Name {
                        namespace: DS,
                        local: "DigestMethod"
                    }
                )
                .unwrap()
        );
        match self.mode {
            DigestMode::Available => Ok(DigestResolution::Supported(())),
            DigestMode::Unsupported => Ok(DigestResolution::Unsupported),
            DigestMode::Malformed => Ok(DigestResolution::Malformed),
            DigestMode::Refused => Err(MemoryError::WorkExceeded),
        }
    }
}

// Deliberate non-cryptographic XOR checksum checks data flow and admission.
// This test cannot qualify cryptographic math, XML policy or path trust.
impl Digest<'_> for WholeCertificate {
    type Identity = ();
    type State = u8;
    type Output = [u8; 1];
    type Error = MemoryError;

    fn width(&self, (): &()) -> Option<usize> {
        Some(1)
    }
    fn compare<W: Work>(&self, (): &(), (): &(), _: &W) -> Result<Ordering, MemoryError> {
        Ok(Ordering::Equal)
    }
    fn start<W: Work>(&self, (): &(), _: &W) -> Result<DigestResolution<u8>, MemoryError> {
        Ok(DigestResolution::Supported(0))
    }
    fn update<W: Work>(
        &self,
        checksum: &mut u8,
        certificate: &[u8],
        execution: &W,
    ) -> Result<(), MemoryError> {
        execution.charge_work(u64::try_from(certificate.len()).unwrap())?;
        self.calls.set(self.calls.get() + 1);
        self.pointer.set(certificate.as_ptr() as usize);
        for byte in certificate {
            *checksum ^= byte;
        }
        Ok(())
    }
    fn finish<W: Work>(&self, checksum: u8, _: &W) -> Result<[u8; 1], MemoryError> {
        Ok([checksum])
    }
    fn matches<W: Work>(
        &self,
        (): &(),
        output: &[u8; 1],
        expected: &[u8],
        execution: &W,
    ) -> Result<bool, MemoryError> {
        execution.charge_work(u64::try_from(expected.len()).unwrap())?;
        Ok(expected == output)
    }
}

#[test]
fn certificate_digest_compares_complete_original_bytes_without_materialization() {
    // Only the selected provider hashes: the core passes the exact original
    // certificate pointer and never re-encodes or accumulates a chunk copy.
    let memory = funded();
    let bytes = candidate_certificate();
    let parser = structured_asn1::Parser::der(memory.clone());
    let candidate = structured_pkix::Certificate::read(&parser, &bytes, 900).unwrap();
    let mut checksum = 0;
    for byte in &bytes {
        checksum ^= byte;
    }
    let (mut xml, [_, _, _, method, _, _]) = certificate_tree();
    xml.binary = vec![checksum];
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let refs = certificate::read(&access, &signed).unwrap();
    let provider = WholeCertificate {
        mode: DigestMode::Available,
        method,
        calls: Cell::new(0),
        pointer: Cell::new(0),
    };
    let held = memory.used();
    assert_eq!(
        certificate::compare(&xml, &refs, 0, &candidate, &provider, &memory).unwrap(),
        certificate::Agreement::Matches
    );
    assert_eq!(provider.calls.get(), 1);
    assert_eq!(provider.pointer.get(), bytes.as_ptr() as usize);
    assert_eq!(memory.used(), held);
    xml.binary = vec![checksum ^ 1];
    assert_eq!(
        certificate::compare(&xml, &refs, 0, &candidate, &provider, &memory).unwrap(),
        certificate::Agreement::Differs
    );
}

#[test]
fn certificate_digest_preflights_width_and_keeps_provider_refusal_distinct() {
    // Unsupported/malformed method and impossible value width do not hash the
    // certificate. A callback resource error never becomes a semantic mismatch.
    let memory = funded();
    let bytes = candidate_certificate();
    let parser = structured_asn1::Parser::der(memory.clone());
    let candidate = structured_pkix::Certificate::read(&parser, &bytes, 900).unwrap();
    let (mut xml, [_, _, _, method, _, _]) = certificate_tree();
    xml.binary = vec![0, 0];
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let refs = certificate::read(&access, &signed).unwrap();
    let mut provider = WholeCertificate {
        mode: DigestMode::Available,
        method,
        calls: Cell::new(0),
        pointer: Cell::new(0),
    };
    for (mode, expected) in [
        (
            DigestMode::Available,
            certificate::Agreement::MalformedValue,
        ),
        (DigestMode::Unsupported, certificate::Agreement::Unsupported),
        (
            DigestMode::Malformed,
            certificate::Agreement::MalformedMethod,
        ),
    ] {
        provider.mode = mode;
        assert_eq!(
            certificate::compare(&xml, &refs, 0, &candidate, &provider, &memory).unwrap(),
            expected
        );
    }
    assert_eq!(provider.calls.get(), 0);
    provider.mode = DigestMode::Refused;
    assert!(matches!(
        certificate::compare(&xml, &refs, 0, &candidate, &provider, &memory),
        Err(binary::Error::Callback {
            cause: certificate::DigestError::Provider(MemoryError::WorkExceeded),
            ..
        })
    ));
    assert!(matches!(
        certificate::compare(&xml, &refs, 1, &candidate, &provider, &memory),
        Err(binary::Error::Callback {
            cause: certificate::DigestError::ReferenceIndex { index: 1 },
            ..
        })
    ));
    assert_eq!(provider.calls.get(), 0);
}

#[test]
fn encapsulated_default_der_does_not_accept_a_ber_outer_header() {
    // EN319132-1 §5.1.3 makes absent Encoding mean DER. A readable BER token
    // must not silently inherit the envelope reader's leniency in that branch.
    let memory = funded();
    let parser = structured_asn1::Parser::ber(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let mut xml = Tree::normal();
    xml.binary = binary_token();
    let length = xml.binary[1];
    xml.binary[1] = 0x81;
    xml.binary.insert(2, length);
    let location = Location {
        snapshot: xml.snapshot(),
        node: 8,
    };
    let called = Cell::new(false);
    assert!(
        binary::with_token(&xml, location, &operation, |_, _| {
            called.set(true);
            Ok::<_, ()>(())
        })
        .is_err()
    );
    assert!(!called.get());
    assert_eq!(memory.used(), 0);
    xml.binary_encoding = Some(" \thttp://uri.etsi.org/01903/v1.2.2#BER\n");
    binary::with_token(&xml, location, &operation, |_, _| {
        called.set(true);
        Ok::<_, ()>(())
    })
    .unwrap();
    assert!(called.get());
    assert_eq!(memory.used(), 0);
}

#[test]
fn unsupported_pki_encoding_is_not_malformed_or_silently_treated_as_ber() {
    // Recognized unimplemented codecs and unknown Encoding URIs have distinct
    // outcomes, both before decoded-capacity admission and token callbacks.
    let memory = funded();
    let parser = structured_asn1::Parser::ber(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let mut xml = Tree::normal();
    xml.binary = binary_token();
    let location = Location {
        snapshot: xml.snapshot(),
        node: 8,
    };
    let called = Cell::new(false);
    for (uri, encoding) in [
        (
            "http://uri.etsi.org/01903/v1.2.2#CER",
            binary::PkiEncoding::Cer,
        ),
        (
            "http://uri.etsi.org/01903/v1.2.2#PER",
            binary::PkiEncoding::Per,
        ),
        (
            "http://uri.etsi.org/01903/v1.2.2#XER",
            binary::PkiEncoding::Xer,
        ),
    ] {
        xml.binary_encoding = Some(uri);
        assert!(
            matches!(binary::with_token(&xml, location, &operation, |_, _| {
            called.set(true);
            Ok::<_, ()>(())
        }), Err(binary::Error::UnsupportedEncoding { encoding: actual, .. }) if actual == encoding)
        );
    }
    xml.binary_encoding = Some("urn:unknown");
    assert!(matches!(
        binary::with_token(&xml, location, &operation, |_, _| {
            called.set(true);
            Ok::<_, ()>(())
        }),
        Err(binary::Error::UnknownEncoding { .. })
    ));
    assert!(!called.get());
    assert_eq!(memory.used(), 0);
}

#[test]
fn certificate_digest_alone_does_not_prove_selected_signature_key_association() {
    // A missing receipt is distinct from failed math or distrust. Even a full
    // digest match must not manufacture the missing selected-key association.
    fn identity_domain<
        'v,
        'd,
        X: Xml,
        Y: Xml<Id = X::Id, Document = X::Document, Generation = X::Generation>,
    >(
        value: certificate::Checked<'v, 'd, X>,
    ) -> certificate::Checked<'v, 'd, Y> {
        value
    }
    let memory = funded();
    let bytes = candidate_certificate();
    let parser = structured_asn1::Parser::der(memory.clone());
    let candidate = structured_pkix::Certificate::read(&parser, &bytes, 900).unwrap();
    let mut checksum = 0;
    for byte in &bytes {
        checksum ^= byte;
    }
    let (mut xml, [_, _, _, method, _, _]) = certificate_tree();
    xml.binary = vec![checksum];
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let signed = properties::read(&access, &bound).unwrap();
    let refs = certificate::read(&access, &signed).unwrap();
    let provider = WholeCertificate {
        mode: DigestMode::Available,
        method,
        calls: Cell::new(0),
        pointer: Cell::new(0),
    };
    for association in [
        certificate::KeyAssociation::Unproven,
        certificate::KeyAssociation::Differs,
        certificate::KeyAssociation::Same,
    ] {
        xml.key_association = association;
        let checked =
            certificate::check(&xml, &bound, &refs, &candidate, &provider, &memory).unwrap();
        let checked = identity_domain::<Tree, Tree>(checked);
        assert_eq!(checked.digest(), certificate::Agreement::Matches);
        assert_eq!(checked.key(), association);
        assert_eq!(
            checked.is_bound(),
            association == certificate::KeyAssociation::Same
        );
        assert!(core::ptr::eq(
            checked.certificate(),
            core::ptr::from_ref(&candidate)
        ));
        assert!(core::ptr::eq(
            checked.reference(),
            core::ptr::from_ref(&refs)
        ));
    }
}

#[test]
fn certificate_catalogue_from_another_signature_is_rejected_before_provider_work() {
    // Both signatures are in one source generation. Matching node-local numbers
    // or certificate digests cannot move one signature's signed properties to another.
    let memory = funded();
    let bytes = candidate_certificate();
    let parser = structured_asn1::Parser::der(memory.clone());
    let candidate = structured_pkix::Certificate::read(&parser, &bytes, 900).unwrap();
    let (mut xml, [_, _, _, method, _, _]) = certificate_tree();
    let other_signature = xml.nodes.len();
    xml.nodes.push(element(Some(1), DS, "Signature"));
    let object = xml.nodes.len();
    xml.nodes.push(element(Some(other_signature), DS, "Object"));
    let qualifying = xml.nodes.len();
    xml.nodes
        .push(element(Some(object), XADES, "QualifyingProperties"));
    let properties = xml.nodes.len();
    xml.nodes
        .push(element(Some(qualifying), XADES, "SignedProperties"));
    let parent = xml.nodes.len();
    xml.nodes.push(element(
        Some(properties),
        XADES,
        "SignedSignatureProperties",
    ));
    xml.nodes.push(element(Some(parent), XADES, "SigningTime"));
    let signed_info = xml.nodes.len();
    xml.nodes
        .push(element(Some(other_signature), DS, "SignedInfo"));
    let first = xml.nodes.len();
    xml.nodes.push(element(Some(signed_info), DS, "Reference"));
    let second = xml.nodes.len();
    xml.nodes.push(element(Some(signed_info), DS, "Reference"));
    xml.nodes
        .push(element(Some(other_signature), DS, "SignatureValue"));
    let refs = {
        let signed = signed_roles(&xml, &memory).unwrap();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        certificate::read(&access, &signed).unwrap()
    };
    xml.target = Target::LocalBareName(other_signature);
    xml.references[0].node = first;
    xml.references[1].node = second;
    xml.references[1].target = Some(properties);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, other_signature).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let provider = WholeCertificate {
        mode: DigestMode::Available,
        method,
        calls: Cell::new(0),
        pointer: Cell::new(0),
    };
    // Even a foreign catalogue cannot perform its identity comparison for free.
    // Admission must precede ForeignCatalogue and all provider callbacks.
    for budget in [0, 1] {
        let current = Memory::new(Arc::new(MemoryAccount::new(0, budget)));
        assert!(matches!(
            certificate::check(&xml, &bound, &refs, &candidate, &provider, &current),
            Err(certificate::CheckError::Access(AccessError::Resource {
                node, cause: MemoryError::WorkExceeded
            })) if node == refs.node()
        ));
        assert_eq!(current.used(), 0);
        assert_eq!(provider.calls.get(), 0);
    }
    assert!(
        matches!(certificate::check(&xml, &bound, &refs, &candidate, &provider, &memory), Err(certificate::CheckError::ForeignCatalogue { signature, .. }) if signature == other_signature)
    );
    assert_eq!(provider.calls.get(), 0);
}

#[test]
fn issuer_hint_reuses_shared_kernel_and_retains_faults_with_xml_location() {
    // The hint is a standalone RFC5035 IssuerSerial, not a fake ESS attribute.
    // Empty GeneralNames remains readable with its original adverse diagnostic.
    let memory = funded();
    let retained = {
        let (mut xml, [_, _, _, _, _, issuer]) = certificate_tree();
        xml.binary = vec![0x30, 5, 0x30, 0, 2, 1, 7];
        let signed = signed_roles(&xml, &memory).unwrap();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        let refs = certificate::read(&access, &signed).unwrap();
        let parser = structured_asn1::Parser::ber(memory.clone());
        let operation = parser.read(&[], 900).unwrap();
        let value = certificate::with_issuer_serial(&xml, &refs, 0, &operation, |reading| {
            assert_eq!(reading.value().serial().as_u64(), Some(7));
            let (_, faults) = reading.into_parts();
            Ok::<_, ()>(faults)
        })
        .unwrap()
        .unwrap();
        assert_eq!(
            value.location,
            Location {
                snapshot: xml.snapshot(),
                node: issuer
            }
        );
        value
    };
    assert_eq!(retained.value.len(), 1);
    assert_eq!(
        retained.value[0].code(),
        structured_cms::ErrorCode::AttributeSyntax
    );
    assert_eq!(retained.value[0].field(), "xades.issuerSerialV2");
    assert_eq!(retained.value[0].offset(), 2); // Decoded source, never XML byte 902.
    assert!(memory.used() > 0);
    drop(retained);
    assert_eq!(memory.used(), 0);
}

#[test]
fn issuer_hint_comparison_reuses_ess_name_semantics_and_current_query_funding() {
    // Standalone hints use the same serial/name comparison as CMS ESS, including
    // matching normalized issuer names from independently retained certificates.
    let certificate_account = funded();
    let bytes = candidate_certificate();
    let cert_parser = structured_asn1::Parser::der(certificate_account.clone());
    let candidate = structured_pkix::Certificate::read(&cert_parser, &bytes, 900).unwrap();
    let held = certificate_account.used();
    let old_work = certificate_account.work_used();
    for (name, serial, matches) in [
        (b'a', 1, true),
        (b'A', 1, true),
        (b'b', 1, false),
        (b'a', 2, false),
    ] {
        let memory = funded();
        let (mut xml, [_, _, _, _, _, issuer]) = certificate_tree();
        xml.binary = vec![
            0x30, 21, 0x30, 16, 0xa4, 14, 0x30, 12, 0x31, 10, 0x30, 8, 6, 3, 0x55, 4, 3, 0x0c, 1,
            name, 2, 1, serial,
        ];
        let signed = signed_roles(&xml, &memory).unwrap();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        let refs = certificate::read(&access, &signed).unwrap();
        let parser = structured_asn1::Parser::der(memory.clone());
        let operation = parser.read(&[], 900).unwrap();
        let located = certificate::with_issuer_serial(&xml, &refs, 0, &operation, |reading| {
            assert!(reading.faults().is_empty());
            reading.matches_certificate(&candidate, &operation)
        })
        .unwrap()
        .unwrap();
        assert_eq!(located.location.node, issuer);
        assert_eq!(
            located.value,
            if matches {
                structured_cms::ess::IssuerAgreement::Matches
            } else {
                structured_cms::ess::IssuerAgreement::Differs
            }
        );
        assert_eq!(certificate_account.used(), held);
        assert_eq!(certificate_account.work_used(), old_work);
    }
}

#[test]
fn issuer_hint_comparison_preserves_query_local_refusal_instead_of_mismatch() {
    // Comparing a retained hint under a new query is legitimate. Its exhausted
    // local work ceiling must refuse before normalization, not ForeignReader.
    let memory = funded();
    let bytes = candidate_certificate();
    let certificate_parser = structured_asn1::Parser::der(memory.clone());
    let candidate = structured_pkix::Certificate::read(&certificate_parser, &bytes, 900).unwrap();
    let (mut xml, _) = certificate_tree();
    xml.binary = vec![
        0x30, 21, 0x30, 16, 0xa4, 14, 0x30, 12, 0x31, 10, 0x30, 8, 6, 3, 0x55, 4, 3, 0x0c, 1, b'a',
        2, 1, 1,
    ];
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let refs = certificate::read(&access, &signed).unwrap();
    let source = structured_asn1::Parser::der(memory.clone());
    let operation = source.read(&[], 900).unwrap();
    let exhausted = structured_asn1::Parser::new(
        structured_asn1::Encoding::Der,
        structured_asn1::Limits {
            max_work: 0,
            ..structured_asn1::Limits::default()
        },
        memory.clone(),
    );
    let query = exhausted.read(&[], 1000).unwrap();
    let held = memory.used();
    let error = certificate::with_issuer_serial(&xml, &refs, 0, &operation, |reading| {
        reading.matches_certificate(&candidate, &query)
    })
    .unwrap_err();
    let binary::Error::Callback {
        cause: certificate::IssuerError::Callback(cause),
        ..
    } = error
    else {
        panic!("comparison refusal was rewritten");
    };
    assert!(cause.is_resource());
    assert_eq!(cause.field(), "xades.issuerSerialV2");
    assert_eq!(cause.offset(), 0);
    assert_eq!(memory.used(), held);
}

#[test]
fn issuer_hint_schema_fixed_der_cannot_be_overridden_by_an_encoding_attribute() {
    // IssuerSerialV2 is fixed DER by §5.2.2, unlike generic EncapsulatedPKIData.
    // An unrelated attribute cannot grant permissive ASN.1 parsing or a callback.
    let memory = funded();
    let (mut xml, _) = certificate_tree();
    xml.binary = vec![0x30, 0x81, 5, 0x30, 0, 2, 1, 7];
    xml.binary_encoding = Some("http://uri.etsi.org/01903/v1.2.2#BER");
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let refs = certificate::read(&access, &signed).unwrap();
    let parser = structured_asn1::Parser::ber(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let called = Cell::new(false);
    assert!(matches!(
        certificate::with_issuer_serial(&xml, &refs, 0, &operation, |_| {
            called.set(true);
            Ok::<_, ()>(())
        }),
        Err(binary::Error::Callback {
            cause: certificate::IssuerError::Encoding(_),
            ..
        })
    ));
    assert!(!called.get());
}

#[test]
fn absent_issuer_hint_still_checks_generation_without_inventing_asn_work() {
    // Absence is optional metadata, not a fictitious nested ASN.1 value. The
    // original-generation guard still runs before returning that absence.
    let memory = funded();
    let (mut xml, [_, _, _, _, _, issuer]) = certificate_tree();
    xml.nodes[issuer].parent = None;
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let refs = certificate::read(&access, &signed).unwrap();
    let limits = structured_asn1::Limits {
        max_depth: 0,
        ..structured_asn1::Limits::default()
    };
    let parser =
        structured_asn1::Parser::new(structured_asn1::Encoding::Ber, limits, memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    assert!(
        certificate::with_issuer_serial(&xml, &refs, 0, &operation, |_| Ok::<_, ()>(()))
            .unwrap()
            .is_none()
    );
    xml.generation.set(2);
    assert!(matches!(
        certificate::with_issuer_serial(&xml, &refs, 0, &operation, |_| Ok::<_, ()>(())),
        Err(binary::Error::Access {
            cause: AccessError::Changed { .. },
            ..
        })
    ));
}

#[test]
fn baseline_md5_and_missing_method_refuse_before_provider_or_value_decode() {
    // The baseline's unconditional MD5 exclusion is a format rule, distinct
    // from provider availability or caller-deployment algorithm preferences.
    let memory = funded();
    let bytes = candidate_certificate();
    let parser = structured_asn1::Parser::der(memory.clone());
    let candidate = structured_pkix::Certificate::read(&parser, &bytes, 900).unwrap();
    let (mut xml, [_, _, _, method, _, _]) = certificate_tree();
    xml.binary = vec![0; 100_000]; // Copying this would exceed the account.
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let refs = certificate::read(&access, &signed).unwrap();
    let provider = WholeCertificate {
        mode: DigestMode::Refused,
        method,
        calls: Cell::new(0),
        pointer: Cell::new(0),
    };
    let held = memory.used();
    for (uri, answer) in [
        (
            Some(" \thttp://www.w3.org/2001/04/xmldsig-more#md5\n"),
            certificate::Agreement::ProhibitedAlgorithm,
        ),
        (None, certificate::Agreement::MalformedMethod),
    ] {
        xml.digest_algorithm = uri;
        assert_eq!(
            certificate::compare(&xml, &refs, 0, &candidate, &provider, &memory).unwrap(),
            answer
        );
        assert_eq!(memory.used(), held);
    }
    assert_eq!(provider.calls.get(), 0);
}

#[test]
fn owned_binary_transfer_keeps_pointer_source_and_charge_after_owner_drop() {
    // Material retention uses the decoder's actual allocation, not a second
    // copy. XML source identity and charge survive owner/scope destruction.
    let memory = funded();
    let retained = {
        let mut xml = Tree::normal();
        xml.binary = vec![3, 4, 5];
        let location = Location {
            snapshot: xml.snapshot(),
            node: 8,
        };
        let retained = binary::owned(&xml, location, &memory).unwrap();
        assert_eq!(retained.location, location);
        assert_eq!(retained.value.as_ptr() as usize, xml.decoded_pointer.get());
        retained
    };
    assert_eq!(&*retained.value, &[3, 4, 5]);
    assert_eq!(memory.used(), retained.value.capacity());
    drop(retained);
    assert_eq!(memory.used(), 0);
}

#[test]
fn moved_cached_binary_storage_stays_on_its_original_account() {
    // A real move does not charge storage a second time to a new operation.
    // Current traversal/dispatch work is still paid by that new operation.
    let storage = funded();
    let mut buffer = storage.vector();
    buffer.try_extend_from_slice(&[3, 4, 5]).unwrap();
    let pointer = buffer.as_ptr();
    let charged = storage.used();
    let xml = Tree::normal();
    *xml.cached_binary.borrow_mut() = Some(buffer);
    let query = memory(2);
    let value = binary::owned(
        &xml,
        Location {
            snapshot: xml.snapshot(),
            node: 8,
        },
        &query,
    )
    .unwrap();
    assert_eq!(value.value.as_ptr(), pointer);
    assert_eq!(query.used(), 0);
    assert_eq!(query.work_used(), 2);
    assert_eq!(storage.used(), charged);
    drop(value);
    assert_eq!(storage.used(), 0);
}

#[test]
fn owned_binary_generation_and_capacity_refusals_release_partial_output() {
    // A generation change cannot publish the new owned value; failed storage
    // admission is an error, not a successful empty collateral buffer.
    let memory = funded();
    let mut xml = Tree::normal();
    xml.binary = vec![3, 4, 5];
    xml.mutate_binary = true;
    let location = Location {
        snapshot: xml.snapshot(),
        node: 8,
    };
    assert!(matches!(
        binary::owned(&xml, location, &memory),
        Err(AccessError::Changed { node: 8 })
    ));
    assert_eq!(memory.used(), 0);
    xml.mutate_binary = false;
    let query = memory_for_owned_refusal();
    assert!(matches!(
        binary::owned(
            &xml,
            Location {
                snapshot: xml.snapshot(),
                node: 8
            },
            &query
        ),
        Err(AccessError::Xml(Cause::Resource(
            MemoryError::LimitExceeded
        )))
    ));
    assert_eq!(query.used(), 0);
}

fn memory_for_owned_refusal() -> Memory {
    Memory::new(Arc::new(MemoryAccount::new(0, 1000)))
}

#[test]
fn simultaneous_owned_sources_keep_independent_bytes_and_xml_locations() {
    // Two material models can borrow distinct retained sources simultaneously;
    // neither requires nested callbacks, a second copy or a synthetic joined source.
    let memory = funded();
    let operation_parser = structured_asn1::Parser::der(memory.clone());
    let operation = operation_parser.read(&[], 900).unwrap();
    let mut xml = Tree::normal();
    xml.binary = vec![2, 1, 7];
    let first_location = Location {
        snapshot: xml.snapshot(),
        node: 5,
    };
    let first = binary::owned_source(&xml, first_location, &operation).unwrap();
    let first_pointer = first.value.bytes.as_ptr();
    xml.binary = vec![2, 1, 9];
    let second_location = Location {
        snapshot: xml.snapshot(),
        node: 8,
    };
    let second = binary::owned_source(&xml, second_location, &operation).unwrap();
    drop(xml);
    let mut catalogue = memory.vector();
    catalogue.try_push(first).unwrap();
    catalogue.try_push(second).unwrap();
    assert_eq!(catalogue[0].value.bytes.as_ptr(), first_pointer);
    assert_eq!(catalogue[0].location, first_location);
    assert_eq!(catalogue[1].location, second_location);
    {
        let mut left = catalogue[0]
            .value
            .reader_in(&operation, structured_asn1::Encoding::Der)
            .unwrap();
        let mut right = catalogue[1]
            .value
            .reader_in(&operation, structured_asn1::Encoding::Der)
            .unwrap();
        let a = left.next_element().unwrap().unwrap();
        let b = right.next_element().unwrap().unwrap();
        assert_eq!(left.integer(&a).unwrap().as_u64(), Some(7));
        assert_eq!(right.integer(&b).unwrap().as_u64(), Some(9));
        assert_eq!(a.as_bytes().as_ptr(), first_pointer);
    }
    drop(catalogue);
    assert_eq!(memory.used(), 0);
}

#[test]
fn owned_source_preserves_origin_work_depth_and_current_operation_refusal() {
    // Moving a source cannot reset its cumulative local budget or divert work
    // to its storage account. Both original coordinates and current limits apply.
    let storage = funded();
    let mut bytes = storage.vector();
    bytes.try_extend_from_slice(&[2, 1, 7]).unwrap();
    let source = binary::OwnedDecoded {
        parser: structured_asn1::Parser::new(
            structured_asn1::Encoding::Ber,
            structured_asn1::Limits {
                max_work: 1,
                ..Default::default()
            },
            storage.clone(),
        ),
        bytes,
        base: 700,
        depth: 3,
    };
    let storage_work = storage.work_used();
    let query = funded();
    let operation_parser = structured_asn1::Parser::der(query.clone());
    let operation = operation_parser.read(&[], 900).unwrap();
    {
        let reader = source
            .reader_in(&operation, structured_asn1::Encoding::Der)
            .unwrap();
        assert_eq!(reader.depth(), 3);
        assert_eq!(reader.offset(), 700);
    }
    let moved = source;
    let error = moved
        .reader_in(&operation, structured_asn1::Encoding::Der)
        .unwrap_err();
    assert_eq!(
        error.code(),
        structured_asn1::ErrorCode::LimitExceeded(structured_asn1::Limit::Work)
    );
    assert_eq!(error.offset(), 700);
    assert_eq!(query.work_used(), 1);
    assert_eq!(storage.work_used(), storage_work);

    let zero_parser = structured_asn1::Parser::new(
        structured_asn1::Encoding::Der,
        structured_asn1::Limits {
            max_work: 0,
            ..Default::default()
        },
        query.clone(),
    );
    let zero = zero_parser.read(&[], 900).unwrap();
    let fresh_source = binary::OwnedDecoded {
        parser: structured_asn1::Parser::ber(storage.clone()),
        bytes: moved.bytes,
        base: 700,
        depth: 3,
    };
    let error = fresh_source
        .reader_in(&zero, structured_asn1::Encoding::Der)
        .unwrap_err();
    assert_eq!(
        error.code(),
        structured_asn1::ErrorCode::LimitExceeded(structured_asn1::Limit::Work)
    );
    assert_eq!(error.offset(), 700);
    assert_eq!(query.work_used(), 1);
    drop(fresh_source);
    assert_eq!(storage.used(), 0);
}

fn material_tree(local: &'static str, bytes: Vec<u8>) -> Tree {
    let mut xml = Tree::normal();
    xml.nodes[8] = element(Some(7), XADES, local);
    xml.binary = bytes;
    xml
}

#[test]
fn complete_material_pass_parses_all_known_sources_and_preserves_open_formats() {
    // Frozen source buffers back one shared PKIX pass. Original storage roles
    // and opaque entries survive, without claiming trust or LT completeness.
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let mut xml = preparation_tree();
    xml.binary = candidate_certificate();
    let ordinary = xml.nodes.len();
    xml.nodes.push(element(Some(7), XADES, "CertificateValues"));
    xml.nodes.push(element(
        Some(ordinary),
        XADES,
        "EncapsulatedX509Certificate",
    ));
    let opaque = xml.nodes.len();
    xml.nodes
        .push(element(Some(ordinary), XADES, "OtherCertificate"));
    let general = xml.nodes.len();
    xml.nodes
        .push(element(Some(7), XADES_141, "AnyValidationData"));
    let certificates = xml.nodes.len();
    xml.nodes
        .push(element(Some(general), XADES, "CertificateValues"));
    xml.nodes.push(element(
        Some(certificates),
        XADES,
        "EncapsulatedX509Certificate",
    ));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Timestamped, None).unwrap();
    let held = memory.used();
    let count = material::with_values(&xml, &prepared, &operation, |_, view| {
        assert_eq!(view.sources().len(), 3);
        assert_eq!(view.values().len(), 3);
        assert!(matches!(view.sources()[0].placement(), material::Placement::KeyInfo(0)));
        assert!(matches!(view.sources()[1].placement(), material::Placement::Signature(node) if node == ordinary));
        assert!(matches!(view.sources()[2].placement(), material::Placement::General(node) if node == general));
        for (original, parsed) in view.sources().iter().zip(view.values()) {
            assert_eq!(parsed.location, original.source().location());
            let material::Value::Certificate(certificate) = &parsed.value else { panic!("not certificate") };
            assert_eq!(certificate.as_bytes().as_ptr(), original.source().bytes().as_ptr());
        }
        assert_eq!(view.opaque().len(), 1);
        assert_eq!(view.opaque()[0].location.node, opaque);
        Ok::<_, ()>(view.values().len())
    }).unwrap();
    assert_eq!(count, 3);
    assert_eq!(memory.used(), held);
    assert_eq!(xml.streamed.get(), 0);
    drop(prepared);
    assert_eq!(memory.used(), 0);
}

#[test]
fn original_key_info_crls_enter_the_same_validation_material_pool() {
    // XMLDSig1.1 §4.5.4 permits X509CRL in the original X509Data. Losing it
    // would falsely report missing validation data and invite duplicate copies.
    let memory = funded();
    let mut xml = preparation_tree();
    xml.binary = candidate_certificate();
    let key_info = xml.element_child(0, DS, "KeyInfo");
    let data = xml.element_child(key_info, DS, "X509Data");
    // This checks decoding/provenance, not authentication. Do not generate
    // unrelated ECDSA CRL/OCSP signatures: those have separate real-math tests.
    for update in [b"260101000000Z", b"270101000000Z"] {
        let node = xml.nodes.len();
        xml.nodes.push(element(Some(data), DS, "X509CRL"));
        xml.binary_routes.push((node, readable_crl_fixture(update)));
    }
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Timestamped, None).unwrap();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let before = memory.used();
    material::with_values(&xml, &prepared, &operation, |_, view| {
        assert_eq!(view.sources().len(), 3);
        assert_eq!(view.values().len(), 3);
        for (source, value) in view.sources()[1..].iter().zip(&view.values()[1..]) {
            assert_eq!(source.source().kind(), material::Kind::Crl);
            assert!(matches!(
                source.placement(),
                material::Placement::KeyInfo(0)
            ));
            let material::Value::Crl(crl) = &value.value else {
                panic!("original X509CRL")
            };
            assert_eq!(crl.as_bytes(), source.source().bytes());
            assert_eq!(crl.as_bytes().as_ptr(), source.source().bytes().as_ptr());
        }
        Ok::<_, ()>(())
    })
    .unwrap();
    assert_eq!(memory.used(), before);
}

fn readable_crl_fixture(update: &[u8]) -> Vec<u8> {
    readable_crl_fixture_as(update, structured_pkix::time::Form::Utc)
}

fn readable_crl_fixture_as(update: &[u8], form: structured_pkix::time::Form) -> Vec<u8> {
    // Trusted DER metadata writer; unknown algorithm/dummy signature MUST NOT
    // be used as an authentication oracle. Same fixture in native and Miri.
    use timestamp_oracle::tlv;
    let algorithm = tlv(0x30, &tlv(6, &[42, 3]));
    let name = [0x30, 12, 0x31, 10, 0x30, 8, 6, 3, 0x55, 4, 3, 0x0c, 1, b'a'];
    let body = tlv(
        0x30,
        &[
            tlv(2, &[1]),
            algorithm.clone(),
            name.to_vec(),
            tlv(
                match form {
                    structured_pkix::time::Form::Utc => 0x17,
                    structured_pkix::time::Form::Generalized => 0x18,
                },
                update,
            ),
        ]
        .concat(),
    );
    tlv(0x30, &[body, algorithm, tlv(3, &[0, 1])].concat())
}

fn ocsp_material_envelope(payload: &[u8], kind: &[u8]) -> Vec<u8> {
    use timestamp_oracle::tlv;
    let mut fields = tlv(6, kind);
    fields.extend(tlv(4, payload));
    let mut envelope = vec![10, 1, 0];
    envelope.extend(tlv(0xa0, &tlv(0x30, &fields)));
    tlv(0x30, &envelope)
}

fn ocsp_material_with_certificate(certificate: &[u8]) -> Vec<u8> {
    use timestamp_oracle::tlv;
    let algorithm = tlv(0x30, &tlv(6, &[42]));
    let mut id = algorithm.clone();
    id.extend(tlv(4, &[0x11; 20]));
    id.extend(tlv(4, &[0x22; 20]));
    id.extend([2, 1, 1]);
    let mut assertion = tlv(0x30, &id);
    assertion.extend([0x80, 0]);
    assertion.extend(tlv(24, b"20260101000000Z"));
    let mut data = tlv(0xa2, &tlv(4, &[0x33; 20]));
    data.extend(tlv(24, b"20260101000000Z"));
    data.extend(tlv(0x30, &tlv(0x30, &assertion)));
    let mut basic = tlv(0x30, &data);
    basic.extend(algorithm);
    basic.extend([3, 2, 0, 0xab]);
    basic.extend(tlv(0xa0, &tlv(0x30, certificate)));
    ocsp_material_envelope(
        &tlv(0x30, &basic),
        structured_pkix::oid::OCSP_BASIC.as_bytes(),
    )
}

fn ocsp_pipeline_tree(bytes: Vec<u8>) -> (Tree, usize) {
    let mut xml = preparation_tree();
    xml.binary = candidate_certificate();
    let property = xml.nodes.len();
    xml.nodes.push(element(Some(7), XADES, "RevocationValues"));
    let group = xml.nodes.len();
    xml.nodes.push(element(Some(property), XADES, "OCSPValues"));
    let node = xml.nodes.len();
    xml.nodes
        .push(element(Some(group), XADES, "EncapsulatedOCSPValue"));
    xml.binary_routes.push((node, bytes));
    (xml, node)
}

#[test]
fn material_pipeline_reads_basic_payload_and_embedded_certificates_from_the_original_whole_response()
 {
    // EN5.4.2 permits certificates embedded in status material. The basic model
    // must borrow its whole OCSP source, not reparse copied certificate bytes or
    // confuse syntactically good status with authenticated revocation evidence.
    let expected = candidate_certificate();
    let (xml, node) = ocsp_pipeline_tree(ocsp_material_with_certificate(&expected));
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Timestamped, None).unwrap();
    let held = memory.used();
    material::with_values(&xml, &prepared, &operation, |_, view| {
        assert_eq!(view.values().len(), 2);
        assert_eq!(view.basic_responses().len(), 1);
        let basic = &view.basic_responses()[0];
        assert_eq!(basic.source_index(), 1);
        assert_eq!(basic.location().node, node);
        assert_eq!(basic.location().snapshot, xml.snapshot());
        let material::Value::Ocsp(structured_pkix::ocsp::Response::Successful(envelope)) =
            &view.values()[1].value
        else {
            panic!("whole success envelope");
        };
        assert_eq!(
            basic.response().as_bytes().as_ptr(),
            envelope.payload().as_ptr()
        );
        assert_eq!(basic.response().span(), envelope.span());
        assert_eq!(basic.response().certificates().len(), 1);
        assert_eq!(basic.response().certificates()[0].as_bytes(), expected);
        assert_eq!(basic.response().responses().len(), 1);
        assert_eq!(
            basic.response().responses()[0].status(),
            structured_pkix::ocsp::Status::Good
        );
        Ok::<_, ()>(())
    })
    .unwrap();
    assert_eq!(memory.used(), held);
    drop(prepared);
    assert_eq!(memory.used(), 0);
}

#[test]
fn material_pipeline_refuses_a_broken_known_basic_payload_before_exposing_the_set() {
    // A successful wire envelope cannot hide a malformed known BasicResponse.
    // Error coordinates remain decoded OCSP source + original XML enclosure.
    let bytes = ocsp_material_envelope(&[0x25, 0], structured_pkix::oid::OCSP_BASIC.as_bytes());
    let (xml, node) = ocsp_pipeline_tree(bytes);
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Timestamped, None).unwrap();
    let held = memory.used();
    let calls = Cell::new(0);
    let result = material::with_values(&xml, &prepared, &operation, |_, _| {
        calls.set(calls.get() + 1);
        Ok::<_, ()>(())
    });
    let Err(material::PipelineError::Parse(error)) = result else {
        panic!("must reject basic syntax");
    };
    assert_eq!(error.location.node, node);
    assert_eq!(error.location.snapshot, xml.snapshot());
    let material::Cause::Pki(cause) = error.value else {
        panic!("PKIX cause");
    };
    assert!(!cause.is_resource());
    assert!(cause.offset() > 0);
    assert_eq!(calls.get(), 0);
    assert_eq!(memory.used(), held);
}

#[test]
fn material_pipeline_preserves_ocsp_protocol_failures_and_unknown_types_without_basic_promotion() {
    // Parsing neither hides failure envelopes nor interprets unsupported opaque
    // response syntaxes as good status or an empty successful basic response.
    for unknown in [false, true] {
        let bytes = if unknown {
            ocsp_material_envelope(&[0x25, 0], &[42])
        } else {
            vec![48, 3, 10, 1, 3]
        };
        let (xml, node) = ocsp_pipeline_tree(bytes);
        let memory = funded();
        let parser = structured_asn1::Parser::der(memory.clone());
        let operation = parser.read(&[], 900).unwrap();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        let prepared =
            prepare::prepare(&access, 0, unsigned::Requirements::Timestamped, None).unwrap();
        let held = memory.used();
        material::with_values(&xml, &prepared, &operation, |_, view| {
            assert!(view.basic_responses().is_empty());
            assert_eq!(view.values().len(), 2);
            assert_eq!(view.values()[1].location.node, node);
            if unknown {
                assert!(matches!(&view.values()[1].value, material::Value::Ocsp(structured_pkix::ocsp::Response::Successful(value)) if value.kind().as_bytes() == [42]));
            } else {
                assert!(matches!(&view.values()[1].value, material::Value::Ocsp(structured_pkix::ocsp::Response::Failure { failure: structured_pkix::ocsp::Failure::TryLater, .. })));
            }
            Ok::<_, ()>(())
        }).unwrap();
        assert_eq!(memory.used(), held);
    }
}

#[test]
fn material_index_keeps_every_exact_certificate_copy_including_ocsp_placement() {
    // The same certificate may occur before/after an archive cutoff or in a
    // different actor's material. Availability must retain every original
    // location, not collapse duplicates to one arbitrary or latest copy.
    let expected = candidate_certificate();
    let (mut xml, ocsp) = ocsp_pipeline_tree(ocsp_material_with_certificate(&expected));
    let ordinary = xml.nodes.len();
    xml.nodes.push(element(Some(7), XADES, "CertificateValues"));
    let direct = xml.nodes.len();
    xml.nodes.push(element(
        Some(ordinary),
        XADES,
        "EncapsulatedX509Certificate",
    ));
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Timestamped, None).unwrap();
    let held = memory.used();
    material::with_values(&xml, &prepared, &operation, |query, view| {
        let execution = query.execution();
        let access = Access::new(&xml, xml.snapshot(), &execution);
        let index = material::index(&view, &access).unwrap();
        let material::Value::Certificate(certificate) = &view.values()[0].value else {
            panic!("header certificate");
        };
        let copies = index
            .certificate(certificate, &access)
            .unwrap()
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(copies.len(), 3);
        assert_eq!(
            copies
                .iter()
                .map(material::Occurrence::source)
                .collect::<Vec<_>>(),
            [
                structured_ltv::material::ArtifactSource::Material(0),
                structured_ltv::material::ArtifactSource::Material(1),
                structured_ltv::material::ArtifactSource::Material(2)
            ]
        );
        assert_eq!(
            copies[0].location().node,
            prepared.header().certificates()[0]
        );
        assert!(matches!(
            copies[0].placement(),
            material::Placement::KeyInfo(0)
        ));
        assert_eq!(copies[0].embedded_certificate(), None);
        assert_eq!(copies[1].location().node, ocsp);
        assert_eq!(copies[1].embedded_certificate(), Some(0));
        assert_eq!(copies[2].location().node, direct);
        assert_eq!(copies[2].embedded_certificate(), None);
        let statuses = index
            .ocsp(view.basic_responses()[0].response(), &access)
            .unwrap()
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(statuses.len(), 1);
        assert_eq!(
            statuses[0].source(),
            structured_ltv::material::ArtifactSource::Material(1)
        );
        assert_eq!(statuses[0].location().node, ocsp);
        Ok::<_, ()>(())
    })
    .unwrap();
    assert_eq!(memory.used(), held);
    drop(prepared);
    assert_eq!(memory.used(), 0);
}

#[test]
fn material_index_queries_use_current_work_and_fuse_after_a_source_or_resource_refusal() {
    // Storage ownership supplies no query credit. Source changes and traversal
    // refusal cannot silently turn remaining copies into an empty successful set.
    let mut xml = preparation_tree();
    xml.binary = candidate_certificate();
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Timestamped, None).unwrap();
    let held = memory.used();
    let result = material::with_values(&xml, &prepared, &operation, |query, view| {
        let execution = query.execution();
        let access = Access::new(&xml, xml.snapshot(), &execution);
        let index = material::index(&view, &access).unwrap();
        let material::Value::Certificate(certificate) = &view.values()[0].value else {
            panic!("header certificate");
        };
        let empty_work = Memory::new(Arc::new(MemoryAccount::new(0, 0)));
        let current = Access::new(&xml, xml.snapshot(), &empty_work);
        assert!(matches!(
            index.certificate(certificate, &current),
            Err(structured_ltv::material::IndexError::Scope(
                structured_ltv::source::ScopeError::Resource { .. }
            ))
        ));
        let current_work = funded();
        let current = Access::new(&xml, xml.snapshot(), &current_work);
        let mut copies = index.certificate(certificate, &current).unwrap();
        current_work
            .charge_work(current_work.work_remaining() as u64)
            .unwrap();
        assert!(matches!(
            copies.next(),
            Some(Err(structured_ltv::material::IndexError::Scope(
                structured_ltv::source::ScopeError::Resource { .. }
            )))
        ));
        assert!(copies.next().is_none());
        let foreign = Access::new(
            &xml,
            Snapshot {
                document: 10,
                generation: 1,
            },
            &execution,
        );
        assert!(matches!(
            index.certificate(certificate, &foreign),
            Err(structured_ltv::material::IndexError::Foreign { .. })
        ));
        let mut copies = index.certificate(certificate, &access).unwrap();
        xml.generation.set(2);
        assert!(matches!(
            copies.next(),
            Some(Err(structured_ltv::material::IndexError::Scope(
                structured_ltv::source::ScopeError::Changed { .. }
            )))
        ));
        assert!(copies.next().is_none());
        assert!(matches!(
            index.certificate(certificate, &access),
            Err(structured_ltv::material::IndexError::Scope(
                structured_ltv::source::ScopeError::Changed { .. }
            ))
        ));
        Ok::<_, ()>(())
    });
    assert!(matches!(
        result,
        Err(material::PipelineError::Access(AccessError::Changed { .. }))
    ));
    assert_eq!(memory.used(), held);
}

#[test]
fn material_index_requires_whole_artifact_identity_and_drops_partial_construction_on_refusal() {
    // Equal subject/TBS is insufficient: another certificate signature denotes
    // a different original artifact. Failed index construction returns no prefix
    // and frees temporary entries even if map admission fails after entry growth.
    let mut xml = preparation_tree();
    xml.binary = candidate_certificate();
    let mut other = xml.binary.clone();
    *other.last_mut().unwrap() = 9;
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Timestamped, None).unwrap();
    let other_parser = structured_asn1::Parser::der(memory.clone());
    let other = structured_pkix::Certificate::read(&other_parser, &other, 0).unwrap();
    let held = memory.used();
    material::with_values(&xml, &prepared, &operation, |query, view| {
        let execution = query.execution();
        let access = Access::new(&xml, xml.snapshot(), &execution);
        let index = material::index(&view, &access).unwrap();
        assert!(index.certificate(&other, &access).unwrap().next().is_none());
        for capacity in [0, 400] {
            let limited = Memory::new(Arc::new(MemoryAccount::new(capacity, 100_000)));
            let current = Access::new(&xml, xml.snapshot(), &limited);
            assert!(matches!(
                material::index(&view, &current),
                Err(structured_ltv::material::IndexError::Resource { .. })
            ));
            assert_eq!(limited.used(), 0);
        }
        Ok::<_, ()>(())
    })
    .unwrap();
    assert_eq!(memory.used(), held);
}

#[test]
fn complete_material_pass_never_exposes_a_successful_subset_before_late_parse_failure() {
    // The header certificate parses successfully, but an OCSP-labelled member
    // containing certificate bytes must fail before any visitor can see it.
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let mut xml = preparation_tree();
    xml.binary = candidate_certificate();
    let property = xml.nodes.len();
    xml.nodes.push(element(Some(7), XADES, "RevocationValues"));
    let group = xml.nodes.len();
    xml.nodes.push(element(Some(property), XADES, "OCSPValues"));
    let wrong = xml.nodes.len();
    xml.nodes
        .push(element(Some(group), XADES, "EncapsulatedOCSPValue"));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Timestamped, None).unwrap();
    let held = memory.used();
    let called = Cell::new(false);
    let result = material::with_values(&xml, &prepared, &operation, |_, _| {
        called.set(true);
        Ok::<_, ()>(())
    });
    assert!(
        matches!(&result, Err(material::PipelineError::Parse(cause)) if cause.location.node == wrong)
    );
    drop(result);
    assert!(!called.get());
    assert_eq!(memory.used(), held);
}

#[test]
fn complete_material_pass_drops_visitor_results_on_source_change_and_keeps_original_errors() {
    // Visitor failure is opaque and terminal. A successful owned result cannot
    // escape a generation change; its capacity and all temporary sources drop.
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let mut xml = preparation_tree();
    xml.binary = candidate_certificate();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Timestamped, None).unwrap();
    let held = memory.used();
    assert!(matches!(
        material::with_values(&xml, &prepared, &operation, |_, _| Err::<(), _>("visitor")),
        Err(material::PipelineError::Visitor("visitor"))
    ));
    assert_eq!(memory.used(), held);
    let result = material::with_values(&xml, &prepared, &operation, |current, _| {
        let mut result = current.execution().vector();
        result.try_extend_from_slice(b"independent result")?;
        xml.generation.set(2);
        Ok::<_, MemoryError>(result.into_owned())
    });
    assert!(matches!(
        result,
        Err(material::PipelineError::Access(AccessError::Changed { .. }))
    ));
    assert_eq!(xml.generation.get(), 2);
    assert_eq!(memory.used(), held);
}

#[test]
fn complete_material_pass_funds_current_local_and_storage_budgets_before_decode() {
    // Preparation's old storage account is not credit for this new operation;
    // both the current local ceiling and current allocation account are enforced.
    let storage = funded();
    let mut xml = preparation_tree();
    xml.binary = candidate_certificate();
    let access = Access::new(&xml, xml.snapshot(), &storage);
    let prepared = prepare::prepare(&access, 0, unsigned::Requirements::Timestamped, None).unwrap();
    let query = funded();
    let parser = structured_asn1::Parser::new(
        structured_asn1::Encoding::Der,
        structured_asn1::Limits {
            max_work: 0,
            ..Default::default()
        },
        query.clone(),
    );
    let operation = parser.read(&[], 900).unwrap();
    let called = Cell::new(false);
    assert!(matches!(
        material::with_values(&xml, &prepared, &operation, |_, _| {
            called.set(true);
            Ok::<_, ()>(())
        }),
        Err(material::PipelineError::Access(AccessError::Resource {
            cause: MemoryError::WorkExceeded,
            ..
        }))
    ));
    assert!(!called.get());
    assert_eq!(xml.decoded_pointer.get(), 0);
    let no_storage = Memory::new(Arc::new(MemoryAccount::new(0, 100_000)));
    let parser = structured_asn1::Parser::der(no_storage.clone());
    let operation = parser.read(&[], 900).unwrap();
    let result = material::with_values(&xml, &prepared, &operation, |_, _| {
        called.set(true);
        Ok::<_, ()>(())
    });
    assert!(matches!(
        result,
        Err(material::PipelineError::Retain {
            cause: material::RetainError::Access(AccessError::Xml(Cause::Resource(
                MemoryError::LimitExceeded
            ))),
            ..
        })
    ));
    assert!(!called.get());
    assert_eq!(no_storage.used(), 0);
    assert_eq!(xml.decoded_pointer.get(), 0);
}

#[test]
fn key_info_material_reuses_owned_source_and_pkix_without_a_second_certificate_codec() {
    // Original selected header certificates use the same retained source/PKIX
    // route as embedded material. Invalid ordinals never start decoding.
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let source = {
        let (mut xml, _, node, _) = header_tree();
        xml.binary = candidate_certificate();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        let layout = container::locate(&access, 0).unwrap();
        let bound = binding::bind(&access, &layout).unwrap();
        let header = header::read(&access, &bound).unwrap();
        assert!(matches!(
            material::retain_header(&xml, &header, 1, &operation),
            Err(material::RetainError::HeaderIndex {
                signature: 0,
                index: 1
            })
        ));
        assert_eq!(xml.decoded_pointer.get(), 0);
        let source = material::retain_header(&xml, &header, 0, &operation).unwrap();
        assert_eq!(
            source.location(),
            Location {
                snapshot: xml.snapshot(),
                node
            }
        );
        assert_eq!(source.bytes().as_ptr() as usize, xml.decoded_pointer.get());
        source
    };
    {
        let value = source.read_in(&operation).unwrap();
        let material::Value::Certificate(certificate) = &value.value else {
            panic!("not certificate")
        };
        assert_eq!(certificate.as_bytes().as_ptr(), source.bytes().as_ptr());
    }
    drop(source);
    assert_eq!(memory.used(), 0);
}

#[test]
fn embedded_certificate_and_whole_ocsp_response_reuse_pkix_and_retain_locations() {
    // Simultaneous original material is parsed by the common PKIX kernels.
    // An OCSP error envelope stays a failure; parsing cannot confer good status.
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let certificate_xml = material_tree("EncapsulatedX509Certificate", candidate_certificate());
    let mut ocsp_xml = material_tree("EncapsulatedOCSPValue", vec![48, 3, 10, 1, 3]);
    ocsp_xml.document = 10;
    let location = Location {
        snapshot: certificate_xml.snapshot(),
        node: 8,
    };
    let cert = material::retain(
        &certificate_xml,
        location,
        material::Kind::Certificate,
        &operation,
    )
    .unwrap();
    assert_eq!(
        cert.bytes().as_ptr() as usize,
        certificate_xml.decoded_pointer.get()
    );
    let response_location = Location {
        snapshot: ocsp_xml.snapshot(),
        node: 8,
    };
    let response = material::retain(
        &ocsp_xml,
        response_location,
        material::Kind::Ocsp,
        &operation,
    )
    .unwrap();
    drop(certificate_xml);
    drop(ocsp_xml);
    {
        let a = cert.read_in(&operation).unwrap();
        let b = response.read_in(&operation).unwrap();
        assert_eq!(a.location, location);
        assert_eq!(b.location, response_location);
        assert_ne!(a.location, b.location);
        match a.value {
            material::Value::Certificate(value) => {
                assert_eq!(value.as_bytes().as_ptr(), cert.bytes().as_ptr());
            }
            other => panic!("unexpected {other:?}"),
        }
        assert!(matches!(
            b.value,
            material::Value::Ocsp(structured_pkix::ocsp::Response::Failure {
                failure: structured_pkix::ocsp::Failure::TryLater,
                ..
            })
        ));
    }
    drop(cert);
    drop(response);
    assert_eq!(memory.used(), 0);
}

#[test]
fn embedded_crl_reuses_original_der_certificate_list() {
    // This is a schema/provenance fixture, deliberately not authenticated math.
    // The original CRL TLV, rather than a reconstructed TBS, reaches PKIX.
    fn tlv(tag: u8, bytes: &[u8]) -> Vec<u8> {
        assert!(bytes.len() < 128);
        [
            vec![tag, u8::try_from(bytes.len()).unwrap()],
            bytes.to_vec(),
        ]
        .concat()
    }
    let algorithm = [48, 3, 6, 1, 42];
    let name = [48, 12, 49, 10, 48, 8, 6, 3, 0x55, 4, 3, 12, 1, b'a'];
    let tbs = tlv(
        48,
        &[algorithm.as_slice(), &name, &tlv(23, b"261012120000Z")].concat(),
    );
    let bytes = tlv(48, &[&tbs, algorithm.as_slice(), &[3, 2, 0, 8]].concat());
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let xml = material_tree("EncapsulatedCRLValue", bytes);
    let source = material::retain(
        &xml,
        Location {
            snapshot: xml.snapshot(),
            node: 8,
        },
        material::Kind::Crl,
        &operation,
    )
    .unwrap();
    let read = source.read_in(&operation).unwrap();
    assert!(matches!(read.value, material::Value::Crl(_)));
    drop(read);
    drop(source);
    assert_eq!(memory.used(), 0);
}

#[test]
fn material_schema_tail_and_current_resource_refusals_are_not_missing_status() {
    // DER, whole-envelope and exact-value boundaries are independent of XML's
    // Encoding hint. No syntax/resource failure becomes successful absent data.
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let mut xml = material_tree("EncapsulatedOCSPValue", vec![48, 3, 10, 1, 3, 0]);
    let location = Location {
        snapshot: xml.snapshot(),
        node: 8,
    };
    let source = material::retain(&xml, location, material::Kind::Ocsp, &operation).unwrap();
    let error = source.read_in(&operation).unwrap_err();
    assert_eq!(error.location, location);
    assert!(matches!(
        error.value,
        material::Cause::Trailing { offset: 5 }
    ));
    drop(source);
    xml.binary = vec![48, 0x81, 3, 10, 1, 3];
    xml.binary_encoding = Some("http://uri.etsi.org/01903/v1.2.2#BER");
    let source = material::retain(&xml, location, material::Kind::Ocsp, &operation).unwrap();
    assert!(matches!(
        source.read_in(&operation),
        Err(binary::Located {
            value: material::Cause::Encoding(_),
            ..
        })
    ));
    drop(source);
    xml.binary = vec![48, 3, 10, 1, 3];
    let source = material::retain(&xml, location, material::Kind::Ocsp, &operation).unwrap();
    let zero_parser = structured_asn1::Parser::new(
        structured_asn1::Encoding::Der,
        structured_asn1::Limits {
            max_work: 0,
            ..Default::default()
        },
        memory.clone(),
    );
    let zero = zero_parser.read(&[], 900).unwrap();
    let error = source.read_in(&zero).unwrap_err();
    match error.value {
        material::Cause::Encoding(cause) => assert_eq!(
            cause.code(),
            structured_asn1::ErrorCode::LimitExceeded(structured_asn1::Limit::Work)
        ),
        other => panic!("unexpected {other:?}"),
    }
    drop(source);
    assert_eq!(memory.used(), 0);
}

#[test]
fn material_expanded_name_refuses_before_decode_or_retention() {
    // A lookalike from another namespace cannot be promoted to a PKI source.
    let memory = funded();
    let parser = structured_asn1::Parser::der(memory.clone());
    let operation = parser.read(&[], 900).unwrap();
    let mut xml = material_tree("EncapsulatedOCSPValue", vec![0; 100_000]);
    xml.nodes[8] = element(Some(7), "urn:other", "EncapsulatedOCSPValue");
    assert!(matches!(
        material::retain(
            &xml,
            Location {
                snapshot: xml.snapshot(),
                node: 8
            },
            material::Kind::Ocsp,
            &operation
        ),
        Err(material::RetainError::Name {
            node: 8,
            kind: material::Kind::Ocsp
        })
    ));
    assert_eq!(xml.decoded_pointer.get(), 0);
    assert_eq!(memory.used(), 0);
}

#[test]
fn certificate_material_catalogue_preserves_opaque_and_x509_values_in_order() {
    // The normative choice permits repeated X509 values and opaque other
    // certificates. Keeping either is not a completeness/authentication claim.
    let memory = funded();
    let mut xml = material_tree("CertificateValues", vec![]);
    let first = xml.nodes.len();
    xml.nodes.push(element(Some(8), XADES, "OtherCertificate"));
    let second = xml.nodes.len();
    xml.nodes
        .push(element(Some(8), XADES, "EncapsulatedX509Certificate"));
    let third = xml.nodes.len();
    xml.nodes
        .push(element(Some(8), XADES, "EncapsulatedX509Certificate"));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let catalogue = material::read(&access, 8).unwrap();
    assert_eq!(catalogue.snapshot(), xml.snapshot());
    assert_eq!(catalogue.parent(), 8);
    assert_eq!(
        catalogue.entries(),
        &[
            material::Entry::OtherCertificate { node: first },
            material::Entry::Encapsulated {
                node: second,
                kind: material::Kind::Certificate
            },
            material::Entry::Encapsulated {
                node: third,
                kind: material::Kind::Certificate
            },
        ]
    );
    assert!(memory.used() > 0);
    assert_eq!(xml.decoded_pointer.get(), 0);
    drop(catalogue);
    assert_eq!(memory.used(), 0);
}

#[test]
fn attribute_material_uses_the_same_certificate_and_status_value_grammar() {
    // Annex C aliases these property types. Attribute-role presence conditions
    // and issuer trust do not justify copying or weakening the value decoder.
    let memory = funded();
    let mut xml = Tree::normal();
    let certificates = xml.nodes.len();
    xml.nodes
        .push(element(Some(7), XADES, "AttrAuthoritiesCertValues"));
    let certificate = xml.nodes.len();
    xml.nodes.push(element(
        Some(certificates),
        XADES,
        "EncapsulatedX509Certificate",
    ));
    let revocation = xml.nodes.len();
    xml.nodes
        .push(element(Some(7), XADES, "AttributeRevocationValues"));
    let group = xml.nodes.len();
    xml.nodes
        .push(element(Some(revocation), XADES, "OCSPValues"));
    let response = xml.nodes.len();
    xml.nodes
        .push(element(Some(group), XADES, "EncapsulatedOCSPValue"));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let certs = material::read(&access, certificates).unwrap();
    assert_eq!(
        certs.entries(),
        &[material::Entry::Encapsulated {
            node: certificate,
            kind: material::Kind::Certificate
        }]
    );
    let status = material::read(&access, revocation).unwrap();
    assert_eq!(
        status.entries(),
        &[material::Entry::Encapsulated {
            node: response,
            kind: material::Kind::Ocsp
        }]
    );
    assert_eq!(xml.decoded_pointer.get(), 0);
}

#[test]
fn revocation_material_catalogue_preserves_each_group_without_decoding() {
    // CRL, whole OCSP and other status are distinct schema roles, not an
    // interchangeable count of "validation values" or a last-group overwrite.
    let memory = funded();
    let mut xml = material_tree("RevocationValues", vec![]);
    let crls = xml.nodes.len();
    xml.nodes.push(element(Some(8), XADES, "CRLValues"));
    let first = xml.nodes.len();
    xml.nodes
        .push(element(Some(crls), XADES, "EncapsulatedCRLValue"));
    let second = xml.nodes.len();
    xml.nodes
        .push(element(Some(crls), XADES, "EncapsulatedCRLValue"));
    let ocsp = xml.nodes.len();
    xml.nodes.push(element(Some(8), XADES, "OCSPValues"));
    let third = xml.nodes.len();
    xml.nodes
        .push(element(Some(ocsp), XADES, "EncapsulatedOCSPValue"));
    let other = xml.nodes.len();
    xml.nodes.push(element(Some(8), XADES, "OtherValues"));
    let fourth = xml.nodes.len();
    xml.nodes.push(element(Some(other), XADES, "OtherValue"));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let catalogue = material::read(&access, 8).unwrap();
    assert_eq!(
        catalogue.entries(),
        &[
            material::Entry::Encapsulated {
                node: first,
                kind: material::Kind::Crl
            },
            material::Entry::Encapsulated {
                node: second,
                kind: material::Kind::Crl
            },
            material::Entry::Encapsulated {
                node: third,
                kind: material::Kind::Ocsp
            },
            material::Entry::OtherStatus { node: fourth },
        ]
    );
    assert_eq!(xml.decoded_pointer.get(), 0);
    drop(catalogue);
    assert_eq!(memory.used(), 0);
}

#[test]
fn material_empty_property_and_empty_group_have_different_schema_meanings() {
    // Empty properties are schema-valid; present empty groups are not. Neither
    // case is a shortcut to deciding conditional B-LT material completeness.
    let memory = funded();
    for property in ["CertificateValues", "RevocationValues"] {
        let xml = material_tree(property, vec![]);
        let access = Access::new(&xml, xml.snapshot(), &memory);
        assert_eq!(material::read(&access, 8).unwrap().entries(), []);
    }
    for (group, member) in [
        ("CRLValues", "EncapsulatedCRLValue"),
        ("OCSPValues", "EncapsulatedOCSPValue"),
        ("OtherValues", "OtherValue"),
    ] {
        let mut xml = material_tree("RevocationValues", vec![]);
        let child = xml.nodes.len();
        xml.nodes.push(element(Some(8), XADES, group));
        let access = Access::new(&xml, xml.snapshot(), &memory);
        match material::read(&access, 8) {
            Err(properties::Error::Missing { parent, local }) => {
                assert_eq!(parent, child);
                assert_eq!(local, member);
            }
            _ => panic!("empty group must refuse"),
        }
    }
    assert_eq!(memory.used(), 0);
}

#[test]
fn material_group_order_duplicate_and_catalogue_capacity_refuse_atomically() {
    // A partial first group cannot hide a duplicate/out-of-order later group,
    // and a storage failure cannot publish a truncated successful catalogue.
    let memory = funded();
    let mut xml = material_tree("RevocationValues", vec![]);
    let first = xml.nodes.len();
    xml.nodes.push(element(Some(8), XADES, "OCSPValues"));
    xml.nodes
        .push(element(Some(first), XADES, "EncapsulatedOCSPValue"));
    let second = xml.nodes.len();
    xml.nodes.push(element(Some(8), XADES, "CRLValues"));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(material::read(&access, 8), Err(properties::Error::Order { node }) if node == second)
    );
    assert_eq!(memory.used(), 0);
    xml.nodes[second] = element(Some(8), XADES, "OCSPValues");
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(material::read(&access, 8), Err(properties::Error::Duplicate { node }) if node == second)
    );
    assert_eq!(memory.used(), 0);
    xml.nodes.pop();
    let no_storage = Memory::new(Arc::new(MemoryAccount::new(0, 100_000)));
    let access = Access::new(&xml, xml.snapshot(), &no_storage);
    assert!(matches!(
        material::read(&access, 8),
        Err(properties::Error::Resource {
            cause: MemoryError::LimitExceeded,
            ..
        })
    ));
    assert_eq!(no_storage.used(), 0);
    assert_eq!(xml.decoded_pointer.get(), 0);
}

#[test]
fn signed_data_object_properties_do_not_skip_invalid_child_schema() {
    // Whole SignedProperties coverage cannot excuse an invalid authenticated
    // data-object property. The old structural reader never inspected this child.
    let memory = funded();
    let (mut xml, _) = certificate_tree();
    assert!(signed_roles(&xml, &memory).is_ok());
    let objects = xml.nodes.len();
    xml.nodes
        .push(element(Some(3), XADES, "SignedDataObjectProperties"));
    let invalid = xml.nodes.len();
    xml.nodes.push(element(Some(objects), XADES, "SigningTime"));
    assert!(matches!(signed_roles(&xml, &memory),
        Err(properties::Error::Unexpected { node }) if node == invalid));
    assert_eq!(memory.used(), 0);
}

#[test]
fn signed_data_object_roles_keep_repetitions_order_and_original_covered_nodes() {
    // Each schema member is repeatable, so two formats are not a duplicate.
    // The full catalogue stays under the selected SignedProperties binding.
    let memory = funded();
    let (mut xml, _) = certificate_tree();
    let parent = xml.nodes.len();
    xml.nodes
        .push(element(Some(3), XADES, "SignedDataObjectProperties"));
    let mut expected = Vec::new();
    for (namespace, local, role) in [
        (
            XADES,
            "DataObjectFormat",
            properties::DataObjectRole::Format,
        ),
        (
            XADES,
            "DataObjectFormat",
            properties::DataObjectRole::Format,
        ),
        (
            XADES,
            "CommitmentTypeIndication",
            properties::DataObjectRole::Commitment,
        ),
        (
            XADES,
            "AllDataObjectsTimeStamp",
            properties::DataObjectRole::AllTimestamp,
        ),
        (
            XADES,
            "IndividualDataObjectsTimeStamp",
            properties::DataObjectRole::IndividualTimestamp,
        ),
        (
            "urn:extension",
            "Property",
            properties::DataObjectRole::Extension,
        ),
    ] {
        let node = xml.nodes.len();
        xml.nodes.push(element(Some(parent), namespace, local));
        expected.push(properties::DataObject { node, role });
    }
    let signed = signed_roles(&xml, &memory).unwrap();
    assert_eq!(signed.signature(), 0);
    assert_eq!(signed.snapshot(), xml.snapshot());
    let objects = signed.data_objects().unwrap();
    assert_eq!(objects.parent(), parent);
    assert_eq!(objects.entries(), expected);
    assert!(memory.used() > 0);
    drop(signed);
    assert_eq!(memory.used(), 0);
}

#[test]
fn signed_data_object_sequence_and_wildcard_are_not_first_match_acceptance() {
    // Known properties cannot follow a later group or foreign extension;
    // an absent namespace is never admitted by ##other.
    let memory = funded();
    let (mut xml, _) = certificate_tree();
    let parent = xml.nodes.len();
    xml.nodes
        .push(element(Some(3), XADES, "SignedDataObjectProperties"));
    let first = xml.nodes.len();
    xml.nodes
        .push(element(Some(parent), XADES, "CommitmentTypeIndication"));
    let later = xml.nodes.len();
    xml.nodes
        .push(element(Some(parent), XADES, "DataObjectFormat"));
    assert!(
        matches!(signed_roles(&xml, &memory), Err(properties::Error::Order { node }) if node == later)
    );
    assert_eq!(memory.used(), 0);
    xml.nodes[first] = element(Some(parent), "urn:extension", "Property");
    assert!(
        matches!(signed_roles(&xml, &memory), Err(properties::Error::Order { node }) if node == later)
    );
    assert_eq!(memory.used(), 0);
    xml.nodes.pop();
    xml.nodes[first] = element(Some(parent), "", "Property");
    assert!(
        matches!(signed_roles(&xml, &memory), Err(properties::Error::Unexpected { node }) if node == first)
    );
    assert_eq!(memory.used(), 0);
}

#[test]
fn signed_data_objects_preserve_absent_empty_and_capacity_refusal() {
    // Absence and present-empty differ structurally. Failed retention of an
    // actual member cannot be returned as either of those successful outcomes.
    let memory = funded();
    let (mut xml, _) = certificate_tree();
    assert!(
        signed_roles(&xml, &memory)
            .unwrap()
            .data_objects()
            .is_none()
    );
    let parent = xml.nodes.len();
    xml.nodes
        .push(element(Some(3), XADES, "SignedDataObjectProperties"));
    let signed = signed_roles(&xml, &memory).unwrap();
    assert_eq!(signed.data_objects().unwrap().entries(), []);
    drop(signed);
    let node = xml.nodes.len();
    xml.nodes
        .push(element(Some(parent), XADES, "DataObjectFormat"));
    let no_storage = Memory::new(Arc::new(MemoryAccount::new(0, 100_000)));
    assert!(matches!(signed_roles(&xml, &no_storage),
        Err(properties::Error::Resource { node: at, cause: MemoryError::LimitExceeded }) if at == node));
    assert_eq!(no_storage.used(), 0);
}

#[test]
fn signed_data_object_catalogue_keeps_current_local_work_and_owned_lifetime() {
    // The same full public path must pay every repeated member under the current
    // producing operation. A retained result keeps capacity after that scope ends.
    let memory = funded();
    let (mut xml, _) = certificate_tree();
    let parent = xml.nodes.len();
    xml.nodes
        .push(element(Some(3), XADES, "SignedDataObjectProperties"));
    for _ in 0..32 {
        xml.nodes
            .push(element(Some(parent), XADES, "DataObjectFormat"));
    }
    let full = signed_roles(&xml, &memory).unwrap();
    let complete_work = memory.work_used();
    assert_eq!(full.data_objects().unwrap().entries().len(), 32);
    drop(full);
    assert_eq!(memory.used(), 0);

    let query = funded();
    let parser = structured_asn1::Parser::new(
        structured_asn1::Encoding::Der,
        structured_asn1::Limits {
            max_work: u64::try_from(complete_work - 1).unwrap(),
            ..Default::default()
        },
        query.clone(),
    );
    let operation = parser.read(&[], 0).unwrap();
    let execution = operation.execution();
    let access = Access::new(&xml, xml.snapshot(), &execution);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    assert!(matches!(
        properties::read(&access, &bound),
        Err(properties::Error::Access(
            AccessError::Resource {
                cause: MemoryError::WorkExceeded,
                ..
            } | AccessError::Xml(Cause::Resource(MemoryError::WorkExceeded))
        ) | properties::Error::Child(structured_xades::xml::ChildError::Access(
            AccessError::Resource {
                cause: MemoryError::WorkExceeded,
                ..
            }
        )) | properties::Error::Resource {
            cause: MemoryError::WorkExceeded,
            ..
        })
    ));
    assert_eq!(query.used(), 0);

    let retained = {
        let parser = structured_asn1::Parser::der(query.clone());
        let operation = parser.read(&[], 0).unwrap();
        let execution = operation.execution();
        let access = Access::new(&xml, xml.snapshot(), &execution);
        let layout = container::locate(&access, 0).unwrap();
        let bound = binding::bind(&access, &layout).unwrap();
        properties::read(&access, &bound).unwrap()
    };
    assert_eq!(retained.data_objects().unwrap().entries().len(), 32);
    assert!(query.used() > 0);
    drop(retained);
    assert_eq!(query.used(), 0);
}

fn all_data_timestamp_tree() -> (Tree, usize, usize) {
    let (mut xml, _) = certificate_tree();
    let objects = xml.nodes.len();
    xml.nodes
        .push(element(Some(3), XADES, "SignedDataObjectProperties"));
    let stamp = xml.nodes.len();
    xml.nodes
        .push(element(Some(objects), XADES, "AllDataObjectsTimeStamp"));
    let method = xml.nodes.len();
    xml.nodes
        .push(element(Some(stamp), DS, "CanonicalizationMethod"));
    xml.nodes
        .push(element(Some(stamp), XADES, "EncapsulatedTimeStamp"));
    (xml, stamp, method)
}

fn individual_timestamp_tree() -> (Tree, usize, usize, [usize; 2]) {
    let (mut xml, node, method) = all_data_timestamp_tree();
    xml.nodes[node].value = Node::Element(Name {
        namespace: XADES,
        local: "IndividualDataObjectsTimeStamp",
    });
    // The fixture keeps original sibling order by inserting Include records
    // before the existing method/token records. Existing ids below method stay.
    xml.nodes
        .insert(method, element(Some(node), XADES, "Include"));
    xml.nodes
        .insert(method + 1, element(Some(node), XADES, "Include"));
    let manifest = xml.nodes.len();
    xml.nodes.push(element(Some(1), DS, "Manifest"));
    let reference = xml.nodes.len();
    xml.nodes.push(element(Some(manifest), DS, "Reference"));
    xml.references.push(Reference {
        node: reference,
        target: None,
        kind: None,
        status: Status::Valid,
        coverage: Coverage::Transformed,
    });
    xml.include_routes = vec![
        (method, 2, Boolean::Value(true)),
        (method + 1, 0, Boolean::Value(true)),
    ];
    (xml, node, method + 2, [method, method + 1])
}

#[test]
fn individual_timestamp_replays_signed_manifest_and_direct_references_in_include_order() {
    // EN5.2.8.2 does not restrict Include to SignedInfo. The synthetic adapter
    // maps first Include to Manifest and second to SignedInfo; output framing
    // tests original identity/order, not canonicalization interoperability.
    for outside_signature in [false, true] {
        let memory = funded();
        let (mut xml, node, method, _) = individual_timestamp_tree();
        if outside_signature {
            // Authentication comes from the selected owner's receipt, not a
            // requirement that every signed Manifest be inside ds:Signature.
            let manifest = xml.nodes[xml.references[2].node].parent.unwrap();
            let document = xml.nodes.len();
            xml.nodes.push(Record {
                parent: None,
                value: Node::Other,
            });
            xml.nodes[0].parent = Some(document);
            xml.nodes[manifest].parent = Some(document);
        }
        let signed = signed_roles(&xml, &memory).unwrap();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        let layout = container::locate(&access, 0).unwrap();
        let bound = binding::bind(&access, &layout).unwrap();
        let stamp =
            timestamp::read_individual_data_objects(&access, &bound, &signed, node).unwrap();
        assert_eq!(stamp.role(), Role::IndividualDataObjects);
        let mut input = Input::prepare(&xml, &bound, &stamp, &memory).unwrap();
        let mut sink = Collect::new(&memory);
        stream::run(&mut input, &mut sink, &memory, "individual.input", 0).unwrap();
        let method = u8::try_from(method).unwrap();
        assert_eq!(
            sink.bytes(),
            &[b'R', 2, b'M', method, b'R', 0, b'M', method]
        );
        assert_eq!(xml.streamed.get(), 2);
    }
}

#[test]
fn individual_include_requires_explicit_true_and_authenticated_eligible_reference_before_replay() {
    // Absent/false/bad boolean, unauthenticated reference and recursion through
    // SignedProperties cannot be hidden as an empty timestamp input.
    for value in [Boolean::Absent, Boolean::Value(false), Boolean::Malformed] {
        let memory = funded();
        let (mut xml, node, _, includes) = individual_timestamp_tree();
        xml.include_routes[0].2 = value;
        let signed = signed_roles(&xml, &memory).unwrap();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        let layout = container::locate(&access, 0).unwrap();
        let bound = binding::bind(&access, &layout).unwrap();
        let stamp =
            timestamp::read_individual_data_objects(&access, &bound, &signed, node).unwrap();
        assert!(
            matches!(Input::prepare(&xml, &bound, &stamp, &memory), Err(InputError::ReferencedData { node, value: actual }) if node == includes[0] && actual == value)
        );
        assert_eq!(xml.streamed.get(), 0);
    }
    for ordinal in [1, 2] {
        let memory = funded();
        let (mut xml, node, _, includes) = individual_timestamp_tree();
        xml.include_routes[0].1 = ordinal;
        if ordinal == 2 {
            xml.references[2].status = Status::Unchecked;
        }
        let signed = signed_roles(&xml, &memory).unwrap();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        let layout = container::locate(&access, 0).unwrap();
        let bound = binding::bind(&access, &layout).unwrap();
        let stamp =
            timestamp::read_individual_data_objects(&access, &bound, &signed, node).unwrap();
        assert!(
            matches!(Input::prepare(&xml, &bound, &stamp, &memory), Err(InputError::Reference { node, resolution: Resolution::Resolved(()) }) if node == includes[0])
        );
        assert_eq!(xml.streamed.get(), 0);
    }
}

#[test]
fn individual_include_order_scope_and_resource_refusal_never_return_partial_input() {
    // Property membership and all Include preparation finish before any source
    // output. Failure must roll back temporary parts, retaining only stamp data.
    let memory = funded();
    let (xml, node, _, _) = individual_timestamp_tree();
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let stamp = timestamp::read_individual_data_objects(&access, &bound, &signed, node).unwrap();
    let unfunded = Memory::new(Arc::new(MemoryAccount::new(0, 100_000)));
    assert!(matches!(
        Input::prepare(&xml, &bound, &stamp, &unfunded),
        Err(InputError::Resource { .. })
    ));
    assert_eq!(unfunded.used(), 0);
    assert_eq!(xml.streamed.get(), 0);
    assert!(matches!(
        timestamp::read_all_data_objects(&access, &bound, &signed, node),
        Err(InputError::Unexpected { .. })
    ));
    drop(stamp);
    drop(signed);
    let mut xml = xml;
    let late = xml.nodes.len();
    xml.nodes.push(element(Some(node), XADES, "Include"));
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(timestamp::read_individual_data_objects(&access, &bound, &signed, node), Err(InputError::Order { node }) if node == late)
    );
}

#[test]
fn all_data_timestamp_uses_reference_order_and_excludes_each_properties_reference() {
    // EN5.2.8.1 uses direct reference OUTPUTS, not XML elements or nested
    // Manifest targets. Repeated protecting references never include the timestamp
    // itself recursively through SignedProperties. Framing is not a C14N oracle.
    let memory = funded();
    let (mut xml, node, method) = all_data_timestamp_tree();
    let repeat = xml.nodes.len();
    xml.nodes.push(element(Some(9), DS, "Reference"));
    let mut reference = xml.references[1];
    reference.node = repeat;
    xml.references.push(reference);
    let manifest_reference = xml.nodes.len();
    xml.nodes.push(element(Some(9), DS, "Reference"));
    xml.references.push(Reference {
        node: manifest_reference,
        target: Some(1),
        kind: Some("http://www.w3.org/2000/09/xmldsig#Manifest"),
        status: Status::Valid,
        coverage: Coverage::Transformed,
    });
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let stamp = timestamp::read_all_data_objects(&access, &bound, &signed, node).unwrap();
    assert_eq!(stamp.role(), Role::AllDataObjects);
    let mut input = Input::prepare(&xml, &bound, &stamp, &memory).unwrap();
    let mut sink = Collect::new(&memory);
    stream::run(
        &mut input,
        &mut sink,
        &memory,
        "all-data.timestamp.input",
        0,
    )
    .unwrap();
    let method = u8::try_from(method).unwrap();
    assert_eq!(
        sink.bytes(),
        &[b'R', 0, b'M', method, b'R', 3, b'M', method]
    );
    assert_eq!(xml.streamed.get(), 2);
}

#[test]
fn all_data_timestamp_keeps_multiple_tokens_and_refuses_explicit_include() {
    // AllDataObjects uses implicit input; Individual's Include/referencedData
    // mechanism is different. More than one RFC3161 token is permitted here.
    let memory = funded();
    let (mut xml, node, method) = all_data_timestamp_tree();
    let extra = xml.nodes.len();
    xml.nodes
        .push(element(Some(node), XADES, "EncapsulatedTimeStamp"));
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let stamp = timestamp::read_all_data_objects(&access, &bound, &signed, node).unwrap();
    assert_eq!(stamp.tokens(), &[method + 1, extra]);
    drop(stamp);
    drop(signed);
    xml.nodes[extra] = element(Some(node), XADES, "Include");
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(timestamp::read_all_data_objects(&access, &bound, &signed, node),
        Err(InputError::Unexpected { node: at }) if at == extra)
    );
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn all_data_timestamp_cannot_substitute_unsigned_or_another_signature_property() {
    // Identical QName is insufficient: membership in this selected signature's
    // authenticated SignedDataObjectProperties is checked before input execution.
    let memory = funded();
    let (mut xml, node, _) = all_data_timestamp_tree();
    let wrong = xml.nodes.len();
    xml.nodes
        .push(element(Some(7), XADES, "AllDataObjectsTimeStamp"));
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    assert!(
        matches!(timestamp::read_all_data_objects(&access, &bound, &signed, wrong),
        Err(InputError::Foreign { node: at }) if at == wrong)
    );
    assert!(matches!(
        timestamp::read(&access, &bound, node),
        Err(InputError::Foreign { .. })
    ));
    let mut foreign = xml;
    foreign.document = 10;
    let foreign_signed = signed_roles(&foreign, &memory).unwrap();
    let access = Access::new(&foreign, foreign.snapshot(), &memory);
    assert!(matches!(
        timestamp::read_all_data_objects(&access, &bound, &foreign_signed, node),
        Err(InputError::Foreign { .. })
    ));
    assert_eq!(foreign.streamed.get(), 0);
}

fn data_format_tree() -> (Tree, usize, usize) {
    let (mut xml, _) = certificate_tree();
    let parent = xml.nodes.len();
    xml.nodes
        .push(element(Some(3), XADES, "SignedDataObjectProperties"));
    let node = xml.nodes.len();
    xml.nodes
        .push(element(Some(parent), XADES, "DataObjectFormat"));
    let mime = xml.mime(node);
    xml.object_reference = Some("#data-reference");
    (xml, node, mime)
}

fn commitment_tree(all: bool) -> (Tree, usize, usize) {
    let (mut xml, _) = certificate_tree();
    let parent = xml.nodes.len();
    xml.nodes
        .push(element(Some(3), XADES, "SignedDataObjectProperties"));
    let node = xml.nodes.len();
    xml.nodes
        .push(element(Some(parent), XADES, "CommitmentTypeIndication"));
    let kind = xml.nodes.len();
    xml.nodes
        .push(element(Some(node), XADES, "CommitmentTypeId"));
    let identifier = xml.nodes.len();
    xml.nodes.push(element(Some(kind), XADES, "Identifier"));
    xml.nodes.push(Record {
        parent: Some(identifier),
        value: Node::Text("https://example.test/commitment"),
    });
    let choice = xml.nodes.len();
    xml.nodes.push(element(
        Some(node),
        XADES,
        if all {
            "AllSignedDataObjects"
        } else {
            "ObjectReference"
        },
    ));
    if !all {
        xml.nodes.push(Record {
            parent: Some(choice),
            value: Node::Text("#data-reference"),
        });
        xml.include_routes.push((choice, 0, Boolean::Absent));
    }
    (xml, node, choice)
}

fn policy_tree(implied: bool, special: bool) -> (Tree, usize, usize) {
    let (mut xml, _) = certificate_tree();
    let property = xml.nodes.len();
    xml.nodes
        .push(element(Some(4), XADES, "SignaturePolicyIdentifier"));
    let node = xml.nodes.len();
    xml.nodes.push(element(
        Some(property),
        XADES,
        if implied {
            "SignaturePolicyImplied"
        } else {
            "SignaturePolicyId"
        },
    ));
    if !implied {
        let id = xml.nodes.len();
        xml.nodes.push(element(Some(node), XADES, "SigPolicyId"));
        let identifier = xml.nodes.len();
        xml.nodes.push(element(Some(id), XADES, "Identifier"));
        xml.nodes.push(Record {
            parent: Some(identifier),
            value: Node::Text("https://example.test/policy/v1"),
        });
        if special {
            let transforms = xml.nodes.len();
            xml.nodes.push(element(Some(node), DS, "Transforms"));
            xml.nodes.push(element(Some(transforms), DS, "Transform"));
            xml.digest_algorithm = Some(
                "http://uri.etsi.org/01903/v1.3.2/SignaturePolicy/SPDocDigestAsInSpecification",
            );
        }
        let hash = xml.nodes.len();
        xml.nodes.push(element(Some(node), XADES, "SigPolicyHash"));
        xml.nodes.push(element(Some(hash), DS, "DigestMethod"));
        xml.nodes.push(element(Some(hash), DS, "DigestValue"));
    }
    (xml, property, node)
}

#[test]
fn explicit_policy_store_preserves_embedded_or_local_source_without_acquisition() {
    // The unsigned source choice does not authenticate its content. It only
    // becomes eligible for comparison under the original explicit signed hash.
    let memory = funded();
    for local in [false, true] {
        let (mut xml, _, _) = policy_tree(false, false);
        let store = xml.nodes.len();
        xml.nodes
            .push(element(Some(7), XADES_141, "SignaturePolicyStore"));
        let specification = xml.nodes.len();
        xml.nodes
            .push(element(Some(store), XADES_141, "SPDocSpecification"));
        let identifier = xml.nodes.len();
        xml.nodes
            .push(element(Some(specification), XADES, "Identifier"));
        xml.nodes.push(Record {
            parent: Some(identifier),
            value: Node::Text("https://example.test/spec"),
        });
        let value = xml.nodes.len();
        xml.nodes.push(element(
            Some(store),
            XADES_141,
            if local {
                "SigPolDocLocalURI"
            } else {
                "SignaturePolicyDocument"
            },
        ));
        let signed = signed_roles(&xml, &memory).unwrap();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        let declaration = policy::read(&access, &signed).unwrap().unwrap();
        let layout = container::locate(&access, 0).unwrap();
        let bound = binding::bind(&access, &layout).unwrap();
        let unsigned = unsigned::read(&access, &bound, unsigned::Requirements::Basic).unwrap();
        // The source dispatch consumes one unit; the retained policy pair
        // still needs two. Preserve the last unit rather than entering fields.
        let short = Memory::new(Arc::new(MemoryAccount::new(0, 2)));
        let lower = Access::new(&xml, xml.snapshot(), &short);
        assert!(matches!(
            policy::read_store(&lower, &unsigned, Some(&declaration)),
            Err(properties::Error::Resource {
                cause: MemoryError::WorkExceeded,
                ..
            })
        ));
        assert_eq!(short.work_remaining(), 1);
        let source = policy::read_store(&access, &unsigned, Some(&declaration))
            .unwrap()
            .unwrap();
        assert_eq!(source.snapshot(), xml.snapshot());
        assert_eq!(source.signature(), 0);
        assert_eq!(source.node(), store);
        assert_eq!(source.specification().identifier, identifier);
        match source.document() {
            policy::StoredDocument::Local(node) => {
                assert!(local);
                assert_eq!(node, value);
            }
            policy::StoredDocument::Embedded(node) => {
                assert!(!local);
                assert_eq!(node, value);
            }
        }
        assert_eq!(xml.streamed.get(), 0);
        assert_eq!(xml.decoded_pointer.get(), 0);
    }
    assert_eq!(memory.used(), 0);
}

#[test]
fn signed_policy_implied_is_an_original_empty_choice_not_an_accepted_policy() {
    // Implied remains explicit original syntax: neither absent policy nor an
    // empty marker selects an application policy or proves a document digest.
    let memory = funded();
    let (mut xml, property, node) = policy_tree(true, false);
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let reading = policy::read(&access, &signed).unwrap().unwrap();
    assert_eq!(reading.snapshot(), xml.snapshot());
    assert_eq!(reading.signature(), 0);
    assert_eq!(reading.node(), property);
    assert!(
        matches!(reading.declaration(), policy::Declaration::Implied(original) if *original == node)
    );
    let unexpected = xml.nodes.len();
    xml.nodes.push(element(Some(node), XADES, "not-empty"));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(policy::read(&access, &signed), Err(properties::Error::Unexpected { node }) if node == unexpected)
    );
    xml.nodes.pop();
    let extra = xml.nodes.len();
    xml.nodes
        .push(element(Some(property), XADES, "SignaturePolicyId"));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(policy::read(&access, &signed), Err(properties::Error::Unexpected { node }) if node == extra)
    );
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn policy_specification_transform_requires_original_qualifier_before_execution() {
    // EN5.2.9.1 requires an actual SPDocSpecification for its special transform.
    // AnyType can contain multiple direct specifications; parsing preserves all
    // of them without pretending to choose/execute one technical specification.
    let memory = funded();
    let (mut xml, _, node) = policy_tree(false, true);
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        policy::read(&access, &signed),
        Err(properties::Error::Missing {
            local: "SPDocSpecification",
            ..
        })
    ));
    assert_eq!(memory.used(), 0);
    let qualifiers = xml.nodes.len();
    xml.nodes
        .push(element(Some(node), XADES, "SigPolicyQualifiers"));
    let wrapper = xml.nodes.len();
    xml.nodes
        .push(element(Some(qualifiers), XADES, "SigPolicyQualifier"));
    let mut originals = Vec::new();
    for value in [
        "https://example.test/spec/one",
        "https://example.test/spec/two",
    ] {
        let specification = xml.nodes.len();
        originals.push(specification);
        xml.nodes
            .push(element(Some(wrapper), XADES_141, "SPDocSpecification"));
        let identifier = xml.nodes.len();
        xml.nodes
            .push(element(Some(specification), XADES, "Identifier"));
        xml.nodes.push(Record {
            parent: Some(identifier),
            value: Node::Text(value),
        });
    }
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let reading = policy::read(&access, &signed).unwrap().unwrap();
    let policy::Declaration::Explicit(explicit) = reading.declaration() else {
        panic!("not explicit")
    };
    assert_eq!(explicit.qualifiers.len(), 1);
    assert_eq!(&*explicit.qualifiers[0].specifications, &originals);
    assert!(memory.used() > 0);
    drop(reading);
    assert_eq!(memory.used(), 0);
    let empty = Memory::new(Arc::new(MemoryAccount::new(0, 100_000)));
    let access = Access::new(&xml, xml.snapshot(), &empty);
    assert!(matches!(
        policy::read(&access, &signed),
        Err(properties::Error::Resource { .. })
    ));
    assert_eq!(empty.used(), 0);
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn absent_optional_signed_properties_still_check_the_live_source_generation() {
    // An absent optional property must not turn stale original evidence into
    // a successful None result merely because no inner node needs decoding.
    let memory = funded();
    let (xml, _) = certificate_tree();
    let signed = signed_roles(&xml, &memory).unwrap();
    xml.generation.set(2);
    let access = Access::new(&xml, signed.snapshot(), &memory);
    assert!(matches!(
        role::read(&access, &signed),
        Err(properties::Error::Access(AccessError::Changed { .. }))
    ));
    assert!(matches!(
        policy::read(&access, &signed),
        Err(properties::Error::Access(AccessError::Changed { .. }))
    ));
}

#[test]
fn signed_commitment_all_and_explicit_manifest_share_original_reference_authority() {
    // All expands the actual authenticated catalogue except SignedProperties;
    // explicit element-content URI resolves through that same owner authority.
    let memory = funded();
    for all in [true, false] {
        let (mut xml, node, choice) = commitment_tree(all);
        let manifest = xml.nodes.len();
        xml.nodes.push(element(Some(1), DS, "Manifest"));
        let reference = xml.nodes.len();
        xml.nodes.push(element(Some(manifest), DS, "Reference"));
        xml.references.push(Reference {
            node: reference,
            target: None,
            kind: None,
            status: Status::Valid,
            coverage: Coverage::Transformed,
        });
        if !all {
            xml.include_routes[0] = (choice, 2, Boolean::Absent);
        }
        let signed = signed_roles(&xml, &memory).unwrap();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        let layout = container::locate(&access, 0).unwrap();
        let bound = binding::bind(&access, &layout).unwrap();
        let original = commitment::read(&access, &signed, 0).unwrap();
        assert_eq!(original.node(), node);
        // Retained commitment evidence cannot spend an unadmitted comparison
        // before this operation's first source query or output allocation.
        let short = Memory::new(Arc::new(MemoryAccount::new(0, 1)));
        let lower = Access::new(&xml, xml.snapshot(), &short);
        assert!(matches!(
            commitment::associate(&lower, &bound, &original),
            Err(commitment::AssociationError::Property(
                properties::Error::Resource {
                    cause: MemoryError::WorkExceeded,
                    ..
                }
            ))
        ));
        assert_eq!(short.work_remaining(), 1);
        let objects = commitment::associate(&access, &bound, &original).unwrap();
        if all {
            assert_eq!(
                objects
                    .iter()
                    .map(|object| object.reference)
                    .collect::<Vec<_>>(),
                [10, reference]
            );
        } else {
            assert_eq!(objects.len(), 1);
            assert_eq!(objects[0].reference, reference);
        }
        assert_eq!(objects.last().unwrap().parent, Parent::Manifest(manifest));
        assert_eq!(xml.streamed.get(), 0);
    }
}

#[test]
fn signed_commitment_refuses_mixed_choices_qualifiers_and_nonempty_all_marker() {
    // Choice/cardinality and the commitment-specific no-Qualifier rule are
    // schema/normative requirements, independent of the commitment URI meaning.
    let memory = funded();
    let (mut xml, node, choice) = commitment_tree(true);
    let extra = xml.nodes.len();
    xml.nodes
        .push(element(Some(node), XADES, "ObjectReference"));
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(commitment::read(&access, &signed, 0), Err(properties::Error::Unexpected { node }) if node == extra)
    );
    xml.nodes.pop();
    xml.identifier_qualifier = Some("OIDAsURI");
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        commitment::read(&access, &signed, 0),
        Err(properties::Error::Forbidden { .. })
    ));
    xml.identifier_qualifier = None;
    let nested = xml.nodes.len();
    xml.nodes.push(element(Some(choice), XADES, "bad"));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(commitment::read(&access, &signed, 0), Err(properties::Error::Unexpected { node }) if node == nested)
    );
    xml.nodes.pop();
    xml.nodes.push(Record {
        parent: Some(choice),
        value: Node::Text("not empty"),
    });
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        commitment::read(&access, &signed, 0),
        Err(properties::Error::Child(_))
    ));
}

#[test]
fn signed_commitment_catalogue_refusal_rolls_back_without_partial_association() {
    // Retained output is dropped if catalogue completion fails; a resource
    // refusal cannot become an empty successful all-objects commitment.
    let storage = funded();
    let (mut xml, _, _) = commitment_tree(true);
    let signed = signed_roles(&xml, &storage).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &storage);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let original = commitment::read(&access, &signed, 0).unwrap();
    xml.reference_completeness = Completeness::Incomplete(10);
    let query = funded();
    let access = Access::new(&xml, xml.snapshot(), &query);
    assert!(matches!(
        commitment::associate(&access, &bound, &original),
        Err(commitment::AssociationError::Incomplete { node: 10 })
    ));
    assert_eq!(query.used(), 0);
    let empty = Memory::new(Arc::new(MemoryAccount::new(0, 100_000)));
    let access = Access::new(&xml, xml.snapshot(), &empty);
    assert!(matches!(
        commitment::associate(&access, &bound, &original),
        Err(commitment::AssociationError::Property(
            properties::Error::Resource { .. }
        ))
    ));
    assert_eq!(empty.used(), 0);
    xml.document += 1;
    let access = Access::new(&xml, xml.snapshot(), &query);
    assert!(matches!(
        commitment::associate(&access, &bound, &original),
        Err(commitment::AssociationError::Property(
            properties::Error::Access(AccessError::Changed { .. })
        ))
    ));
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn data_format_identifier_uses_one_auxiliary_schema_and_exact_qualifier() {
    // An optional ObjectIdentifier still has required inner syntax; Qualifier
    // uses the official schema's case-sensitive enumeration, never a default.
    let storage = funded();
    let (mut xml, property, object_identifier) = data_format_tree();
    xml.nodes.last_mut().unwrap().value = Node::Other;
    xml.nodes[object_identifier].value = Node::Element(Name {
        namespace: XADES,
        local: "ObjectIdentifier",
    });
    let signed = signed_roles(&xml, &storage).unwrap();
    let query = memory(100_000);
    let access = Access::new(&xml, xml.snapshot(), &query);
    assert!(matches!(format::read(&access, &signed, 0),
        Err(format::FormatError::Property(properties::Error::Missing { parent, local: "Identifier" })) if parent == object_identifier));
    let identifier = xml.nodes.len();
    xml.nodes
        .push(element(Some(object_identifier), XADES, "Identifier"));
    xml.nodes.push(Record {
        parent: Some(identifier),
        value: Node::Text("urn:oid:1.2.3"),
    });
    xml.mime(property);
    xml.identifier_qualifier = Some("OIDAsURN");
    let access = Access::new(&xml, xml.snapshot(), &query);
    assert!(format::read(&access, &signed, 0).is_ok());
    let read = identifier::read(&access, object_identifier).unwrap();
    assert_eq!(read.identifier, identifier);
    assert_eq!(read.qualifier, Some(identifier::Qualifier::Urn));
    xml.identifier_qualifier = Some("OIDASURN");
    let access = Access::new(&xml, xml.snapshot(), &query);
    assert!(matches!(identifier::read(&access, object_identifier),
        Err(properties::Error::Unexpected { node }) if node == identifier));
    xml.identifier_qualifier = None;
    let documentation = xml.nodes.len();
    xml.nodes.push(element(
        Some(object_identifier),
        XADES,
        "DocumentationReferences",
    ));
    let access = Access::new(&xml, xml.snapshot(), &query);
    assert!(matches!(identifier::read(&access, object_identifier),
        Err(properties::Error::Missing { parent, local: "DocumentationReference" }) if parent == documentation));
    let reference = xml.nodes.len();
    xml.nodes.push(element(
        Some(documentation),
        XADES,
        "DocumentationReference",
    ));
    xml.nodes.push(Record {
        parent: Some(reference),
        value: Node::Text("https://example.test/spec"),
    });
    let access = Access::new(&xml, xml.snapshot(), &query);
    let read = identifier::read(&access, object_identifier).unwrap();
    assert_eq!(read.qualifier, None);
    assert_eq!(read.documentation, Some(documentation));
    assert_eq!(query.used(), 0);
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn production_place_requires_a_nonempty_ordered_current_schema() {
    // EN §5.2.5 forbids an empty place even though all XSD fields are optional.
    // Once present, the fields keep the V2 sequence and singleton cardinality.
    let memory = funded();
    let (mut xml, _) = certificate_tree();
    let place = xml.nodes.len();
    xml.nodes
        .push(element(Some(4), XADES, "SignatureProductionPlaceV2"));
    assert!(matches!(signed_roles(&xml, &memory),
        Err(properties::Error::Missing { parent, .. }) if parent == place));
    let city = xml.nodes.len();
    xml.nodes.push(element(Some(place), XADES, "City"));
    xml.nodes.push(Record {
        parent: Some(city),
        value: Node::Text("Kyiv"),
    });
    assert_eq!(signed_roles(&xml, &memory).unwrap().place(), Some(place));
    let country = xml.nodes.len();
    xml.nodes.push(element(Some(place), XADES, "CountryName"));
    let late_street = xml.nodes.len();
    xml.nodes.push(element(Some(place), XADES, "StreetAddress"));
    assert!(matches!(signed_roles(&xml, &memory),
        Err(properties::Error::Order { node }) if node == late_street));
    xml.nodes.pop();
    xml.nodes[country].value = Node::Element(Name {
        namespace: XADES,
        local: "City",
    });
    assert!(matches!(signed_roles(&xml, &memory),
        Err(properties::Error::Duplicate { node }) if node == country));
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn baseline_data_format_keeps_covered_original_nodes_without_allocating() {
    // This validates baseline structure only, not URI resolution, MIME syntax
    // or agreement with the data object's own attributes.
    let storage = funded();
    let (xml, node, mime) = data_format_tree();
    let signed = signed_roles(&xml, &storage).unwrap();
    let original_access = Access::new(&xml, xml.snapshot(), &storage);
    let bound = binding::bind(
        &original_access,
        &container::locate(&original_access, 0).unwrap(),
    )
    .unwrap();
    let query = memory(100_000);
    let access = Access::new(&xml, xml.snapshot(), &query);
    let original = format::read(&access, &signed, 0).unwrap();
    // Reusing a retained format does not pay its new binding comparison.
    let short = Memory::new(Arc::new(MemoryAccount::new(0, 1)));
    let lower = Access::new(&xml, xml.snapshot(), &short);
    assert!(matches!(
        format::resolve(&lower, &bound, &original),
        Err(format::FormatError::Property(properties::Error::Access(
            AccessError::Resource {
                cause: MemoryError::WorkExceeded,
                ..
            }
        )))
    ));
    assert_eq!(short.work_remaining(), 1);
    assert_eq!(original.snapshot(), xml.snapshot());
    assert_eq!(original.signature(), 0);
    assert_eq!(original.node(), node);
    assert_eq!(original.mime(), mime);
    assert_eq!(original.description(), None);
    assert_eq!(original.identifier(), None);
    assert_eq!(original.encoding(), None);
    assert_eq!(query.used(), 0);
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn data_format_object_metadata_is_exact_across_text_fragments_and_comments() {
    // EN §5.2.4 requires exact values, not MIME case folding. Character-data
    // fragments concatenate, comments contribute nothing, and mismatch refuses.
    let memory = funded();
    let (mut xml, property, mime) = data_format_tree();
    xml.nodes.last_mut().unwrap().value = Node::Other;
    xml.object_mime = Some("text/plain");
    xml.nodes.push(Record {
        parent: Some(mime),
        value: Node::Text("text/"),
    });
    xml.nodes.push(Record {
        parent: Some(mime),
        value: Node::Other,
    });
    let last = xml.nodes.len();
    xml.nodes.push(Record {
        parent: Some(mime),
        value: Node::Text("plain"),
    });
    xml.references[0].target = Some(1);
    xml.include_routes.push((property, 0, Boolean::Absent));
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let original = format::read(&access, &signed, 0).unwrap();
    assert!(matches!(
        format::resolve(&access, &bound, &original),
        Ok(Resolution::Resolved(_))
    ));
    xml.nodes[last].value = Node::Text("PLAIN");
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(format::resolve(&access, &bound, &original).is_err());
    // A nested element is not simple content, even if its text would match.
    xml.nodes[last].value = Node::Element(Name {
        namespace: XADES,
        local: "nested",
    });
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(format::resolve(&access, &bound, &original).is_err());
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn data_format_encoding_agreement_preserves_absence_and_original_target_kind() {
    // A present Encoding must match exactly. Absent object metadata adds no
    // default, and similarly named attributes on a non-ds:Object are irrelevant.
    let storage = funded();
    let (mut xml, property, _) = data_format_tree();
    let encoding = xml.nodes.len();
    xml.nodes.push(element(Some(property), XADES, "Encoding"));
    let text = xml.nodes.len();
    xml.nodes.push(Record {
        parent: Some(encoding),
        value: Node::Text("urn:encoding:one"),
    });
    xml.references[0].target = Some(1);
    xml.include_routes.push((property, 0, Boolean::Absent));
    let signed = signed_roles(&xml, &storage).unwrap();
    let query = memory(100_000);
    let access = Access::new(&xml, xml.snapshot(), &query);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let original = format::read(&access, &signed, 0).unwrap();
    assert!(matches!(
        format::resolve(&access, &bound, &original),
        Ok(Resolution::Resolved(_))
    ));
    xml.binary_encoding = Some("urn:encoding:one");
    let access = Access::new(&xml, xml.snapshot(), &query);
    assert!(matches!(
        format::resolve(&access, &bound, &original),
        Ok(Resolution::Resolved(_))
    ));
    xml.nodes[text].value = Node::Text("urn:encoding:ONE");
    let access = Access::new(&xml, xml.snapshot(), &query);
    assert!(matches!(format::resolve(&access, &bound, &original),
        Err(format::FormatError::Metadata { node, object: 1, local: "Encoding" }) if node == encoding));
    xml.nodes[1].value = Node::Element(Name {
        namespace: "urn:application",
        local: "Object",
    });
    let access = Access::new(&xml, xml.snapshot(), &query);
    assert!(matches!(
        format::resolve(&access, &bound, &original),
        Ok(Resolution::Resolved(_))
    ));
    assert_eq!(query.used(), 0);
    let exhausted = memory(0);
    let access = Access::new(&xml, xml.snapshot(), &exhausted);
    assert!(matches!(
        format::resolve(&access, &bound, &original),
        Err(format::FormatError::Property(properties::Error::Access(
            AccessError::Resource { .. }
        )))
    ));
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn data_format_simple_values_reject_nested_elements_without_object_metadata() {
    // Simple-typed property content cannot hide a child element merely because
    // no ds:Object attribute is present to trigger the agreement comparison.
    let memory = funded();
    let (mut xml, _, mime) = data_format_tree();
    xml.nodes
        .push(element(Some(mime), "urn:application", "nested"));
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        format::read(&access, &signed, 0),
        Err(format::FormatError::Property(
            properties::Error::Unexpected { .. }
        ))
    ));
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn data_format_empty_mime_is_malformed_not_an_implied_default() {
    // RFC2045 subtype and type are mandatory. XAdES has no missing/empty
    // MimeType fallback to text/plain, regardless of ds:Object metadata.
    let memory = funded();
    let (mut xml, _, mime) = data_format_tree();
    xml.nodes.last_mut().unwrap().value = Node::Other;
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(format::read(&access, &signed, 0),
        Err(format::FormatError::Property(properties::Error::Lexical { node, local: "MimeType" })) if node == mime));
}

#[test]
fn baseline_data_format_requires_mime_and_original_reference_attribute() {
    // The general schema permits absent MimeType; the baseline table does not.
    // Neither omission receives a fabricated MIME value or default reference.
    let memory = funded();
    let (mut xml, node, _) = data_format_tree();
    // Remove the complete MimeType field, including its text child.
    xml.nodes.pop();
    xml.nodes.pop();
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(format::read(&access, &signed, 0),
        Err(format::FormatError::Property(properties::Error::Missing { parent, local: "MimeType" })) if parent == node));
    xml.object_reference = None;
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(format::read(&access, &signed, 0),
        Err(format::FormatError::Property(properties::Error::Missing { parent, local: "ObjectReference" })) if parent == node));
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn baseline_format_resolves_signed_manifest_without_promoting_foreign_or_unchecked_references() {
    // Same resolver as timestamp Include: a signed Manifest instruction is
    // eligible, a foreign instruction or unchecked target is not a digest fact.
    let memory = funded();
    let (mut xml, node, _) = data_format_tree();
    let manifest = xml.nodes.len();
    xml.nodes.push(element(Some(1), DS, "Manifest"));
    let reference = xml.nodes.len();
    xml.nodes.push(element(Some(manifest), DS, "Reference"));
    xml.references.push(Reference {
        node: reference,
        target: Some(1),
        kind: None,
        status: Status::Valid,
        coverage: Coverage::ElementContent,
    });
    xml.object_reference = Some("#manifest");
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let original = format::read(&access, &signed, 0).unwrap();
    let Resolution::Resolved(resolved) = format::resolve(&access, &bound, &original).unwrap()
    else {
        panic!("signed Manifest reference unresolved");
    };
    assert_eq!(resolved.reference, reference);
    assert_eq!(resolved.target, Some(1));
    assert_eq!(resolved.parent, Parent::Manifest(manifest));
    assert_eq!(xml.streamed.get(), 0);
    xml.object_reference = Some("#foreign");
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        format::resolve(&access, &bound, &original),
        Ok(Resolution::Unauthenticated)
    ));
    xml.object_reference = Some("#manifest");
    xml.references[2].status = Status::Unchecked;
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(format::resolve(&access, &bound, &original), Err(format::FormatError::Reference { node: at, status: Status::Unchecked }) if at == reference)
    );
    xml.generation.set(2);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(format::resolve(&access, &bound, &original), Err(format::FormatError::Property(properties::Error::Access(AccessError::Changed { node: at }))) if at == node)
    );
}

#[test]
fn format_coverage_requires_every_authenticated_manifest_object_not_just_resolved_properties() {
    // A missing format for an indirectly signed object used to be invisible
    // to property-only traversal. Complete instruction enumeration detects it.
    let memory = funded();
    let (mut xml, format_node, _) = data_format_tree();
    xml.object_reference = Some("#direct");
    let manifest = xml.nodes.len();
    xml.nodes.push(element(Some(1), DS, "Manifest"));
    let reference = xml.nodes.len();
    xml.nodes.push(element(Some(manifest), DS, "Reference"));
    xml.references.push(Reference {
        node: reference,
        target: None,
        kind: None,
        status: Status::Valid,
        coverage: Coverage::Transformed,
    });
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let before = memory.used();
    assert!(
        matches!(format::check_references(&access, &bound, &signed, None), Err(format::CoverageError::Missing { reference: at }) if at == reference)
    );
    assert_eq!(memory.used(), before);
    let parent = xml.nodes[format_node].parent.unwrap();
    let additional = xml.nodes.len();
    xml.nodes
        .push(element(Some(parent), XADES, "DataObjectFormat"));
    xml.mime(additional);
    xml.include_routes = vec![
        (format_node, 0, Boolean::Absent),
        (additional, 2, Boolean::Absent),
    ];
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let complete = format::check_references(&access, &bound, &signed, None).unwrap();
    assert_eq!(complete.len(), 2);
    assert_eq!(complete[0].reference(), 10);
    assert_eq!(complete[0].original().node(), format_node);
    assert_eq!(complete[1].reference(), reference);
    assert_eq!(complete[1].original().node(), additional);
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn format_coverage_preserves_incomplete_manifest_and_duplicate_association_refusals() {
    // A fail-fast/unchecked Manifest array cannot prove catalogue completeness,
    // even when all descriptors present so far have their format.
    let memory = funded();
    let (mut xml, node, _) = data_format_tree();
    xml.object_reference = Some("#direct");
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    xml.reference_completeness = Completeness::Unchecked(1);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        format::check_references(&access, &bound, &signed, None),
        Err(format::CoverageError::Incomplete { node: 1 })
    ));
    xml.reference_completeness = Completeness::Incomplete(1);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        format::check_references(&access, &bound, &signed, None),
        Err(format::CoverageError::Incomplete { node: 1 })
    ));
    xml.reference_completeness = Completeness::Complete;
    let parent = xml.nodes[node].parent.unwrap();
    let duplicate = xml.nodes.len();
    xml.nodes
        .push(element(Some(parent), XADES, "DataObjectFormat"));
    xml.mime(duplicate);
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        format::check_references(&access, &bound, &signed, None),
        Err(format::CoverageError::Repeated { reference: 10 })
    ));
}

#[test]
fn format_counter_exemption_pays_identity_before_reporting_foreign() {
    // A genuine counter receipt for another selected signature is foreign,
    // but the current operation must fund that conclusion before comparing.
    let mut xml = preparation_tree();
    let (child, _, _) = append_nested_counter(&mut xml, 7, 12, true);
    let counter_node = xml.nodes[child].parent.unwrap();
    let original = funded();
    let signed = signed_roles(&xml, &original).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &original);
    let bound = binding::bind(&access, &container::locate(&access, 0).unwrap()).unwrap();
    let receipt = counter::bind(
        &access,
        counter::read(&access, &bound, counter_node).unwrap(),
    )
    .unwrap();
    let short = Memory::new(Arc::new(MemoryAccount::new(0, 3)));
    let lower = Access::new(&xml, xml.snapshot(), &short);
    assert!(
        matches!(format::check_references(&lower, &bound, &signed, Some(&receipt)),
        Err(format::CoverageError::Access(AccessError::Resource {
            node, cause: MemoryError::WorkExceeded })) if node == counter_node)
    );
    assert_eq!(short.work_remaining(), 1);
    assert_eq!(short.used(), 0);
    assert!(matches!(
        format::check_references(&access, &bound, &signed, Some(&receipt)),
        Err(format::CoverageError::ForeignCounter { .. })
    ));
}

#[test]
fn format_coverage_keeps_resource_lookup_and_unchecked_target_separate() {
    // Missing/foreign lookup is a semantic disposition. Memory/work refusal and
    // unchecked target digest cannot become a successful empty association list.
    let memory = funded();
    let (mut xml, _, _) = data_format_tree();
    xml.object_reference = Some("#direct");
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    // The source signature equality costs two units before any XML query or
    // output allocation. A short admission must preserve the remaining unit.
    let short = Memory::new(Arc::new(MemoryAccount::new(0, 1)));
    let lower = Access::new(&xml, xml.snapshot(), &short);
    assert!(matches!(
        format::check_references(&lower, &bound, &signed, None),
        Err(format::CoverageError::Access(AccessError::Resource {
            cause: MemoryError::WorkExceeded,
            ..
        }))
    ));
    assert_eq!(short.work_remaining(), 1);
    assert_eq!(short.used(), 0);
    let no_storage = Memory::new(Arc::new(MemoryAccount::new(0, 300_000)));
    let access = Access::new(&xml, xml.snapshot(), &no_storage);
    assert!(matches!(
        format::check_references(&access, &bound, &signed, None),
        Err(format::CoverageError::Access(AccessError::Resource { .. }))
    ));
    assert_eq!(no_storage.used(), 0);
    xml.object_reference = Some("#foreign");
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        format::check_references(&access, &bound, &signed, None),
        Err(format::CoverageError::Lookup {
            resolution: Resolution::Unauthenticated,
            ..
        })
    ));
    xml.object_reference = Some("#direct");
    xml.references[0].status = Status::Unchecked;
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        format::check_references(&access, &bound, &signed, None),
        Err(format::CoverageError::Format(
            format::FormatError::Reference {
                node: 10,
                status: Status::Unchecked
            }
        ))
    ));
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn wide_format_identity_index_releases_scratch_and_retains_only_admitted_result_capacity() {
    // Identity indexing avoids scanning every format for each reference. The
    // existing map still conservatively prepays its possible hidden probes;
    // this test does not claim a linear worst-case admission bound.
    let memory = Memory::new(Arc::new(MemoryAccount::new(2_000_000, 10_000_000)));
    let (mut xml, first, _) = data_format_tree();
    let parent = xml.nodes[first].parent.unwrap();
    xml.include_routes.push((first, 0, Boolean::Absent));
    for _ in 1..128 {
        let reference = xml.nodes.len();
        xml.nodes.push(element(Some(9), DS, "Reference"));
        let ordinal = xml.references.len();
        xml.references.push(Reference {
            node: reference,
            target: None,
            kind: None,
            status: Status::Valid,
            coverage: Coverage::Transformed,
        });
        let property = xml.nodes.len();
        xml.nodes
            .push(element(Some(parent), XADES, "DataObjectFormat"));
        xml.mime(property);
        xml.include_routes
            .push((property, ordinal, Boolean::Absent));
    }
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let held = memory.used();
    let covered = format::check_references(&access, &bound, &signed, None).unwrap();
    assert_eq!(covered.len(), 128);
    assert_eq!(
        covered.last().unwrap().reference(),
        xml.references.last().unwrap().node
    );
    assert_eq!(
        memory.used() - held,
        covered.capacity() * core::mem::size_of::<format::CoveredFormat<usize, u8, u8>>()
    );
    drop(covered);
    assert_eq!(memory.used(), held);
}

#[test]
fn empty_format_catalogue_cannot_rebind_evidence_to_another_document() {
    // The empty fast path must apply the same source guard as nonempty formats.
    // Coincident numeric node IDs in another document are not original identity.
    let memory = funded();
    let (xml, _) = certificate_tree();
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let mut foreign = xml;
    foreign.document = 10;
    foreign.references[0].target = Some(3);
    let access = Access::new(&foreign, foreign.snapshot(), &memory);
    assert!(matches!(
        format::check_references(&access, &bound, &signed, None),
        Err(format::CoverageError::Access(AccessError::Changed {
            node: 0
        }))
    ));
}

#[test]
fn baseline_data_format_rejects_duplicate_order_and_unrecognized_children() {
    // A correct first MimeType cannot hide a later malformed member.
    let memory = funded();
    let (mut xml, node, _) = data_format_tree();
    let later = xml.nodes.len();
    xml.nodes.push(element(Some(node), XADES, "MimeType"));
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(format::read(&access, &signed, 0),
        Err(format::FormatError::Property(properties::Error::Duplicate { node: at })) if at == later));
    xml.nodes[later] = element(Some(node), XADES, "Description");
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(format::read(&access, &signed, 0),
        Err(format::FormatError::Property(properties::Error::Order { node: at })) if at == later));
    xml.nodes[later] = element(Some(node), "urn:foreign", "Encoding");
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(format::read(&access, &signed, 0),
        Err(format::FormatError::Property(properties::Error::Unexpected { node: at })) if at == later));
}

#[test]
fn baseline_data_format_distinguishes_caller_selection_source_and_resource_refusal() {
    // Invalid caller index, changed document and a spent operation are terminal
    // causes, not malformed MIME text, missing data or a successful empty format.
    let memory = funded();
    let (xml, _, _) = data_format_tree();
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        format::read(&access, &signed, 1),
        Err(format::FormatError::Selection {
            signature: 0,
            index: 1
        })
    ));
    let zero = self::memory(0);
    let access = Access::new(&xml, xml.snapshot(), &zero);
    assert!(matches!(
        format::read(&access, &signed, 0),
        Err(format::FormatError::Property(properties::Error::Access(
            AccessError::Resource {
                cause: MemoryError::WorkExceeded,
                ..
            }
        )))
    ));
    assert_eq!(zero.used(), 0);
    xml.generation.set(2);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        format::read(&access, &signed, 0),
        Err(format::FormatError::Property(properties::Error::Access(
            AccessError::Changed { .. }
        )))
    ));
    assert_eq!(xml.streamed.get(), 0);
}

fn validation_tree(local: &'static str) -> (Tree, usize, usize) {
    let mut xml = Tree::normal();
    let node = xml.nodes.len();
    xml.nodes.push(element(Some(7), XADES_141, local));
    let certificates = xml.nodes.len();
    xml.nodes
        .push(element(Some(node), XADES, "CertificateValues"));
    xml.nodes.push(element(
        Some(certificates),
        XADES,
        "EncapsulatedX509Certificate",
    ));
    (xml, node, certificates)
}

#[test]
fn validation_data_preserves_material_and_adjacent_timestamp_over_wrong_uri_hint() {
    // EN5.5.1.2 gives adjacent timestamp priority; a wrong URI hint cannot discard
    // otherwise usable material. No timestamp authentication is asserted here.
    let memory = funded();
    let (mut xml, node, certificates) = validation_tree("TimeStampValidationData");
    xml.validation_uri = Some("#another-timestamp");
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    // Membership follows a successful original child lookup. Leave too little
    // for both identities and require refusal before deriving an adjacent hint.
    let current = funded();
    let observed = query_boundary::Boundary::after_inspection(&xml, node, &current, 1);
    let lower = Access::new(&observed, xml.snapshot(), &current);
    assert!(matches!(validation::read(&lower, &bound, node),
        Err(validation::Error::Input(InputError::Resource {
            node: at, cause: MemoryError::WorkExceeded })) if at == node));
    assert_eq!(current.work_remaining(), 1);
    let data = validation::read(&access, &bound, node).unwrap();
    assert_eq!(data.role(), validation::Role::Timestamp);
    assert_eq!(data.node(), node);
    assert_eq!(data.signature(), 0);
    assert_eq!(data.snapshot(), xml.snapshot());
    assert_eq!(data.hint(), Some(validation::Hint::Adjacent(8)));
    assert_eq!(data.certificates().unwrap().parent(), certificates);
    assert_eq!(
        data.certificates().unwrap().entries(),
        &[material::Entry::Encapsulated {
            node: certificates + 1,
            kind: material::Kind::Certificate
        },]
    );
    assert!(data.revocations().is_none());
    assert_eq!(xml.decoded_pointer.get(), 0);
    drop(data);
    assert_eq!(memory.used(), 0);
}

#[test]
fn nonadjacent_timestamp_validation_uri_stays_an_unresolved_hint() {
    // No private URI grammar, synthetic ordinal, or guessed preceding property
    // can create an association to a different timestamp.
    let memory = funded();
    let (mut xml, node, _) = validation_tree("TimeStampValidationData");
    xml.nodes[8] = element(Some(7), XADES_141, "AnyValidationData");
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let data = validation::read(&access, &bound, node).unwrap();
    assert_eq!(
        data.hint(),
        Some(validation::Hint::Unresolved { uri_present: false })
    );
    drop(data);
    xml.validation_uri = Some("not-a-qualified-timestamp-target");
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let data = validation::read(&access, &bound, node).unwrap();
    assert_eq!(
        data.hint(),
        Some(validation::Hint::Unresolved { uri_present: true })
    );
}

#[test]
fn any_validation_data_forbids_uri_and_requires_actual_material_entries() {
    // URI is prohibited by prose despite being permitted by the common XSD type.
    // Two empty material containers cannot satisfy the required actual values.
    let memory = funded();
    let (mut xml, node, certificates) = validation_tree("AnyValidationData");
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let data = validation::read(&access, &bound, node).unwrap();
    assert_eq!(data.role(), validation::Role::Any);
    assert_eq!(data.hint(), None);
    drop(data);
    xml.validation_uri = Some("");
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(validation::read(&access, &bound, node), Err(validation::Error::ForbiddenUri { node: at }) if at == node)
    );
    assert_eq!(memory.used(), 0);
    xml.validation_uri = None;
    xml.nodes.pop();
    assert_eq!(xml.nodes.len(), certificates + 1);
    xml.nodes
        .push(element(Some(node), XADES, "RevocationValues"));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(
        matches!(validation::read(&access, &bound, node), Err(validation::Error::Empty { node: at }) if at == node)
    );
    assert_eq!(memory.used(), 0);
}

#[test]
fn validation_data_schema_and_capacity_errors_drop_partial_catalogues() {
    // A later error returns no successful partial material set and releases
    // capacity held by the earlier certificate catalogue.
    let memory = funded();
    let (mut xml, node, _) = validation_tree("TimeStampValidationData");
    let later = xml.nodes.len();
    xml.nodes
        .push(element(Some(node), XADES, "CertificateValues"));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    assert!(matches!(validation::read(&access, &bound, node),
        Err(validation::Error::Property(properties::Error::Duplicate { node: at })) if at == later));
    assert_eq!(memory.used(), 0);
    xml.nodes.pop();
    let no_storage = self::memory(100_000);
    let access = Access::new(&xml, xml.snapshot(), &no_storage);
    assert!(matches!(
        validation::read(&access, &bound, node),
        Err(validation::Error::Property(properties::Error::Resource {
            cause: MemoryError::LimitExceeded,
            ..
        }))
    ));
    assert_eq!(no_storage.used(), 0);
    let mut foreign = xml;
    foreign.document = 10;
    let access = Access::new(&foreign, foreign.snapshot(), &memory);
    assert!(matches!(
        validation::read(&access, &bound, node),
        Err(validation::Error::Input(InputError::Access(
            AccessError::Changed { .. }
        )))
    ));
}

#[test]
fn selected_certificate_preflight_refuses_baseline_cert_uri_before_provider() {
    // EN6.3(i) forbids Cert/@URI in the current baseline, despite its presence
    // in the general XAdES schema. Inspection may retain it; baseline checking
    // must not start selected-key/digest work on this nonconforming property.
    let memory = funded();
    let bytes = candidate_certificate();
    let parser = structured_asn1::Parser::der(memory.clone());
    let candidate = structured_pkix::Certificate::read(&parser, &bytes, 900).unwrap();
    let (mut xml, [_, cert, _, method, _, _]) = certificate_tree();
    xml.key_association = certificate::KeyAssociation::Same;
    xml.binary = vec![bytes.iter().fold(0_u8, |sum, byte| sum ^ byte)];
    xml.validation_uri = Some("#a-certificate-hint");
    let signed = signed_roles(&xml, &memory).unwrap();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let refs = certificate::read(&access, &signed).unwrap();
    let provider = WholeCertificate {
        mode: DigestMode::Available,
        method,
        calls: Cell::new(0),
        pointer: Cell::new(0),
    };
    for uri in ["#a-certificate-hint", ""] {
        xml.validation_uri = Some(uri);
        match certificate::check(&xml, &bound, &refs, &candidate, &provider, &memory) {
            Err(certificate::CheckError::ForbiddenCertificateUri { location }) => {
                assert_eq!(location.snapshot, xml.snapshot());
                assert_eq!(location.node, cert);
            }
            Err(error) => panic!("wrong refusal {error:?}"),
            Ok(_) => panic!("baseline Cert URI must be reported before callbacks"),
        }
        assert_eq!(provider.calls.get(), 0);
        assert_eq!(xml.decoded_pointer.get(), 0);
    }
}

#[test]
fn fresh_xml_binary_source_has_real_asn_root_depth_without_a_phantom_container() {
    // XML base64 is not an enclosing ASN.1 element. A fresh ASN primitive at
    // depth zero is allowed by max_depth=0; entering a SEQUENCE still is not.
    let memory = funded();
    let parser = structured_asn1::Parser::new(
        structured_asn1::Encoding::Der,
        structured_asn1::Limits {
            max_depth: 0,
            ..Default::default()
        },
        memory.clone(),
    );
    let operation = parser.read(&[], 900).unwrap();
    let mut xml = Tree::normal();
    xml.binary = vec![2, 1, 7];
    let location = Location {
        snapshot: xml.snapshot(),
        node: 8,
    };
    let value = binary::with_der_reader(&xml, location, &operation, |reader| {
        assert_eq!(reader.depth(), 0);
        reader.read_integer().map(structured_asn1::Integer::as_u64)
    })
    .unwrap();
    assert_eq!(value, Some(7));
    assert_eq!(memory.used(), 0);
}

fn counter_tree() -> (Tree, usize, usize, usize) {
    let mut xml = Tree::normal();
    let node = xml.nodes.len();
    xml.nodes.push(element(Some(7), XADES, "CounterSignature"));
    let child = xml.nodes.len();
    xml.nodes.push(element(Some(node), DS, "Signature"));
    let info = xml.nodes.len();
    xml.nodes.push(element(Some(child), DS, "SignedInfo"));
    let reference = xml.nodes.len();
    xml.nodes.push(element(Some(info), DS, "Reference"));
    xml.nodes.push(element(Some(child), DS, "SignatureValue"));
    xml.counter_evidence = Some(CounterEvidence {
        child,
        status: Status::Valid,
        references: vec![Reference {
            node: reference,
            target: Some(12),
            kind: None,
            status: Status::Valid,
            coverage: Coverage::ElementContent,
        }],
    });
    (xml, node, child, reference)
}

#[test]
fn enveloped_counter_binds_exact_parent_value_with_one_plain_dsig_reference() {
    // XML owner receipts are synthetic here: this checks common binding, not
    // signature arithmetic. Plain XMLDSig counter need not have SignedProperties.
    let memory = funded();
    let (xml, node, child, reference) = counter_tree();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    // A complete child lookup does not include membership equality work.
    let current = funded();
    let observed = query_boundary::Boundary::after_inspection(&xml, node, &current, 1);
    let lower = Access::new(&observed, xml.snapshot(), &current);
    assert!(matches!(counter::read(&lower, &bound, node),
        Err(counter::Error::Input(InputError::Resource {
            node: at, cause: MemoryError::WorkExceeded })) if at == node));
    assert_eq!(current.work_remaining(), 1);
    let original = counter::read(&access, &bound, node).unwrap();
    assert_eq!(original.node(), node);
    assert_eq!(original.child(), child);
    assert_eq!(original.parent(), 0);
    assert_eq!(original.value(), 12);
    // The callback dispatch fits; matching its target is separate work. Both
    // eligibility and selection reuse that same admitted comparison.
    let short = funded();
    let observed = query_boundary::Boundary::exhaust(&xml, 5, &short, 3);
    let lower = Access::new(&observed, xml.snapshot(), &short);
    assert!(matches!(counter::bind(&lower, original),
        Err(counter::Error::Binding(BindingError::Access(AccessError::Resource {
            node, cause: MemoryError::WorkExceeded }))) if node == reference));
    assert_eq!(short.work_remaining(), 1);
    let original = counter::read(&access, &bound, node).unwrap();
    let checked = counter::bind(&access, original).unwrap();
    assert_eq!(checked.reference(), reference);
    assert_eq!(checked.ordinal(), 0);
    assert_eq!(checked.original().snapshot(), xml.snapshot());
    assert_eq!(memory.used(), 0);
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn properties_binding_admits_target_after_reference_callback() {
    // The eligible Reference has been read completely, but its target relation
    // is still a new two-operand scan under the current operation.
    let xml = Tree::normal();
    let original = funded();
    let access = Access::new(&xml, xml.snapshot(), &original);
    let layout = container::locate(&access, 0).unwrap();
    let short = funded();
    let observed = query_boundary::Boundary::exhaust(&xml, 10, &short, 1);
    let lower = Access::new(&observed, xml.snapshot(), &short);
    assert!(matches!(
        binding::bind(&lower, &layout),
        Err(BindingError::Access(AccessError::Resource {
            node: 11,
            cause: MemoryError::WorkExceeded
        }))
    ));
    assert_eq!(short.work_remaining(), 1);
    assert_eq!(short.used(), 0);
}

fn append_nested_counter(
    xml: &mut Tree,
    parent: usize,
    value: usize,
    nested: bool,
) -> (usize, usize, Option<usize>) {
    let node = xml.nodes.len();
    xml.nodes
        .push(element(Some(parent), XADES, "CounterSignature"));
    let child = xml.nodes.len();
    xml.nodes.push(element(Some(node), DS, "Signature"));
    let info = xml.nodes.len();
    xml.nodes.push(element(Some(child), DS, "SignedInfo"));
    let reference = xml.nodes.len();
    xml.nodes.push(element(Some(info), DS, "Reference"));
    let child_value = xml.nodes.len();
    xml.nodes.push(element(Some(child), DS, "SignatureValue"));
    xml.nested_counters.push(CounterEvidence {
        child,
        status: Status::Valid,
        references: vec![Reference {
            node: reference,
            target: Some(value),
            kind: None,
            status: Status::Valid,
            coverage: Coverage::ElementContent,
        }],
    });
    let unsigned = if nested {
        let object = xml.nodes.len();
        xml.nodes.push(element(Some(child), DS, "Object"));
        let qualifying = xml.nodes.len();
        xml.nodes
            .push(element(Some(object), XADES, "QualifyingProperties"));
        let signed = xml.nodes.len();
        xml.nodes
            .push(element(Some(qualifying), XADES, "SignedProperties"));
        xml.nodes
            .push(element(Some(signed), XADES, "SignedSignatureProperties"));
        let unsigned = xml.nodes.len();
        xml.nodes
            .push(element(Some(qualifying), XADES, "UnsignedProperties"));
        let properties = xml.nodes.len();
        xml.nodes.push(element(
            Some(unsigned),
            XADES,
            "UnsignedSignatureProperties",
        ));
        Some(properties)
    } else {
        None
    };
    (child, child_value, unsigned)
}

#[test]
fn iterative_counter_walk_preserves_nested_parent_binding_and_direct_sibling_order() {
    // Deep children cover the immediately preceding parent's value, never the
    // root value; no recursive Rust call or parent catalogue clone is required.
    let mut xml = Tree::normal();
    let (first, mut value, mut parent) = append_nested_counter(&mut xml, 7, 12, true);
    let mut expected = vec![first];
    for depth in 0..128 {
        let (child, next, container) =
            append_nested_counter(&mut xml, parent.unwrap(), value, depth != 127);
        expected.push(child);
        value = next;
        parent = container;
    }
    for _ in 0..32 {
        let (child, _, _) = append_nested_counter(&mut xml, 7, 12, false);
        expected.push(child);
    }
    xml.index_edges();
    let memory = Memory::new(Arc::new(MemoryAccount::new(1_000_000, 100_000_000)));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let results = counter::walk(&access, &bound, |checked, layout| {
        if let Some(layout) = layout {
            assert_eq!(layout.signature(), checked.original().child());
        }
        Ok::<_, ()>(checked.original().child())
    })
    .unwrap();
    assert_eq!(&results[..], expected);
    assert!(memory.used() > 0);
    drop(results);
    assert_eq!(memory.used(), 0);
}

#[test]
fn counter_walk_terminates_on_late_child_math_or_visitor_refusal_without_partial_results() {
    // An earlier successful callback cannot turn an invalid later sibling into a
    // successful prefix. Caller side effects remain visible, retained results do not.
    for visitor_refusal in [false, true] {
        let mut xml = Tree::normal();
        append_nested_counter(&mut xml, 7, 12, false);
        let (bad, _, _) = append_nested_counter(&mut xml, 7, 12, false);
        if !visitor_refusal {
            xml.nested_counters[1].status = Status::Invalid;
        }
        let memory = funded();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        let layout = container::locate(&access, 0).unwrap();
        let bound = binding::bind(&access, &layout).unwrap();
        let visits = Cell::new(0);
        let result = counter::walk(&access, &bound, |checked, _| {
            visits.set(visits.get() + 1);
            if checked.original().child() == bad {
                return Err("visitor refused");
            }
            let mut owned = memory.vector();
            owned.try_extend_from_slice(b"retained diagnostic").unwrap();
            Ok(owned)
        });
        if visitor_refusal {
            assert!(matches!(
                result,
                Err(counter::WalkError::Visitor("visitor refused"))
            ));
            assert_eq!(visits.get(), 2);
        } else {
            assert!(
                matches!(result, Err(counter::WalkError::Counter(counter::Error::Binding(BindingError::Signature { node, status: Status::Invalid }))) if node == bad)
            );
            assert_eq!(visits.get(), 1);
        }
        assert_eq!(memory.used(), 0);
    }
}

#[test]
fn counter_walk_rejects_current_resource_and_mutated_source_instead_of_returning_empty() {
    // Source invalidation drops the callback's accounted result; zero current
    // capacity/work refuses before any child callback or successful empty set.
    let mut xml = Tree::normal();
    append_nested_counter(&mut xml, 7, 12, false);
    let storage = funded();
    let access = Access::new(&xml, xml.snapshot(), &storage);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    for work in [
        Memory::new(Arc::new(MemoryAccount::new(0, 100_000))),
        Memory::new(Arc::new(MemoryAccount::new(4096, 0))),
    ] {
        let access = Access::new(&xml, xml.snapshot(), &work);
        let called = Cell::new(false);
        assert!(
            counter::walk(&access, &bound, |_, _| {
                called.set(true);
                Ok::<_, ()>(())
            })
            .is_err()
        );
        assert!(!called.get());
        assert_eq!(work.used(), 0);
    }
    let result = counter::walk(&access, &bound, |_, _| {
        let mut owned = storage.vector();
        owned.try_push(1u8).unwrap();
        xml.generation.set(2);
        Ok::<_, ()>(owned)
    });
    assert!(matches!(
        result,
        Err(counter::WalkError::Access(AccessError::Changed { .. }))
    ));
    assert_eq!(storage.used(), 0);
}

#[test]
fn counter_walk_never_relabels_broken_child_properties_as_a_plain_dsig_leaf() {
    // Only missing QualifyingProperties means a plain XMLDSig leaf. A broken
    // property container or counter wrapper is a terminal located schema error.
    for malformed_wrapper in [false, true] {
        let mut xml = Tree::normal();
        append_nested_counter(&mut xml, 7, 12, false);
        let bad = if malformed_wrapper {
            let node = xml.nodes.len();
            xml.nodes.push(element(Some(7), XADES, "CounterSignature"));
            node
        } else {
            let (child, _, _) = append_nested_counter(&mut xml, 7, 12, false);
            let object = xml.nodes.len();
            xml.nodes.push(element(Some(child), DS, "Object"));
            let node = xml.nodes.len();
            xml.nodes
                .push(element(Some(object), XADES, "QualifyingProperties"));
            node
        };
        let memory = funded();
        let access = Access::new(&xml, xml.snapshot(), &memory);
        let layout = container::locate(&access, 0).unwrap();
        let bound = binding::bind(&access, &layout).unwrap();
        let calls = Cell::new(0);
        let result = counter::walk(&access, &bound, |_, _| {
            calls.set(calls.get() + 1);
            let mut result = memory.vector();
            result.try_push(7u8).unwrap();
            Ok::<_, ()>(result)
        });
        if malformed_wrapper {
            assert!(
                matches!(result, Err(counter::WalkError::Counter(counter::Error::Input(InputError::Missing { node, local: "Signature" }))) if node == bad)
            );
        } else {
            assert!(
                matches!(result, Err(counter::WalkError::Container(ContainerError::Missing { parent, local: "SignedProperties" })) if parent == bad)
            );
        }
        assert_eq!(calls.get(), 1);
        assert_eq!(memory.used(), 0);
    }
}

#[test]
fn enveloped_counter_never_substitutes_parent_status_wrong_target_or_filtered_coverage() {
    // A successful parent signature and a located nested Signature do not prove
    // that the child authenticates the complete parent SignatureValue element.
    let memory = funded();
    let (mut xml, node, child, reference) = counter_tree();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    xml.counter_evidence.as_mut().unwrap().status = Status::Invalid;
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let original = counter::read(&access, &bound, node).unwrap();
    assert!(matches!(counter::bind(&access, original),
        Err(counter::Error::Binding(BindingError::Signature { node: at, status: Status::Invalid })) if at == child));
    xml.counter_evidence.as_mut().unwrap().status = Status::Valid;
    xml.counter_evidence.as_mut().unwrap().references[0].target = Some(child);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let original = counter::read(&access, &bound, node).unwrap();
    assert!(matches!(
        counter::bind(&access, original),
        Err(counter::Error::Binding(BindingError::Missing { node: 12 }))
    ));
    xml.counter_evidence.as_mut().unwrap().references[0].target = Some(12);
    xml.counter_evidence.as_mut().unwrap().references[0].coverage = Coverage::Transformed;
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let original = counter::read(&access, &bound, node).unwrap();
    assert!(matches!(counter::bind(&access, original),
        Err(counter::Error::Binding(BindingError::Reference { node: at })) if at == reference));
    assert_eq!(xml.streamed.get(), 0);
}

#[test]
fn enveloped_counter_schema_generation_and_work_refusals_remain_terminal() {
    // Extra child signatures are not first-match recovery; source changes and
    // operation exhaustion cannot be reported as a successful bound counter.
    let memory = funded();
    let (mut xml, node, _, _) = counter_tree();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let layout = container::locate(&access, 0).unwrap();
    let bound = binding::bind(&access, &layout).unwrap();
    let duplicate = xml.nodes.len();
    xml.nodes.push(element(Some(node), DS, "Signature"));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(counter::read(&access, &bound, node),
        Err(counter::Error::Input(InputError::Duplicate { node: at })) if at == duplicate));
    xml.nodes.pop();
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let original = counter::read(&access, &bound, node).unwrap();
    let zero = self::memory(0);
    let access = Access::new(&xml, xml.snapshot(), &zero);
    assert!(matches!(
        counter::bind(&access, original),
        Err(counter::Error::Binding(BindingError::Access(
            AccessError::Resource {
                cause: MemoryError::WorkExceeded,
                ..
            }
        )))
    ));
    let access = Access::new(&xml, xml.snapshot(), &memory);
    let original = counter::read(&access, &bound, node).unwrap();
    xml.generation.set(2);
    let access = Access::new(&xml, xml.snapshot(), &memory);
    assert!(matches!(
        counter::bind(&access, original),
        Err(counter::Error::Input(InputError::Access(
            AccessError::Changed { .. }
        )))
    ));
    assert_eq!(memory.used(), 0);
}

#[test]
fn collecting_sink_retains_resource_cause_and_rolls_back_failed_copy() {
    // The fixture sink itself must not bypass accounting while testing streams.
    let memory = memory(1000);
    let mut sink = Collect::new(&memory);
    assert!(matches!(
        sink.write(&[1, 2, 3, 4], &memory),
        Err(SinkError::Resource(MemoryError::LimitExceeded))
    ));
    assert_eq!(sink.bytes(), []);
}

fn checked_with_local_operation<'source>(
    leaf: &'source structured_pkix::Certificate<'source>,
    root: &'source structured_pkix::Certificate<'source>,
    current: &Memory,
) -> structured_ltv::pki::CheckedPath<'source> {
    use structured_ltv::pki::{self, PathOutcome};
    use structured_pkix::path::{self, Anchor, Interval, ValidationBounds, ValidationContext};
    let operation = structured_asn1::Parser::der(current.clone());
    let query = operation.read(&[], 1200).unwrap();
    let execution = query.execution();
    let verifier = timestamp_oracle::PathOracle {
        calls: Cell::new(0),
    };
    let context = ValidationContext {
        execution: &execution,
        interval: Interval::at(1_767_225_600),
        policy: path::Policy::default(),
        bounds: ValidationBounds::default(),
        verifier: &verifier,
        watch: &(),
        recognizer: &(),
    };
    let PathOutcome::Accepted(result) =
        pki::check_path(&[leaf], Anchor::from_certificate(root), &context).unwrap()
    else {
        panic!("actual original actor path")
    };
    assert_eq!(verifier.calls.get(), 1);
    result
}

#[test]
fn checked_actor_path_outlives_operation_and_keeps_original_key_and_charge() {
    // Static facts/member capacity outlive temporary operations; original
    // certificate/key views retain their source without repeating verification.
    #[cfg(miri)]
    eprintln!("retained path: generating two original certificates");
    let (leaf_bytes, root_bytes) = timestamp_oracle::certificate_pair();
    let memory = Memory::new(Arc::new(MemoryAccount::new(1 << 20, 1 << 24)));
    let source = structured_asn1::Parser::der(memory.clone());
    let leaf = structured_pkix::Certificate::read(&source, &leaf_bytes, 400).unwrap();
    let root = structured_pkix::Certificate::read(&source, &root_bytes, 1600).unwrap();
    let current = Memory::new(Arc::new(MemoryAccount::new(1 << 20, 1 << 24)));
    #[cfg(miri)]
    eprintln!("retained path: parsing complete; verifying original path");
    let path = checked_with_local_operation(&leaf, &root, &current);
    assert!(core::ptr::eq(
        path.members()[0].certificate(),
        &raw const leaf
    ));
    assert_eq!(
        path.facts().key.key().as_bytes().as_ptr(),
        leaf.tbs().public_key().as_bytes().as_ptr()
    );
    assert!(current.used() > 0);
    drop(path);
    assert_eq!(current.used(), 0);
}

#[test]
fn crl_reason_union_binds_every_original_authority_without_partial_receipts() {
    // All-reason absence cannot inherit the first source's authority: changing
    // only the second authority must reject with its exact contributor ordinal.
    use structured_ltv::pki::{
        CrlAuthorities, CrlAuthority, CrlBindingError, CrlBindingFailure, CrlCoverageBinding,
        CrlCoverageError,
    };
    use structured_pkix::{
        crl,
        path::WorkingKey,
        temporal::{Instant, LeapTable},
    };
    xml_digest::with_checked_path(|path, _, _, work| {
        let (first, second) = timestamp_oracle::crl_reason_partitions(2);
        let parser = structured_asn1::Parser::der(work.clone());
        let lists = [
            crl::CertificateList::read(&parser, &first, 2000).unwrap(),
            crl::CertificateList::read(&parser, &second, 3000).unwrap(),
        ];
        let issuer = path.anchor().certificate().unwrap();
        let verifier = timestamp_oracle::PathOracle {
            calls: Cell::new(0),
        };
        let utc = LeapTable::new(0, 2_000_000_000, &[]).unwrap();
        let context = crl::Evaluation {
            now: Instant::new(1_767_225_600, 0).unwrap().into(),
            utc: &utc,
            execution: work,
            origin: crl::SourceOrigin::Cached,
        };
        let mut coverage = None;
        for list in &lists {
            let crl::Authentication::Authenticated(source) = list
                .authenticate(
                    issuer,
                    WorkingKey::standing(issuer.tbs().public_key()),
                    &verifier,
                    work,
                )
                .unwrap()
            else {
                panic!("real reason-partition signature")
            };
            let crl::CompleteEvidence::Facts(facts) = source
                .evaluate(path.members()[0].certificate(), &context)
                .unwrap()
            else {
                panic!("applicable original partition")
            };
            let absence = facts.absence().unwrap();
            assert!(!absence.reasons().is_complete());
            match &mut coverage {
                None => coverage = Some(crl::Coverage::new(absence, work).unwrap()),
                Some(coverage) => assert_eq!(
                    coverage.add(absence, work).unwrap(),
                    crl::CoverageAdmission::Added
                ),
            }
        }
        let coverage = coverage.unwrap();
        assert!(coverage.reasons().is_complete());
        assert_eq!(coverage.sources().len(), 2);
        assert_crl_reason_authorities(path, &coverage, &utc, work);
        let bad = [
            CrlAuthorities::Complete(CrlAuthority::Anchor),
            CrlAuthorities::Complete(CrlAuthority::Path(path)),
        ];
        assert!(matches!(
            path.bind_crl_coverage(0, &coverage, &bad, &utc, work)
                .unwrap(),
            CrlCoverageBinding::Rejected {
                source: 1,
                cause: CrlBindingFailure::Signer
            }
        ));
        let denied = Memory::new(Arc::new(MemoryAccount::new(0, 0)));
        assert!(matches!(
            path.bind_crl_coverage(0, &coverage, &bad, &utc, &denied),
            Err(CrlCoverageError::Source {
                source: 0,
                cause: CrlBindingError::Resource(_)
            })
        ));
        assert_eq!(verifier.calls.get(), 2);
    });
}

fn assert_crl_reason_authorities<'a>(
    path: &structured_ltv::pki::CheckedPath<'a>,
    coverage: &structured_pkix::crl::Coverage<'_, 'a>,
    utc: &structured_pkix::temporal::LeapTable<'_>,
    work: &Memory,
) {
    use structured_ltv::pki::{CrlAuthorities, CrlAuthority, CrlCoverageBinding, CrlCoverageError};
    let authorities = [CrlAuthorities::Complete(CrlAuthority::Anchor); 2];
    let before = work.used();
    let CrlCoverageBinding::Bound(bound) = path
        .bind_crl_coverage(0, coverage, &authorities, utc, work)
        .unwrap()
    else {
        panic!("every contributor uses actual common anchor")
    };
    assert!(core::ptr::eq(bound.path(), path));
    assert!(core::ptr::eq(bound.coverage(), coverage));
    assert_eq!(bound.authorities().as_ptr(), authorities.as_ptr());
    assert_eq!(bound.member(), 0);
    assert_eq!(work.used(), before);
    assert_crl_status_census(path, &bound, work);
    let before_work = work.work_used();
    assert!(matches!(
        path.bind_crl(0, &coverage.sources()[0], authorities[0], utc, work)
            .unwrap(),
        structured_ltv::pki::CrlBinding::Bound(_)
    ));
    let first_work = work.work_used() - before_work;
    let limited = Memory::new(Arc::new(MemoryAccount::new(1 << 20, first_work + 2)));
    assert!(matches!(
        path.bind_crl_coverage(0, coverage, &authorities, utc, &limited),
        Err(CrlCoverageError::Source {
            source: 1,
            cause: structured_ltv::pki::CrlBindingError::Resource(_)
        })
    ));
    assert_eq!(limited.used(), 0);
    assert!(matches!(
        path.bind_crl_coverage(0, coverage, &authorities[..1], utc, work),
        Err(CrlCoverageError::AuthorityCount)
    ));
}

fn assert_crl_status_census<'a>(
    path: &structured_ltv::pki::CheckedPath<'a>,
    coverage: &structured_ltv::pki::CrlCoverage<'_, '_, 'a>,
    work: &Memory,
) {
    // A valid all-reason source is still incomplete when omitted, supplied
    // twice, relabelled in time, or queried without current execution admission.
    use structured_ltv::pki::{MemberStatus, StatusCensusError};
    let at = coverage.coverage().evaluated_at();
    let entries = [MemberStatus::Crl(coverage)];
    let before = work.used();
    let census = path.status_census(at, &entries, work).unwrap();
    assert!(core::ptr::eq(census.path(), path));
    assert_eq!(census.evaluated_at(), at);
    assert_eq!(census.entries().as_ptr(), entries.as_ptr());
    assert_eq!(work.used(), before);
    assert!(matches!(
        path.status_census(at, &[], work),
        Err(StatusCensusError::Count)
    ));
    let twice = [MemberStatus::Crl(coverage), MemberStatus::Crl(coverage)];
    assert!(matches!(
        path.status_census(at, &twice, work),
        Err(StatusCensusError::Count)
    ));
    let later = structured_pkix::temporal::Instant::new(at.start().seconds() + 1, 0)
        .unwrap()
        .into();
    assert!(matches!(
        path.status_census(later, &entries, work),
        Err(StatusCensusError::Time { index: 0 })
    ));
    let denied = Memory::new(Arc::new(MemoryAccount::new(0, 0)));
    assert!(matches!(
        path.status_census(at, &entries, &denied),
        Err(StatusCensusError::Resource(_))
    ));
    assert_eq!(denied.used(), 0);
}