Simplify unit test setup/teardown, and allocate state on the heap
What changed, and why it matters
This commit is a pure refactoring of the project's unit tests. It replaces manually initialized static mock dispatcher objects with heap-allocated ones managed by cmocka test fixtures, and moves hash-pool reset into the common setup function. There is no change to the actual Ledger Bitcoin app firmware or its security-critical code, so this commit does not introduce or fix any user-facing security vulnerability.
No security action required. Treat as normal code-quality/test-maintenance change. If desired, verify that the unit-test suite still passes and that the new heap allocations are properly freed on all teardown paths.
Security signals we found
No changes to production firmware code
No changes to cryptographic or APDU handling logic
Refactoring confined to unit-test harness and fixtures
Heap allocation introduced only in test mock setup/teardown
Evidence from the diff
The diff modifies only files under unit-tests/. It adds mock_dispatcher_setup() and mock_dispatcher_teardown() fixtures that malloc/free a mock_dispatcher_t, and updates mock_dispatcher_init() to also reset g_sha256_pool_next. All test cases are updated to receive the mock via cmocka’s void **state instead of declaring static local structs, and test registration switches from cmocka_unit_test() to cmocka_unit_test_setup_teardown(). No production source files, dispatcher logic, cryptographic code, or APDU handling are changed.
Changed components
unit-tests/libs/mock_dispatcher.cunit-tests/libs/mock_dispatcher.hunit-tests/test_check_merkle_tree_sorted.cunit-tests/test_extract_bip32_derivation.cunit-tests/test_get_merkle_leaf_element.cunit-tests/test_get_merkle_leaf_hash.cunit-tests/test_get_merkle_leaf_index.cunit-tests/test_get_merkle_preimage.cunit-tests/test_get_preimage.cunit-tests/test_stream_merkle_leaf_element.cunit-tests/test_stream_merkleized_map_value.cunit-tests/test_stream_preimage.cInspect captured patch +639 / −1090
diff --git a/unit-tests/libs/mock_dispatcher.c b/unit-tests/libs/mock_dispatcher.c
index 7874274..c345a28 100644
--- a/unit-tests/libs/mock_dispatcher.c
+++ b/unit-tests/libs/mock_dispatcher.c
@@ -6,6 +6,7 @@
*/
#include <string.h>
+#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
@@ -24,10 +25,6 @@ static mock_dispatcher_t *g_active_mock = NULL;
/* ---- External: reset the cx_hash_mock pool ---- */
extern int g_sha256_pool_next;
-void mock_dispatcher_reset_hash_pool(void) {
- g_sha256_pool_next = 0;
-}
-
/* ---- Helper: compute SHA-256 of a buffer ---- */
static void mock_sha256(const uint8_t *data, size_t len, uint8_t out[32]) {
calc_sha_256(out, data, len);
@@ -436,6 +433,32 @@ void mock_dispatcher_init(mock_dispatcher_t *mock) {
/* Set global pointer so callbacks can find us */
g_active_mock = mock;
+
+ /* Reset the cx_hash_mock pool so each test starts from a clean slate. */
+ g_sha256_pool_next = 0;
+}
+
+int mock_dispatcher_setup(void **state) {
+ mock_dispatcher_t *mock = malloc(sizeof(mock_dispatcher_t));
+ if (mock == NULL) {
+ return -1;
+ }
+ mock_dispatcher_init(mock);
+ *state = mock;
+ return 0;
+}
+
+int mock_dispatcher_teardown(void **state) {
+ mock_dispatcher_t *mock = *state;
+ if (mock == NULL) {
+ return 0;
+ }
+ if (g_active_mock == mock) {
+ g_active_mock = NULL;
+ }
+ free(mock);
+ *state = NULL;
+ return 0;
}
void mock_dispatcher_add_preimage(mock_dispatcher_t *mock, const uint8_t *data, size_t len) {
diff --git a/unit-tests/libs/mock_dispatcher.h b/unit-tests/libs/mock_dispatcher.h
index c4203b3..502bd5b 100644
--- a/unit-tests/libs/mock_dispatcher.h
+++ b/unit-tests/libs/mock_dispatcher.h
@@ -11,12 +11,22 @@
* - CCMD_GET_MORE_ELEMENTS (0xA0)
* - CCMD_YIELD (0x10)
*
- * Usage:
+ * Usage (manual):
* mock_dispatcher_t mock;
* mock_dispatcher_init(&mock);
* mock_dispatcher_add_preimage(&mock, data, len);
* dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
* int result = call_get_preimage(dc, hash, out, out_len);
+ *
+ * Usage (cmocka fixture):
+ * static void test_something(void **state) {
+ * mock_dispatcher_t *mock = *state;
+ * ...
+ * }
+ * ...
+ * cmocka_unit_test_setup_teardown(test_something,
+ * mock_dispatcher_setup,
+ * mock_dispatcher_teardown);
*/
#include <stdint.h>
@@ -125,11 +135,24 @@ typedef struct {
/* ---- Public API ---- */
/**
- * Initialize a mock dispatcher. Zero-initializes all state and wires up
- * the function pointers in mock->dc.
+ * Initialize a mock dispatcher. Zero-initializes all state, wires up
+ * the function pointers in mock->dc, and resets the cx_hash_mock pool so
+ * tests start from a clean slate.
*/
void mock_dispatcher_init(mock_dispatcher_t *mock);
+/**
+ * cmocka setup fixture: allocates a mock_dispatcher_t on the heap, initializes
+ * it, and stores the pointer in *state.
+ */
+int mock_dispatcher_setup(void **state);
+
+/**
+ * cmocka teardown fixture: frees the mock_dispatcher_t allocated by
+ * mock_dispatcher_setup.
+ */
+int mock_dispatcher_teardown(void **state);
+
/**
* Register a known preimage. Computes sha256(data) and stores the mapping.
* The mock will respond to CCMD_GET_PREIMAGE requests matching this hash.
@@ -224,8 +247,3 @@ static inline dispatcher_context_t *mock_dispatcher_get_dc(mock_dispatcher_t *mo
return &mock->dc;
}
-/**
- * Reset the hash context pool (call between independent tests to avoid
- * exhausting the fixed-size pool in cx_hash_mock).
- */
-void mock_dispatcher_reset_hash_pool(void);
diff --git a/unit-tests/test_check_merkle_tree_sorted.c b/unit-tests/test_check_merkle_tree_sorted.c
index 5173df6..63d0adb 100644
--- a/unit-tests/test_check_merkle_tree_sorted.c
+++ b/unit-tests/test_check_merkle_tree_sorted.c
@@ -67,11 +67,7 @@ static void tracking_callback(dispatcher_context_t *dc,
* Happy path: three elements in strict lexicographic order.
*/
static void test_sorted_three_elements(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
/* Elements in sorted order: "aaa" < "bbb" < "ccc" */
const uint8_t *elems[] = {(const uint8_t *) "aaa",
@@ -79,14 +75,14 @@ static void test_sorted_three_elements(void **state) {
(const uint8_t *) "ccc"};
size_t lens[] = {3, 3, 3};
- mock_dispatcher_add_list(&mock, elems, lens, 3);
+ mock_dispatcher_add_list(mock, elems, lens, 3);
uint8_t root[32];
- memcpy(root, mock.trees[mock.n_trees - 1].root, 32);
+ memcpy(root, mock->trees[mock->n_trees - 1].root, 32);
callback_tracker_t tracker;
memset(&tracker, 0, sizeof(tracker));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_check_merkle_tree_sorted_with_callback(dc,
&tracker,
root,
@@ -109,23 +105,19 @@ static void test_sorted_three_elements(void **state) {
* Happy path: single element tree is always sorted.
*/
static void test_sorted_single_element(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t *elems[] = {(const uint8_t *) "hello"};
size_t lens[] = {5};
- mock_dispatcher_add_list(&mock, elems, lens, 1);
+ mock_dispatcher_add_list(mock, elems, lens, 1);
uint8_t root[32];
- memcpy(root, mock.trees[mock.n_trees - 1].root, 32);
+ memcpy(root, mock->trees[mock->n_trees - 1].root, 32);
callback_tracker_t tracker;
memset(&tracker, 0, sizeof(tracker));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_check_merkle_tree_sorted_with_callback(dc,
&tracker,
root,
@@ -141,20 +133,16 @@ static void test_sorted_single_element(void **state) {
* Happy path: NULL callback (no callback invoked, just order checking).
*/
static void test_sorted_null_callback(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t *elems[] = {(const uint8_t *) "aa", (const uint8_t *) "bb"};
size_t lens[] = {2, 2};
- mock_dispatcher_add_list(&mock, elems, lens, 2);
+ mock_dispatcher_add_list(mock, elems, lens, 2);
uint8_t root[32];
- memcpy(root, mock.trees[mock.n_trees - 1].root, 32);
+ memcpy(root, mock->trees[mock->n_trees - 1].root, 32);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_check_merkle_tree_sorted_with_callback(dc,
NULL,
root,
@@ -168,18 +156,14 @@ static void test_sorted_null_callback(void **state) {
* Happy path: empty tree (size=0) should succeed immediately.
*/
static void test_sorted_empty_tree(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t root[32] = {0};
callback_tracker_t tracker;
memset(&tracker, 0, sizeof(tracker));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_check_merkle_tree_sorted_with_callback(dc,
&tracker,
root,
@@ -196,11 +180,7 @@ static void test_sorted_empty_tree(void **state) {
* call_check_merkle_tree_sorted_with_callback should detect the unsorted order.
*/
static void test_unsorted_descending(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
/* Elements in reverse order: "ccc" > "bbb" > "aaa" */
const uint8_t *elems[] = {(const uint8_t *) "ccc",
@@ -208,11 +188,11 @@ static void test_unsorted_descending(void **state) {
(const uint8_t *) "aaa"};
size_t lens[] = {3, 3, 3};
- mock_dispatcher_add_list(&mock, elems, lens, 3);
+ mock_dispatcher_add_list(mock, elems, lens, 3);
uint8_t root[32];
- memcpy(root, mock.trees[mock.n_trees - 1].root, 32);
+ memcpy(root, mock->trees[mock->n_trees - 1].root, 32);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_check_merkle_tree_sorted_with_callback(dc,
NULL,
root,
@@ -226,22 +206,18 @@ static void test_unsorted_descending(void **state) {
* Error: duplicate elements (equal keys are not strictly sorted).
*/
static void test_unsorted_duplicates(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t *elems[] = {(const uint8_t *) "aaa",
(const uint8_t *) "aaa",
(const uint8_t *) "bbb"};
size_t lens[] = {3, 3, 3};
- mock_dispatcher_add_list(&mock, elems, lens, 3);
+ mock_dispatcher_add_list(mock, elems, lens, 3);
uint8_t root[32];
- memcpy(root, mock.trees[mock.n_trees - 1].root, 32);
+ memcpy(root, mock->trees[mock->n_trees - 1].root, 32);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_check_merkle_tree_sorted_with_callback(dc,
NULL,
root,
@@ -256,25 +232,21 @@ static void test_unsorted_duplicates(void **state) {
* "a" < "aa" < "b" in lexicographic order (shorter prefix comes first).
*/
static void test_sorted_different_lengths(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t *elems[] = {(const uint8_t *) "a",
(const uint8_t *) "aa",
(const uint8_t *) "b"};
size_t lens[] = {1, 2, 1};
- mock_dispatcher_add_list(&mock, elems, lens, 3);
+ mock_dispatcher_add_list(mock, elems, lens, 3);
uint8_t root[32];
- memcpy(root, mock.trees[mock.n_trees - 1].root, 32);
+ memcpy(root, mock->trees[mock->n_trees - 1].root, 32);
callback_tracker_t tracker;
memset(&tracker, 0, sizeof(tracker));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_check_merkle_tree_sorted_with_callback(dc,
&tracker,
root,
@@ -293,21 +265,17 @@ static void test_sorted_different_lengths(void **state) {
* "aa" before "a" — the longer prefix comes first, violating order.
*/
static void test_unsorted_prefix(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t *elems[] = {(const uint8_t *) "aa",
(const uint8_t *) "a"};
size_t lens[] = {2, 1};
- mock_dispatcher_add_list(&mock, elems, lens, 2);
+ mock_dispatcher_add_list(mock, elems, lens, 2);
uint8_t root[32];
- memcpy(root, mock.trees[mock.n_trees - 1].root, 32);
+ memcpy(root, mock->trees[mock->n_trees - 1].root, 32);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_check_merkle_tree_sorted_with_callback(dc,
NULL,
root,
@@ -322,11 +290,7 @@ static void test_unsorted_prefix(void **state) {
* Verifies correctness with a larger tree that exercises multiple levels of merkle proofs.
*/
static void test_sorted_many_elements(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
/* Generate 10 sorted elements: "\x00", "\x01", ..., "\x09" */
uint8_t raw[10][1];
@@ -339,14 +303,14 @@ static void test_sorted_many_elements(void **state) {
lens[i] = 1;
}
- mock_dispatcher_add_list(&mock, elems, lens, 10);
+ mock_dispatcher_add_list(mock, elems, lens, 10);
uint8_t root[32];
- memcpy(root, mock.trees[mock.n_trees - 1].root, 32);
+ memcpy(root, mock->trees[mock->n_trees - 1].root, 32);
callback_tracker_t tracker;
memset(&tracker, 0, sizeof(tracker));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_check_merkle_tree_sorted_with_callback(dc,
&tracker,
root,
@@ -369,20 +333,16 @@ static void test_sorted_many_elements(void **state) {
* different number of elements.
*/
static void test_wrong_tree_size(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t *elems[] = {(const uint8_t *) "a", (const uint8_t *) "b"};
size_t lens[] = {1, 1};
- mock_dispatcher_add_list(&mock, elems, lens, 2);
+ mock_dispatcher_add_list(mock, elems, lens, 2);
uint8_t root[32];
- memcpy(root, mock.trees[mock.n_trees - 1].root, 32);
+ memcpy(root, mock->trees[mock->n_trees - 1].root, 32);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
/* Pass size=5 when actual tree has 2 elements */
int result = call_check_merkle_tree_sorted_with_callback(dc,
NULL,
@@ -417,18 +377,14 @@ static void commitment_tracking_callback(dispatcher_context_t *dc,
}
static void test_map_commitment_passed(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t *elems[] = {(const uint8_t *) "x", (const uint8_t *) "y"};
size_t lens[] = {1, 1};
- mock_dispatcher_add_list(&mock, elems, lens, 2);
+ mock_dispatcher_add_list(mock, elems, lens, 2);
uint8_t root[32];
- memcpy(root, mock.trees[mock.n_trees - 1].root, 32);
+ memcpy(root, mock->trees[mock->n_trees - 1].root, 32);
merkleized_map_commitment_t dummy_commitment;
memset(&dummy_commitment, 0xAB, sizeof(dummy_commitment));
@@ -436,7 +392,7 @@ static void test_map_commitment_passed(void **state) {
commitment_tracker_t tracker;
memset(&tracker, 0, sizeof(tracker));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_check_merkle_tree_sorted_with_callback(dc,
&tracker,
root,
@@ -451,19 +407,21 @@ static void test_map_commitment_passed(void **state) {
/* ---------- Main ---------- */
int main(void) {
+#define T(fn) cmocka_unit_test_setup_teardown(fn, mock_dispatcher_setup, mock_dispatcher_teardown)
const struct CMUnitTest tests[] = {
- cmocka_unit_test(test_sorted_three_elements),
- cmocka_unit_test(test_sorted_single_element),
- cmocka_unit_test(test_sorted_null_callback),
- cmocka_unit_test(test_sorted_empty_tree),
- cmocka_unit_test(test_unsorted_descending),
- cmocka_unit_test(test_unsorted_duplicates),
- cmocka_unit_test(test_sorted_different_lengths),
- cmocka_unit_test(test_unsorted_prefix),
- cmocka_unit_test(test_sorted_many_elements),
- cmocka_unit_test(test_wrong_tree_size),
- cmocka_unit_test(test_map_commitment_passed),
+ T(test_sorted_three_elements),
+ T(test_sorted_single_element),
+ T(test_sorted_null_callback),
+ T(test_sorted_empty_tree),
+ T(test_unsorted_descending),
+ T(test_unsorted_duplicates),
+ T(test_sorted_different_lengths),
+ T(test_unsorted_prefix),
+ T(test_sorted_many_elements),
+ T(test_wrong_tree_size),
+ T(test_map_commitment_passed),
};
+#undef T
return cmocka_run_group_tests(tests, NULL, NULL);
}
diff --git a/unit-tests/test_extract_bip32_derivation.c b/unit-tests/test_extract_bip32_derivation.c
index 4c3cbad..112802e 100644
--- a/unit-tests/test_extract_bip32_derivation.c
+++ b/unit-tests/test_extract_bip32_derivation.c
@@ -114,11 +114,7 @@ static int find_sorted_value_index(const psbt_map_t *map, uint8_t key_type) {
* Expected: fingerprint=0xf5acc2fd, path=m/84'/1'/0'/1/8
*/
static void test_wpkh_input_bip32_derivation(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
static uint8_t psbt_bin[MAX_PSBT_BIN];
int psbt_len = base64_decode(psbt_wpkh_1to2_b64, psbt_bin, sizeof(psbt_bin));
@@ -128,14 +124,14 @@ static void test_wpkh_input_bip32_derivation(void **state) {
assert_int_equal(psbt_parse(psbt_bin, (size_t) psbt_len, 1, 2, &parsed), 0);
mock_psbt_t psbt_info;
- assert_int_equal(mock_dispatcher_add_psbt(&mock, psbt_bin, (size_t) psbt_len, 1, 2, &psbt_info),
+ assert_int_equal(mock_dispatcher_add_psbt(mock, psbt_bin, (size_t) psbt_len, 1, 2, &psbt_info),
0);
/* Find the PSBT_IN_BIP32_DERIVATION entry's sorted index */
int idx = find_sorted_value_index(&parsed.input_maps[0], PSBT_IN_BIP32_DERIVATION);
assert_true(idx >= 0);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
uint32_t out[1 + MAX_BIP32_PATH_STEPS];
memset(out, 0, sizeof(out));
@@ -160,11 +156,7 @@ static void test_wpkh_input_bip32_derivation(void **state) {
* Expected: fingerprint=0xf5acc2fd, path=m/84'/1'/0'/1/10
*/
static void test_wpkh_output_bip32_derivation(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
static uint8_t psbt_bin[MAX_PSBT_BIN];
int psbt_len = base64_decode(psbt_wpkh_1to2_b64, psbt_bin, sizeof(psbt_bin));
@@ -174,7 +166,7 @@ static void test_wpkh_output_bip32_derivation(void **state) {
assert_int_equal(psbt_parse(psbt_bin, (size_t) psbt_len, 1, 2, &parsed), 0);
mock_psbt_t psbt_info;
- assert_int_equal(mock_dispatcher_add_psbt(&mock, psbt_bin, (size_t) psbt_len, 1, 2, &psbt_info),
+ assert_int_equal(mock_dispatcher_add_psbt(mock, psbt_bin, (size_t) psbt_len, 1, 2, &psbt_info),
0);
/* Find which output has a BIP32 derivation entry */
@@ -189,7 +181,7 @@ static void test_wpkh_output_bip32_derivation(void **state) {
}
assert_true(out_idx >= 0);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
uint32_t out[1 + MAX_BIP32_PATH_STEPS];
memset(out, 0, sizeof(out));
@@ -214,11 +206,7 @@ static void test_wpkh_output_bip32_derivation(void **state) {
* Expected: 0 leaf hashes, fingerprint=0xf5acc2fd, path=m/86'/1'/0'/1/3
*/
static void test_taproot_input_tap_bip32_derivation(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
static uint8_t psbt_bin[MAX_PSBT_BIN];
int psbt_len = base64_decode(psbt_tr_1to2_b64, psbt_bin, sizeof(psbt_bin));
@@ -228,13 +216,13 @@ static void test_taproot_input_tap_bip32_derivation(void **state) {
assert_int_equal(psbt_parse(psbt_bin, (size_t) psbt_len, 1, 2, &parsed), 0);
mock_psbt_t psbt_info;
- assert_int_equal(mock_dispatcher_add_psbt(&mock, psbt_bin, (size_t) psbt_len, 1, 2, &psbt_info),
+ assert_int_equal(mock_dispatcher_add_psbt(mock, psbt_bin, (size_t) psbt_len, 1, 2, &psbt_info),
0);
int idx = find_sorted_value_index(&parsed.input_maps[0], PSBT_IN_TAP_BIP32_DERIVATION);
assert_true(idx >= 0);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
uint32_t out[1 + MAX_BIP32_PATH_STEPS];
memset(out, 0, sizeof(out));
@@ -259,11 +247,7 @@ static void test_taproot_input_tap_bip32_derivation(void **state) {
* Expected: 0 leaf hashes, fingerprint=0xf5acc2fd, path=m/86'/1'/0'/1/2
*/
static void test_taproot_output_tap_bip32_derivation(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
static uint8_t psbt_bin[MAX_PSBT_BIN];
int psbt_len = base64_decode(psbt_tr_1to2_b64, psbt_bin, sizeof(psbt_bin));
@@ -273,7 +257,7 @@ static void test_taproot_output_tap_bip32_derivation(void **state) {
assert_int_equal(psbt_parse(psbt_bin, (size_t) psbt_len, 1, 2, &parsed), 0);
mock_psbt_t psbt_info;
- assert_int_equal(mock_dispatcher_add_psbt(&mock, psbt_bin, (size_t) psbt_len, 1, 2, &psbt_info),
+ assert_int_equal(mock_dispatcher_add_psbt(mock, psbt_bin, (size_t) psbt_len, 1, 2, &psbt_info),
0);
/* Find which output has a TAP_BIP32_DERIVATION entry */
@@ -288,7 +272,7 @@ static void test_taproot_output_tap_bip32_derivation(void **state) {
}
assert_true(out_idx >= 0);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
uint32_t out[1 + MAX_BIP32_PATH_STEPS];
memset(out, 0, sizeof(out));
@@ -329,20 +313,16 @@ static void register_single_value(mock_dispatcher_t *mock,
* Exercises the early-reject branch in the data callback.
*/
static void test_extract_nontap_too_long(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
/* max_out_data_length = 4*(1+10) = 44 → 45 bytes is too long */
uint8_t value[45];
for (size_t i = 0; i < sizeof(value); i++) value[i] = (uint8_t) i;
uint8_t root[32];
- register_single_value(&mock, value, sizeof(value), root);
+ register_single_value(mock, value, sizeof(value), root);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
uint32_t out[1 + MAX_BIP32_PATH_STEPS];
int result = extract_bip32_derivation(dc, PSBT_IN_BIP32_DERIVATION, root, 1, 0, out);
@@ -354,18 +334,14 @@ static void test_extract_nontap_too_long(void **state) {
* for the announced number of hashes.
*/
static void test_extract_tap_too_short(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t value[1] = {0x01}; /* n_hashes = 1, but no room for the hash */
uint8_t root[32];
- register_single_value(&mock, value, sizeof(value), root);
+ register_single_value(mock, value, sizeof(value), root);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
uint32_t out[1 + MAX_BIP32_PATH_STEPS];
int result = extract_bip32_derivation(dc, PSBT_IN_TAP_BIP32_DERIVATION, root, 1, 0, out);
@@ -377,20 +353,16 @@ static void test_extract_tap_too_short(void **state) {
* exceed max_out_data_length.
*/
static void test_extract_tap_out_too_long(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
/* n_hashes=0, 50 bytes of fingerprint+path → out_data_length = 49 > 44 */
uint8_t value[50];
memset(value, 0, sizeof(value));
uint8_t root[32];
- register_single_value(&mock, value, sizeof(value), root);
+ register_single_value(mock, value, sizeof(value), root);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
uint32_t out[1 + MAX_BIP32_PATH_STEPS];
int result = extract_bip32_derivation(dc, PSBT_IN_TAP_BIP32_DERIVATION, root, 1, 0, out);
@@ -404,11 +376,7 @@ static void test_extract_tap_out_too_long(void **state) {
* the data callback (memmove + read into tail).
*/
static void test_extract_tap_multi_chunk(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t value[1 + 32 * 7 + 28];
memset(value, 0, sizeof(value));
@@ -426,9 +394,9 @@ static void test_extract_tap_multi_chunk(void **state) {
}
uint8_t root[32];
- register_single_value(&mock, value, sizeof(value), root);
+ register_single_value(mock, value, sizeof(value), root);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
uint32_t out[1 + MAX_BIP32_PATH_STEPS];
int result = extract_bip32_derivation(dc, PSBT_IN_TAP_BIP32_DERIVATION, root, 1, 0, out);
@@ -443,18 +411,14 @@ static void test_extract_tap_multi_chunk(void **state) {
* set, so extract_bip32_derivation reports an error.
*/
static void test_extract_empty_value(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t value[1] = {0x00}; /* unused — register_single_value needs a non-NULL ptr */
uint8_t root[32];
- register_single_value(&mock, value, 0, root);
+ register_single_value(mock, value, 0, root);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
uint32_t out[1 + MAX_BIP32_PATH_STEPS];
int result = extract_bip32_derivation(dc, PSBT_IN_BIP32_DERIVATION, root, 1, 0, out);
@@ -500,19 +464,15 @@ static int tamper_huge_preimage_len(uint8_t *response_buf,
}
static void test_extract_huge_preimage_len(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t value[5] = {0, 0, 0, 0, 0};
uint8_t root[32];
- register_single_value(&mock, value, sizeof(value), root);
+ register_single_value(mock, value, sizeof(value), root);
- mock_dispatcher_set_tamper_hook(&mock, tamper_huge_preimage_len, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_huge_preimage_len, NULL);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
uint32_t out[1 + MAX_BIP32_PATH_STEPS];
int result = extract_bip32_derivation(dc, PSBT_IN_BIP32_DERIVATION, root, 1, 0, out);
@@ -548,19 +508,15 @@ static int tamper_long_len_n_hashes(uint8_t *response_buf,
}
static void test_extract_tap_too_many_hashes(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t value[2] = {130, 0};
uint8_t root[32];
- register_single_value(&mock, value, sizeof(value), root);
+ register_single_value(mock, value, sizeof(value), root);
- mock_dispatcher_set_tamper_hook(&mock, tamper_long_len_n_hashes, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_long_len_n_hashes, NULL);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
uint32_t out[1 + MAX_BIP32_PATH_STEPS];
int result = extract_bip32_derivation(dc, PSBT_IN_TAP_BIP32_DERIVATION, root, 1, 0, out);
@@ -570,19 +526,21 @@ static void test_extract_tap_too_many_hashes(void **state) {
/* ---------- Main ---------- */
int main(void) {
+#define T(fn) cmocka_unit_test_setup_teardown(fn, mock_dispatcher_setup, mock_dispatcher_teardown)
const struct CMUnitTest tests[] = {
- cmocka_unit_test(test_wpkh_input_bip32_derivation),
- cmocka_unit_test(test_wpkh_output_bip32_derivation),
- cmocka_unit_test(test_taproot_input_tap_bip32_derivation),
- cmocka_unit_test(test_taproot_output_tap_bip32_derivation),
- cmocka_unit_test(test_extract_nontap_too_long),
- cmocka_unit_test(test_extract_tap_too_short),
- cmocka_unit_test(test_extract_tap_out_too_long),
- cmocka_unit_test(test_extract_tap_multi_chunk),
- cmocka_unit_test(test_extract_empty_value),
- cmocka_unit_test(test_extract_huge_preimage_len),
- cmocka_unit_test(test_extract_tap_too_many_hashes),
+ T(test_wpkh_input_bip32_derivation),
+ T(test_wpkh_output_bip32_derivation),
+ T(test_taproot_input_tap_bip32_derivation),
+ T(test_taproot_output_tap_bip32_derivation),
+ T(test_extract_nontap_too_long),
+ T(test_extract_tap_too_short),
+ T(test_extract_tap_out_too_long),
+ T(test_extract_tap_multi_chunk),
+ T(test_extract_empty_value),
+ T(test_extract_huge_preimage_len),
+ T(test_extract_tap_too_many_hashes),
};
+#undef T
return cmocka_run_group_tests(tests, NULL, NULL);
}
diff --git a/unit-tests/test_get_merkle_leaf_element.c b/unit-tests/test_get_merkle_leaf_element.c
index 4f00176..cfd7653 100644
--- a/unit-tests/test_get_merkle_leaf_element.c
+++ b/unit-tests/test_get_merkle_leaf_element.c
@@ -50,23 +50,19 @@ static void build_tree(mock_dispatcher_t *mock,
* Happy path: single element tree, retrieve the only leaf.
*/
static void test_get_leaf_element_single(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t elem[] = {0xCA, 0xFE};
const uint8_t *elems[] = {elem};
size_t lens[] = {sizeof(elem)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 1, root);
+ build_tree(mock, elems, lens, 1, root);
uint8_t out[256];
memset(out, 0xAA, sizeof(out));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_element(dc, root, 1, 0, out, sizeof(out));
assert_int_equal(result, (int) sizeof(elem));
@@ -77,11 +73,7 @@ static void test_get_leaf_element_single(void **state) {
* Happy path: three-element tree, retrieve each leaf by index.
*/
static void test_get_leaf_element_three_elements(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t *elems[] = {(const uint8_t *) "alpha",
(const uint8_t *) "beta",
@@ -89,9 +81,9 @@ static void test_get_leaf_element_three_elements(void **state) {
size_t lens[] = {5, 4, 5};
uint8_t root[32];
- build_tree(&mock, elems, lens, 3, root);
+ build_tree(mock, elems, lens, 3, root);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
for (size_t i = 0; i < 3; i++) {
uint8_t out[256];
@@ -108,11 +100,7 @@ static void test_get_leaf_element_three_elements(void **state) {
* Happy path: power-of-two number of elements (4 elements, balanced tree).
*/
static void test_get_leaf_element_four_elements(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t e0[] = {0x00, 0x01, 0x02};
uint8_t e1[] = {0x10, 0x11};
@@ -123,9 +111,9 @@ static void test_get_leaf_element_four_elements(void **state) {
size_t lens[] = {sizeof(e0), sizeof(e1), sizeof(e2), sizeof(e3)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 4, root);
+ build_tree(mock, elems, lens, 4, root);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
for (size_t i = 0; i < 4; i++) {
uint8_t out[256];
@@ -142,23 +130,19 @@ static void test_get_leaf_element_four_elements(void **state) {
* Edge case: leaf element of exactly 1 byte.
*/
static void test_get_leaf_element_one_byte(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t elem[] = {0x42};
const uint8_t *elems[] = {elem};
size_t lens[] = {1};
uint8_t root[32];
- build_tree(&mock, elems, lens, 1, root);
+ build_tree(mock, elems, lens, 1, root);
uint8_t out[64];
memset(out, 0, sizeof(out));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_element(dc, root, 1, 0, out, sizeof(out));
assert_int_equal(result, 1);
@@ -169,11 +153,7 @@ static void test_get_leaf_element_one_byte(void **state) {
* Error: output buffer too small for the leaf element.
*/
static void test_get_leaf_element_buffer_too_small(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t elem[100];
for (size_t i = 0; i < sizeof(elem); i++) {
@@ -183,11 +163,11 @@ static void test_get_leaf_element_buffer_too_small(void **state) {
size_t lens[] = {sizeof(elem)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 1, root);
+ build_tree(mock, elems, lens, 1, root);
uint8_t out[10]; /* Too small */
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_element(dc, root, 1, 0, out, sizeof(out));
assert_true(result < 0);
@@ -197,18 +177,14 @@ static void test_get_leaf_element_buffer_too_small(void **state) {
* Error: wrong Merkle root (no matching tree).
*/
static void test_get_leaf_element_wrong_root(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t elem[] = {0xAB, 0xCD};
const uint8_t *elems[] = {elem};
size_t lens[] = {sizeof(elem)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 1, root);
+ build_tree(mock, elems, lens, 1, root);
/* Corrupt the root */
uint8_t bad_root[32];
@@ -216,7 +192,7 @@ static void test_get_leaf_element_wrong_root(void **state) {
uint8_t out[256];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_element(dc, bad_root, 1, 0, out, sizeof(out));
assert_true(result < 0);
@@ -226,21 +202,17 @@ static void test_get_leaf_element_wrong_root(void **state) {
* Error: leaf index out of bounds (>= tree_size).
*/
static void test_get_leaf_element_index_out_of_bounds(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t *elems[] = {(const uint8_t *) "x", (const uint8_t *) "y"};
size_t lens[] = {1, 1};
uint8_t root[32];
- build_tree(&mock, elems, lens, 2, root);
+ build_tree(mock, elems, lens, 2, root);
uint8_t out[256];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_element(dc, root, 2, 5, out, sizeof(out));
assert_true(result < 0);
@@ -250,11 +222,7 @@ static void test_get_leaf_element_index_out_of_bounds(void **state) {
* Happy path: larger tree (8 elements) to exercise deeper proof paths.
*/
static void test_get_leaf_element_eight_elements(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t data[8][16];
const uint8_t *elems[8];
@@ -269,9 +237,9 @@ static void test_get_leaf_element_eight_elements(void **state) {
}
uint8_t root[32];
- build_tree(&mock, elems, lens, 8, root);
+ build_tree(mock, elems, lens, 8, root);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
for (size_t i = 0; i < 8; i++) {
uint8_t out[256];
@@ -308,11 +276,7 @@ static int tamper_corrupt_leaf_in_proof(uint8_t *response_buf,
}
static void test_get_leaf_element_corrupted_proof(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t e0[] = {0x01, 0x02, 0x03};
uint8_t e1[] = {0x04, 0x05, 0x06};
@@ -323,12 +287,12 @@ static void test_get_leaf_element_corrupted_proof(void **state) {
size_t lens[] = {sizeof(e0), sizeof(e1), sizeof(e2), sizeof(e3)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 4, root);
+ build_tree(mock, elems, lens, 4, root);
- mock_dispatcher_set_tamper_hook(&mock, tamper_corrupt_leaf_in_proof, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_corrupt_leaf_in_proof, NULL);
uint8_t out[256];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_element(dc, root, 4, 0, out, sizeof(out));
assert_true(result < 0);
@@ -354,23 +318,19 @@ static int tamper_corrupt_preimage_after_proof(uint8_t *response_buf,
}
static void test_get_leaf_element_corrupted_preimage(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t e0[] = {0xDE, 0xAD, 0xBE, 0xEF};
const uint8_t *elems[] = {e0};
size_t lens[] = {sizeof(e0)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 1, root);
+ build_tree(mock, elems, lens, 1, root);
- mock_dispatcher_set_tamper_hook(&mock, tamper_corrupt_preimage_after_proof, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_corrupt_preimage_after_proof, NULL);
uint8_t out[256];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_element(dc, root, 1, 0, out, sizeof(out));
/* Preimage hash mismatch → must fail */
@@ -380,18 +340,20 @@ static void test_get_leaf_element_corrupted_preimage(void **state) {
/* ---------- Main ---------- */
int main(void) {
+#define T(fn) cmocka_unit_test_setup_teardown(fn, mock_dispatcher_setup, mock_dispatcher_teardown)
const struct CMUnitTest tests[] = {
- cmocka_unit_test(test_get_leaf_element_single),
- cmocka_unit_test(test_get_leaf_element_three_elements),
- cmocka_unit_test(test_get_leaf_element_four_elements),
- cmocka_unit_test(test_get_leaf_element_one_byte),
- cmocka_unit_test(test_get_leaf_element_buffer_too_small),
- cmocka_unit_test(test_get_leaf_element_wrong_root),
- cmocka_unit_test(test_get_leaf_element_index_out_of_bounds),
- cmocka_unit_test(test_get_leaf_element_eight_elements),
- cmocka_unit_test(test_get_leaf_element_corrupted_proof),
- cmocka_unit_test(test_get_leaf_element_corrupted_preimage),
+ T(test_get_leaf_element_single),
+ T(test_get_leaf_element_three_elements),
+ T(test_get_leaf_element_four_elements),
+ T(test_get_leaf_element_one_byte),
+ T(test_get_leaf_element_buffer_too_small),
+ T(test_get_leaf_element_wrong_root),
+ T(test_get_leaf_element_index_out_of_bounds),
+ T(test_get_leaf_element_eight_elements),
+ T(test_get_leaf_element_corrupted_proof),
+ T(test_get_leaf_element_corrupted_preimage),
};
+#undef T
return cmocka_run_group_tests(tests, NULL, NULL);
}
diff --git a/unit-tests/test_get_merkle_leaf_hash.c b/unit-tests/test_get_merkle_leaf_hash.c
index 098a091..bf3d545 100644
--- a/unit-tests/test_get_merkle_leaf_hash.c
+++ b/unit-tests/test_get_merkle_leaf_hash.c
@@ -63,18 +63,14 @@ static void compute_leaf_hash(const uint8_t *elem, size_t len, uint8_t out[32])
* Happy path: single element tree, retrieve the only leaf hash.
*/
static void test_get_leaf_hash_single(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t elem[] = {0xCA, 0xFE};
const uint8_t *elems[] = {elem};
size_t lens[] = {sizeof(elem)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 1, root);
+ build_tree(mock, elems, lens, 1, root);
uint8_t expected_hash[32];
compute_leaf_hash(elem, sizeof(elem), expected_hash);
@@ -82,7 +78,7 @@ static void test_get_leaf_hash_single(void **state) {
uint8_t out[32];
memset(out, 0, sizeof(out));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_hash(dc, root, 1, 0, out);
assert_int_equal(result, 0);
@@ -93,11 +89,7 @@ static void test_get_leaf_hash_single(void **state) {
* Happy path: three-element tree, retrieve each leaf hash by index.
*/
static void test_get_leaf_hash_three_elements(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t *elems[] = {(const uint8_t *) "alpha",
(const uint8_t *) "beta",
@@ -105,9 +97,9 @@ static void test_get_leaf_hash_three_elements(void **state) {
size_t lens[] = {5, 4, 5};
uint8_t root[32];
- build_tree(&mock, elems, lens, 3, root);
+ build_tree(mock, elems, lens, 3, root);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
for (size_t i = 0; i < 3; i++) {
uint8_t expected_hash[32];
@@ -127,11 +119,7 @@ static void test_get_leaf_hash_three_elements(void **state) {
* Happy path: power-of-two number of elements (4 elements, balanced tree).
*/
static void test_get_leaf_hash_four_elements(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t e0[] = {0x00, 0x01, 0x02};
uint8_t e1[] = {0x10, 0x11};
@@ -142,9 +130,9 @@ static void test_get_leaf_hash_four_elements(void **state) {
size_t lens[] = {sizeof(e0), sizeof(e1), sizeof(e2), sizeof(e3)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 4, root);
+ build_tree(mock, elems, lens, 4, root);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
for (size_t i = 0; i < 4; i++) {
uint8_t expected_hash[32];
@@ -164,11 +152,7 @@ static void test_get_leaf_hash_four_elements(void **state) {
* Happy path: larger unbalanced tree (5 elements).
*/
static void test_get_leaf_hash_five_elements(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t e0[] = {0xAA};
uint8_t e1[] = {0xBB, 0xCC};
@@ -180,9 +164,9 @@ static void test_get_leaf_hash_five_elements(void **state) {
size_t lens[] = {sizeof(e0), sizeof(e1), sizeof(e2), sizeof(e3), sizeof(e4)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 5, root);
+ build_tree(mock, elems, lens, 5, root);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
for (size_t i = 0; i < 5; i++) {
uint8_t expected_hash[32];
@@ -202,11 +186,7 @@ static void test_get_leaf_hash_five_elements(void **state) {
* Happy path: 8-element balanced tree (depth 3).
*/
static void test_get_leaf_hash_eight_elements(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t data[8][4];
const uint8_t *elems[8];
@@ -221,9 +201,9 @@ static void test_get_leaf_hash_eight_elements(void **state) {
}
uint8_t root[32];
- build_tree(&mock, elems, lens, 8, root);
+ build_tree(mock, elems, lens, 8, root);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
for (size_t i = 0; i < 8; i++) {
uint8_t expected_hash[32];
@@ -243,18 +223,14 @@ static void test_get_leaf_hash_eight_elements(void **state) {
* Edge case: leaf element of exactly 1 byte.
*/
static void test_get_leaf_hash_one_byte_element(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t elem[] = {0x42};
const uint8_t *elems[] = {elem};
size_t lens[] = {1};
uint8_t root[32];
- build_tree(&mock, elems, lens, 1, root);
+ build_tree(mock, elems, lens, 1, root);
uint8_t expected_hash[32];
compute_leaf_hash(elem, 1, expected_hash);
@@ -262,7 +238,7 @@ static void test_get_leaf_hash_one_byte_element(void **state) {
uint8_t out[32];
memset(out, 0, sizeof(out));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_hash(dc, root, 1, 0, out);
assert_int_equal(result, 0);
@@ -273,25 +249,21 @@ static void test_get_leaf_hash_one_byte_element(void **state) {
* Error: wrong Merkle root (no matching tree registered).
*/
static void test_get_leaf_hash_wrong_root(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t elem[] = {0xDE, 0xAD};
const uint8_t *elems[] = {elem};
size_t lens[] = {sizeof(elem)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 1, root);
+ build_tree(mock, elems, lens, 1, root);
/* Corrupt the root */
uint8_t bad_root[32];
memset(bad_root, 0xFF, 32);
uint8_t out[32];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_hash(dc, bad_root, 1, 0, out);
assert_true(result < 0);
@@ -301,11 +273,7 @@ static void test_get_leaf_hash_wrong_root(void **state) {
* Happy path: two-element tree, verify both leaves.
*/
static void test_get_leaf_hash_two_elements(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t e0[] = {0x01, 0x02, 0x03};
const uint8_t e1[] = {0x04, 0x05};
@@ -313,9 +281,9 @@ static void test_get_leaf_hash_two_elements(void **state) {
size_t lens[] = {sizeof(e0), sizeof(e1)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 2, root);
+ build_tree(mock, elems, lens, 2, root);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
for (size_t i = 0; i < 2; i++) {
uint8_t expected_hash[32];
@@ -356,11 +324,7 @@ static int tamper_corrupt_proof_hash(uint8_t *response_buf,
}
static void test_get_leaf_hash_corrupted_proof(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t e0[] = {0x00, 0x01};
uint8_t e1[] = {0x10, 0x11};
@@ -371,12 +335,12 @@ static void test_get_leaf_hash_corrupted_proof(void **state) {
size_t lens[] = {sizeof(e0), sizeof(e1), sizeof(e2), sizeof(e3)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 4, root);
+ build_tree(mock, elems, lens, 4, root);
- mock_dispatcher_set_tamper_hook(&mock, tamper_corrupt_proof_hash, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_corrupt_proof_hash, NULL);
uint8_t out[32];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_hash(dc, root, 4, 0, out);
assert_true(result < 0);
@@ -401,11 +365,7 @@ static int tamper_corrupt_leaf_hash(uint8_t *response_buf,
}
static void test_get_leaf_hash_corrupted_leaf(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t e0[] = {0xAA, 0xBB};
uint8_t e1[] = {0xCC, 0xDD};
@@ -414,12 +374,12 @@ static void test_get_leaf_hash_corrupted_leaf(void **state) {
size_t lens[] = {sizeof(e0), sizeof(e1)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 2, root);
+ build_tree(mock, elems, lens, 2, root);
- mock_dispatcher_set_tamper_hook(&mock, tamper_corrupt_leaf_hash, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_corrupt_leaf_hash, NULL);
uint8_t out[32];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_hash(dc, root, 2, 0, out);
assert_true(result < 0);
@@ -444,11 +404,7 @@ static int tamper_proof_elements_overflow(uint8_t *response_buf,
}
static void test_get_leaf_hash_proof_elements_overflow(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t e0[] = {0xAA};
uint8_t e1[] = {0xBB};
@@ -457,12 +413,12 @@ static void test_get_leaf_hash_proof_elements_overflow(void **state) {
size_t lens[] = {sizeof(e0), sizeof(e1)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 2, root);
+ build_tree(mock, elems, lens, 2, root);
- mock_dispatcher_set_tamper_hook(&mock, tamper_proof_elements_overflow, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_proof_elements_overflow, NULL);
uint8_t out[32];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_hash(dc, root, 2, 0, out);
assert_true(result < 0);
@@ -489,11 +445,7 @@ static int tamper_zero_proof_size(uint8_t *response_buf,
}
static void test_get_leaf_hash_zero_proof_size(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t e0[] = {0xAA};
uint8_t e1[] = {0xBB};
@@ -503,12 +455,12 @@ static void test_get_leaf_hash_zero_proof_size(void **state) {
size_t lens[] = {1, 1, 1};
uint8_t root[32];
- build_tree(&mock, elems, lens, 3, root);
+ build_tree(mock, elems, lens, 3, root);
- mock_dispatcher_set_tamper_hook(&mock, tamper_zero_proof_size, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_zero_proof_size, NULL);
uint8_t out[32];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_hash(dc, root, 3, 0, out);
assert_true(result < 0);
@@ -533,11 +485,7 @@ static int tamper_bad_proof_element_size(uint8_t *response_buf,
}
static void test_get_leaf_hash_bad_proof_element_size(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
/* Need a tree large enough that proof spills: 128 elements → depth 7,
* first response fits 6 proof hashes → 1 goes via GET_MORE_ELEMENTS */
@@ -553,12 +501,12 @@ static void test_get_leaf_hash_bad_proof_element_size(void **state) {
}
uint8_t root[32];
- build_tree(&mock, elems, lens, 128, root);
+ build_tree(mock, elems, lens, 128, root);
- mock_dispatcher_set_tamper_hook(&mock, tamper_bad_proof_element_size, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_bad_proof_element_size, NULL);
uint8_t out[32];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_hash(dc, root, 128, 0, out);
assert_true(result < 0);
@@ -585,11 +533,7 @@ static int tamper_truncate_proof_elements(uint8_t *response_buf,
}
static void test_get_leaf_hash_truncated_proof_elements(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t e0[] = {0x00, 0x01};
uint8_t e1[] = {0x10, 0x11};
@@ -600,12 +544,12 @@ static void test_get_leaf_hash_truncated_proof_elements(void **state) {
size_t lens[] = {sizeof(e0), sizeof(e1), sizeof(e2), sizeof(e3)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 4, root);
+ build_tree(mock, elems, lens, 4, root);
- mock_dispatcher_set_tamper_hook(&mock, tamper_truncate_proof_elements, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_truncate_proof_elements, NULL);
uint8_t out[32];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_hash(dc, root, 4, 0, out);
assert_true(result < 0);
@@ -632,11 +576,7 @@ static int tamper_fail_more(uint8_t *response_buf,
}
static void test_get_leaf_hash_more_comm_failure(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
/* 128 elements so the proof has 7 hashes; first response fits 6, so 1
* goes via GET_MORE_ELEMENTS. */
@@ -651,12 +591,12 @@ static void test_get_leaf_hash_more_comm_failure(void **state) {
}
uint8_t root[32];
- build_tree(&mock, elems, lens, 128, root);
+ build_tree(mock, elems, lens, 128, root);
- mock_dispatcher_set_tamper_hook(&mock, tamper_fail_more, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_fail_more, NULL);
uint8_t out[32];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_hash(dc, root, 128, 0, out);
assert_true(result < 0);
@@ -683,11 +623,7 @@ static int tamper_truncate_more(uint8_t *response_buf,
}
static void test_get_leaf_hash_truncated_more(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t data[128][2];
const uint8_t *elems[128];
@@ -700,12 +636,12 @@ static void test_get_leaf_hash_truncated_more(void **state) {
}
uint8_t root[32];
- build_tree(&mock, elems, lens, 128, root);
+ build_tree(mock, elems, lens, 128, root);
- mock_dispatcher_set_tamper_hook(&mock, tamper_truncate_more, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_truncate_more, NULL);
uint8_t out[32];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_hash(dc, root, 128, 0, out);
assert_true(result < 0);
@@ -737,11 +673,7 @@ static int tamper_more_proof_overflow(uint8_t *response_buf,
}
static void test_get_leaf_hash_more_proof_overflow(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t data[128][2];
const uint8_t *elems[128];
@@ -754,12 +686,12 @@ static void test_get_leaf_hash_more_proof_overflow(void **state) {
}
uint8_t root[32];
- build_tree(&mock, elems, lens, 128, root);
+ build_tree(mock, elems, lens, 128, root);
- mock_dispatcher_set_tamper_hook(&mock, tamper_more_proof_overflow, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_more_proof_overflow, NULL);
uint8_t out[32];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_hash(dc, root, 128, 0, out);
assert_true(result < 0);
@@ -768,25 +700,27 @@ static void test_get_leaf_hash_more_proof_overflow(void **state) {
/* ---------- Main ---------- */
int main(void) {
+#define T(fn) cmocka_unit_test_setup_teardown(fn, mock_dispatcher_setup, mock_dispatcher_teardown)
const struct CMUnitTest tests[] = {
- cmocka_unit_test(test_get_leaf_hash_single),
- cmocka_unit_test(test_get_leaf_hash_three_elements),
- cmocka_unit_test(test_get_leaf_hash_four_elements),
- cmocka_unit_test(test_get_leaf_hash_five_elements),
- cmocka_unit_test(test_get_leaf_hash_eight_elements),
- cmocka_unit_test(test_get_leaf_hash_one_byte_element),
- cmocka_unit_test(test_get_leaf_hash_wrong_root),
- cmocka_unit_test(test_get_leaf_hash_two_elements),
- cmocka_unit_test(test_get_leaf_hash_corrupted_proof),
- cmocka_unit_test(test_get_leaf_hash_corrupted_leaf),
- cmocka_unit_test(test_get_leaf_hash_proof_elements_overflow),
- cmocka_unit_test(test_get_leaf_hash_zero_proof_size),
- cmocka_unit_test(test_get_leaf_hash_bad_proof_element_size),
- cmocka_unit_test(test_get_leaf_hash_truncated_proof_elements),
- cmocka_unit_test(test_get_leaf_hash_more_comm_failure),
- cmocka_unit_test(test_get_leaf_hash_truncated_more),
- cmocka_unit_test(test_get_leaf_hash_more_proof_overflow),
+ T(test_get_leaf_hash_single),
+ T(test_get_leaf_hash_three_elements),
+ T(test_get_leaf_hash_four_elements),
+ T(test_get_leaf_hash_five_elements),
+ T(test_get_leaf_hash_eight_elements),
+ T(test_get_leaf_hash_one_byte_element),
+ T(test_get_leaf_hash_wrong_root),
+ T(test_get_leaf_hash_two_elements),
+ T(test_get_leaf_hash_corrupted_proof),
+ T(test_get_leaf_hash_corrupted_leaf),
+ T(test_get_leaf_hash_proof_elements_overflow),
+ T(test_get_leaf_hash_zero_proof_size),
+ T(test_get_leaf_hash_bad_proof_element_size),
+ T(test_get_leaf_hash_truncated_proof_elements),
+ T(test_get_leaf_hash_more_comm_failure),
+ T(test_get_leaf_hash_truncated_more),
+ T(test_get_leaf_hash_more_proof_overflow),
};
+#undef T
return cmocka_run_group_tests(tests, NULL, NULL);
}
diff --git a/unit-tests/test_get_merkle_leaf_index.c b/unit-tests/test_get_merkle_leaf_index.c
index 4f723e4..ee6a766 100644
--- a/unit-tests/test_get_merkle_leaf_index.c
+++ b/unit-tests/test_get_merkle_leaf_index.c
@@ -64,23 +64,19 @@ static void compute_leaf_hash(const uint8_t *elem, size_t len, uint8_t out[32])
* Happy path: single element tree, find the only leaf by its hash.
*/
static void test_get_leaf_index_single(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t elem[] = {0xCA, 0xFE};
const uint8_t *elems[] = {elem};
size_t lens[] = {sizeof(elem)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 1, root);
+ build_tree(mock, elems, lens, 1, root);
uint8_t leaf_hash[32];
compute_leaf_hash(elem, sizeof(elem), leaf_hash);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_index(dc, 1, root, leaf_hash);
assert_int_equal(result, 0);
@@ -90,11 +86,7 @@ static void test_get_leaf_index_single(void **state) {
* Happy path: three-element tree, find each leaf index.
*/
static void test_get_leaf_index_three_elements(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t *elems[] = {(const uint8_t *) "alpha",
(const uint8_t *) "beta",
@@ -102,9 +94,9 @@ static void test_get_leaf_index_three_elements(void **state) {
size_t lens[] = {5, 4, 5};
uint8_t root[32];
- build_tree(&mock, elems, lens, 3, root);
+ build_tree(mock, elems, lens, 3, root);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
for (size_t i = 0; i < 3; i++) {
uint8_t leaf_hash[32];
@@ -120,11 +112,7 @@ static void test_get_leaf_index_three_elements(void **state) {
* Happy path: power-of-two number of elements (4 elements, balanced tree).
*/
static void test_get_leaf_index_four_elements(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t e0[] = {0x00, 0x01, 0x02};
uint8_t e1[] = {0x10, 0x11};
@@ -135,9 +123,9 @@ static void test_get_leaf_index_four_elements(void **state) {
size_t lens[] = {sizeof(e0), sizeof(e1), sizeof(e2), sizeof(e3)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 4, root);
+ build_tree(mock, elems, lens, 4, root);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
for (size_t i = 0; i < 4; i++) {
uint8_t leaf_hash[32];
@@ -153,11 +141,7 @@ static void test_get_leaf_index_four_elements(void **state) {
* Happy path: 5-element unbalanced tree.
*/
static void test_get_leaf_index_five_elements(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t e0[] = {0xAA};
uint8_t e1[] = {0xBB, 0xCC};
@@ -169,9 +153,9 @@ static void test_get_leaf_index_five_elements(void **state) {
size_t lens[] = {sizeof(e0), sizeof(e1), sizeof(e2), sizeof(e3), sizeof(e4)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 5, root);
+ build_tree(mock, elems, lens, 5, root);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
for (size_t i = 0; i < 5; i++) {
uint8_t leaf_hash[32];
@@ -187,24 +171,20 @@ static void test_get_leaf_index_five_elements(void **state) {
* Error: unknown leaf hash (not in the tree).
*/
static void test_get_leaf_index_unknown_hash(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t elem[] = {0xCA, 0xFE};
const uint8_t *elems[] = {elem};
size_t lens[] = {sizeof(elem)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 1, root);
+ build_tree(mock, elems, lens, 1, root);
/* Use a leaf hash that doesn't exist in the tree */
uint8_t fake_hash[32];
memset(fake_hash, 0xDE, 32);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_index(dc, 1, root, fake_hash);
assert_true(result < 0);
@@ -214,18 +194,14 @@ static void test_get_leaf_index_unknown_hash(void **state) {
* Error: wrong Merkle root (no matching tree registered).
*/
static void test_get_leaf_index_wrong_root(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t elem[] = {0xDE, 0xAD};
const uint8_t *elems[] = {elem};
size_t lens[] = {sizeof(elem)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 1, root);
+ build_tree(mock, elems, lens, 1, root);
uint8_t leaf_hash[32];
compute_leaf_hash(elem, sizeof(elem), leaf_hash);
@@ -234,7 +210,7 @@ static void test_get_leaf_index_wrong_root(void **state) {
uint8_t bad_root[32];
memset(bad_root, 0xFF, 32);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_index(dc, 1, bad_root, leaf_hash);
assert_true(result < 0);
@@ -244,11 +220,7 @@ static void test_get_leaf_index_wrong_root(void **state) {
* Happy path: two-element tree, find both leaves.
*/
static void test_get_leaf_index_two_elements(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t e0[] = {0x01, 0x02, 0x03};
const uint8_t e1[] = {0x04, 0x05};
@@ -256,9 +228,9 @@ static void test_get_leaf_index_two_elements(void **state) {
size_t lens[] = {sizeof(e0), sizeof(e1)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 2, root);
+ build_tree(mock, elems, lens, 2, root);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
for (size_t i = 0; i < 2; i++) {
uint8_t leaf_hash[32];
@@ -274,11 +246,7 @@ static void test_get_leaf_index_two_elements(void **state) {
* Happy path: 8-element balanced tree (depth 3).
*/
static void test_get_leaf_index_eight_elements(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t data[8][4];
const uint8_t *elems[8];
@@ -293,9 +261,9 @@ static void test_get_leaf_index_eight_elements(void **state) {
}
uint8_t root[32];
- build_tree(&mock, elems, lens, 8, root);
+ build_tree(mock, elems, lens, 8, root);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
for (size_t i = 0; i < 8; i++) {
uint8_t leaf_hash[32];
@@ -333,11 +301,7 @@ static int tamper_wrong_index(uint8_t *response_buf,
}
static void test_get_leaf_index_wrong_index(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t e0[] = {0x01, 0x02, 0x03};
uint8_t e1[] = {0x04, 0x05, 0x06};
@@ -346,15 +310,15 @@ static void test_get_leaf_index_wrong_index(void **state) {
size_t lens[] = {sizeof(e0), sizeof(e1)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 2, root);
+ build_tree(mock, elems, lens, 2, root);
/* Query for e1 (index 1), tamper will claim index 0 */
uint8_t leaf_hash[32];
compute_leaf_hash(e1, sizeof(e1), leaf_hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_wrong_index, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_wrong_index, NULL);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_index(dc, 2, root, leaf_hash);
/* Verification fetch at wrong index → hash mismatch → must fail */
@@ -381,11 +345,7 @@ static int tamper_oob_index(uint8_t *response_buf,
}
static void test_get_leaf_index_oob_index(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t e0[] = {0xAA};
uint8_t e1[] = {0xBB};
@@ -394,14 +354,14 @@ static void test_get_leaf_index_oob_index(void **state) {
size_t lens[] = {1, 1};
uint8_t root[32];
- build_tree(&mock, elems, lens, 2, root);
+ build_tree(mock, elems, lens, 2, root);
uint8_t leaf_hash[32];
compute_leaf_hash(e0, sizeof(e0), leaf_hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_oob_index, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_oob_index, NULL);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_index(dc, 2, root, leaf_hash);
/* index >= size → must fail */
@@ -426,25 +386,21 @@ static int tamper_fail_first(uint8_t *response_buf,
}
static void test_get_leaf_index_initial_comm_failure(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t elem[] = {0xCA, 0xFE};
const uint8_t *elems[] = {elem};
size_t lens[] = {sizeof(elem)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 1, root);
+ build_tree(mock, elems, lens, 1, root);
uint8_t leaf_hash[32];
compute_leaf_hash(elem, sizeof(elem), leaf_hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_fail_first, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_fail_first, NULL);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_index(dc, 1, root, leaf_hash);
assert_int_equal(result, -3);
@@ -470,25 +426,21 @@ static int tamper_invalid_found(uint8_t *response_buf,
}
static void test_get_leaf_index_invalid_found(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t elem[] = {0xCA, 0xFE};
const uint8_t *elems[] = {elem};
size_t lens[] = {sizeof(elem)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 1, root);
+ build_tree(mock, elems, lens, 1, root);
uint8_t leaf_hash[32];
compute_leaf_hash(elem, sizeof(elem), leaf_hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_invalid_found, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_invalid_found, NULL);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_index(dc, 1, root, leaf_hash);
assert_int_equal(result, -2);
@@ -516,25 +468,21 @@ static int tamper_fail_second_call(uint8_t *response_buf,
}
static void test_get_leaf_index_verify_comm_failure(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t elem[] = {0xCA, 0xFE};
const uint8_t *elems[] = {elem};
size_t lens[] = {sizeof(elem)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 1, root);
+ build_tree(mock, elems, lens, 1, root);
uint8_t leaf_hash[32];
compute_leaf_hash(elem, sizeof(elem), leaf_hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_fail_second_call, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_fail_second_call, NULL);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_leaf_index(dc, 1, root, leaf_hash);
assert_int_equal(result, -4);
@@ -543,21 +491,23 @@ static void test_get_leaf_index_verify_comm_failure(void **state) {
/* ---------- Main ---------- */
int main(void) {
+#define T(fn) cmocka_unit_test_setup_teardown(fn, mock_dispatcher_setup, mock_dispatcher_teardown)
const struct CMUnitTest tests[] = {
- cmocka_unit_test(test_get_leaf_index_single),
- cmocka_unit_test(test_get_leaf_index_three_elements),
- cmocka_unit_test(test_get_leaf_index_four_elements),
- cmocka_unit_test(test_get_leaf_index_five_elements),
- cmocka_unit_test(test_get_leaf_index_unknown_hash),
- cmocka_unit_test(test_get_leaf_index_wrong_root),
- cmocka_unit_test(test_get_leaf_index_two_elements),
- cmocka_unit_test(test_get_leaf_index_eight_elements),
- cmocka_unit_test(test_get_leaf_index_wrong_index),
- cmocka_unit_test(test_get_leaf_index_oob_index),
- cmocka_unit_test(test_get_leaf_index_initial_comm_failure),
- cmocka_unit_test(test_get_leaf_index_invalid_found),
- cmocka_unit_test(test_get_leaf_index_verify_comm_failure),
+ T(test_get_leaf_index_single),
+ T(test_get_leaf_index_three_elements),
+ T(test_get_leaf_index_four_elements),
+ T(test_get_leaf_index_five_elements),
+ T(test_get_leaf_index_unknown_hash),
+ T(test_get_leaf_index_wrong_root),
+ T(test_get_leaf_index_two_elements),
+ T(test_get_leaf_index_eight_elements),
+ T(test_get_leaf_index_wrong_index),
+ T(test_get_leaf_index_oob_index),
+ T(test_get_leaf_index_initial_comm_failure),
+ T(test_get_leaf_index_invalid_found),
+ T(test_get_leaf_index_verify_comm_failure),
};
+#undef T
return cmocka_run_group_tests(tests, NULL, NULL);
}
diff --git a/unit-tests/test_get_merkle_preimage.c b/unit-tests/test_get_merkle_preimage.c
index 97e64de..574cf18 100644
--- a/unit-tests/test_get_merkle_preimage.c
+++ b/unit-tests/test_get_merkle_preimage.c
@@ -63,11 +63,7 @@ static void add_merkle_preimage(mock_dispatcher_t *mock,
* GET_MORE_ELEMENTS needed).
*/
static void test_get_merkle_preimage_small(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[50];
for (size_t i = 0; i < sizeof(element); i++) {
@@ -75,12 +71,12 @@ static void test_get_merkle_preimage_small(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
uint8_t out[256];
memset(out, 0xAA, sizeof(out));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_preimage(dc, hash, out, sizeof(out));
/* Returns element length (preimage_len - 1, stripping 0x00 prefix) */
@@ -93,11 +89,7 @@ static void test_get_merkle_preimage_small(void **state) {
* all the bytes.
*/
static void test_get_merkle_preimage_large(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
/* Element of 250 bytes; with the 0x00 prefix the preimage is 251 bytes,
* which might not fully fit in the first response chunk. */
@@ -107,12 +99,12 @@ static void test_get_merkle_preimage_large(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
uint8_t out[512];
memset(out, 0, sizeof(out));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, (int) sizeof(element));
@@ -123,17 +115,13 @@ static void test_get_merkle_preimage_large(void **state) {
* Error: requesting preimage of an unknown hash should return a negative value.
*/
static void test_get_merkle_preimage_unknown_hash(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ 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);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_preimage(dc, hash, out, sizeof(out));
/* process_interruption returns -1 → call_get_merkle_preimage returns -1 */
@@ -145,11 +133,7 @@ static void test_get_merkle_preimage_unknown_hash(void **state) {
* call_get_merkle_preimage should return -4.
*/
static void test_get_merkle_preimage_buffer_too_small(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[100];
for (size_t i = 0; i < sizeof(element); i++) {
@@ -157,11 +141,11 @@ static void test_get_merkle_preimage_buffer_too_small(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
uint8_t out[50]; /* Too small for 100-byte element */
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, -4);
@@ -172,21 +156,17 @@ static void test_get_merkle_preimage_buffer_too_small(void **state) {
* Preimage is (0x00 || 0x42) = 2 bytes, output should be just 0x42.
*/
static void test_get_merkle_preimage_one_byte(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[1] = {0x42};
uint8_t hash[32];
- add_merkle_preimage(&mock, element, 1, hash);
+ add_merkle_preimage(mock, element, 1, hash);
uint8_t out[64];
memset(out, 0, sizeof(out));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, 1);
@@ -202,11 +182,7 @@ static void test_get_merkle_preimage_one_byte(void **state) {
* So a preimage of 253 bytes fits in one chunk → element of 252 bytes.
*/
static void test_get_merkle_preimage_exact_fit(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[252];
for (size_t i = 0; i < sizeof(element); i++) {
@@ -214,12 +190,12 @@ static void test_get_merkle_preimage_exact_fit(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
uint8_t out[512];
memset(out, 0, sizeof(out));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, (int) sizeof(element));
@@ -235,11 +211,7 @@ static void test_get_merkle_preimage_exact_fit(void **state) {
* Element length = 253.
*/
static void test_get_merkle_preimage_one_byte_overflow(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[253];
for (size_t i = 0; i < sizeof(element); i++) {
@@ -247,12 +219,12 @@ static void test_get_merkle_preimage_one_byte_overflow(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
uint8_t out[512];
memset(out, 0, sizeof(out));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, (int) sizeof(element));
@@ -263,11 +235,7 @@ static void test_get_merkle_preimage_one_byte_overflow(void **state) {
* Edge case: output buffer exactly matches element length (no spare room).
*/
static void test_get_merkle_preimage_exact_buffer(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[64];
for (size_t i = 0; i < sizeof(element); i++) {
@@ -275,12 +243,12 @@ static void test_get_merkle_preimage_exact_buffer(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
uint8_t out[64]; /* Exactly the element size */
memset(out, 0, sizeof(out));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, (int) sizeof(element));
@@ -310,11 +278,7 @@ static int tamper_corrupt_data(uint8_t *response_buf,
}
static void test_get_merkle_preimage_corrupted_data(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[50];
for (size_t i = 0; i < sizeof(element); i++) {
@@ -322,12 +286,12 @@ static void test_get_merkle_preimage_corrupted_data(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_corrupt_data, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_corrupt_data, NULL);
uint8_t out[256];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_preimage(dc, hash, out, sizeof(out));
/* Must detect hash mismatch */
@@ -352,11 +316,7 @@ static int tamper_corrupt_continuation(uint8_t *response_buf,
}
static void test_get_merkle_preimage_corrupted_continuation(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
/* Element large enough to require continuation (preimage = 0x00 || element) */
uint8_t element[300];
@@ -365,12 +325,12 @@ static void test_get_merkle_preimage_corrupted_continuation(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_corrupt_continuation, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_corrupt_continuation, NULL);
uint8_t out[512];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_preimage(dc, hash, out, sizeof(out));
/* Must detect the corruption (hash mismatch or protocol error) */
@@ -396,11 +356,7 @@ static int tamper_truncate(uint8_t *response_buf,
}
static void test_get_merkle_preimage_truncated(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[10];
for (size_t i = 0; i < sizeof(element); i++) {
@@ -408,12 +364,12 @@ static void test_get_merkle_preimage_truncated(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_truncate, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_truncate, NULL);
uint8_t out[64];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, -2);
@@ -439,20 +395,16 @@ static int tamper_zero_len(uint8_t *response_buf,
}
static void test_get_merkle_preimage_zero_len(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[5] = {1, 2, 3, 4, 5};
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_zero_len, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_zero_len, NULL);
uint8_t out[64];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, -3);
@@ -480,20 +432,16 @@ static int tamper_partial_len_over(uint8_t *response_buf,
}
static void test_get_merkle_preimage_partial_len_over(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[5] = {1, 2, 3, 4, 5};
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_partial_len_over, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_partial_len_over, NULL);
uint8_t out[64];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, -5);
@@ -520,11 +468,7 @@ static int tamper_fail_second(uint8_t *response_buf,
}
static void test_get_merkle_preimage_comm_failure(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[300];
for (size_t i = 0; i < sizeof(element); i++) {
@@ -532,12 +476,12 @@ static void test_get_merkle_preimage_comm_failure(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_fail_second, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_fail_second, NULL);
uint8_t out[512];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, -6);
@@ -562,11 +506,7 @@ static int tamper_truncate_more(uint8_t *response_buf,
}
static void test_get_merkle_preimage_truncated_more(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[300];
for (size_t i = 0; i < sizeof(element); i++) {
@@ -574,12 +514,12 @@ static void test_get_merkle_preimage_truncated_more(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_truncate_more, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_truncate_more, NULL);
uint8_t out[512];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, -7);
@@ -604,11 +544,7 @@ static int tamper_more_bad_size(uint8_t *response_buf,
}
static void test_get_merkle_preimage_bad_element_size(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[300];
for (size_t i = 0; i < sizeof(element); i++) {
@@ -616,12 +552,12 @@ static void test_get_merkle_preimage_bad_element_size(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_more_bad_size, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_more_bad_size, NULL);
uint8_t out[512];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, -8);
@@ -649,11 +585,7 @@ static int tamper_more_bytes(uint8_t *response_buf,
}
static void test_get_merkle_preimage_more_bytes(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
/* element 253 → preimage 254 → spill 3 bytes */
uint8_t element[253];
@@ -662,12 +594,12 @@ static void test_get_merkle_preimage_more_bytes(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_more_bytes, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_more_bytes, NULL);
uint8_t out[512];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_merkle_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, -9);
@@ -676,25 +608,27 @@ static void test_get_merkle_preimage_more_bytes(void **state) {
/* ---------- Main ---------- */
int main(void) {
+#define T(fn) cmocka_unit_test_setup_teardown(fn, mock_dispatcher_setup, mock_dispatcher_teardown)
const struct CMUnitTest tests[] = {
- cmocka_unit_test(test_get_merkle_preimage_small),
- cmocka_unit_test(test_get_merkle_preimage_large),
- cmocka_unit_test(test_get_merkle_preimage_unknown_hash),
- cmocka_unit_test(test_get_merkle_preimage_buffer_too_small),
- cmocka_unit_test(test_get_merkle_preimage_one_byte),
- cmocka_unit_test(test_get_merkle_preimage_exact_fit),
- cmocka_unit_test(test_get_merkle_preimage_one_byte_overflow),
- cmocka_unit_test(test_get_merkle_preimage_exact_buffer),
- cmocka_unit_test(test_get_merkle_preimage_corrupted_data),
- cmocka_unit_test(test_get_merkle_preimage_corrupted_continuation),
- cmocka_unit_test(test_get_merkle_preimage_truncated),
- cmocka_unit_test(test_get_merkle_preimage_zero_len),
- cmocka_unit_test(test_get_merkle_preimage_partial_len_over),
- cmocka_unit_test(test_get_merkle_preimage_comm_failure),
- cmocka_unit_test(test_get_merkle_preimage_truncated_more),
- cmocka_unit_test(test_get_merkle_preimage_bad_element_size),
- cmocka_unit_test(test_get_merkle_preimage_more_bytes),
+ T(test_get_merkle_preimage_small),
+ T(test_get_merkle_preimage_large),
+ T(test_get_merkle_preimage_unknown_hash),
+ T(test_get_merkle_preimage_buffer_too_small),
+ T(test_get_merkle_preimage_one_byte),
+ T(test_get_merkle_preimage_exact_fit),
+ T(test_get_merkle_preimage_one_byte_overflow),
+ T(test_get_merkle_preimage_exact_buffer),
+ T(test_get_merkle_preimage_corrupted_data),
+ T(test_get_merkle_preimage_corrupted_continuation),
+ T(test_get_merkle_preimage_truncated),
+ T(test_get_merkle_preimage_zero_len),
+ T(test_get_merkle_preimage_partial_len_over),
+ T(test_get_merkle_preimage_comm_failure),
+ T(test_get_merkle_preimage_truncated_more),
+ T(test_get_merkle_preimage_bad_element_size),
+ T(test_get_merkle_preimage_more_bytes),
};
+#undef T
return cmocka_run_group_tests(tests, NULL, NULL);
}
diff --git a/unit-tests/test_get_preimage.c b/unit-tests/test_get_preimage.c
index de3e1fe..b6c7906 100644
--- a/unit-tests/test_get_preimage.c
+++ b/unit-tests/test_get_preimage.c
@@ -43,11 +43,7 @@ static void compute_sha256(const uint8_t *data, size_t len, uint8_t out[32]) {
* GET_MORE_ELEMENTS needed).
*/
static void test_get_preimage_small(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
/* A small preimage: 50 bytes */
uint8_t preimage[50];
@@ -55,7 +51,7 @@ static void test_get_preimage_small(void **state) {
preimage[i] = (uint8_t) (i & 0xFF);
}
- mock_dispatcher_add_preimage(&mock, preimage, sizeof(preimage));
+ mock_dispatcher_add_preimage(mock, preimage, sizeof(preimage));
uint8_t hash[32];
compute_sha256(preimage, sizeof(preimage), hash);
@@ -63,7 +59,7 @@ static void test_get_preimage_small(void **state) {
uint8_t out[256];
memset(out, 0xAA, sizeof(out));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ 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));
@@ -75,11 +71,7 @@ static void test_get_preimage_small(void **state) {
* all the bytes.
*/
static void test_get_preimage_large(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
/* A larger preimage: 300 bytes */
uint8_t preimage[300];
@@ -87,7 +79,7 @@ static void test_get_preimage_large(void **state) {
preimage[i] = (uint8_t) ((i * 7 + 13) & 0xFF);
}
- mock_dispatcher_add_preimage(&mock, preimage, sizeof(preimage));
+ mock_dispatcher_add_preimage(mock, preimage, sizeof(preimage));
uint8_t hash[32];
compute_sha256(preimage, sizeof(preimage), hash);
@@ -95,7 +87,7 @@ static void test_get_preimage_large(void **state) {
uint8_t out[512];
memset(out, 0, sizeof(out));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ 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));
@@ -106,17 +98,13 @@ static void test_get_preimage_large(void **state) {
* Error: requesting preimage of an unknown hash should return a negative value.
*/
static void test_get_preimage_unknown_hash(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ 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);
+ 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 */
@@ -128,25 +116,21 @@ static void test_get_preimage_unknown_hash(void **state) {
* call_get_preimage should return -10.
*/
static void test_get_preimage_buffer_too_small(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ 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));
+ 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);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, -10);
@@ -156,14 +140,10 @@ static void test_get_preimage_buffer_too_small(void **state) {
* Edge case: minimal preimage of exactly 1 byte.
*/
static void test_get_preimage_one_byte(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t preimage[1] = {0x42};
- mock_dispatcher_add_preimage(&mock, preimage, 1);
+ mock_dispatcher_add_preimage(mock, preimage, 1);
uint8_t hash[32];
compute_sha256(preimage, 1, hash);
@@ -171,7 +151,7 @@ static void test_get_preimage_one_byte(void **state) {
uint8_t out[64];
memset(out, 0, sizeof(out));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, 1);
@@ -188,18 +168,14 @@ static void test_get_preimage_one_byte(void **state) {
* So a 253-byte preimage should fit exactly with no GET_MORE_ELEMENTS.
*/
static void test_get_preimage_exact_fit(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ 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));
+ mock_dispatcher_add_preimage(mock, preimage, sizeof(preimage));
uint8_t hash[32];
compute_sha256(preimage, sizeof(preimage), hash);
@@ -207,7 +183,7 @@ static void test_get_preimage_exact_fit(void **state) {
uint8_t out[512];
memset(out, 0, sizeof(out));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ 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));
@@ -219,11 +195,7 @@ static void test_get_preimage_exact_fit(void **state) {
* so a few bytes go through GET_MORE_ELEMENTS).
*/
static void test_get_preimage_one_byte_overflow(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
/* Varint encodings above 253 bytes (and less than 65536) take 3 bytes,
* therefore max_payload = 255 - 3 - 1 = 251.
@@ -234,7 +206,7 @@ static void test_get_preimage_one_byte_overflow(void **state) {
preimage[i] = (uint8_t) (i * 3);
}
- mock_dispatcher_add_preimage(&mock, preimage, sizeof(preimage));
+ mock_dispatcher_add_preimage(mock, preimage, sizeof(preimage));
uint8_t hash[32];
compute_sha256(preimage, sizeof(preimage), hash);
@@ -242,7 +214,7 @@ static void test_get_preimage_one_byte_overflow(void **state) {
uint8_t out[512];
memset(out, 0, sizeof(out));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ 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));
@@ -273,26 +245,22 @@ static int tamper_corrupt_preimage_data(uint8_t *response_buf,
}
static void test_get_preimage_corrupted_data(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ 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));
+ 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);
+ mock_dispatcher_set_tamper_hook(mock, tamper_corrupt_preimage_data, NULL);
uint8_t out[256];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
/* Must detect the hash mismatch */
@@ -320,26 +288,22 @@ static int tamper_partial_len_overflow(uint8_t *response_buf,
}
static void test_get_preimage_partial_len_overflow(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ 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));
+ 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);
+ mock_dispatcher_set_tamper_hook(mock, tamper_partial_len_overflow, NULL);
uint8_t out[256];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ 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 */
@@ -367,22 +331,18 @@ static int tamper_zero_preimage_len(uint8_t *response_buf,
}
static void test_get_preimage_zero_len(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ 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));
+ 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);
+ mock_dispatcher_set_tamper_hook(mock, tamper_zero_preimage_len, NULL);
uint8_t out[256];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
assert_true(result < 0);
@@ -406,26 +366,22 @@ static int tamper_more_elements_bad_size(uint8_t *response_buf,
}
static void test_get_preimage_bad_element_size(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ 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));
+ 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);
+ 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);
+ 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 */
@@ -450,11 +406,7 @@ static int tamper_more_bytes_than_remaining(uint8_t *response_buf,
}
static void test_get_preimage_more_bytes_than_remaining(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
/* 254 bytes: varint=3 bytes, max_payload = 255-3-1 = 251, spill = 3 bytes */
uint8_t preimage[254];
@@ -462,15 +414,15 @@ static void test_get_preimage_more_bytes_than_remaining(void **state) {
preimage[i] = (uint8_t) (i * 7);
}
- mock_dispatcher_add_preimage(&mock, preimage, sizeof(preimage));
+ 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);
+ 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);
+ 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 */
@@ -496,26 +448,22 @@ static int tamper_corrupt_continuation(uint8_t *response_buf,
}
static void test_get_preimage_corrupted_continuation(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ 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));
+ 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);
+ mock_dispatcher_set_tamper_hook(mock, tamper_corrupt_continuation, NULL);
uint8_t out[512];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
assert_true(result < 0);
@@ -552,22 +500,18 @@ static int tamper_preimage_len_too_big(uint8_t *response_buf,
}
static void test_get_preimage_overflow_len(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ 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));
+ 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);
+ 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);
+ 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 */
@@ -599,22 +543,18 @@ static int tamper_partial_len_strictly_over(uint8_t *response_buf,
}
static void test_get_preimage_partial_len_strictly_over(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t preimage[5] = {0x10, 0x20, 0x30, 0x40, 0x50};
- mock_dispatcher_add_preimage(&mock, preimage, sizeof(preimage));
+ 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);
+ 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);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, -4);
@@ -641,26 +581,22 @@ static int tamper_more_elements_bad_size_strict(uint8_t *response_buf,
}
static void test_get_preimage_bad_element_size_strict(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ 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));
+ 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);
+ 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);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, -7);
@@ -690,26 +626,22 @@ static int tamper_more_bytes_with_padding(uint8_t *response_buf,
}
static void test_get_preimage_more_bytes_strict(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ 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));
+ 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);
+ 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);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
assert_int_equal(result, -8);
@@ -736,26 +668,22 @@ static int tamper_fail_second_call(uint8_t *response_buf,
}
static void test_get_preimage_communication_failure(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ 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));
+ 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);
+ mock_dispatcher_set_tamper_hook(mock, tamper_fail_second_call, NULL);
uint8_t out[512];
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_get_preimage(dc, hash, out, sizeof(out));
assert_true(result < 0);
@@ -764,26 +692,28 @@ static void test_get_preimage_communication_failure(void **state) {
/* ---------- Main ---------- */
int main(void) {
+#define T(fn) cmocka_unit_test_setup_teardown(fn, mock_dispatcher_setup, mock_dispatcher_teardown)
const struct CMUnitTest tests[] = {
- cmocka_unit_test(test_get_preimage_small),
- cmocka_unit_test(test_get_preimage_large),
- cmocka_unit_test(test_get_preimage_unknown_hash),
- cmocka_unit_test(test_get_preimage_buffer_too_small),
- cmocka_unit_test(test_get_preimage_one_byte),
- cmocka_unit_test(test_get_preimage_exact_fit),
- cmocka_unit_test(test_get_preimage_one_byte_overflow),
- cmocka_unit_test(test_get_preimage_corrupted_data),
- cmocka_unit_test(test_get_preimage_partial_len_overflow),
- cmocka_unit_test(test_get_preimage_zero_len),
- cmocka_unit_test(test_get_preimage_bad_element_size),
- cmocka_unit_test(test_get_preimage_more_bytes_than_remaining),
- cmocka_unit_test(test_get_preimage_corrupted_continuation),
- cmocka_unit_test(test_get_preimage_overflow_len),
- cmocka_unit_test(test_get_preimage_partial_len_strictly_over),
- cmocka_unit_test(test_get_preimage_bad_element_size_strict),
- cmocka_unit_test(test_get_preimage_more_bytes_strict),
- cmocka_unit_test(test_get_preimage_communication_failure),
+ 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);
}
diff --git a/unit-tests/test_stream_merkle_leaf_element.c b/unit-tests/test_stream_merkle_leaf_element.c
index e7b987a..7e55dd0 100644
--- a/unit-tests/test_stream_merkle_leaf_element.c
+++ b/unit-tests/test_stream_merkle_leaf_element.c
@@ -70,23 +70,19 @@ static void acc_data_callback(buffer_t *data, void *state) {
* Happy path: single element tree, retrieve the only leaf.
*/
static void test_stream_leaf_element_single(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t elem[] = {0xCA, 0xFE};
const uint8_t *elems[] = {elem};
size_t lens[] = {sizeof(elem)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 1, root);
+ build_tree(mock, elems, lens, 1, root);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result =
call_stream_merkle_leaf_element(dc, root, 1, 0, acc_len_callback, acc_data_callback, &acc);
@@ -101,11 +97,7 @@ static void test_stream_leaf_element_single(void **state) {
* Happy path: three-element tree, retrieve each leaf by index.
*/
static void test_stream_leaf_element_three_elements(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t *elems[] = {(const uint8_t *) "alpha",
(const uint8_t *) "beta",
@@ -113,9 +105,9 @@ static void test_stream_leaf_element_three_elements(void **state) {
size_t lens[] = {5, 4, 5};
uint8_t root[32];
- build_tree(&mock, elems, lens, 3, root);
+ build_tree(mock, elems, lens, 3, root);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
for (size_t i = 0; i < 3; i++) {
stream_accumulator_t acc;
@@ -141,11 +133,7 @@ static void test_stream_leaf_element_three_elements(void **state) {
* Happy path: power-of-two number of elements (4 elements, balanced tree).
*/
static void test_stream_leaf_element_four_elements(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t e0[] = {0x00, 0x01, 0x02};
uint8_t e1[] = {0x10, 0x11};
@@ -156,9 +144,9 @@ static void test_stream_leaf_element_four_elements(void **state) {
size_t lens[] = {sizeof(e0), sizeof(e1), sizeof(e2), sizeof(e3)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 4, root);
+ build_tree(mock, elems, lens, 4, root);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
for (size_t i = 0; i < 4; i++) {
stream_accumulator_t acc;
@@ -184,23 +172,19 @@ static void test_stream_leaf_element_four_elements(void **state) {
* Edge case: leaf element of exactly 1 byte.
*/
static void test_stream_leaf_element_one_byte(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t elem[] = {0x42};
const uint8_t *elems[] = {elem};
size_t lens[] = {1};
uint8_t root[32];
- build_tree(&mock, elems, lens, 1, root);
+ build_tree(mock, elems, lens, 1, root);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result =
call_stream_merkle_leaf_element(dc, root, 1, 0, acc_len_callback, acc_data_callback, &acc);
@@ -215,18 +199,14 @@ static void test_stream_leaf_element_one_byte(void **state) {
* Error: wrong Merkle root (no matching tree).
*/
static void test_stream_leaf_element_wrong_root(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t elem[] = {0xAB, 0xCD};
const uint8_t *elems[] = {elem};
size_t lens[] = {sizeof(elem)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 1, root);
+ build_tree(mock, elems, lens, 1, root);
/* Corrupt the root */
uint8_t bad_root[32];
@@ -235,7 +215,7 @@ static void test_stream_leaf_element_wrong_root(void **state) {
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_merkle_leaf_element(dc,
bad_root,
1,
@@ -251,22 +231,18 @@ static void test_stream_leaf_element_wrong_root(void **state) {
* Error: leaf index out of bounds (>= tree_size).
*/
static void test_stream_leaf_element_index_out_of_bounds(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t *elems[] = {(const uint8_t *) "x", (const uint8_t *) "y"};
size_t lens[] = {1, 1};
uint8_t root[32];
- build_tree(&mock, elems, lens, 2, root);
+ build_tree(mock, elems, lens, 2, root);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_merkle_leaf_element(dc,
root,
2,
@@ -282,11 +258,7 @@ static void test_stream_leaf_element_index_out_of_bounds(void **state) {
* Happy path: larger tree (8 elements) to exercise deeper proof paths.
*/
static void test_stream_leaf_element_eight_elements(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t data[8][16];
const uint8_t *elems[8];
@@ -301,9 +273,9 @@ static void test_stream_leaf_element_eight_elements(void **state) {
}
uint8_t root[32];
- build_tree(&mock, elems, lens, 8, root);
+ build_tree(mock, elems, lens, 8, root);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
for (size_t i = 0; i < 8; i++) {
stream_accumulator_t acc;
@@ -329,23 +301,19 @@ static void test_stream_leaf_element_eight_elements(void **state) {
* Happy path: NULL len_callback should work (len_callback is optional).
*/
static void test_stream_leaf_element_null_len_callback(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t elem[] = {0xDE, 0xAD, 0xBE, 0xEF};
const uint8_t *elems[] = {elem};
size_t lens[] = {sizeof(elem)};
uint8_t root[32];
- build_tree(&mock, elems, lens, 1, root);
+ build_tree(mock, elems, lens, 1, root);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result =
call_stream_merkle_leaf_element(dc, root, 1, 0, NULL, acc_data_callback, &acc);
@@ -358,16 +326,18 @@ static void test_stream_leaf_element_null_len_callback(void **state) {
/* ---------- Main ---------- */
int main(void) {
+#define T(fn) cmocka_unit_test_setup_teardown(fn, mock_dispatcher_setup, mock_dispatcher_teardown)
const struct CMUnitTest tests[] = {
- cmocka_unit_test(test_stream_leaf_element_single),
- cmocka_unit_test(test_stream_leaf_element_three_elements),
- cmocka_unit_test(test_stream_leaf_element_four_elements),
- cmocka_unit_test(test_stream_leaf_element_one_byte),
- cmocka_unit_test(test_stream_leaf_element_wrong_root),
- cmocka_unit_test(test_stream_leaf_element_index_out_of_bounds),
- cmocka_unit_test(test_stream_leaf_element_eight_elements),
- cmocka_unit_test(test_stream_leaf_element_null_len_callback),
+ T(test_stream_leaf_element_single),
+ T(test_stream_leaf_element_three_elements),
+ T(test_stream_leaf_element_four_elements),
+ T(test_stream_leaf_element_one_byte),
+ T(test_stream_leaf_element_wrong_root),
+ T(test_stream_leaf_element_index_out_of_bounds),
+ T(test_stream_leaf_element_eight_elements),
+ T(test_stream_leaf_element_null_len_callback),
};
+#undef T
return cmocka_run_group_tests(tests, NULL, NULL);
}
diff --git a/unit-tests/test_stream_merkleized_map_value.c b/unit-tests/test_stream_merkleized_map_value.c
index 2e73603..18982be 100644
--- a/unit-tests/test_stream_merkleized_map_value.c
+++ b/unit-tests/test_stream_merkleized_map_value.c
@@ -58,11 +58,7 @@ static void acc_data_callback(buffer_t *data, void *state) {
* Happy path: single key-value pair, look up by key and stream the value.
*/
static void test_stream_map_value_single(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t key[] = {0x01, 0x02};
const uint8_t value[] = {0xAA, 0xBB, 0xCC};
@@ -73,12 +69,12 @@ static void test_stream_map_value_single(void **state) {
const size_t value_lens[] = {sizeof(value)};
merkleized_map_commitment_t commitment;
- mock_dispatcher_add_map(&mock, keys, key_lens, values, value_lens, 1, &commitment);
+ mock_dispatcher_add_map(mock, keys, key_lens, values, value_lens, 1, &commitment);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_merkleized_map_value(dc,
&commitment,
key,
@@ -99,11 +95,7 @@ static void test_stream_map_value_single(void **state) {
* Keys must be in sorted order for the Merkle map to work.
*/
static void test_stream_map_value_three_pairs(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
/* Keys are sorted lexicographically by mock_dispatcher_add_map */
const uint8_t k0[] = {0x01};
@@ -119,9 +111,9 @@ static void test_stream_map_value_three_pairs(void **state) {
const size_t value_lens[] = {sizeof(v0), sizeof(v1), sizeof(v2)};
merkleized_map_commitment_t commitment;
- mock_dispatcher_add_map(&mock, keys, key_lens, values, value_lens, 3, &commitment);
+ mock_dispatcher_add_map(mock, keys, key_lens, values, value_lens, 3, &commitment);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
/* Look up each key-value pair */
const uint8_t *test_keys[] = {k0, k1, k2};
@@ -153,11 +145,7 @@ static void test_stream_map_value_three_pairs(void **state) {
* Error: key not found in the map.
*/
static void test_stream_map_value_key_not_found(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t key[] = {0x01};
const uint8_t value[] = {0xAA};
@@ -168,7 +156,7 @@ static void test_stream_map_value_key_not_found(void **state) {
const size_t value_lens[] = {sizeof(value)};
merkleized_map_commitment_t commitment;
- mock_dispatcher_add_map(&mock, keys, key_lens, values, value_lens, 1, &commitment);
+ mock_dispatcher_add_map(mock, keys, key_lens, values, value_lens, 1, &commitment);
/* Look up a key that doesn't exist */
const uint8_t missing_key[] = {0xFF};
@@ -176,7 +164,7 @@ static void test_stream_map_value_key_not_found(void **state) {
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_merkleized_map_value(dc,
&commitment,
missing_key,
@@ -192,11 +180,7 @@ static void test_stream_map_value_key_not_found(void **state) {
* Edge case: value of exactly 1 byte.
*/
static void test_stream_map_value_one_byte_value(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t key[] = {0x42};
const uint8_t value[] = {0x99};
@@ -207,12 +191,12 @@ static void test_stream_map_value_one_byte_value(void **state) {
const size_t value_lens[] = {sizeof(value)};
merkleized_map_commitment_t commitment;
- mock_dispatcher_add_map(&mock, keys, key_lens, values, value_lens, 1, &commitment);
+ mock_dispatcher_add_map(mock, keys, key_lens, values, value_lens, 1, &commitment);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_merkleized_map_value(dc,
&commitment,
key,
@@ -232,11 +216,7 @@ static void test_stream_map_value_one_byte_value(void **state) {
* Happy path: NULL len_callback should work (len_callback is optional).
*/
static void test_stream_map_value_null_len_callback(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
const uint8_t key[] = {0x05};
const uint8_t value[] = {0x10, 0x20, 0x30};
@@ -247,12 +227,12 @@ static void test_stream_map_value_null_len_callback(void **state) {
const size_t value_lens[] = {sizeof(value)};
merkleized_map_commitment_t commitment;
- mock_dispatcher_add_map(&mock, keys, key_lens, values, value_lens, 1, &commitment);
+ mock_dispatcher_add_map(mock, keys, key_lens, values, value_lens, 1, &commitment);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_merkleized_map_value(dc,
&commitment,
key,
@@ -271,11 +251,7 @@ static void test_stream_map_value_null_len_callback(void **state) {
* Happy path: keys provided in unsorted order (mock_dispatcher_add_map sorts them).
*/
static void test_stream_map_value_unsorted_keys(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
/* Provide keys in reverse order; add_map will sort them */
const uint8_t k0[] = {0x03};
@@ -291,9 +267,9 @@ static void test_stream_map_value_unsorted_keys(void **state) {
const size_t value_lens[] = {sizeof(v0), sizeof(v1), sizeof(v2)};
merkleized_map_commitment_t commitment;
- mock_dispatcher_add_map(&mock, keys, key_lens, values, value_lens, 3, &commitment);
+ mock_dispatcher_add_map(mock, keys, key_lens, values, value_lens, 3, &commitment);
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
/* Look up each original key and verify it retrieves the correct value */
for (size_t i = 0; i < 3; i++) {
@@ -319,14 +295,16 @@ static void test_stream_map_value_unsorted_keys(void **state) {
/* ---------- Main ---------- */
int main(void) {
+#define T(fn) cmocka_unit_test_setup_teardown(fn, mock_dispatcher_setup, mock_dispatcher_teardown)
const struct CMUnitTest tests[] = {
- cmocka_unit_test(test_stream_map_value_single),
- cmocka_unit_test(test_stream_map_value_three_pairs),
- cmocka_unit_test(test_stream_map_value_key_not_found),
- cmocka_unit_test(test_stream_map_value_one_byte_value),
- cmocka_unit_test(test_stream_map_value_null_len_callback),
- cmocka_unit_test(test_stream_map_value_unsorted_keys),
+ T(test_stream_map_value_single),
+ T(test_stream_map_value_three_pairs),
+ T(test_stream_map_value_key_not_found),
+ T(test_stream_map_value_one_byte_value),
+ T(test_stream_map_value_null_len_callback),
+ T(test_stream_map_value_unsorted_keys),
};
+#undef T
return cmocka_run_group_tests(tests, NULL, NULL);
}
diff --git a/unit-tests/test_stream_preimage.c b/unit-tests/test_stream_preimage.c
index 8a429d3..45025b2 100644
--- a/unit-tests/test_stream_preimage.c
+++ b/unit-tests/test_stream_preimage.c
@@ -85,11 +85,7 @@ static void acc_data_callback(buffer_t *data, void *state) {
* GET_MORE_ELEMENTS needed).
*/
static void test_stream_preimage_small(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[50];
for (size_t i = 0; i < sizeof(element); i++) {
@@ -97,12 +93,12 @@ static void test_stream_preimage_small(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_preimage(dc, hash, acc_len_callback, acc_data_callback, &acc);
assert_int_equal(result, (int) sizeof(element));
@@ -117,11 +113,7 @@ static void test_stream_preimage_small(void **state) {
* all the bytes.
*/
static void test_stream_preimage_large(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[300];
for (size_t i = 0; i < sizeof(element); i++) {
@@ -129,12 +121,12 @@ static void test_stream_preimage_large(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_preimage(dc, hash, acc_len_callback, acc_data_callback, &acc);
assert_int_equal(result, (int) sizeof(element));
@@ -148,18 +140,14 @@ static void test_stream_preimage_large(void **state) {
* Error: requesting preimage of an unknown hash should return a negative value.
*/
static void test_stream_preimage_unknown_hash(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t hash[32] = {0xDE, 0xAD, 0xBE, 0xEF};
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_preimage(dc, hash, acc_len_callback, acc_data_callback, &acc);
assert_true(result < 0);
@@ -170,21 +158,17 @@ static void test_stream_preimage_unknown_hash(void **state) {
* Preimage is (0x00 || 0x42) = 2 bytes, streamed output should be just 0x42.
*/
static void test_stream_preimage_one_byte(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[1] = {0x42};
uint8_t hash[32];
- add_merkle_preimage(&mock, element, 1, hash);
+ add_merkle_preimage(mock, element, 1, hash);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_preimage(dc, hash, acc_len_callback, acc_data_callback, &acc);
assert_int_equal(result, 1);
@@ -198,11 +182,7 @@ static void test_stream_preimage_one_byte(void **state) {
* Happy path: NULL len_callback should work (len_callback is optional).
*/
static void test_stream_preimage_null_len_callback(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[30];
for (size_t i = 0; i < sizeof(element); i++) {
@@ -210,12 +190,12 @@ static void test_stream_preimage_null_len_callback(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_preimage(dc, hash, NULL, acc_data_callback, &acc);
assert_int_equal(result, (int) sizeof(element));
@@ -233,11 +213,7 @@ static void test_stream_preimage_null_len_callback(void **state) {
* A preimage of 253 bytes means element of 252 bytes (253 - 1 for 0x00 prefix).
*/
static void test_stream_preimage_exact_fit(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[252];
for (size_t i = 0; i < sizeof(element); i++) {
@@ -245,12 +221,12 @@ static void test_stream_preimage_exact_fit(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_preimage(dc, hash, acc_len_callback, acc_data_callback, &acc);
assert_int_equal(result, (int) sizeof(element));
@@ -265,11 +241,7 @@ static void test_stream_preimage_exact_fit(void **state) {
* go through GET_MORE_ELEMENTS.
*/
static void test_stream_preimage_one_byte_overflow(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
/* Preimage length = 254 (element 253 + prefix 1).
* Varint for 254 takes 3 bytes, so max_payload = 255 - 3 - 1 = 251.
@@ -281,12 +253,12 @@ static void test_stream_preimage_one_byte_overflow(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_preimage(dc, hash, acc_len_callback, acc_data_callback, &acc);
assert_int_equal(result, (int) sizeof(element));
@@ -319,11 +291,7 @@ static int tamper_corrupt_stream_data(uint8_t *response_buf,
}
static void test_stream_preimage_corrupted_data(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[50];
for (size_t i = 0; i < sizeof(element); i++) {
@@ -331,14 +299,14 @@ static void test_stream_preimage_corrupted_data(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_corrupt_stream_data, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_corrupt_stream_data, NULL);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_preimage(dc, hash, acc_len_callback, acc_data_callback, &acc);
assert_true(result < 0);
@@ -362,11 +330,7 @@ static int tamper_corrupt_stream_continuation(uint8_t *response_buf,
}
static void test_stream_preimage_corrupted_continuation(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[300];
for (size_t i = 0; i < sizeof(element); i++) {
@@ -374,14 +338,14 @@ static void test_stream_preimage_corrupted_continuation(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_corrupt_stream_continuation, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_corrupt_stream_continuation, NULL);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_preimage(dc, hash, acc_len_callback, acc_data_callback, &acc);
assert_true(result < 0);
@@ -407,11 +371,7 @@ static int tamper_truncate_response(uint8_t *response_buf,
}
static void test_stream_preimage_truncated_response(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[20];
for (size_t i = 0; i < sizeof(element); i++) {
@@ -419,14 +379,14 @@ static void test_stream_preimage_truncated_response(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_truncate_response, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_truncate_response, NULL);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_preimage(dc, hash, acc_len_callback, acc_data_callback, &acc);
assert_int_equal(result, -2);
@@ -461,22 +421,18 @@ static int tamper_overflow_len(uint8_t *response_buf,
}
static void test_stream_preimage_overflow_len(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[5] = {1, 2, 3, 4, 5};
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_overflow_len, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_overflow_len, NULL);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_preimage(dc, hash, acc_len_callback, acc_data_callback, &acc);
assert_int_equal(result, -10);
@@ -502,22 +458,18 @@ static int tamper_zero_preimage_len(uint8_t *response_buf,
}
static void test_stream_preimage_zero_len(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[5] = {1, 2, 3, 4, 5};
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_zero_preimage_len, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_zero_preimage_len, NULL);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_preimage(dc, hash, acc_len_callback, acc_data_callback, &acc);
assert_int_equal(result, -3);
@@ -545,22 +497,18 @@ static int tamper_partial_len_over(uint8_t *response_buf,
}
static void test_stream_preimage_partial_len_over(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[5] = {1, 2, 3, 4, 5};
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_partial_len_over, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_partial_len_over, NULL);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_preimage(dc, hash, acc_len_callback, acc_data_callback, &acc);
assert_int_equal(result, -4);
@@ -586,11 +534,7 @@ static int tamper_truncate_more_elements(uint8_t *response_buf,
}
static void test_stream_preimage_truncated_more_elements(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[300];
for (size_t i = 0; i < sizeof(element); i++) {
@@ -598,14 +542,14 @@ static void test_stream_preimage_truncated_more_elements(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_truncate_more_elements, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_truncate_more_elements, NULL);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_preimage(dc, hash, acc_len_callback, acc_data_callback, &acc);
assert_int_equal(result, -6);
@@ -631,11 +575,7 @@ static int tamper_more_elements_bad_size(uint8_t *response_buf,
}
static void test_stream_preimage_bad_element_size(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[300];
for (size_t i = 0; i < sizeof(element); i++) {
@@ -643,14 +583,14 @@ static void test_stream_preimage_bad_element_size(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_more_elements_bad_size, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_more_elements_bad_size, NULL);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_preimage(dc, hash, acc_len_callback, acc_data_callback, &acc);
assert_int_equal(result, -7);
@@ -678,11 +618,7 @@ static int tamper_more_bytes_with_padding(uint8_t *response_buf,
}
static void test_stream_preimage_more_bytes_than_remaining(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[253]; /* spill = 3 bytes */
for (size_t i = 0; i < sizeof(element); i++) {
@@ -690,14 +626,14 @@ static void test_stream_preimage_more_bytes_than_remaining(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_more_bytes_with_padding, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_more_bytes_with_padding, NULL);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_preimage(dc, hash, acc_len_callback, acc_data_callback, &acc);
assert_int_equal(result, -8);
@@ -723,11 +659,7 @@ static int tamper_fail_on_second(uint8_t *response_buf,
}
static void test_stream_preimage_communication_failure(void **state) {
- (void) state;
-
- static mock_dispatcher_t mock;
- mock_dispatcher_init(&mock);
- mock_dispatcher_reset_hash_pool();
+ mock_dispatcher_t *mock = *state;
uint8_t element[300];
for (size_t i = 0; i < sizeof(element); i++) {
@@ -735,14 +667,14 @@ static void test_stream_preimage_communication_failure(void **state) {
}
uint8_t hash[32];
- add_merkle_preimage(&mock, element, sizeof(element), hash);
+ add_merkle_preimage(mock, element, sizeof(element), hash);
- mock_dispatcher_set_tamper_hook(&mock, tamper_fail_on_second, NULL);
+ mock_dispatcher_set_tamper_hook(mock, tamper_fail_on_second, NULL);
stream_accumulator_t acc;
memset(&acc, 0, sizeof(acc));
- dispatcher_context_t *dc = mock_dispatcher_get_dc(&mock);
+ dispatcher_context_t *dc = mock_dispatcher_get_dc(mock);
int result = call_stream_preimage(dc, hash, acc_len_callback, acc_data_callback, &acc);
assert_true(result < 0);
@@ -751,25 +683,27 @@ static void test_stream_preimage_communication_failure(void **state) {
/* ---------- Main ---------- */
int main(void) {
+#define T(fn) cmocka_unit_test_setup_teardown(fn, mock_dispatcher_setup, mock_dispatcher_teardown)
const struct CMUnitTest tests[] = {
- cmocka_unit_test(test_stream_preimage_small),
- cmocka_unit_test(test_stream_preimage_large),
- cmocka_unit_test(test_stream_preimage_unknown_hash),
- cmocka_unit_test(test_stream_preimage_one_byte),
- cmocka_unit_test(test_stream_preimage_null_len_callback),
- cmocka_unit_test(test_stream_preimage_exact_fit),
- cmocka_unit_test(test_stream_preimage_one_byte_overflow),
- cmocka_unit_test(test_stream_preimage_corrupted_data),
- cmocka_unit_test(test_stream_preimage_corrupted_continuation),
- cmocka_unit_test(test_stream_preimage_truncated_response),
- cmocka_unit_test(test_stream_preimage_overflow_len),
- cmocka_unit_test(test_stream_preimage_zero_len),
- cmocka_unit_test(test_stream_preimage_partial_len_over),
- cmocka_unit_test(test_stream_preimage_truncated_more_elements),
- cmocka_unit_test(test_stream_preimage_bad_element_size),
- cmocka_unit_test(test_stream_preimage_more_bytes_than_remaining),
- cmocka_unit_test(test_stream_preimage_communication_failure),
+ T(test_stream_preimage_small),
+ T(test_stream_preimage_large),
+ T(test_stream_preimage_unknown_hash),
+ T(test_stream_preimage_one_byte),
+ T(test_stream_preimage_null_len_callback),
+ T(test_stream_preimage_exact_fit),
+ T(test_stream_preimage_one_byte_overflow),
+ T(test_stream_preimage_corrupted_data),
+ T(test_stream_preimage_corrupted_continuation),
+ T(test_stream_preimage_truncated_response),
+ T(test_stream_preimage_overflow_len),
+ T(test_stream_preimage_zero_len),
+ T(test_stream_preimage_partial_len_over),
+ T(test_stream_preimage_truncated_more_elements),
+ T(test_stream_preimage_bad_element_size),
+ T(test_stream_preimage_more_bytes_than_remaining),
+ T(test_stream_preimage_communication_failure),
};
+#undef T
return cmocka_run_group_tests(tests, NULL, NULL);
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.