cachekit-core 0.2.0

LZ4 compression, xxHash3 integrity, AES-256-GCM encryption for byte payloads
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/* C99 test harness for cachekit-core FFI
 * Tests compression, encryption, and error handling
 */

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include "../../include/cachekit.h"

/* ANSI color codes for output */
#define GREEN "\033[0;32m"
#define RED "\033[0;31m"
#define RESET "\033[0m"

/* Test result tracking */
static int total_tests = 0;
static int passed_tests = 0;

void report_test(const char* name, bool success) {
    total_tests++;
    if (success) {
        passed_tests++;
        printf(GREEN "PASS" RESET ": %s\n", name);
    } else {
        printf(RED "FAIL" RESET ": %s\n", name);
    }
}

/* ========== COMPRESSION TESTS ========== */

int test_compress_decompress_roundtrip(void) {
    const char* input = "Hello, CacheKit FFI! This is a test of LZ4 compression with xxHash3-64 checksums.";
    size_t input_len = strlen(input);

    /* Compress */
    uint8_t compressed[4096];
    size_t compressed_len = sizeof(compressed);

    enum CACHEKIT_CachekitError err = cachekit_compress(
        (const uint8_t*)input, input_len,
        compressed, &compressed_len
    );

    if (err != OK) {
        printf("  compress returned error %d\n", err);
        return 1;
    }

    if (compressed_len == 0 || compressed_len >= input_len + 100) {
        printf("  suspicious compressed size: %zu\n", compressed_len);
        return 1;
    }

    /* Decompress */
    uint8_t decompressed[4096];
    size_t decompressed_len = sizeof(decompressed);

    err = cachekit_decompress(
        compressed, compressed_len,
        decompressed, &decompressed_len
    );

    if (err != OK) {
        printf("  decompress returned error %d\n", err);
        return 1;
    }

    /* Verify */
    if (decompressed_len != input_len) {
        printf("  length mismatch: expected %zu, got %zu\n", input_len, decompressed_len);
        return 1;
    }

    if (memcmp(input, decompressed, input_len) != 0) {
        printf("  data mismatch after roundtrip\n");
        return 1;
    }

    return 0;
}

int test_compress_null_pointer_input(void) {
    uint8_t output[1024];
    size_t output_len = sizeof(output);

    enum CACHEKIT_CachekitError err = cachekit_compress(
        NULL, 100,
        output, &output_len
    );

    if (err != NULL_POINTER) {
        printf("  expected NULL_POINTER, got %d\n", err);
        return 1;
    }

    return 0;
}

int test_compress_null_pointer_output(void) {
    const char* input = "test data";
    size_t output_len = 1024;

    enum CACHEKIT_CachekitError err = cachekit_compress(
        (const uint8_t*)input, strlen(input),
        NULL, &output_len
    );

    if (err != NULL_POINTER) {
        printf("  expected NULL_POINTER, got %d\n", err);
        return 1;
    }

    return 0;
}

int test_compress_null_pointer_output_len(void) {
    const char* input = "test data";
    uint8_t output[1024];

    enum CACHEKIT_CachekitError err = cachekit_compress(
        (const uint8_t*)input, strlen(input),
        output, NULL
    );

    if (err != NULL_POINTER) {
        printf("  expected NULL_POINTER, got %d\n", err);
        return 1;
    }

    return 0;
}

int test_compress_buffer_too_small(void) {
    const char* input = "This is a longer string that should require more space when compressed";
    size_t input_len = strlen(input);

    uint8_t output[10]; /* Intentionally too small */
    size_t output_len = sizeof(output);
    size_t original_output_len = output_len;

    enum CACHEKIT_CachekitError err = cachekit_compress(
        (const uint8_t*)input, input_len,
        output, &output_len
    );

    if (err != BUFFER_TOO_SMALL) {
        printf("  expected BUFFER_TOO_SMALL, got %d\n", err);
        return 1;
    }

    /* Verify output_len was updated with required size */
    if (output_len <= original_output_len) {
        printf("  output_len not updated: %zu (should be > %zu)\n", output_len, original_output_len);
        return 1;
    }

    return 0;
}

int test_decompress_corrupted_data(void) {
    /* Random garbage that's not valid compressed data */
    uint8_t corrupted[100];
    for (size_t i = 0; i < sizeof(corrupted); i++) {
        corrupted[i] = (uint8_t)(i * 7 + 13);
    }

    uint8_t output[1024];
    size_t output_len = sizeof(output);

    enum CACHEKIT_CachekitError err = cachekit_decompress(
        corrupted, sizeof(corrupted),
        output, &output_len
    );

    /* Should fail with either INVALID_INPUT, CHECKSUM_MISMATCH, or DECOMPRESSION_FAILED */
    if (err == OK) {
        printf("  corrupted data should not decompress successfully\n");
        return 1;
    }

    return 0;
}

int test_decompress_tampered_checksum(void) {
    const char* input = "Test data for checksum tampering";
    size_t input_len = strlen(input);

    /* Compress valid data */
    uint8_t compressed[1024];
    size_t compressed_len = sizeof(compressed);

    enum CACHEKIT_CachekitError err = cachekit_compress(
        (const uint8_t*)input, input_len,
        compressed, &compressed_len
    );

    if (err != OK) {
        printf("  compress failed: %d\n", err);
        return 1;
    }

    /* Tamper with the compressed data (flip some bits) */
    if (compressed_len > 10) {
        compressed[compressed_len - 5] ^= 0xFF;
    }

    /* Try to decompress tampered data */
    uint8_t output[1024];
    size_t output_len = sizeof(output);

    err = cachekit_decompress(
        compressed, compressed_len,
        output, &output_len
    );

    /* Should fail with CHECKSUM_MISMATCH or DECOMPRESSION_FAILED */
    if (err == OK) {
        printf("  tampered data should not decompress successfully\n");
        return 1;
    }

    return 0;
}

int test_compressed_bound(void) {
    size_t input_len = 1000;
    size_t bound = cachekit_compressed_bound(input_len);

    /* Bound should be larger than input (due to overhead) */
    if (bound <= input_len) {
        printf("  compressed_bound too small: %zu for input %zu\n", bound, input_len);
        return 1;
    }

    /* Bound should be reasonable (not absurdly large) */
    if (bound > input_len * 2 + 1000) {
        printf("  compressed_bound too large: %zu for input %zu\n", bound, input_len);
        return 1;
    }

    return 0;
}

/* ========== ENCRYPTION TESTS ========== */

int test_encrypt_decrypt_roundtrip(void) {
    /* 256-bit key (32 bytes) */
    uint8_t key[32] = {
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
        0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
        0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
        0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20
    };

    const char* plaintext = "Secret message for zero-knowledge encryption test";
    size_t plaintext_len = strlen(plaintext);

    const char* aad = "tenant-id-12345";
    size_t aad_len = strlen(aad);

    /* Create encryptor */
    enum CACHEKIT_CachekitError init_err;
    struct CACHEKIT_CachekitEncryptor* enc = cachekit_encryptor_new(&init_err);
    if (enc == NULL) {
        printf("  cachekit_encryptor_new returned null (error: %d)\n", init_err);
        return 1;
    }

    /* Encrypt */
    uint8_t ciphertext[1024];
    size_t ciphertext_len = sizeof(ciphertext);

    enum CACHEKIT_CachekitError err = cachekit_encrypt(
        enc,
        key, sizeof(key),
        (const uint8_t*)aad, aad_len,
        (const uint8_t*)plaintext, plaintext_len,
        ciphertext, &ciphertext_len
    );

    if (err != OK) {
        printf("  encrypt failed: %d\n", err);
        cachekit_encryptor_free(enc);
        return 1;
    }

    /* Verify ciphertext is longer than plaintext (includes nonce + tag) */
    if (ciphertext_len != plaintext_len + 28) {
        printf("  unexpected ciphertext length: %zu (expected %zu)\n",
               ciphertext_len, plaintext_len + 28);
        cachekit_encryptor_free(enc);
        return 1;
    }

    /* Decrypt */
    uint8_t decrypted[1024];
    size_t decrypted_len = sizeof(decrypted);

    err = cachekit_decrypt(
        enc,
        key, sizeof(key),
        (const uint8_t*)aad, aad_len,
        ciphertext, ciphertext_len,
        decrypted, &decrypted_len
    );

    if (err != OK) {
        printf("  decrypt failed: %d\n", err);
        cachekit_encryptor_free(enc);
        return 1;
    }

    /* Verify */
    if (decrypted_len != plaintext_len) {
        printf("  length mismatch: expected %zu, got %zu\n", plaintext_len, decrypted_len);
        cachekit_encryptor_free(enc);
        return 1;
    }

    if (memcmp(plaintext, decrypted, plaintext_len) != 0) {
        printf("  data mismatch after roundtrip\n");
        cachekit_encryptor_free(enc);
        return 1;
    }

    cachekit_encryptor_free(enc);
    return 0;
}

int test_encrypt_wrong_aad_fails(void) {
    uint8_t key[32] = {0};
    const char* plaintext = "test";
    const char* aad_encrypt = "correct-aad";
    const char* aad_decrypt = "wrong-aad";

    enum CACHEKIT_CachekitError init_err;
    struct CACHEKIT_CachekitEncryptor* enc = cachekit_encryptor_new(&init_err);
    if (enc == NULL) {
        return 1;
    }

    /* Encrypt with one AAD */
    uint8_t ciphertext[1024];
    size_t ciphertext_len = sizeof(ciphertext);

    enum CACHEKIT_CachekitError err = cachekit_encrypt(
        enc,
        key, sizeof(key),
        (const uint8_t*)aad_encrypt, strlen(aad_encrypt),
        (const uint8_t*)plaintext, strlen(plaintext),
        ciphertext, &ciphertext_len
    );

    if (err != OK) {
        cachekit_encryptor_free(enc);
        return 1;
    }

    /* Try to decrypt with different AAD */
    uint8_t decrypted[1024];
    size_t decrypted_len = sizeof(decrypted);

    err = cachekit_decrypt(
        enc,
        key, sizeof(key),
        (const uint8_t*)aad_decrypt, strlen(aad_decrypt),
        ciphertext, ciphertext_len,
        decrypted, &decrypted_len
    );

    if (err != DECRYPTION_FAILED) {
        printf("  expected DECRYPTION_FAILED, got %d\n", err);
        cachekit_encryptor_free(enc);
        return 1;
    }

    cachekit_encryptor_free(enc);
    return 0;
}

int test_encrypt_invalid_key_length(void) {
    uint8_t short_key[16] = {0}; /* Only 128 bits, need 256 */
    const char* plaintext = "test";
    const char* aad = "tenant";

    enum CACHEKIT_CachekitError init_err;
    struct CACHEKIT_CachekitEncryptor* enc = cachekit_encryptor_new(&init_err);
    if (enc == NULL) {
        return 1;
    }

    uint8_t ciphertext[1024];
    size_t ciphertext_len = sizeof(ciphertext);

    enum CACHEKIT_CachekitError err = cachekit_encrypt(
        enc,
        short_key, sizeof(short_key),
        (const uint8_t*)aad, strlen(aad),
        (const uint8_t*)plaintext, strlen(plaintext),
        ciphertext, &ciphertext_len
    );

    if (err != INVALID_KEY_LENGTH) {
        printf("  expected INVALID_KEY_LENGTH, got %d\n", err);
        cachekit_encryptor_free(enc);
        return 1;
    }

    cachekit_encryptor_free(enc);
    return 0;
}

int test_encryptor_counter(void) {
    enum CACHEKIT_CachekitError init_err;
    struct CACHEKIT_CachekitEncryptor* enc = cachekit_encryptor_new(&init_err);
    if (enc == NULL) {
        return 1;
    }

    uint64_t initial_counter = cachekit_encryptor_get_counter(enc);

    /* Counter should start at 0 */
    if (initial_counter != 0) {
        printf("  expected initial counter 0, got %llu\n",
               (unsigned long long)initial_counter);
        cachekit_encryptor_free(enc);
        return 1;
    }

    /* Perform encryption */
    uint8_t key[32] = {0};
    const char* plaintext = "test";
    uint8_t ciphertext[1024];
    size_t ciphertext_len = sizeof(ciphertext);

    enum CACHEKIT_CachekitError err = cachekit_encrypt(
        enc,
        key, sizeof(key),
        (const uint8_t*)"", 0,
        (const uint8_t*)plaintext, strlen(plaintext),
        ciphertext, &ciphertext_len
    );

    if (err != OK) {
        cachekit_encryptor_free(enc);
        return 1;
    }

    /* Counter should increment */
    uint64_t after_counter = cachekit_encryptor_get_counter(enc);
    if (after_counter != 1) {
        printf("  expected counter 1 after encryption, got %llu\n",
               (unsigned long long)after_counter);
        cachekit_encryptor_free(enc);
        return 1;
    }

    cachekit_encryptor_free(enc);
    return 0;
}

int test_derive_key_hkdf(void) {
    const uint8_t master[32] = {
        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
        0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
    };

    const char* salt = "tenant-12345";
    const char* domain = "cache-encryption";

    uint8_t derived[32];

    enum CACHEKIT_CachekitError err = cachekit_derive_key(
        master, sizeof(master),
        (const uint8_t*)salt, strlen(salt),
        (const uint8_t*)domain, strlen(domain),
        derived
    );

    if (err != OK) {
        printf("  derive_key failed: %d\n", err);
        return 1;
    }

    /* Derived key should be different from master */
    if (memcmp(master, derived, 32) == 0) {
        printf("  derived key identical to master key\n");
        return 1;
    }

    /* Derive again with same inputs - should be deterministic */
    uint8_t derived2[32];
    err = cachekit_derive_key(
        master, sizeof(master),
        (const uint8_t*)salt, strlen(salt),
        (const uint8_t*)domain, strlen(domain),
        derived2
    );

    if (err != OK || memcmp(derived, derived2, 32) != 0) {
        printf("  key derivation not deterministic\n");
        return 1;
    }

    /* Different salt should produce different key */
    uint8_t derived3[32];
    const char* salt2 = "tenant-67890";
    err = cachekit_derive_key(
        master, sizeof(master),
        (const uint8_t*)salt2, strlen(salt2),
        (const uint8_t*)domain, strlen(domain),
        derived3
    );

    if (err != OK) {
        return 1;
    }

    if (memcmp(derived, derived3, 32) == 0) {
        printf("  different salts produced same derived key\n");
        return 1;
    }

    return 0;
}

int test_derive_key_invalid_master_length(void) {
    const uint8_t short_master[15] = {0}; /* Less than minimum 16 bytes */
    const char* salt = "tenant";
    const char* domain = "cache";
    uint8_t derived[32];

    enum CACHEKIT_CachekitError err = cachekit_derive_key(
        short_master, sizeof(short_master),
        (const uint8_t*)salt, strlen(salt),
        (const uint8_t*)domain, strlen(domain),
        derived
    );

    if (err != INVALID_KEY_LENGTH) {
        printf("  expected INVALID_KEY_LENGTH, got %d\n", err);
        return 1;
    }

    return 0;
}

/* ========== MAIN TEST RUNNER ========== */

int main(void) {
    printf("=== CacheKit C FFI Test Suite ===\n\n");

    printf("--- Compression Tests ---\n");
    report_test("compress_decompress_roundtrip", test_compress_decompress_roundtrip() == 0);
    report_test("compress_null_pointer_input", test_compress_null_pointer_input() == 0);
    report_test("compress_null_pointer_output", test_compress_null_pointer_output() == 0);
    report_test("compress_null_pointer_output_len", test_compress_null_pointer_output_len() == 0);
    report_test("compress_buffer_too_small", test_compress_buffer_too_small() == 0);
    report_test("decompress_corrupted_data", test_decompress_corrupted_data() == 0);
    report_test("decompress_tampered_checksum", test_decompress_tampered_checksum() == 0);
    report_test("compressed_bound", test_compressed_bound() == 0);

    printf("\n--- Encryption Tests ---\n");
    report_test("encrypt_decrypt_roundtrip", test_encrypt_decrypt_roundtrip() == 0);
    report_test("encrypt_wrong_aad_fails", test_encrypt_wrong_aad_fails() == 0);
    report_test("encrypt_invalid_key_length", test_encrypt_invalid_key_length() == 0);
    report_test("encryptor_counter", test_encryptor_counter() == 0);
    report_test("derive_key_hkdf", test_derive_key_hkdf() == 0);
    report_test("derive_key_invalid_master_length", test_derive_key_invalid_master_length() == 0);

    printf("\n=== Summary ===\n");
    printf("Tests run: %d\n", total_tests);
    printf("Passed: " GREEN "%d" RESET "\n", passed_tests);
    printf("Failed: " RED "%d" RESET "\n", total_tests - passed_tests);

    return (passed_tests == total_tests) ? 0 : 1;
}