vcon-server 0.4.10

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
//! A rotation of the master key over the registry in PostgreSQL: what
//! must work, what must be refused, and the edges. The tests in
//! `on_a_server` need a PostgreSQL server.

use super::super::catalog::audit_key_statements;
use super::super::registry::{
    Column, KeyColumn, KeyValue, REMADE, membership_key, row_key, setting_key,
};
use crate::envelope::REGISTRY;
use crate::ids::{TenantId, UserId};

/// The names of the columns a rotation writes for `column`.
fn written(column: &Column) -> Vec<&'static str> {
    match *column {
        Column::Sealed(name) | Column::TenantKey(name) => vec![name],
        Column::SealedWithMac { sealed, mac } => vec![mac, sealed],
    }
}

/// The name of a column of a primary key.
fn key_name(column: &KeyColumn) -> &'static str {
    match *column {
        KeyColumn::Bytes(name) | KeyColumn::BytesOrNone(name) | KeyColumn::Text(name) => name,
    }
}

// What must work.

#[test]
fn the_registry_s_log_keys_are_read_and_sealed_again_with_no_tenant_named() {
    let (select, update) = audit_key_statements(REGISTRY);
    assert_eq!(
        select, "SELECT key_id, wrapped FROM vcon.admin_audit_key ORDER BY ordinal",
        "the oldest first, from the registry's table"
    );
    assert_eq!(
        update, "UPDATE vcon.admin_audit_key SET wrapped = $2 WHERE key_id = $1",
        "the sealed copy alone is written"
    );
}

#[test]
fn a_tenant_s_log_keys_are_read_and_sealed_again_for_that_tenant_alone() {
    let (select, update) = audit_key_statements(TenantId([4; 16]));
    assert_eq!(
        select, "SELECT key_id, wrapped FROM vcon.audit_key WHERE tenant = $1 ORDER BY ordinal",
        "the tenant's keys, oldest first"
    );
    assert_eq!(
        update, "UPDATE vcon.audit_key SET wrapped = $2 WHERE key_id = $1 AND tenant = $3",
        "the tenant is the last parameter, after the two the registry's statement takes"
    );
}

#[test]
fn a_membership_s_row_key_is_the_one_the_registry_seals_it_with() {
    let (user, tenant) = (UserId([7; 16]), TenantId([9; 16]));
    assert_eq!(
        row_key(&[
            KeyValue::Bytes(Some(user.0.to_vec())),
            KeyValue::Bytes(Some(tenant.0.to_vec())),
        ]),
        membership_key(user, Some(tenant)),
        "the user, then the tenant"
    );
}

#[test]
fn a_setting_s_row_key_is_the_one_the_registry_seals_it_with() {
    let tenant = TenantId([4; 16]);
    assert_eq!(
        row_key(&[
            KeyValue::Bytes(Some(tenant.0.to_vec())),
            KeyValue::Text("retention.days".to_owned()),
        ]),
        setting_key(tenant, "retention.days"),
        "the tenant, then the setting's name"
    );
}

#[test]
fn the_rotation_makes_six_tables_again_each_once() {
    let tables: Vec<&str> = REMADE.iter().map(|t| t.table).collect();
    assert_eq!(
        tables,
        [
            "tenant",
            "api_key",
            "user_account",
            "membership",
            "tenant_setting",
            "session"
        ],
        "the registry's tables with a sealed column"
    );
}

#[test]
fn a_table_is_read_by_its_key_then_the_columns_made_again() {
    let tenant = REMADE
        .iter()
        .find(|t| t.table == "tenant")
        .expect("the tenants");
    assert_eq!(
        tenant.fetch_text(),
        "SELECT id, name_mac, name_sealed, retention_sealed, metadata_sealed, role_cap_sealed, \
         wrapped_tenant_key, pending_wrapped_tenant_key FROM vcon.tenant",
        "the key first, then each column in the order it is written"
    );
}

#[test]
fn a_batch_is_written_in_one_statement_that_finds_each_row_by_its_key() {
    let session = REMADE
        .iter()
        .find(|t| t.table == "session")
        .expect("the sessions");
    assert_eq!(
        session.update_text(),
        "UPDATE vcon.session t SET scopes_sealed = n.scopes_sealed, amr_sealed = n.amr_sealed \
         FROM unnest($1::bytea[], $2::bytea[], $3::bytea[]) AS n(id_hash, scopes_sealed, amr_sealed) \
         WHERE t.id_hash = n.id_hash",
        "one array for each column, the key's first"
    );
}

// What must be refused.

#[test]
fn no_statement_of_the_rotation_s_tables_names_the_log() {
    for table in &REMADE {
        for text in [table.fetch_text(), table.update_text()] {
            assert!(
                !text.contains("audit"),
                "a record of the log is never written again: {text}"
            );
        }
    }
}

#[test]
fn no_batch_writes_a_column_of_a_row_s_key() {
    for table in &REMADE {
        let text = table.update_text();
        for key in table.key {
            let name = key_name(key);
            assert!(
                !text.contains(&format!(" {name} = n.{name},"))
                    && !text.contains(&format!(" {name} = n.{name} FROM")),
                "{}: {name} names the row and stays as it is: {text}",
                table.table
            );
        }
    }
}

// The edges.

#[test]
fn an_operator_s_row_key_is_the_user_alone() {
    let user = UserId([7; 16]);
    assert_eq!(
        row_key(&[
            KeyValue::Bytes(Some(user.0.to_vec())),
            KeyValue::Bytes(None)
        ]),
        membership_key(user, None),
        "a tenant that is NULL adds no byte"
    );
}

#[test]
fn a_row_key_of_no_value_is_empty_and_a_name_gives_its_utf8_bytes() {
    assert_eq!(row_key(&[]), Vec::<u8>::new(), "no value, no byte");
    assert_eq!(
        row_key(&[KeyValue::Text("\u{fc}".to_owned())]),
        [0xc3, 0xbc],
        "a name's bytes are its UTF-8"
    );
}

#[test]
fn a_key_column_that_may_be_null_is_matched_as_not_distinct() {
    let membership = REMADE
        .iter()
        .find(|t| t.table == "membership")
        .expect("the memberships");
    assert_eq!(
        membership.update_text(),
        "UPDATE vcon.membership t SET roles_sealed = n.roles_sealed \
         FROM unnest($1::bytea[], $2::bytea[], $3::bytea[]) AS n(user_id, tenant, roles_sealed) \
         WHERE t.user_id = n.user_id AND t.tenant IS NOT DISTINCT FROM n.tenant",
        "an operator's membership has no tenant, and = finds no NULL"
    );
}

#[test]
fn a_key_column_of_text_goes_as_an_array_of_text() {
    let setting = REMADE
        .iter()
        .find(|t| t.table == "tenant_setting")
        .expect("the settings");
    assert_eq!(
        setting.update_text(),
        "UPDATE vcon.tenant_setting t SET value_sealed = n.value_sealed \
         FROM unnest($1::bytea[], $2::text[], $3::bytea[]) AS n(tenant, name, value_sealed) \
         WHERE t.tenant = n.tenant AND t.name = n.name",
        "a setting's name is text"
    );
}

#[test]
fn only_the_tenants_hold_keys_wrapped_under_the_master_key() {
    for table in &REMADE {
        assert!(!table.key.is_empty(), "{}: a row has a key", table.table);
        assert!(
            !table.columns.is_empty(),
            "{}: a table with no column is not in the list",
            table.table
        );
        let wraps = table
            .columns
            .iter()
            .filter(|c| matches!(c, Column::TenantKey(_)))
            .count();
        if table.table == "tenant" {
            assert_eq!(wraps, 2, "the tenant key and the pending one");
            assert_eq!(
                table.key,
                [KeyColumn::Bytes("id")],
                "the tenant's id is the wrapped key's context"
            );
        } else {
            assert_eq!(wraps, 0, "{} holds no tenant key", table.table);
        }
    }
}

#[test]
fn no_column_is_written_twice_in_a_table() {
    for table in &REMADE {
        let mut names: Vec<&str> = table.columns.iter().flat_map(written).collect();
        let all = names.len();
        names.sort_unstable();
        names.dedup();
        assert_eq!(names.len(), all, "{}: each column once", table.table);
    }
}

#[cfg(feature = "backend-tests")]
mod on_a_server {
    use std::sync::{Arc, mpsc};
    use std::time::Duration;

    use time::OffsetDateTime;

    use super::super::super::catalog::Database;
    use super::super::super::registry::{PgRegistry, REMADE, Remade};
    use super::super::catalog_tests::on_a_server::{database, tenant_catalog};
    use super::super::registry_tests::on_a_server::{
        as_owner, exported, hex, log_keys, named, wait,
    };
    use super::super::schema_tests::live::{Db, in_scope};
    use super::{key_name, written};
    use crate::audit::{Action, Actor, Entry};
    use crate::backend::{
        CommitError, KeyRow, MembershipRow, Registry, RegistryTx, SessionRow, TenantRow, UserRow,
    };
    use crate::clock::nanos;
    use crate::envelope::{self, Context, Key};
    use crate::ids::{KeyId, TenantId, UserId};
    use crate::tenants::TenantError;

    const T0: i64 = 1_792_756_800;

    /// The rows a rotation reads and writes in one statement, in the
    /// tests that do not set it.
    const BATCH: i64 = 1_000;

    /// Every table that holds a row of the registry.
    const TABLES: [&str; 10] = [
        "tenant",
        "api_key",
        "user_account",
        "membership",
        "tenant_setting",
        "session",
        "master_key_use",
        "registry_key",
        "admin_audit_key",
        "admin_audit",
    ];

    const ISSUER: &str = "https://issuer.example.com";

    /// A migrated database of the test's own and two master keys: the
    /// one the registry is under, and the one a rotation moves it to.
    struct World {
        db: Db,
        database: Arc<Database>,
        old: Key,
        new: Key,
    }

    fn world() -> World {
        let (db, database) = database();
        World {
            db,
            database,
            old: Key::generate().expect("a key"),
            new: Key::generate().expect("a key"),
        }
    }

    fn catalog_key(master: &Key) -> Key {
        envelope::catalog_key(master).expect("the catalog key")
    }

    /// The registry, opened under `master`'s catalog key.
    fn open(w: &World, master: &Key) -> Result<PgRegistry, TenantError> {
        PgRegistry::open(w.database.clone(), &catalog_key(master))
    }

    /// The time the rotations of these tests are made at.
    fn now() -> OffsetDateTime {
        OffsetDateTime::from_unix_timestamp(T0 + 3_600).expect("a time")
    }

    fn rotate(w: &World) -> Result<(), TenantError> {
        PgRegistry::rekey_master(&w.database, &w.old, &w.new, now(), BATCH)
    }

    fn tenant_id(n: u8) -> TenantId {
        TenantId([n; 16])
    }

    fn user_id(n: u8) -> UserId {
        UserId([n; 16])
    }

    /// A bytea literal of `bytes`.
    fn lit(bytes: &[u8]) -> String {
        format!("'\\x{}'", hex(bytes))
    }

    /// Every row of one table of the registry, as one hash.
    fn table(db: &Db, name: &str) -> String {
        as_owner(
            db,
            &format!(
                "SELECT coalesce(md5(string_agg(t::text, ';' ORDER BY t::text)), 'none') \
                 FROM vcon.{name} t"
            ),
        )
        .concat()
    }

    /// Every row of the registry, by table.
    fn state(db: &Db) -> Vec<(&'static str, String)> {
        TABLES.iter().map(|name| (*name, table(db, name))).collect()
    }

    /// The names of the tables whose rows differ between two states.
    fn changed(
        before: &[(&'static str, String)],
        after: &[(&'static str, String)],
    ) -> Vec<&'static str> {
        before
            .iter()
            .zip(after)
            .filter(|(b, a)| b.1 != a.1)
            .map(|(b, _)| b.0)
            .collect()
    }

    fn write<T>(r: &PgRegistry, f: impl FnOnce(&mut dyn RegistryTx) -> T) -> T {
        let mut tx = r.immediate().expect("a write begins");
        let out = f(tx.as_mut());
        tx.commit().expect("the write commits");
        out
    }

    fn about(target: &str) -> Entry {
        Entry {
            actor: Actor::Cli,
            action: Action::Read,
            target: Some(target.to_owned()),
            version: None,
            status: 200,
        }
    }

    /// A tenant whose key, and with `pending` a second one, is wrapped
    /// under `master`; the ids of the two keys.
    fn tenant(
        master: &Key,
        n: u8,
        name: &str,
        full: bool,
        pending: bool,
    ) -> (TenantRow, KeyId, Option<KeyId>) {
        let id = tenant_id(n);
        let ctx = Context::Tenant { tenant: id };
        let key = Key::generate().expect("a key");
        let next = pending.then(|| Key::generate().expect("a key"));
        let text = |value: &str| full.then(|| value.to_owned());
        let row = TenantRow {
            id,
            name: name.to_owned(),
            retention: text("P30D-retention"),
            metadata: text("metadata-level"),
            role_cap: text("role-cap-text"),
            created_at: T0,
            tenant_key_id: key.id(),
            wrapped: envelope::wrap(master, &ctx, &key),
            pending: next.as_ref().map(|k| envelope::wrap(master, &ctx, k)),
            key_since: T0,
        };
        (row, key.id(), next.map(|k| k.id()))
    }

    fn user(n: u8, label: &str, subject: Option<&str>, deactivated: bool) -> UserRow {
        UserRow {
            id: user_id(n),
            label: label.to_owned(),
            identity: subject.map(|s| (ISSUER.to_owned(), s.to_owned())),
            created_at: T0,
            deactivated_at: deactivated.then_some(T0 + 5),
        }
    }

    fn session(n: u8, user: u8, tenant: Option<u8>) -> SessionRow {
        SessionRow {
            id_hash: vec![n; 32],
            user: user_id(user),
            tenant: tenant.map(tenant_id),
            scopes: "scopes-of-the-session".to_owned(),
            created_at: T0,
            last_seen_at: T0 + 1,
            idle_expires_at: T0 + 900,
            expires_at: T0 + 86_400,
            idle_seconds: 900,
            amr: "authentication-methods".to_owned(),
            csrf_hash: vec![n ^ 0xff; 32],
        }
    }

    /// The tenants' keys a filled registry holds: the tenant, its key's
    /// id, and the pending key's.
    type TenantKeys = Vec<(TenantId, KeyId, Option<KeyId>)>;

    /// Fills the registry under `master` with rows of every table: three
    /// tenants (the first with a pending key, the second with no optional
    /// text), two API keys, three users (one with no identity, one
    /// deactivated that shares the first's), three memberships (one an
    /// operator's), three settings, two sessions and two records.
    fn fill_under(w: &World, master: &Key) -> TenantKeys {
        let registry = open(w, master).expect("the registry opens");
        let tenants = [
            tenant(master, 1, "alpha-tenant-name", true, true),
            tenant(master, 2, "beta-tenant-name", false, false),
            tenant(master, 3, "gamma-tenant-name", true, false),
        ];
        write(&registry, |tx| {
            for (row, _, _) in &tenants {
                tx.insert_tenant(row).expect("a tenant");
            }
            for (n, tenant) in [(1, None), (2, Some(tenant_id(1)))] {
                tx.insert_key(&KeyRow {
                    id: KeyId([n; 8]),
                    tenant,
                    token_hash: vec![n; 32],
                    scopes: "scopes-of-the-key".to_owned(),
                    label: format!("label-of-key-{n}"),
                    created_at: T0,
                    revoked_at: None,
                    expires_at: Some(T0 + 99),
                    last_used_at: None,
                })
                .expect("a key");
            }
            tx.insert_user(&user(3, "user-three-label", Some("subject-one"), true))
                .expect("a user");
            tx.insert_user(&user(1, "user-one-label", Some("subject-one"), false))
                .expect("a user");
            tx.insert_user(&user(2, "user-two-label", None, false))
                .expect("a user");
            for (user, tenant) in [(1, Some(1)), (1, None), (2, Some(2))] {
                tx.insert_membership(&MembershipRow {
                    user: user_id(user),
                    tenant: tenant.map(tenant_id),
                    roles: format!("roles-of-{user}"),
                    granted_at: T0,
                })
                .expect("a membership");
            }
            tx.put_setting(tenant_id(1), "retention.days", "thirty-days-value")
                .expect("a setting");
            tx.put_setting(tenant_id(1), "n\u{e4}me", "second-value")
                .expect("a setting");
            tx.put_setting(tenant_id(2), "retention.days", "other-value")
                .expect("a setting");
            tx.insert_session(&session(0xa1, 1, Some(1)))
                .expect("a session");
            tx.insert_session(&session(0xa2, 1, None))
                .expect("a session");
            let at = OffsetDateTime::from_unix_timestamp(T0).expect("a time");
            tx.record(&about("target-one"), at).expect("a record");
            tx.record(&about("target-two"), at).expect("a record");
        });
        tenants.iter().map(|(row, k, p)| (row.id, *k, *p)).collect()
    }

    fn fill(w: &World) -> TenantKeys {
        fill_under(w, &w.old)
    }

    /// What the registry reads of every table, the tenants' wrapped keys
    /// left out: a rotation wraps them again.
    #[derive(Debug, PartialEq)]
    struct Rows {
        tenants: Vec<TenantRow>,
        keys: Vec<Option<KeyRow>>,
        users: Vec<UserRow>,
        memberships: Vec<MembershipRow>,
        settings: Vec<Vec<(String, String)>>,
        sessions: Vec<Option<SessionRow>>,
    }

    fn read(registry: &PgRegistry) -> Rows {
        let mut tx = registry.snapshot().expect("a read begins");
        let mut tenants = tx.tenants().expect("the tenants");
        tenants.sort_by_key(|t| t.id.0);
        for t in &mut tenants {
            t.wrapped.clear();
            t.pending = t.pending.as_ref().map(|_| Vec::new());
        }
        let keys = [1, 2]
            .iter()
            .map(|n| tx.key(KeyId([*n; 8])).expect("a key"))
            .collect();
        let mut users = tx.users().expect("the users");
        users.sort_by_key(|u| u.id.0);
        let mut memberships = tx.memberships().expect("the memberships");
        memberships.sort_by_key(|m| (m.user.0, m.tenant.map(|t| t.0)));
        let settings = [1, 2, 3]
            .iter()
            .map(|n| {
                let mut settings = tx.settings(tenant_id(*n)).expect("the settings");
                settings.sort();
                settings
            })
            .collect();
        let sessions = [0xa1_u8, 0xa2]
            .iter()
            .map(|n| tx.session(&[*n; 32]).expect("a session"))
            .collect();
        tx.commit().expect("the read ends");
        Rows {
            tenants,
            keys,
            users,
            memberships,
            settings,
            sessions,
        }
    }

    /// What a filled registry reads, with the registry still under the
    /// old master key.
    fn filled(w: &World) -> Rows {
        read(&open(w, &w.old).expect("the registry opens under the old key"))
    }

    /// The wrapped keys of each tenant as the database holds them, in
    /// the order of the tenants' ids.
    fn wraps(w: &World, master: &Key) -> Vec<(TenantId, Vec<u8>, Option<Vec<u8>>)> {
        let registry = open(w, master).expect("the registry opens");
        let mut tx = registry.snapshot().expect("a read begins");
        let mut tenants = tx.tenants().expect("the tenants");
        tx.commit().expect("the read ends");
        tenants.sort_by_key(|t| t.id.0);
        tenants
            .into_iter()
            .map(|t| (t.id, t.wrapped, t.pending))
            .collect()
    }

    /// Each value of `column` in `spec`'s table, in hex, in the order of
    /// the rows' keys; a NULL is an empty text.
    fn values(db: &Db, spec: &Remade, column: &str) -> Vec<String> {
        let order: Vec<&str> = spec.key.iter().map(key_name).collect();
        as_owner(
            db,
            &format!(
                "SELECT coalesce(encode({column}, 'hex'), '') FROM vcon.{} ORDER BY {}",
                spec.table,
                order.join(", ")
            ),
        )
    }

    /// Changes the last byte of one sealed value.
    fn flip(db: &Db, table: &str, column: &str, row: &str) {
        let last = format!("octet_length({column}) - 1");
        let rows = as_owner(
            db,
            &format!(
                "UPDATE vcon.{table} SET {column} = \
                 set_byte({column}, {last}, get_byte({column}, {last}) # 1) \
                 WHERE {row} RETURNING 1"
            ),
        );
        assert_eq!(rows.len(), 1, "{table}.{column}: one row is changed");
    }

    /// Each record of the registry's log, as the database holds it, in
    /// the order they were written.
    fn records(db: &Db) -> Vec<String> {
        as_owner(db, "SELECT a::text FROM vcon.admin_audit a ORDER BY seq")
    }

    /// The rows of the master keys' use: the key's id in hex, its wraps
    /// and the time it has been in use since.
    fn uses(db: &Db) -> Vec<String> {
        as_owner(
            db,
            "SELECT encode(master_key_id, 'hex'), wraps, since FROM vcon.master_key_use \
             ORDER BY master_key_id",
        )
    }

    // What must work.

    #[test]
    fn the_registry_opens_under_the_new_master_key_and_not_under_the_old() {
        let w = world();
        fill(&w);
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        assert!(
            open(&w, &w.new).is_ok(),
            "the registry opens under the new master key"
        );
        assert_eq!(
            open(&w, &w.old).err(),
            Some(TenantError::WrongKey),
            "and is refused under the old one"
        );
    }

    #[test]
    fn every_row_reads_the_same_under_the_new_master_key() {
        let w = world();
        fill(&w);
        let before = filled(&w);
        assert_eq!(before.tenants.len(), 3, "the tenants were written");
        assert_eq!(before.users.len(), 3, "the users were written");
        assert_eq!(before.memberships.len(), 3, "the memberships were written");
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        let after = read(&open(&w, &w.new).expect("opens under the new key"));
        assert_eq!(after, before, "what a person typed is what it was");
    }

    #[test]
    fn a_tenant_is_found_by_its_name_under_the_new_master_key() {
        let w = world();
        fill(&w);
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        let registry = open(&w, &w.new).expect("opens under the new key");
        let mut tx = registry.snapshot().expect("a read begins");
        assert_eq!(
            tx.name_taken("alpha-tenant-name"),
            Ok(true),
            "the name's HMAC was made again under the new index key"
        );
        assert_eq!(
            tx.name_taken("no-such-tenant"),
            Ok(false),
            "a name no tenant has is not found"
        );
    }

    #[test]
    fn a_user_is_found_by_its_identity_under_the_new_master_key() {
        let w = world();
        fill(&w);
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        let registry = open(&w, &w.new).expect("opens under the new key");
        let mut tx = registry.snapshot().expect("a read begins");
        assert_eq!(
            tx.identity_taken(ISSUER, "subject-one"),
            Ok(true),
            "the identity's HMAC was made again under the new index key"
        );
        assert_eq!(
            tx.identity_taken(ISSUER, "subject-nine"),
            Ok(false),
            "an identity no user has is not found"
        );
    }

    #[test]
    fn each_tenant_key_opens_under_the_new_master_key() {
        let w = world();
        let keys = fill(&w);
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        let wraps = wraps(&w, &w.new);
        assert_eq!(wraps.len(), 3, "three tenants");
        for ((tenant, wrapped, _), (id, key, _)) in wraps.iter().zip(&keys) {
            assert_eq!(tenant, id, "the tenants in the order of their ids");
            let opened = envelope::unwrap(&w.new, &Context::Tenant { tenant: *tenant }, wrapped)
                .expect("the tenant key opens under the new master key");
            assert_eq!(opened.id(), *key, "and is the key the tenant had");
        }
    }

    #[test]
    fn a_pending_tenant_key_opens_under_the_new_master_key() {
        let w = world();
        let keys = fill(&w);
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        let wraps = wraps(&w, &w.new);
        let (tenant, _, pending) = &wraps[0];
        let pending = pending
            .as_ref()
            .expect("the first tenant has a pending key");
        let opened = envelope::unwrap(&w.new, &Context::Tenant { tenant: *tenant }, pending)
            .expect("the pending key opens under the new master key");
        assert_eq!(
            Some(opened.id()),
            keys[0].2,
            "and is the key that was pending"
        );
    }

    #[test]
    fn no_tenant_key_opens_under_the_old_master_key_after() {
        let w = world();
        fill(&w);
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        for (tenant, wrapped, pending) in wraps(&w, &w.new) {
            let ctx = Context::Tenant { tenant };
            assert!(
                envelope::unwrap(&w.old, &ctx, &wrapped).is_err(),
                "the old master key opens no tenant key"
            );
            if let Some(pending) = pending {
                assert!(
                    envelope::unwrap(&w.old, &ctx, &pending).is_err(),
                    "and no pending one"
                );
            }
        }
    }

    #[test]
    fn the_new_master_key_s_use_counts_the_keys_it_wrapped() {
        let w = world();
        fill(&w);
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        assert_eq!(
            uses(&w.db),
            [format!("{} 4 {}", hex(&w.new.id().0), nanos(now()))],
            "three tenant keys and one pending, since the rotation"
        );
        let registry = open(&w, &w.new).expect("opens under the new key");
        let mut tx = registry.snapshot().expect("a read begins");
        assert_eq!(tx.master_wraps(w.new.id()), Ok(4), "as the registry reads");
        assert_eq!(
            tx.master_since(w.new.id()),
            Ok(Some(nanos(now()))),
            "the new key is in use since the rotation"
        );
    }

    #[test]
    fn the_database_names_the_new_catalog_key() {
        let w = world();
        fill(&w);
        assert_eq!(
            named(&w.db),
            [hex(&catalog_key(&w.old).id().0)],
            "the old key's before"
        );
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        assert_eq!(
            named(&w.db),
            [hex(&catalog_key(&w.new).id().0)],
            "the one row names the new master key's catalog key"
        );
    }

    #[test]
    fn the_log_keeps_its_key_and_the_key_is_sealed_again() {
        let w = world();
        fill(&w);
        let before = log_keys(&w.db);
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        let after = log_keys(&w.db);
        assert_eq!(before.len(), 1, "one key before");
        assert_eq!(after.len(), 1, "one key after");
        let (b, a): (Vec<&str>, Vec<&str>) = (
            before[0].split(' ').collect(),
            after[0].split(' ').collect(),
        );
        assert_eq!(a[..2], b[..2], "the ordinal and the id stay");
        assert_ne!(a[2], b[2], "the sealed copy is made again");
    }

    #[test]
    fn the_records_written_before_read_after() {
        let w = world();
        fill(&w);
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        let registry = open(&w, &w.new).expect("opens under the new key");
        let text = exported(&registry).expect("the log is read");
        assert!(text.contains("target-one"), "the first target: {text}");
        assert!(text.contains("target-two"), "the second target: {text}");
    }

    #[test]
    fn no_record_of_the_log_is_changed_and_one_is_added() {
        let w = world();
        fill(&w);
        let before = records(&w.db);
        assert_eq!(before.len(), 2, "two records before");
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        let after = records(&w.db);
        assert_eq!(after.len(), 3, "one record more");
        assert_eq!(
            after[..2],
            before[..],
            "the records that were there are byte for byte what they were"
        );
    }

    #[test]
    fn the_rotation_is_recorded_under_the_log_s_key() {
        let w = world();
        fill(&w);
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        let key = as_owner(
            &w.db,
            "SELECT encode(key_id, 'hex') FROM vcon.admin_audit_key",
        )
        .concat();
        assert_eq!(
            as_owner(
                &w.db,
                "SELECT actor, action, status, count, target_sealed IS NULL, \
                 encode(key_id, 'hex') FROM vcon.admin_audit ORDER BY seq DESC LIMIT 1",
            ),
            [format!("cli rekey_master 200 1 t {key}")],
            "the command line rotated the master key"
        );
        let registry = open(&w, &w.new).expect("opens under the new key");
        let mut tx = registry.snapshot().expect("a read begins");
        assert_eq!(
            tx.record_times(Action::RekeyMaster, 200).map(|t| t.len()),
            Ok(1),
            "one record of the rotation"
        );
    }

    #[test]
    fn a_second_run_changes_nothing() {
        let w = world();
        fill(&w);
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        let before = state(&w.db);
        assert_eq!(
            rotate(&w),
            Ok(()),
            "a rotation that was made is asked for again"
        );
        assert_eq!(
            changed(&before, &state(&w.db)),
            Vec::<&str>::new(),
            "and no row is written, nor a second record"
        );
    }

    #[test]
    fn a_rotation_after_a_rotation_moves_the_registry_again() {
        let w = world();
        fill(&w);
        let before = filled(&w);
        assert_eq!(rotate(&w), Ok(()), "the first rotation is made");
        let third = Key::generate().expect("a key");
        assert_eq!(
            PgRegistry::rekey_master(&w.database, &w.new, &third, now(), BATCH),
            Ok(()),
            "and the second"
        );
        let registry = open(&w, &third).expect("opens under the third key");
        assert_eq!(read(&registry), before, "the rows read as they did");
        let mut tx = registry.snapshot().expect("a read begins");
        assert_eq!(
            tx.record_times(Action::RekeyMaster, 200).map(|t| t.len()),
            Ok(2),
            "each rotation has its record"
        );
        assert_eq!(
            open(&w, &w.new).err(),
            Some(TenantError::WrongKey),
            "the second key is the old one now"
        );
    }

    #[test]
    fn one_row_in_a_statement_gives_what_a_thousand_give() {
        let w = world();
        fill(&w);
        let before = filled(&w);
        assert_eq!(
            PgRegistry::rekey_master(&w.database, &w.old, &w.new, now(), 1),
            Ok(()),
            "a rotation that reads one row at a time"
        );
        let registry = open(&w, &w.new).expect("opens under the new key");
        assert_eq!(read(&registry), before, "every row was moved");
        assert_eq!(
            uses(&w.db),
            [format!("{} 4 {}", hex(&w.new.id().0), nanos(now()))],
            "and every tenant key counted"
        );
    }

    #[test]
    fn every_sealed_value_hmac_and_wrapped_key_is_written_again() {
        let w = world();
        fill(&w);
        let read_all = |db: &Db| -> Vec<(String, Vec<String>)> {
            REMADE
                .iter()
                .flat_map(|spec| {
                    spec.columns.iter().flat_map(written).map(move |column| {
                        (format!("{}.{column}", spec.table), values(db, spec, column))
                    })
                })
                .collect()
        };
        let before = read_all(&w.db);
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        let after = read_all(&w.db);
        assert_eq!(before.len(), 16, "sixteen columns in six tables");
        for ((name, was), (_, is)) in before.iter().zip(&after) {
            assert_eq!(was.len(), is.len(), "{name}: the same rows");
            assert!(!was.is_empty(), "{name}: the table has rows");
            for (was, is) in was.iter().zip(is) {
                assert_eq!(
                    was.is_empty(),
                    is.is_empty(),
                    "{name}: a NULL stays one, a value stays one"
                );
                assert!(
                    was.is_empty() || was != is,
                    "{name}: a value under the old key is not kept"
                );
            }
        }
    }

    #[test]
    fn what_is_not_sealed_is_as_it_was() {
        let w = world();
        fill(&w);
        let rest = |db: &Db| -> Vec<String> {
            REMADE
                .iter()
                .map(|spec| {
                    let made: Vec<&str> = spec.columns.iter().flat_map(written).collect();
                    let kept: Vec<String> = as_owner(
                        db,
                        &format!(
                            "SELECT column_name::text FROM information_schema.columns \
                             WHERE table_schema = 'vcon' AND table_name = '{}' \
                             ORDER BY ordinal_position",
                            spec.table
                        ),
                    )
                    .into_iter()
                    .filter(|c| !made.contains(&c.as_str()))
                    .collect();
                    assert!(!kept.is_empty(), "{}: a table has its key", spec.table);
                    let row = format!("ROW({})::text", kept.join(", "));
                    as_owner(
                        db,
                        &format!(
                            "SELECT md5(string_agg({row}, ';' ORDER BY {row})) FROM vcon.{}",
                            spec.table
                        ),
                    )
                    .concat()
                })
                .collect()
        };
        let before = rest(&w.db);
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        assert_eq!(
            rest(&w.db),
            before,
            "ids, times, hashes and names are not written"
        );
    }

    #[test]
    fn the_registry_takes_writes_after() {
        let w = world();
        fill(&w);
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        let registry = open(&w, &w.new).expect("opens under the new key");
        let (row, _, _) = tenant(&w.new, 9, "delta-tenant-name", true, false);
        write(&registry, |tx| {
            tx.insert_tenant(&row).expect("a tenant is added");
            tx.add_master_wrap(w.new.id(), nanos(now()))
                .expect("the wrap is counted");
            tx.record(&about("target-after"), now()).expect("a record");
        });
        let mut tx = registry.snapshot().expect("a read begins");
        assert_eq!(tx.tenants().map(|t| t.len()), Ok(4), "four tenants now");
        assert_eq!(
            tx.master_wraps(w.new.id()),
            Ok(5),
            "the count goes on from the rotation's"
        );
        drop(tx);
        let text = exported(&registry).expect("the log is read");
        assert!(text.contains("target-after"), "the new record: {text}");
        let mut again = registry.immediate().expect("a write begins");
        let (twin, _, _) = tenant(&w.new, 10, "alpha-tenant-name", true, false);
        assert!(
            again.insert_tenant(&twin).is_err(),
            "a name a tenant has is still one name, under the new HMAC"
        );
    }

    #[test]
    fn the_database_holds_no_registry_text_in_the_clear_after() {
        let w = world();
        fill(&w);
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        for spec in &REMADE {
            let rows = as_owner(&w.db, &format!("SELECT t::text FROM vcon.{} t", spec.table));
            assert!(!rows.is_empty(), "{}: the table has rows", spec.table);
            let all = rows.join("\n");
            for text in [
                "alpha-tenant-name",
                "P30D-retention",
                "metadata-level",
                "role-cap-text",
                "scopes-of-the-key",
                "label-of-key-1",
                "user-one-label",
                "subject-one",
                "roles-of-1",
                "thirty-days-value",
                "scopes-of-the-session",
                "authentication-methods",
            ] {
                assert!(
                    !all.contains(text) && !all.contains(&hex(text.as_bytes())),
                    "{}: {text} is not in the clear",
                    spec.table
                );
            }
        }
    }

    #[test]
    fn the_time_of_the_rotation_is_the_one_given() {
        let w = world();
        fill(&w);
        let at = OffsetDateTime::from_unix_timestamp(T0 + 86_400).expect("a time");
        assert_eq!(
            PgRegistry::rekey_master(&w.database, &w.old, &w.new, at, BATCH),
            Ok(()),
            "a rotation a day later"
        );
        assert_eq!(
            uses(&w.db),
            [format!("{} 4 {}", hex(&w.new.id().0), nanos(at))],
            "the new key is in use since the time given, not the clock's"
        );
    }

    // What must be refused.

    #[test]
    fn a_registry_under_neither_key_is_refused_and_left_as_it_is() {
        let w = world();
        fill(&w);
        let before = state(&w.db);
        let stranger = Key::generate().expect("a key");
        assert_eq!(
            PgRegistry::rekey_master(&w.database, &stranger, &w.new, now(), BATCH),
            Err(TenantError::WrongKey),
            "the registry is under neither key"
        );
        assert_eq!(
            changed(&before, &state(&w.db)),
            Vec::<&str>::new(),
            "no row is written"
        );
        assert!(open(&w, &w.old).is_ok(), "it opens under its key as before");
    }

    #[test]
    fn a_batch_of_no_row_is_refused() {
        let w = world();
        fill(&w);
        let before = state(&w.db);
        assert_eq!(
            PgRegistry::rekey_master(&w.database, &w.old, &w.new, now(), 0),
            Err(TenantError::Io),
            "a statement must read a row"
        );
        assert_eq!(
            changed(&before, &state(&w.db)),
            Vec::<&str>::new(),
            "no row is written"
        );
        assert_eq!(rotate(&w), Ok(()), "with a batch of rows it is made");
    }

    #[test]
    fn a_batch_below_no_row_is_refused() {
        let w = world();
        fill(&w);
        let before = state(&w.db);
        for batch in [-1, i64::MIN] {
            assert_eq!(
                PgRegistry::rekey_master(&w.database, &w.old, &w.new, now(), batch),
                Err(TenantError::Io),
                "a batch of {batch} rows"
            );
        }
        assert_eq!(
            changed(&before, &state(&w.db)),
            Vec::<&str>::new(),
            "no row is written"
        );
        assert_eq!(rotate(&w), Ok(()), "with a batch of rows it is made");
    }

    #[test]
    fn a_batch_of_no_row_is_refused_before_a_database_is_opened_for_the_first_time() {
        let w = world();
        assert_eq!(
            PgRegistry::rekey_master(&w.database, &w.old, &w.new, now(), 0),
            Err(TenantError::Io),
            "a statement must read a row"
        );
        assert_eq!(
            named(&w.db),
            Vec::<String>::new(),
            "the refusal comes before the first open, which would name the old key"
        );
        assert_eq!(
            log_keys(&w.db),
            Vec::<String>::new(),
            "and would make the log's key"
        );
    }

    #[test]
    fn an_open_registry_is_not_rotated_with_a_batch_of_no_row() {
        let w = world();
        fill(&w);
        let registry = open(&w, &w.old).expect("the registry opens");
        let before = state(&w.db);
        assert_eq!(
            registry.rotate(&w.old, &w.new, now(), 0),
            Err(TenantError::Io),
            "a cursor that reads no row would move the key's id and no value"
        );
        assert_eq!(
            changed(&before, &state(&w.db)),
            Vec::<&str>::new(),
            "no row is written"
        );
        assert!(open(&w, &w.old).is_ok(), "it opens under its key as before");
    }

    /// A rotation over a registry in which one value of `table.column`,
    /// in the row `row`, does not open: it stops, nothing of it is kept,
    /// and once the value is what it was the rotation is made.
    fn a_value_that_does_not_open_stops_the_rotation(table: &str, column: &str, row: &str) {
        let w = world();
        fill(&w);
        let rows = filled(&w);
        flip(&w.db, table, column, row);
        let before = state(&w.db);
        assert_eq!(
            rotate(&w),
            Err(TenantError::Io),
            "{table}.{column} does not open under the old key"
        );
        assert_eq!(
            changed(&before, &state(&w.db)),
            Vec::<&str>::new(),
            "nothing is kept of a rotation that stopped at {table}.{column}"
        );
        assert_eq!(
            named(&w.db),
            [hex(&catalog_key(&w.old).id().0)],
            "the database still names the old key"
        );
        flip(&w.db, table, column, row);
        assert_eq!(
            rotate(&w),
            Ok(()),
            "with the value as it was, the rotation is made"
        );
        assert_eq!(
            read(&open(&w, &w.new).expect("opens under the new key")),
            rows,
            "and every row reads as it did"
        );
    }

    fn tenant_is(n: u8) -> String {
        format!("id = {}", lit(&[n; 16]))
    }

    #[test]
    fn a_tenant_name_that_does_not_open_stops_the_rotation() {
        a_value_that_does_not_open_stops_the_rotation("tenant", "name_sealed", &tenant_is(3));
    }

    #[test]
    fn a_retention_that_does_not_open_stops_the_rotation() {
        a_value_that_does_not_open_stops_the_rotation("tenant", "retention_sealed", &tenant_is(1));
    }

    #[test]
    fn a_metadata_level_that_does_not_open_stops_the_rotation() {
        a_value_that_does_not_open_stops_the_rotation("tenant", "metadata_sealed", &tenant_is(3));
    }

    #[test]
    fn a_role_cap_that_does_not_open_stops_the_rotation() {
        a_value_that_does_not_open_stops_the_rotation("tenant", "role_cap_sealed", &tenant_is(1));
    }

    #[test]
    fn a_tenant_key_that_does_not_open_stops_the_rotation() {
        a_value_that_does_not_open_stops_the_rotation(
            "tenant",
            "wrapped_tenant_key",
            &tenant_is(2),
        );
    }

    #[test]
    fn a_pending_tenant_key_that_does_not_open_stops_the_rotation() {
        a_value_that_does_not_open_stops_the_rotation(
            "tenant",
            "pending_wrapped_tenant_key",
            &tenant_is(1),
        );
    }

    #[test]
    fn an_api_key_s_scopes_that_do_not_open_stop_the_rotation() {
        let row = format!("id = {}", lit(&[2; 8]));
        a_value_that_does_not_open_stops_the_rotation("api_key", "scopes_sealed", &row);
    }

    #[test]
    fn an_api_key_s_label_that_does_not_open_stops_the_rotation() {
        let row = format!("id = {}", lit(&[1; 8]));
        a_value_that_does_not_open_stops_the_rotation("api_key", "label_sealed", &row);
    }

    #[test]
    fn a_user_s_label_that_does_not_open_stops_the_rotation() {
        a_value_that_does_not_open_stops_the_rotation(
            "user_account",
            "label_sealed",
            &tenant_is(2),
        );
    }

    #[test]
    fn a_user_s_identity_that_does_not_open_stops_the_rotation() {
        a_value_that_does_not_open_stops_the_rotation(
            "user_account",
            "identity_sealed",
            &tenant_is(3),
        );
    }

    #[test]
    fn an_operator_s_roles_that_do_not_open_stop_the_rotation() {
        let row = format!("user_id = {} AND tenant IS NULL", lit(&[1; 16]));
        a_value_that_does_not_open_stops_the_rotation("membership", "roles_sealed", &row);
    }

    #[test]
    fn a_setting_that_does_not_open_stops_the_rotation() {
        let row = format!("tenant = {} AND name = 'retention.days'", lit(&[2; 16]));
        a_value_that_does_not_open_stops_the_rotation("tenant_setting", "value_sealed", &row);
    }

    #[test]
    fn a_session_s_scopes_that_do_not_open_stop_the_rotation() {
        let row = format!("id_hash = {}", lit(&[0xa1; 32]));
        a_value_that_does_not_open_stops_the_rotation("session", "scopes_sealed", &row);
    }

    #[test]
    fn a_session_s_methods_that_do_not_open_stop_the_rotation() {
        let row = format!("id_hash = {}", lit(&[0xa2; 32]));
        a_value_that_does_not_open_stops_the_rotation("session", "amr_sealed", &row);
    }

    #[test]
    fn a_newest_log_key_that_does_not_open_stops_the_rotation() {
        let w = world();
        fill(&w);
        flip(&w.db, "admin_audit_key", "wrapped", "ordinal = 1");
        let before = state(&w.db);
        assert_eq!(
            rotate(&w),
            Err(TenantError::Io),
            "the key that seals the rotation's record does not open"
        );
        assert_eq!(
            changed(&before, &state(&w.db)),
            Vec::<&str>::new(),
            "no row is written"
        );
        flip(&w.db, "admin_audit_key", "wrapped", "ordinal = 1");
        assert_eq!(rotate(&w), Ok(()), "with the key as it was, it is made");
    }

    #[test]
    fn a_registry_whose_log_has_no_key_is_not_rotated() {
        let w = world();
        drop(open(&w, &w.old).expect("the first open"));
        as_owner(&w.db, "DELETE FROM vcon.admin_audit_key");
        let before = state(&w.db);
        assert_eq!(
            rotate(&w),
            Err(TenantError::Io),
            "a registry that would not open is not moved"
        );
        assert_eq!(
            changed(&before, &state(&w.db)),
            Vec::<&str>::new(),
            "no row is written"
        );
    }

    /// One tenant under `master` in a database that names no key and
    /// whose log has none, as a registry of before schema version 12
    /// left it.
    fn before_version_12(w: &World, master: &Key) {
        let registry = open(w, master).expect("the registry opens");
        let (row, _, _) = tenant(master, 1, "alpha-tenant-name", true, true);
        write(&registry, |tx| tx.insert_tenant(&row).expect("a tenant"));
        as_owner(&w.db, "DELETE FROM vcon.registry_key");
        as_owner(&w.db, "DELETE FROM vcon.admin_audit_key");
    }

    #[test]
    fn a_database_that_names_no_key_with_rows_under_a_third_is_refused() {
        let w = world();
        let stranger = Key::generate().expect("a key");
        before_version_12(&w, &stranger);
        let before = state(&w.db);
        assert_eq!(
            rotate(&w),
            Err(TenantError::WrongKey),
            "the tenant opens under neither key"
        );
        assert_eq!(
            changed(&before, &state(&w.db)),
            Vec::<&str>::new(),
            "no id is written and no key of the log made"
        );
    }

    #[test]
    fn a_commit_that_is_rolled_back_keeps_nothing() {
        let w = world();
        fill(&w);
        let rows = filled(&w);
        let registry = open(&w, &w.old).expect("the registry opens");
        let before = state(&w.db);
        registry.fail_commit(1, CommitError::RolledBack);
        assert_eq!(
            registry.rotate(&w.old, &w.new, now(), BATCH),
            Err(TenantError::Io),
            "the commit failed"
        );
        assert_eq!(
            changed(&before, &state(&w.db)),
            Vec::<&str>::new(),
            "the one transaction kept nothing"
        );
        assert_eq!(
            registry.rotate(&w.old, &w.new, now(), BATCH),
            Ok(()),
            "the next commit is kept"
        );
        assert_eq!(
            read(&open(&w, &w.new).expect("opens under the new key")),
            rows,
            "and every row reads as it did"
        );
    }

    #[test]
    fn a_sealed_value_moved_to_another_row_stops_the_rotation() {
        let w = world();
        fill(&w);
        as_owner(
            &w.db,
            &format!(
                "UPDATE vcon.tenant SET name_sealed = \
                 (SELECT name_sealed FROM vcon.tenant WHERE {}) WHERE {}",
                tenant_is(1),
                tenant_is(3)
            ),
        );
        let before = state(&w.db);
        assert_eq!(
            rotate(&w),
            Err(TenantError::Io),
            "a value is bound to its row"
        );
        assert_eq!(
            changed(&before, &state(&w.db)),
            Vec::<&str>::new(),
            "no row is written"
        );
    }

    #[test]
    fn a_tenant_key_wrapped_for_another_tenant_stops_the_rotation() {
        let w = world();
        fill(&w);
        as_owner(
            &w.db,
            &format!(
                "UPDATE vcon.tenant SET wrapped_tenant_key = \
                 (SELECT wrapped_tenant_key FROM vcon.tenant WHERE {}) WHERE {}",
                tenant_is(2),
                tenant_is(3)
            ),
        );
        let before = state(&w.db);
        assert_eq!(
            rotate(&w),
            Err(TenantError::Io),
            "a wrapped key is bound to its tenant"
        );
        assert_eq!(
            changed(&before, &state(&w.db)),
            Vec::<&str>::new(),
            "no row is written"
        );
    }

    #[test]
    fn a_registry_moved_to_a_third_key_while_the_rotation_waited_is_refused() {
        let w = world();
        fill(&w);
        let registry = open(&w, &w.old).expect("the registry opens");
        as_owner(
            &w.db,
            "UPDATE vcon.registry_key SET catalog_key_id = '\\x0102030405060708'",
        );
        let before = state(&w.db);
        assert_eq!(
            registry.rotate(&w.old, &w.new, now(), BATCH),
            Err(TenantError::WrongKey),
            "the id is compared again with the lock held"
        );
        assert_eq!(
            changed(&before, &state(&w.db)),
            Vec::<&str>::new(),
            "no row is written"
        );
    }

    // The edges.

    #[test]
    fn an_empty_registry_is_rotated() {
        let w = world();
        drop(open(&w, &w.old).expect("the first open"));
        assert_eq!(rotate(&w), Ok(()), "a registry with no row");
        assert_eq!(
            named(&w.db),
            [hex(&catalog_key(&w.new).id().0)],
            "the database names the new key"
        );
        assert_eq!(
            uses(&w.db),
            [format!("{} 0 {}", hex(&w.new.id().0), nanos(now()))],
            "the new master key has wrapped no key yet"
        );
        assert_eq!(records(&w.db).len(), 1, "the rotation is recorded");
        assert!(open(&w, &w.new).is_ok(), "it opens under the new key");
    }

    #[test]
    fn a_database_no_registry_has_opened_is_rotated() {
        let w = world();
        assert_eq!(named(&w.db), Vec::<String>::new(), "no key is named");
        assert_eq!(rotate(&w), Ok(()), "the first open is the rotation's");
        assert_eq!(
            named(&w.db),
            [hex(&catalog_key(&w.new).id().0)],
            "the database names the new key"
        );
        assert_eq!(log_keys(&w.db).len(), 1, "the log has its key");
        assert!(open(&w, &w.new).is_ok(), "it opens under the new key");
        assert_eq!(
            open(&w, &w.old).err(),
            Some(TenantError::WrongKey),
            "and not under the old"
        );
    }

    #[test]
    fn rows_of_before_version_12_under_the_old_key_are_rotated() {
        let w = world();
        before_version_12(&w, &w.old);
        assert_eq!(rotate(&w), Ok(()), "the tenant opens under the old key");
        assert_eq!(
            named(&w.db),
            [hex(&catalog_key(&w.new).id().0)],
            "the database names the new key"
        );
        let registry = open(&w, &w.new).expect("opens under the new key");
        let mut tx = registry.snapshot().expect("a read begins");
        assert_eq!(
            tx.tenant(tenant_id(1)).map(|t| t.map(|t| t.name)),
            Ok(Some("alpha-tenant-name".to_owned())),
            "the tenant reads under the new key"
        );
        assert_eq!(uses(&w.db).len(), 1, "one master key is in use");
    }

    #[test]
    fn rows_of_before_version_12_under_the_new_key_are_left_and_named() {
        let w = world();
        before_version_12(&w, &w.new);
        let tenants = table(&w.db, "tenant");
        assert_eq!(rotate(&w), Ok(()), "the registry is under the new key");
        assert_eq!(
            table(&w.db, "tenant"),
            tenants,
            "the tenant is not written again"
        );
        assert_eq!(
            named(&w.db),
            [hex(&catalog_key(&w.new).id().0)],
            "the open that found it names the key"
        );
        assert_eq!(
            records(&w.db),
            Vec::<String>::new(),
            "no rotation was made, so none is recorded"
        );
    }

    #[test]
    fn a_value_that_is_null_stays_null() {
        let w = world();
        fill(&w);
        let nulls = |db: &Db| {
            as_owner(
                db,
                &format!(
                    "SELECT retention_sealed IS NULL, metadata_sealed IS NULL, \
                     role_cap_sealed IS NULL, pending_wrapped_tenant_key IS NULL \
                     FROM vcon.tenant WHERE {}",
                    tenant_is(2)
                ),
            )
        };
        assert_eq!(nulls(&w.db), ["t t t t"], "the second tenant has none");
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        assert_eq!(nulls(&w.db), ["t t t t"], "and has none after");
        assert_eq!(
            uses(&w.db),
            [format!("{} 4 {}", hex(&w.new.id().0), nanos(now()))],
            "a pending key that is not there is not counted"
        );
    }

    #[test]
    fn a_user_with_no_identity_keeps_none() {
        let w = world();
        fill(&w);
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        assert_eq!(
            as_owner(
                &w.db,
                &format!(
                    "SELECT identity_mac IS NULL, identity_sealed IS NULL \
                     FROM vcon.user_account WHERE {}",
                    tenant_is(2)
                ),
            ),
            ["t t"],
            "no HMAC and no sealed copy are made of nothing"
        );
        let registry = open(&w, &w.new).expect("opens under the new key");
        let mut tx = registry.snapshot().expect("a read begins");
        assert_eq!(
            tx.user(user_id(2)).map(|u| u.map(|u| u.identity)),
            Ok(Some(None)),
            "the user reads with no identity"
        );
    }

    #[test]
    fn an_operator_s_membership_reads_after() {
        let w = world();
        fill(&w);
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        let registry = open(&w, &w.new).expect("opens under the new key");
        let mut tx = registry.snapshot().expect("a read begins");
        assert_eq!(
            tx.membership(user_id(1), None).map(|m| m.map(|m| m.roles)),
            Ok(Some("roles-of-1".to_owned())),
            "the row with no tenant was found by its user and sealed again"
        );
    }

    #[test]
    fn an_older_log_key_that_does_not_open_is_left_as_it_is() {
        let w = world();
        fill(&w);
        as_owner(&w.db, "UPDATE vcon.admin_audit_key SET ordinal = 2");
        as_owner(
            &w.db,
            "INSERT INTO vcon.admin_audit_key (key_id, ordinal, wrapped) \
             VALUES ('\\x0a0b0c0d0e0f0a0b', 1, decode(repeat('5a', 61), 'hex'))",
        );
        assert_eq!(rotate(&w), Ok(()), "the newest key opens");
        let keys = log_keys(&w.db);
        assert_eq!(keys.len(), 2, "two keys still");
        assert_eq!(
            keys[0],
            format!("1 0a0b0c0d0e0f0a0b {}", "5a".repeat(61)),
            "the key that did not open is not written"
        );
        let registry = open(&w, &w.new).expect("opens under the new key");
        let text = exported(&registry).expect("the log is read");
        assert!(text.contains("target-one"), "the newest key reads: {text}");
    }

    #[test]
    fn two_users_that_share_an_identity_share_its_new_hmac() {
        let w = world();
        fill(&w);
        let macs = |db: &Db| {
            as_owner(
                db,
                "SELECT encode(identity_mac, 'hex') FROM vcon.user_account \
                 WHERE identity_mac IS NOT NULL ORDER BY id",
            )
        };
        let before = macs(&w.db);
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        let after = macs(&w.db);
        assert_eq!(after.len(), 2, "the user and the deactivated one");
        assert_eq!(after[0], after[1], "one identity, one HMAC");
        assert_ne!(after[0], before[0], "under the new index key");
    }

    #[test]
    fn a_batch_of_as_many_rows_as_a_table_has_moves_them_all() {
        let w = world();
        fill(&w);
        let before = filled(&w);
        assert_eq!(
            PgRegistry::rekey_master(&w.database, &w.old, &w.new, now(), 3),
            Ok(()),
            "three tenants, three users, three memberships, three settings"
        );
        assert_eq!(
            read(&open(&w, &w.new).expect("opens under the new key")),
            before,
            "every row was moved"
        );
    }

    #[test]
    fn a_batch_of_one_row_fewer_than_a_table_has_moves_them_all() {
        let w = world();
        fill(&w);
        let before = filled(&w);
        assert_eq!(
            PgRegistry::rekey_master(&w.database, &w.old, &w.new, now(), 2),
            Ok(()),
            "the third row is in a second batch, and a table of two rows in one"
        );
        assert_eq!(
            read(&open(&w, &w.new).expect("opens under the new key")),
            before,
            "every row was moved"
        );
        assert_eq!(
            uses(&w.db),
            [format!("{} 4 {}", hex(&w.new.id().0), nanos(now()))],
            "the keys of both batches are counted"
        );
    }

    #[test]
    fn the_use_rows_of_other_master_keys_are_removed() {
        let w = world();
        fill(&w);
        let registry = open(&w, &w.old).expect("the registry opens");
        write(&registry, |tx| {
            tx.add_master_wrap(w.old.id(), T0).expect("the old key's");
            tx.add_master_wrap(KeyId([0xee; 8]), T0)
                .expect("a key of an older rotation");
        });
        assert_eq!(uses(&w.db).len(), 2, "two keys in use before");
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        assert_eq!(
            uses(&w.db),
            [format!("{} 4 {}", hex(&w.new.id().0), nanos(now()))],
            "the new master key alone is in use"
        );
    }

    #[test]
    fn two_rotations_at_once_make_one() {
        let w = world();
        fill(&w);
        let before = filled(&w);
        let results: Vec<Result<(), TenantError>> = std::thread::scope(|scope| {
            let runs: Vec<_> = (0..2).map(|_| scope.spawn(|| rotate(&w))).collect();
            runs.into_iter()
                .map(|run| run.join().expect("a rotation's thread"))
                .collect()
        });
        assert_eq!(results, [Ok(()), Ok(())], "both end well");
        let registry = open(&w, &w.new).expect("opens under the new key");
        assert_eq!(read(&registry), before, "the rows read as they did");
        let mut tx = registry.snapshot().expect("a read begins");
        assert_eq!(
            tx.record_times(Action::RekeyMaster, 200).map(|t| t.len()),
            Ok(1),
            "one rotation was made: the other found it done"
        );
    }

    #[test]
    fn a_rotation_to_the_key_the_registry_is_under_writes_nothing() {
        let w = world();
        fill(&w);
        let before = state(&w.db);
        assert_eq!(
            PgRegistry::rekey_master(&w.database, &w.old, &w.old, now(), BATCH),
            Ok(()),
            "the registry is under the new key already"
        );
        assert_eq!(
            PgRegistry::rekey_master(&w.database, &w.new, &w.old, now(), BATCH),
            Ok(()),
            "whatever the old key is said to be"
        );
        assert_eq!(
            changed(&before, &state(&w.db)),
            Vec::<&str>::new(),
            "no row is written and nothing recorded"
        );
    }

    #[test]
    fn every_sealed_column_of_the_registry_is_in_the_rotation_s_list() {
        let w = world();
        let mut there = as_owner(
            &w.db,
            "SELECT c.table_name || '.' || c.column_name FROM information_schema.columns c \
             WHERE c.table_schema = 'vcon' \
               AND c.table_name IN (SELECT tablename::text FROM pg_policies \
                   WHERE schemaname = 'vcon' AND policyname = 'registry_rows') \
               AND (c.column_name LIKE '%\\_sealed' OR c.column_name LIKE '%\\_mac' \
                    OR c.column_name LIKE '%wrapped%') \
             ORDER BY 1",
        );
        // The log's records are never written again, and the rotation
        // seals the log's keys with the function a tenant's log has.
        let apart = [
            "admin_audit.target_mac",
            "admin_audit.target_sealed",
            "admin_audit_key.wrapped",
        ];
        for name in apart {
            assert!(
                there.iter().any(|c| c == name),
                "{name} is a column of the schema"
            );
        }
        there.retain(|c| !apart.contains(&c.as_str()));
        let mut listed: Vec<String> = REMADE
            .iter()
            .flat_map(|spec| {
                spec.columns
                    .iter()
                    .flat_map(written)
                    .map(move |column| format!("{}.{column}", spec.table))
            })
            .collect();
        listed.sort();
        assert_eq!(
            listed, there,
            "a column the schema seals and the rotation does not make again \
             could not be read after one"
        );
    }

    #[test]
    fn a_tenant_s_own_rows_are_not_written() {
        let w = world();
        fill(&w);
        let (tenant, _key, _catalog) = tenant_catalog(&w.database);
        let own = |db: &Db| -> Vec<String> {
            ["tenant_catalog", "audit_key", "key_use", "audit"]
                .iter()
                .map(|name| {
                    let sql = format!(
                        "SELECT coalesce(md5(string_agg(t::text, ';' ORDER BY t::text)), 'none') \
                         FROM vcon.{name} t"
                    );
                    wait(async {
                        let admin = db.admin().await;
                        admin
                            .batch_execute("SET ROLE vcon_owner")
                            .await
                            .expect("the owner role");
                        in_scope(&admin, &tenant.to_string(), &sql)
                            .await
                            .unwrap_or_else(|e| panic!("{sql}: {e}"))
                            .iter()
                            .find_map(|m| match m {
                                tokio_postgres::SimpleQueryMessage::Row(row) => {
                                    row.get(0).map(str::to_owned)
                                }
                                _ => None,
                            })
                            .unwrap_or_else(|| panic!("{sql}: no row"))
                    })
                })
                .collect()
        };
        let before = own(&w.db);
        assert_ne!(before[0], "none", "the tenant has its catalog row");
        assert_ne!(before[1], "none", "and its log's key");
        assert_eq!(rotate(&w), Ok(()), "the rotation is made");
        assert_eq!(
            own(&w.db),
            before,
            "a tenant's catalog is under its tenant key, which the rotation only wraps again"
        );
    }

    #[test]
    fn a_commit_with_no_answer_that_was_kept_is_found_done_by_the_next_run() {
        let w = world();
        fill(&w);
        let rows = filled(&w);
        let registry = open(&w, &w.old).expect("the registry opens");
        registry.fail_commit(1, CommitError::Unknown);
        assert_eq!(
            registry.rotate(&w.old, &w.new, now(), BATCH),
            Err(TenantError::Io),
            "the commit got no answer"
        );
        let before = state(&w.db);
        assert_eq!(rotate(&w), Ok(()), "the next run finds the rotation made");
        assert_eq!(
            changed(&before, &state(&w.db)),
            Vec::<&str>::new(),
            "and writes nothing"
        );
        assert_eq!(
            read(&open(&w, &w.new).expect("opens under the new key")),
            rows,
            "every row reads as it did"
        );
    }

    #[test]
    fn a_rotation_waits_for_a_write_of_the_registry_to_end() {
        let w = world();
        fill(&w);
        let registry = open(&w, &w.old).expect("the registry opens");
        let mut first = registry.immediate().expect("a write begins");
        let (row, key, _) = tenant(&w.old, 7, "eta-tenant-name", true, false);
        first.insert_tenant(&row).expect("inserts");
        let (done, rotated) = mpsc::channel();
        std::thread::scope(|scope| {
            scope.spawn(|| done.send(rotate(&w)).expect("the test waits"));
            assert_eq!(
                rotated.recv_timeout(Duration::from_millis(400)),
                Err(mpsc::RecvTimeoutError::Timeout),
                "the rotation has not ended while the write holds the lock"
            );
            first.commit().expect("the write commits");
            assert_eq!(
                rotated.recv_timeout(Duration::from_secs(30)),
                Ok(Ok(())),
                "the rotation is made once the write has ended"
            );
        });
        let wraps = wraps(&w, &w.new);
        let (tenant, wrapped, _) = wraps.last().expect("the tenant the write added");
        assert_eq!(*tenant, tenant_id(7), "the write's tenant is there");
        let opened = envelope::unwrap(&w.new, &Context::Tenant { tenant: *tenant }, wrapped)
            .expect("its key was wrapped again with the others");
        assert_eq!(opened.id(), key, "and is the key it had");
    }

    #[test]
    fn a_rotation_that_stopped_gives_up_the_lock() {
        let w = world();
        fill(&w);
        let row = format!("id_hash = {}", lit(&[0xa2; 32]));
        flip(&w.db, "session", "amr_sealed", &row);
        assert_eq!(
            rotate(&w),
            Err(TenantError::Io),
            "a value of the last table does not open"
        );
        let registry = open(&w, &w.old).expect("the registry opens under the old key");
        let (extra, _, _) = tenant(&w.old, 8, "theta-tenant-name", true, false);
        write(&registry, |tx| {
            tx.insert_tenant(&extra).expect("a write is taken");
        });
        assert_eq!(wraps(&w, &w.old).len(), 4, "the write was kept");
        flip(&w.db, "session", "amr_sealed", &row);
        assert_eq!(
            rotate(&w),
            Ok(()),
            "and with the value as it was, the rotation is made"
        );
    }

    #[test]
    fn a_rotation_that_found_the_registry_moved_while_it_waited_writes_nothing() {
        let w = world();
        fill(&w);
        let registry = open(&w, &w.old).expect("the registry opens");
        assert_eq!(rotate(&w), Ok(()), "another run makes the rotation");
        let before = state(&w.db);
        assert_eq!(
            registry.rotate(&w.old, &w.new, now(), BATCH),
            Ok(()),
            "the id, compared with the lock held, is the new key's"
        );
        assert_eq!(
            changed(&before, &state(&w.db)),
            Vec::<&str>::new(),
            "no row is written a second time"
        );
    }
}