-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathtest_get_preimage.c
More file actions
710 lines (558 loc) · 22.1 KB
/
test_get_preimage.c
File metadata and controls
710 lines (558 loc) · 22.1 KB
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
/**
* Unit tests for call_get_preimage using the mock dispatcher.
*
* Tests verify that the C implementation of call_get_preimage correctly
* handles the client command protocol (GET_PREIMAGE / GET_MORE_ELEMENTS)
* and validates the SHA-256 hash of the received data.
*/
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <cmocka.h>
#include "mock_dispatcher.h"
#include "client_commands.h"
#include "handler/lib/get_preimage.h"
/* ---------- Helpers ---------- */
static void compute_sha256(const uint8_t *data, size_t len, uint8_t out[32]) {
cx_hash_sha256(data, len, out, 32);
}
/* ---------- Test cases ---------- */
/**
* Happy path: small preimage (fits entirely in the first response, no
* GET_MORE_ELEMENTS needed).
*/
static void test_get_preimage_small(void **state) {
mock_dispatcher_t *mock = *state;
/* A small preimage: 50 bytes */
uint8_t preimage[50];
for (size_t i = 0; i < sizeof(preimage); i++) {
preimage[i] = (uint8_t) (i & 0xFF);
}
mock_dispatcher_add_preimage(mock, preimage, sizeof(preimage));
uint8_t hash[32];
compute_sha256(preimage, sizeof(preimage), hash);
uint8_t out[256];
memset(out, 0xAA, sizeof(out));
dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, (int) sizeof(preimage));
assert_memory_equal(out, preimage, sizeof(preimage));
}
/**
* Happy path: large preimage that requires GET_MORE_ELEMENTS to transfer
* all the bytes.
*/
static void test_get_preimage_large(void **state) {
mock_dispatcher_t *mock = *state;
/* A larger preimage: 300 bytes */
uint8_t preimage[300];
for (size_t i = 0; i < sizeof(preimage); i++) {
preimage[i] = (uint8_t) ((i * 7 + 13) & 0xFF);
}
mock_dispatcher_add_preimage(mock, preimage, sizeof(preimage));
uint8_t hash[32];
compute_sha256(preimage, sizeof(preimage), hash);
uint8_t out[512];
memset(out, 0, sizeof(out));
dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, (int) sizeof(preimage));
assert_memory_equal(out, preimage, sizeof(preimage));
}
/**
* Error: requesting preimage of an unknown hash should return a negative value.
*/
static void test_get_preimage_unknown_hash(void **state) {
mock_dispatcher_t *mock = *state;
/* Don't register any preimage; just call with a random hash */
uint8_t hash[32] = {0xDE, 0xAD, 0xBE, 0xEF};
uint8_t out[256];
dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
/* process_interruption returns -1 → call_get_preimage returns -1 */
assert_true(result < 0);
}
/**
* Error: output buffer too small for the preimage.
* call_get_preimage should return -10.
*/
static void test_get_preimage_buffer_too_small(void **state) {
mock_dispatcher_t *mock = *state;
uint8_t preimage[100];
for (size_t i = 0; i < sizeof(preimage); i++) {
preimage[i] = (uint8_t) i;
}
mock_dispatcher_add_preimage(mock, preimage, sizeof(preimage));
uint8_t hash[32];
compute_sha256(preimage, sizeof(preimage), hash);
uint8_t out[50]; /* Too small! */
dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, -10);
}
/**
* Edge case: minimal preimage of exactly 1 byte.
*/
static void test_get_preimage_one_byte(void **state) {
mock_dispatcher_t *mock = *state;
uint8_t preimage[1] = {0x42};
mock_dispatcher_add_preimage(mock, preimage, 1);
uint8_t hash[32];
compute_sha256(preimage, 1, hash);
uint8_t out[64];
memset(out, 0, sizeof(out));
dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, 1);
assert_int_equal(out[0], 0x42);
}
/**
* Edge case: preimage that exactly fills the max first-response payload.
*
* For a preimage of length L, the varint encoding takes:
* 1 byte if L < 253, 3 bytes if L < 65536, etc.
* Max payload = 255 - varint_len - 1.
* For varint_len=1: max_payload = 253.
* So a 253-byte preimage should fit exactly with no GET_MORE_ELEMENTS.
*/
static void test_get_preimage_exact_fit(void **state) {
mock_dispatcher_t *mock = *state;
uint8_t preimage[253];
for (size_t i = 0; i < sizeof(preimage); i++) {
preimage[i] = (uint8_t) (i ^ 0xA5);
}
mock_dispatcher_add_preimage(mock, preimage, sizeof(preimage));
uint8_t hash[32];
compute_sha256(preimage, sizeof(preimage), hash);
uint8_t out[512];
memset(out, 0, sizeof(out));
dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, (int) sizeof(preimage));
assert_memory_equal(out, preimage, sizeof(preimage));
}
/**
* Edge case: preimage of length 254 (one byte over the exact-fit boundary,
* so a few bytes go through GET_MORE_ELEMENTS).
*/
static void test_get_preimage_one_byte_overflow(void **state) {
mock_dispatcher_t *mock = *state;
/* Varint encodings above 253 bytes (and less than 65536) take 3 bytes,
* therefore max_payload = 255 - 3 - 1 = 251.
* Hence, for length 254, 3 bytes go through GET_MORE_ELEMENTS.
*/
uint8_t preimage[254];
for (size_t i = 0; i < sizeof(preimage); i++) {
preimage[i] = (uint8_t) (i * 3);
}
mock_dispatcher_add_preimage(mock, preimage, sizeof(preimage));
uint8_t hash[32];
compute_sha256(preimage, sizeof(preimage), hash);
uint8_t out[512];
memset(out, 0, sizeof(out));
dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, (int) sizeof(preimage));
assert_memory_equal(out, preimage, sizeof(preimage));
}
/* ==========================================================================
* Adversarial tests: malicious client behavior
* ========================================================================== */
/**
* Adversarial: client returns valid structure but corrupted data bytes,
* causing a SHA-256 hash mismatch.
*/
static int tamper_corrupt_preimage_data(uint8_t *response_buf,
size_t *response_len,
uint8_t cmd,
int call_count,
void *user_data) {
(void) user_data;
(void) call_count;
if (cmd == CCMD_GET_PREIMAGE && *response_len > 3) {
/* Flip a byte in the data portion (after 1-byte varint + partial_data_len) */
response_buf[3] ^= 0xFF;
}
return 0;
}
static void test_get_preimage_corrupted_data(void **state) {
mock_dispatcher_t *mock = *state;
uint8_t preimage[50];
for (size_t i = 0; i < sizeof(preimage); i++) {
preimage[i] = (uint8_t) (i & 0xFF);
}
mock_dispatcher_add_preimage(mock, preimage, sizeof(preimage));
uint8_t hash[32];
compute_sha256(preimage, sizeof(preimage), hash);
mock_dispatcher_set_tamper_hook(mock, tamper_corrupt_preimage_data, NULL);
uint8_t out[256];
dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
/* Must detect the hash mismatch */
assert_true(result < 0);
}
/**
* Adversarial: client claims partial_data_len > preimage_len.
*/
static int tamper_partial_len_overflow(uint8_t *response_buf,
size_t *response_len,
uint8_t cmd,
int call_count,
void *user_data) {
(void) user_data;
(void) call_count;
if (cmd == CCMD_GET_PREIMAGE && *response_len >= 2) {
uint8_t preimage_len = response_buf[0];
if (preimage_len < 200) {
response_buf[1] = preimage_len + 10;
}
}
return 0;
}
static void test_get_preimage_partial_len_overflow(void **state) {
mock_dispatcher_t *mock = *state;
uint8_t preimage[20];
for (size_t i = 0; i < sizeof(preimage); i++) {
preimage[i] = (uint8_t) i;
}
mock_dispatcher_add_preimage(mock, preimage, sizeof(preimage));
uint8_t hash[32];
compute_sha256(preimage, sizeof(preimage), hash);
mock_dispatcher_set_tamper_hook(mock, tamper_partial_len_overflow, NULL);
uint8_t out[256];
dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
/* Detected via buffer_can_read or partial_data_len > preimage_len */
assert_true(result < 0);
}
/**
* Adversarial: client returns preimage_len = 0 (invalid: at minimum the prefix
* byte should be present).
*/
static int tamper_zero_preimage_len(uint8_t *response_buf,
size_t *response_len,
uint8_t cmd,
int call_count,
void *user_data) {
(void) user_data;
(void) call_count;
if (cmd == CCMD_GET_PREIMAGE) {
response_buf[0] = 0x00;
response_buf[1] = 0x00;
*response_len = 2;
}
return 0;
}
static void test_get_preimage_zero_len(void **state) {
mock_dispatcher_t *mock = *state;
uint8_t preimage[10] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A};
mock_dispatcher_add_preimage(mock, preimage, sizeof(preimage));
uint8_t hash[32];
compute_sha256(preimage, sizeof(preimage), hash);
mock_dispatcher_set_tamper_hook(mock, tamper_zero_preimage_len, NULL);
uint8_t out[256];
dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
assert_true(result < 0);
}
/**
* Adversarial: during GET_MORE_ELEMENTS, client returns elements_len != 1.
*/
static int tamper_more_elements_bad_size(uint8_t *response_buf,
size_t *response_len,
uint8_t cmd,
int call_count,
void *user_data) {
(void) user_data;
(void) call_count;
if (cmd == CCMD_GET_MORE_ELEMENTS && *response_len >= 2) {
response_buf[1] = 4; /* el_len = 4 instead of 1 */
}
return 0;
}
static void test_get_preimage_bad_element_size(void **state) {
mock_dispatcher_t *mock = *state;
uint8_t preimage[300];
for (size_t i = 0; i < sizeof(preimage); i++) {
preimage[i] = (uint8_t) (i & 0xFF);
}
mock_dispatcher_add_preimage(mock, preimage, sizeof(preimage));
uint8_t hash[32];
compute_sha256(preimage, sizeof(preimage), hash);
mock_dispatcher_set_tamper_hook(mock, tamper_more_elements_bad_size, NULL);
uint8_t out[512];
dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
/* Detected via buffer_can_read or elements_len != 1 */
assert_true(result < 0);
}
/**
* Adversarial: during GET_MORE_ELEMENTS, client sends more bytes than remaining.
*/
static int tamper_more_bytes_than_remaining(uint8_t *response_buf,
size_t *response_len,
uint8_t cmd,
int call_count,
void *user_data) {
(void) user_data;
(void) call_count;
if (cmd == CCMD_GET_MORE_ELEMENTS && *response_len >= 2) {
response_buf[0] = 250; /* claim 250 elements */
}
return 0;
}
static void test_get_preimage_more_bytes_than_remaining(void **state) {
mock_dispatcher_t *mock = *state;
/* 254 bytes: varint=3 bytes, max_payload = 255-3-1 = 251, spill = 3 bytes */
uint8_t preimage[254];
for (size_t i = 0; i < sizeof(preimage); i++) {
preimage[i] = (uint8_t) (i * 7);
}
mock_dispatcher_add_preimage(mock, preimage, sizeof(preimage));
uint8_t hash[32];
compute_sha256(preimage, sizeof(preimage), hash);
mock_dispatcher_set_tamper_hook(mock, tamper_more_bytes_than_remaining, NULL);
uint8_t out[512];
dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
/* n_bytes > bytes_remaining → -8, or buffer_can_read fails → -6 */
assert_true(result < 0);
}
/**
* Adversarial: client corrupts continuation data (GET_MORE_ELEMENTS payload),
* causing hash mismatch at the end.
*/
static int tamper_corrupt_continuation(uint8_t *response_buf,
size_t *response_len,
uint8_t cmd,
int call_count,
void *user_data) {
(void) user_data;
(void) call_count;
if (cmd == CCMD_GET_MORE_ELEMENTS && *response_len > 2) {
response_buf[2] ^= 0xFF;
}
return 0;
}
static void test_get_preimage_corrupted_continuation(void **state) {
mock_dispatcher_t *mock = *state;
uint8_t preimage[300];
for (size_t i = 0; i < sizeof(preimage); i++) {
preimage[i] = (uint8_t) (i & 0xFF);
}
mock_dispatcher_add_preimage(mock, preimage, sizeof(preimage));
uint8_t hash[32];
compute_sha256(preimage, sizeof(preimage), hash);
mock_dispatcher_set_tamper_hook(mock, tamper_corrupt_continuation, NULL);
uint8_t out[512];
dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
assert_true(result < 0);
}
/**
* Adversarial: client returns a preimage_len > UINT32_MAX (in the 9-byte varint
* encoding). call_get_preimage should reject it with -11.
*/
static int tamper_preimage_len_too_big(uint8_t *response_buf,
size_t *response_len,
uint8_t cmd,
int call_count,
void *user_data) {
(void) user_data;
(void) call_count;
if (cmd == CCMD_GET_PREIMAGE) {
/* Rewrite the response with a 9-byte varint encoding 2^32 (overflow). */
response_buf[0] = 0xFF; /* 9-byte varint marker */
/* Little-endian uint64 = 0x0000000100000000 (= 2^32) */
response_buf[1] = 0x00;
response_buf[2] = 0x00;
response_buf[3] = 0x00;
response_buf[4] = 0x00;
response_buf[5] = 0x01;
response_buf[6] = 0x00;
response_buf[7] = 0x00;
response_buf[8] = 0x00;
response_buf[9] = 0x00; /* partial_data_len */
*response_len = 10;
}
return 0;
}
static void test_get_preimage_overflow_len(void **state) {
mock_dispatcher_t *mock = *state;
uint8_t preimage[10] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A};
mock_dispatcher_add_preimage(mock, preimage, sizeof(preimage));
uint8_t hash[32];
compute_sha256(preimage, sizeof(preimage), hash);
mock_dispatcher_set_tamper_hook(mock, tamper_preimage_len_too_big, NULL);
uint8_t out[256];
dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
/* preimage_len_u64 > UINT32_MAX → return -11 */
assert_int_equal(result, -11);
}
/**
* Adversarial: client claims partial_data_len > preimage_len, while also
* supplying enough trailing bytes to satisfy buffer_can_read. Must hit the
* `partial_data_len > preimage_len` check (return -4), not -2.
*/
static int tamper_partial_len_strictly_over(uint8_t *response_buf,
size_t *response_len,
uint8_t cmd,
int call_count,
void *user_data) {
(void) user_data;
(void) call_count;
if (cmd == CCMD_GET_PREIMAGE && *response_len >= 2) {
uint8_t preimage_len = response_buf[0];
/* Claim one more byte than actually exists in the preimage. */
response_buf[1] = preimage_len + 1;
/* Append a single dummy byte so buffer_can_read(partial_data_len) passes. */
response_buf[*response_len] = 0xCC;
(*response_len)++;
}
return 0;
}
static void test_get_preimage_partial_len_strictly_over(void **state) {
mock_dispatcher_t *mock = *state;
uint8_t preimage[5] = {0x10, 0x20, 0x30, 0x40, 0x50};
mock_dispatcher_add_preimage(mock, preimage, sizeof(preimage));
uint8_t hash[32];
compute_sha256(preimage, sizeof(preimage), hash);
mock_dispatcher_set_tamper_hook(mock, tamper_partial_len_strictly_over, NULL);
uint8_t out[256];
dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, -4);
}
/**
* Adversarial: during GET_MORE_ELEMENTS, client returns elements_len != 1 while
* setting n_bytes = 0 so buffer_can_read(0) trivially passes — exercises the
* `elements_len != 1` rejection (return -7).
*/
static int tamper_more_elements_bad_size_strict(uint8_t *response_buf,
size_t *response_len,
uint8_t cmd,
int call_count,
void *user_data) {
(void) user_data;
(void) call_count;
if (cmd == CCMD_GET_MORE_ELEMENTS && *response_len >= 2) {
response_buf[0] = 0; /* n_bytes = 0 → buffer_can_read(0) is true */
response_buf[1] = 2; /* elements_len = 2 (invalid) */
}
return 0;
}
static void test_get_preimage_bad_element_size_strict(void **state) {
mock_dispatcher_t *mock = *state;
uint8_t preimage[300];
for (size_t i = 0; i < sizeof(preimage); i++) {
preimage[i] = (uint8_t) (i & 0xFF);
}
mock_dispatcher_add_preimage(mock, preimage, sizeof(preimage));
uint8_t hash[32];
compute_sha256(preimage, sizeof(preimage), hash);
mock_dispatcher_set_tamper_hook(mock, tamper_more_elements_bad_size_strict, NULL);
uint8_t out[512];
dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, -7);
}
/**
* Adversarial: during GET_MORE_ELEMENTS, client claims n_bytes > bytes_remaining
* but pads the buffer so buffer_can_read still passes — exercises the
* `n_bytes > bytes_remaining` rejection (return -8).
*/
static int tamper_more_bytes_with_padding(uint8_t *response_buf,
size_t *response_len,
uint8_t cmd,
int call_count,
void *user_data) {
(void) user_data;
(void) call_count;
if (cmd == CCMD_GET_MORE_ELEMENTS && *response_len >= 2) {
uint8_t orig_n = response_buf[0];
response_buf[0] = orig_n + 1;
/* Pad one extra byte so buffer_can_read((orig_n+1)*1) still passes. */
response_buf[*response_len] = 0xAB;
(*response_len)++;
}
return 0;
}
static void test_get_preimage_more_bytes_strict(void **state) {
mock_dispatcher_t *mock = *state;
uint8_t preimage[254];
for (size_t i = 0; i < sizeof(preimage); i++) {
preimage[i] = (uint8_t) (i * 7);
}
mock_dispatcher_add_preimage(mock, preimage, sizeof(preimage));
uint8_t hash[32];
compute_sha256(preimage, sizeof(preimage), hash);
mock_dispatcher_set_tamper_hook(mock, tamper_more_bytes_with_padding, NULL);
uint8_t out[512];
dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, -8);
}
/**
* Adversarial: communication failure mid-transfer (process_interruption fails
* on the second call).
*/
static int tamper_fail_second_call(uint8_t *response_buf,
size_t *response_len,
uint8_t cmd,
int call_count,
void *user_data) {
(void) response_buf;
(void) response_len;
(void) cmd;
(void) user_data;
if (call_count >= 1) {
return -1;
}
return 0;
}
static void test_get_preimage_communication_failure(void **state) {
mock_dispatcher_t *mock = *state;
uint8_t preimage[300];
for (size_t i = 0; i < sizeof(preimage); i++) {
preimage[i] = (uint8_t) (i * 3);
}
mock_dispatcher_add_preimage(mock, preimage, sizeof(preimage));
uint8_t hash[32];
compute_sha256(preimage, sizeof(preimage), hash);
mock_dispatcher_set_tamper_hook(mock, tamper_fail_second_call, NULL);
uint8_t out[512];
dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
assert_true(result < 0);
}
/* ---------- Main ---------- */
int main(void) {
#define T(fn) cmocka_unit_test_setup_teardown(fn, mock_dispatcher_setup, mock_dispatcher_teardown)
const struct CMUnitTest tests[] = {
T(test_get_preimage_small),
T(test_get_preimage_large),
T(test_get_preimage_unknown_hash),
T(test_get_preimage_buffer_too_small),
T(test_get_preimage_one_byte),
T(test_get_preimage_exact_fit),
T(test_get_preimage_one_byte_overflow),
T(test_get_preimage_corrupted_data),
T(test_get_preimage_partial_len_overflow),
T(test_get_preimage_zero_len),
T(test_get_preimage_bad_element_size),
T(test_get_preimage_more_bytes_than_remaining),
T(test_get_preimage_corrupted_continuation),
T(test_get_preimage_overflow_len),
T(test_get_preimage_partial_len_strictly_over),
T(test_get_preimage_bad_element_size_strict),
T(test_get_preimage_more_bytes_strict),
T(test_get_preimage_communication_failure),
};
#undef T
return cmocka_run_group_tests(tests, NULL, NULL);
}