Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
/***********************************************************************************************************************************
Test Stanza Commands
***********************************************************************************************************************************/
#include "common/crypto/cipherBlock.h"
#include "common/io/bufferWrite.h"
#include "info/infoArchive.intern.h"
#include "info/infoBackup.intern.h"
#include "postgres/interface.h"
#include "postgres/version.h"
#include "storage/posix/storage.h"
 
#include "harness/config.h"
#include "harness/info.h"
#include "harness/postgres.h"
#include "harness/pq.h"
 
/***********************************************************************************************************************************
Test data
***********************************************************************************************************************************/
// Passphrase generated with openssl rand -base64 32, as required to create a stanza at format 6
#define TEST_CIPHER_PASS_FMT6                                       "SOb5SRNoVLdVRtr2K3lr/oDmocARh2c+SObK6H8CtFY="
 
/***********************************************************************************************************************************
Test Run
***********************************************************************************************************************************/
static void
testRun(void)
{
    FUNCTION_HARNESS_VOID();
 
    // Create storage objects for the testing location and for the harness directory for items such as logs, stop files, etc
    Storage *storageTest = storagePosixNewP(TEST_PATH_STR, .write = true);
    Storage *storageHrn = storagePosixNewP(HRN_PATH_STR, .write = true);
 
    #define TEST_STANZA                                             "db"
    #define TEST_WAL_MIGRATE                                        "archive/db/15-1/000000010000000000000001"
    #define TEST_STANZA_OTHER                                       "otherstanza"
 
    StringList *argListBase = strLstNew();
    hrnCfgArgRawBool(argListBase, cfgOptOnline, false);
    hrnCfgArgRawZ(argListBase, cfgOptStanza, TEST_STANZA);
    hrnCfgArgRawZ(argListBase, cfgOptPgPath, TEST_PATH "/pg");
    hrnCfgArgRawZ(argListBase, cfgOptRepoPath, TEST_PATH "/repo");
 
    // *****************************************************************************************************************************
    if (testBegin("cmdStanzaCreate(), checkStanzaInfo(), cmdStanzaDelete()"))
    {
        TEST_TITLE("stanza-create: repo option not valid");
 
        // Load Parameters
        StringList *argList = strLstDup(argListBase);
        hrnCfgArgKeyRawZ(argList, cfgOptRepoPath, 2, TEST_PATH "/repo2");
        hrnCfgArgRawZ(argList, cfgOptRepo, "2");
 
        TEST_ERROR(
            hrnCfgLoadP(cfgCmdStanzaCreate, argList), OptionInvalidError, "option 'repo' not valid for command 'stanza-create'");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("stanza-create: stop file error");
 
        HRN_CFG_LOAD(cfgCmdStanzaCreate, argListBase);
 
        // Create the stop file, test, then remove the stop file
        HRN_STORAGE_PUT_EMPTY(storageHrn, strZ(lockStopFileName(cfgOptionStr(cfgOptStanza))));
        TEST_ERROR(cmdStanzaCreate(), StopError, "stop file exists for stanza db");
        HRN_STORAGE_REMOVE(storageHrn, strZ(lockStopFileName(cfgOptionStr(cfgOptStanza))));
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("stanza-create: validate info files created");
 
        argList = strLstDup(argListBase);
        HRN_CFG_LOAD(cfgCmdStanzaCreate, argList);
 
        // Create pg_control
        HRN_PG_CONTROL_PUT(storagePgWrite(), PG_VERSION_10);
 
        TEST_RESULT_VOID(cmdStanzaCreate(), "stanza create - one repo, no files exist");
        TEST_RESULT_LOG("P00   INFO: stanza-create for stanza 'db' on repo1");
 
        HRN_INFO_PUT(
            storageHrn, "test.info",
            "[db]\n"
            "db-id=1\n"
            "db-system-id=" HRN_PG_SYSTEMID_10_Z "\n"
            "db-version=\"10\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-id\":" HRN_PG_SYSTEMID_10_Z ",\"db-version\":\"10\"}\n",
            .comment = "put archive info to test file");
 
        TEST_RESULT_BOOL(
            bufEq(
                storageGetP(storageNewReadP(storageRepoIdx(0), INFO_ARCHIVE_PATH_FILE_STR)),
                storageGetP(storageNewReadP(storageRepoIdx(0), STRDEF(INFO_ARCHIVE_PATH_FILE INFO_COPY_EXT)))) &&
            bufEq(
                storageGetP(storageNewReadP(storageRepoIdx(0), INFO_ARCHIVE_PATH_FILE_STR)),
                storageGetP(storageNewReadP(storageHrn, STRDEF("test.info")))),
            true, "test and stanza archive info files are equal");
 
        HRN_INFO_PUT(
            storageHrn, "test.info",
            "[db]\n"
            "db-catalog-version=201707211\n"
            "db-control-version=1002\n"
            "db-id=1\n"
            "db-system-id=" HRN_PG_SYSTEMID_10_Z "\n"
            "db-version=\"10\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-catalog-version\":201707211,\"db-control-version\":1002,\"db-system-id\":" HRN_PG_SYSTEMID_10_Z
            ",\"db-version\":\"10\"}\n",
            .comment = "put backup info to test file");
 
        TEST_RESULT_BOOL(
            bufEq(
                storageGetP(storageNewReadP(storageRepoIdx(0), INFO_BACKUP_PATH_FILE_STR)),
                storageGetP(storageNewReadP(storageRepoIdx(0), STRDEF(INFO_BACKUP_PATH_FILE INFO_COPY_EXT)))) &&
            bufEq(
                storageGetP(storageNewReadP(storageRepoIdx(0), INFO_BACKUP_PATH_FILE_STR)),
                storageGetP(storageNewReadP(storageHrn, STRDEF("test.info")))),
            true, "test and stanza backup info files are equal");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("stanza-create at format 6 requires a passphrase that looks generated");
 
        argList = strLstDup(argListBase);
        hrnCfgArgKeyRawZ(argList, cfgOptRepoPath, 4, TEST_PATH "/repo4");
        hrnCfgArgKeyRawStrId(argList, cfgOptRepoCipherType, 4, cipherTypeAes256Cbc);
        hrnCfgArgKeyRawZ(argList, cfgOptRepoFormat, 4, "6");
 
        #define TEST_CIPHER_PASS_FMT6_ERROR                                                                                        \
            "option 'repo4-cipher-pass' must be at least 256 random bits encoded as hex, base64, or base64url to create a"         \
            " stanza at repository format 6 or higher\n"                                                                           \
            "HINT: generate a passphrase from at least 256 random bits, e.g. with 'openssl rand -base64 32'."
 
        #define TEST_CIPHER_PASS_FMT6_LOG                                                                                          \
            "P00   INFO: stanza-create for stanza 'db' on repo1\n"                                                                 \
            "P00   INFO: stanza 'db' already exists on repo1 and is valid\n"                                                       \
            "P00   INFO: stanza-create for stanza 'db' on repo4"
 
        // Valid base64 that is less than 256 bits
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 4, "SOb5SRNoVLdVRtr2K3lr/oDmocARh2c+SObK6H8CtA==");
        HRN_CFG_LOAD(cfgCmdStanzaCreate, argList);
 
        TEST_ERROR(cmdStanzaCreate(), OptionInvalidValueError, TEST_CIPHER_PASS_FMT6_ERROR);
        TEST_RESULT_LOG(TEST_CIPHER_PASS_FMT6_LOG);
 
        // Hex that is less than 256 bits
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 4, "48e6f949136854b75546daf62b796bfe80e6a1c01187673e");
        HRN_CFG_LOAD(cfgCmdStanzaCreate, argList);
 
        TEST_ERROR(cmdStanzaCreate(), OptionInvalidValueError, TEST_CIPHER_PASS_FMT6_ERROR);
        TEST_RESULT_LOG(TEST_CIPHER_PASS_FMT6_LOG);
 
        // Not base64
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 4, "not base64");
        HRN_CFG_LOAD(cfgCmdStanzaCreate, argList);
 
        TEST_ERROR(cmdStanzaCreate(), OptionInvalidValueError, TEST_CIPHER_PASS_FMT6_ERROR);
        TEST_RESULT_LOG(TEST_CIPHER_PASS_FMT6_LOG);
 
        // Text
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 4, "Y29ycmVjdAlob3JzZQ0KYmF0dGVyeSBzdGFwbGUgYW5kIG1vcmUgdGV4dA==");
        HRN_CFG_LOAD(cfgCmdStanzaCreate, argList);
 
        TEST_ERROR(cmdStanzaCreate(), OptionInvalidValueError, TEST_CIPHER_PASS_FMT6_ERROR);
        TEST_RESULT_LOG(TEST_CIPHER_PASS_FMT6_LOG);
 
        // Too few distinct bytes
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 4, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=");
        HRN_CFG_LOAD(cfgCmdStanzaCreate, argList);
 
        TEST_ERROR(cmdStanzaCreate(), OptionInvalidValueError, TEST_CIPHER_PASS_FMT6_ERROR);
        TEST_RESULT_LOG(TEST_CIPHER_PASS_FMT6_LOG);
 
        // Too few distinct differences
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 4, "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8=");
        HRN_CFG_LOAD(cfgCmdStanzaCreate, argList);
 
        TEST_ERROR(cmdStanzaCreate(), OptionInvalidValueError, TEST_CIPHER_PASS_FMT6_ERROR);
        TEST_RESULT_LOG(TEST_CIPHER_PASS_FMT6_LOG);
 
        // Prior documentation example
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 4, "zWaf6XtpjIVZC5444yXB+cgFDFl7MxGlgkZSaoPvTGirhPygu4jOKOXf9LO4vjfO");
        HRN_CFG_LOAD(cfgCmdStanzaCreate, argList);
 
        TEST_ERROR(
            cmdStanzaCreate(), OptionInvalidValueError,
            "option 'repo4-cipher-pass' must not be the example passphrase from the documentation\n"
            "HINT: generate a passphrase from at least 256 random bits, e.g. with 'openssl rand -base64 32'.");
        TEST_RESULT_LOG(TEST_CIPHER_PASS_FMT6_LOG);
 
        // base64url without padding
        TEST_RESULT_BOOL(
            cipherPassGenerated(cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF("SOb5SRNoVLdVRtr2K3lr_oDmocARh2c-SObK6H8CtFY"))),
            true, "base64url without padding");
 
        // Hex
        TEST_RESULT_BOOL(
            cipherPassGenerated(
                cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF("48e6f949136854b75546daf62b796bfe80e6a1c01187673e48e6cae87f02b456"))),
            true, "hex");
 
        TEST_RESULT_BOOL(storagePathExistsP(storageTest, STRDEF("repo4")), false, "nothing created");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("cmdStanzaCreate success - multi-repo and encryption");
 
        argList = strLstDup(argListBase);
        hrnCfgArgKeyRawZ(argList, cfgOptRepoPath, 2, TEST_PATH "/repo2");
        hrnCfgArgKeyRawStrId(argList, cfgOptRepoCipherType, 2, cipherTypeAes256Cbc);
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 2, "zWaf6XtpjIVZC5444yXB+cgFDFl7MxGlgkZSaoPvTGi=");
        hrnCfgArgKeyRawZ(argList, cfgOptRepoPath, 3, TEST_PATH "/repo3");
        hrnCfgArgKeyRawZ(argList, cfgOptRepoFormat, 3, "6");
        // Request a format for repo1 as well, where the stanza already exists
        hrnCfgArgKeyRawZ(argList, cfgOptRepoFormat, 1, "6");
        hrnCfgArgKeyRawZ(argList, cfgOptRepoPath, 4, TEST_PATH "/repo4");
        hrnCfgArgKeyRawStrId(argList, cfgOptRepoCipherType, 4, cipherTypeAes256Cbc);
        hrnCfgArgKeyRawZ(argList, cfgOptRepoFormat, 4, "6");
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 4, TEST_CIPHER_PASS_FMT6);
        HRN_CFG_LOAD(cfgCmdStanzaCreate, argList);
 
        TEST_ERROR(
            cmdStanzaCreate(), OptionInvalidValueError,
            "option 'repo2-cipher-pass' must not be the example passphrase from the documentation\n"
            "HINT: generate a passphrase from at least 256 random bits, e.g. with 'openssl rand -base64 32'.");
        TEST_RESULT_LOG(
            "P00   INFO: stanza-create for stanza 'db' on repo1\n"
            "P00   INFO: stanza 'db' already exists on repo1 and is valid\n"
            "P00   INFO: stanza-create for stanza 'db' on repo2");
 
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 2, "12345678");
        HRN_CFG_LOAD(cfgCmdStanzaCreate, argList);
 
        const time_t timeCreate = time(NULL);
 
        TEST_RESULT_VOID(cmdStanzaCreate(), "stanza create - files already exist on repo1 and both are valid");
        TEST_RESULT_LOG(
            "P00   INFO: stanza-create for stanza 'db' on repo1\n"
            "P00   INFO: stanza 'db' already exists on repo1 and is valid\n"
            "P00   INFO: stanza-create for stanza 'db' on repo2\n"
            "P00   INFO: stanza-create for stanza 'db' on repo3\n"
            "P00   INFO: stanza-create for stanza 'db' on repo4");
 
        InfoArchive *infoArchive = NULL;
        TEST_ASSIGN(
            infoArchive,
            infoArchiveLoadFile(
                storageRepoIdx(1), INFO_ARCHIVE_PATH_FILE_STR, cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF("12345678"))),
            "load archive info from encrypted repo2");
        TEST_RESULT_UINT(cipherSpecType(infoArchiveCipherSpec(infoArchive)), cipherTypeAes256Cbc, "cipher sub set");
 
        InfoBackup *infoBackup = NULL;
        TEST_ASSIGN(
            infoBackup,
            infoBackupLoadFile(
                storageRepoIdx(1), INFO_BACKUP_PATH_FILE_STR, cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF("12345678"))),
            "load backup info from encrypted repo2");
        TEST_RESULT_UINT(cipherSpecType(infoBackupCipherSpec(infoBackup)), cipherTypeAes256Cbc, "cipher sub set");
 
        TEST_RESULT_BOOL(
            bufEq(cipherSpecPass(infoArchiveCipherSpec(infoArchive)), cipherSpecPass(infoBackupCipherSpec(infoBackup))), false,
            "cipher sub different for archive and backup");
 
        // Confirm non-encrypted repo created successfully. This repo was created with an explicit format so also confirm that the
        // requested format was stored rather than the default.
        TEST_ASSIGN(
            infoArchive,
            infoArchiveLoadFile(storageRepoIdx(2), INFO_ARCHIVE_PATH_FILE_STR, cipherSpecNewNone()),
            "load archive info from repo3");
        TEST_RESULT_UINT(
            cipherSpecType(infoArchiveCipherSpec(infoArchive)), cipherTypeNone,
            "archive cipher sub not set on non-encrypted repo");
        TEST_RESULT_UINT(infoArchiveFormat(infoArchive), REPOSITORY_FORMAT_6, "archive info at requested format");
 
        TEST_ASSIGN(
            infoBackup, infoBackupLoadFile(storageRepoIdx(2), INFO_BACKUP_PATH_FILE_STR, cipherSpecNewNone()),
            "load backup info from repo3");
        TEST_RESULT_UINT(
            cipherSpecType(infoBackupCipherSpec(infoBackup)), cipherTypeNone,
            "backup cipher sub not set on non-encrypted repo");
        TEST_RESULT_UINT(infoBackupFormat(infoBackup), REPOSITORY_FORMAT_6, "backup info at requested format");
 
        // The stanza on repo1 already existed, so the format requested for it was ignored. A stanza is migrated with
        // stanza-upgrade rather than with this command, which sets the format only for a stanza it creates.
        TEST_RESULT_UINT(infoArchiveFormat(infoArchive) != REPOSITORY_FORMAT_DEFAULT, true, "requested format is not the default");
        TEST_ASSIGN(
            infoArchive, infoArchiveLoadFile(storageRepoIdx(0), INFO_ARCHIVE_PATH_FILE_STR, cipherSpecNewNone()),
            "load archive info from repo1");
        TEST_RESULT_UINT(
            infoArchiveFormat(infoArchive), REPOSITORY_FORMAT_DEFAULT, "archive info on existing stanza still at default format");
 
        // Confirm other repo encrypted with different password. Format 6 stores the archive key at the first key id.
        TEST_ASSIGN(
            infoArchive,
            infoArchiveLoadFile(
                storageRepoIdx(3), INFO_ARCHIVE_PATH_FILE_STR,
                cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF(TEST_CIPHER_PASS_FMT6))),
            "load archive info from encrypted repo4");
 
        const CipherSpecMap *const cipherSpecMapCreate = infoArchiveCipherSpecMap(infoArchive);
 
        TEST_RESULT_UINT(cipherSpecMapSize(cipherSpecMapCreate), 1, "one archive key");
        TEST_RESULT_STR_Z(cipherSpecMapGetIdx(cipherSpecMapCreate, 0)->id, "1", "archive key stored at id 1");
        TEST_RESULT_STR_Z(cipherSpecMapIdCurrent(cipherSpecMapCreate), "1", "archive key at id 1 is current");
        TEST_RESULT_BOOL(
            infoArchiveCipherRotateTime(infoArchive) >= timeCreate &&
            infoArchiveCipherRotateTime(infoArchive) <= time(NULL),
            true, "rotation time is the create time");
        TEST_RESULT_UINT(
            cipherSpecDigest(cipherSpecMapGet(cipherSpecMapCreate, STRDEF("1"))), hashTypeSha256,
            "archive key derives with sha256");
        TEST_RESULT_UINT(
            bufSize(cipherSpecPass(cipherSpecMapGet(cipherSpecMapCreate, STRDEF("1")))), 32, "archive key is 256 bits");
 
        TEST_ASSIGN(
            infoBackup,
            infoBackupLoadFile(
                storageRepoIdx(3), INFO_BACKUP_PATH_FILE_STR,
                cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF(TEST_CIPHER_PASS_FMT6))),
            "load backup info from encrypted repo4");
        TEST_RESULT_BOOL(cipherSpecMapEmpty(infoCipherSpecMap(infoPgInfo(infoBackupPg(infoBackup)))), true, "no backup keys");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("cmdStanzaCreate missing files - multi-repo and encryption");
 
        // Remove backup.info on repo1
        TEST_STORAGE_EXISTS(storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE, .remove = true);
 
        // Remove archive.info on repo2
        TEST_STORAGE_EXISTS(storageRepoIdxWrite(1), INFO_ARCHIVE_PATH_FILE, .remove = true);
 
        // Remove info files on repo3
        TEST_STORAGE_EXISTS(storageRepoIdxWrite(2), INFO_ARCHIVE_PATH_FILE, .remove = true);
        TEST_STORAGE_EXISTS(storageRepoIdxWrite(2), INFO_BACKUP_PATH_FILE, .remove = true);
 
        // Remove copy files repo4
        TEST_STORAGE_EXISTS(storageRepoIdxWrite(3), INFO_ARCHIVE_PATH_FILE INFO_COPY_EXT, .remove = true);
        TEST_STORAGE_EXISTS(storageRepoIdxWrite(3), INFO_BACKUP_PATH_FILE INFO_COPY_EXT, .remove = true);
 
        TEST_RESULT_VOID(cmdStanzaCreate(), "stanza create - success with missing files");
        TEST_RESULT_LOG(
            "P00   INFO: stanza-create for stanza 'db' on repo1\n"
            "P00   INFO: stanza-create for stanza 'db' on repo2\n"
            "P00   INFO: stanza-create for stanza 'db' on repo3\n"
            "P00   INFO: stanza-create for stanza 'db' on repo4");
 
        TEST_RESULT_BOOL(
            bufEq(
                storageGetP(storageNewReadP(storageRepoIdx(0), INFO_BACKUP_PATH_FILE_STR)),
                storageGetP(storageNewReadP(storageRepoIdx(0), STRDEF(INFO_BACKUP_PATH_FILE INFO_COPY_EXT)))),
            true, "backup.info recreated repo1 from backup.info.copy");
        TEST_RESULT_BOOL(
            bufEq(
                storageGetP(storageNewReadP(storageRepoIdx(1), INFO_ARCHIVE_PATH_FILE_STR)),
                storageGetP(storageNewReadP(storageRepoIdx(1), STRDEF(INFO_ARCHIVE_PATH_FILE INFO_COPY_EXT)))),
            true, "archive.info repo2 recreated from archive.info.copy");
        TEST_RESULT_BOOL(
            bufEq(
                storageGetP(storageNewReadP(storageRepoIdx(2), INFO_BACKUP_PATH_FILE_STR)),
                storageGetP(storageNewReadP(storageRepoIdx(2), STRDEF(INFO_BACKUP_PATH_FILE INFO_COPY_EXT)))) &&
            bufEq(
                storageGetP(storageNewReadP(storageRepoIdx(2), INFO_ARCHIVE_PATH_FILE_STR)),
                storageGetP(storageNewReadP(storageRepoIdx(2), STRDEF(INFO_ARCHIVE_PATH_FILE INFO_COPY_EXT)))),
            true, "info files recreated repo3 from copy files");
        TEST_RESULT_BOOL(
            bufEq(
                storageGetP(storageNewReadP(storageRepoIdx(3), INFO_BACKUP_PATH_FILE_STR)),
                storageGetP(storageNewReadP(storageRepoIdx(3), STRDEF(INFO_BACKUP_PATH_FILE INFO_COPY_EXT)))) &&
            bufEq(
                storageGetP(storageNewReadP(storageRepoIdx(3), INFO_ARCHIVE_PATH_FILE_STR)),
                storageGetP(storageNewReadP(storageRepoIdx(3), STRDEF(INFO_ARCHIVE_PATH_FILE INFO_COPY_EXT)))),
            true, "copy files recreated repo4 from info files");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("cmdStanzaDelete - multi-repo and encryption, delete");
 
        StringList *argListCmd = strLstNew();
        hrnCfgArgKeyRawZ(argListCmd, cfgOptRepoPath, 1, TEST_PATH "/repo");
        hrnCfgArgKeyRawZ(argListCmd, cfgOptRepoPath, 2, TEST_PATH "/repo2");
        hrnCfgArgKeyRawZ(argListCmd, cfgOptRepoPath, 3, TEST_PATH "/repo3");
        hrnCfgArgKeyRawZ(argListCmd, cfgOptRepoPath, 4, TEST_PATH "/repo4");
        hrnCfgArgRawZ(argListCmd, cfgOptStanza, TEST_STANZA);
        hrnCfgArgRawZ(argListCmd, cfgOptPgPath, TEST_PATH "/pg");
 
        TEST_ERROR(
            hrnCfgLoadP(cfgCmdStanzaDelete, argListCmd), OptionRequiredError,
            "stanza-delete command requires option: repo\n"
            "HINT: this command requires a specific repository to operate on");
 
        // Add the repo option
        StringList *argListDelete = strLstDup(argListCmd);
        hrnCfgArgRawZ(argListDelete, cfgOptRepo, "4");
        HRN_CFG_LOAD(cfgCmdStanzaDelete, argListDelete);
 
        TEST_ERROR(
            cmdStanzaDelete(), FileMissingError,
            "stop file does not exist for stanza 'db'\n"
            "HINT: has the pgbackrest stop command been run on this server for this stanza?");
 
        // Create the stop file
        HRN_STORAGE_PUT_EMPTY(storageHrn, strZ(lockStopFileName(cfgOptionStr(cfgOptStanza))));
 
        TEST_STORAGE_LIST(
            storageTest, "repo4",
            "archive/\n"
            "archive/db/\n"
            "archive/db/archive.info\n"
            "archive/db/archive.info.copy\n"
            "backup/\n"
            "backup/db/\n"
            "backup/db/backup.info\n"
            "backup/db/backup.info.copy\n",
            .comment = "stanza exists in repo4");
 
        TEST_RESULT_VOID(cmdStanzaDelete(), "stanza delete - repo4");
 
        TEST_STORAGE_LIST(storageTest, "repo4", "archive/\nbackup/\n", .comment = "stanza deleted");
 
        TEST_RESULT_BOOL(
            storageExistsP(storageHrn, lockStopFileName(cfgOptionStr(cfgOptStanza))), false, "confirm stop file removed");
 
        // Remove the cipher pass environment variable otherwise stanza-create will recreate the stanza
        hrnCfgEnvKeyRemoveRaw(cfgOptRepoCipherPass, 4);
 
        // Stanza with directories only
        argListDelete = strLstDup(argListCmd);
        hrnCfgArgRawZ(argListDelete, cfgOptRepo, "3");
        HRN_CFG_LOAD(cfgCmdStanzaDelete, argListDelete);
 
        HRN_STORAGE_PATH_CREATE(
            storageRepoIdxWrite(2), STORAGE_REPO_ARCHIVE "/10-1/1234567812345678", .comment = "create archive sub directory");
        HRN_STORAGE_PATH_CREATE(
            storageRepoIdxWrite(2), STORAGE_REPO_BACKUP "/20190708-154306F", .comment = "create backup sub directory");
        HRN_STORAGE_PUT_EMPTY(storageHrn, strZ(lockStopFileName(cfgOptionStr(cfgOptStanza))), .comment = "create stop file");
 
        TEST_RESULT_VOID(cmdStanzaDelete(), "stanza delete - repo3 - sub directories only");
 
        TEST_STORAGE_LIST(storageTest, "repo3", "archive/\nbackup/\n", .comment = "stanza deleted");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("cmdStanzaCreate errors");
 
        argList = strLstDup(argListBase);
        hrnCfgArgKeyRawZ(argList, cfgOptRepoPath, 2, TEST_PATH "/repo2");
        hrnCfgArgKeyRawStrId(argList, cfgOptRepoCipherType, 2, cipherTypeAes256Cbc);
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 2, "12345678");
        HRN_CFG_LOAD(cfgCmdStanzaCreate, argList);
 
        // Backup files removed - archive.info and archive.info.copy exist repo2
        TEST_STORAGE_EXISTS(
            storageRepoIdxWrite(1), INFO_BACKUP_PATH_FILE INFO_COPY_EXT, .remove = true,
            .comment = "repo2: remove backup.info.copy");
        TEST_STORAGE_EXISTS(
            storageRepoIdxWrite(1), INFO_BACKUP_PATH_FILE, .remove = true, .comment = "repo2: remove backup.info");
 
        TEST_ERROR(
            cmdStanzaCreate(), FileMissingError,
            "archive.info exists but backup.info is missing on repo2\n"
            "HINT: this may be a symptom of repository corruption!");
        TEST_RESULT_LOG(
            "P00   INFO: stanza-create for stanza 'db' on repo1\n"
            "P00   INFO: stanza 'db' already exists on repo1 and is valid\n"
            "P00   INFO: stanza-create for stanza 'db' on repo2");
 
        // Archive files removed - backup.info and backup.info.copy exist repo1
        TEST_STORAGE_EXISTS(
            storageRepoIdxWrite(0), INFO_ARCHIVE_PATH_FILE INFO_COPY_EXT, .remove = true,
            .comment = "repo1: remove archive.info.copy");
        TEST_STORAGE_EXISTS(
            storageRepoIdxWrite(0), INFO_ARCHIVE_PATH_FILE, .remove = true, .comment = "repo1: remove archive.info");
 
        TEST_ERROR(
            cmdStanzaCreate(), FileMissingError,
            "backup.info exists but archive.info is missing on repo1\n"
            "HINT: this may be a symptom of repository corruption!");
        TEST_RESULT_LOG("P00   INFO: stanza-create for stanza 'db' on repo1");
 
        // Delete the last repo so only 1 remains
        argListDelete = strLstDup(argListCmd);
        hrnCfgArgRawZ(argListDelete, cfgOptRepo, "2");
        HRN_CFG_LOAD(cfgCmdStanzaDelete, argListDelete);
 
        HRN_STORAGE_PUT_EMPTY(storageHrn, strZ(lockStopFileName(cfgOptionStr(cfgOptStanza))), .comment = "create stop file");
 
        TEST_RESULT_VOID(cmdStanzaDelete(), "stanza delete - only 1 remains");
 
        // Remove the cipher pass environment variable otherwise stanza-create will recreate the stanza
        hrnCfgEnvKeyRemoveRaw(cfgOptRepoCipherPass, 2);
 
        argList = strLstDup(argListBase);
        HRN_CFG_LOAD(cfgCmdStanzaCreate, argList);
 
        // Archive files removed - backup.info exists
        TEST_STORAGE_EXISTS(
            storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE INFO_COPY_EXT, .remove = true,
            .comment = "repo1: remove backup.info.copy");
        TEST_ERROR(
            cmdStanzaCreate(), FileMissingError,
            "backup.info exists but archive.info is missing on repo1\n"
            "HINT: this may be a symptom of repository corruption!");
        TEST_RESULT_LOG("P00   INFO: stanza-create for stanza 'db' on repo1");
 
        // Archive files removed - backup.info.copy exists, backup.info moved to backup.info.copy
        HRN_STORAGE_MOVE(storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE, INFO_BACKUP_PATH_FILE INFO_COPY_EXT);
        TEST_ERROR(
            cmdStanzaCreate(), FileMissingError,
            "backup.info exists but archive.info is missing on repo1\n"
            "HINT: this may be a symptom of repository corruption!");
        TEST_RESULT_LOG("P00   INFO: stanza-create for stanza 'db' on repo1");
 
        // Backup files removed - archive.info file exists
        HRN_INFO_PUT(
            storageRepoIdxWrite(0), INFO_ARCHIVE_PATH_FILE,
            "[db]\n"
            "db-id=1\n"
            "db-system-id=" HRN_PG_SYSTEMID_10_Z "\n"
            "db-version=\"10\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-id\":" HRN_PG_SYSTEMID_10_Z ",\"db-version\":\"10\"}\n",
            .comment = "put archive info to file repo1");
        TEST_STORAGE_EXISTS(
            storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE INFO_COPY_EXT, .remove = true,
            .comment = "repo1: remove backup.info.copy");
        TEST_ERROR(
            cmdStanzaCreate(), FileMissingError,
            "archive.info exists but backup.info is missing on repo1\n"
            "HINT: this may be a symptom of repository corruption!");
        TEST_RESULT_LOG("P00   INFO: stanza-create for stanza 'db' on repo1");
 
        // Backup files removed - archive.info.copy file exists (renamed from archive.info)
        HRN_STORAGE_MOVE(storageRepoIdxWrite(0), INFO_ARCHIVE_PATH_FILE, INFO_ARCHIVE_PATH_FILE INFO_COPY_EXT);
        TEST_ERROR(
            cmdStanzaCreate(), FileMissingError,
            "archive.info exists but backup.info is missing on repo1\n"
            "HINT: this may be a symptom of repository corruption!");
        TEST_RESULT_LOG("P00   INFO: stanza-create for stanza 'db' on repo1");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("checkStanzaInfo() - already checked in checkTest so just a sanity check here");
 
        // Create a corrupted backup file - db id
        HRN_INFO_PUT(
            storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE,
            "[db]\n"
            "db-catalog-version=201707211\n"
            "db-control-version=1002\n"
            "db-id=2\n"
            "db-system-id=" HRN_PG_SYSTEMID_10_Z "\n"
            "db-version=\"10\"\n"
            "\n"
            "[db:history]\n"
            "2={\"db-catalog-version\":201707211,\"db-control-version\":1002,\"db-system-id\":" HRN_PG_SYSTEMID_10_Z
            ",\"db-version\":\"10\"}\n",
            .comment = "put backup info to file - bad db-id");
 
        TEST_ERROR(
            cmdStanzaCreate(), FileInvalidError,
            "backup info file and archive info file do not match\n"
            "archive: id = 1, version = 10, system-id = " HRN_PG_SYSTEMID_10_Z "\n"
            "backup : id = 2, version = 10, system-id = " HRN_PG_SYSTEMID_10_Z "\n"
            "HINT: this may be a symptom of repository corruption!");
        TEST_RESULT_LOG("P00   INFO: stanza-create for stanza 'db' on repo1");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("archive.info file and backup.info files that match but do not match the current database version");
 
        // Copy files may or may not exist - remove
        HRN_STORAGE_REMOVE(
            storageRepoIdxWrite(0), INFO_ARCHIVE_PATH_FILE INFO_COPY_EXT, .comment = "repo1: remove archive.info.copy");
        HRN_STORAGE_REMOVE(
            storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE INFO_COPY_EXT, .comment = "repo1: remove backup.info.copy");
 
        HRN_INFO_PUT(
            storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE,
            "[db]\n"
            "db-catalog-version=202506291\n"
            "db-control-version=1800\n"
            "db-id=1\n"
            "db-system-id=" HRN_PG_SYSTEMID_17_Z "\n"
            "db-version=\"18\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-catalog-version\":202506291,\"db-control-version\":1800,\"db-system-id\":" HRN_PG_SYSTEMID_17_Z
            ",\"db-version\":\"18\"}\n");
 
        HRN_INFO_PUT(
            storageRepoIdxWrite(0), INFO_ARCHIVE_PATH_FILE,
            "[db]\n"
            "db-id=1\n"
            "db-system-id=" HRN_PG_SYSTEMID_17_Z "\n"
            "db-version=\"18\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-id\":" HRN_PG_SYSTEMID_17_Z ",\"db-version\":\"18\"}\n");
 
        TEST_ERROR(
            cmdStanzaCreate(), FileInvalidError,
            "backup and archive info files exist but do not match the database\n"
            "HINT: is this the correct stanza?\n"
            "HINT: did an error occur during stanza-upgrade?");
        TEST_RESULT_LOG("P00   INFO: stanza-create for stanza 'db' on repo1");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("archive.info and backup.info files that match but do not match the current database system-id");
 
        HRN_INFO_PUT(
            storageRepoIdxWrite(0), INFO_ARCHIVE_PATH_FILE,
            "[db]\n"
            "db-id=1\n"
            "db-system-id=6569239123849665999\n"
            "db-version=\"10\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-id\":6569239123849665999,\"db-version\":\"10\"}\n");
 
        HRN_INFO_PUT(
            storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE,
            "[db]\n"
            "db-catalog-version=201707211\n"
            "db-control-version=1002\n"
            "db-id=1\n"
            "db-system-id=6569239123849665999\n"
            "db-version=\"10\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-catalog-version\":201707211,\"db-control-version\":1002,\"db-system-id\":6569239123849665999"
            ",\"db-version\":\"10\"}\n");
 
        TEST_ERROR(
            cmdStanzaCreate(), FileInvalidError,
            "backup and archive info files exist but do not match the database\n"
            "HINT: is this the correct stanza?\n"
            "HINT: did an error occur during stanza-upgrade?");
        TEST_RESULT_LOG("P00   INFO: stanza-create for stanza 'db' on repo1");
 
        // Remove the info files and add sub directory to backup
        TEST_STORAGE_EXISTS(storageRepoIdxWrite(0), INFO_ARCHIVE_PATH_FILE, .remove = true);
        TEST_STORAGE_EXISTS(storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE, .remove = true);
        HRN_STORAGE_PATH_CREATE(
            storageRepoIdxWrite(0), STORAGE_REPO_BACKUP "/backup.history", .comment = "create directory in backup");
 
        TEST_ERROR(cmdStanzaCreate(), PathNotEmptyError, "backup directory not empty");
        TEST_RESULT_LOG("P00   INFO: stanza-create for stanza 'db' on repo1");
 
        // File in archive, directory in backup
        HRN_STORAGE_PUT_Z(storageRepoIdxWrite(0), STORAGE_REPO_ARCHIVE "/somefile", "some content");
        TEST_ERROR(cmdStanzaCreate(), PathNotEmptyError, "backup directory and/or archive directory not empty");
        TEST_RESULT_LOG("P00   INFO: stanza-create for stanza 'db' on repo1");
 
        // File in archive, backup empty
        HRN_STORAGE_PATH_REMOVE(storageRepoIdxWrite(0), STORAGE_REPO_BACKUP "/backup.history", .comment = "remove backup subdir");
        TEST_ERROR(cmdStanzaCreate(), PathNotEmptyError, "archive directory not empty");
        TEST_RESULT_LOG("P00   INFO: stanza-create for stanza 'db' on repo1");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("repeat last test using --force (deprecated)");
 
        hrnCfgArgRawBool(argList, cfgOptForce, true);
        HRN_CFG_LOAD(cfgCmdStanzaCreate, argList);
        TEST_ERROR(cmdStanzaCreate(), PathNotEmptyError, "archive directory not empty");
        TEST_RESULT_LOG(
            "P00   WARN: option --force is no longer supported\n"
            "P00   INFO: stanza-create for stanza 'db' on repo1");
 
        // Clean-up file in archive directory
        HRN_STORAGE_REMOVE(storageRepoIdxWrite(0), STORAGE_REPO_ARCHIVE "/somefile", .comment = "remove old test file");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("stanza-create forcing another PG version");
 
        argList = strLstDup(argListBase);
        hrnCfgArgRawZ(argList, cfgOptPgVersionForce, "15");
        HRN_CFG_LOAD(cfgCmdStanzaCreate, argList);
 
        HRN_PG_CONTROL_OVERRIDE_VERSION_PUT(storagePgWrite(), PG_VERSION_15, 1501, .catalogVersion = 202211111);
 
        TEST_RESULT_VOID(cmdStanzaCreate(), "stanza create - forcing another PG version");
        TEST_RESULT_LOG("P00   INFO: stanza-create for stanza 'db' on repo1");
 
        HRN_INFO_PUT(
            storageHrn, "test.info",
            "[db]\n"
            "db-catalog-version=202211111\n"
            "db-control-version=1300\n"
            "db-id=1\n"
            "db-system-id=" HRN_PG_SYSTEMID_15_Z "\n"
            "db-version=\"15\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-catalog-version\":202211111,\"db-control-version\":1300,\"db-system-id\":" HRN_PG_SYSTEMID_15_Z
            ",\"db-version\":\"15\"}\n",
            .comment = "put backup info to test file, control version for PG 15, catalog version forced from pg_control");
 
        TEST_RESULT_BOOL(
            bufEq(
                storageGetP(storageNewReadP(storageRepoIdx(0), INFO_BACKUP_PATH_FILE_STR)),
                storageGetP(storageNewReadP(storageHrn, STRDEF("test.info")))),
            true, "test and stanza backup info files are equal");
    }
 
    // *****************************************************************************************************************************
    if (testBegin("pgValidate(), online=y"))
    {
        // Load Parameters
        StringList *argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, TEST_STANZA);
        hrnCfgArgRawZ(argList, cfgOptPgPath, TEST_PATH "/pg");
        hrnCfgArgRawZ(argList, cfgOptRepoPath, TEST_PATH "/repo");
        HRN_CFG_LOAD(cfgCmdStanzaCreate, argList);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("pgControl and database match");
 
        // Create pg_control
        HRN_PG_CONTROL_PUT(storagePgWrite(), PG_VERSION_18);
 
        HRN_PQ_SCRIPT_SET(
            HRN_PQ_SCRIPT_OPEN(1, "dbname='postgres' port=5432", PG_VERSION_18, TEST_PATH "/pg", false, NULL, NULL));
 
        TEST_RESULT_VOID(cmdStanzaCreate(), "stanza create - db online");
        TEST_RESULT_LOG("P00   INFO: stanza-create for stanza 'db' on repo1");
        TEST_STORAGE_LIST(
            storageTest, TEST_PATH "/repo",
            "archive/\n"
            "archive/db/\n"
            "archive/db/archive.info\n"
            "archive/db/archive.info.copy\n"
            "backup/\n"
            "backup/db/\n"
            "backup/db/backup.info\n"
            "backup/db/backup.info.copy\n",
            .comment = "stanza created");
 
        HRN_CFG_LOAD(cfgCmdStanzaUpgrade, argList);
        HRN_PQ_SCRIPT_SET(
            HRN_PQ_SCRIPT_OPEN(1, "dbname='postgres' port=5432", PG_VERSION_18, TEST_PATH "/pg", false, NULL, NULL));
 
        TEST_RESULT_VOID(cmdStanzaUpgrade(), "stanza upgrade - db online");
        TEST_RESULT_LOG(
            "P00   INFO: stanza-upgrade for stanza 'db' on repo1\n"
            "P00   INFO: stanza 'db' on repo1 is already up to date");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("pg_control and version mismatch");
 
        // Create pg_control with different version
        HRN_PG_CONTROL_PUT(storagePgWrite(), PG_VERSION_10);
 
        HRN_PQ_SCRIPT_SET(
            HRN_PQ_SCRIPT_OPEN(1, "dbname='postgres' port=5432", PG_VERSION_11, TEST_PATH "/pg", false, NULL, NULL));
 
        TEST_ERROR(
            pgValidate(), DbMismatchError,
            "version '" PG_VERSION_11_Z "' and path '" TEST_PATH "/pg' queried from cluster do not match version '" PG_VERSION_10_Z
            "' and '" TEST_PATH "/pg' read from '" TEST_PATH "/pg/" PG_PATH_GLOBAL "/" PG_FILE_PGCONTROL "'\n"
            "HINT: the pg1-path and pg1-port settings likely reference different clusters.");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("pg_control and path mismatch");
 
        // Create pg_control
        HRN_PG_CONTROL_PUT(storagePgWrite(), PG_VERSION_15);
 
        HRN_PQ_SCRIPT_SET(
            HRN_PQ_SCRIPT_OPEN(1, "dbname='postgres' port=5432", PG_VERSION_15, TEST_PATH "/pg2", false, NULL, NULL));
 
        TEST_ERROR(
            pgValidate(), DbMismatchError,
            "version '" PG_VERSION_15_Z "' and path '" TEST_PATH "/pg2' queried from cluster do not match version '" PG_VERSION_15_Z
            "' and '" TEST_PATH "/pg' read from '" TEST_PATH "/pg/" PG_PATH_GLOBAL "/" PG_FILE_PGCONTROL "'\n"
            "HINT: the pg1-path and pg1-port settings likely reference different clusters.");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("primary at pg2");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, TEST_STANZA);
        hrnCfgArgRawZ(argList, cfgOptPgPath, TEST_PATH "/pg");
        hrnCfgArgRawZ(argList, cfgOptRepoPath, TEST_PATH "/repo");
        hrnCfgArgKeyRawZ(argList, cfgOptPgPath, 2, TEST_PATH "/pg1");
        hrnCfgArgKeyRawZ(argList, cfgOptPgPort, 2, "5434");
 
        HRN_CFG_LOAD(cfgCmdStanzaCreate, argList);
 
        // Create pg_control for primary
        HRN_PG_CONTROL_PUT(storagePgIdxWrite(1), PG_VERSION_13);
 
        // Create pg_control for standby
        HRN_PG_CONTROL_PUT(storagePgIdxWrite(0), PG_VERSION_13);
 
        HRN_PQ_SCRIPT_SET(
            HRN_PQ_SCRIPT_OPEN(1, "dbname='postgres' port=5432", PG_VERSION_13, TEST_PATH "/pg", true, NULL, NULL),
            HRN_PQ_SCRIPT_OPEN(2, "dbname='postgres' port=5434", PG_VERSION_13, TEST_PATH "/pg1", false, NULL, NULL));
 
        PgControl pgControl = {0};
        TEST_ASSIGN(pgControl, pgValidate(), "validate primary on pg2");
        TEST_RESULT_UINT(pgControl.version, PG_VERSION_13, "version set");
        TEST_RESULT_UINT(pgControl.systemId, HRN_PG_SYSTEMID_13, "systemId set");
        TEST_RESULT_UINT(pgControl.catalogVersion, 202007201, "catalogVersion set");
    }
 
    // *****************************************************************************************************************************
    if (testBegin("cmdStanzaUpgrade()"))
    {
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("stanza-upgrade - config errors");
 
        // Load Parameters
        StringList *argList = strLstDup(argListBase);
        hrnCfgArgKeyRawZ(argList, cfgOptRepoPath, 2, TEST_PATH "/repo2");
        hrnCfgArgRawZ(argList, cfgOptRepo, "2");
 
        TEST_ERROR(
            hrnCfgLoadP(cfgCmdStanzaUpgrade, argList), OptionInvalidError,
            "option 'repo' not valid for command 'stanza-upgrade'");
 
        HRN_CFG_LOAD(cfgCmdStanzaUpgrade, argListBase);
 
        // Create the stop file, test and remove
        HRN_STORAGE_PUT_EMPTY(storageHrn, strZ(lockStopFileName(cfgOptionStr(cfgOptStanza))));
        TEST_ERROR(cmdStanzaUpgrade(), StopError, "stop file exists for stanza db");
        HRN_STORAGE_REMOVE(storageHrn, strZ(lockStopFileName(cfgOptionStr(cfgOptStanza))));
 
        // -------------------------------------------------------------------------------------------------------------------------
        // Create pg_control for the rest of the tests
        HRN_PG_CONTROL_PUT(storagePgWrite(), PG_VERSION_10);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("stanza-upgrade - info file mismatch: db-id");
 
        // Stanza with only archive.info and backup.info but no .copy files
        HRN_INFO_PUT(
            storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE,
            "[db]\n"
            "db-catalog-version=201707211\n"
            "db-control-version=1002\n"
            "db-id=1\n"
            "db-system-id=" HRN_PG_SYSTEMID_10_Z "\n"
            "db-version=\"10\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-catalog-version\":201707211,\"db-control-version\":1002,\"db-system-id\":" HRN_PG_SYSTEMID_10_Z
            ",\"db-version\":\"10\"}\n");
 
        // backup info up to date but archive info db-id mismatch
        HRN_INFO_PUT(
            storageRepoIdxWrite(0), INFO_ARCHIVE_PATH_FILE,
            "[db]\n"
            "db-id=2\n"
            "db-system-id=" HRN_PG_SYSTEMID_10_Z "\n"
            "db-version=\"10\"\n"
            "\n"
            "[db:history]\n"
            "2={\"db-id\":" HRN_PG_SYSTEMID_10_Z ",\"db-version\":\"10\"}\n");
 
        TEST_ERROR(
            cmdStanzaUpgrade(), FileInvalidError,
            "backup info file and archive info file do not match\n"
            "archive: id = 2, version = 10, system-id = " HRN_PG_SYSTEMID_10_Z "\n"
            "backup : id = 1, version = 10, system-id = " HRN_PG_SYSTEMID_10_Z "\n"
            "HINT: this may be a symptom of repository corruption!");
        TEST_RESULT_LOG("P00   INFO: stanza-upgrade for stanza 'db' on repo1");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("stanza-upgrade - info file mismatch: archive version");
 
        // backup info up to date but archive info version is not
        HRN_INFO_PUT(
            storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE,
            "[db]\n"
            "db-catalog-version=201707211\n"
            "db-control-version=1002\n"
            "db-id=2\n"
            "db-system-id=" HRN_PG_SYSTEMID_10_Z "\n"
            "db-version=\"10\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-catalog-version\":202506291,\"db-control-version\":1800,\"db-system-id\":6569239123849665999"
            ",\"db-version\":\"18\"}\n"
            "2={\"db-catalog-version\":201707211,\"db-control-version\":1002,\"db-system-id\":" HRN_PG_SYSTEMID_10_Z
            ",\"db-version\":\"10\"}\n");
        HRN_INFO_PUT(
            storageRepoIdxWrite(0), INFO_ARCHIVE_PATH_FILE,
            "[db]\n"
            "db-id=1\n"
            "db-system-id=" HRN_PG_SYSTEMID_17_Z "\n"
            "db-version=\"18\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-id\":" HRN_PG_SYSTEMID_17_Z ",\"db-version\":\"18\"}\n");
 
        TEST_RESULT_VOID(cmdStanzaUpgrade(), "stanza upgrade - archive.info file upgraded - version");
        TEST_RESULT_LOG("P00   INFO: stanza-upgrade for stanza 'db' on repo1");
 
        HRN_INFO_PUT(
            storageHrn, "test.info",
            "[db]\n"
            "db-id=2\n"
            "db-system-id=" HRN_PG_SYSTEMID_10_Z "\n"
            "db-version=\"10\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-id\":" HRN_PG_SYSTEMID_17_Z ",\"db-version\":\"18\"}\n"
            "2={\"db-id\":" HRN_PG_SYSTEMID_10_Z ",\"db-version\":\"10\"}\n",
            .comment = "put archive info to test file");
 
        TEST_RESULT_BOOL(
            bufEq(
                storageGetP(storageNewReadP(storageRepoIdx(0), INFO_ARCHIVE_PATH_FILE_STR)),
                storageGetP(storageNewReadP(storageRepoIdx(0), STRDEF(INFO_ARCHIVE_PATH_FILE INFO_COPY_EXT)))) &&
            bufEq(
                storageGetP(storageNewReadP(storageRepoIdx(0), INFO_ARCHIVE_PATH_FILE_STR)),
                storageGetP(storageNewReadP(storageHrn, STRDEF("test.info")))),
            true, "test and stanza archive info files are equal");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("stanza-upgrade - info file mismatch: backup version");
 
        // archive info up to date but backup info version is not
        HRN_INFO_PUT(
            storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE,
            "[db]\n"
            "db-catalog-version=202506291\n"
            "db-control-version=1800\n"
            "db-id=1\n"
            "db-system-id=" HRN_PG_SYSTEMID_17_Z "\n"
            "db-version=\"18\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-catalog-version\":202506291,\"db-control-version\":1800,\"db-system-id\":" HRN_PG_SYSTEMID_17_Z
            ",\"db-version\":\"18\"}\n");
 
        TEST_RESULT_VOID(cmdStanzaUpgrade(), "stanza upgrade - backup.info file upgraded - version");
        TEST_RESULT_LOG("P00   INFO: stanza-upgrade for stanza 'db' on repo1");
 
        HRN_INFO_PUT(
            storageHrn, "test.info",
            "[db]\n"
            "db-catalog-version=201707211\n"
            "db-control-version=1002\n"
            "db-id=2\n"
            "db-system-id=" HRN_PG_SYSTEMID_10_Z "\n"
            "db-version=\"10\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-catalog-version\":202506291,\"db-control-version\":1800,\"db-system-id\":" HRN_PG_SYSTEMID_17_Z
            ",\"db-version\":\"18\"}\n"
            "2={\"db-catalog-version\":201707211,\"db-control-version\":1002,\"db-system-id\":" HRN_PG_SYSTEMID_10_Z
            ",\"db-version\":\"10\"}\n",
            .comment = "put backup info to test file");
 
        TEST_RESULT_BOOL(
            bufEq(
                storageGetP(storageNewReadP(storageRepoIdx(0), INFO_BACKUP_PATH_FILE_STR)),
                storageGetP(storageNewReadP(storageRepoIdx(0), STRDEF(INFO_BACKUP_PATH_FILE INFO_COPY_EXT)))) &&
            bufEq(
                storageGetP(storageNewReadP(storageRepoIdx(0), INFO_BACKUP_PATH_FILE_STR)),
                storageGetP(storageNewReadP(storageHrn, STRDEF("test.info")))),
            true, "test and stanza backup info files are equal");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("stanza-upgrade - info file mismatch: archive system-id");
 
        // backup info up to date but archive info system-id is not
        HRN_INFO_PUT(
            storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE,
            "[db]\n"
            "db-catalog-version=201707211\n"
            "db-control-version=1002\n"
            "db-id=2\n"
            "db-system-id=" HRN_PG_SYSTEMID_10_Z "\n"
            "db-version=\"10\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-catalog-version\":202506291,\"db-control-version\":1800,\"db-system-id\":6569239123849665999"
            ",\"db-version\":\"18\"}\n"
            "2={\"db-catalog-version\":201707211,\"db-control-version\":1002,\"db-system-id\":" HRN_PG_SYSTEMID_10_Z
            ",\"db-version\":\"10\"}\n");
        HRN_INFO_PUT(
            storageRepoIdxWrite(0), INFO_ARCHIVE_PATH_FILE,
            "[db]\n"
            "db-id=1\n"
            "db-system-id=6569239123849665999\n"
            "db-version=\"10\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-id\":6569239123849665999,\"db-version\":\"10\"}\n");
 
        TEST_RESULT_VOID(cmdStanzaUpgrade(), "stanza upgrade - archive.info file upgraded - system-id");
        TEST_RESULT_LOG("P00   INFO: stanza-upgrade for stanza 'db' on repo1");
 
        HRN_INFO_PUT(
            storageHrn, "test.info",
            "[db]\n"
            "db-id=2\n"
            "db-system-id=" HRN_PG_SYSTEMID_10_Z "\n"
            "db-version=\"10\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-id\":6569239123849665999,\"db-version\":\"10\"}\n"
            "2={\"db-id\":" HRN_PG_SYSTEMID_10_Z ",\"db-version\":\"10\"}\n",
            .comment = "put archive info to test file");
 
        TEST_RESULT_BOOL(
            bufEq(
                storageGetP(storageNewReadP(storageRepoIdx(0), INFO_ARCHIVE_PATH_FILE_STR)),
                storageGetP(storageNewReadP(storageRepoIdx(0), STRDEF(INFO_ARCHIVE_PATH_FILE INFO_COPY_EXT)))) &&
            bufEq(
                storageGetP(storageNewReadP(storageRepoIdx(0), INFO_ARCHIVE_PATH_FILE_STR)),
                storageGetP(storageNewReadP(storageHrn, STRDEF("test.info")))),
            true, "test and stanza archive info files are equal");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("stanza-upgrade - info file mismatch: backup system-id");
 
        // archive info up to date but backup info system-id is not
        HRN_INFO_PUT(
            storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE,
            "[db]\n"
            "db-catalog-version=201707211\n"
            "db-control-version=1002\n"
            "db-id=1\n"
            "db-system-id=6569239123849665999\n"
            "db-version=\"10\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-catalog-version\":201707211,\"db-control-version\":1002,\"db-system-id\":6569239123849665999"
            ",\"db-version\":\"10\"}\n");
 
        TEST_RESULT_VOID(cmdStanzaUpgrade(), "stanza upgrade - backup.info file upgraded - system-id");
        TEST_RESULT_LOG("P00   INFO: stanza-upgrade for stanza 'db' on repo1");
 
        HRN_INFO_PUT(
            storageHrn, "test.info",
            "[db]\n"
            "db-catalog-version=201707211\n"
            "db-control-version=1002\n"
            "db-id=2\n"
            "db-system-id=" HRN_PG_SYSTEMID_10_Z "\n"
            "db-version=\"10\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-catalog-version\":201707211,\"db-control-version\":1002,\"db-system-id\":6569239123849665999"
            ",\"db-version\":\"10\"}\n"
            "2={\"db-catalog-version\":201707211,\"db-control-version\":1002,\"db-system-id\":" HRN_PG_SYSTEMID_10_Z
            ",\"db-version\":\"10\"}\n",
            .comment = "put backup info to test file");
        TEST_RESULT_BOOL(
            bufEq(
                storageGetP(storageNewReadP(storageRepoIdx(0), INFO_BACKUP_PATH_FILE_STR)),
                storageGetP(storageNewReadP(storageRepoIdx(0), STRDEF(INFO_BACKUP_PATH_FILE INFO_COPY_EXT)))) &&
            bufEq(
                storageGetP(storageNewReadP(storageRepoIdx(0), INFO_BACKUP_PATH_FILE_STR)),
                storageGetP(storageNewReadP(storageHrn, STRDEF("test.info")))),
            true, "test and stanza backup info files are equal");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("stanza-upgrade forcing another PG version");
 
        argList = strLstDup(argListBase);
        hrnCfgArgRawZ(argList, cfgOptPgVersionForce, "15");
        HRN_CFG_LOAD(cfgCmdStanzaUpgrade, argList);
 
        HRN_PG_CONTROL_OVERRIDE_VERSION_PUT(storagePgWrite(), PG_VERSION_15, 1501, .catalogVersion = 202211111);
 
        HRN_INFO_PUT(
            storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE,
            "[db]\n"
            "db-catalog-version=201707211\n"
            "db-control-version=1002\n"
            "db-id=1\n"
            "db-system-id=6569239123849665999\n"
            "db-version=\"10\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-catalog-version\":201707211,\"db-control-version\":1002,\"db-system-id\":6569239123849665999"
            ",\"db-version\":\"10\"}\n");
 
        HRN_INFO_PUT(
            storageRepoIdxWrite(0), INFO_ARCHIVE_PATH_FILE,
            "[db]\n"
            "db-id=1\n"
            "db-system-id=6569239123849665999\n"
            "db-version=\"10\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-id\":6569239123849665999,\"db-version\":\"10\"}\n");
 
        TEST_RESULT_VOID(cmdStanzaUpgrade(), "stanza upgrade - forcing another PG version");
        TEST_RESULT_LOG("P00   INFO: stanza-upgrade for stanza 'db' on repo1");
 
        HRN_INFO_PUT(
            storageHrn, "test.info",
            "[db]\n"
            "db-id=2\n"
            "db-system-id=" HRN_PG_SYSTEMID_15_Z "\n"
            "db-version=\"15\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-id\":6569239123849665999,\"db-version\":\"10\"}\n"
            "2={\"db-id\":" HRN_PG_SYSTEMID_15_Z ",\"db-version\":\"15\"}\n",
            .comment = "put archive info to test file");
 
        TEST_RESULT_BOOL(
            bufEq(
                storageGetP(storageNewReadP(storageRepoIdx(0), INFO_ARCHIVE_PATH_FILE_STR)),
                storageGetP(storageNewReadP(storageHrn, STRDEF("test.info")))),
            true, "test and stanza archive info files are equal");
 
        HRN_INFO_PUT(
            storageHrn, "test.info",
            "[db]\n"
            "db-catalog-version=202211111\n"
            "db-control-version=1300\n"
            "db-id=2\n"
            "db-system-id=" HRN_PG_SYSTEMID_15_Z "\n"
            "db-version=\"15\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-catalog-version\":201707211,\"db-control-version\":1002,\"db-system-id\":6569239123849665999"
            ",\"db-version\":\"10\"}\n"
            "2={\"db-catalog-version\":202211111,\"db-control-version\":1300,\"db-system-id\":" HRN_PG_SYSTEMID_15_Z
            ",\"db-version\":\"15\"}\n",
            .comment = "put backup info to test file, control version for PG 15, catalog version forced from pg_control");
 
        TEST_RESULT_BOOL(
            bufEq(
                storageGetP(storageNewReadP(storageRepoIdx(0), INFO_BACKUP_PATH_FILE_STR)),
                storageGetP(storageNewReadP(storageHrn, STRDEF("test.info")))),
            true, "test and stanza backup info files are equal");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("stanza-upgrade - repository format");
 
        // The format is left alone when repo-format is not specified, since the option default would otherwise downgrade a
        // repository that has already been upgraded
        argList = strLstDup(argListBase);
        hrnCfgArgRawZ(argList, cfgOptPgVersionForce, "15");
        HRN_CFG_LOAD(cfgCmdStanzaUpgrade, argList);
 
        TEST_RESULT_VOID(cmdStanzaUpgrade(), "stanza upgrade - format not requested");
        TEST_RESULT_LOG(
            "P00   INFO: stanza-upgrade for stanza 'db' on repo1\n"
            "P00   INFO: stanza 'db' on repo1 is already up to date");
 
        TEST_RESULT_UINT(
            infoArchiveFormat(infoArchiveLoadFile(storageRepoIdx(0), INFO_ARCHIVE_PATH_FILE_STR, cipherSpecNewNone())),
            REPOSITORY_FORMAT_DEFAULT, "archive info still at default format");
 
        // A format in a configuration file is warned about and ignored, which is why the option is command line only. A format
        // left in the configuration would otherwise migrate a stanza as a side effect of an upgrade run for another reason.
        HRN_STORAGE_PUT_Z(
            storageTest, "repo-format.conf",
            "[global]\n"
            "repo1-format=6\n");
 
        argList = strLstDup(argListBase);
        hrnCfgArgRawZ(argList, cfgOptConfig, TEST_PATH "/repo-format.conf");
        hrnCfgArgRawZ(argList, cfgOptPgVersionForce, "15");
        HRN_CFG_LOAD(cfgCmdStanzaUpgrade, argList);
 
        TEST_RESULT_LOG("P00   WARN: configuration file contains command-line only option 'repo1-format'");
 
        TEST_RESULT_VOID(cmdStanzaUpgrade(), "stanza upgrade - format in configuration file");
        TEST_RESULT_LOG(
            "P00   INFO: stanza-upgrade for stanza 'db' on repo1\n"
            "P00   INFO: stanza 'db' on repo1 is already up to date");
 
        TEST_RESULT_UINT(
            infoArchiveFormat(infoArchiveLoadFile(storageRepoIdx(0), INFO_ARCHIVE_PATH_FILE_STR, cipherSpecNewNone())),
            REPOSITORY_FORMAT_DEFAULT, "archive info still at default format");
 
        // Upgrade the format
        argList = strLstDup(argListBase);
        hrnCfgArgRawZ(argList, cfgOptPgVersionForce, "15");
        hrnCfgArgRawZ(argList, cfgOptRepoFormat, "6");
        HRN_CFG_LOAD(cfgCmdStanzaUpgrade, argList);
 
        TEST_RESULT_VOID(cmdStanzaUpgrade(), "stanza upgrade - format 6");
        TEST_RESULT_LOG(
            "P00   INFO: stanza-upgrade for stanza 'db' on repo1\n"
            "P00   INFO: upgrade repository format from 5 to 6");
 
        TEST_RESULT_UINT(
            infoArchiveFormat(infoArchiveLoadFile(storageRepoIdx(0), INFO_ARCHIVE_PATH_FILE_STR, cipherSpecNewNone())),
            REPOSITORY_FORMAT_6, "archive info at format 6");
        TEST_RESULT_UINT(
            infoBackupFormat(infoBackupLoadFile(storageRepoIdx(0), INFO_BACKUP_PATH_FILE_STR, cipherSpecNewNone())),
            REPOSITORY_FORMAT_6, "backup info at format 6");
 
        // Requesting the format the repository is already at does nothing
        HRN_CFG_LOAD(cfgCmdStanzaUpgrade, argList);
 
        TEST_RESULT_VOID(cmdStanzaUpgrade(), "stanza upgrade - format 6 again");
        TEST_RESULT_LOG(
            "P00   INFO: stanza-upgrade for stanza 'db' on repo1\n"
            "P00   INFO: stanza 'db' on repo1 is already up to date");
 
        // The save of archive.info and backup.info is not atomic, so an upgrade interrupted between the two leaves the info files
        // at different formats. Running the upgrade again brings the lagging file forward.
        HRN_INFO_PUT(
            storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE,
            "[db]\n"
            "db-catalog-version=202211111\n"
            "db-control-version=1300\n"
            "db-id=2\n"
            "db-system-id=" HRN_PG_SYSTEMID_15_Z "\n"
            "db-version=\"15\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-catalog-version\":201707211,\"db-control-version\":1002,\"db-system-id\":6569239123849665999"
            ",\"db-version\":\"10\"}\n"
            "2={\"db-catalog-version\":202211111,\"db-control-version\":1300,\"db-system-id\":" HRN_PG_SYSTEMID_15_Z
            ",\"db-version\":\"15\"}\n",
            .comment = "put backup info at prior format to simulate an interrupted upgrade");
 
        TEST_RESULT_UINT(
            infoBackupFormat(infoBackupLoadFile(storageRepoIdx(0), INFO_BACKUP_PATH_FILE_STR, cipherSpecNewNone())),
            REPOSITORY_FORMAT_DEFAULT, "backup info back at default format");
 
        // An upgrade that does not request a format brings the lagging file forward since the higher of the two formats is the
        // only target that does not downgrade a file
        argList = strLstDup(argListBase);
        hrnCfgArgRawZ(argList, cfgOptPgVersionForce, "15");
        HRN_CFG_LOAD(cfgCmdStanzaUpgrade, argList);
 
        TEST_RESULT_VOID(cmdStanzaUpgrade(), "stanza upgrade - format mismatch between info files");
        TEST_RESULT_LOG(
            "P00   INFO: stanza-upgrade for stanza 'db' on repo1\n"
            "P00   WARN: repository format mismatch from an interrupted stanza-upgrade will be repaired\n"
            "P00   INFO: upgrade repository format from 5 to 6");
 
        TEST_RESULT_UINT(
            infoBackupFormat(infoBackupLoadFile(storageRepoIdx(0), INFO_BACKUP_PATH_FILE_STR, cipherSpecNewNone())),
            REPOSITORY_FORMAT_6, "backup info brought forward to format 6");
 
        // The lagging file is normally backup.info since archive.info is saved first, but the format the upgrade reports is the
        // lower of the two whichever file it is
        InfoArchive *const infoArchivePrior = infoArchiveLoadFile(
            storageRepoIdx(0), INFO_ARCHIVE_PATH_FILE_STR, cipherSpecNewNone());
        infoArchiveFormatSet(infoArchivePrior, REPOSITORY_FORMAT_5);
        infoArchiveSaveFile(infoArchivePrior, storageRepoIdxWrite(0), INFO_ARCHIVE_PATH_FILE_STR, cipherSpecNewNone());
 
        // A downgrade is measured against the file that is ahead, so requesting the format the archive info is at does not put
        // the backup info back to it
        argList = strLstDup(argListBase);
        hrnCfgArgRawZ(argList, cfgOptPgVersionForce, "15");
        hrnCfgArgRawZ(argList, cfgOptRepoFormat, "5");
        HRN_CFG_LOAD(cfgCmdStanzaUpgrade, argList);
 
        TEST_ERROR(
            cmdStanzaUpgrade(), FormatError,
            "unable to downgrade repository format from 6 to 5\n"
            "HINT: backups and archives already written at format 6 would not be readable by a version that only supports"
            " format 5.");
        TEST_RESULT_LOG("P00   INFO: stanza-upgrade for stanza 'db' on repo1");
 
        argList = strLstDup(argListBase);
        hrnCfgArgRawZ(argList, cfgOptPgVersionForce, "15");
        hrnCfgArgRawZ(argList, cfgOptRepoFormat, "6");
        HRN_CFG_LOAD(cfgCmdStanzaUpgrade, argList);
 
        TEST_RESULT_VOID(cmdStanzaUpgrade(), "stanza upgrade - archive info behind backup info");
        TEST_RESULT_LOG(
            "P00   INFO: stanza-upgrade for stanza 'db' on repo1\n"
            "P00   WARN: repository format mismatch from an interrupted stanza-upgrade will be repaired\n"
            "P00   INFO: upgrade repository format from 5 to 6");
 
        TEST_RESULT_UINT(
            infoBackupFormat(infoBackupLoadFile(storageRepoIdx(0), INFO_BACKUP_PATH_FILE_STR, cipherSpecNewNone())),
            REPOSITORY_FORMAT_6, "backup info brought forward to format 6");
        TEST_RESULT_UINT(
            infoArchiveFormat(infoArchiveLoadFile(storageRepoIdx(0), INFO_ARCHIVE_PATH_FILE_STR, cipherSpecNewNone())),
            REPOSITORY_FORMAT_6, "archive info still at format 6");
 
        // The format cannot be downgraded
        argList = strLstDup(argListBase);
        hrnCfgArgRawZ(argList, cfgOptPgVersionForce, "15");
        hrnCfgArgRawZ(argList, cfgOptRepoFormat, "5");
        HRN_CFG_LOAD(cfgCmdStanzaUpgrade, argList);
 
        TEST_ERROR(
            cmdStanzaUpgrade(), FormatError,
            "unable to downgrade repository format from 6 to 5\n"
            "HINT: backups and archives already written at format 6 would not be readable by a version that only supports"
            " format 5.");
        TEST_RESULT_LOG("P00   INFO: stanza-upgrade for stanza 'db' on repo1");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("stanza-upgrade - repository format from the environment");
 
        // Put the repository back to the default format so it can be migrated again. This is done directly since the command
        // refuses to downgrade.
        InfoArchive *const infoArchiveReset = infoArchiveLoadFile(
            storageRepoIdx(0), INFO_ARCHIVE_PATH_FILE_STR, cipherSpecNewNone());
        infoArchiveFormatSet(infoArchiveReset, REPOSITORY_FORMAT_DEFAULT);
        infoArchiveSaveFile(infoArchiveReset, storageRepoIdxWrite(0), INFO_ARCHIVE_PATH_FILE_STR, cipherSpecNewNone());
 
        InfoBackup *const infoBackupReset = infoBackupLoadFile(storageRepoIdx(0), INFO_BACKUP_PATH_FILE_STR, cipherSpecNewNone());
        infoBackupFormatSet(infoBackupReset, REPOSITORY_FORMAT_DEFAULT);
        infoBackupSaveFile(infoBackupReset, storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE_STR, cipherSpecNewNone());
 
        // The option cannot be set in a configuration file but it can be set in the environment, where it migrates a stanza
        // without appearing in the command that ran. That is how the environment works for every option so it is allowed, but the
        // migration must still happen.
        argList = strLstDup(argListBase);
        hrnCfgArgRawZ(argList, cfgOptPgVersionForce, "15");
        hrnCfgEnvKeyRawZ(cfgOptRepoFormat, 1, "6");
        HRN_CFG_LOAD(cfgCmdStanzaUpgrade, argList);
 
        TEST_RESULT_VOID(cmdStanzaUpgrade(), "stanza upgrade - format from the environment");
        TEST_RESULT_LOG(
            "P00   INFO: stanza-upgrade for stanza 'db' on repo1\n"
            "P00   INFO: upgrade repository format from 5 to 6");
 
        hrnCfgEnvKeyRemoveRaw(cfgOptRepoFormat, 1);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("stanza-upgrade - a file encrypted before a format migration is readable after it");
 
        // A stanza on an encrypted repository at the format before the digest could be stored
        argList = strLstNew();
        hrnCfgArgRawBool(argList, cfgOptOnline, false);
        hrnCfgArgRawZ(argList, cfgOptStanza, TEST_STANZA);
        hrnCfgArgRawZ(argList, cfgOptPgPath, TEST_PATH "/pg");
        hrnCfgArgKeyRawZ(argList, cfgOptRepoPath, 1, TEST_PATH "/repo-migrate");
        hrnCfgArgRawZ(argList, cfgOptPgVersionForce, "15");
        hrnCfgArgKeyRawStrId(argList, cfgOptRepoCipherType, 1, cipherTypeAes256Cbc);
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 1, "12345678");
        HRN_CFG_LOAD(cfgCmdStanzaCreate, argList);
 
        TEST_RESULT_VOID(cmdStanzaCreate(), "stanza create on encrypted repo");
        TEST_RESULT_LOG("P00   INFO: stanza-create for stanza 'db' on repo1");
 
        const CipherSpec *const cipherSpecMain = cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF("12345678"));
        const InfoArchive *infoArchiveMigrate = NULL;
 
        TEST_ASSIGN(
            infoArchiveMigrate, infoArchiveLoadFile(storageRepoIdx(0), INFO_ARCHIVE_PATH_FILE_STR, cipherSpecMain),
            "load archive info before migration");
 
        // Write a file with the archive sub-pass, which is how everything below the info files is encrypted
        Buffer *const walBuffer = bufNew(0);
        IoWrite *const walWrite = ioBufferWriteNew(walBuffer);
 
        cipherBlockFilterGroupAdd(
            ioWriteFilterGroup(walWrite), cipherModeEncrypt, infoArchiveCipherSpec(infoArchiveMigrate));
        ioWriteOpen(walWrite);
        ioWrite(walWrite, BUFSTRDEF("WAL BEFORE MIGRATION"));
        ioWriteClose(walWrite);
 
        HRN_STORAGE_PUT(storageRepoIdxWrite(0), TEST_WAL_MIGRATE, walBuffer, .comment = "wal written at format 5");
 
        // Migrate the repository
        hrnCfgArgKeyRawZ(argList, cfgOptRepoFormat, 1, "6");
        HRN_CFG_LOAD(cfgCmdStanzaUpgrade, argList);
 
        const time_t timeMigrate = time(NULL);
 
        TEST_RESULT_VOID(cmdStanzaUpgrade(), "stanza upgrade - format 6 on encrypted repo");
        TEST_RESULT_LOG(
            "P00   INFO: stanza-upgrade for stanza 'db' on repo1\n"
            "P00   INFO: upgrade repository format from 5 to 6\n"
            "P00   WARN: option 'repo1-cipher-pass' should be at least 256 random bits encoded as hex, base64, or base64url at"
            " repository format 6 or higher\n"
            "            HINT: generate a passphrase from at least 256 random bits, e.g. with 'openssl rand -base64 32', and"
            " change it with stanza-rekey.");
 
        // The pass is unchanged by the migration, so a file it encrypted before the migration must still be readable. The digest
        // the pass derives with cannot follow the format of the info file storing it or this read would fail.
        TEST_ASSIGN(
            infoArchiveMigrate, infoArchiveLoadFile(storageRepoIdx(0), INFO_ARCHIVE_PATH_FILE_STR, cipherSpecMain),
            "load archive info after migration");
        TEST_RESULT_UINT(infoArchiveFormat(infoArchiveMigrate), REPOSITORY_FORMAT_6, "archive info at format 6");
 
        // The migrated key stays at id 0 and a key for new WAL is added at id 1
        const CipherSpecMap *const cipherSpecMapMigrate = infoArchiveCipherSpecMap(infoArchiveMigrate);
 
        TEST_RESULT_UINT(cipherSpecMapSize(cipherSpecMapMigrate), 2, "migrated and new archive key");
        TEST_RESULT_UINT(
            cipherSpecDigest(cipherSpecMapGet(cipherSpecMapMigrate, CIPHER_SPEC_MAP_ID_DEFAULT_STR)), hashTypeSha1,
            "migrated key derives with sha1");
        TEST_RESULT_UINT(
            cipherSpecDigest(cipherSpecMapGet(cipherSpecMapMigrate, STRDEF("1"))), hashTypeSha256,
            "new key derives with sha256");
        TEST_RESULT_STR_Z(cipherSpecMapIdCurrent(cipherSpecMapMigrate), "1", "new key is current");
        TEST_RESULT_BOOL(
            infoArchiveCipherRotateTime(infoArchiveMigrate) >= timeMigrate &&
            infoArchiveCipherRotateTime(infoArchiveMigrate) <= time(NULL),
            true, "rotation time is the migration time");
 
        StorageRead *const walRead = storageNewReadP(storageRepoIdx(0), STRDEF(TEST_WAL_MIGRATE));
 
        cipherBlockFilterGroupAdd(
            ioReadFilterGroup(storageReadIo(walRead)), cipherModeDecrypt, infoArchiveCipherSpec(infoArchiveMigrate));
 
        TEST_RESULT_STR_Z(
            strNewBuf(storageGetP(walRead)), "WAL BEFORE MIGRATION", "wal from before the migration is still readable");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("stanza-upgrade - example passphrase");
 
        // Save the info files with the example passphrase
        const CipherSpec *const cipherSpecExample = cipherSpecNewP(
            cipherTypeAes256Cbc, BUFSTRDEF("zWaf6XtpjIVZC5444yXB+cgFDFl7MxGlgkZSaoPvTGi="));
 
        infoArchiveSaveFile(
            infoArchiveLoadFile(storageRepoIdx(0), INFO_ARCHIVE_PATH_FILE_STR, cipherSpecMain), storageRepoIdxWrite(0),
            INFO_ARCHIVE_PATH_FILE_STR, cipherSpecExample);
 
        InfoBackup *const infoBackupExample = infoBackupLoadFile(storageRepoIdx(0), INFO_BACKUP_PATH_FILE_STR, cipherSpecMain);
        infoBackupFormatSet(infoBackupExample, REPOSITORY_FORMAT_6);
        infoBackupSaveFile(infoBackupExample, storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE_STR, cipherSpecExample);
 
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 1, "zWaf6XtpjIVZC5444yXB+cgFDFl7MxGlgkZSaoPvTGi=");
        HRN_CFG_LOAD(cfgCmdStanzaUpgrade, argList);
 
        TEST_RESULT_VOID(cmdStanzaUpgrade(), "stanza upgrade");
        TEST_RESULT_LOG(
            "P00   INFO: stanza-upgrade for stanza 'db' on repo1\n"
            "P00   INFO: stanza 'db' on repo1 is already up to date\n"
            "P00   WARN: option 'repo1-cipher-pass' should not be the example passphrase from the documentation\n"
            "            HINT: generate a passphrase from at least 256 random bits, e.g. with 'openssl rand -base64 32', and"
            " change it with stanza-rekey.");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("stanza-upgrade - no passphrase warning with the old passphrase set");
 
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPassOld, 1, "12345678");
        HRN_CFG_LOAD(cfgCmdStanzaUpgrade, argList);
 
        TEST_RESULT_VOID(cmdStanzaUpgrade(), "stanza upgrade");
        TEST_RESULT_LOG(
            "P00   INFO: stanza-upgrade for stanza 'db' on repo1\n"
            "P00   INFO: stanza 'db' on repo1 is already up to date");
 
        hrnCfgEnvKeyRemoveRaw(cfgOptRepoCipherPassOld, 1);
        hrnCfgEnvKeyRemoveRaw(cfgOptRepoCipherPass, 1);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("stanza-upgrade - every format that can be read can be requested");
 
        // The allow list for repo-format in build/config.yaml and REPOSITORY_FORMAT_MIN/MAX in version.h are declared separately,
        // so make sure they match
        for (unsigned int format = REPOSITORY_FORMAT_MIN; format <= REPOSITORY_FORMAT_MAX; format++)
        {
            argList = strLstDup(argListBase);
            hrnCfgArgKeyRawFmt(argList, cfgOptRepoFormat, 1, "%u", format);
            HRN_CFG_LOAD(cfgCmdStanzaUpgrade, argList);
 
            TEST_RESULT_UINT(cfgOptionIdxUInt(cfgOptRepoFormat, 0), format, "format allowed");
        }
 
        const unsigned int formatUnsupported = REPOSITORY_FORMAT_MAX + 1;
 
        argList = strLstDup(argListBase);
        hrnCfgArgKeyRawFmt(argList, cfgOptRepoFormat, 1, "%u", formatUnsupported);
 
        TEST_ERROR_FMT(
            hrnCfgLoadP(cfgCmdStanzaUpgrade, argList), OptionInvalidValueError,
            "'%u' is not allowed for 'repo1-format' option\n"
            "HINT: allowed values are '5', '6'", formatUnsupported);
    }
 
    // *****************************************************************************************************************************
    if (testBegin("cmdStanzaDelete(), stanzaDelete()"))
    {
        // Load Parameters
        StringList *argListCmd = strLstNew();
        hrnCfgArgKeyRawZ(argListCmd, cfgOptRepoPath, 1, TEST_PATH "/repo");
 
        // Load Parameters
        StringList *argList = strLstDup(argListCmd);
        hrnCfgArgRawZ(argList, cfgOptStanza, TEST_STANZA_OTHER);
        hrnCfgArgKeyRawZ(argList, cfgOptPgPath, 1, TEST_PATH "/" TEST_STANZA_OTHER);
        hrnCfgArgRawBool(argList, cfgOptOnline, false);
        HRN_CFG_LOAD(cfgCmdStanzaCreate, argList);
 
        // Create pg_control for stanza-create
        HRN_PG_CONTROL_PUT(storagePgWrite(), PG_VERSION_10);
 
        TEST_RESULT_VOID(cmdStanzaCreate(), "create a stanza that will not be deleted");
        TEST_RESULT_LOG("P00   INFO: stanza-create for stanza 'otherstanza' on repo1");
 
        argList = strLstDup(argListCmd);
        hrnCfgArgRawZ(argList, cfgOptStanza, TEST_STANZA);
        hrnCfgArgKeyRawZ(argList, cfgOptPgPath, 1, TEST_PATH "/pg");
        HRN_CFG_LOAD(cfgCmdStanzaDelete, argList);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("stanza-delete - stanza already deleted");
 
        TEST_RESULT_VOID(cmdStanzaDelete(), "stanza delete - success on stanza does not exist");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("stanza-delete - only archive exists");
 
        // Confirm stanza does not exist
        TEST_STORAGE_LIST(
            storageTest, "repo/archive", "otherstanza/\n", .noRecurse = true,
            .comment = "stanza '" TEST_STANZA "' archive does not exist");
        TEST_STORAGE_LIST(
            storageTest, "repo/backup", "otherstanza/\n", .noRecurse = true,
            .comment = "stanza '" TEST_STANZA "' backup does not exist");
 
        // Create stanza archive only
        HRN_STORAGE_PUT_EMPTY(
            storageRepoWrite(), INFO_ARCHIVE_PATH_FILE, .comment = "create empty archive info for stanza '" TEST_STANZA "'");
        HRN_STORAGE_PUT_EMPTY(storageHrn, strZ(lockStopFileName(cfgOptionStr(cfgOptStanza))), .comment = "create stop file");
        TEST_STORAGE_LIST(
            storageTest, "repo/archive",
            "db/\n"
            "otherstanza/\n",
            .noRecurse = true, .comment = "stanza archive exists");
        TEST_RESULT_VOID(cmdStanzaDelete(), "stanza delete - archive only");
 
        TEST_STORAGE_LIST(
            storageTest, "repo/archive", "otherstanza/\n", .noRecurse = true, .comment = "stanza '" TEST_STANZA "' deleted");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("stanza-delete - only backup exists");
 
        // Create stanza backup only
        HRN_STORAGE_PUT_EMPTY(
            storageRepoWrite(), INFO_BACKUP_PATH_FILE,
            .comment = "create empty backup info for stanza '" TEST_STANZA "'");
        HRN_STORAGE_PUT_EMPTY(storageHrn, strZ(lockStopFileName(cfgOptionStr(cfgOptStanza))), .comment = "create stop file");
        TEST_STORAGE_LIST(
            storageTest, "repo/backup",
            "db/\n"
            "otherstanza/\n",
            .noRecurse = true, .comment = "stanza backup exists");
 
        TEST_RESULT_VOID(cmdStanzaDelete(), "stanza delete - backup only");
        TEST_STORAGE_LIST(
            storageTest, "repo/backup", "otherstanza/\n", .noRecurse = true, .comment = "stanza '" TEST_STANZA "' deleted");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("stanza-delete - empty directories");
 
        TEST_RESULT_VOID(cmdStanzaDelete(), "remove stanza '" TEST_STANZA "'");
 
        // Create only stanza paths
        HRN_STORAGE_PATH_CREATE(storageTest, "repo/archive/" TEST_STANZA, .comment = "create empty stanza archive path");
        HRN_STORAGE_PATH_CREATE(storageTest, "repo/backup/" TEST_STANZA, .comment = "create empty stanza backup path");
 
        TEST_RESULT_VOID(cmdStanzaDelete(), "stanza delete - empty directories");
        TEST_STORAGE_LIST(
            storageTest, "repo/archive", "otherstanza/\n", .noRecurse = true, .comment = "stanza '" TEST_STANZA "' deleted");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("delete errors when pg appears to be running");
 
        HRN_STORAGE_PUT_EMPTY(
            storageRepoWrite(), INFO_BACKUP_PATH_FILE, .comment = "create empty backup info for stanza '" TEST_STANZA "'");
        HRN_STORAGE_PUT_EMPTY(storageHrn, strZ(lockStopFileName(cfgOptionStr(cfgOptStanza))), .comment = "create stop file");
        HRN_STORAGE_PUT_EMPTY(storagePgWrite(), PG_FILE_POSTMTRPID, .comment = "create " PG_FILE_POSTMTRPID " file");
 
        TEST_ERROR(
            cmdStanzaDelete(), PgRunningError, PG_FILE_POSTMTRPID " exists - looks like " PG_NAME " is running. "
            "To delete stanza 'db' on repo1, shut down " PG_NAME " for stanza 'db' and try again, or use --force.");
 
        // Specify repo option
        StringList *argListDel = strLstDup(argList);
        hrnCfgArgKeyRawZ(argListDel, cfgOptRepoPath, 2, TEST_PATH "/repo2");
        hrnCfgArgRawZ(argListDel, cfgOptRepo, "2");
        HRN_CFG_LOAD(cfgCmdStanzaDelete, argListDel);
 
        HRN_STORAGE_PUT_EMPTY(storageRepoIdxWrite(1), INFO_BACKUP_PATH_FILE, .comment = "create empty backup info repo2");
 
        TEST_ERROR(
            cmdStanzaDelete(), PgRunningError, PG_FILE_POSTMTRPID " exists - looks like " PG_NAME " is running. "
            "To delete stanza 'db' on repo2, shut down " PG_NAME " for stanza 'db' and try again, or use --force.");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("force delete when pg appears to be running, multi-repo");
 
        argList = strLstDup(argListCmd);
        hrnCfgArgRawZ(argList, cfgOptStanza, TEST_STANZA);
        hrnCfgArgKeyRawFmt(argList, cfgOptPgPath, 1, TEST_PATH "/pg");
        hrnCfgArgKeyRawZ(argList, cfgOptRepoPath, 2, TEST_PATH "/repo2");
        hrnCfgArgRawZ(argList, cfgOptRepo, "1");
        hrnCfgArgRawBool(argList, cfgOptForce, true);
        HRN_CFG_LOAD(cfgCmdStanzaDelete, argList);
 
        TEST_RESULT_VOID(cmdStanzaDelete(), "stanza '" TEST_STANZA "' delete --force");
        TEST_RESULT_BOOL(storagePathExistsP(storageTest, strNewZ("repo/backup/" TEST_STANZA)), false, "repo1: stanza deleted");
        TEST_RESULT_BOOL(storagePathExistsP(storageTest, strNewZ("repo2/backup/" TEST_STANZA)), true, "repo2: stanza not deleted");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("ensure other stanza never deleted from repo1");
 
        TEST_STORAGE_LIST(
            storageRepo(), "",
            "archive/\n"
            "archive/otherstanza/\n"
            "archive/otherstanza/archive.info\n"
            "archive/otherstanza/archive.info.copy\n"
            "backup/\n"
            "backup/otherstanza/\n"
            "backup/otherstanza/backup.info\n"
            "backup/otherstanza/backup.info.copy\n",
            .comment = "otherstanza exists");
    }
 
    // *****************************************************************************************************************************
    if (testBegin("cmdStanzaRekey()"))
    {
        #define TEST_CIPHER_PASS_NEW                                "U3wR+u+uplH37Lo863u+aJ74uVMPqCAevLGzu2sajYE="
 
        const CipherSpec *const cipherSpecRekey = cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF(TEST_CIPHER_PASS_NEW));
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("unencrypted repo");
 
        StringList *argListRekey = strLstNew();
        hrnCfgArgRawZ(argListRekey, cfgOptStanza, TEST_STANZA);
        hrnCfgArgKeyRawZ(argListRekey, cfgOptRepoPath, 1, TEST_PATH "/repo");
 
        StringList *argList = strLstDup(argListRekey);
        HRN_CFG_LOAD(cfgCmdStanzaRekey, argList);
 
        TEST_ERROR(cmdStanzaRekey(), RepoInvalidError, "stanza-rekey requires an encrypted repository");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("encrypted stanzas at format 6 and format 5");
 
        argList = strLstDup(argListBase);
        hrnCfgArgKeyRawZ(argList, cfgOptRepoPath, 2, TEST_PATH "/repo2");
        hrnCfgArgKeyRawStrId(argList, cfgOptRepoCipherType, 2, cipherTypeAes256Cbc);
        hrnCfgArgKeyRawZ(argList, cfgOptRepoFormat, 2, "6");
        hrnCfgArgKeyRawZ(argList, cfgOptRepoPath, 3, TEST_PATH "/repo3");
        hrnCfgArgKeyRawStrId(argList, cfgOptRepoCipherType, 3, cipherTypeAes256Cbc);
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 2, TEST_CIPHER_PASS_FMT6);
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 3, "12345678");
        HRN_CFG_LOAD(cfgCmdStanzaCreate, argList);
 
        HRN_PG_CONTROL_PUT(storagePgWrite(), PG_VERSION_10);
 
        TEST_RESULT_VOID(cmdStanzaCreate(), "stanza create");
        TEST_RESULT_LOG(
            "P00   INFO: stanza-create for stanza 'db' on repo1\n"
            "P00   INFO: stanza-create for stanza 'db' on repo2\n"
            "P00   INFO: stanza-create for stanza 'db' on repo3");
 
        hrnCfgArgKeyRawZ(argListRekey, cfgOptRepoPath, 2, TEST_PATH "/repo2");
        hrnCfgArgKeyRawStrId(argListRekey, cfgOptRepoCipherType, 2, cipherTypeAes256Cbc);
        hrnCfgArgKeyRawZ(argListRekey, cfgOptRepoPath, 3, TEST_PATH "/repo3");
        hrnCfgArgKeyRawStrId(argListRekey, cfgOptRepoCipherType, 3, cipherTypeAes256Cbc);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("archive key rotation without a rekey");
 
        argList = strLstDup(argListRekey);
        hrnCfgArgRawZ(argList, cfgOptRepo, "2");
        HRN_CFG_LOAD(cfgCmdStanzaRekey, argList);
 
        TEST_RESULT_VOID(cmdStanzaRekey(), "stanza rekey");
        TEST_RESULT_LOG(
            "P00   INFO: stanza-rekey for stanza 'db' on repo2\n"
            "P00   INFO: rotate archive key to id 2");
        TEST_RESULT_STR_Z(
            cipherSpecMapIdCurrent(
                infoArchiveCipherSpecMap(
                    infoArchiveLoadFile(storageRepoIdx(1), INFO_ARCHIVE_PATH_FILE_STR, cfgCipherSpecMainIdx(1)))),
            "2", "archive key rotated");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("new passphrase equal to the old passphrase");
 
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPassOld, 2, TEST_CIPHER_PASS_FMT6);
        HRN_CFG_LOAD(cfgCmdStanzaRekey, argList);
 
        TEST_ERROR(
            cmdStanzaRekey(), OptionInvalidValueError,
            "option 'repo2-cipher-pass' must not be the same as option 'repo2-cipher-pass-old'");
        TEST_RESULT_LOG("P00   INFO: stanza-rekey for stanza 'db' on repo2");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("new passphrase that is not generated");
 
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 2, "12345678");
        HRN_CFG_LOAD(cfgCmdStanzaRekey, argList);
 
        TEST_ERROR(
            cmdStanzaRekey(), OptionInvalidValueError,
            "option 'repo2-cipher-pass' must be at least 256 random bits encoded as hex, base64, or base64url to rekey a stanza at"
            " repository format 6 or higher\n"
            "HINT: generate a passphrase from at least 256 random bits, e.g. with 'openssl rand -base64 32'.");
        TEST_RESULT_LOG("P00   INFO: stanza-rekey for stanza 'db' on repo2");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("rekey");
 
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 2, TEST_CIPHER_PASS_NEW);
        HRN_CFG_LOAD(cfgCmdStanzaRekey, argList);
 
        TEST_RESULT_VOID(cmdStanzaRekey(), "stanza rekey");
        TEST_RESULT_LOG(
            "P00   INFO: stanza-rekey for stanza 'db' on repo2\n"
            "P00   INFO: rotate archive key to id 3\n"
            "P00   INFO: rekey info files with the new passphrase");
 
        TEST_RESULT_STR_Z(
            cipherSpecMapIdCurrent(
                infoArchiveCipherSpecMap(infoArchiveLoadFile(storageRepoIdx(1), INFO_ARCHIVE_PATH_FILE_STR, cipherSpecRekey))),
            "3", "archive info rekeyed and rotated");
        TEST_RESULT_UINT(
            infoBackupFormat(infoBackupLoadFile(storageRepoIdx(1), INFO_BACKUP_PATH_FILE_STR, cipherSpecRekey)),
            REPOSITORY_FORMAT_6, "backup info rekeyed");
 
        HRN_STORAGE_REMOVE(storageRepoIdxWrite(1), INFO_ARCHIVE_PATH_FILE, .comment = "remove archive.info");
        HRN_STORAGE_REMOVE(storageRepoIdxWrite(1), INFO_BACKUP_PATH_FILE, .comment = "remove backup.info");
 
        TEST_RESULT_VOID(
            infoArchiveLoadFile(storageRepoIdx(1), INFO_ARCHIVE_PATH_FILE_STR, cipherSpecRekey), "archive info copy rekeyed");
        TEST_RESULT_VOID(
            infoBackupLoadFile(storageRepoIdx(1), INFO_BACKUP_PATH_FILE_STR, cipherSpecRekey), "backup info copy rekeyed");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("rekey again on every repo");
 
        argList = strLstDup(argListRekey);
        HRN_CFG_LOAD(cfgCmdStanzaRekey, argList);
 
        TEST_RESULT_VOID(cmdStanzaRekey(), "stanza rekey");
        TEST_RESULT_LOG(
            "P00   INFO: stanza-rekey for stanza 'db' on repo2\n"
            "P00   INFO: rotate archive key to id 4\n"
            "P00   INFO: rekey info files with the new passphrase\n"
            "P00   INFO: stanza-rekey for stanza 'db' on repo3\n"
            "P00   WARN: archive key rotation requires repository format 6 or higher");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("rekey at format 5");
 
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 3, "87654321");
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPassOld, 3, "12345678");
        argList = strLstDup(argListRekey);
        hrnCfgArgRawZ(argList, cfgOptRepo, "3");
        HRN_CFG_LOAD(cfgCmdStanzaRekey, argList);
 
        // Save archive.info with the old passphrase before the rekey
        TEST_RESULT_VOID(
            infoArchiveSaveFile(
                infoArchiveLoadFile(storageRepoIdx(2), INFO_ARCHIVE_PATH_FILE_STR, cfgCipherSpecMainIdx(2)), storageRepoIdxWrite(2),
                INFO_ARCHIVE_PATH_FILE_STR, cfgCipherSpecMainIdx(2)),
            "save archive info");
        TEST_RESULT_BOOL(
            infoArchiveCipherPrior(infoArchiveLoadFile(storageRepoIdx(2), INFO_ARCHIVE_PATH_FILE_STR, cfgCipherSpecMainIdx(2))),
            true, "archive info has old passphrase");
 
        // Save archive.info with the new passphrase and leave backup.info with the old passphrase
        InfoArchive *const infoArchiveRekey = infoArchiveLoadFile(
            storageRepoIdx(2), INFO_ARCHIVE_PATH_FILE_STR, cfgCipherSpecMainIdx(2));
 
        infoArchiveCipherPriorClear(infoArchiveRekey);
 
        TEST_RESULT_VOID(
            infoArchiveSaveFile(infoArchiveRekey, storageRepoIdxWrite(2), INFO_ARCHIVE_PATH_FILE_STR, cfgCipherSpecMainIdx(2)),
            "save archive info with new passphrase");
 
        TEST_RESULT_VOID(cmdStanzaRekey(), "stanza rekey");
        TEST_RESULT_LOG(
            "P00   INFO: stanza-rekey for stanza 'db' on repo3\n"
            "P00   INFO: rekey info files with the new passphrase");
        TEST_RESULT_UINT(
            infoBackupFormat(
                infoBackupLoadFile(
                    storageRepoIdx(2), INFO_BACKUP_PATH_FILE_STR, cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF("87654321")))),
            REPOSITORY_FORMAT_DEFAULT, "backup info rekeyed");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("missing stanza");
 
        hrnCfgEnvKeyRemoveRaw(cfgOptRepoCipherPassOld, 3);
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, TEST_STANZA_OTHER);
        hrnCfgArgRawZ(argList, cfgOptRepo, "3");
        hrnCfgArgKeyRawZ(argList, cfgOptRepoPath, 1, TEST_PATH "/repo");
        hrnCfgArgKeyRawZ(argList, cfgOptRepoPath, 3, TEST_PATH "/repo3");
        hrnCfgArgKeyRawStrId(argList, cfgOptRepoCipherType, 3, cipherTypeAes256Cbc);
        HRN_CFG_LOAD(cfgCmdStanzaRekey, argList);
 
        TEST_ERROR(
            cmdStanzaRekey(), FileMissingError,
            "unable to load info file '" TEST_PATH "/repo3/archive/otherstanza/archive.info' or"
            " '" TEST_PATH "/repo3/archive/otherstanza/archive.info.copy':\n"
            "FileMissingError: unable to open missing file '" TEST_PATH "/repo3/archive/otherstanza/archive.info' for read\n"
            "FileMissingError: unable to open missing file '" TEST_PATH "/repo3/archive/otherstanza/archive.info.copy' for read\n"
            "HINT: archive.info cannot be opened but is required to push/get WAL segments.\n"
            "HINT: is archive_command configured correctly in postgresql.conf?\n"
            "HINT: has a stanza-create been performed?\n"
            "HINT: use --no-archive-check to disable archive checks during backup if you have an alternate archiving scheme.");
        TEST_RESULT_LOG("P00   INFO: stanza-rekey for stanza 'otherstanza' on repo3");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("missing backup info");
 
        argList = strLstDup(argListRekey);
        hrnCfgArgRawZ(argList, cfgOptRepo, "3");
        HRN_CFG_LOAD(cfgCmdStanzaRekey, argList);
 
        HRN_STORAGE_REMOVE(storageRepoIdxWrite(2), INFO_BACKUP_PATH_FILE, .comment = "remove backup.info");
        HRN_STORAGE_REMOVE(storageRepoIdxWrite(2), INFO_BACKUP_PATH_FILE INFO_COPY_EXT, .comment = "remove backup.info.copy");
 
        TEST_ERROR(
            cmdStanzaRekey(), FileMissingError,
            "unable to load info file '" TEST_PATH "/repo3/backup/db/backup.info' or"
            " '" TEST_PATH "/repo3/backup/db/backup.info.copy':\n"
            "FileMissingError: unable to open missing file '" TEST_PATH "/repo3/backup/db/backup.info' for read\n"
            "FileMissingError: unable to open missing file '" TEST_PATH "/repo3/backup/db/backup.info.copy' for read\n"
            "HINT: backup.info cannot be opened and is required to perform a backup.\n"
            "HINT: has a stanza-create been performed?");
        TEST_RESULT_LOG("P00   INFO: stanza-rekey for stanza 'db' on repo3");
 
        hrnCfgEnvKeyRemoveRaw(cfgOptRepoCipherPass, 2);
        hrnCfgEnvKeyRemoveRaw(cfgOptRepoCipherPassOld, 2);
        hrnCfgEnvKeyRemoveRaw(cfgOptRepoCipherPass, 3);
    }
 
    FUNCTION_HARNESS_RETURN_VOID();
}