Исходный код СУБД 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
/***********************************************************************************************************************************
Test Repository Format
***********************************************************************************************************************************/
#include "common/io/bufferRead.h"
#include "common/io/bufferWrite.h"
#include "common/io/io.h"
#include "version.h"
 
/***********************************************************************************************************************************
Data for testing
***********************************************************************************************************************************/
#define TEST_PASS                                                   "areallybadpassphrase"
#define TEST_PLAINTEXT                                              "plaintext"
#define TEST_BUFFER_SIZE                                            256
 
/***********************************************************************************************************************************
Test Run
***********************************************************************************************************************************/
static void
testRun(void)
{
    FUNCTION_HARNESS_VOID();
 
    // *****************************************************************************************************************************
    if (testBegin("repoFormatValidate()"))
    {
        TEST_ERROR(
            repoFormatValidate(REPOSITORY_FORMAT_MIN - 1), FormatError,
            "repository format 4 is no longer supported by " PROJECT_NAME "\n"
            "HINT: " PROJECT_NAME " " PROJECT_VERSION " supports repository format 5 to 6.");
        TEST_ERROR(
            repoFormatValidate(REPOSITORY_FORMAT_MAX + 1), FormatError,
            "repository format 7 requires a newer version of " PROJECT_NAME "\n"
            "HINT: " PROJECT_NAME " " PROJECT_VERSION " supports repository format 5 to 6.");
 
        TEST_RESULT_VOID(repoFormatValidate(REPOSITORY_FORMAT_5), "format 5 is readable");
        TEST_RESULT_VOID(repoFormatValidate(REPOSITORY_FORMAT_6), "format 6 is readable");
    }
 
    // *****************************************************************************************************************************
    if (testBegin("repoFormatDigest()"))
    {
        TEST_RESULT_UINT(repoFormatDigest(REPOSITORY_FORMAT_5), hashTypeSha1, "format 5 derives with sha1");
        TEST_RESULT_UINT(repoFormatDigest(REPOSITORY_FORMAT_6), hashTypeSha256, "format 6 derives with sha256");
    }
 
    // *****************************************************************************************************************************
    if (testBegin("cipherBlockFormatNew()"))
    {
        const Buffer *const testPlainText = BUFSTRDEF(TEST_PLAINTEXT);
        const Buffer *const testPass = BUFSTRDEF(TEST_PASS);
        const CipherSpec *const cipherSpec = cipherSpecNewP(cipherTypeAes256Cbc, testPass);
 
        CipherSpecMap *const keyMapDefault = cipherSpecMapNew();
        cipherSpecMapAdd(keyMapDefault, CIPHER_SPEC_MAP_ID_DEFAULT_STR, cipherSpec);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("nothing is added when the repository is not encrypted");
 
        Buffer *plain = bufNew(0);
        IoWrite *plainWrite = ioBufferWriteNew(plain);
 
        TEST_RESULT_VOID(
            cipherBlockFormatFilterGroupWriteAddP(ioWriteFilterGroup(plainWrite), cipherSpecNewNone(), REPOSITORY_FORMAT_6),
            "add write filter for no cipher");
        ioWriteOpen(plainWrite);
        ioWrite(plainWrite, testPlainText);
        ioWriteClose(plainWrite);
 
        TEST_RESULT_STR_Z(strNewBuf(plain), TEST_PLAINTEXT, "content is stored as it is");
 
        IoWrite *plainRead = ioBufferWriteNew(bufNew(0));
        TEST_RESULT_PTR_NE(
            cipherBlockFormatFilterGroupReadAdd(ioWriteFilterGroup(plainRead), cipherSpecNewNone()), NULL,
            "add read filter for no cipher");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("write a header and read the format back from it");
 
        Buffer *headerBuffer = bufNew(0);
        IoWrite *headerWrite = ioBufferWriteNew(headerBuffer);
 
        cipherBlockFormatFilterGroupWriteAddP(ioWriteFilterGroup(headerWrite), cipherSpec, REPOSITORY_FORMAT_6);
        ioWriteOpen(headerWrite);
        ioWrite(headerWrite, testPlainText);
        ioWriteClose(headerWrite);
 
        TEST_RESULT_BOOL(
            memcmp(bufPtrConst(headerBuffer), CIPHER_BLOCK_FORMAT_MAGIC "006_", CIPHER_BLOCK_FORMAT_HEADER_SIZE) == 0, true,
            "header contains the format");
 
        // The format is not given on decrypt, so it comes from the header and is what the pass derives with
        Buffer *headerResult = bufNew(0);
        IoWrite *headerRead = ioBufferWriteNew(headerResult);
        IoFilterGroup *headerFilterGroup = ioWriteFilterGroup(headerRead);
 
        cipherBlockFormatFilterGroupReadAdd(headerFilterGroup, cipherSpec);
        ioWriteOpen(headerRead);
        ioWrite(headerRead, headerBuffer);
        ioWriteClose(headerRead);
 
        TEST_RESULT_STR_Z(strNewBuf(headerResult), TEST_PLAINTEXT, "content decrypted with the digest the header called for");
        TEST_RESULT_UINT(
            cipherBlockFormatResult(ioFilterGroupResultP(headerFilterGroup, CIPHER_BLOCK_FORMAT_FILTER_TYPE)),
            REPOSITORY_FORMAT_6, "filter reports the format");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("a file with no key id has a key check");
 
        Buffer *const keyCheckResult = bufNew(0);
        IoWrite *const keyCheckRead = ioBufferWriteNew(keyCheckResult);
 
        cipherBlockFormatFilterGroupReadAdd(
            ioWriteFilterGroup(keyCheckRead), cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF("other")));
        ioWriteOpen(keyCheckRead);
 
        TEST_ERROR(ioWrite(keyCheckRead, headerBuffer), CryptoError, "cipher passphrase does not match");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("replacing the header and key check with the magic gives the format openssl reads");
 
        Buffer *const opensslBuffer = bufNew(0);
        const size_t opensslSkip = CIPHER_BLOCK_FORMAT_HEADER_SIZE + CIPHER_BLOCK_KEY_CHECK_SIZE;
 
        bufCat(opensslBuffer, BUFSTRDEF(CIPHER_BLOCK_MAGIC));
        bufCatSub(opensslBuffer, headerBuffer, opensslSkip, bufUsed(headerBuffer) - opensslSkip);
 
        Buffer *const opensslResult = bufNew(0);
        IoWrite *const opensslRead = ioBufferWriteNew(opensslResult);
 
        cipherBlockFilterGroupAdd(
            ioWriteFilterGroup(opensslRead), cipherModeDecrypt,
            cipherSpecDupP(cipherSpec, .digestDefault = repoFormatDigest(REPOSITORY_FORMAT_6)));
        ioWriteOpen(opensslRead);
        ioWrite(opensslRead, opensslBuffer);
        ioWriteClose(opensslRead);
 
        TEST_RESULT_STR_Z(strNewBuf(opensslResult), TEST_PLAINTEXT, "content decrypted");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("content is handed on in pieces when the destination cannot take it whole");
 
        // Content long enough that decryption has output to hand on while input is still arriving, so the destination fills before
        // the source has been consumed
        Buffer *const piecesPlainText = bufNew(TEST_BUFFER_SIZE);
        memset(bufPtr(piecesPlainText), 'x', bufSize(piecesPlainText));
        bufUsedSet(piecesPlainText, bufSize(piecesPlainText));
 
        Buffer *const piecesBuffer = bufNew(0);
        IoWrite *const piecesWrite = ioBufferWriteNew(piecesBuffer);
 
        cipherBlockFormatFilterGroupWriteAddP(ioWriteFilterGroup(piecesWrite), cipherSpec, REPOSITORY_FORMAT_6);
        ioWriteOpen(piecesWrite);
        ioWrite(piecesWrite, piecesPlainText);
        ioWriteClose(piecesWrite);
 
        IoRead *const readPieces = ioBufferReadNew(piecesBuffer);
        Buffer *const piecesResult = bufNew(0);
        Buffer *const piece = bufNew(4);
 
        cipherBlockFormatFilterGroupReadAdd(ioReadFilterGroup(readPieces), cipherSpec);
 
        ioBufferSizeSet(4);
        ioReadOpen(readPieces);
 
        while (!ioReadEof(readPieces))
        {
            bufUsedZero(piece);
            ioRead(readPieces, piece);
            bufCat(piecesResult, piece);
        }
 
        ioReadClose(readPieces);
        ioBufferSizeSet(TEST_BUFFER_SIZE);
 
        TEST_RESULT_BOOL(bufEq(piecesResult, piecesPlainText), true, "content decrypted in pieces");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("the header is read no matter how the input is split");
 
        Buffer *const splitResult = bufNew(0);
        IoWrite *const splitWrite = ioBufferWriteNew(splitResult);
 
        cipherBlockFormatFilterGroupReadAdd(ioWriteFilterGroup(splitWrite), cipherSpec);
 
        // Write in pieces smaller than the header so it arrives in more than one part, then a piece that completes it exactly, so
        // that what follows arrives with the header already read
        ioBufferSizeSet(4);
        ioWriteOpen(splitWrite);
        ioWrite(splitWrite, BUF(bufPtrConst(headerBuffer), 4));
        ioWrite(splitWrite, BUF(bufPtrConst(headerBuffer) + 4, 4));
        ioWrite(splitWrite, BUF(bufPtrConst(headerBuffer) + 8, bufUsed(headerBuffer) - 8));
        ioWriteClose(splitWrite);
        ioBufferSizeSet(TEST_BUFFER_SIZE);
 
        TEST_RESULT_STR_Z(strNewBuf(splitResult), TEST_PLAINTEXT, "content decrypted from split input");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("a file that begins with the magic was written before there was a header");
 
        Buffer *magicBuffer = bufNew(0);
        IoWrite *magicWrite = ioBufferWriteNew(magicBuffer);
 
        cipherBlockFormatFilterGroupWriteAddP(ioWriteFilterGroup(magicWrite), cipherSpec, REPOSITORY_FORMAT_5);
        ioWriteOpen(magicWrite);
        ioWrite(magicWrite, testPlainText);
        ioWriteClose(magicWrite);
 
        TEST_RESULT_BOOL(
            memcmp(bufPtrConst(magicBuffer), CIPHER_BLOCK_MAGIC, CIPHER_BLOCK_MAGIC_SIZE) == 0, true,
            "a format before the header writes the magic");
 
        headerResult = bufNew(0);
        headerRead = ioBufferWriteNew(headerResult);
        headerFilterGroup = ioWriteFilterGroup(headerRead);
 
        cipherBlockFormatFilterGroupReadAdd(headerFilterGroup, cipherSpec);
        ioWriteOpen(headerRead);
        ioWrite(headerRead, magicBuffer);
        ioWriteClose(headerRead);
 
        TEST_RESULT_STR_Z(strNewBuf(headerResult), TEST_PLAINTEXT, "content decrypted");
        TEST_RESULT_UINT(
            cipherBlockFormatResult(ioFilterGroupResultP(headerFilterGroup, CIPHER_BLOCK_FORMAT_FILTER_TYPE)),
            REPOSITORY_FORMAT_5, "filter reports the format before the header");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("a format given on decrypt must be the one the header contains");
 
        IoWrite *const headerMismatch = ioBufferWriteNew(bufNew(0));
 
        ioFilterGroupAdd(
            ioWriteFilterGroup(headerMismatch), cipherBlockFormatNewP(keyMapDefault, .format = REPOSITORY_FORMAT_5));
        ioWriteOpen(headerMismatch);
 
        TEST_ERROR(ioWrite(headerMismatch, headerBuffer), FormatError, "expected repository format 5 but found 6");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("a format given on decrypt that the header agrees with is accepted, here from a pack");
 
        headerResult = bufNew(0);
        headerRead = ioBufferWriteNew(headerResult);
 
        ioFilterGroupAdd(
            ioWriteFilterGroup(headerRead),
            cipherBlockFormatNewPack(
                ioFilterParamList(cipherBlockFormatNewP(keyMapDefault, .format = REPOSITORY_FORMAT_6))));
        ioWriteOpen(headerRead);
        ioWrite(headerRead, headerBuffer);
        ioWriteClose(headerRead);
 
        TEST_RESULT_STR_Z(strNewBuf(headerResult), TEST_PLAINTEXT, "content decrypted");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("a file too short to hold a header cannot have one");
 
        IoWrite *const headerShort = ioBufferWriteNew(bufNew(0));
 
        cipherBlockFormatFilterGroupReadAdd(ioWriteFilterGroup(headerShort), cipherSpec);
        ioWriteOpen(headerShort);
        ioWrite(headerShort, BUFSTRDEF("PGBR"));
 
        TEST_ERROR(ioWriteClose(headerShort), CryptoError, "cipher header missing");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("damaged headers");
 
        // Decrypt the buffer above after damaging a byte of the header, which is the same buffer for each case
        #define TEST_HEADER_DAMAGE(damageIdx, damageChar, errorType, errorMessage)                                                 \
            do                                                                                                                     \
            {                                                                                                                      \
                Buffer *const damaged = bufDup(headerBuffer);                                                                      \
                bufPtr(damaged)[damageIdx] = damageChar;                                                                           \
                                                                                                                                   \
                IoWrite *const write = ioBufferWriteNew(bufNew(0));                                                                \
                cipherBlockFormatFilterGroupReadAdd(ioWriteFilterGroup(write), cipherSpec);                                    \
                ioWriteOpen(write);                                                                                                \
                                                                                                                                   \
                TEST_ERROR(ioWrite(write, damaged), errorType, errorMessage);                                                      \
            }                                                                                                                      \
            while (0)
 
        // Where the format should be, so this version cannot tell what it is looking at
        TEST_HEADER_DAMAGE(CIPHER_BLOCK_FORMAT_MAGIC_SIZE, 'X', FormatError, "invalid cipher header");
 
        // The byte held back for later, which must be the one this version writes
        TEST_HEADER_DAMAGE(CIPHER_BLOCK_FORMAT_HEADER_SIZE - 1, 'X', FormatError, "invalid cipher header");
 
        // A format newer than this version can read, reported before anything is decrypted
        TEST_HEADER_DAMAGE(
            CIPHER_BLOCK_FORMAT_HEADER_SIZE - 2, '7', FormatError,
            "repository format 7 requires a newer version of " PROJECT_NAME "\n"
            "HINT: " PROJECT_NAME " " PROJECT_VERSION " supports repository format 5 to 6.");
 
        // A format older than this version can read
        TEST_HEADER_DAMAGE(
            CIPHER_BLOCK_FORMAT_HEADER_SIZE - 2, '4', FormatError,
            "repository format 4 is no longer supported by " PROJECT_NAME "\n"
            "HINT: " PROJECT_NAME " " PROJECT_VERSION " supports repository format 5 to 6.");
 
        // Neither a header nor the magic, which is what a file that was never encrypted looks like from here
        TEST_HEADER_DAMAGE(0, 'X', CryptoError, "cipher header invalid");
 
        #undef TEST_HEADER_DAMAGE
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("read with the key id stored in the header");
 
        const CipherSpec *const keySpecOld = cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF("oldkey"), .digest = hashTypeSha1);
        const CipherSpec *const keySpecNew = cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF("newkey"));
 
        CipherSpecMap *const keyMap = cipherSpecMapNew();
        cipherSpecMapAdd(keyMap, STRDEF(CIPHER_SPEC_MAP_ID_DEFAULT), keySpecOld);
        cipherSpecMapAdd(keyMap, STRDEF("7"), keySpecNew);
 
        Buffer *const keyBuffer = bufNew(0);
        IoRead *const keyWrite = ioBufferReadNew(testPlainText);
        Buffer *const keyPiece = bufNew(4);
 
        // Build the write filters from their param lists, as the remote protocol does
        IoFilterGroup *const keyWriteGroup = ioFilterGroupNew();
        cipherBlockFormatFilterGroupWriteAddP(keyWriteGroup, keySpecNew, REPOSITORY_FORMAT_6, .keyId = STRDEF("7"));
 
        PackRead *const keyWriteParam = pckReadNew(ioFilterGroupParamAll(keyWriteGroup));
 
        TEST_RESULT_UINT(pckReadStrIdP(keyWriteParam), CIPHER_BLOCK_FILTER_TYPE, "block cipher filter");
        ioFilterGroupAdd(ioReadFilterGroup(keyWrite), cipherBlockNewPack(pckReadPackP(keyWriteParam)));
        TEST_RESULT_UINT(pckReadStrIdP(keyWriteParam), CIPHER_BLOCK_FORMAT_HEADER_FILTER_TYPE, "header filter");
        ioFilterGroupAdd(ioReadFilterGroup(keyWrite), cipherBlockFormatHeaderNewPack(pckReadPackP(keyWriteParam)));
 
        // Take the result in pieces smaller than the header so the header is written in more than one part
        ioBufferSizeSet(4);
        ioReadOpen(keyWrite);
 
        while (!ioReadEof(keyWrite))
        {
            bufUsedZero(keyPiece);
            ioRead(keyWrite, keyPiece);
            bufCat(keyBuffer, keyPiece);
        }
 
        ioReadClose(keyWrite);
        ioBufferSizeSet(TEST_BUFFER_SIZE);
 
        TEST_RESULT_BOOL(
            memcmp(bufPtrConst(keyBuffer), CIPHER_BLOCK_FORMAT_MAGIC "006K\0017", CIPHER_BLOCK_FORMAT_HEADER_SIZE + 2) == 0, true,
            "header contains the key after its length");
 
        Buffer *keyResult = bufNew(0);
        IoWrite *keyRead = ioBufferWriteNew(keyResult);
 
        cipherBlockFormatFilterGroupReadAddMap(ioWriteFilterGroup(keyRead), keyMap);
        ioWriteOpen(keyRead);
        ioWrite(keyRead, keyBuffer);
        ioWriteClose(keyRead);
 
        TEST_RESULT_STR_Z(strNewBuf(keyResult), TEST_PLAINTEXT, "content decrypted with the key the header contains");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("key id split across input, from a pack");
 
        keyResult = bufNew(0);
        keyRead = ioBufferWriteNew(keyResult);
 
        // Build from a pack, as the remote protocol does
        ioFilterGroupAdd(
            ioWriteFilterGroup(keyRead), cipherBlockFormatNewPack(ioFilterParamList(cipherBlockFormatNewP(keyMap))));
 
        // Split so the length byte and the key id each arrive separately
        ioBufferSizeSet(4);
        ioWriteOpen(keyRead);
        ioWrite(keyRead, BUF(bufPtrConst(keyBuffer), CIPHER_BLOCK_FORMAT_HEADER_SIZE));
        ioWrite(keyRead, BUF(bufPtrConst(keyBuffer) + CIPHER_BLOCK_FORMAT_HEADER_SIZE, 1));
        ioWrite(keyRead, BUF(bufPtrConst(keyBuffer) + CIPHER_BLOCK_FORMAT_HEADER_SIZE + 1, 1));
        ioWrite(
            keyRead,
            BUF(
                bufPtrConst(keyBuffer) + CIPHER_BLOCK_FORMAT_HEADER_SIZE + 2,
                bufUsed(keyBuffer) - CIPHER_BLOCK_FORMAT_HEADER_SIZE - 2));
        ioWriteClose(keyRead);
        ioBufferSizeSet(TEST_BUFFER_SIZE);
 
        TEST_RESULT_STR_Z(strNewBuf(keyResult), TEST_PLAINTEXT, "content decrypted from split input");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("key digest is used, not the digest of the format");
 
        // A key kept by migration derives with SHA-1 even in a file written at a format that derives with SHA-256
        Buffer *const digestBuffer = bufNew(0);
        IoWrite *const digestWrite = ioBufferWriteNew(digestBuffer);
 
        cipherBlockFormatFilterGroupWriteAddP(ioWriteFilterGroup(digestWrite), keySpecOld, REPOSITORY_FORMAT_6);
        ioWriteOpen(digestWrite);
        ioWrite(digestWrite, testPlainText);
        ioWriteClose(digestWrite);
 
        Buffer *const digestResult = bufNew(0);
        IoWrite *const digestRead = ioBufferWriteNew(digestResult);
 
        cipherBlockFormatFilterGroupReadAddMap(ioWriteFilterGroup(digestRead), keyMap);
        ioWriteOpen(digestRead);
        ioWrite(digestRead, digestBuffer);
        ioWriteClose(digestRead);
 
        TEST_RESULT_STR_Z(strNewBuf(digestResult), TEST_PLAINTEXT, "content decrypted with the digest of the key");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("file with no key id uses the migrated key");
 
        Buffer *const migratedBuffer = bufNew(0);
        IoWrite *const migratedWrite = ioBufferWriteNew(migratedBuffer);
 
        cipherBlockFormatFilterGroupWriteAddP(ioWriteFilterGroup(migratedWrite), keySpecOld, REPOSITORY_FORMAT_5);
        ioWriteOpen(migratedWrite);
        ioWrite(migratedWrite, testPlainText);
        ioWriteClose(migratedWrite);
 
        Buffer *const migratedResult = bufNew(0);
        IoWrite *const migratedRead = ioBufferWriteNew(migratedResult);
 
        cipherBlockFormatFilterGroupReadAddMap(ioWriteFilterGroup(migratedRead), keyMap);
        ioWriteOpen(migratedRead);
        ioWrite(migratedRead, migratedBuffer);
        ioWriteClose(migratedRead);
 
        TEST_RESULT_STR_Z(strNewBuf(migratedResult), TEST_PLAINTEXT, "content decrypted with the migrated key");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("key id not in the map");
 
        CipherSpecMap *const keyMapShort = cipherSpecMapNew();
        cipherSpecMapAdd(keyMapShort, STRDEF(CIPHER_SPEC_MAP_ID_DEFAULT), keySpecOld);
 
        IoWrite *const keyMissing = ioBufferWriteNew(bufNew(0));
        cipherBlockFormatFilterGroupReadAddMap(ioWriteFilterGroup(keyMissing), keyMapShort);
        ioWriteOpen(keyMissing);
 
        TEST_ERROR(ioWrite(keyMissing, keyBuffer), CryptoError, "unable to find cipher key '7'");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("key id in a file read with a single key");
 
        IoWrite *const keyUnexpected = ioBufferWriteNew(bufNew(0));
        cipherBlockFormatFilterGroupReadAdd(ioWriteFilterGroup(keyUnexpected), keySpecNew);
        ioWriteOpen(keyUnexpected);
 
        TEST_ERROR(ioWrite(keyUnexpected, keyBuffer), CryptoError, "unable to find cipher key '7'");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("key id with zero length");
 
        Buffer *const keyEmpty = bufDup(keyBuffer);
        bufPtr(keyEmpty)[CIPHER_BLOCK_FORMAT_HEADER_SIZE] = 0;
 
        IoWrite *const keyEmptyRead = ioBufferWriteNew(bufNew(0));
        cipherBlockFormatFilterGroupReadAddMap(ioWriteFilterGroup(keyEmptyRead), keyMap);
        ioWriteOpen(keyEmptyRead);
 
        TEST_ERROR(ioWrite(keyEmptyRead, keyEmpty), FormatError, "invalid cipher header");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("no filter added when the map is empty");
 
        IoWrite *const keyNone = ioBufferWriteNew(bufNew(0));
        TEST_RESULT_PTR_NE(
            cipherBlockFormatFilterGroupReadAddMap(ioWriteFilterGroup(keyNone), cipherSpecMapNew()), NULL,
            "add read filter for no keys");
        TEST_RESULT_UINT(ioFilterGroupSize(ioWriteFilterGroup(keyNone)), 0, "no filter added");
    }
 
    // *****************************************************************************************************************************
    if (testBegin("cipherSpecMapNew()"))
    {
        const CipherSpec *const cipherSpec1 = cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF("key1"), .digest = hashTypeSha256);
        const CipherSpec *const cipherSpec2 = cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF("key2"), .digest = hashTypeSha1);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("add and get keys");
 
        CipherSpecMap *map = NULL;
        TEST_ASSIGN(map, cipherSpecMapNew(), "new map");
        TEST_RESULT_BOOL(cipherSpecMapEmpty(map), true, "no keys");
 
        // Add out of order to check that the map sorts
        TEST_RESULT_VOID(cipherSpecMapAdd(map, STRDEF("2"), cipherSpec2), "add 2");
        TEST_RESULT_VOID(cipherSpecMapAdd(map, STRDEF("0"), cipherSpec1), "add 0");
 
        TEST_RESULT_UINT(cipherSpecMapSize(map), 2, "two keys");
        TEST_RESULT_STR_Z(cipherSpecMapGetIdx(map, 0)->id, "0", "first id");
        TEST_RESULT_STR_Z(cipherSpecMapGetIdx(map, 1)->id, "2", "second id");
 
        // Each key keeps its own digest
        TEST_RESULT_UINT(cipherSpecDigest(cipherSpecMapGet(map, STRDEF("0"))), hashTypeSha256, "key 0 digest");
        TEST_RESULT_UINT(cipherSpecDigest(cipherSpecMapGet(map, STRDEF("2"))), hashTypeSha1, "key 2 digest");
        TEST_RESULT_STR_Z(strNewBuf(cipherSpecPass(cipherSpecMapGet(map, STRDEF("2")))), "key2", "key 2 pass");
 
        TEST_RESULT_STR_Z(cipherSpecMapIdCurrent(map), "2", "adding the key with no id did not make it current");
 
        // A map with only the key that has no id has no current key
        CipherSpecMap *const mapDefault = cipherSpecMapNew();
        TEST_RESULT_VOID(cipherSpecMapAdd(mapDefault, CIPHER_SPEC_MAP_ID_DEFAULT_STR, cipherSpec1), "add key with no id");
        TEST_RESULT_PTR(cipherSpecMapIdCurrent(mapDefault), NULL, "no current key");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("duplicate keeps the current key");
 
        TEST_RESULT_PTR(cipherSpecMapIdCurrent(cipherSpecMapDup(cipherSpecMapNew())), NULL, "dup empty map");
 
        CipherSpecMap *mapDup = NULL;
        TEST_ASSIGN(mapDup, cipherSpecMapDup(map), "dup map");
        TEST_RESULT_UINT(cipherSpecMapSize(mapDup), 2, "two keys");
        TEST_RESULT_STR_Z(cipherSpecMapIdCurrent(mapDup), "2", "current key id");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("key id not found");
 
        TEST_ERROR(cipherSpecMapGet(map, STRDEF("1")), CryptoError, "unable to find cipher key '1'");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("pack round trip");
 
        PackWrite *const packWrite = pckWriteNewP();
        TEST_RESULT_VOID(cipherSpecMapPack(packWrite, map), "pack map");
        pckWriteEndP(packWrite);
 
        CipherSpecMap *mapPack = NULL;
        TEST_ASSIGN(mapPack, cipherSpecMapNewPack(pckReadNew(pckWriteResult(packWrite))), "unpack map");
 
        TEST_RESULT_UINT(cipherSpecMapSize(mapPack), 2, "two keys");
        TEST_RESULT_STR_Z(strNewBuf(cipherSpecPass(cipherSpecMapGet(mapPack, STRDEF("0")))), "key1", "key 0 pass");
        TEST_RESULT_UINT(cipherSpecDigest(cipherSpecMapGet(mapPack, STRDEF("2"))), hashTypeSha1, "key 2 digest");
        TEST_RESULT_STR_Z(cipherSpecMapIdCurrent(mapPack), "2", "current key id");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("log ids but not keys");
 
        char logBuf[STACK_TRACE_PARAM_MAX];
 
        TEST_RESULT_VOID(FUNCTION_LOG_OBJECT_FORMAT(map, cipherSpecMapToLog, logBuf, sizeof(logBuf)), "cipherSpecMapToLog");
        TEST_RESULT_Z(logBuf, "{ids: [0, 2]}", "check log");
 
        TEST_RESULT_VOID(cipherSpecMapFree(map), "free map");
    }
 
    FUNCTION_HARNESS_RETURN_VOID();
}