vcon-server 0.4.5

Stores vCons encrypted at rest, on local disk or in an S3 bucket (as objects or Parquet files), and returns them to authorized readers.
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
//! The server's configuration: one TOML file. A configuration error stops
//! the start and names its key, and an unknown key is an error.

use std::collections::BTreeSet;
use std::fmt;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::time::Duration;

use serde::Deserialize;

use crate::api::ConnectionLimits;
use crate::retention::Retention;
use crate::session::Origin;

/// The configuration `serve` runs with. Durations are written in whole
/// seconds and sizes in bytes; each must be more than zero, and each has a
/// default.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Config {
    /// The address and port `serve` listens on (`listen`).
    pub listen: SocketAddr,
    /// The file holding the server's PEM certificate chain (`tls.cert`).
    pub tls_cert: PathBuf,
    /// The file holding the server's PEM private key (`tls.key`).
    pub tls_key: PathBuf,
    /// The data directory (`data_dir`).
    pub data_dir: PathBuf,
    /// The file holding the 32-byte master key (`master_key`).
    pub master_key: PathBuf,
    /// For tenants that set none; 7 years unless configured.
    pub retention_default: Retention,
    /// Time between purge sweeps; [`PURGE_INTERVAL`] unless configured.
    pub purge_interval: Duration,
    /// Age of the oldest work waiting for key rotation before `serve`
    /// warns; [`ROTATION_WARN`] unless configured.
    pub rotation_warn: Duration,
    /// The largest request body.
    pub body_limit: usize,
    /// The largest size gzip inflation may reach while a body is inspected.
    pub inflated_limit: u64,
    /// How long a request body may take to arrive.
    pub read_timeout: Duration,
    /// How long a connection may take over its handshake and its request
    /// heads and how long it may wait between requests
    /// (`limits.timeouts.handshake`, `first_head`, `head` and `idle`), and
    /// how many a listener holds at once (`limits.connections`).
    pub connection: ConnectionLimits,
    /// Extension names accepted in `critical` and `must_support`.
    pub supported_critical: BTreeSet<String>,
    /// The MCP server's limits.
    pub mcp: crate::mcp::Limits,
    /// How many reports of a content-security-policy violation a listener
    /// takes in a minute, from every address and from one
    /// (`limits.csp_reports` and `limits.csp_reports_per_address`).
    pub reports: crate::api::ReportLimits,
    /// Where the data is kept.
    pub storage: Storage,
    /// How `export parquet` reaches a bucket: the `[export]` table.
    pub export: Export,
    /// Where the catalogs and the registry are kept: the `[catalog]` table.
    pub catalog: CatalogStore,
    /// The operator listener: the `[operator]` table. Without it, the one
    /// listener serves operator keys too.
    pub operator: Option<Operator>,
    /// The origin a browser reaches `listen` at (`public_origin`), which a
    /// request by session must come from. Without it, `listen` serves no
    /// session.
    pub public_origin: Option<Origin>,
    /// Whether a key may be made with no expiry (`keys.no_expiry`); true
    /// unless configured. Keys made earlier are not changed.
    pub keys_no_expiry: bool,
}

/// The operator listener: the `[operator]` table. With it, `serve` listens
/// on a second address that serves operator keys only, and `listen` serves
/// tenant keys only.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Operator {
    /// The address and port of the operator listener (`operator.listen`).
    pub listen: SocketAddr,
    /// Its certificate chain (`operator.tls.cert`), or `tls.cert` when the
    /// table has no `[operator.tls]`.
    pub tls_cert: PathBuf,
    /// Its private key (`operator.tls.key`), or `tls.key` when the table
    /// has no `[operator.tls]`.
    pub tls_key: PathBuf,
    /// The origin a browser reaches the operator listener at
    /// (`operator.public_origin`). Without it, that listener serves no
    /// session.
    pub public_origin: Option<Origin>,
}

/// Whether two listening addresses would take the same socket: the same
/// port, other than the port 0 that asks the system for a free one, on
/// the same address, or on addresses of one family of which one is the
/// unspecified address, which takes every address of the family.
#[must_use]
pub fn same_socket(a: SocketAddr, b: SocketAddr) -> bool {
    a.port() == b.port()
        && a.port() != 0
        && (a.ip() == b.ip()
            || (a.is_ipv4() == b.is_ipv4() && (a.ip().is_unspecified() || b.ip().is_unspecified())))
}

/// Where the catalogs and the registry are kept: `catalog.backend`.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum CatalogStore {
    /// SQLCipher files in `data_dir`.
    #[default]
    Local,
    /// A PostgreSQL database, reached over TLS: `[catalog.postgres]`.
    Postgres(Box<PostgresSettings>),
}

/// How to reach the PostgreSQL database: the `[catalog.postgres]` table.
/// TLS, with the server's certificate chain and host name verified, is the
/// only way it is reached.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PostgresSettings {
    /// The servers' host names or IP addresses, tried in order.
    pub hosts: Vec<String>,
    /// The TCP port of every host.
    pub port: u16,
    /// The database.
    pub dbname: String,
    /// The role the server logs in as.
    pub user: String,
    /// PEM certificates of the authorities trusted for the servers' TLS
    /// certificates, in place of the system's; None trusts the system's.
    pub ca_file: Option<PathBuf>,
    /// The file holding the role's password, for SCRAM with channel binding.
    pub password_file: Option<PathBuf>,
    /// The file holding the PEM client certificate chain.
    pub client_cert_file: Option<PathBuf>,
    /// The file holding the client certificate's PEM private key.
    pub client_key_file: Option<PathBuf>,
    /// The most connections open at once.
    pub pool: u32,
    /// How long a connection, or a wait for a free one, may take.
    pub connect_timeout: Duration,
    /// The longest a statement may run (PostgreSQL's `statement_timeout`).
    pub statement_timeout: Duration,
    /// The longest a statement may wait for a lock (`lock_timeout`).
    pub lock_timeout: Duration,
}

/// The `[export]` table.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Export {
    /// How an `s3://` target is reached (`[export.s3]`); None takes
    /// `[storage.s3]`'s service and keys.
    pub s3: Option<ExportS3>,
}

/// How to reach the S3-compatible service an export writes to: every key
/// of `[storage.s3]` but the bucket and prefix, which `--to` names, and
/// `allow_versioning`, which only the server's own storage reads.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExportS3 {
    /// The service's URL; None is AWS S3 for the region.
    pub endpoint: Option<String>,
    /// The region, used in request signing.
    pub region: String,
    /// The bucket in the path, not in the host name.
    pub path_style: bool,
    /// The access key id; None uses the AWS default credential chain.
    pub access_key_id: Option<String>,
    /// The file holding the secret access key.
    pub secret_access_key_file: Option<PathBuf>,
    /// Accept an `http://` endpoint.
    pub allow_http: bool,
    /// PEM certificates of the authorities trusted for the endpoint's TLS
    /// certificate, in place of the system's.
    pub ca_file: Option<PathBuf>,
    /// How long one request may take.
    pub timeout: Duration,
}

impl ExportS3 {
    /// The settings for `bucket` under `prefix` on this service.
    #[must_use]
    pub fn for_bucket(&self, bucket: &str, prefix: &str) -> S3Settings {
        S3Settings {
            endpoint: self.endpoint.clone(),
            region: self.region.clone(),
            bucket: bucket.to_owned(),
            prefix: prefix.to_owned(),
            path_style: self.path_style,
            access_key_id: self.access_key_id.clone(),
            secret_access_key_file: self.secret_access_key_file.clone(),
            allow_http: self.allow_http,
            ca_file: self.ca_file.clone(),
            allow_versioning: false,
            timeout: self.timeout,
        }
    }
}

impl From<&S3Settings> for ExportS3 {
    /// The service and keys of `[storage.s3]`.
    fn from(s: &S3Settings) -> ExportS3 {
        ExportS3 {
            endpoint: s.endpoint.clone(),
            region: s.region.clone(),
            path_style: s.path_style,
            access_key_id: s.access_key_id.clone(),
            secret_access_key_file: s.secret_access_key_file.clone(),
            allow_http: s.allow_http,
            ca_file: s.ca_file.clone(),
            timeout: s.timeout,
        }
    }
}

/// Why the storage the configuration names cannot be used: the message says
/// which setting or step.
#[derive(Debug, Clone, PartialEq, Eq)]
#[must_use]
pub struct StorageError(pub String);

impl fmt::Display for StorageError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl std::error::Error for StorageError {}

/// Where the server keeps its data: the `[storage]` table. A file without
/// it keeps everything in `data_dir`.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Storage {
    /// Where the sealed vCon objects are kept.
    pub objects: ObjectStore,
    /// Parquet storage, `[storage.parquet]`: the objects in the bucket are
    /// packed into Parquet files. None: they stay objects.
    pub parquet: Option<ParquetSettings>,
}

/// The `[storage.parquet]` table.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ParquetSettings {
    /// Time from the end of one pack run to the start of the next.
    pub pack_interval: Duration,
    /// The largest sealed object packed, in bytes; a larger one stays an
    /// object.
    pub pack_limit: u64,
    /// How long an older manifest, and a file a compaction stopped naming,
    /// stays for readers outside the server.
    pub reader_grace: Duration,
}

/// How long an older manifest, and a file a compaction stopped naming,
/// stays for readers outside the server unless configured: 15 minutes.
pub const READER_GRACE: Duration = Duration::from_secs(900);

/// Time between pack runs unless configured.
pub const PACK_INTERVAL: Duration = Duration::from_secs(300);

/// The largest sealed object packed unless configured: 16 MiB, the default
/// largest request body.
pub const PACK_LIMIT: u64 = 16 << 20;

/// Where the sealed vCon objects are kept: `storage.objects`.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum ObjectStore {
    /// Files under `data_dir`, beside each tenant's catalog.
    #[default]
    Local,
    /// An S3-compatible bucket.
    S3(S3Settings),
}

/// How to reach the bucket: the `[storage.s3]` table.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct S3Settings {
    /// The service's URL; None is AWS S3 for the region.
    pub endpoint: Option<String>,
    /// The region, used in request signing.
    pub region: String,
    /// The bucket, which must exist.
    pub bucket: String,
    /// Written before `tenants/` in every object's name.
    pub prefix: String,
    /// The bucket in the path, not in the host name.
    pub path_style: bool,
    /// The access key id; None uses the AWS default credential chain.
    pub access_key_id: Option<String>,
    /// The file holding the secret access key.
    pub secret_access_key_file: Option<PathBuf>,
    /// Accept an `http://` endpoint.
    pub allow_http: bool,
    /// PEM certificates of the authorities trusted for the endpoint's
    /// TLS certificate, in place of the system's; None trusts the system's.
    pub ca_file: Option<PathBuf>,
    /// Start even though the bucket has versioning on.
    pub allow_versioning: bool,
    /// How long one request may take.
    pub timeout: Duration,
}

/// Time between purge sweeps unless configured: a version is purged
/// within an hour of falling due.
pub const PURGE_INTERVAL: Duration = Duration::from_secs(3600);

/// Age of the oldest work waiting for key rotation before `serve` warns,
/// unless configured: 30 days.
pub const ROTATION_WARN: Duration = Duration::from_secs(30 * 86_400);

/// The largest request body unless configured: 16 MiB. The largest of
/// 34,885 vCon files measured (public datasets and recorded test calls) was
/// 5,141,813 bytes, a two-minute recording held inline; nearly all of their
/// recordings are referenced by URL instead.
pub const BODY_LIMIT: usize = 16 << 20;

/// The largest inflation unless configured: four times [`BODY_LIMIT`], so
/// any vCon accepted plain is also accepted gzipped, with room left for
/// gzipped payloads inside its signed layers.
pub const INFLATED_LIMIT: u64 = 4 * BODY_LIMIT as u64;

/// How long a request body may take to arrive unless configured: a body at
/// [`BODY_LIMIT`] needs about 2.2 Mbit/s.
pub const READ_TIMEOUT: Duration = Duration::from_secs(60);

/// A configuration that could not be read, naming the key at fault.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConfigError(pub String);

impl fmt::Display for ConfigError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl std::error::Error for ConfigError {}

/// Reads the configuration file at `path`.
///
/// # Errors
///
/// A [`ConfigError`] naming the file if it cannot be read, or any error
/// [`parse`] gives for its text.
pub fn load(path: &Path) -> Result<Config, ConfigError> {
    let text = std::fs::read_to_string(path)
        .map_err(|e| ConfigError(format!("{}: {e}", path.display())))?;
    parse(&text)
}

/// Reads a configuration from its TOML text.
///
/// # Errors
///
/// A [`ConfigError`] naming the key at fault: text that is not TOML, an
/// unknown key, a required key missing, or a value of the wrong type or
/// not more than zero.
pub fn parse(text: &str) -> Result<Config, ConfigError> {
    let raw: Raw = toml::from_str(text).map_err(|e| ConfigError(e.to_string()))?;
    let storage = storage(raw.storage)?;
    let catalog = catalog(raw.catalog, &storage)?;
    let retention_default = match raw.retention_default {
        None => Retention::Years(7),
        Some(text) => Retention::parse(&text).ok_or_else(|| {
            ConfigError(format!(
                "retention_default: {text:?} is not a period such as 7y, 18m or 90d"
            ))
        })?,
    };
    let tls = (raw.tls.cert, raw.tls.key);
    let public_origin = origin("public_origin", raw.public_origin)?;
    Ok(Config {
        listen: raw.listen,
        tls_cert: tls.0.clone(),
        tls_key: tls.1.clone(),
        data_dir: raw.data_dir,
        master_key: raw.master_key,
        retention_default,
        purge_interval: seconds("purge_interval", raw.purge_interval, PURGE_INTERVAL)?,
        rotation_warn: seconds("rotation_warn", raw.rotation_warn, ROTATION_WARN)?,
        body_limit: match raw.limits.body {
            None => BODY_LIMIT,
            Some(body) => usize::try_from(positive("limits.body", body)?)
                .map_err(|_| ConfigError("limits.body is too large".to_owned()))?,
        },
        inflated_limit: match raw.limits.inflated {
            None => INFLATED_LIMIT,
            Some(inflated) => positive("limits.inflated", inflated)?,
        },
        read_timeout: seconds(
            "limits.timeouts.read",
            raw.limits.timeouts.read,
            READ_TIMEOUT,
        )?,
        connection: connection_limits(&raw.limits)?,
        supported_critical: raw.supported_critical.into_iter().collect(),
        mcp: mcp_limits(&raw.mcp)?,
        reports: report_limits(&raw.limits)?,
        storage,
        export: Export {
            s3: raw.export.s3.map(export_s3).transpose()?,
        },
        catalog,
        operator: operator(raw.operator, raw.listen, &tls, public_origin.as_ref())?,
        public_origin,
        keys_no_expiry: raw.keys.no_expiry.unwrap_or(true),
    })
}

/// A listener's public origin, if `key` is written.
fn origin(key: &str, raw: Option<String>) -> Result<Option<Origin>, ConfigError> {
    raw.map(|text| {
        Origin::parse(&text).ok_or_else(|| {
            ConfigError(format!(
                "{key}: {text:?} is not an origin: write https://, a host in lower case and, \
                 unless it is 443, a port, with nothing after, such as https://vcon.example.com"
            ))
        })
    })
    .transpose()
}

/// The `[operator]` table, checked against the tenant listener.
fn operator(
    raw: Option<RawOperator>,
    listen: SocketAddr,
    tls: &(PathBuf, PathBuf),
    tenant_origin: Option<&Origin>,
) -> Result<Option<Operator>, ConfigError> {
    let Some(raw) = raw else {
        return Ok(None);
    };
    if same_socket(raw.listen, listen) {
        return Err(ConfigError(format!(
            "operator.listen: {} is the socket `listen` takes ({listen}); the operator listener \
             needs an address or port of its own",
            raw.listen
        )));
    }
    let (tls_cert, tls_key) = match raw.tls {
        Some(own) => (own.cert, own.key),
        None => tls.clone(),
    };
    let public_origin = origin("operator.public_origin", raw.public_origin)?;
    // A browser keeps a cookie by host and not by port, so two listeners
    // on one host would each be sent the other's session cookie.
    if let (Some(own), Some(tenant)) = (&public_origin, tenant_origin)
        && own.host() == tenant.host()
    {
        return Err(ConfigError(format!(
            "operator.public_origin: {own} has the host of `public_origin` ({tenant}); a browser \
             keeps a session cookie by host and not by port, so the operator listener needs a \
             host name of its own"
        )));
    }
    Ok(Some(Operator {
        listen: raw.listen,
        tls_cert,
        tls_key,
        public_origin,
    }))
}

/// The `[mcp]` table's limits, each key that is not written at its default.
fn mcp_limits(raw: &RawMcp) -> Result<crate::mcp::Limits, ConfigError> {
    let default = crate::mcp::Limits::DEFAULT;
    let narrow = |key: &str, value: u64| {
        u32::try_from(positive(key, value)?).map_err(|_| ConfigError(format!("{key} is too large")))
    };
    Ok(crate::mcp::Limits {
        calls_per_second: match raw.calls_per_second {
            None => default.calls_per_second,
            Some(value) => narrow("mcp.calls_per_second", value)?,
        },
        concurrent: match raw.concurrent {
            None => default.concurrent,
            Some(value) => narrow("mcp.concurrent", value)?,
        },
        response_bytes: match raw.response_bytes {
            None => default.response_bytes,
            Some(value) => usize::try_from(positive("mcp.response_bytes", value)?)
                .map_err(|_| ConfigError("mcp.response_bytes is too large".to_owned()))?,
        },
    })
}

/// How long one S3 request may take unless configured.
pub const S3_TIMEOUT: Duration = Duration::from_secs(30);

/// The `[storage]` table checked: an S3 table only with `objects = "s3"`,
/// an `http://` endpoint only when allowed, and an access key id only with
/// the file of its secret.
fn storage(raw: RawStorage) -> Result<Storage, ConfigError> {
    let objects = match (raw.objects.as_deref(), raw.s3) {
        (None | Some("local"), None) => ObjectStore::Local,
        (None | Some("local"), Some(_)) => {
            return Err(ConfigError(
                "storage.s3 is set but storage.objects is not \"s3\"".to_owned(),
            ));
        }
        (Some("s3"), None) => {
            return Err(ConfigError(
                "storage.objects is \"s3\" but there is no storage.s3 table".to_owned(),
            ));
        }
        (Some("s3"), Some(s3)) => ObjectStore::S3(s3_settings(s3)?),
        (Some(other), _) => {
            return Err(ConfigError(format!(
                "storage.objects: {other:?} is not \"local\" or \"s3\""
            )));
        }
    };
    let parquet = match (&objects, raw.parquet) {
        (_, None) => None,
        (ObjectStore::S3(_), Some(p)) => Some(ParquetSettings {
            pack_interval: seconds(
                "storage.parquet.pack_interval",
                p.pack_interval,
                PACK_INTERVAL,
            )?,
            pack_limit: match p.pack_limit {
                None => PACK_LIMIT,
                Some(limit) => positive("storage.parquet.pack_limit", limit)?,
            },
            reader_grace: seconds("storage.parquet.reader_grace", p.reader_grace, READER_GRACE)?,
        }),
        (ObjectStore::Local, Some(_)) => {
            return Err(ConfigError(
                "storage.parquet packs the objects of a bucket, so it needs \
                 storage.objects = \"s3\""
                    .to_owned(),
            ));
        }
    };
    Ok(Storage { objects, parquet })
}

/// The PostgreSQL port unless configured.
pub const POSTGRES_PORT: u16 = 5432;

/// The most connections open at once unless configured.
pub const POSTGRES_POOL: u32 = 16;

/// How long a connection, or a wait for a free one, may take unless
/// configured.
pub const POSTGRES_CONNECT_TIMEOUT: Duration = Duration::from_secs(5);

/// The longest a statement may run unless configured.
pub const POSTGRES_STATEMENT_TIMEOUT: Duration = Duration::from_secs(30);

/// The longest a statement may wait for a lock unless configured.
pub const POSTGRES_LOCK_TIMEOUT: Duration = Duration::from_secs(10);

/// The longest `statement_timeout` and `lock_timeout`, in whole seconds:
/// PostgreSQL holds each as milliseconds in a 32-bit integer.
pub const POSTGRES_TIMEOUT_LIMIT: u64 = i32::MAX as u64 / 1000;

/// The longest database or role name: PostgreSQL cuts a longer one to this
/// many bytes (NAMEDATALEN - 1), which would name another database or role.
pub const POSTGRES_NAME_LIMIT: usize = 63;

/// The `[catalog]` table checked: a `[catalog.postgres]` table only with
/// `backend = "postgres"`, and that only with the objects in a bucket.
fn catalog(raw: RawCatalog, storage: &Storage) -> Result<CatalogStore, ConfigError> {
    match (raw.backend.as_deref(), raw.postgres) {
        (None | Some("local"), None) => Ok(CatalogStore::Local),
        (None | Some("local"), Some(_)) => Err(ConfigError(
            "catalog.postgres is set but catalog.backend is not \"postgres\"".to_owned(),
        )),
        (Some("postgres"), None) => Err(ConfigError(
            "catalog.backend is \"postgres\" but there is no catalog.postgres table".to_owned(),
        )),
        (Some("postgres"), Some(pg)) => {
            // A shared catalog with objects in one host's data directory
            // would name objects the other hosts cannot read, and recovery
            // on one host would take the others' rows as naming missing
            // objects.
            if !matches!(storage.objects, ObjectStore::S3(_)) {
                return Err(ConfigError(
                    "catalog.backend = \"postgres\" needs storage.objects = \"s3\": a catalog \
                     in PostgreSQL names objects that every server must be able to read"
                        .to_owned(),
                ));
            }
            postgres_settings(pg).map(|pg| CatalogStore::Postgres(Box::new(pg)))
        }
        (Some(other), _) => Err(ConfigError(format!(
            "catalog.backend: {other:?} is not \"local\" or \"postgres\""
        ))),
    }
}

fn postgres_settings(raw: RawPostgres) -> Result<PostgresSettings, ConfigError> {
    const KEY: &str = "catalog.postgres";
    if raw.hosts.is_empty() {
        return Err(ConfigError(format!(
            "{KEY}.hosts is empty; name at least one server"
        )));
    }
    let mut seen = BTreeSet::new();
    for host in &raw.hosts {
        check_host(host).map_err(|why| ConfigError(format!("{KEY}.hosts: {host:?} {why}")))?;
        if !seen.insert(host.to_ascii_lowercase()) {
            return Err(ConfigError(format!("{KEY}.hosts names {host:?} twice")));
        }
    }
    let port = match raw.port {
        None => POSTGRES_PORT,
        Some(port) => u16::try_from(positive(&format!("{KEY}.port"), port)?).map_err(|_| {
            ConfigError(format!("{KEY}.port {port} is not a TCP port (1 to 65535)"))
        })?,
    };
    check_name("dbname", &raw.dbname)?;
    check_name("user", &raw.user)?;
    for (name, path) in [
        ("ca_file", &raw.ca_file),
        ("password_file", &raw.password_file),
        ("client_cert_file", &raw.client_cert_file),
        ("client_key_file", &raw.client_key_file),
    ] {
        if path.as_ref().is_some_and(|p| p.as_os_str().is_empty()) {
            return Err(ConfigError(format!("{KEY}.{name} is empty")));
        }
    }
    match (&raw.client_cert_file, &raw.client_key_file) {
        (Some(_), None) => {
            return Err(ConfigError(format!(
                "{KEY}.client_cert_file is set without {KEY}.client_key_file"
            )));
        }
        (None, Some(_)) => {
            return Err(ConfigError(format!(
                "{KEY}.client_key_file is set without {KEY}.client_cert_file"
            )));
        }
        _ => {}
    }
    if raw.password_file.is_none() && raw.client_cert_file.is_none() {
        return Err(ConfigError(format!(
            "set {KEY}.password_file, {KEY}.client_cert_file, or both: a server that asks for \
             neither is not accepted"
        )));
    }
    let pool = match raw.pool {
        None => POSTGRES_POOL,
        Some(pool) => u32::try_from(positive(&format!("{KEY}.pool"), pool)?)
            .map_err(|_| ConfigError(format!("{KEY}.pool is too large")))?,
    };
    let limited = |name: &str, value: Option<u64>, default: Duration| {
        let key = format!("{KEY}.{name}");
        let value = seconds(&key, value, default)?;
        if value.as_secs() > POSTGRES_TIMEOUT_LIMIT {
            return Err(ConfigError(format!(
                "{key} is more than {POSTGRES_TIMEOUT_LIMIT} seconds, the longest PostgreSQL holds"
            )));
        }
        Ok(value)
    };
    Ok(PostgresSettings {
        hosts: raw.hosts,
        port,
        dbname: raw.dbname,
        user: raw.user,
        ca_file: raw.ca_file,
        password_file: raw.password_file,
        client_cert_file: raw.client_cert_file,
        client_key_file: raw.client_key_file,
        pool,
        connect_timeout: seconds(
            &format!("{KEY}.connect_timeout"),
            raw.connect_timeout,
            POSTGRES_CONNECT_TIMEOUT,
        )?,
        statement_timeout: limited(
            "statement_timeout",
            raw.statement_timeout,
            POSTGRES_STATEMENT_TIMEOUT,
        )?,
        lock_timeout: limited("lock_timeout", raw.lock_timeout, POSTGRES_LOCK_TIMEOUT)?,
    })
}

/// A database or role name PostgreSQL takes as written: not empty, no NUL,
/// and no longer than [`POSTGRES_NAME_LIMIT`] bytes.
fn check_name(key: &str, name: &str) -> Result<(), ConfigError> {
    let refused = |why: &str| Err(ConfigError(format!("catalog.postgres.{key} {why}")));
    if name.is_empty() {
        return refused("is empty");
    }
    if name.contains('\0') {
        return refused("holds a NUL character, which PostgreSQL names cannot");
    }
    if name.len() > POSTGRES_NAME_LIMIT {
        return refused(&format!(
            "is {} bytes; PostgreSQL would cut it to {POSTGRES_NAME_LIMIT} bytes and name \
             another database or role",
            name.len()
        ));
    }
    Ok(())
}

/// A host the catalog may connect to: an IP address, or a DNS name in
/// ASCII (RFC 1123 labels). The reason a host is refused, after its name.
fn check_host(host: &str) -> Result<(), String> {
    if host.parse::<std::net::IpAddr>().is_ok() {
        return Ok(());
    }
    if host.starts_with('/') {
        return Err(
            "is a Unix socket, which carries no TLS; the catalog is reached over TCP \
                    with TLS only"
                .to_owned(),
        );
    }
    if let Some(bare) = host.strip_prefix('[').and_then(|h| h.strip_suffix(']'))
        && bare.parse::<std::net::Ipv6Addr>().is_ok()
    {
        return Err(format!(
            "is written in brackets; write the address as {bare:?}"
        ));
    }
    if host.is_empty() {
        return Err("is empty".to_owned());
    }
    if !host.is_ascii() {
        return Err("is not ASCII; write an internationalized name in its xn-- form".to_owned());
    }
    if host.contains(':') {
        return Err(
            "holds a colon; give the port in catalog.postgres.port, which every host uses"
                .to_owned(),
        );
    }
    if host.len() > 253 {
        return Err("is longer than 253 characters, the longest DNS name".to_owned());
    }
    let good_label = |label: &str| {
        (1..=63).contains(&label.len())
            && !label.starts_with('-')
            && !label.ends_with('-')
            && label
                .bytes()
                .all(|b| b.is_ascii_alphanumeric() || b == b'-')
    };
    if !host.split('.').all(good_label) {
        return Err(
            "is not a DNS name: labels of 1 to 63 letters, digits and hyphens, joined by dots"
                .to_owned(),
        );
    }
    Ok(())
}

/// `[export.s3]` checked as `[storage.s3]` is, but for its bucket.
fn export_s3(raw: RawExportS3) -> Result<ExportS3, ConfigError> {
    check_service(
        "export.s3",
        raw.endpoint.as_deref(),
        raw.allow_http,
        raw.ca_file.is_some(),
        raw.access_key_id.is_some(),
        raw.secret_access_key_file.is_some(),
    )?;
    Ok(ExportS3 {
        endpoint: raw.endpoint,
        region: raw.region,
        path_style: raw.path_style,
        access_key_id: raw.access_key_id,
        secret_access_key_file: raw.secret_access_key_file,
        allow_http: raw.allow_http,
        ca_file: raw.ca_file,
        timeout: seconds("export.s3.timeout", raw.timeout, S3_TIMEOUT)?,
    })
}

/// The rules for how a service is reached, the same for every table that
/// names one: an `http://` endpoint only when allowed and never with a CA
/// file, any other endpoint `https://`, and an access key id only with the
/// file of its secret.
fn check_service(
    table: &str,
    endpoint: Option<&str>,
    allow_http: bool,
    ca_file: bool,
    access_key_id: bool,
    secret_access_key_file: bool,
) -> Result<(), ConfigError> {
    if let Some(endpoint) = endpoint {
        if endpoint.starts_with("http://") {
            if ca_file {
                return Err(ConfigError(format!(
                    "{table}.ca_file is set, but {table}.endpoint {endpoint:?} is plain \
                     HTTP, which uses no certificate"
                )));
            }
            if !allow_http {
                return Err(ConfigError(format!(
                    "{table}.endpoint {endpoint:?} is plain HTTP; set {table}.allow_http = \
                     true only for a test service on this host"
                )));
            }
        } else if !endpoint.starts_with("https://") {
            return Err(ConfigError(format!(
                "{table}.endpoint {endpoint:?} is not an https:// URL"
            )));
        }
    }
    match (access_key_id, secret_access_key_file) {
        (true, false) => Err(ConfigError(format!(
            "{table}.access_key_id is set without {table}.secret_access_key_file"
        ))),
        (false, true) => Err(ConfigError(format!(
            "{table}.secret_access_key_file is set without {table}.access_key_id"
        ))),
        _ => Ok(()),
    }
}

fn s3_settings(raw: RawS3) -> Result<S3Settings, ConfigError> {
    if raw.bucket.is_empty() {
        return Err(ConfigError("storage.s3.bucket is empty".to_owned()));
    }
    if !raw.prefix.is_empty() && (raw.prefix.starts_with('/') || !raw.prefix.ends_with('/')) {
        return Err(ConfigError(format!(
            "storage.s3.prefix {:?} must not start with / and must end with /, \
             such as \"server-a/\"",
            raw.prefix
        )));
    }
    check_service(
        "storage.s3",
        raw.endpoint.as_deref(),
        raw.allow_http,
        raw.ca_file.is_some(),
        raw.access_key_id.is_some(),
        raw.secret_access_key_file.is_some(),
    )?;
    Ok(S3Settings {
        endpoint: raw.endpoint,
        region: raw.region,
        bucket: raw.bucket,
        prefix: raw.prefix,
        path_style: raw.path_style,
        access_key_id: raw.access_key_id,
        secret_access_key_file: raw.secret_access_key_file,
        allow_http: raw.allow_http,
        ca_file: raw.ca_file,
        allow_versioning: raw.allow_versioning,
        timeout: seconds("storage.s3.timeout", raw.timeout, S3_TIMEOUT)?,
    })
}

fn positive(key: &str, value: u64) -> Result<u64, ConfigError> {
    if value == 0 {
        return Err(ConfigError(format!("{key} must be more than 0")));
    }
    Ok(value)
}

/// The most seconds a connection limit may be set to: an hour. A limit
/// meant to close a stalled connection that waits longer than this closes
/// none in practice.
pub const CONNECTION_LIMIT_MAX: u64 = 3600;

/// The most reports a minute `limits.csp_reports` and
/// `limits.csp_reports_per_address` may be set to. Each report taken costs
/// one write to the registry, so a larger number would bound nothing.
pub const CSP_REPORTS_MAX: u64 = 6000;

/// How many reports of a content-security-policy violation a listener
/// takes in a minute (`limits.csp_reports`) and from one address
/// (`limits.csp_reports_per_address`), or the defaults for those not
/// written. More from one address than from all is refused: the second
/// number could never be reached.
fn report_limits(limits: &RawLimits) -> Result<crate::api::ReportLimits, ConfigError> {
    let default = crate::api::ReportLimits::DEFAULT;
    let read = |key: &str, value: Option<u64>, default: u32| -> Result<u32, ConfigError> {
        let Some(value) = value else {
            return Ok(default);
        };
        let value = positive(key, value)?;
        u32::try_from(value)
            .ok()
            .filter(|_| value <= CSP_REPORTS_MAX)
            .ok_or_else(|| {
                ConfigError(format!(
                    "{key} must be at most {CSP_REPORTS_MAX}, and is {value}"
                ))
            })
    };
    let per_minute = read("limits.csp_reports", limits.csp_reports, default.per_minute)?;
    // A minute's number set under the default for one address lowers that
    // default with it, so that setting one key alone is never refused.
    let per_address = read(
        "limits.csp_reports_per_address",
        limits.csp_reports_per_address,
        default.per_address.min(per_minute),
    )?;
    if per_address > per_minute {
        return Err(ConfigError(format!(
            "limits.csp_reports_per_address ({per_address}) must be at most limits.csp_reports ({per_minute})"
        )));
    }
    Ok(crate::api::ReportLimits {
        per_minute,
        per_address,
    })
}

/// The most connections `limits.connections` may be set to: a bound that
/// keeps a mistyped number from asking for millions of file descriptors.
pub const CONNECTIONS_MAX: u64 = 65_536;

/// The most connections a listener holds at once (`limits.connections`),
/// or `default` when it is not written.
fn connections(value: Option<u64>, default: usize) -> Result<usize, ConfigError> {
    let Some(value) = value else {
        return Ok(default);
    };
    let value = positive("limits.connections", value)?;
    usize::try_from(value)
        .ok()
        .filter(|_| value <= CONNECTIONS_MAX)
        .ok_or_else(|| {
            ConfigError(format!(
                "limits.connections must be at most {CONNECTIONS_MAX}, and is {value}"
            ))
        })
}

/// The `[limits]` keys that bound a connection and the number of them,
/// each that is not written at its default.
fn connection_limits(limits: &RawLimits) -> Result<ConnectionLimits, ConfigError> {
    let raw = &limits.timeouts;
    let default = ConnectionLimits::default();
    let limit = |key: &str, value: Option<u64>, default: Duration| {
        let limit = seconds(key, value, default)?;
        if limit.as_secs() > CONNECTION_LIMIT_MAX {
            return Err(ConfigError(format!(
                "{key} must be at most {CONNECTION_LIMIT_MAX} seconds, and is {}",
                limit.as_secs()
            )));
        }
        Ok(limit)
    };
    Ok(ConnectionLimits {
        handshake: limit(
            "limits.timeouts.handshake",
            raw.handshake,
            default.handshake,
        )?,
        first_head: limit(
            "limits.timeouts.first_head",
            raw.first_head,
            default.first_head,
        )?,
        head: limit("limits.timeouts.head", raw.head, default.head)?,
        idle: limit("limits.timeouts.idle", raw.idle, default.idle)?,
        goodbye: default.goodbye,
        accept_pause: default.accept_pause,
        max: connections(limits.connections, default.max)?,
    })
}

/// `value` in whole seconds, or `default` when it is not written.
fn seconds(key: &str, value: Option<u64>, default: Duration) -> Result<Duration, ConfigError> {
    match value {
        None => Ok(default),
        Some(value) => positive(key, value).map(Duration::from_secs),
    }
}

/// The file as written, before its values are checked.
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Raw {
    listen: SocketAddr,
    tls: RawTls,
    data_dir: PathBuf,
    master_key: PathBuf,
    retention_default: Option<String>,
    purge_interval: Option<u64>,
    rotation_warn: Option<u64>,
    #[serde(default)]
    limits: RawLimits,
    #[serde(default)]
    supported_critical: Vec<String>,
    #[serde(default)]
    mcp: RawMcp,
    #[serde(default)]
    storage: RawStorage,
    #[serde(default)]
    export: RawExport,
    #[serde(default)]
    catalog: RawCatalog,
    operator: Option<RawOperator>,
    public_origin: Option<String>,
    #[serde(default)]
    keys: RawKeys,
}

/// The `[keys]` table: the rules for new access keys.
#[derive(Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawKeys {
    no_expiry: Option<bool>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RawOperator {
    listen: SocketAddr,
    tls: Option<RawTls>,
    public_origin: Option<String>,
}

#[derive(Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawCatalog {
    backend: Option<String>,
    postgres: Option<RawPostgres>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RawPostgres {
    hosts: Vec<String>,
    port: Option<u64>,
    dbname: String,
    user: String,
    ca_file: Option<PathBuf>,
    password_file: Option<PathBuf>,
    client_cert_file: Option<PathBuf>,
    client_key_file: Option<PathBuf>,
    pool: Option<u64>,
    connect_timeout: Option<u64>,
    statement_timeout: Option<u64>,
    lock_timeout: Option<u64>,
}

#[derive(Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawExport {
    s3: Option<RawExportS3>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RawExportS3 {
    endpoint: Option<String>,
    region: String,
    #[serde(default)]
    path_style: bool,
    access_key_id: Option<String>,
    secret_access_key_file: Option<PathBuf>,
    #[serde(default)]
    allow_http: bool,
    ca_file: Option<PathBuf>,
    timeout: Option<u64>,
}

#[derive(Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawStorage {
    objects: Option<String>,
    s3: Option<RawS3>,
    parquet: Option<RawParquet>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RawParquet {
    pack_interval: Option<u64>,
    pack_limit: Option<u64>,
    reader_grace: Option<u64>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RawS3 {
    endpoint: Option<String>,
    region: String,
    bucket: String,
    #[serde(default)]
    prefix: String,
    #[serde(default)]
    path_style: bool,
    access_key_id: Option<String>,
    secret_access_key_file: Option<PathBuf>,
    #[serde(default)]
    allow_http: bool,
    ca_file: Option<PathBuf>,
    #[serde(default)]
    allow_versioning: bool,
    timeout: Option<u64>,
}

#[derive(Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawMcp {
    calls_per_second: Option<u64>,
    concurrent: Option<u64>,
    response_bytes: Option<u64>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RawTls {
    cert: PathBuf,
    key: PathBuf,
}

#[derive(Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawLimits {
    body: Option<u64>,
    inflated: Option<u64>,
    connections: Option<u64>,
    csp_reports: Option<u64>,
    csp_reports_per_address: Option<u64>,
    #[serde(default)]
    timeouts: RawTimeouts,
}

#[derive(Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawTimeouts {
    read: Option<u64>,
    handshake: Option<u64>,
    first_head: Option<u64>,
    head: Option<u64>,
    idle: Option<u64>,
}

#[cfg(test)]
mod tests {
    use super::*;

    const FULL: &str = r#"
listen = "127.0.0.1:8443"
data_dir = "/var/lib/vcon-server"
master_key = "/etc/vcon-server/master.key"
retention_default = "18m"
purge_interval = 1800
rotation_warn = 604800
supported_critical = ["known", "other"]
public_origin = "https://vcon.example.com"

[tls]
cert = "/etc/vcon-server/cert.pem"
key = "/etc/vcon-server/key.pem"

[operator]
listen = "127.0.0.1:8444"
public_origin = "https://ops.example.com:8444"

[operator.tls]
cert = "/etc/vcon-server/operator-cert.pem"
key = "/etc/vcon-server/operator-key.pem"

[keys]
no_expiry = false

[limits]
body = 10485760
inflated = 104857600
connections = 64
csp_reports = 300
csp_reports_per_address = 30

[limits.timeouts]
read = 30
handshake = 5
first_head = 15
head = 20
idle = 90

[mcp]
calls_per_second = 20
concurrent = 8
response_bytes = 262144

[storage]
objects = "s3"

[storage.s3]
endpoint = "https://objects.example.com:9000"
region = "eu-west-1"
bucket = "vcon"
prefix = "server-a/"
path_style = true
access_key_id = "vcon-server"
secret_access_key_file = "/etc/vcon-server/s3.secret"
allow_http = true
ca_file = "/etc/vcon-server/s3-ca.pem"
allow_versioning = true
timeout = 60

[storage.parquet]
pack_interval = 600
pack_limit = 8388608
reader_grace = 1800

[export.s3]
endpoint = "https://analytics.example.com"
region = "eu-west-2"
path_style = true
access_key_id = "vcon-export"
secret_access_key_file = "/etc/vcon-server/export.secret"
allow_http = true
ca_file = "/etc/vcon-server/export-ca.pem"
timeout = 120

[catalog]
backend = "postgres"

[catalog.postgres]
hosts = ["db1.example.com", "192.0.2.10", "2001:db8::10"]
port = 6432
dbname = "vcon"
user = "vcon_app"
ca_file = "/etc/vcon-server/pg-ca.pem"
password_file = "/etc/vcon-server/postgres.password"
client_cert_file = "/etc/vcon-server/pg-client.pem"
client_key_file = "/etc/vcon-server/pg-client.key"
pool = 8
connect_timeout = 10
statement_timeout = 60
lock_timeout = 20
"#;

    #[test]
    fn a_full_configuration_is_read() {
        assert_eq!(
            parse(FULL),
            Ok(Config {
                listen: "127.0.0.1:8443".parse().expect("address"),
                tls_cert: "/etc/vcon-server/cert.pem".into(),
                tls_key: "/etc/vcon-server/key.pem".into(),
                data_dir: "/var/lib/vcon-server".into(),
                master_key: "/etc/vcon-server/master.key".into(),
                retention_default: Retention::Months(18),
                purge_interval: Duration::from_secs(1800),
                rotation_warn: Duration::from_secs(604_800),
                body_limit: 10_485_760,
                inflated_limit: 104_857_600,
                read_timeout: Duration::from_secs(30),
                connection: ConnectionLimits {
                    handshake: Duration::from_secs(5),
                    first_head: Duration::from_secs(15),
                    head: Duration::from_secs(20),
                    idle: Duration::from_secs(90),
                    goodbye: Duration::from_secs(5),
                    accept_pause: Duration::from_secs(1),
                    max: 64,
                },
                supported_critical: ["known", "other"].map(String::from).into(),
                mcp: crate::mcp::Limits {
                    calls_per_second: 20,
                    concurrent: 8,
                    response_bytes: 262_144,
                },
                reports: crate::api::ReportLimits {
                    per_minute: 300,
                    per_address: 30,
                },
                storage: Storage {
                    objects: ObjectStore::S3(S3Settings {
                        endpoint: Some("https://objects.example.com:9000".to_owned()),
                        region: "eu-west-1".to_owned(),
                        bucket: "vcon".to_owned(),
                        prefix: "server-a/".to_owned(),
                        path_style: true,
                        access_key_id: Some("vcon-server".to_owned()),
                        secret_access_key_file: Some("/etc/vcon-server/s3.secret".into()),
                        allow_http: true,
                        ca_file: Some("/etc/vcon-server/s3-ca.pem".into()),
                        allow_versioning: true,
                        timeout: Duration::from_secs(60),
                    }),
                    parquet: Some(ParquetSettings {
                        pack_interval: Duration::from_secs(600),
                        pack_limit: 8_388_608,
                        reader_grace: Duration::from_secs(1800),
                    }),
                },
                export: Export {
                    s3: Some(ExportS3 {
                        endpoint: Some("https://analytics.example.com".to_owned()),
                        region: "eu-west-2".to_owned(),
                        path_style: true,
                        access_key_id: Some("vcon-export".to_owned()),
                        secret_access_key_file: Some("/etc/vcon-server/export.secret".into()),
                        allow_http: true,
                        ca_file: Some("/etc/vcon-server/export-ca.pem".into()),
                        timeout: Duration::from_secs(120),
                    }),
                },
                catalog: CatalogStore::Postgres(Box::new(PostgresSettings {
                    hosts: ["db1.example.com", "192.0.2.10", "2001:db8::10"]
                        .map(String::from)
                        .into(),
                    port: 6432,
                    dbname: "vcon".to_owned(),
                    user: "vcon_app".to_owned(),
                    ca_file: Some("/etc/vcon-server/pg-ca.pem".into()),
                    password_file: Some("/etc/vcon-server/postgres.password".into()),
                    client_cert_file: Some("/etc/vcon-server/pg-client.pem".into()),
                    client_key_file: Some("/etc/vcon-server/pg-client.key".into()),
                    pool: 8,
                    connect_timeout: Duration::from_secs(10),
                    statement_timeout: Duration::from_secs(60),
                    lock_timeout: Duration::from_secs(20),
                })),
                operator: Some(Operator {
                    listen: "127.0.0.1:8444".parse().expect("address"),
                    tls_cert: "/etc/vcon-server/operator-cert.pem".into(),
                    tls_key: "/etc/vcon-server/operator-key.pem".into(),
                    public_origin: Origin::parse("https://ops.example.com:8444"),
                }),
                public_origin: Origin::parse("https://vcon.example.com"),
                keys_no_expiry: false,
            }),
            "every key the file writes is read into its field"
        );
    }

    mod operator_listener {
        use super::*;

        /// The least configuration, with an `[operator]` table made of `table`.
        fn with(table: &str) -> String {
            format!("{LEAST}\n{table}")
        }

        fn address(text: &str) -> SocketAddr {
            text.parse().expect("an address")
        }

        #[test]
        fn without_the_table_there_is_no_operator_listener() {
            assert_eq!(
                parse(LEAST).expect("parses").operator,
                None,
                "one listener, as before the table existed"
            );
        }

        #[test]
        fn the_table_with_its_address_alone_takes_the_servers_certificate_and_key() {
            let config = parse(&with("[operator]\nlisten = \"127.0.0.1:8444\"\n")).expect("parses");
            assert_eq!(
                config.operator,
                Some(Operator {
                    listen: address("127.0.0.1:8444"),
                    tls_cert: "/etc/vcon-server/cert.pem".into(),
                    tls_key: "/etc/vcon-server/key.pem".into(),
                    public_origin: None,
                }),
                "tls.cert and tls.key serve both listeners"
            );
        }

        #[test]
        fn the_table_may_name_a_certificate_and_key_of_its_own() {
            let config = parse(&with(
                "[operator]\nlisten = \"127.0.0.1:8444\"\n\n[operator.tls]\n\
                 cert = \"/etc/vcon-server/ops.pem\"\nkey = \"/etc/vcon-server/ops.key\"\n",
            ))
            .expect("parses");
            assert_eq!(
                config.operator,
                Some(Operator {
                    listen: address("127.0.0.1:8444"),
                    tls_cert: "/etc/vcon-server/ops.pem".into(),
                    tls_key: "/etc/vcon-server/ops.key".into(),
                    public_origin: None,
                }),
                "the operator listener's own pair"
            );
            assert_eq!(
                config.tls_cert,
                PathBuf::from("/etc/vcon-server/cert.pem"),
                "the tenant listener keeps its own"
            );
        }

        #[test]
        fn the_table_leaves_every_other_key_as_it_was() {
            let without = parse(LEAST).expect("parses");
            let with = parse(&with("[operator]\nlisten = \"127.0.0.1:8444\"\n")).expect("parses");
            assert_eq!(
                Config {
                    operator: None,
                    ..with
                },
                without,
                "only the operator member differs"
            );
        }

        #[test]
        fn the_operator_listener_may_be_on_any_address_the_operator_chooses() {
            for listen in [
                "127.0.0.1:8444",
                "[::1]:8444",
                "192.0.2.10:8444",
                "[2001:db8::10]:8444",
                "0.0.0.0:8444",
                "127.0.0.1:0",
            ] {
                let config =
                    parse(&with(&format!("[operator]\nlisten = \"{listen}\"\n"))).expect(listen);
                assert_eq!(
                    config.operator.expect("an operator listener").listen,
                    address(listen),
                    "{listen}"
                );
            }
        }

        #[test]
        fn the_same_port_on_another_address_is_another_socket() {
            for (tenant, operator) in [
                ("192.0.2.10:8443", "127.0.0.1:8443"),
                ("127.0.0.1:8443", "[::1]:8443"),
                ("0.0.0.0:8443", "[::1]:8443"),
            ] {
                let text = with(&format!("[operator]\nlisten = \"{operator}\"\n")).replace(
                    "127.0.0.1:8443\"\ndata_dir",
                    &format!("{tenant}\"\ndata_dir"),
                );
                assert!(
                    parse(&text).is_ok(),
                    "{tenant} and {operator}: {:?}",
                    parse(&text)
                );
            }
        }

        #[test]
        fn port_zero_twice_is_two_sockets() {
            let text = with("[operator]\nlisten = \"127.0.0.1:0\"\n")
                .replace("127.0.0.1:8443", "127.0.0.1:0");
            let config = parse(&text).expect("parses");
            assert_eq!(config.listen, address("127.0.0.1:0"), "the tenant listener");
            assert_eq!(
                config.operator.expect("an operator listener").listen,
                address("127.0.0.1:0"),
                "the operator listener"
            );
        }

        #[test]
        fn the_table_without_its_address_is_refused_by_name() {
            for table in [
                "[operator]\n",
                "[operator.tls]\ncert = \"a.pem\"\nkey = \"a.key\"\n",
            ] {
                let error = parse(&with(table)).expect_err("no address");
                assert!(error.0.contains("`listen`"), "{table}: {error}");
            }
        }

        #[test]
        fn the_tenant_listeners_own_socket_is_refused() {
            let error =
                parse(&with("[operator]\nlisten = \"127.0.0.1:8443\"\n")).expect_err("one socket");
            assert_eq!(
                error.0,
                "operator.listen: 127.0.0.1:8443 is the socket `listen` takes (127.0.0.1:8443); \
                 the operator listener needs an address or port of its own",
                "the refusal names both keys and both addresses"
            );
        }

        #[test]
        fn a_socket_the_tenant_listener_covers_is_refused() {
            for (tenant, operator) in [
                ("0.0.0.0:8443", "127.0.0.1:8443"),
                ("127.0.0.1:8443", "0.0.0.0:8443"),
                ("[::]:8443", "[::1]:8443"),
                ("[2001:db8::10]:8443", "[::]:8443"),
            ] {
                let text = with(&format!("[operator]\nlisten = \"{operator}\"\n")).replace(
                    "127.0.0.1:8443\"\ndata_dir",
                    &format!("{tenant}\"\ndata_dir"),
                );
                let error = parse(&text).expect_err("one socket");
                assert!(
                    error.0.starts_with("operator.listen: "),
                    "{tenant} and {operator}: {error}"
                );
            }
        }

        #[test]
        fn an_address_that_is_not_one_is_refused_by_name() {
            for listen in ["localhost:8444", "127.0.0.1", "8444", "", "127.0.0.1:99999"] {
                let error = parse(&with(&format!("[operator]\nlisten = \"{listen}\"\n")))
                    .expect_err(listen);
                assert!(error.0.contains("listen"), "{listen}: {error}");
            }
        }

        #[test]
        fn an_address_of_the_wrong_type_is_refused() {
            for value in ["8444", "true", "[\"127.0.0.1:8444\"]"] {
                let error =
                    parse(&with(&format!("[operator]\nlisten = {value}\n"))).expect_err(value);
                assert!(error.0.contains("listen"), "{value}: {error}");
            }
        }

        #[test]
        fn an_unknown_key_in_the_table_is_refused_by_name() {
            for (table, key) in [
                (
                    "[operator]\nlisten = \"127.0.0.1:8444\"\nport = 8444\n",
                    "port",
                ),
                (
                    "[operator]\nlisten = \"127.0.0.1:8444\"\n[operator.tls]\ncert = \"a\"\nkey = \"b\"\nca = \"c\"\n",
                    "ca",
                ),
                (
                    "[operator]\nlisten = \"127.0.0.1:8444\"\n[operator.limits]\nbody = 1\n",
                    "limits",
                ),
            ] {
                let error = parse(&with(table)).expect_err(key);
                assert!(error.0.contains(&format!("`{key}`")), "{key}: {error}");
            }
        }

        #[test]
        fn half_a_certificate_and_key_pair_is_refused_by_the_missing_half() {
            for (given, missing) in [("cert", "key"), ("key", "cert")] {
                let table = format!(
                    "[operator]\nlisten = \"127.0.0.1:8444\"\n[operator.tls]\n{given} = \"a.pem\"\n"
                );
                let error = parse(&with(&table)).expect_err(missing);
                assert!(
                    error.0.contains(&format!("`{missing}`")),
                    "{given} alone: {error}"
                );
            }
        }

        #[test]
        fn the_table_written_twice_is_refused() {
            let table = "[operator]\nlisten = \"127.0.0.1:8444\"\n[operator]\nlisten = \"127.0.0.1:8445\"\n";
            assert!(parse(&with(table)).is_err(), "TOML takes a table once");
        }

        #[test]
        fn operator_as_a_value_and_not_a_table_is_refused() {
            let text = format!("operator = \"127.0.0.1:8444\"\n{LEAST}");
            let error = parse(&text).expect_err("not a table");
            assert!(error.0.contains("operator"), "{error}");
        }

        #[test]
        fn two_addresses_are_one_socket_only_on_one_port_and_one_address() {
            let same = [
                ("127.0.0.1:8443", "127.0.0.1:8443"),
                ("0.0.0.0:8443", "127.0.0.1:8443"),
                ("192.0.2.10:8443", "0.0.0.0:8443"),
                ("[::]:8443", "[2001:db8::10]:8443"),
                ("[::1]:8443", "[::1]:8443"),
                ("0.0.0.0:8443", "0.0.0.0:8443"),
            ];
            for (a, b) in same {
                assert!(same_socket(address(a), address(b)), "{a} and {b}");
                assert!(same_socket(address(b), address(a)), "{b} and {a}");
            }
            let different = [
                ("127.0.0.1:8443", "127.0.0.1:8444"),
                ("127.0.0.1:8443", "192.0.2.10:8443"),
                ("0.0.0.0:8443", "0.0.0.0:8444"),
                ("127.0.0.1:8443", "[::1]:8443"),
                ("0.0.0.0:8443", "[::1]:8443"),
                ("127.0.0.1:0", "127.0.0.1:0"),
                ("0.0.0.0:0", "127.0.0.1:0"),
            ];
            for (a, b) in different {
                assert!(!same_socket(address(a), address(b)), "{a} and {b}");
                assert!(!same_socket(address(b), address(a)), "{b} and {a}");
            }
        }
    }

    /// `keys.no_expiry`: whether a key may be made with no expiry.
    mod key_expiry {
        use super::*;

        fn with(table: &str) -> Result<Config, ConfigError> {
            parse(&format!("{LEAST}\n{table}"))
        }

        #[test]
        fn without_the_key_a_key_may_be_made_with_no_expiry() {
            assert!(
                parse(LEAST).expect("parses").keys_no_expiry,
                "the default allows it"
            );
        }

        #[test]
        fn the_key_is_read_as_written() {
            for (text, want) in [("true", true), ("false", false)] {
                let config = with(&format!("[keys]\nno_expiry = {text}\n")).expect("parses");
                assert_eq!(config.keys_no_expiry, want, "no_expiry = {text}");
            }
        }

        #[test]
        fn an_empty_table_leaves_the_default() {
            assert!(
                with("[keys]\n").expect("parses").keys_no_expiry,
                "a table with no key"
            );
        }

        #[test]
        fn a_value_that_is_not_a_boolean_is_refused() {
            for text in ["\"false\"", "0", "1", "[]", "90"] {
                let error = with(&format!("[keys]\nno_expiry = {text}\n")).expect_err(text);
                assert!(error.0.contains("no_expiry"), "{text}: {error}");
            }
        }

        #[test]
        fn another_key_in_the_table_is_refused() {
            let error = with("[keys]\ndefault_days = 30\n").expect_err("an unknown key");
            assert!(error.0.contains("default_days"), "{error}");
        }

        #[test]
        fn the_key_outside_its_table_is_refused() {
            let error = parse(&format!("no_expiry = false\n{LEAST}")).expect_err("the wrong place");
            assert!(error.0.contains("no_expiry"), "{error}");
        }
    }

    mod public_origins {
        use super::*;
        use crate::session::Origin;

        const OPERATOR: &str = "[operator]\nlisten = \"127.0.0.1:8444\"\n";

        /// The least configuration with `public_origin` set to `tenant`,
        /// and an `[operator]` table that ends with the lines `operator`.
        fn with(tenant: Option<&str>, operator: &str) -> Result<Config, ConfigError> {
            let first = tenant.map_or_else(String::new, |o| format!("public_origin = {o}\n"));
            parse(&format!("{first}{LEAST}\n{OPERATOR}{operator}"))
        }

        fn origin(text: &str) -> Option<Origin> {
            Some(Origin::parse(text).expect("an origin"))
        }

        fn operators(config: &Config) -> Option<Origin> {
            config
                .operator
                .as_ref()
                .expect("an operator listener")
                .public_origin
                .clone()
        }

        // What must work.

        #[test]
        fn without_the_keys_no_listener_has_a_public_origin() {
            let config = with(None, "").expect("parses");
            assert_eq!(config.public_origin, None, "sessions are off on `listen`");
            assert_eq!(operators(&config), None, "and on `operator.listen`");
            assert_eq!(
                parse(LEAST).expect("parses").public_origin,
                None,
                "the least"
            );
        }

        #[test]
        fn public_origin_is_the_tenant_listener_s() {
            let config = with(Some("\"https://vcon.example.com\""), "").expect("parses");
            assert_eq!(
                config.public_origin,
                origin("https://vcon.example.com"),
                "the tenant listener's"
            );
            assert_eq!(operators(&config), None, "the operator listener has none");
        }

        #[test]
        fn operator_public_origin_is_the_operator_listener_s() {
            let config = with(
                Some("\"https://vcon.example.com\""),
                "public_origin = \"https://ops.example.com:8444\"\n",
            )
            .expect("parses");
            assert_eq!(
                config.public_origin,
                origin("https://vcon.example.com"),
                "the tenant listener's"
            );
            assert_eq!(
                operators(&config),
                origin("https://ops.example.com:8444"),
                "the operator listener's"
            );
        }

        #[test]
        fn a_server_with_one_listener_takes_public_origin_alone() {
            let text = format!("public_origin = \"https://vcon.example.com:8443\"\n{LEAST}");
            let config = parse(&text).expect("parses");
            assert_eq!(
                config.public_origin,
                origin("https://vcon.example.com:8443"),
                "the one listener's"
            );
            assert_eq!(config.operator, None, "no operator listener");
        }

        // What must be refused.

        #[test]
        fn a_public_origin_that_is_not_https_and_a_host_is_refused_by_its_key() {
            for bad in [
                "http://vcon.example.com",
                "https://vcon.example.com/",
                "https://vcon.example.com/console",
                "vcon.example.com",
                "https://Vcon.example.com",
                "",
            ] {
                let error = with(Some(&format!("{bad:?}")), "").expect_err(bad);
                assert!(
                    error.0.starts_with("public_origin: ") && error.0.contains(bad),
                    "{error}"
                );
            }
        }

        #[test]
        fn an_operator_public_origin_that_is_not_an_origin_is_refused_by_its_key() {
            let error = with(None, "public_origin = \"https://ops.example.com/admin\"\n")
                .expect_err("a path");
            assert!(
                error.0.starts_with("operator.public_origin: "),
                "the key is named: {error}"
            );
        }

        #[test]
        fn two_public_origins_on_one_host_are_refused_whatever_their_ports() {
            for (tenant, operator) in [
                ("https://vcon.example.com", "https://vcon.example.com"),
                ("https://vcon.example.com", "https://vcon.example.com:8444"),
                (
                    "https://vcon.example.com:8443",
                    "https://vcon.example.com:8444",
                ),
                ("https://[2001:db8::7]:8443", "https://[2001:db8::7]:8444"),
            ] {
                let error = with(
                    Some(&format!("{tenant:?}")),
                    &format!("public_origin = {operator:?}\n"),
                )
                .expect_err("one host");
                assert!(
                    error.0.starts_with("operator.public_origin: ") && error.0.contains("host"),
                    "{tenant} and {operator}: {error}"
                );
            }
        }

        #[test]
        fn a_public_origin_that_is_not_a_string_is_refused() {
            assert!(with(Some("8443"), "").is_err(), "a number");
            assert!(
                with(Some("[\"https://a.example.com\"]"), "").is_err(),
                "a list"
            );
            assert!(with(None, "public_origin = true\n").is_err(), "a flag");
        }

        // Limits and odd inputs.

        #[test]
        fn the_operator_listener_may_have_a_public_origin_when_the_tenant_s_has_none() {
            let config =
                with(None, "public_origin = \"https://ops.example.com\"\n").expect("parses");
            assert_eq!(config.public_origin, None, "sessions off for tenants");
            assert_eq!(
                operators(&config),
                origin("https://ops.example.com"),
                "the operator listener's"
            );
        }

        #[test]
        fn two_public_origins_on_two_hosts_may_share_a_port() {
            let config = with(
                Some("\"https://vcon.example.com:8443\""),
                "public_origin = \"https://ops.example.com:8443\"\n",
            )
            .expect("two hosts");
            assert_eq!(
                operators(&config),
                origin("https://ops.example.com:8443"),
                "the operator listener's"
            );
        }

        #[test]
        fn a_public_origin_may_be_an_address_or_localhost() {
            for text in [
                "https://localhost:8443",
                "https://192.0.2.7:8443",
                "https://[2001:db8::7]:8443",
            ] {
                let config = with(Some(&format!("{text:?}")), "").expect(text);
                assert_eq!(config.public_origin, origin(text), "{text}");
            }
        }

        #[test]
        fn a_host_that_is_another_s_prefix_is_another_host() {
            let config = with(
                Some("\"https://vcon.example.com\""),
                "public_origin = \"https://vcon.example.com.example.net\"\n",
            )
            .expect("two hosts");
            assert_eq!(
                operators(&config),
                origin("https://vcon.example.com.example.net"),
                "the operator listener's"
            );
        }
    }

    mod connection_limits {
        use super::*;

        fn with(timeouts: &str) -> Result<Config, ConfigError> {
            parse(&format!("{LEAST}\n[limits.timeouts]\n{timeouts}"))
        }

        #[test]
        fn the_defaults_are_ten_seconds_thirty_thirty_and_two_minutes_idle() {
            let config = parse(LEAST).expect("parses");
            assert_eq!(
                config.connection,
                ConnectionLimits {
                    handshake: Duration::from_secs(10),
                    first_head: Duration::from_secs(30),
                    head: Duration::from_secs(30),
                    idle: Duration::from_secs(120),
                    goodbye: Duration::from_secs(5),
                    accept_pause: Duration::from_secs(1),
                    max: 512,
                },
                "with no [limits.timeouts] table"
            );
            assert_eq!(
                config.connection,
                ConnectionLimits::default(),
                "the defaults serve applies without a configuration"
            );
        }

        #[test]
        fn each_key_sets_its_own_limit_and_no_other() {
            let default = ConnectionLimits::default();
            for (line, expected) in [
                (
                    "handshake = 3",
                    ConnectionLimits {
                        handshake: Duration::from_secs(3),
                        ..default
                    },
                ),
                (
                    "first_head = 7",
                    ConnectionLimits {
                        first_head: Duration::from_secs(7),
                        ..default
                    },
                ),
                (
                    "head = 75",
                    ConnectionLimits {
                        head: Duration::from_secs(75),
                        ..default
                    },
                ),
                (
                    "idle = 300",
                    ConnectionLimits {
                        idle: Duration::from_secs(300),
                        ..default
                    },
                ),
            ] {
                let config = with(line).expect(line);
                assert_eq!(config.connection, expected, "{line}");
                assert_eq!(
                    config.read_timeout, READ_TIMEOUT,
                    "{line}: the body limit is its own key"
                );
            }
        }

        #[test]
        fn the_four_keys_together_are_all_read() {
            let config = with("handshake = 2\nfirst_head = 4\nhead = 6\nread = 8\nidle = 10")
                .expect("parses");
            assert_eq!(
                (
                    config.connection.handshake,
                    config.connection.first_head,
                    config.connection.head,
                    config.read_timeout,
                    config.connection.idle,
                ),
                (
                    Duration::from_secs(2),
                    Duration::from_secs(4),
                    Duration::from_secs(6),
                    Duration::from_secs(8),
                    Duration::from_secs(10),
                ),
                "each key in its own member"
            );
        }

        #[test]
        fn an_idle_connection_is_kept_two_minutes_unless_told() {
            assert_eq!(
                parse(LEAST).expect("parses").connection.idle,
                Duration::from_secs(120),
                "with no [limits.timeouts] table"
            );
            assert_eq!(
                with("head = 5").expect("parses").connection.idle,
                Duration::from_secs(120),
                "with a table that does not say"
            );
        }

        #[test]
        fn the_idle_limit_may_be_shorter_than_the_head_limit_and_longer() {
            // Neither bounds the other: one is a wait between requests,
            // the other the time a request head takes once begun.
            for (idle, head) in [(1, 3600), (3600, 1), (30, 30)] {
                let config = with(&format!("idle = {idle}\nhead = {head}")).expect("parses");
                assert_eq!(
                    (config.connection.idle, config.connection.head),
                    (Duration::from_secs(idle), Duration::from_secs(head)),
                    "idle {idle}, head {head}"
                );
            }
        }

        #[test]
        fn the_wait_after_a_connection_is_told_to_close_has_no_key() {
            let error = with("goodbye = 2").expect_err("no such key");
            assert!(error.0.contains("`goodbye`"), "{error}");
            assert_eq!(
                with("idle = 9").expect("parses").connection.goodbye,
                Duration::from_secs(5),
                "five seconds whatever the keys say"
            );
        }

        #[test]
        fn one_second_and_one_hour_are_the_bounds_and_both_are_taken() {
            for key in ["handshake", "first_head", "head", "idle"] {
                for seconds in [1, CONNECTION_LIMIT_MAX] {
                    assert!(
                        with(&format!("{key} = {seconds}")).is_ok(),
                        "{key} = {seconds}"
                    );
                }
            }
        }

        #[test]
        fn zero_is_refused_by_name() {
            for key in ["handshake", "first_head", "head", "idle"] {
                let error = with(&format!("{key} = 0")).expect_err(key);
                assert_eq!(
                    error.0,
                    format!("limits.timeouts.{key} must be more than 0"),
                    "{key}"
                );
            }
        }

        #[test]
        fn more_than_an_hour_is_refused_by_name_with_the_bound() {
            for key in ["handshake", "first_head", "head", "idle"] {
                for seconds in [3601_u64, 86_400, u64::from(u32::MAX)] {
                    let error = with(&format!("{key} = {seconds}")).expect_err(key);
                    assert_eq!(
                        error.0,
                        format!(
                            "limits.timeouts.{key} must be at most 3600 seconds, and is {seconds}"
                        ),
                        "{key} = {seconds}"
                    );
                }
            }
        }

        #[test]
        fn a_value_that_is_not_whole_seconds_is_refused() {
            for key in ["handshake", "first_head", "head", "idle"] {
                for value in ["-1", "1.5", "\"10\"", "true", "[10]"] {
                    let error = with(&format!("{key} = {value}")).expect_err(key);
                    assert!(error.0.contains(key), "{key} = {value}: {error}");
                }
            }
        }

        #[test]
        fn the_wait_after_a_failed_accept_has_no_key() {
            let error = with("accept_pause = 2").expect_err("no such key");
            assert!(error.0.contains("`accept_pause`"), "{error}");
        }

        /// The least configuration with `line` in a `[limits]` table.
        fn with_limit(line: &str) -> Result<Config, ConfigError> {
            parse(&format!("{LEAST}\n[limits]\n{line}\n"))
        }

        #[test]
        fn a_listener_takes_120_reports_a_minute_and_20_from_one_address_unless_told() {
            let reports = parse(LEAST).expect("parses").reports;
            assert_eq!(
                (reports.per_minute, reports.per_address),
                (120, 20),
                "the defaults"
            );
            assert_eq!(reports, crate::api::ReportLimits::DEFAULT, "the gate's own");
        }

        #[test]
        fn both_report_limits_are_read() {
            let reports = with_limit("csp_reports = 300\ncsp_reports_per_address = 30")
                .expect("parses")
                .reports;
            assert_eq!(
                (reports.per_minute, reports.per_address),
                (300, 30),
                "as written"
            );
        }

        #[test]
        fn the_minutes_number_alone_is_read_and_one_addresss_stays() {
            let reports = with_limit("csp_reports = 500").expect("parses").reports;
            assert_eq!(
                (reports.per_minute, reports.per_address),
                (500, 20),
                "one key"
            );
        }

        #[test]
        fn one_addresss_number_alone_is_read_and_the_minutes_stays() {
            let reports = with_limit("csp_reports_per_address = 7")
                .expect("parses")
                .reports;
            assert_eq!(
                (reports.per_minute, reports.per_address),
                (120, 7),
                "one key"
            );
        }

        #[test]
        fn a_minutes_number_under_20_lowers_one_addresss_default_with_it() {
            let reports = with_limit("csp_reports = 5").expect("parses").reports;
            assert_eq!(
                (reports.per_minute, reports.per_address),
                (5, 5),
                "never more than all"
            );
        }

        #[test]
        fn the_bounds_of_the_report_limits_are_taken() {
            let least = with_limit("csp_reports = 1\ncsp_reports_per_address = 1")
                .expect("the least")
                .reports;
            assert_eq!((least.per_minute, least.per_address), (1, 1), "the least");
            let most = with_limit("csp_reports = 6000\ncsp_reports_per_address = 6000")
                .expect("the most")
                .reports;
            assert_eq!(
                (most.per_minute, most.per_address),
                (6000, 6000),
                "the most"
            );
        }

        #[test]
        fn the_report_limits_change_no_other_limit() {
            let config = with_limit("csp_reports = 300").expect("parses");
            let base = parse(LEAST).expect("parses");
            assert_eq!(config.connection, base.connection, "the connection limits");
            assert_eq!(config.body_limit, base.body_limit, "the body limit");
        }

        #[test]
        fn no_reports_at_all_is_refused_by_name() {
            for key in ["csp_reports", "csp_reports_per_address"] {
                let error = with_limit(&format!("{key} = 0")).expect_err("zero");
                assert_eq!(
                    error.0,
                    format!("limits.{key} must be more than 0"),
                    "{key}"
                );
            }
        }

        #[test]
        fn more_reports_than_6000_is_refused_by_name_with_the_bound() {
            for key in ["csp_reports", "csp_reports_per_address"] {
                for value in [6001_u64, 1_000_000, u64::from(u32::MAX) + 1] {
                    let error = with_limit(&format!("{key} = {value}")).expect_err("too many");
                    assert_eq!(
                        error.0,
                        format!("limits.{key} must be at most 6000, and is {value}"),
                        "{key} = {value}"
                    );
                }
            }
        }

        #[test]
        fn more_from_one_address_than_from_all_is_refused_with_both_numbers() {
            let error =
                with_limit("csp_reports = 10\ncsp_reports_per_address = 11").expect_err("more");
            assert_eq!(
                error.0,
                "limits.csp_reports_per_address (11) must be at most limits.csp_reports (10)",
                "the two numbers"
            );
            let error =
                with_limit("csp_reports_per_address = 121").expect_err("more than the default");
            assert!(
                error
                    .0
                    .contains("(121) must be at most limits.csp_reports (120)"),
                "{error}"
            );
        }

        #[test]
        fn a_report_limit_that_is_not_a_whole_number_is_refused() {
            for key in ["csp_reports", "csp_reports_per_address"] {
                for value in ["-1", "1.5", "\"ten\"", "true"] {
                    let error = with_limit(&format!("{key} = {value}")).expect_err(value);
                    assert!(error.0.contains(key), "{key} = {value}: {error}");
                }
            }
        }

        #[test]
        fn a_report_limit_written_outside_the_limits_table_is_refused() {
            let error = parse(&format!("{LEAST}\ncsp_reports = 5\n")).expect_err("the wrong table");
            assert!(error.0.contains("csp_reports"), "{error}");
        }

        #[test]
        fn a_listener_holds_512_connections_at_once_unless_told() {
            assert_eq!(
                parse(LEAST).expect("parses").connection.max,
                512,
                "with no [limits] table"
            );
            assert_eq!(
                with_limit("body = 1024").expect("parses").connection.max,
                512,
                "with a [limits] table that does not say"
            );
        }

        #[test]
        fn the_most_connections_at_once_is_read_and_changes_no_other_limit() {
            let config = with_limit("connections = 2000").expect("parses");
            assert_eq!(
                config.connection,
                ConnectionLimits {
                    max: 2000,
                    ..ConnectionLimits::default()
                },
                "only the most at once differs"
            );
        }

        #[test]
        fn one_connection_and_65536_are_the_bounds_and_both_are_taken() {
            for connections in [1_usize, 65_536] {
                assert_eq!(
                    with_limit(&format!("connections = {connections}"))
                        .expect("parses")
                        .connection
                        .max,
                    connections,
                    "{connections}"
                );
            }
        }

        #[test]
        fn no_connections_at_all_is_refused_by_name() {
            let error = with_limit("connections = 0").expect_err("zero");
            assert_eq!(
                error.0, "limits.connections must be more than 0",
                "a listener that takes no connection serves nothing"
            );
        }

        #[test]
        fn more_connections_than_65536_is_refused_by_name_with_the_bound() {
            for connections in [65_537_u64, 1_000_000, u64::from(u32::MAX)] {
                let error =
                    with_limit(&format!("connections = {connections}")).expect_err("too many");
                assert_eq!(
                    error.0,
                    format!("limits.connections must be at most 65536, and is {connections}"),
                    "{connections}"
                );
            }
        }

        #[test]
        fn a_number_of_connections_that_is_not_a_whole_number_is_refused() {
            for value in ["-1", "1.5", "\"512\"", "true", "[512]"] {
                let error = with_limit(&format!("connections = {value}")).expect_err(value);
                assert!(error.0.contains("connections"), "{value}: {error}");
            }
        }
    }

    #[test]
    fn retention_and_supported_extensions_have_defaults() {
        let text = FULL
            .replace("retention_default = \"18m\"\n", "")
            .replace("supported_critical = [\"known\", \"other\"]\n", "");
        let config = parse(&text).expect("parses");
        assert_eq!(
            config.retention_default,
            Retention::Years(7),
            "with no retention_default, vCons are kept seven years"
        );
        assert_eq!(
            config.supported_critical,
            BTreeSet::new(),
            "with no supported_critical, no extension is supported"
        );
    }

    /// The configuration with only the keys that have no default.
    const LEAST: &str = r#"
listen = "127.0.0.1:8443"
data_dir = "/var/lib/vcon-server"
master_key = "/etc/vcon-server/master.key"

[tls]
cert = "/etc/vcon-server/cert.pem"
key = "/etc/vcon-server/key.pem"
"#;

    #[test]
    fn periods_and_limits_have_defaults() {
        let config = parse(LEAST).expect("parses");
        assert_eq!(
            config.purge_interval,
            Duration::from_secs(3600),
            "the purge sweep runs hourly by default"
        );
        assert_eq!(
            config.rotation_warn,
            Duration::from_secs(30 * 86_400),
            "an overdue rotation is warned of after 30 days by default"
        );
        assert_eq!(
            config.body_limit,
            16 << 20,
            "a body may be 16 MiB by default"
        );
        assert_eq!(
            config.inflated_limit,
            64 << 20,
            "a body may inflate to 64 MiB by default"
        );
        assert_eq!(
            config.read_timeout,
            Duration::from_secs(60),
            "a request has 60 seconds to arrive by default"
        );
        assert_eq!(
            config.mcp,
            crate::mcp::Limits::DEFAULT,
            "with no [mcp] table, the MCP endpoint takes its default limits"
        );
    }

    /// A key left out of a table that is present takes its default; the
    /// others keep their written values.
    #[test]
    fn each_limit_defaults_alone() {
        let base = parse(FULL).expect("parses");
        for (line, default) in [
            (
                "body = 10485760\n",
                Config {
                    body_limit: 16 << 20,
                    ..base.clone()
                },
            ),
            (
                "inflated = 104857600\n",
                Config {
                    inflated_limit: 64 << 20,
                    ..base.clone()
                },
            ),
            (
                "read = 30\n",
                Config {
                    read_timeout: Duration::from_secs(60),
                    ..base.clone()
                },
            ),
            (
                "purge_interval = 1800\n",
                Config {
                    purge_interval: Duration::from_secs(3600),
                    ..base.clone()
                },
            ),
            (
                "rotation_warn = 604800\n",
                Config {
                    rotation_warn: Duration::from_secs(30 * 86_400),
                    ..base.clone()
                },
            ),
            (
                "[limits.timeouts]\nread = 30\nhandshake = 5\nfirst_head = 15\nhead = 20\nidle = 90\n",
                Config {
                    read_timeout: Duration::from_secs(60),
                    connection: ConnectionLimits {
                        max: base.connection.max,
                        ..ConnectionLimits::default()
                    },
                    ..base.clone()
                },
            ),
            (
                "connections = 64\n",
                Config {
                    connection: ConnectionLimits {
                        max: 512,
                        ..base.connection
                    },
                    ..base.clone()
                },
            ),
            (
                "handshake = 5\n",
                Config {
                    connection: ConnectionLimits {
                        handshake: Duration::from_secs(10),
                        ..base.connection
                    },
                    ..base.clone()
                },
            ),
            (
                "first_head = 15\n",
                Config {
                    connection: ConnectionLimits {
                        first_head: Duration::from_secs(30),
                        ..base.connection
                    },
                    ..base.clone()
                },
            ),
            (
                "head = 20\n",
                Config {
                    connection: ConnectionLimits {
                        head: Duration::from_secs(30),
                        ..base.connection
                    },
                    ..base.clone()
                },
            ),
            (
                "idle = 90\n",
                Config {
                    connection: ConnectionLimits {
                        idle: Duration::from_secs(120),
                        ..base.connection
                    },
                    ..base.clone()
                },
            ),
            (
                "calls_per_second = 20\n",
                Config {
                    mcp: crate::mcp::Limits {
                        calls_per_second: 100,
                        ..base.mcp
                    },
                    ..base.clone()
                },
            ),
            (
                "concurrent = 8\n",
                Config {
                    mcp: crate::mcp::Limits {
                        concurrent: 100,
                        ..base.mcp
                    },
                    ..base.clone()
                },
            ),
            (
                "response_bytes = 262144\n",
                Config {
                    mcp: crate::mcp::Limits {
                        response_bytes: 1 << 20,
                        ..base.mcp
                    },
                    ..base.clone()
                },
            ),
        ] {
            assert!(FULL.contains(line), "{line}");
            assert_eq!(parse(&FULL.replace(line, "")), Ok(default), "{line}");
        }
    }

    /// The defaults differ from the full file's values, so each test above
    /// tells a default from a written value.
    #[test]
    fn the_full_file_sets_no_value_equal_to_its_default() {
        let (full, least) = (parse(FULL).expect("full"), parse(LEAST).expect("least"));
        assert_ne!(
            full.purge_interval, least.purge_interval,
            "FULL writes purge_interval as a value other than its default"
        );
        assert_ne!(
            full.rotation_warn, least.rotation_warn,
            "FULL writes rotation_warn as a value other than its default"
        );
        assert_ne!(
            full.body_limit, least.body_limit,
            "FULL writes limits.body as a value other than its default"
        );
        assert_ne!(
            full.inflated_limit, least.inflated_limit,
            "FULL writes limits.inflated as a value other than its default"
        );
        assert_ne!(
            full.read_timeout, least.read_timeout,
            "FULL writes limits.timeouts.read as a value other than its default"
        );
        assert_ne!(
            full.mcp.calls_per_second, least.mcp.calls_per_second,
            "FULL writes mcp.calls_per_second as a value other than its default"
        );
        assert_ne!(
            full.mcp.concurrent, least.mcp.concurrent,
            "FULL writes mcp.concurrent as a value other than its default"
        );
        assert_ne!(
            full.mcp.response_bytes, least.mcp.response_bytes,
            "FULL writes mcp.response_bytes as a value other than its default"
        );
        assert_ne!(
            full.storage, least.storage,
            "FULL writes storage.objects as a value other than its default"
        );
        let parquet = full.storage.parquet.expect("FULL has storage.parquet");
        let least_parquet = parquet_least();
        for (key, differs) in [
            (
                "storage.parquet.pack_interval",
                parquet.pack_interval != least_parquet.pack_interval,
            ),
            (
                "storage.parquet.pack_limit",
                parquet.pack_limit != least_parquet.pack_limit,
            ),
            (
                "storage.parquet.reader_grace",
                parquet.reader_grace != least_parquet.reader_grace,
            ),
        ] {
            assert!(
                differs,
                "FULL writes {key} as a value other than its default"
            );
        }
        let ObjectStore::S3(s3) = full.storage.objects else {
            panic!("FULL keeps its objects in S3")
        };
        let least_s3 = s3_least();
        for (key, differs) in [
            ("storage.s3.endpoint", s3.endpoint != least_s3.endpoint),
            ("storage.s3.prefix", s3.prefix != least_s3.prefix),
            (
                "storage.s3.path_style",
                s3.path_style != least_s3.path_style,
            ),
            (
                "storage.s3.access_key_id",
                s3.access_key_id != least_s3.access_key_id,
            ),
            (
                "storage.s3.secret_access_key_file",
                s3.secret_access_key_file != least_s3.secret_access_key_file,
            ),
            (
                "storage.s3.allow_http",
                s3.allow_http != least_s3.allow_http,
            ),
            ("storage.s3.ca_file", s3.ca_file != least_s3.ca_file),
            (
                "storage.s3.allow_versioning",
                s3.allow_versioning != least_s3.allow_versioning,
            ),
            ("storage.s3.timeout", s3.timeout != least_s3.timeout),
        ] {
            assert!(
                differs,
                "FULL writes {key} as a value other than its default"
            );
        }
        let export = full.export.s3.expect("FULL has export.s3");
        let least_export = parse(&format!("{LEAST}\n[export.s3]\nregion = \"us-east-1\"\n"))
            .expect("least export")
            .export
            .s3
            .expect("export.s3");
        for (key, differs) in [
            (
                "export.s3.endpoint",
                export.endpoint != least_export.endpoint,
            ),
            ("export.s3.region", export.region != least_export.region),
            (
                "export.s3.path_style",
                export.path_style != least_export.path_style,
            ),
            (
                "export.s3.access_key_id",
                export.access_key_id != least_export.access_key_id,
            ),
            (
                "export.s3.secret_access_key_file",
                export.secret_access_key_file != least_export.secret_access_key_file,
            ),
            (
                "export.s3.allow_http",
                export.allow_http != least_export.allow_http,
            ),
            ("export.s3.ca_file", export.ca_file != least_export.ca_file),
            ("export.s3.timeout", export.timeout != least_export.timeout),
        ] {
            assert!(
                differs,
                "FULL writes {key} as a value other than its default"
            );
        }
        assert_ne!(
            full.catalog, least.catalog,
            "FULL writes catalog.backend as a value other than its default"
        );
        let CatalogStore::Postgres(pg) = full.catalog else {
            panic!("FULL keeps its catalogs in PostgreSQL")
        };
        let least_pg = catalog::postgres_least();
        for (key, differs) in [
            ("catalog.postgres.port", pg.port != least_pg.port),
            ("catalog.postgres.ca_file", pg.ca_file != least_pg.ca_file),
            (
                "catalog.postgres.password_file",
                pg.password_file != least_pg.password_file,
            ),
            (
                "catalog.postgres.client_cert_file",
                pg.client_cert_file != least_pg.client_cert_file,
            ),
            (
                "catalog.postgres.client_key_file",
                pg.client_key_file != least_pg.client_key_file,
            ),
            ("catalog.postgres.pool", pg.pool != least_pg.pool),
            (
                "catalog.postgres.connect_timeout",
                pg.connect_timeout != least_pg.connect_timeout,
            ),
            (
                "catalog.postgres.statement_timeout",
                pg.statement_timeout != least_pg.statement_timeout,
            ),
            (
                "catalog.postgres.lock_timeout",
                pg.lock_timeout != least_pg.lock_timeout,
            ),
        ] {
            assert!(
                differs,
                "FULL writes {key} as a value other than its default"
            );
        }
    }

    /// The S3 settings with only the keys that have no default.
    fn s3_least() -> S3Settings {
        let text = format!(
            "{LEAST}\n[storage]\nobjects = \"s3\"\n[storage.s3]\nregion = \"us-east-1\"\nbucket = \"vcon\"\n"
        );
        match parse(&text)
            .expect("the least S3 configuration reads")
            .storage
            .objects
        {
            ObjectStore::S3(s3) => s3,
            ObjectStore::Local => panic!("objects = \"s3\" keeps the objects in S3"),
        }
    }

    #[test]
    fn without_a_storage_table_everything_is_local() {
        assert_eq!(
            parse(LEAST).expect("parses").storage,
            Storage {
                objects: ObjectStore::Local,
                parquet: None,
            },
            "a file written before storage backends keeps its meaning"
        );
    }

    /// The Parquet settings of an empty `[storage.parquet]` table.
    fn parquet_least() -> ParquetSettings {
        let text = format!(
            "{LEAST}\n[storage]\nobjects = \"s3\"\n[storage.s3]\nregion = \"us-east-1\"\n\
             bucket = \"vcon\"\n[storage.parquet]\n"
        );
        parse(&text)
            .expect("the least Parquet configuration reads")
            .storage
            .parquet
            .expect("an empty [storage.parquet] turns Parquet storage on")
    }

    #[test]
    fn parquet_settings_have_defaults() {
        assert_eq!(
            parquet_least(),
            ParquetSettings {
                pack_interval: PACK_INTERVAL,
                pack_limit: PACK_LIMIT,
                reader_grace: READER_GRACE,
            },
            "an empty table takes every default"
        );
        assert_eq!(READER_GRACE, Duration::from_secs(900), "15 minutes");
        assert_eq!(PACK_INTERVAL, Duration::from_secs(300), "five minutes");
        assert_eq!(PACK_LIMIT, 16 << 20, "16 MiB");
    }

    #[test]
    fn s3_settings_have_defaults() {
        assert_eq!(
            s3_least(),
            S3Settings {
                endpoint: None,
                region: "us-east-1".to_owned(),
                bucket: "vcon".to_owned(),
                prefix: String::new(),
                path_style: false,
                access_key_id: None,
                secret_access_key_file: None,
                allow_http: false,
                ca_file: None,
                allow_versioning: false,
                timeout: Duration::from_secs(30),
            },
            "only region and bucket are required"
        );
    }

    /// `[export.s3]` takes `[storage.s3]`'s keys but the bucket, the
    /// prefix and `allow_versioning`, with the same defaults and rules,
    /// each refusal naming its own table.
    #[test]
    fn export_s3_settings_are_read_and_checked_as_storage_s3_is() {
        let export = |extra: &str| format!("{LEAST}\n[export.s3]\nregion = \"us-east-1\"\n{extra}");
        assert_eq!(
            parse(LEAST).expect("least").export,
            Export::default(),
            "no table"
        );
        assert_eq!(
            parse(&export("")).expect("least export").export.s3,
            Some(ExportS3 {
                endpoint: None,
                region: "us-east-1".to_owned(),
                path_style: false,
                access_key_id: None,
                secret_access_key_file: None,
                allow_http: false,
                ca_file: None,
                timeout: S3_TIMEOUT,
            }),
            "only region is required"
        );
        for (extra, key) in [
            (
                "endpoint = \"http://127.0.0.1:9000\"\n",
                "export.s3.allow_http",
            ),
            ("endpoint = \"objects.example.com\"\n", "export.s3.endpoint"),
            (
                "access_key_id = \"vcon\"\n",
                "export.s3.secret_access_key_file",
            ),
            (
                "secret_access_key_file = \"/s\"\n",
                "export.s3.access_key_id",
            ),
            ("timeout = 0\n", "export.s3.timeout"),
            (
                "endpoint = \"http://127.0.0.1:9000\"\nallow_http = true\nca_file = \"/c.pem\"\n",
                "export.s3.ca_file",
            ),
            ("bucket = \"vcon\"\n", "bucket"),
            ("prefix = \"a/\"\n", "prefix"),
            ("allow_versioning = true\n", "allow_versioning"),
        ] {
            let ConfigError(message) = parse(&export(extra)).expect_err(extra);
            assert!(message.contains(key), "{extra}: {message}");
        }
        assert!(
            parse(&format!("{LEAST}\n[export.s3]\n"))
                .expect_err("no region")
                .0
                .contains("region"),
            "region is required"
        );
    }

    /// The settings an `s3://` target uses: this service, with `--to`'s
    /// bucket and prefix; from `[storage.s3]`, its service and keys.
    #[test]
    fn an_export_service_names_its_bucket_and_prefix() {
        let full = parse(FULL).expect("full");
        let export = full.export.s3.expect("export.s3");
        let s = export.for_bucket("reports", "vcon/");
        assert_eq!(
            (s.bucket.as_str(), s.prefix.as_str()),
            ("reports", "vcon/"),
            "the target's bucket and prefix"
        );
        assert_eq!(s.endpoint, export.endpoint, "the export's endpoint");
        assert_eq!(s.region, export.region, "the export's region");
        assert_eq!(
            s.access_key_id, export.access_key_id,
            "the export's access key id"
        );
        assert_eq!(
            s.secret_access_key_file, export.secret_access_key_file,
            "the export's secret file"
        );
        assert_eq!(s.ca_file, export.ca_file, "the export's CA file");
        assert_eq!(
            (s.path_style, s.allow_http),
            (true, true),
            "FULL sets path_style and allow_http"
        );
        assert_eq!(s.timeout, Duration::from_secs(120), "the export's timeout");
        assert!(!s.allow_versioning, "never read for an export");
        let ObjectStore::S3(storage) = full.storage.objects else {
            panic!("FULL keeps its objects in S3")
        };
        let from = ExportS3::from(&storage).for_bucket(&storage.bucket, &storage.prefix);
        assert_eq!(
            from,
            S3Settings {
                allow_versioning: false,
                ..storage
            },
            "storage.s3's service and keys, whole"
        );
    }

    #[test]
    fn s3_settings_that_do_not_hold_together_are_named() {
        let s3 = |extra: &str| {
            format!(
                "{LEAST}\n[storage]\nobjects = \"s3\"\n[storage.s3]\nregion = \"us-east-1\"\nbucket = \"vcon\"\n{extra}"
            )
        };
        for (text, key) in [
            (
                format!("{LEAST}\n[storage]\nobjects = \"s3\"\n"),
                "storage.s3",
            ),
            (
                format!("{LEAST}\n[storage]\nobjects = \"s3\"\n[storage.s3]\nbucket = \"vcon\"\n"),
                "region",
            ),
            (
                format!(
                    "{LEAST}\n[storage]\nobjects = \"s3\"\n[storage.s3]\nregion = \"us-east-1\"\n"
                ),
                "bucket",
            ),
            (
                s3("endpoint = \"http://127.0.0.1:9000\"\n"),
                "storage.s3.allow_http",
            ),
            (
                s3("endpoint = \"objects.example.com\"\n"),
                "storage.s3.endpoint",
            ),
            (
                s3("access_key_id = \"vcon-server\"\n"),
                "storage.s3.secret_access_key_file",
            ),
            (
                s3("secret_access_key_file = \"/etc/vcon-server/s3.secret\"\n"),
                "storage.s3.access_key_id",
            ),
            (s3("timeout = 0\n"), "storage.s3.timeout"),
            (
                s3(
                    "endpoint = \"http://127.0.0.1:9000\"\nallow_http = true\nca_file = \"/etc/vcon-server/s3-ca.pem\"\n",
                ),
                "storage.s3.ca_file",
            ),
            (s3("prefix = \"/server-a/\"\n"), "storage.s3.prefix"),
            (s3("prefix = \"server-a\"\n"), "storage.s3.prefix"),
            (
                s3("bucket = \"\"\n").replace("bucket = \"vcon\"\n", ""),
                "storage.s3.bucket",
            ),
            (
                format!("{LEAST}\n[storage]\nobjects = \"tape\"\n"),
                "objects",
            ),
            (format!("{LEAST}\n[storage.parquet]\n"), "storage.parquet"),
            (
                format!("{LEAST}\n[storage]\nobjects = \"local\"\n[storage.parquet]\n"),
                "storage.parquet",
            ),
            (
                s3("[storage.parquet]\npack_interval = 0\n"),
                "storage.parquet.pack_interval",
            ),
            (
                s3("[storage.parquet]\npack_limit = 0\n"),
                "storage.parquet.pack_limit",
            ),
            (
                s3("[storage.parquet]\nreader_grace = 0\n"),
                "storage.parquet.reader_grace",
            ),
            (
                format!(
                    "{LEAST}\n[storage]\nobjects = \"local\"\n[storage.s3]\nregion = \"us-east-1\"\nbucket = \"vcon\"\n"
                ),
                "storage.s3",
            ),
        ] {
            let error = parse(&text).expect_err(key);
            assert!(error.0.contains(key), "the error names {key}: {error}");
        }
    }

    #[test]
    fn an_http_endpoint_reads_when_allowed() {
        let text = format!(
            "{LEAST}\n[storage]\nobjects = \"s3\"\n[storage.s3]\nregion = \"us-east-1\"\nbucket = \"vcon\"\nendpoint = \"http://127.0.0.1:9000\"\nallow_http = true\n"
        );
        assert!(
            parse(&text).is_ok(),
            "allow_http lets a test container's plain endpoint through"
        );
    }

    /// Each error names its key.
    #[test]
    fn every_missing_key_without_a_default_is_named() {
        for (line, key) in [
            ("listen = \"127.0.0.1:8443\"\n", "listen"),
            ("data_dir = \"/var/lib/vcon-server\"\n", "data_dir"),
            (
                "master_key = \"/etc/vcon-server/master.key\"\n",
                "master_key",
            ),
            ("cert = \"/etc/vcon-server/cert.pem\"\n", "cert"),
            ("key = \"/etc/vcon-server/key.pem\"\n", "key"),
        ] {
            assert!(FULL.contains(line), "{line}");
            let error = parse(&FULL.replace(line, "")).expect_err(key);
            assert!(error.0.contains(&format!("`{key}`")), "{key}: {error}");
        }
    }

    #[test]
    fn an_unknown_key_is_named() {
        for (text, key) in [
            (format!("colour = \"blue\"\n{FULL}"), "colour"),
            (FULL.replace("read = 30", "read = 30\nwrite = 30"), "write"),
            (FULL.replace("[tls]", "[tls]\nca = \"x\""), "ca"),
            (
                FULL.replace("[mcp]", "[mcp]\nresources = true"),
                "resources",
            ),
        ] {
            let error = parse(&text).expect_err(key);
            assert!(error.0.contains(&format!("`{key}`")), "{key}: {error}");
        }
    }

    #[test]
    fn a_bad_value_is_named() {
        for (from, to, key) in [
            ("\"127.0.0.1:8443\"", "\"localhost\"", "listen"),
            ("\"18m\"", "\"7w\"", "retention_default"),
            (
                "purge_interval = 1800",
                "purge_interval = 0",
                "purge_interval",
            ),
            (
                "rotation_warn = 604800",
                "rotation_warn = 0",
                "rotation_warn",
            ),
            ("body = 10485760", "body = 0", "limits.body"),
            ("inflated = 104857600", "inflated = 0", "limits.inflated"),
            ("connections = 64", "connections = 0", "limits.connections"),
            (
                "connections = 64",
                "connections = 65537",
                "limits.connections",
            ),
            (
                "calls_per_second = 20",
                "calls_per_second = 0",
                "mcp.calls_per_second",
            ),
            ("concurrent = 8", "concurrent = 0", "mcp.concurrent"),
            (
                "response_bytes = 262144",
                "response_bytes = 0",
                "mcp.response_bytes",
            ),
            (
                "concurrent = 8",
                "concurrent = 4294967296",
                "mcp.concurrent",
            ),
            ("read = 30", "read = 0", "limits.timeouts.read"),
            ("read = 30", "read = -1", "read"),
            (
                "handshake = 5",
                "handshake = 0",
                "limits.timeouts.handshake",
            ),
            (
                "handshake = 5",
                "handshake = 3601",
                "limits.timeouts.handshake",
            ),
            ("handshake = 5", "handshake = \"5s\"", "handshake"),
            (
                "first_head = 15",
                "first_head = 0",
                "limits.timeouts.first_head",
            ),
            (
                "first_head = 15",
                "first_head = 3601",
                "limits.timeouts.first_head",
            ),
            ("first_head = 15", "first_head = 1.5", "first_head"),
            ("\nhead = 20", "\nhead = 0", "limits.timeouts.head"),
            ("\nhead = 20", "\nhead = 3601", "limits.timeouts.head"),
            ("\nhead = 20", "\nhead = 18446744073709551615", "head"),
            ("idle = 90", "idle = 0", "limits.timeouts.idle"),
            ("idle = 90", "idle = 3601", "limits.timeouts.idle"),
            ("idle = 90", "idle = \"2m\"", "idle"),
        ] {
            let error = parse(&FULL.replace(from, to)).expect_err(key);
            assert!(error.0.contains(key), "{key}: {error}");
        }
    }

    /// The website's configuration page lists every key the full file
    /// sets, each with the default the code gives it, and every example
    /// file on the site reads.
    mod site_page {
        use super::*;

        fn page(name: &str) -> String {
            let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
                .join("../../website/content/docs")
                .join(name);
            std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display()))
        }

        /// Every key in a TOML table, dotted.
        fn keys(table: &toml::Table, prefix: &str, out: &mut Vec<String>) {
            for (key, value) in table {
                let name = format!("{prefix}{key}");
                match value {
                    toml::Value::Table(inner) => keys(inner, &format!("{name}."), out),
                    _ => out.push(name),
                }
            }
        }

        /// The table row for `key`, by its first cell.
        fn row<'a>(text: &'a str, key: &str) -> &'a str {
            let start = format!("| `{key}` |");
            let mut rows = text.lines().filter(|l| l.starts_with(&start));
            let row = rows.next().unwrap_or_else(|| panic!("no row for {key}"));
            assert!(rows.next().is_none(), "two rows for {key}");
            row
        }

        #[test]
        fn every_key_is_listed_with_its_default() {
            let text = page("configuration.md");
            let defaults = parse(LEAST).expect("least");
            let mut all = Vec::new();
            keys(&FULL.parse::<toml::Table>().expect("toml"), "", &mut all);
            assert_eq!(all.len(), 64, "{all:?}");
            let pg = catalog::postgres_least();
            for key in all {
                let default = match key.as_str() {
                    "listen" | "data_dir" | "master_key" | "tls.cert" | "tls.key" => {
                        "required".to_owned()
                    }
                    "operator.listen" => "none: `listen` serves both".to_owned(),
                    "operator.tls.cert" => "`tls.cert`".to_owned(),
                    "operator.tls.key" => "`tls.key`".to_owned(),
                    "public_origin" | "operator.public_origin" => {
                        assert_eq!(defaults.public_origin, None, "the page says none");
                        "none: the listener serves no session".to_owned()
                    }
                    "retention_default" => format!("`{}`", defaults.retention_default),
                    "purge_interval" => format!("`{}`", defaults.purge_interval.as_secs()),
                    "rotation_warn" => format!("`{}`", defaults.rotation_warn.as_secs()),
                    "limits.body" => format!("`{}`", defaults.body_limit),
                    "limits.inflated" => format!("`{}`", defaults.inflated_limit),
                    "limits.connections" => format!("`{}`", defaults.connection.max),
                    "limits.csp_reports" => format!("`{}`", defaults.reports.per_minute),
                    "limits.csp_reports_per_address" => {
                        format!("`{}`", defaults.reports.per_address)
                    }
                    "limits.timeouts.read" => format!("`{}`", defaults.read_timeout.as_secs()),
                    "limits.timeouts.handshake" => {
                        format!("`{}`", defaults.connection.handshake.as_secs())
                    }
                    "limits.timeouts.first_head" => {
                        format!("`{}`", defaults.connection.first_head.as_secs())
                    }
                    "limits.timeouts.head" => format!("`{}`", defaults.connection.head.as_secs()),
                    "limits.timeouts.idle" => format!("`{}`", defaults.connection.idle.as_secs()),
                    "keys.no_expiry" => format!("`{}`", defaults.keys_no_expiry),
                    "mcp.calls_per_second" => format!("`{}`", defaults.mcp.calls_per_second),
                    "mcp.concurrent" => format!("`{}`", defaults.mcp.concurrent),
                    "mcp.response_bytes" => format!("`{}`", defaults.mcp.response_bytes),
                    "storage.objects" => "`local`".to_owned(),
                    "storage.s3.region" | "storage.s3.bucket" => "required with `s3`".to_owned(),
                    "storage.s3.endpoint"
                    | "storage.s3.access_key_id"
                    | "storage.s3.secret_access_key_file"
                    | "storage.s3.ca_file" => "none".to_owned(),
                    "storage.s3.prefix" => "empty".to_owned(),
                    "storage.s3.path_style" => format!("`{}`", s3_least().path_style),
                    "storage.s3.allow_http" => format!("`{}`", s3_least().allow_http),
                    "storage.s3.allow_versioning" => {
                        format!("`{}`", s3_least().allow_versioning)
                    }
                    "storage.s3.timeout" => format!("`{}`", s3_least().timeout.as_secs()),
                    "storage.parquet.pack_interval" => {
                        format!("`{}`", parquet_least().pack_interval.as_secs())
                    }
                    "storage.parquet.pack_limit" => format!("`{}`", parquet_least().pack_limit),
                    "storage.parquet.reader_grace" => {
                        format!("`{}`", parquet_least().reader_grace.as_secs())
                    }
                    "export.s3.region" => "required with `export.s3`".to_owned(),
                    "export.s3.endpoint"
                    | "export.s3.access_key_id"
                    | "export.s3.secret_access_key_file"
                    | "export.s3.ca_file" => "none".to_owned(),
                    "export.s3.path_style" => format!("`{}`", s3_least().path_style),
                    "export.s3.allow_http" => format!("`{}`", s3_least().allow_http),
                    "export.s3.timeout" => format!("`{}`", S3_TIMEOUT.as_secs()),
                    "catalog.backend" => "`local`".to_owned(),
                    "catalog.postgres.hosts"
                    | "catalog.postgres.dbname"
                    | "catalog.postgres.user" => "required with `postgres`".to_owned(),
                    "catalog.postgres.ca_file"
                    | "catalog.postgres.password_file"
                    | "catalog.postgres.client_cert_file"
                    | "catalog.postgres.client_key_file" => "none".to_owned(),
                    "catalog.postgres.port" => format!("`{}`", pg.port),
                    "catalog.postgres.pool" => format!("`{}`", pg.pool),
                    "catalog.postgres.connect_timeout" => {
                        format!("`{}`", pg.connect_timeout.as_secs())
                    }
                    "catalog.postgres.statement_timeout" => {
                        format!("`{}`", pg.statement_timeout.as_secs())
                    }
                    "catalog.postgres.lock_timeout" => {
                        format!("`{}`", pg.lock_timeout.as_secs())
                    }
                    "supported_critical" => {
                        assert!(
                            defaults.supported_critical.is_empty(),
                            "the page says none, which holds only while the default is empty"
                        );
                        "none".to_owned()
                    }
                    other => panic!("a key this test does not know: {other}"),
                };
                let row = row(&text, &key);
                let last = row
                    .trim_end_matches('|')
                    .rsplit('|')
                    .next()
                    .expect("a last cell")
                    .trim();
                assert!(
                    last.starts_with(&default),
                    "{key}: `{last}` is not {default}"
                );
            }
        }

        /// The limits no key sets are stated as the code fixes them.
        #[test]
        fn every_fixed_limit_is_listed_with_its_value() {
            let text = page("configuration.md");
            for (limit, value) in [
                ("Signed layers in one body", vcon::read::MAX_SIGNED_LAYERS),
                (
                    "Arrays and objects open at once",
                    vcon::read::MAX_JSON_DEPTH,
                ),
            ] {
                let start = format!("| {limit} |");
                let rows: Vec<&str> = text.lines().filter(|l| l.starts_with(&start)).collect();
                assert_eq!(rows.len(), 1, "one row for {limit}");
                let cell = rows[0].split('|').nth(2).expect("a value cell").trim();
                assert_eq!(cell, format!("`{value}`"), "{limit}");
            }
        }

        #[test]
        fn every_example_file_on_the_site_reads() {
            let mut examples = 0;
            for name in [
                "configuration.md",
                "getting-started.md",
                "container.md",
                "postgres-catalog.md",
            ] {
                let text = page(name);
                for block in text.split("```toml\n").skip(1) {
                    let toml = block.split("```").next().expect("a block");
                    parse(toml).unwrap_or_else(|e| panic!("{name}: {e}\n{toml}"));
                    examples += 1;
                }
            }
            assert_eq!(
                examples, 7,
                "four on the configuration page, one each on getting started, the \
                 container page and the PostgreSQL catalog page, so a block the split misses \
                 is caught"
            );
        }
    }

    /// The `[catalog]` table: where the catalogs and the registry are kept,
    /// and how the PostgreSQL database is reached.
    mod catalog {
        use super::*;

        /// LEAST with its objects in a bucket and its catalogs in PostgreSQL,
        /// with only the `[catalog.postgres]` keys that have no default and
        /// a password file.
        pub(super) fn postgres_text() -> String {
            format!(
                "{LEAST}\n[storage]\nobjects = \"s3\"\n\n[storage.s3]\nregion = \"us-east-1\"\n\
                 bucket = \"vcon\"\n\n[catalog]\nbackend = \"postgres\"\n\n[catalog.postgres]\n\
                 hosts = [\"db1.example.com\"]\ndbname = \"vcon\"\nuser = \"vcon_app\"\n\
                 password_file = \"/etc/vcon-server/pg.password\"\n"
            )
        }

        /// `postgres_text` with `from` replaced by `to`; `from` must be there.
        fn edited(from: &str, to: &str) -> String {
            let text = postgres_text();
            assert!(text.contains(from), "the least text holds {from:?}");
            text.replace(from, to)
        }

        /// `postgres_text` with `line` added to `[catalog.postgres]`.
        fn with(line: &str) -> String {
            format!("{}{line}\n", postgres_text())
        }

        /// The PostgreSQL settings `text` reads to.
        fn postgres(text: &str) -> PostgresSettings {
            match parse(text)
                .unwrap_or_else(|e| panic!("{e}\n{text}"))
                .catalog
            {
                CatalogStore::Postgres(pg) => *pg,
                CatalogStore::Local => panic!("backend = \"postgres\" keeps the catalogs there"),
            }
        }

        /// The PostgreSQL settings with only the keys that have no default.
        pub(super) fn postgres_least() -> PostgresSettings {
            postgres(&postgres_text())
        }

        /// The message `text` is refused with.
        fn refused(text: &str) -> String {
            parse(text).expect_err(text).0
        }

        mod success {
            use super::*;

            #[test]
            fn a_postgres_catalog_is_read_with_its_defaults() {
                assert_eq!(
                    postgres_least(),
                    PostgresSettings {
                        hosts: vec!["db1.example.com".to_owned()],
                        port: 5432,
                        dbname: "vcon".to_owned(),
                        user: "vcon_app".to_owned(),
                        ca_file: None,
                        password_file: Some("/etc/vcon-server/pg.password".into()),
                        client_cert_file: None,
                        client_key_file: None,
                        pool: 16,
                        connect_timeout: Duration::from_secs(5),
                        statement_timeout: Duration::from_secs(30),
                        lock_timeout: Duration::from_secs(10),
                    },
                    "only hosts, dbname, user and a way to log in are required"
                );
            }

            #[test]
            fn without_a_catalog_table_the_catalogs_are_local() {
                assert_eq!(
                    parse(LEAST).expect("parses").catalog,
                    CatalogStore::Local,
                    "a file written before the PostgreSQL catalog keeps its meaning"
                );
            }

            #[test]
            fn backend_local_is_the_local_catalog() {
                let text = format!("{LEAST}\n[catalog]\nbackend = \"local\"\n");
                assert_eq!(
                    parse(&text).expect("parses").catalog,
                    CatalogStore::Local,
                    "\"local\" names the default"
                );
            }

            #[test]
            fn a_client_certificate_alone_is_enough_to_log_in() {
                let text = edited(
                    "password_file = \"/etc/vcon-server/pg.password\"\n",
                    "client_cert_file = \"/c.pem\"\nclient_key_file = \"/c.key\"\n",
                );
                let pg = postgres(&text);
                assert_eq!(
                    (pg.password_file, pg.client_cert_file, pg.client_key_file),
                    (None, Some("/c.pem".into()), Some("/c.key".into())),
                    "a certificate and its key, with no password"
                );
            }

            #[test]
            fn a_password_and_a_client_certificate_are_read_together() {
                let pg = postgres(&with(
                    "client_cert_file = \"/c.pem\"\nclient_key_file = \"/c.key\"",
                ));
                assert!(
                    pg.password_file.is_some() && pg.client_cert_file.is_some(),
                    "both ways of logging in, as pg_hba.conf's clientcert=verify-full asks"
                );
            }

            #[test]
            fn several_hosts_are_kept_in_their_order() {
                let text = edited(
                    "hosts = [\"db1.example.com\"]",
                    "hosts = [\"db2.example.com\", \"db1.example.com\", \"192.0.2.10\"]",
                );
                assert_eq!(
                    postgres(&text).hosts,
                    ["db2.example.com", "db1.example.com", "192.0.2.10"],
                    "tried in the order written"
                );
            }

            #[test]
            fn parquet_storage_with_a_postgres_catalog_is_read() {
                let text = edited(
                    "bucket = \"vcon\"\n",
                    "bucket = \"vcon\"\n\n[storage.parquet]\n",
                );
                let config = parse(&text).expect("parses");
                assert!(
                    config.storage.parquet.is_some()
                        && matches!(config.catalog, CatalogStore::Postgres(_)),
                    "Parquet files in the bucket and the catalogs in PostgreSQL"
                );
            }
        }

        mod failure {
            use super::*;

            /// `text` is refused with a message holding every one of `names`.
            fn refused_naming(text: &str, names: &[&str]) {
                let message = refused(text);
                for name in names {
                    assert!(message.contains(name), "names {name}: {message}");
                }
            }

            #[test]
            fn an_unknown_backend_is_named() {
                refused_naming(
                    &edited("backend = \"postgres\"", "backend = \"mysql\""),
                    &["catalog.backend", "\"mysql\""],
                );
            }

            #[test]
            fn a_postgres_table_without_the_postgres_backend_is_refused() {
                refused_naming(
                    &edited("backend = \"postgres\"", "backend = \"local\""),
                    &["catalog.postgres", "catalog.backend"],
                );
            }

            #[test]
            fn the_postgres_backend_without_its_table_is_refused() {
                let text = format!(
                    "{LEAST}\n[storage]\nobjects = \"s3\"\n[storage.s3]\nregion = \"us-east-1\"\n\
                     bucket = \"vcon\"\n[catalog]\nbackend = \"postgres\"\n"
                );
                refused_naming(&text, &["catalog.postgres"]);
            }

            #[test]
            fn a_postgres_catalog_with_local_objects_is_refused() {
                let text = format!(
                    "{LEAST}\n[catalog]\nbackend = \"postgres\"\n[catalog.postgres]\n\
                     hosts = [\"db1.example.com\"]\ndbname = \"vcon\"\nuser = \"vcon_app\"\n\
                     password_file = \"/p\"\n"
                );
                refused_naming(&text, &["catalog.backend", "storage.objects", "\"s3\""]);
            }

            #[test]
            fn an_empty_host_list_is_refused() {
                refused_naming(
                    &edited("hosts = [\"db1.example.com\"]", "hosts = []"),
                    &["catalog.postgres.hosts"],
                );
            }

            #[test]
            fn an_empty_host_name_is_refused() {
                refused_naming(
                    &edited("hosts = [\"db1.example.com\"]", "hosts = [\"\"]"),
                    &["catalog.postgres.hosts"],
                );
            }

            #[test]
            fn a_host_that_is_not_a_dns_name_is_refused() {
                for host in [
                    "db_1.example.com",
                    "db..example.com",
                    "-db.example.com",
                    "db .example.com",
                ] {
                    refused_naming(
                        &edited("\"db1.example.com\"", &format!("{host:?}")),
                        &["catalog.postgres.hosts", host],
                    );
                }
            }

            #[test]
            fn a_missing_host_list_is_named() {
                refused_naming(&edited("hosts = [\"db1.example.com\"]\n", ""), &["hosts"]);
            }

            #[test]
            fn a_port_of_zero_is_refused() {
                refused_naming(&with("port = 0"), &["catalog.postgres.port"]);
            }

            #[test]
            fn a_port_above_65535_is_refused() {
                refused_naming(&with("port = 65536"), &["catalog.postgres.port"]);
            }

            #[test]
            fn an_empty_database_name_is_refused() {
                refused_naming(
                    &edited("dbname = \"vcon\"", "dbname = \"\""),
                    &["catalog.postgres.dbname"],
                );
            }

            #[test]
            fn a_missing_database_name_is_named() {
                refused_naming(&edited("dbname = \"vcon\"\n", ""), &["dbname"]);
            }

            #[test]
            fn an_empty_user_is_refused() {
                refused_naming(
                    &edited("user = \"vcon_app\"", "user = \"\""),
                    &["catalog.postgres.user"],
                );
            }

            #[test]
            fn a_missing_user_is_named() {
                refused_naming(&edited("user = \"vcon_app\"\n", ""), &["user"]);
            }

            #[test]
            fn no_password_and_no_client_certificate_is_refused() {
                refused_naming(
                    &edited("password_file = \"/etc/vcon-server/pg.password\"\n", ""),
                    &[
                        "catalog.postgres.password_file",
                        "catalog.postgres.client_cert_file",
                    ],
                );
            }

            #[test]
            fn a_client_certificate_without_its_key_is_refused() {
                refused_naming(
                    &with("client_cert_file = \"/c.pem\""),
                    &["catalog.postgres.client_key_file"],
                );
            }

            #[test]
            fn a_client_key_without_its_certificate_is_refused() {
                refused_naming(
                    &with("client_key_file = \"/c.key\""),
                    &["catalog.postgres.client_cert_file"],
                );
            }

            #[test]
            fn an_empty_file_name_is_refused() {
                for (key, text) in [
                    ("ca_file", with("ca_file = \"\"")),
                    (
                        "password_file",
                        edited("\"/etc/vcon-server/pg.password\"", "\"\""),
                    ),
                    (
                        "client_cert_file",
                        with("client_cert_file = \"\"\nclient_key_file = \"/c.key\""),
                    ),
                    (
                        "client_key_file",
                        with("client_cert_file = \"/c.pem\"\nclient_key_file = \"\""),
                    ),
                ] {
                    refused_naming(&text, &[&format!("catalog.postgres.{key}")]);
                }
            }

            #[test]
            fn a_pool_of_zero_is_refused() {
                refused_naming(&with("pool = 0"), &["catalog.postgres.pool"]);
            }

            #[test]
            fn a_pool_too_large_to_count_is_refused() {
                refused_naming(&with("pool = 4294967296"), &["catalog.postgres.pool"]);
            }

            #[test]
            fn a_connect_timeout_of_zero_is_refused() {
                refused_naming(
                    &with("connect_timeout = 0"),
                    &["catalog.postgres.connect_timeout"],
                );
            }

            #[test]
            fn a_statement_timeout_of_zero_is_refused() {
                refused_naming(
                    &with("statement_timeout = 0"),
                    &["catalog.postgres.statement_timeout"],
                );
            }

            #[test]
            fn a_lock_timeout_of_zero_is_refused() {
                refused_naming(
                    &with("lock_timeout = 0"),
                    &["catalog.postgres.lock_timeout"],
                );
            }

            /// There is no sslmode: TLS with verification is the only mode.
            #[test]
            fn an_sslmode_key_is_an_unknown_key() {
                refused_naming(&with("sslmode = \"disable\""), &["`sslmode`"]);
            }

            #[test]
            fn an_unknown_key_in_the_catalog_table_is_named() {
                refused_naming(
                    &edited(
                        "backend = \"postgres\"",
                        "backend = \"postgres\"\nreplicas = 2",
                    ),
                    &["`replicas`"],
                );
            }
        }

        mod edge {
            use super::*;

            fn host_text(host: &str) -> String {
                edited("\"db1.example.com\"", &format!("{host:?}"))
            }

            #[test]
            fn ipv6_hosts_are_read() {
                for host in ["2001:db8::10", "::1", "2001:db8:0:0:0:0:0:10"] {
                    assert_eq!(
                        postgres(&host_text(host)).hosts,
                        [host],
                        "an IPv6 address is a host as written"
                    );
                }
            }

            #[test]
            fn a_bracketed_ipv6_host_is_refused_with_its_bare_form() {
                let message = refused(&host_text("[2001:db8::10]"));
                assert!(
                    message.contains("catalog.postgres.hosts")
                        && message.contains("\"2001:db8::10\""),
                    "names the key and the form to write: {message}"
                );
            }

            /// A Unix socket carries no TLS, and TLS is the only way the
            /// catalog is reached.
            #[test]
            fn a_unix_socket_host_is_refused_because_tls_is_required() {
                let message = refused(&host_text("/var/run/postgresql"));
                assert!(
                    message.contains("catalog.postgres.hosts")
                        && message.contains("Unix socket")
                        && message.contains("TLS"),
                    "says why: {message}"
                );
            }

            #[test]
            fn a_unicode_host_name_is_refused_with_a_pointer_to_its_ascii_form() {
                let message = refused(&host_text("bücher.example.com"));
                assert!(
                    message.contains("catalog.postgres.hosts") && message.contains("xn--"),
                    "an internationalized name is written in its xn-- form: {message}"
                );
            }

            #[test]
            fn an_ascii_form_of_a_unicode_host_name_is_read() {
                assert_eq!(
                    postgres(&host_text("xn--bcher-kva.example.com")).hosts,
                    ["xn--bcher-kva.example.com"],
                    "the xn-- form is a DNS name"
                );
            }

            #[test]
            fn unicode_database_and_role_names_are_read() {
                let text = edited("dbname = \"vcon\"", "dbname = \"vcon_数据\"")
                    .replace("user = \"vcon_app\"", "user = \"vcon_ünï\"");
                let pg = postgres(&text);
                assert_eq!(
                    (pg.dbname.as_str(), pg.user.as_str()),
                    ("vcon_数据", "vcon_ünï"),
                    "PostgreSQL names may hold any character but NUL"
                );
            }

            /// PostgreSQL cuts a longer name to 63 bytes, which would log in
            /// to another database or as another role than written.
            #[test]
            fn a_name_of_63_bytes_is_read_and_one_of_64_refused() {
                let at_limit = "数".repeat(21);
                assert_eq!(at_limit.len(), 63, "21 three-byte characters");
                for key in ["dbname", "user"] {
                    let line = |name: &str| format!("{key} = \"{name}\"");
                    let from = if key == "dbname" {
                        "dbname = \"vcon\""
                    } else {
                        "user = \"vcon_app\""
                    };
                    let pg = postgres(&edited(from, &line(&at_limit)));
                    let read = if key == "dbname" { pg.dbname } else { pg.user };
                    assert_eq!(read, at_limit, "{key}: 63 bytes is PostgreSQL's limit");
                    let message = refused(&edited(from, &line(&format!("{at_limit}a"))));
                    assert!(
                        message.contains(&format!("catalog.postgres.{key}"))
                            && message.contains("63 bytes"),
                        "{key}: {message}"
                    );
                }
            }

            #[test]
            fn a_nul_in_a_name_is_refused() {
                for (from, to, key) in [
                    ("dbname = \"vcon\"", "dbname = \"vc\\u0000on\"", "dbname"),
                    ("user = \"vcon_app\"", "user = \"vcon\\u0000\"", "user"),
                ] {
                    let message = refused(&edited(from, to));
                    assert!(
                        message.contains(&format!("catalog.postgres.{key}"))
                            && message.contains("NUL"),
                        "{key}: {message}"
                    );
                }
            }

            #[test]
            fn the_first_and_last_ports_are_read() {
                for port in [1, 65535] {
                    assert_eq!(
                        u64::from(postgres(&with(&format!("port = {port}"))).port),
                        port,
                        "every TCP port but 0"
                    );
                }
            }

            /// PostgreSQL holds both timeouts as milliseconds in a 32-bit
            /// integer: 2147483647 ms, so 2147483 whole seconds.
            #[test]
            fn timeouts_at_the_postgresql_limit_are_read_and_one_past_refused() {
                for key in ["statement_timeout", "lock_timeout"] {
                    let pg = postgres(&with(&format!("{key} = 2147483")));
                    let read = if key == "statement_timeout" {
                        pg.statement_timeout
                    } else {
                        pg.lock_timeout
                    };
                    assert_eq!(read, Duration::from_secs(2_147_483), "{key} at the limit");
                    let message = refused(&with(&format!("{key} = 2147484")));
                    assert!(
                        message.contains(&format!("catalog.postgres.{key}"))
                            && message.contains("2147483"),
                        "{key}: {message}"
                    );
                }
            }

            #[test]
            fn a_one_second_connect_timeout_is_read() {
                assert_eq!(
                    postgres(&with("connect_timeout = 1")).connect_timeout,
                    Duration::from_secs(1),
                    "the shortest timeout"
                );
            }

            #[test]
            fn the_same_host_twice_is_refused() {
                let message = refused(&edited(
                    "hosts = [\"db1.example.com\"]",
                    "hosts = [\"db1.example.com\", \"DB1.example.com\"]",
                ));
                assert!(
                    message.contains("catalog.postgres.hosts") && message.contains("twice"),
                    "a DNS name in any case is one host: {message}"
                );
            }

            #[test]
            fn a_dns_label_of_63_characters_is_read_and_one_of_64_refused() {
                let label = "a".repeat(63);
                let host = format!("{label}.example.com");
                assert_eq!(
                    postgres(&host_text(&host)).hosts,
                    [host],
                    "63 is the DNS limit"
                );
                let message = refused(&host_text(&format!("a{label}.example.com")));
                assert!(message.contains("catalog.postgres.hosts"), "{message}");
            }

            #[test]
            fn a_host_name_of_more_than_253_characters_is_refused() {
                let label = "a".repeat(63);
                let at_limit = format!("{label}.{label}.{label}.{}", "a".repeat(61));
                assert_eq!(at_limit.len(), 253, "four labels");
                assert_eq!(
                    postgres(&host_text(&at_limit)).hosts,
                    std::slice::from_ref(&at_limit),
                    "253 characters is the DNS limit"
                );
                let message = refused(&host_text(&format!("a{at_limit}")));
                assert!(message.contains("catalog.postgres.hosts"), "{message}");
            }

            #[test]
            fn a_port_written_in_a_host_is_refused_with_the_port_key_named() {
                let message = refused(&host_text("db1.example.com:5432"));
                assert!(
                    message.contains("catalog.postgres.hosts")
                        && message.contains("catalog.postgres.port"),
                    "the port has its own key: {message}"
                );
            }
        }
    }

    #[test]
    fn a_missing_file_is_named() {
        let error = load(Path::new("/nonexistent/vcon-server.toml")).expect_err("missing");
        assert!(error.0.contains("/nonexistent/vcon-server.toml"), "{error}");
    }
}