diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e8563d..cbd7767 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.13) # set the project name project(OlinCoin C CXX) enable_testing() -#set(CMAKE_BUILD_TYPE Debug) +set(CMAKE_BUILD_TYPE Debug) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_BINARY_DIR ${CMAKE_BINARY_DIR}) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) diff --git a/src/core/blocks/base_block.c b/src/core/blocks/base_block.c index 3a093b5..489d7f3 100644 --- a/src/core/blocks/base_block.c +++ b/src/core/blocks/base_block.c @@ -14,6 +14,20 @@ void hash_blockheader(unsigned char *dest, BlockHeader *header) { free(header_buf); } +void free_block(Block *block) { + if (block == NULL) + return; + + if (block->txs != NULL) { + for (unsigned int i = 0; i < block->num_txs; i++) + if (block->txs[i] != NULL) + free_tx(block->txs[i]); + free(block->txs); + } + + free(block); +} + void print_block_header(BlockHeader *header, char *prefix){ char *sub_prefix = malloc(strlen(prefix)+strlen(PRINT_TAB)+1); strcpy(sub_prefix, prefix); diff --git a/src/core/blocks/ser_block.c b/src/core/blocks/ser_block.c index acae83c..c06d960 100644 --- a/src/core/blocks/ser_block.c +++ b/src/core/blocks/ser_block.c @@ -15,13 +15,6 @@ return data; \ } -size_t size_ser_blockheader() { - return sizeof(((BlockHeader*)0)->timestamp) + - sizeof(((BlockHeader*)0)->all_tx) + - sizeof(((BlockHeader*)0)->prev_header_hash) + - sizeof(((BlockHeader*)0)->nonce); -} - ssize_t ser_blockheader(unsigned char *dest, BlockHeader *blockheader) { memcpy(dest, &(blockheader->timestamp), sizeof(blockheader->timestamp)); @@ -42,7 +35,7 @@ ssize_t ser_blockheader(unsigned char *dest, BlockHeader *blockheader) { unsigned char *ser_blockheader_alloc(ssize_t *written, BlockHeader *blockheader) { unsigned char *data; ssize_t ret; - data = malloc(size_ser_blockheader()); + data = malloc(BLOCKHEADER_SER_LEN); ret = ser_blockheader(data, blockheader); RETURN_SER(data, ret, written) } @@ -75,7 +68,7 @@ BlockHeader *deser_blockheader_alloc(ssize_t *read, unsigned char *src) { } size_t size_ser_block(Block *block){ - size_t size = sizeof(block->num_txs) + size_ser_blockheader(); + size_t size = sizeof(block->num_txs) + BLOCKHEADER_SER_LEN; for(unsigned int i = 0; i < block->num_txs; i++) size += size_ser_tx(block->txs[i]); return size; diff --git a/src/core/globals/CMakeLists.txt b/src/core/globals/CMakeLists.txt index ef6d508..4b1fdac 100644 --- a/src/core/globals/CMakeLists.txt +++ b/src/core/globals/CMakeLists.txt @@ -30,6 +30,7 @@ add_library(double_spend STATIC double_spend_set.c) target_include_directories(double_spend PUBLIC ${INCLUDE_ALL}) target_link_libraries(double_spend utxopool) + add_library(init_globals STATIC init_globals.c) target_include_directories(init_globals PUBLIC ${INCLUDE_ALL}) target_link_libraries(init_globals diff --git a/src/core/globals/blockchain.c b/src/core/globals/blockchain.c index 6dcc0be..9d3268e 100644 --- a/src/core/globals/blockchain.c +++ b/src/core/globals/blockchain.c @@ -53,7 +53,7 @@ int read_chain(){ free(path); if(!fp_chain){ return i; - } + } int ret_scanf = fscanf(fp_chain, "%d", &i); fclose(fp_chain); if(ret_scanf != 1){ @@ -114,7 +114,7 @@ int blockchain_init_leveldb(char *db_env){ // Check if we are using an existing DB if(found_genesis == 0){ free(test_genesis_block); - + if(read_hash == 0 && file_chain_height != -1){ Block *test_top_block; int found_top = blockchain_find_leveldb(&test_top_block, file_top_block_hash); @@ -132,14 +132,13 @@ int blockchain_init_leveldb(char *db_env){ } } } - + if(use_existing_db){ chain_height = file_chain_height; memcpy(top_block_header_hash, file_top_block_hash, BLOCK_HASH_LEN); } else{ - int ret = blockchain_add_leveldb(&genesis_block); if(ret != 0){ return 3; @@ -254,7 +253,7 @@ void print_blockchain_hashmap(char *prefix){ printf("%shashmap_id:\n", prefix); dump_buf(sub_prefix, "block_hash:", key_ptr, BLOCK_HASH_LEN); - + Block *read_block = deser_block_alloc(NULL, (unsigned char*)value_ptr); print_block(read_block, prefix); @@ -263,7 +262,7 @@ void print_blockchain_hashmap(char *prefix){ } leveldb_iter_destroy(iter); leveldb_readoptions_destroy(roptions); - leveldb_free(err); + leveldb_free(err); free(sub_prefix); } @@ -285,7 +284,7 @@ void pretty_print_blockchain_hashmap(){ unsigned const char *value_ptr = (unsigned const char*) leveldb_iter_value(iter, &value_len); dump_buf("", "Block_Hash:", key_ptr, BLOCK_HASH_LEN); - + Block *read_block = deser_block_alloc(NULL, (unsigned char*)value_ptr); pretty_print_block_header(&read_block->header, PRINT_TAB); @@ -295,5 +294,5 @@ void pretty_print_blockchain_hashmap(){ } leveldb_iter_destroy(iter); leveldb_readoptions_destroy(roptions); - leveldb_free(err); + leveldb_free(err); } diff --git a/src/core/globals/mempool.c b/src/core/globals/mempool.c index fb76c05..1b63d01 100644 --- a/src/core/globals/mempool.c +++ b/src/core/globals/mempool.c @@ -12,13 +12,13 @@ Transaction *mempool_add(Transaction *tx) { new_entry = malloc(sizeof(MemPool)); hash_tx(new_entry->id, tx); - new_entry->tx = tx; + new_entry->tx = copy_tx(tx); found_entry = mempool_find_node(new_entry->id); if (found_entry == NULL) { HASH_ADD(hh, mempool, id, TX_HASH_LEN, new_entry); utxo_to_tx_add_tx(tx); - return tx; + return new_entry->tx; } free(new_entry); return NULL; @@ -51,7 +51,7 @@ Transaction *mempool_find(unsigned char *tx_hash) { found_entry = mempool_find_node(tx_hash); if (found_entry != NULL) { - return found_entry->tx; + return copy_tx(found_entry->tx); } return NULL; diff --git a/src/core/txs/base_tx.c b/src/core/txs/base_tx.c index a71982b..e24bd79 100644 --- a/src/core/txs/base_tx.c +++ b/src/core/txs/base_tx.c @@ -14,6 +14,7 @@ void hash_tx(unsigned char *dest, Transaction *tx) { tx_buf = malloc(size_ser_tx(tx)); ser_tx(tx_buf, tx); hash_sha256(dest, tx_buf, tx_buf_size); + free(tx_buf); } void free_tx(Transaction *tx){ @@ -40,7 +41,6 @@ Transaction *copy_tx(Transaction *tx){ return NULL; // Copy inputs - // Harder since we need to also copy pub keys copy = malloc(sizeof(Transaction)); copy->num_inputs = tx->num_inputs; copy->inputs = malloc(tx->num_inputs * sizeof(Input)); diff --git a/src/core/txs/ser_tx.c b/src/core/txs/ser_tx.c index 3d43bf6..32f606c 100644 --- a/src/core/txs/ser_tx.c +++ b/src/core/txs/ser_tx.c @@ -65,7 +65,8 @@ ssize_t ser_input(unsigned char *dest, Input *input) { memcpy(sig_len, &(input->sig_len), sizeof(input->sig_len)); unsigned char *sig = sig_len + sizeof(input->sig_len); - memcpy(sig, input->signature, SIGNATURE_LEN); + memset(sig, 0, SIGNATURE_LEN); + memcpy(sig, input->signature, input->sig_len); unsigned char *prev_tx = sig + SIGNATURE_LEN; memcpy(prev_tx, input->prev_tx_id, TX_HASH_LEN); diff --git a/src/core/txs/wallet_pool.c b/src/core/txs/wallet_pool.c index 75c06c4..4d06fbd 100644 --- a/src/core/txs/wallet_pool.c +++ b/src/core/txs/wallet_pool.c @@ -117,6 +117,14 @@ int wallet_pool_count(unsigned int *num_entries){ return db_count(wallet_pool_db, num_entries); } +void free_wallet_entry(WalletEntry *entry) { + if (entry == NULL) + return; + if (entry->key_pair != NULL) + mbedtls_ecp_keypair_free(entry->key_pair); + free(entry); +} + /* KEY POOL FUNCS */ int key_pool_add_leveldb(mbedtls_ecp_keypair *key_pair) { unsigned char db_key[PUB_KEY_HASH_LEN]; @@ -161,7 +169,7 @@ int key_pool_find_leveldb(mbedtls_ecdsa_context **keypair, unsigned char *public if(read == NULL){ return 1; } - + size_t read_bytes; *keypair = deser_keypair_alloc(&read_bytes, (unsigned char*) read); free(read); diff --git a/src/includes/blocks/base_block.h b/src/includes/blocks/base_block.h index 2233563..8c32ab0 100644 --- a/src/includes/blocks/base_block.h +++ b/src/includes/blocks/base_block.h @@ -22,6 +22,13 @@ typedef struct Block{ */ void hash_blockheader(unsigned char *dest, BlockHeader *header); +/** + * @brief Frees a block + * + * @param block Block to free + */ +void free_block(Block *block); + /* Prints a Block Header to stdout so data can be visualized diff --git a/src/includes/blocks/ser_block.h b/src/includes/blocks/ser_block.h index e5f52c1..e652c42 100644 --- a/src/includes/blocks/ser_block.h +++ b/src/includes/blocks/ser_block.h @@ -6,12 +6,13 @@ * Blockheaders ******************************************************************************/ -/** - * @brief Gets size of serialized BlockHeader - * - * @return Size of serialized BlockHeader - */ -size_t size_ser_blockheader(); +#define BLOCKHEADER_SER_LEN \ + ( \ + sizeof(((BlockHeader*)0)->timestamp) + \ + sizeof(((BlockHeader*)0)->all_tx) + \ + sizeof(((BlockHeader*)0)->prev_header_hash) + \ + sizeof(((BlockHeader*)0)->nonce) \ + ) /** * @brief Serialize a BlockHeader diff --git a/src/includes/txs/wallet_pool.h b/src/includes/txs/wallet_pool.h index b93a8ea..604f4c9 100644 --- a/src/includes/txs/wallet_pool.h +++ b/src/includes/txs/wallet_pool.h @@ -91,6 +91,13 @@ int wallet_pool_remove_leveldb(unsigned char *tx_hash, unsigned int vout); */ int wallet_pool_count(unsigned int *num_entries); +/** + * @brief Frees a wallet entry + * + * @param entry Wallet entry to free + */ +void free_wallet_entry(WalletEntry *entry); + /** * @brief Add a keypair to the keypool * diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 314db67..b05ef70 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -1,5 +1,6 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/tests/") message(STATUS "Test Output set to: ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}") +add_subdirectory(includes) add_subdirectory(core) diff --git a/src/tests/core/CMakeLists.txt b/src/tests/core/CMakeLists.txt index 294369b..4bf30db 100644 --- a/src/tests/core/CMakeLists.txt +++ b/src/tests/core/CMakeLists.txt @@ -3,3 +3,4 @@ add_subdirectory(blocks) add_subdirectory(global) add_subdirectory(txs) add_subdirectory(utils) +add_subdirectory(fixtures) diff --git a/src/tests/core/blocks/CMakeLists.txt b/src/tests/core/blocks/CMakeLists.txt index fa6453f..6277fe2 100644 --- a/src/tests/core/blocks/CMakeLists.txt +++ b/src/tests/core/blocks/CMakeLists.txt @@ -22,9 +22,12 @@ target_link_libraries(test_handle_block # Test ser block add_executable(test_ser_block test_ser_block.c) target_compile_options(test_ser_block PRIVATE ${COMPILE_OPTIONS_STD}) -target_include_directories(test_ser_block PUBLIC ${INCLUDE_ALL}) +target_include_directories(test_ser_block + PUBLIC ${INCLUDE_ALL} + PUBLIC ${INCLUDE_FIXTURES}) target_link_libraries(test_ser_block - core_mine) + core_mine + fixtures) add_test(test_create_block ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_create_block) add_test(test_validate_block ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_validate_block) diff --git a/src/tests/core/blocks/test_ser_block.c b/src/tests/core/blocks/test_ser_block.c index 4b2d17f..ef3abbc 100644 --- a/src/tests/core/blocks/test_ser_block.c +++ b/src/tests/core/blocks/test_ser_block.c @@ -1,216 +1,178 @@ #include -#include "minunit.h" -#include "mempool.h" -#include "utxo_pool.h" -#include "create_block.h" -#include "time.h" -#include "ser_block.h" -#include "crypto.h" -#include "wallet_pool.h" +#include -int tests_run = 0; - -static void _set_header(BlockHeader *header) { - header->timestamp = 100; - header->nonce = 123; - memset(header->prev_header_hash, 0xFA, BLOCK_HASH_LEN); - memset(header->all_tx, 0xAF, ALL_TX_HASH_LEN); -} +#include +#include -static Transaction *_make_tx() { - Transaction *tx; - Input *in; - Output *out; - mbedtls_ecdsa_context *key_pair; +#include "ser_block.h" +#include "crypto.h" +#include "fixtures_block.h" - tx = malloc(sizeof(Transaction)); - in = malloc(sizeof(Input)); - out = malloc(sizeof(Output)); +static void test_ser_blockheader_equal(void **state){ + BlockHeader *header, *desered_header; + unsigned char *sered_header; + ssize_t written_ser_header, read_ser_header; - memset(in, 0, sizeof(Input)); - key_pair = gen_keys(); - in->pub_key = &(key_pair->private_Q); + header = *state; + sered_header = ser_blockheader_alloc(&written_ser_header, header); + desered_header = deser_blockheader_alloc(&read_ser_header, sered_header); - memset(out, 0, sizeof(Output)); + assert_int_equal(read_ser_header, BLOCKHEADER_SER_LEN); + assert_int_equal(read_ser_header, written_ser_header); - tx->num_inputs = 1; - tx->inputs = in; - tx->num_outputs = 1; - tx->outputs = out; + assert_int_equal(header->timestamp, desered_header->timestamp); + assert_int_equal(header->nonce, desered_header->nonce); + assert_memory_equal( + header->prev_header_hash, + desered_header->prev_header_hash, + BLOCK_HASH_LEN + ); + assert_memory_equal( + header->all_tx, + desered_header->all_tx, + ALL_TX_HASH_LEN + ); - return tx; + free(sered_header); + free(desered_header); } -static void _fill_mempool(){ - Transaction *input_tx, *tx1; - input_tx = _make_tx(); - input_tx->outputs[0].amt = 100; - utxo_pool_init_leveldb(TEST_DB_LOC); - - utxo_pool_add_leveldb(input_tx, 0); - - tx1 = _make_tx(); - hash_tx(tx1->inputs[0].prev_tx_id, input_tx); - tx1->inputs[0].prev_utxo_output = 0; - tx1->outputs[0].amt = 90; - mempool_init(); - mempool_add(tx1); -} +static void test_ser_blockheader_fill_buf(void **state) { + BlockHeader *header; + unsigned char *sered_header; + unsigned char sered_fill_header[BLOCKHEADER_SER_LEN]; -static char *test_ser_blockheader(){ - BlockHeader *header, *desered_header; - unsigned char *sered_header, *sered_header_2; - ssize_t written_ser_header, read_ser_header; + header = *state; + sered_header = ser_blockheader_alloc(NULL, header); - header = malloc(sizeof(BlockHeader)); - _set_header(header); - sered_header = ser_blockheader_alloc(&written_ser_header, header); - desered_header = deser_blockheader_alloc(&read_ser_header, sered_header); + // Ensure that we fill the entire buffer + for (int i = 0; i < 5; i++) { + memset(sered_fill_header, i, BLOCKHEADER_SER_LEN); + ser_blockheader(sered_fill_header, header); + assert_memory_equal(sered_header, sered_fill_header, BLOCKHEADER_SER_LEN); + } - mu_assert( - "Num of bytes read incorrect", - read_ser_header == (ssize_t)size_ser_blockheader() - ); - mu_assert( - "Num of bytes read and written don't match up", - read_ser_header == written_ser_header - ); + free(sered_header); +} - mu_assert( - "Timestamp isn't consistent after serialization", - header->timestamp == desered_header->timestamp - ); - mu_assert( - "Nonce isn't consistent after serialization", - header->nonce == desered_header->nonce - ); - mu_assert( - "Prev hash isn't consistent after serialization", - memcmp( +static void test_ser_blockheader_pad_bytes(void **state) { + BlockHeader *header, *pad_header; + unsigned char *sered_header; + unsigned char sered_pad_header[BLOCKHEADER_SER_LEN]; + + header = *state; + sered_header = ser_blockheader_alloc(NULL, header); + + // Ensure we don't serialize padding bytes + pad_header = malloc(sizeof(BlockHeader)); + for (int i = 0; i < 5; i++) { + memset(pad_header, i, sizeof(BlockHeader)); + pad_header->timestamp = header->timestamp; + pad_header->nonce = header->nonce; + memcpy( + pad_header->prev_header_hash, header->prev_header_hash, - desered_header->prev_header_hash, BLOCK_HASH_LEN - ) == 0 - ); - mu_assert( - "All tx hash isn't consistent after serialization", - memcmp( + ); + memcpy( + pad_header->all_tx, header->all_tx, - desered_header->all_tx, ALL_TX_HASH_LEN - ) == 0 - ); - - // Ensure that we don't have padding bytes - for (int i = 0; i < 10; i++) { - sered_header_2 = ser_blockheader_alloc(NULL, header); - mu_assert( - "Serialization of header isn't consistent", - memcmp(sered_header, sered_header_2, size_ser_blockheader()) == 0 ); - free(sered_header_2); + ser_blockheader(sered_pad_header, pad_header); + assert_memory_equal(sered_header, sered_pad_header, BLOCKHEADER_SER_LEN); } - free(header); + free(pad_header); free(sered_header); - free(desered_header); - - return NULL; } -static char *test_ser_block(){ +static void test_ser_block_equal(void **state) { Block *block, *desered_block; - unsigned char *sered_block, *sered_block_2; + unsigned char *sered_block; + unsigned char base_tx_hash[TX_HASH_LEN], deser_tx_hash[TX_HASH_LEN]; ssize_t written_ser_block, read_ser_block; - _fill_mempool(); - wallet_init_leveldb(TEST_DB_LOC); - block = create_block_alloc(); + block = *state; sered_block = ser_block_alloc(&written_ser_block, block); desered_block = deser_block_alloc(&read_ser_block, sered_block); - mu_assert( - "Num of bytes read incorrect", - read_ser_block == (ssize_t)size_ser_block(block) - ); - mu_assert( - "Num of bytes read and written don't match up", - read_ser_block == written_ser_block - ); + assert_int_equal(read_ser_block, (ssize_t)size_ser_block(block)); + assert_int_equal(read_ser_block, written_ser_block); - mu_assert( - "Num Txs don't match", - block->num_txs == desered_block->num_txs - ); - mu_assert( - "Header timestamp isn't consistent after serialization", - block->header.timestamp == desered_block->header.timestamp + assert_int_equal(block->num_txs, desered_block->num_txs); + assert_int_equal(block->header.timestamp, desered_block->header.timestamp); + assert_int_equal(block->header.nonce, desered_block->header.nonce); + assert_memory_equal( + block->header.prev_header_hash, + desered_block->header.prev_header_hash, + BLOCK_HASH_LEN ); - mu_assert( - "Header nonce isn't consistent after serialization", - block->header.nonce == desered_block->header.nonce - ); - mu_assert( - "Header prev hash isn't consistent after serialization", - memcmp( - block->header.prev_header_hash, - desered_block->header.prev_header_hash, - BLOCK_HASH_LEN - ) == 0 - ); - mu_assert( - "Header all tx hash isn't consistent after serialization", - memcmp( - block->header.all_tx, - desered_block->header.all_tx, - ALL_TX_HASH_LEN - ) == 0 + assert_memory_equal( + block->header.all_tx, + desered_block->header.all_tx, + ALL_TX_HASH_LEN ); - unsigned char base_tx_hash[TX_HASH_LEN]; - unsigned char deser_tx_hash[TX_HASH_LEN]; for(unsigned int i = 0; i < block->num_txs; i++){ hash_tx(base_tx_hash, block->txs[i]); hash_tx(deser_tx_hash, desered_block->txs[i]); - mu_assert( - "Transaction hashes don't match -> Transaction serialization error", - memcmp(base_tx_hash, deser_tx_hash, TX_HASH_LEN) == 0 - ); - } - - // Ensure that we don't have padding bytes - for (int i = 0; i < 10; i++) { - sered_block_2 = ser_block_alloc(NULL, block); - mu_assert( - "Serialization of block isn't consistent", - memcmp(sered_block, sered_block_2, size_ser_block(block)) == 0 - ); - free(sered_block_2); + assert_memory_equal(base_tx_hash, deser_tx_hash, TX_HASH_LEN); } - free(block); free(sered_block); free(desered_block); - destroy_db(&utxo_pool_db, utxo_pool_path); - destroy_wallet(); - return NULL; } -static char *all_tests() { - mu_run_test(test_ser_blockheader); - mu_run_test(test_ser_block); - return NULL; +static void test_ser_block_fill_buf(void **state) { + Block *block; + unsigned char *sered_block, *sered_fill_block; + size_t block_ser_size; + + block = *state; + sered_block = ser_block_alloc(NULL, block); + + // Ensure that we fill the entire buffer + block_ser_size = size_ser_block(block); + sered_fill_block = malloc(block_ser_size); + for (int i = 0; i < 5; i++) { + memset(sered_fill_block, i, block_ser_size); + ser_block(sered_fill_block, block); + assert_memory_equal(sered_block, sered_fill_block, block_ser_size); + } + + free(sered_block); + free(sered_fill_block); } int main() { - create_proj_folders(); - char *result = all_tests(); - if (result != NULL) { - printf("%s\n", result); - } else { - printf("ser_block.c passing!\n"); - } - printf("Tests run: %d\n", tests_run); - - return result != 0; + const struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown( + test_ser_blockheader_equal, + fixture_setup_unlinked_header, + fixture_teardown_unlinked_header + ), + cmocka_unit_test_setup_teardown( + test_ser_blockheader_fill_buf, + fixture_setup_unlinked_header, + fixture_teardown_unlinked_header + ), + cmocka_unit_test_setup_teardown( + test_ser_blockheader_pad_bytes, + fixture_setup_unlinked_header, + fixture_teardown_unlinked_header + ), + cmocka_unit_test_setup_teardown( + test_ser_block_equal, + fixture_setup_unlinked_block, + fixture_teardown_unlinked_block + ), + cmocka_unit_test_setup_teardown( + test_ser_block_fill_buf, + fixture_setup_unlinked_block, + fixture_teardown_unlinked_block + ) + }; + + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/src/tests/core/fixtures/CMakeLists.txt b/src/tests/core/fixtures/CMakeLists.txt new file mode 100644 index 0000000..db95b3a --- /dev/null +++ b/src/tests/core/fixtures/CMakeLists.txt @@ -0,0 +1,17 @@ +add_library(fixtures STATIC + fixtures_tx.c + fixtures_wallet.c + fixtures_block.c + fixtures_global.c) +target_include_directories(fixtures + PUBLIC ${INCLUDE_ALL} + PUBLIC ${INCLUDE_FIXTURES}) +target_link_libraries(fixtures + core_tx + core_block + wallet_pool + blockchain + mempool + utxo_to_tx + crypto + cmocka-static) diff --git a/src/tests/core/fixtures/fixtures_block.c b/src/tests/core/fixtures/fixtures_block.c new file mode 100644 index 0000000..66e5f7f --- /dev/null +++ b/src/tests/core/fixtures/fixtures_block.c @@ -0,0 +1,66 @@ +#include "fixtures_block.h" +#include "fixtures_tx.h" +#include "crypto.h" +#include "base_block.h" + +#include + +#define NUM_TXS 4 + +static void set_header(BlockHeader *header) { + header->timestamp = 100; + header->nonce = 123; + memset(header->prev_header_hash, 0xFA, BLOCK_HASH_LEN); + memset(header->all_tx, 0xAF, ALL_TX_HASH_LEN); +} + +int fixture_setup_unlinked_header(void **state){ + BlockHeader *header; + + header = malloc(sizeof(BlockHeader)); + set_header(header); + + *state = header; + + return 0; +} + +int fixture_teardown_unlinked_header(void **state) { + free(*state); + return 0; +} + +int fixture_setup_unlinked_block(void **state) { + Block *block; + Transaction *coinbase; + unsigned char tx_hashes[TX_HASH_LEN * NUM_TXS]; + + block = malloc(sizeof(Block)); + set_header(&block->header); + block->num_txs = NUM_TXS; + block->txs = malloc(NUM_TXS * sizeof(Transaction*)); + for (unsigned int i = 0; i < NUM_TXS; i++) + fixture_setup_unlinked_tx((void**)block->txs + i); + + // Make first tx a coinbase by removing inputs + coinbase = block->txs[0]; + for (unsigned int i = 0; i < coinbase->num_inputs; i++) + mbedtls_ecp_point_free(coinbase->inputs[i].pub_key); + free(coinbase->inputs); + coinbase->inputs = NULL; + coinbase->num_inputs = 0; + + // Build all_tx_hash + for (unsigned int i = 0; i < NUM_TXS; i++) + hash_tx(tx_hashes + i * TX_HASH_LEN, block->txs[i]); + hash_sha256(block->header.all_tx, tx_hashes, TX_HASH_LEN * NUM_TXS); + + *state = block; + + return 0; +} + +int fixture_teardown_unlinked_block(void **state) { + free_block(*state); + return 0; +} diff --git a/src/tests/core/fixtures/fixtures_global.c b/src/tests/core/fixtures/fixtures_global.c new file mode 100644 index 0000000..729bf89 --- /dev/null +++ b/src/tests/core/fixtures/fixtures_global.c @@ -0,0 +1,112 @@ +#include "fixtures_tx.h" +#include "fixtures_global.h" +#include "utxo_pool.h" +#include "wallet_pool.h" +#include "blockchain.h" +#include "mempool.h" +#include "utxo_to_tx.h" +#include "init_db.h" + +#include + +static ComposedFixture *composition = NULL; + +int fixture_setup_compose(void **state) { + composition = *state; + *state = NULL; + + int i = 0; + while (composition->setup[i]) + (*composition->setup[i++])(state); + + return 0; +} + +int fixture_teardown_compose(void **state) { + int i = 0; + while (composition->teardown[i]) + (*composition->teardown[i++])(state); + + composition = NULL; + + return 0; +} + +int fixture_clear_utxo_pool(void **state) { + utxo_pool_init_leveldb(TEST_DB_LOC); + destroy_db(&utxo_pool_db, utxo_pool_path); + return 0; +} + +int fixture_setup_utxo_pool(void **state) { + utxo_pool_init_leveldb(TEST_DB_LOC); + return 0; +} + +int fixture_teardown_utxo_pool(void **state) { + destroy_db(&utxo_pool_db, utxo_pool_path); + return 0; +} + +int fixture_clear_wallet(void **state) { + wallet_init_leveldb(TEST_DB_LOC); + destroy_wallet(); + return 0; +} + +int fixture_setup_wallet(void **state) { + wallet_init_leveldb(TEST_DB_LOC); + return 0; +} + +int fixture_teardown_wallet(void **state) { + destroy_wallet(); + return 0; +} + +int fixture_clear_blockchain(void **state) { + blockchain_init_leveldb(TEST_DB_LOC); + destroy_blockchain(); + return 0; +} + +int fixture_setup_blockchain(void **state) { + blockchain_init_leveldb(TEST_DB_LOC); + return 0; +} + +int fixture_teardown_blockchain(void **state) { + destroy_blockchain(); + return 0; +} + +int fixture_setup_mempool(void **state) { + mempool_init(); + return 0; +} + +int fixture_teardown_mempool(void **state) { + MemPool *current, *tmp; + HASH_ITER(hh, mempool, current, tmp) { + HASH_DEL(mempool, current); + free_tx(current->tx); + free(current); + } + mempool = NULL; + return 0; +} + +int fixture_setup_utxo_to_tx(void **state) { + utxo_to_tx_init(); + return 0; +} + +int fixture_teardown_utxo_to_tx(void **state) { + UTXOToTx *current, *tmp; + HASH_ITER(hh, utxo_to_tx, current, tmp) { + HASH_DEL(utxo_to_tx, current); + free(current); + } + utxo_to_tx = NULL; + return 0; +} diff --git a/src/tests/core/fixtures/fixtures_tx.c b/src/tests/core/fixtures/fixtures_tx.c new file mode 100644 index 0000000..f460ce2 --- /dev/null +++ b/src/tests/core/fixtures/fixtures_tx.c @@ -0,0 +1,100 @@ +#include "fixtures_tx.h" +#include "crypto.h" +#include "base_tx.h" + +#include +#include + +#define NUM_OUTPUTS 3 +#define NUM_INPUTS 4 + +static void init_output(Output *output, unsigned long amt) { + output->amt = amt; + // Unlinked, so this does not matter. Filled with random values + memset(&output->public_key_hash, amt + 2, PUB_KEY_HASH_LEN); +} + +static mbedtls_ecdsa_context *init_input(Input *input, unsigned int vout) { + mbedtls_ecdsa_context *keypair; + + keypair = gen_keys(); + input->pub_key = malloc(sizeof(mbedtls_ecp_point)); + mbedtls_ecp_point_init(input->pub_key); + mbedtls_ecp_copy(input->pub_key, &keypair->private_Q); + + input->prev_utxo_output = vout; + // Unlinked, so this does not matter. Filled with random values + memset(&(input->prev_tx_id), vout + 5, TX_HASH_LEN); + + input->sig_len = 0; + memset(&(input->signature), 0, SIGNATURE_LEN); + + return keypair; +} + +static void sign_input( + Input *input, mbedtls_ecdsa_context *keypair, unsigned char *tx_hash +) { + input->sig_len = write_sig( + input->signature, SIGNATURE_LEN, + tx_hash, TX_HASH_LEN, + keypair + ); +} + +int fixture_setup_unlinked_tx(void **state) { + Transaction *tx; + unsigned char tx_hash[TX_HASH_LEN]; + mbedtls_ecdsa_context *keys[NUM_INPUTS]; + + tx = malloc(sizeof(Transaction)); + tx->num_outputs = NUM_OUTPUTS; + tx->num_inputs = NUM_INPUTS; + + tx->outputs = malloc(sizeof(Output) * (tx->num_outputs)); + tx->inputs = malloc(sizeof(Input) * (tx->num_inputs)); + + init_output(&tx->outputs[0], 1); + init_output(&tx->outputs[1], 10); + init_output(&tx->outputs[2], 17); + + keys[0] = init_input(&tx->inputs[0], 5); + keys[1] = init_input(&tx->inputs[1], 9); + keys[2] = init_input(&tx->inputs[2], 0); + keys[3] = init_input(&tx->inputs[3], 2); + + hash_tx(tx_hash, tx); + sign_input(&tx->inputs[0], keys[0], tx_hash); + sign_input(&tx->inputs[1], keys[1], tx_hash); + sign_input(&tx->inputs[2], keys[2], tx_hash); + sign_input(&tx->inputs[3], keys[3], tx_hash); + + for (int i = 0; i < NUM_INPUTS; i++) + mbedtls_ecdsa_free(keys[i]); + + *state = tx; + + return 0; +} + +int fixture_teardown_unlinked_tx(void **state) { + free_tx(*state); + return 0; +} + +int fixture_setup_unlinked_utxo(void **state) { + UTXO *utxo; + + utxo = malloc(sizeof(UTXO)); + utxo->amt = 100; + memset(utxo->public_key_hash, 0xAA, PUB_KEY_HASH_LEN); + + *state = utxo; + + return 0; +} + +int fixture_teardown_unlinked_utxo(void **state) { + free(*state); + return 0; +} diff --git a/src/tests/core/fixtures/fixtures_wallet.c b/src/tests/core/fixtures/fixtures_wallet.c new file mode 100644 index 0000000..d58c88f --- /dev/null +++ b/src/tests/core/fixtures/fixtures_wallet.c @@ -0,0 +1,53 @@ +#include "fixtures_wallet.h" +#include "fixtures_tx.h" +#include "crypto.h" +#include "wallet_pool.h" + +#include +#include + +int fixture_setup_unlinked_wallet_entry(void **state) { + WalletEntry *content; + + content = malloc(sizeof(WalletEntry)); + content->amt = 90; + content->key_pair = gen_keys(); + content->spent = 1; + + *state = content; + + return 0; +} + +int fixture_teardown_unlinked_wallet_entry(void **state) { + free_wallet_entry(*state); + return 0; +} + +int fixture_setup_unlinked_keypair(void **state) { + *state = gen_keys(); + return 0; +} + +int fixture_teardown_unlinked_keypair(void **state) { + mbedtls_ecp_keypair_free(*state); + return 0; +} + +int fixture_setup_unlinked_tx_keypair(void **state) { + void **sub_state; + sub_state = malloc(sizeof(void*) * 2); + fixture_setup_unlinked_tx(&sub_state[0]); + fixture_setup_unlinked_keypair(&sub_state[1]); + *state = sub_state; + return 0; +} + +int fixture_teardown_unlinked_tx_keypair(void **state) { + void **sub_state; + sub_state = *state; + fixture_teardown_unlinked_tx(&sub_state[0]); + fixture_teardown_unlinked_keypair(&sub_state[1]); + free(sub_state); + return 0; +} diff --git a/src/tests/core/global/CMakeLists.txt b/src/tests/core/global/CMakeLists.txt index e853747..c74a97b 100644 --- a/src/tests/core/global/CMakeLists.txt +++ b/src/tests/core/global/CMakeLists.txt @@ -1,39 +1,52 @@ # Blockchain Test add_executable(test_blockchain test_blockchain.c) target_compile_options(test_blockchain PRIVATE ${COMPILE_OPTIONS_STD}) -target_include_directories(test_blockchain PUBLIC ${INCLUDE_ALL}) +target_include_directories(test_blockchain + PUBLIC ${INCLUDE_ALL} + PUBLIC ${INCLUDE_FIXTURES}) target_link_libraries(test_blockchain - blockchain) + blockchain + fixtures) # Mempool Test add_executable(test_mempool test_mempool.c) target_compile_options(test_mempool PRIVATE ${COMPILE_OPTIONS_STD}) -target_include_directories(test_mempool PUBLIC ${INCLUDE_ALL}) +target_include_directories(test_mempool + PUBLIC ${INCLUDE_ALL} + PUBLIC ${INCLUDE_FIXTURES}) target_link_libraries(test_mempool - mempool) + mempool + fixtures) # Test UTXO add_executable(test_utxo_pool test_utxo_pool.c) target_compile_options(test_utxo_pool PRIVATE ${COMPILE_OPTIONS_STD}) -target_include_directories(test_utxo_pool PUBLIC ${INCLUDE_ALL}) +target_include_directories(test_utxo_pool + PUBLIC ${INCLUDE_ALL} + PUBLIC ${INCLUDE_FIXTURES}) target_link_libraries(test_utxo_pool utxopool - init_db) + init_db + fixtures) # Test List add_executable(test_list test_list.c) target_compile_options(test_list PRIVATE ${COMPILE_OPTIONS_STD}) target_include_directories(test_list PUBLIC ${INCLUDE_ALL}) target_link_libraries(test_list - lists) + lists + cmocka-static) # Test UTXO To Tx add_executable(test_utxo_to_tx test_utxo_to_tx.c) target_compile_options(test_utxo_to_tx PRIVATE ${COMPILE_OPTIONS_STD}) -target_include_directories(test_utxo_to_tx PUBLIC ${INCLUDE_ALL}) +target_include_directories(test_utxo_to_tx + PUBLIC ${INCLUDE_ALL} + PUBLIC ${INCLUDE_FIXTURES}) target_link_libraries(test_utxo_to_tx utxo_to_tx - core_tx) + core_tx + fixtures) add_test(test_list ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_list) add_test(test_utxo_pool ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_utxo_pool) @@ -41,4 +54,4 @@ add_test(test_mempool ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_mempool) add_test(test_blockchain ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_blockchain) add_test(test_utxo_to_tx ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_utxo_to_tx) -add_subdirectory(chef_wrap) \ No newline at end of file +add_subdirectory(chef_wrap) diff --git a/src/tests/core/global/test_blockchain.c b/src/tests/core/global/test_blockchain.c index c2d5c62..37273fa 100644 --- a/src/tests/core/global/test_blockchain.c +++ b/src/tests/core/global/test_blockchain.c @@ -1,215 +1,138 @@ #include -#include "minunit.h" -#include "blockchain.h" -#include "init_db.h" - -int tests_run = 0; - -Block *_make_block() { - Block *block; - Transaction *tx; - - block = malloc(sizeof(Block)); - - block->txs = malloc(sizeof(Transaction*) * 1); - block->txs[0] = malloc(sizeof(Transaction)); - tx = block->txs[0]; - - tx->num_inputs = 0; - tx->inputs = NULL; - tx->num_outputs = 0; - tx->outputs = NULL; - block->num_txs = 1; +#include +#include - block->header.timestamp = 1; - memset(block->header.all_tx, 1, TX_HASH_LEN); - memset(block->header.prev_header_hash, 1, BLOCK_HASH_LEN); - block->header.nonce = 1; - - return block; -} +#include "blockchain.h" +#include "init_db.h" +#include "fixtures_block.h" +#include "fixtures_global.h" -static char *test_blockchain_init_exists() { +static void test_blockchain_init_exists(void **state) { + (void)state; int init_ret = blockchain_init_leveldb(TEST_DB_LOC); + assert_int_equal(init_ret, 0); destroy_blockchain(); - init_ret = blockchain_init_leveldb(TEST_DB_LOC); - mu_assert( - "Init return indicates fialure", - init_ret == 0 - ); - unsigned int count; - blockchain_count(&count); - mu_assert( - "Genesis block was not created", - count == 1 - ); - destroy_blockchain(); - return NULL; } -static char *test_blockchain_init_correct() { - char empty_tx_hash[TX_HASH_LEN]; - char empty_block_hash[BLOCK_HASH_LEN]; - - memset(empty_tx_hash, 0, TX_HASH_LEN); - memset(empty_block_hash, 0, BLOCK_HASH_LEN); - - blockchain_init_leveldb(TEST_DB_LOC); - - mu_assert( - "Chain Height Incorrect", - chain_height == 1 - ); - - // The following tests require to find the genesis block - // mu_assert( - // "Genesis num_txs incorrect", - // blockchain->block->num_txs == 0 - // ); - // mu_assert( - // "Genesis txs incorrect", - // blockchain->block->txs == NULL - // ); - // mu_assert( - // "Genesis timestamp incorrect", - // blockchain->block->header.timestamp == 0 - // ); - // mu_assert( - // "Genesis nonce incorrect", - // blockchain->block->header.nonce == 0 - // ); - // mu_assert( - // "Genesis tx hash incorrect", - // memcmp(blockchain->block->header.all_tx, empty_tx_hash, TX_HASH_LEN) == 0 - // ); - // mu_assert( - // "Genesis prev block hash incorrect", - // memcmp( - // blockchain->block->header.prev_header_hash, - // empty_block_hash, - // BLOCK_HASH_LEN - // ) == 0 - // ); - destroy_blockchain(); - return NULL; -} +static void test_blockchain_init_correct(void **state) { + (void)state; + Block genesis_block; + unsigned char genesis_hash[BLOCK_HASH_LEN]; + unsigned int count; -static char *test_blockchain_add() { + blockchain_count(&count); + assert_int_equal(count, 1); + + genesis_block.num_txs = 0; + genesis_block.txs = NULL; + genesis_block.header.timestamp = 0; + genesis_block.header.nonce = 0; + memset(genesis_block.header.all_tx, 0, TX_HASH_LEN); + memset(genesis_block.header.prev_header_hash, 0, BLOCK_HASH_LEN); + hash_blockheader(genesis_hash, &genesis_block.header); + + assert_int_equal(chain_height, 1); + assert_memory_equal(top_block_header_hash, genesis_hash, BLOCK_HASH_LEN); +} - blockchain_init_leveldb(TEST_DB_LOC); +static void test_blockchain_add(void **state) { Block *block; - block = _make_block(); + block = *state; int ret_add = blockchain_add_leveldb(block); unsigned int entries; blockchain_count(&entries); - mu_assert( - "Add return code indicates failure", - ret_add == 0 - ); - mu_assert( - "Add did not return correct block", - entries == 2 - ); - free(block->txs[0]); - free(block->txs); - free(block); - destroy_blockchain(); - return NULL; + assert_int_equal(ret_add, 0); + assert_int_equal(entries, 2); } -static char *test_blockchain_find() { +static void test_blockchain_find(void **state) { Block *block, *ret_block; unsigned char hash[BLOCK_HASH_LEN]; - block = _make_block(); + block = *state; hash_blockheader(hash, &(block->header)); - blockchain_init_leveldb(TEST_DB_LOC); blockchain_add_leveldb(block); int ret_find = blockchain_find_leveldb(&ret_block, hash); - mu_assert( - "Function returned failure", - ret_find == 0 - ); + assert_int_equal(ret_find, 0); + unsigned char found_hash[BLOCK_HASH_LEN]; hash_blockheader(found_hash, &(ret_block->header)); - mu_assert( - "Find did not return correct block", - memcmp(hash, found_hash, BLOCK_HASH_LEN) == 0 - ); - - free(block->txs[0]); - free(block->txs); - free(block); - destroy_blockchain(); - return NULL; + assert_memory_equal(hash, found_hash, BLOCK_HASH_LEN); + + free_block(ret_block); } -static char *test_blockchain_remove() { +static void test_blockchain_remove(void **state) { Block *block, *ret_block = NULL; unsigned char hash[BLOCK_HASH_LEN]; + unsigned int prev_count, count; - block = _make_block(); + block = *state; hash_blockheader(hash, &(block->header)); - blockchain_init_leveldb(TEST_DB_LOC); blockchain_add_leveldb(block); - unsigned long prev_chain_height = chain_height; - unsigned int prev_count; blockchain_count(&prev_count); + int ret_remove = blockchain_remove_leveldb(hash); - unsigned int count; blockchain_count(&count); - mu_assert( - "Remove block returned failure code", - ret_remove == 0 - ); - mu_assert( - "Remove did not decrease blockchain size in expected amount", - count == prev_count-1 - ); - mu_assert( - "Chain Height not decremented", - chain_height = prev_chain_height-1 - ); + assert_int_equal(ret_remove, 0); + assert_int_equal(count, prev_count - 1); int ret_find = blockchain_find_leveldb(&ret_block, hash); - mu_assert( - "Block Find returns success after block removed", - ret_find != 0 - ); - mu_assert( - "Block was not removed", - ret_block == NULL - ); - - free(block->txs[0]); - free(block->txs); - free(block); - free(ret_block); - destroy_blockchain(); - return NULL; -} - -static char *all_tests() { - mu_run_test(test_blockchain_init_exists); - mu_run_test(test_blockchain_init_correct); - mu_run_test(test_blockchain_add); - mu_run_test(test_blockchain_find); - mu_run_test(test_blockchain_remove); - return NULL; + assert_int_not_equal(ret_find, 0); + assert_ptr_equal(ret_block, NULL); } int main() { - create_proj_folders(); - char *result = all_tests(); - if (result != NULL) { - printf("%s\n", result); - } else { - printf("blockchain.c passing!\n"); - } - printf("Tests run: %d\n", tests_run); - - return result != 0; + int (*blockchain_setup[])(void**) = { + fixture_setup_blockchain, + fixture_setup_unlinked_block, + NULL + }; + int (*blockchain_teardown[])(void**) = { + fixture_teardown_blockchain, + fixture_teardown_unlinked_block, + NULL + }; + ComposedFixture composition; + + composition.setup = blockchain_setup; + composition.teardown = blockchain_teardown; + + const struct CMUnitTest tests[] = { + cmocka_unit_test_prestate_setup_teardown( + test_blockchain_init_exists, + NULL, + NULL, + NULL + ), + cmocka_unit_test_prestate_setup_teardown( + test_blockchain_init_correct, + fixture_setup_blockchain, + fixture_teardown_blockchain, + NULL + ), + cmocka_unit_test_prestate_setup_teardown( + test_blockchain_add, + fixture_setup_compose, + fixture_teardown_compose, + &composition + ), + cmocka_unit_test_prestate_setup_teardown( + test_blockchain_find, + fixture_setup_compose, + fixture_teardown_compose, + &composition + ), + cmocka_unit_test_prestate_setup_teardown( + test_blockchain_remove, + fixture_setup_compose, + fixture_teardown_compose, + &composition + ) + }; + + return cmocka_run_group_tests(tests, fixture_clear_blockchain, NULL); } diff --git a/src/tests/core/global/test_list.c b/src/tests/core/global/test_list.c index 836a0a5..8015649 100644 --- a/src/tests/core/global/test_list.c +++ b/src/tests/core/global/test_list.c @@ -1,96 +1,58 @@ #include -#include "minunit.h" -#include "list.h" -int tests_run = 0; +#include +#include -static char *test_list_build() { - List *list; +#include "list.h" - list = build_list(sizeof(long)); +static void test_list_build(void **state) { + List *list; - mu_assert( - "List was not initialized", - list->data != NULL - ); - mu_assert( - "List length incorrect", - list->len == 0 - ); - mu_assert( - "List total length incorrect", - list->total_len == 1 - ); - mu_assert( - "List element size incorrect", - list->element_size == sizeof(long) - ); + (void)state; - return NULL; + list = build_list(sizeof(long)); + assert_ptr_not_equal(list->data, NULL); + assert_int_equal(list->len, 0); + assert_int_equal(list->total_len, 1); + assert_int_equal(list->element_size, sizeof(long)); } -static char *test_list_append() { +static void test_list_append(void **state) { List *list; long entry; + (void)state; + list = build_list(sizeof(long)); entry = 42; - list_append(list, &entry); - - mu_assert( - "List entry not appended", - **(long**)list->data == entry - ); - mu_assert( - "List length post append incorrect", - list->len == 1 - ); - - return NULL; + assert_int_equal(*(long*)(list->data[0]), entry); + assert_int_equal(list->len, 1); } -static char *test_list_pop() { +static void test_list_pop(void **state) { List *list; long entry, *ret; + (void)state; + list = build_list(sizeof(long)); entry = 42; list_append(list, &entry); ret = list_pop(list); - mu_assert( - "List entry address wrong", - ret == &entry - ); - mu_assert( - "List entry value wrong", - *ret == entry - ); - mu_assert( - "List length post pop incorrect", - list->len == 0 - ); - - return NULL; -} - -static char *all_tests() { - mu_run_test(test_list_build); - mu_run_test(test_list_append); - mu_run_test(test_list_pop); - return NULL; + assert_ptr_equal(ret, &entry); + assert_int_equal(*ret, entry); + assert_int_equal(list->len, 0); } int main() { - char *result = all_tests(); - if (result != NULL) { - printf("%s\n", result); - } else { - printf("list.c passing!\n"); - } - printf("Tests run: %d\n", tests_run); + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_list_build), + cmocka_unit_test(test_list_append), + cmocka_unit_test(test_list_pop), + }; - return result != 0; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/src/tests/core/global/test_mempool.c b/src/tests/core/global/test_mempool.c index e18f1f9..10cf18f 100644 --- a/src/tests/core/global/test_mempool.c +++ b/src/tests/core/global/test_mempool.c @@ -1,127 +1,115 @@ #include -#include "minunit.h" -#include "mempool.h" -#include "crypto.h" - -int tests_run = 0; - -Transaction *_make_tx() { - Transaction *tx; - Input *in; - Output *out; - mbedtls_ecdsa_context *key_pair; - - tx = malloc(sizeof(Transaction)); - in = malloc(sizeof(Input)); - out = malloc(sizeof(Output)); - - memset(in, 0, sizeof(Input)); - key_pair = gen_keys(); - in->pub_key = &(key_pair->private_Q); - memset(out, 0, sizeof(Output)); +#include +#include - tx->num_inputs = 1; - tx->inputs = in; - tx->num_outputs = 1; - tx->outputs = out; - - return tx; -} +#include "mempool.h" +#include "crypto.h" +#include "fixtures_tx.h" +#include "fixtures_global.h" -static char *test_mempool_init() { +static void test_mempool_init(void **state) { + (void)state; mempool_init(); - mu_assert( - "Mempool was not initialized", - mempool == NULL - ); - return NULL; + assert_ptr_equal(mempool, NULL); } -static char *test_mempool_add() { +static void test_mempool_add(void **state) { Transaction *tx, *ret_tx; + unsigned int prev_count, count; + unsigned char tx_hash[TX_HASH_LEN], ret_hash[TX_HASH_LEN]; - tx = _make_tx(); - mempool_init(); + tx = *state; + prev_count = HASH_COUNT(mempool); ret_tx = mempool_add(tx); - mu_assert( - "Add did not return correct tx", - ret_tx == tx - ); + count = HASH_COUNT(mempool); + hash_tx(tx_hash, tx); + hash_tx(ret_hash, ret_tx); - free(tx->inputs); - free(tx->outputs); - free(tx); - - return NULL; + assert_ptr_not_equal(ret_tx, NULL); + assert_int_equal(count, prev_count + 1); + assert_memory_equal(tx_hash, ret_hash, TX_HASH_LEN); } -static char *test_mempool_find() { +static void test_mempool_find(void **state) { Transaction *tx, *ret_tx; - unsigned char hash[TX_HASH_LEN]; - - tx = _make_tx(); - hash_tx(hash, tx); + unsigned char tx_hash[TX_HASH_LEN], ret_hash[TX_HASH_LEN]; - mempool_init(); + tx = *state; mempool_add(tx); - ret_tx = mempool_find(hash); - mu_assert( - "Find did not return correct tx", - ret_tx == tx - ); + hash_tx(tx_hash, tx); - free(tx->inputs); - free(tx->outputs); - free(tx); + ret_tx = mempool_find(tx_hash); + hash_tx(ret_hash, ret_tx); - return NULL; + assert_ptr_not_equal(ret_tx, NULL); + assert_memory_equal(tx_hash, ret_hash, TX_HASH_LEN); } -static char *test_mempool_remove() { +static void test_mempool_remove(void **state) { Transaction *tx, *ret_tx; unsigned char hash[TX_HASH_LEN]; + unsigned int prev_count, count; - tx = _make_tx(); + tx = *state; hash_tx(hash, tx); - mempool_init(); mempool_add(tx); + prev_count = HASH_COUNT(mempool); + ret_tx = mempool_remove(hash); - mu_assert( - "Remove did not return correct tx", - ret_tx == tx - ); + count = HASH_COUNT(mempool); + assert_ptr_not_equal(ret_tx, NULL); + assert_int_equal(count, prev_count - 1); ret_tx = mempool_find(hash); - mu_assert( - "Tx was not removed", - ret_tx == NULL - ); - - free(tx->inputs); - free(tx->outputs); - free(tx); - - return NULL; -} + assert_ptr_equal(ret_tx, NULL); -static char *all_tests() { - mu_run_test(test_mempool_init); - mu_run_test(test_mempool_add); - mu_run_test(test_mempool_find); - mu_run_test(test_mempool_remove); - return NULL; + free_tx(ret_tx); } int main() { - char *result = all_tests(); - if (result != NULL) { - printf("%s\n", result); - } else { - printf("mempool.c passing!\n"); - } - printf("Tests run: %d\n", tests_run); - - return result != 0; + int (*mempool_setup[])(void**) = { + fixture_setup_mempool, + fixture_setup_unlinked_tx, + NULL + }; + int (*mempool_teardown[])(void**) = { + fixture_teardown_mempool, + fixture_teardown_unlinked_tx, + NULL + }; + ComposedFixture composition; + + composition.setup = mempool_setup; + composition.teardown = mempool_teardown; + + const struct CMUnitTest tests[] = { + cmocka_unit_test_prestate_setup_teardown( + test_mempool_init, + fixture_setup_mempool, + fixture_teardown_mempool, + NULL + ), + cmocka_unit_test_prestate_setup_teardown( + test_mempool_add, + fixture_setup_compose, + fixture_teardown_compose, + &composition + ), + cmocka_unit_test_prestate_setup_teardown( + test_mempool_find, + fixture_setup_compose, + fixture_teardown_compose, + &composition + ), + cmocka_unit_test_prestate_setup_teardown( + test_mempool_remove, + fixture_setup_compose, + fixture_teardown_compose, + &composition + ) + }; + + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/src/tests/core/global/test_utxo_pool.c b/src/tests/core/global/test_utxo_pool.c index 302fd2b..d5ae57d 100644 --- a/src/tests/core/global/test_utxo_pool.c +++ b/src/tests/core/global/test_utxo_pool.c @@ -1,161 +1,123 @@ #include -#include "minunit.h" + +#include +#include + #include "utxo_pool.h" #include "crypto.h" #include "init_db.h" +#include "fixtures_tx.h" +#include "fixtures_global.h" -int tests_run = 0; - -Transaction *_make_tx() { - Transaction *tx; - Input *in; - Output *out; - mbedtls_ecdsa_context *key_pair; - - tx = malloc(sizeof(Transaction)); - in = malloc(sizeof(Input)); - out = malloc(sizeof(Output)); - - memset(in, 0, sizeof(Input)); - key_pair = gen_keys(); - in->pub_key = &(key_pair->private_Q); - - memset(out, 0, sizeof(Output)); - - tx->num_inputs = 1; - tx->inputs = in; - tx->num_outputs = 1; - tx->outputs = out; - - return tx; -} - -static char *test_utxo_pool_init() { - utxo_pool_init_leveldb(TEST_DB_LOC); - mu_assert( - "UTXO DB is still null", - utxo_pool_db != NULL - ); - mu_assert( - "UTXO path is null", - utxo_pool_path != NULL - ); - destroy_db(&utxo_pool_db, utxo_pool_path); - return NULL; +static void test_utxo_pool_init(void **state) { + (void)state; + assert_ptr_not_equal(utxo_pool_db, NULL); + assert_ptr_not_equal(utxo_pool_path, NULL); } -static char *test_utxo_pool_add() { +static void test_utxo_pool_add(void **state) { Transaction *tx; + unsigned int num_utxo_prev, num_utxo_after; + int ret; - tx = _make_tx(); - utxo_pool_init_leveldb(TEST_DB_LOC); - unsigned int num_utxo_prev; + tx = *state; utxo_pool_count(&num_utxo_prev); - int ret = utxo_pool_add_leveldb(tx,0); - unsigned int num_utxo_after; + ret = utxo_pool_add_leveldb(tx,0); utxo_pool_count(&num_utxo_after); - mu_assert( - "Return Value indicates a failure", - ret == 0 - ); - mu_assert( - "UTXO pool increased in size incorrectly", - num_utxo_after == num_utxo_prev+1 - ); - free(tx->inputs); - free(tx->outputs); - free(tx); - destroy_db(&utxo_pool_db, utxo_pool_path); - return NULL; + assert_int_equal(ret, 0); + assert_int_equal(num_utxo_after, num_utxo_prev + 1); } -static char *test_utxo_pool_find() { +static void test_utxo_pool_find(void **state) { Transaction *tx; UTXO *ret_utxo = NULL; unsigned char hash[TX_HASH_LEN]; + int ret_found; - tx = _make_tx(); + tx = *state; hash_tx(hash, tx); - utxo_pool_init_leveldb(TEST_DB_LOC); utxo_pool_add_leveldb(tx, 0); - int ret_found = utxo_pool_find_leveldb(&ret_utxo, hash, 0); - mu_assert( - "Found did not return proper error code", - ret_found == 0 - ); - mu_assert( - "Returned utxo not different", - ret_utxo != NULL - ); - mu_assert( - "Returned utxo amount if off", - ret_utxo->amt == tx->outputs[0].amt - ); - mu_assert( - "Returned utxo amount if off", - memcmp(ret_utxo->public_key_hash, tx->outputs[0].public_key_hash, PUB_KEY_HASH_LEN) == 0 + ret_found = utxo_pool_find_leveldb(&ret_utxo, hash, 0); + + assert_int_equal(ret_found, 0); + assert_ptr_not_equal(ret_utxo, NULL); + assert_int_equal(ret_utxo->amt, tx->outputs[0].amt); + assert_memory_equal( + ret_utxo->public_key_hash, + tx->outputs[0].public_key_hash, + PUB_KEY_HASH_LEN ); - free(tx->inputs); - free(tx->outputs); - free(tx); free(ret_utxo); - destroy_db(&utxo_pool_db, utxo_pool_path); - return NULL; } -static char *test_utxo_pool_remove() { +static void test_utxo_pool_remove(void **state) { Transaction *tx; UTXO *ret_utxo = NULL; unsigned char hash[TX_HASH_LEN]; + unsigned int prev_count, count; + int ret_remove, ret_found; - tx = _make_tx(); + tx = *state; hash_tx(hash, tx); - utxo_pool_init_leveldb(TEST_DB_LOC); utxo_pool_add_leveldb(tx, 0); - int ret_remove = utxo_pool_remove_leveldb(hash, 0); - - mu_assert( - "Remove returned failed code", - ret_remove == 0 - ); - int ret_found = utxo_pool_find_leveldb(&ret_utxo, hash, 0); - mu_assert( - "utxo was found after it was deleted?", - ret_found != 0 - ); - mu_assert( - "utxo find value not null after delete", - ret_utxo == NULL - ); + utxo_pool_count(&prev_count); - free(tx->inputs); - free(tx->outputs); - free(tx); - destroy_db(&utxo_pool_db, utxo_pool_path); - return NULL; -} + ret_remove = utxo_pool_remove_leveldb(hash, 0); + utxo_pool_count(&count); + assert_int_equal(ret_remove, 0); + assert_int_equal(count, prev_count - 1); -static char *all_tests() { - mu_run_test(test_utxo_pool_init); - mu_run_test(test_utxo_pool_add); - mu_run_test(test_utxo_pool_find); - mu_run_test(test_utxo_pool_remove); - return NULL; + ret_found = utxo_pool_find_leveldb(&ret_utxo, hash, 0); + assert_int_not_equal(ret_found, 0); + assert_ptr_equal(ret_utxo, NULL); } int main() { - create_proj_folders(); - char *result = all_tests(); - if (result != NULL) { - printf("%s\n", result); - } else { - printf("utxo_pool.c passing!\n"); - } - printf("Tests run: %d\n", tests_run); - - return result != 0; + int (*utxo_tx_setup[])(void**) = { + fixture_setup_utxo_pool, + fixture_setup_unlinked_tx, + NULL + }; + int (*utxo_tx_teardown[])(void**) = { + fixture_teardown_utxo_pool, + fixture_teardown_unlinked_tx, + NULL + }; + ComposedFixture composition; + + composition.setup = utxo_tx_setup; + composition.teardown = utxo_tx_teardown; + + const struct CMUnitTest tests[] = { + cmocka_unit_test_prestate_setup_teardown( + test_utxo_pool_init, + fixture_setup_utxo_pool, + fixture_teardown_utxo_pool, + NULL + ), + cmocka_unit_test_prestate_setup_teardown( + test_utxo_pool_add, + fixture_setup_compose, + fixture_teardown_compose, + &composition + ), + cmocka_unit_test_prestate_setup_teardown( + test_utxo_pool_find, + fixture_setup_compose, + fixture_teardown_compose, + &composition + ), + cmocka_unit_test_prestate_setup_teardown( + test_utxo_pool_remove, + fixture_setup_compose, + fixture_teardown_compose, + &composition + ) + }; + + return cmocka_run_group_tests(tests, fixture_clear_utxo_pool, NULL); } diff --git a/src/tests/core/global/test_utxo_to_tx.c b/src/tests/core/global/test_utxo_to_tx.c index e446ccd..f043b86 100644 --- a/src/tests/core/global/test_utxo_to_tx.c +++ b/src/tests/core/global/test_utxo_to_tx.c @@ -1,203 +1,143 @@ #include -#include "minunit.h" -#include "utxo_to_tx.h" -#include "crypto.h" - -int tests_run = 0; - -Transaction *_make_tx() { - Transaction *tx; - Input *in; - Output *out; - mbedtls_ecdsa_context *key_pair; - - tx = malloc(sizeof(Transaction)); - in = malloc(sizeof(Input) * 2); - out = malloc(sizeof(Output)); - - memset(&in[0], 0x2, sizeof(Input)); - key_pair = gen_keys(); - in[0].pub_key = &(key_pair->private_Q); - - memset(&in[1], 0x8, sizeof(Input)); - key_pair = gen_keys(); - in[1].pub_key = &(key_pair->private_Q); - memset(out, 0, sizeof(Output)); +#include +#include - tx->num_inputs = 2; - tx->inputs = in; - tx->num_outputs = 1; - tx->outputs = out; - - return tx; -} - -void _free_tx(Transaction *tx) { - if (tx->inputs != NULL) - free(tx->inputs); - if (tx->outputs != NULL) - free(tx->outputs); - free(tx); -} +#include "utxo_to_tx.h" +#include "crypto.h" +#include "fixtures_tx.h" +#include "fixtures_global.h" -static char *test_utxo_to_tx_init() { +static void test_utxo_to_tx_init(void **state) { + (void)state; utxo_to_tx_init(); - mu_assert( - "UTXO to Tx mapping was not initialized", - utxo_to_tx == NULL - ); - return NULL; + assert_ptr_equal(utxo_to_tx, NULL); } -static char *test_utxo_to_tx_add() { +static void test_utxo_to_tx_add(void **state) { Transaction *tx; int ret_val; Input *in; + unsigned int prev_count, count; unsigned char tx_hash[TX_HASH_LEN]; - tx = _make_tx(); - utxo_to_tx_init(); - + tx = *state; in = &tx->inputs[0]; hash_tx(tx_hash, tx); + prev_count = HASH_COUNT(utxo_to_tx); ret_val = utxo_to_tx_add(in->prev_tx_id, in->prev_utxo_output, tx_hash); - - mu_assert( - "Return value indicates failure", - ret_val == 0 - ); + count = HASH_COUNT(utxo_to_tx); + assert_int_equal(ret_val, 0); // Relies on UTHash storing new additions as first value in utxo_to_tx global // variable, which *should* always happen since it starts as NULL - mu_assert( - "Stored UTXO transaction hash incorrect", - memcmp( - utxo_to_tx->id.tx_hash, - in->prev_tx_id, - TX_HASH_LEN - ) == 0 - ); - mu_assert( - "Stored UTXO vout incorrect", - utxo_to_tx->id.vout == in->prev_utxo_output - ); - mu_assert( - "Stored transaction hash incorrect", - memcmp( - utxo_to_tx->tx_hash, - tx_hash, - TX_HASH_LEN - ) == 0 - ); - - _free_tx(tx); - - return NULL; + assert_memory_equal(utxo_to_tx->id.tx_hash, in->prev_tx_id, TX_HASH_LEN); + assert_int_equal(utxo_to_tx->id.vout, in->prev_utxo_output); + assert_memory_equal(utxo_to_tx->tx_hash, tx_hash, TX_HASH_LEN); + assert_int_equal(count, prev_count + 1); } -static char *test_utxo_to_tx_add_tx() { +static void test_utxo_to_tx_add_tx(void **state) { Transaction *tx; int ret_val, count; unsigned char tx_hash[TX_HASH_LEN]; - tx = _make_tx(); - utxo_to_tx_init(); - + tx = *state; hash_tx(tx_hash, tx); ret_val = utxo_to_tx_add_tx(tx); - - mu_assert( - "Return value indicates failure", - ret_val == 0 - ); + assert_int_equal(ret_val, 0); // add_tx() calls add() under the hood, so we just test to ensure something // was added, and not what was added. This is better served by a mock count = HASH_COUNT(utxo_to_tx); - mu_assert( - "Incorrect final hashmap length", - count == 2 - ); - - _free_tx(tx); - - return NULL; + assert_int_equal(count, 4); } -static char *test_utxo_to_tx_find() { +static void test_utxo_to_tx_find(void **state) { Transaction *tx; Input *in; int ret_val; unsigned char tx_hash[TX_HASH_LEN], ret_hash[TX_HASH_LEN]; - tx = _make_tx(); + tx = *state; in = &tx->inputs[0]; hash_tx(tx_hash, tx); - utxo_to_tx_init(); utxo_to_tx_add_tx(tx); ret_val = utxo_to_tx_find(ret_hash, in->prev_tx_id, in->prev_utxo_output); - mu_assert( - "Return value indicates failure", - ret_val == 0 - ); - mu_assert( - "Find did not return correct transacation hash", - memcmp(tx_hash, ret_hash, TX_HASH_LEN) == 0 - ); - - _free_tx(tx); - - return NULL; + assert_int_equal(ret_val, 0); + assert_memory_equal(tx_hash, ret_hash, TX_HASH_LEN); } -static char *test_utxo_to_tx_remove() { +static void test_utxo_to_tx_remove(void **state) { Transaction *tx; Input *in; int ret_val; + unsigned int prev_count, count; - tx = _make_tx(); + tx = *state; in = &tx->inputs[0]; - - utxo_to_tx_init(); utxo_to_tx_add_tx(tx); + prev_count = HASH_COUNT(utxo_to_tx); ret_val = utxo_to_tx_remove(in->prev_tx_id, in->prev_utxo_output); - mu_assert( - "Return value indicates failure", - ret_val == 0 - ); + count = HASH_COUNT(utxo_to_tx); + assert_int_equal(ret_val, 0); + assert_int_equal(count, prev_count - 1); ret_val = utxo_to_tx_find(NULL, in->prev_tx_id, in->prev_utxo_output); - mu_assert( - "Entry was not removed", - ret_val == 1 - ); - - _free_tx(tx); - - return NULL; -} - -static char *all_tests() { - mu_run_test(test_utxo_to_tx_init); - mu_run_test(test_utxo_to_tx_add); - mu_run_test(test_utxo_to_tx_add_tx); - mu_run_test(test_utxo_to_tx_find); - mu_run_test(test_utxo_to_tx_remove); - return NULL; + assert_int_equal(ret_val, 1); } int main() { - char *result = all_tests(); - if (result != NULL) { - printf("%s\n", result); - } else { - printf("utxo_to_tx.c passing!\n"); - } - printf("Tests run: %d\n", tests_run); - - return result != 0; + int (*utxo_to_tx_setup[])(void**) = { + fixture_setup_utxo_to_tx, + fixture_setup_unlinked_tx, + NULL + }; + int (*utxo_to_tx_teardown[])(void**) = { + fixture_teardown_utxo_to_tx, + fixture_teardown_unlinked_tx, + NULL + }; + ComposedFixture composition; + + composition.setup = utxo_to_tx_setup; + composition.teardown = utxo_to_tx_teardown; + + const struct CMUnitTest tests[] = { + cmocka_unit_test_prestate_setup_teardown( + test_utxo_to_tx_init, + NULL, + NULL, + NULL + ), + cmocka_unit_test_prestate_setup_teardown( + test_utxo_to_tx_add, + fixture_setup_compose, + fixture_teardown_compose, + &composition + ), + cmocka_unit_test_prestate_setup_teardown( + test_utxo_to_tx_add_tx, + fixture_setup_compose, + fixture_teardown_compose, + &composition + ), + cmocka_unit_test_prestate_setup_teardown( + test_utxo_to_tx_find, + fixture_setup_compose, + fixture_teardown_compose, + &composition + ), + cmocka_unit_test_prestate_setup_teardown( + test_utxo_to_tx_remove, + fixture_setup_compose, + fixture_teardown_compose, + &composition + ) + }; + + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/src/tests/core/txs/CMakeLists.txt b/src/tests/core/txs/CMakeLists.txt index b26ac51..a5ba6d7 100644 --- a/src/tests/core/txs/CMakeLists.txt +++ b/src/tests/core/txs/CMakeLists.txt @@ -1,27 +1,39 @@ # Test base Tx add_executable(test_base_tx test_base_tx.c) target_compile_options(test_base_tx PRIVATE ${COMPILE_OPTIONS_STD}) -target_include_directories(test_base_tx PUBLIC ${INCLUDE_ALL}) +target_include_directories(test_base_tx + PUBLIC ${INCLUDE_ALL} + PUBLIC ${INCLUDE_FIXTURES}) target_link_libraries(test_base_tx - core_tx) + core_tx + fixtures) add_executable(test_ser_tx test_ser_tx.c) target_compile_options(test_ser_tx PRIVATE ${COMPILE_OPTIONS_STD}) -target_include_directories(test_ser_tx PUBLIC ${INCLUDE_ALL}) +target_include_directories(test_ser_tx + PUBLIC ${INCLUDE_ALL} + PUBLIC ${INCLUDE_FIXTURES}) target_link_libraries(test_ser_tx - core_tx) + core_tx + fixtures) add_executable(test_wallet_pool test_wallet_pool.c) target_compile_options(test_wallet_pool PRIVATE ${COMPILE_OPTIONS_STD}) -target_include_directories(test_wallet_pool PUBLIC ${INCLUDE_ALL}) +target_include_directories(test_wallet_pool + PUBLIC ${INCLUDE_ALL} + PUBLIC ${INCLUDE_FIXTURES}) target_link_libraries(test_wallet_pool - wallet_pool) + wallet_pool + fixtures) add_executable(test_ser_wallet test_ser_wallet.c) target_compile_options(test_ser_wallet PRIVATE ${COMPILE_OPTIONS_STD}) -target_include_directories(test_ser_wallet PUBLIC ${INCLUDE_ALL}) +target_include_directories(test_ser_wallet + PUBLIC ${INCLUDE_ALL} + PUBLIC ${INCLUDE_FIXTURES}) target_link_libraries(test_ser_wallet - wallet_pool) + wallet_pool + fixtures) add_executable(test_wallet test_wallet.c) target_compile_options(test_wallet PRIVATE ${COMPILE_OPTIONS_STD}) diff --git a/src/tests/core/txs/test_base_tx.c b/src/tests/core/txs/test_base_tx.c index 28f7a39..e3df82d 100644 --- a/src/tests/core/txs/test_base_tx.c +++ b/src/tests/core/txs/test_base_tx.c @@ -1,71 +1,33 @@ #include #include #include -#include "minunit.h" -#include "base_tx.h" -#include "ser_tx.h" -#include "crypto.h" - -int tests_run = 0; - -Transaction *_make_tx() { - Transaction *a_Tx = malloc(sizeof(Transaction)); - a_Tx->num_inputs = 2; - a_Tx->num_outputs = 1; - - a_Tx->inputs = malloc(sizeof(Input) * (a_Tx->num_inputs)); - a_Tx->outputs = malloc(sizeof(Output) * (a_Tx->num_outputs)); - - Output *an_Output = &(a_Tx->outputs[0]); - memset(an_Output, 0, sizeof(Output)); - an_Output->amt = 5; - strcpy((char*)an_Output->public_key_hash, "a_val"); - - Input *input_1 = &(a_Tx->inputs[0]); - memset(input_1, 0, sizeof(Input)); - input_1->prev_utxo_output = 2; - mbedtls_ecdsa_context *key_pair_1 = gen_keys(); - input_1->pub_key = &key_pair_1->private_Q; - Input *input_2 = &(a_Tx->inputs[1]); - memset(input_2, 0, sizeof(Input)); - input_2->prev_utxo_output = 4; - mbedtls_ecdsa_context *key_pair_2 = gen_keys(); - input_2->pub_key = &key_pair_2->private_Q; +#include +#include - return a_Tx; -} +#include "base_tx.h" +#include "fixtures_tx.h" -static char *test_copy_tx() { +static void test_copy_tx(void **state) { Transaction *tx, *copy; unsigned char tx_hash[TX_HASH_LEN], copy_hash[TX_HASH_LEN]; - tx = _make_tx(); + tx = *state; hash_tx(tx_hash, tx); - copy = copy_tx(tx); hash_tx(copy_hash, copy); - mu_assert( - "Copy produces different hash", - memcmp(tx_hash, copy_hash, TX_HASH_LEN) == 0 - ); - return NULL; -} - -static char *all_tests() { - mu_run_test(test_copy_tx); - return NULL; + assert_memory_equal(tx_hash, copy_hash, TX_HASH_LEN); } int main() { - char *result = all_tests(); - if (result != NULL) { - printf("%s\n", result); - } else { - printf("base_tx.c passing!\n"); - } - printf("Tests run: %d\n", tests_run); - - return result != 0; + const struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown( + test_copy_tx, + fixture_setup_unlinked_tx, + fixture_teardown_unlinked_tx + ) + }; + + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/src/tests/core/txs/test_ser_tx.c b/src/tests/core/txs/test_ser_tx.c index 9a8b2dc..c63698b 100644 --- a/src/tests/core/txs/test_ser_tx.c +++ b/src/tests/core/txs/test_ser_tx.c @@ -1,192 +1,249 @@ #include #include #include -#include "minunit.h" + +#include +#include + #include "base_tx.h" #include "ser_tx.h" -#include "ser_key.h" #include "crypto.h" +#include "fixtures_tx.h" -int tests_run = 0; - -Transaction *_make_tx() { - Transaction *a_Tx = malloc(sizeof(Transaction)); - a_Tx->num_inputs = 2; - a_Tx->num_outputs = 1; - - a_Tx->inputs = malloc(sizeof(Input) * (a_Tx->num_inputs)); - a_Tx->outputs = malloc(sizeof(Output) * (a_Tx->num_outputs)); - - Output *an_Output = &(a_Tx->outputs[0]); - memset(an_Output, 0, sizeof(Output)); - an_Output->amt = 5; - strcpy((char*)an_Output->public_key_hash, "a_val"); - - Input *input_1 = &(a_Tx->inputs[0]); - memset(input_1, 0, sizeof(Input)); - input_1->prev_utxo_output = 2; - mbedtls_ecdsa_context *key_pair_1 = gen_keys(); - input_1->pub_key = &key_pair_1->private_Q; - - Input *input_2 = &(a_Tx->inputs[1]); - memset(input_2, 0, sizeof(Input)); - input_2->prev_utxo_output = 4; - mbedtls_ecdsa_context *key_pair_2 = gen_keys(); - input_2->pub_key = &key_pair_2->private_Q; - - return a_Tx; -} - -UTXO *_make_utxo() { - UTXO *utxo; - utxo = malloc(sizeof(UTXO)); - utxo->amt = 100; - memset(utxo->public_key_hash, 0xAA, PUB_KEY_HASH_LEN); - return utxo; -} - -static char *test_ser_tx() { +static void test_ser_tx_equal(void **state) { ssize_t written_ser_tx, read_ser_tx; - - Transaction *a_Tx = _make_tx(); - unsigned char *char_tx = ser_tx_alloc(&read_ser_tx, a_Tx); - Transaction *other_tx = deser_tx_alloc(&written_ser_tx, char_tx); - - mu_assert( - "Num of bytes read incorrect", - read_ser_tx == (ssize_t)size_ser_tx(a_Tx) - ); - mu_assert( - "Num of bytes read and written don't match up", - read_ser_tx == written_ser_tx - ); - - mu_assert( - "Num inputs doesn't match after serialization", - a_Tx->num_inputs == other_tx->num_inputs - ); - mu_assert( - "Num outputs doesn't match after serialization", - a_Tx->num_outputs == other_tx->num_outputs - ); - - // Test Inputs - unsigned char buf_1[PUB_KEY_SER_LEN]; - unsigned char buf_2[PUB_KEY_SER_LEN]; - for (size_t i = 0; i < a_Tx->num_inputs; i++) { - mu_assert( - "Input vout doesn't match up after de/serialization", - a_Tx->inputs[i].prev_utxo_output == other_tx->inputs[i].prev_utxo_output + Transaction *tx, *desered_tx; + unsigned char *sered_tx; + unsigned char buf_1[PUB_KEY_SER_LEN], buf_2[PUB_KEY_SER_LEN]; + unsigned char output_hash[TX_HASH_LEN], deser_output_hash[TX_HASH_LEN]; + + tx = *state; + sered_tx = ser_tx_alloc(&read_ser_tx, tx); + desered_tx = deser_tx_alloc(&written_ser_tx, sered_tx); + + assert_int_equal(read_ser_tx, (ssize_t)size_ser_tx(tx)); + assert_int_equal(read_ser_tx, written_ser_tx); + assert_int_equal(tx->num_inputs, desered_tx->num_inputs); + assert_int_equal(tx->num_outputs, desered_tx->num_outputs); + + for (size_t i = 0; i < tx->num_inputs; i++) { + assert_int_equal( + tx->inputs[i].prev_utxo_output, desered_tx->inputs[i].prev_utxo_output ); - mu_assert( - "Input tx hash doesn't match up after de/serialization", - memcmp( - a_Tx->inputs[i].prev_tx_id, - other_tx->inputs[i].prev_tx_id, - TX_HASH_LEN - ) == 0 - ); - mu_assert( - "Input signature length doesn't match up after de/serialization", - a_Tx->inputs[i].sig_len == other_tx->inputs[i].sig_len + assert_memory_equal( + tx->inputs[i].prev_tx_id, + desered_tx->inputs[i].prev_tx_id, + TX_HASH_LEN ); - mu_assert( - "Input signature doesn't match up after de/serialization", - memcmp( - a_Tx->inputs[i].signature, - other_tx->inputs[i].signature, - SIGNATURE_LEN - ) == 0 + assert_memory_equal( + tx->inputs[i].signature, + desered_tx->inputs[i].signature, + SIGNATURE_LEN ); + assert_int_equal(tx->inputs[i].sig_len, desered_tx->inputs[i].sig_len); + + ser_pub_key(buf_1, tx->inputs[i].pub_key); + ser_pub_key(buf_2, desered_tx->inputs[i].pub_key); + assert_memory_equal(buf_1, buf_2, PUB_KEY_SER_LEN); + } - ser_pub_key(buf_1, a_Tx->inputs[i].pub_key); - ser_pub_key(buf_2, other_tx->inputs[i].pub_key); - mu_assert( - "Input pub key doesn't match up after de/serialization", - memcmp(buf_1, buf_2, PUB_KEY_SER_LEN) == 0 + for (size_t i = 0; i < tx->num_outputs; i++) { + assert_int_equal(tx->outputs[i].amt, desered_tx->outputs[i].amt); + assert_memory_equal( + tx->outputs[i].public_key_hash, + desered_tx->outputs[i].public_key_hash, + PUB_KEY_HASH_LEN ); } - mu_assert( - "Outputs doesn't match after de/serialization", - memcmp(a_Tx->outputs, other_tx->outputs, sizeof(Transaction)) == 0 - ); + hash_tx(output_hash, tx); + hash_tx(deser_output_hash, desered_tx); + assert_memory_equal(output_hash, deser_output_hash, TX_HASH_LEN); - unsigned char output_hash[TX_HASH_LEN]; - hash_tx(output_hash, a_Tx); - unsigned char deser_output_hash[TX_HASH_LEN]; - hash_tx(deser_output_hash, other_tx); + free(sered_tx); + free(desered_tx); +} - mu_assert( - "Hashing isn't consistent after de/serialization", - memcmp(output_hash, deser_output_hash, TX_HASH_LEN) == 0 - ); +static void test_ser_tx_fill_buf(void **state) { + Transaction *tx; + unsigned char *sered_tx, *sered_fill_tx; + size_t tx_ser_size; + + tx = *state; + sered_tx = ser_tx_alloc(NULL, tx); + + // Ensure that we fill the entire buffer + tx_ser_size = size_ser_tx(tx); + sered_fill_tx = malloc(tx_ser_size); + for (int i = 0; i < 5; i++) { + memset(sered_fill_tx, i, tx_ser_size); + ser_tx(sered_fill_tx, tx); + assert_memory_equal(sered_tx, sered_fill_tx, tx_ser_size); + } - return NULL; + free(sered_tx); + free(sered_fill_tx); } -static char *test_ser_utxo() { +static void test_ser_tx_pad_bytes(void **state) { + Transaction *tx, *pad_tx; + unsigned char *sered_tx, *sered_pad_tx; + size_t tx_ser_size; + + tx = *state; + sered_tx = ser_tx_alloc(NULL, tx); + + tx_ser_size = size_ser_tx(tx); + sered_pad_tx = malloc(tx_ser_size); + pad_tx = malloc(sizeof(Transaction)); + for (int i = 0; i < 5; i++) { + memset(pad_tx, i, sizeof(Transaction)); + + // Copy everything but padding bytes + // Fill new mallocs with random bytes to simulate new paddinig + pad_tx->num_inputs = tx->num_inputs; + pad_tx->inputs = malloc(tx->num_inputs * sizeof(Input)); + memset(pad_tx->inputs, i, tx->num_inputs * sizeof(Input)); + for(unsigned int j = 0; j < tx->num_inputs; j++){ + pad_tx->inputs[j].pub_key = malloc(sizeof(mbedtls_ecp_point)); + memset(pad_tx->inputs[j].pub_key, i, sizeof(mbedtls_ecp_point)); + mbedtls_ecp_point_init(pad_tx->inputs[j].pub_key); + mbedtls_ecp_copy( + pad_tx->inputs[j].pub_key, + tx->inputs[j].pub_key + ); + + pad_tx->inputs[j].prev_utxo_output = tx->inputs[j].prev_utxo_output; + memcpy( + pad_tx->inputs[j].prev_tx_id, + tx->inputs[j].prev_tx_id, + TX_HASH_LEN + ); + + pad_tx->inputs[j].sig_len = tx->inputs[j].sig_len; + memcpy( + pad_tx->inputs[j].signature, + tx->inputs[j].signature, + pad_tx->inputs[j].sig_len + ); + } + + pad_tx->num_outputs = tx->num_outputs; + pad_tx->outputs = malloc(tx->num_outputs * sizeof(Output)); + memset(pad_tx->outputs, i, tx->num_outputs * sizeof(Output)); + for (unsigned int j = 0; j < tx->num_outputs; j++) { + pad_tx->outputs[j].amt = tx->outputs[j].amt; + memcpy( + pad_tx->outputs[j].public_key_hash, + tx->outputs[j].public_key_hash, + PUB_KEY_HASH_LEN + ); + } + + ser_tx(sered_pad_tx, pad_tx); + assert_memory_equal(sered_tx, sered_pad_tx, tx_ser_size); + } + + free(pad_tx); + free(sered_tx); + free(sered_pad_tx); +} + +static void test_ser_utxo_equal(void **state) { UTXO *utxo, *desered_utxo; - unsigned char *sered_utxo, *sered_utxo_2; + unsigned char *sered_utxo; ssize_t read_ser_utxo, written_ser_utxo; - utxo = _make_utxo(); + utxo = *state; sered_utxo = ser_utxo_alloc(&read_ser_utxo, utxo); desered_utxo = deser_utxo_alloc(&written_ser_utxo, sered_utxo); - mu_assert( - "Num of bytes read incorrect", - read_ser_utxo == UTXO_SER_LEN - ); - mu_assert( - "Num of bytes read and written don't match up", - read_ser_utxo == written_ser_utxo + assert_int_equal(read_ser_utxo, UTXO_SER_LEN); + assert_int_equal(read_ser_utxo, written_ser_utxo); + assert_int_equal(utxo->amt, desered_utxo->amt); + assert_memory_equal( + utxo->public_key_hash, + desered_utxo->public_key_hash, + PUB_KEY_HASH_LEN ); - mu_assert( - "Amount isn't consistent after de-serialization", - utxo->amt == desered_utxo->amt - ); - mu_assert( - "Public key hash isn't consistent after de-serialization", - memcmp( - utxo->public_key_hash, - desered_utxo->public_key_hash, - PUB_KEY_HASH_LEN - ) == 0 - ); + free(sered_utxo); + free(desered_utxo); +} - // Ensure that we don't have padding bytes - for (int i = 0; i < 10; i++) { - sered_utxo_2 = ser_utxo_alloc(NULL, utxo); - mu_assert( - "Serialization of UTXOs isn't consistent", - memcmp(sered_utxo, sered_utxo_2, UTXO_SER_LEN) == 0 - ); - free(sered_utxo_2); +static void test_ser_utxo_fill_buf(void **state) { + UTXO *utxo; + unsigned char *sered_utxo; + unsigned char sered_fill_utxo[UTXO_SER_LEN]; + + utxo = *state; + sered_utxo = ser_utxo_alloc(NULL, utxo); + + // Ensure that we fill the entire buffer + for (int i = 0; i < 5; i++) { + memset(sered_fill_utxo, i, UTXO_SER_LEN); + ser_utxo(sered_fill_utxo, utxo); + assert_memory_equal(sered_utxo, sered_fill_utxo, UTXO_SER_LEN); } - free(utxo); free(sered_utxo); - free(desered_utxo); - - return NULL; } -static char *all_tests() { - mu_run_test(test_ser_tx); - mu_run_test(test_ser_utxo); - return NULL; +static void test_ser_utxo_pad_bytes(void **state) { + UTXO *utxo, *pad_utxo; + unsigned char *sered_utxo; + unsigned char sered_pad_utxo[UTXO_SER_LEN]; + + utxo = *state; + sered_utxo = ser_utxo_alloc(NULL, utxo); + + // Ensure we don't serialize padding bytes + pad_utxo = malloc(sizeof(UTXO)); + for (int i = 0; i < 5; i++) { + memset(pad_utxo, i, sizeof(UTXO)); + pad_utxo->amt = utxo->amt; + memcpy(pad_utxo->public_key_hash, utxo->public_key_hash, PUB_KEY_HASH_LEN); + ser_utxo(sered_pad_utxo, pad_utxo); + assert_memory_equal(sered_utxo, sered_pad_utxo, UTXO_SER_LEN); + } + + free(pad_utxo); + free(sered_utxo); } int main() { - char *result = all_tests(); - if (result != NULL) { - printf("%s\n", result); - } else { - printf("ser_tx.c passing!\n"); - } - printf("Tests run: %d\n", tests_run); - - return result != 0; + const struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown( + test_ser_tx_equal, + fixture_setup_unlinked_tx, + fixture_teardown_unlinked_tx + ), + cmocka_unit_test_setup_teardown( + test_ser_tx_fill_buf, + fixture_setup_unlinked_tx, + fixture_teardown_unlinked_tx + ), + cmocka_unit_test_setup_teardown( + test_ser_tx_pad_bytes, + fixture_setup_unlinked_tx, + fixture_teardown_unlinked_tx + ), + cmocka_unit_test_setup_teardown( + test_ser_utxo_equal, + fixture_setup_unlinked_utxo, + fixture_teardown_unlinked_utxo + ), + cmocka_unit_test_setup_teardown( + test_ser_utxo_fill_buf, + fixture_setup_unlinked_utxo, + fixture_teardown_unlinked_utxo + ), + cmocka_unit_test_setup_teardown( + test_ser_utxo_pad_bytes, + fixture_setup_unlinked_utxo, + fixture_teardown_unlinked_utxo + ) + }; + + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/src/tests/core/txs/test_ser_wallet.c b/src/tests/core/txs/test_ser_wallet.c index 7d839dd..01ad47e 100644 --- a/src/tests/core/txs/test_ser_wallet.c +++ b/src/tests/core/txs/test_ser_wallet.c @@ -2,68 +2,69 @@ #include #include -#include "minunit.h" -#include "ser_tx.h" -#include "ser_key.h" -#include "ser_wallet.h" -#include "crypto.h" - -int tests_run = 0; +#include +#include -WalletEntry *_make_wallet_entry() { - WalletEntry *content; - content = malloc(sizeof(WalletEntry)); - content->amt = 90; - content->key_pair = gen_keys(); - content->spent = 1; - return content; -} +#include "ser_wallet.h" +#include "fixtures_wallet.h" -static char *test_ser_wallet() { - WalletEntry *entry, *desered_entry, *pad_entry; +static void test_ser_wallet_equal(void **state) { + WalletEntry *entry, *desered_entry; unsigned char *sered_entry; - unsigned char sered_entry_2[WALLET_ENTRY_SER_LEN]; - unsigned char ser_pair_1[KEYPAIR_SER_LEN], ser_pair_2[KEYPAIR_SER_LEN]; ssize_t read_ser_entry, written_ser_entry; - entry = _make_wallet_entry(); + entry = *state; sered_entry = ser_wallet_entry_alloc(&read_ser_entry, entry); desered_entry = deser_wallet_entry_alloc(&written_ser_entry, sered_entry); - mu_assert( - "Num of bytes read incorrect", - read_ser_entry == WALLET_ENTRY_SER_LEN - ); - mu_assert( - "Num of bytes read and written don't match up", - read_ser_entry == written_ser_entry - ); - - mu_assert( - "Amount isn't consistent after de-serialization", - entry->amt == desered_entry->amt - ); - mu_assert( - "Spent isn't consistent after de-serialization", - entry->amt == desered_entry->amt - ); - ser_keypair(ser_pair_1, entry->key_pair); - ser_keypair(ser_pair_2, desered_entry->key_pair); - mu_assert( - "Key pair hash isn't consistent after de-serialization", - memcmp(ser_pair_1, ser_pair_2, KEYPAIR_SER_LEN) == 0 - ); + assert_int_equal(read_ser_entry, WALLET_ENTRY_SER_LEN); + assert_int_equal(read_ser_entry, written_ser_entry); + + assert_int_equal(entry->amt, desered_entry->amt); + assert_int_equal(entry->spent, desered_entry->spent); + + assert_true(mbedtls_ecp_point_cmp( + &entry->key_pair->private_Q, + &desered_entry->key_pair->private_Q + ) == 0); + assert_true(mbedtls_mpi_cmp_mpi( + &entry->key_pair->private_d, + &desered_entry->key_pair->private_d + ) == 0); + assert_true(mbedtls_ecp_check_privkey( + &entry->key_pair->private_grp, + &desered_entry->key_pair->private_d + ) == 0); // Proxy for checking group, breaks if group if wrong + + free(sered_entry); + free(desered_entry); +} + +static void test_ser_wallet_fill_buf(void **state) { + WalletEntry *entry; + unsigned char *sered_entry; + unsigned char sered_fill_entry[WALLET_ENTRY_SER_LEN]; + entry = *state; + sered_entry = ser_wallet_entry_alloc(NULL, entry); // Ensure that we are actually filling the serialization buffer for (int i = 0; i < 5; i++) { - memset(sered_entry_2, i, WALLET_ENTRY_SER_LEN); - ser_wallet_entry(sered_entry_2, entry); - mu_assert( - "Entire serialization buffer is not being overwritten", - memcmp(sered_entry, sered_entry_2, WALLET_ENTRY_SER_LEN) == 0 - ); + memset(sered_fill_entry, i, WALLET_ENTRY_SER_LEN); + ser_wallet_entry(sered_fill_entry, entry); + assert_memory_equal(sered_entry, sered_fill_entry, WALLET_ENTRY_SER_LEN); } + free(sered_entry); +} + +static void test_ser_wallet_pad_bytes(void **state) { + WalletEntry *entry, *pad_entry; + unsigned char *sered_entry; + unsigned char sered_pad_entry[WALLET_ENTRY_SER_LEN]; + + entry = *state; + sered_entry = ser_wallet_entry_alloc(NULL, entry); + // Ensure that we don't have padding bytes pad_entry = malloc(sizeof(WalletEntry)); for (int i = 0; i < 5; i++) { @@ -71,35 +72,32 @@ static char *test_ser_wallet() { pad_entry->amt = entry->amt; pad_entry->spent = entry->spent; pad_entry->key_pair = entry->key_pair; - ser_wallet_entry(sered_entry_2, entry); - mu_assert( - "Padding bytes were copied into serialization", - memcmp(sered_entry, sered_entry_2, WALLET_ENTRY_SER_LEN) == 0 - ); + ser_wallet_entry(sered_pad_entry, pad_entry); + assert_memory_equal(sered_entry, sered_pad_entry, WALLET_ENTRY_SER_LEN); } - free(entry); free(pad_entry); free(sered_entry); - free(desered_entry); - - return NULL; -} - -static char *all_tests() { - mu_run_test(test_ser_wallet); - return NULL; } int main() { - create_proj_folders(); - char *result = all_tests(); - if (result != NULL) { - printf("%s\n", result); - } else { - printf("ser_wallet.c passing!\n"); - } - printf("Tests run: %d\n", tests_run); + const struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown( + test_ser_wallet_equal, + fixture_setup_unlinked_wallet_entry, + fixture_teardown_unlinked_wallet_entry + ), + cmocka_unit_test_setup_teardown( + test_ser_wallet_fill_buf, + fixture_setup_unlinked_wallet_entry, + fixture_teardown_unlinked_wallet_entry + ), + cmocka_unit_test_setup_teardown( + test_ser_wallet_pad_bytes, + fixture_setup_unlinked_wallet_entry, + fixture_teardown_unlinked_wallet_entry + ) + }; - return result != 0; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/src/tests/core/txs/test_wallet_pool.c b/src/tests/core/txs/test_wallet_pool.c index d4020ad..4f1e607 100644 --- a/src/tests/core/txs/test_wallet_pool.c +++ b/src/tests/core/txs/test_wallet_pool.c @@ -1,283 +1,246 @@ #include -#include "minunit.h" -#include "wallet_pool.h" -#include "crypto.h" - -int tests_run = 0; - -Transaction *_make_tx() { - Transaction *tx; - Input *in; - Output *out; - mbedtls_ecdsa_context *key_pair; - - tx = malloc(sizeof(Transaction)); - in = malloc(sizeof(Input)); - out = malloc(sizeof(Output)); - - memset(in, 0x25, sizeof(Input)); - key_pair = gen_keys(); - in->pub_key = &(key_pair->private_Q); - memset(out, 0x50, sizeof(Output)); +#include +#include - tx->num_inputs = 1; - tx->inputs = in; - tx->num_outputs = 1; - tx->outputs = out; - - return tx; -} - -void _free_tx(Transaction *tx) { - free(tx->inputs); - free(tx->outputs); - free(tx); -} +#include "wallet_pool.h" +#include "crypto.h" +#include "fixtures_wallet.h" +#include "fixtures_global.h" -static char *test_wallet_init() { - wallet_init_leveldb(TEST_DB_LOC); - destroy_wallet(); +static void test_wallet_init(void **state) { int init_ret = wallet_init_leveldb(TEST_DB_LOC); - unsigned int wallet_count; + unsigned int wallet_count, key_count; + (void)state; wallet_pool_count(&wallet_count); - unsigned int key_count; key_pool_count(&key_count); - mu_assert( - "Init wallet returned failure code", - init_ret == 0 - ); - mu_assert( - "Wallet pool is not empty", - wallet_count == 0 - ); - mu_assert( - "Key pool is not empty", - key_count == 0 - ); + assert_int_equal(init_ret, 0); + assert_int_equal(wallet_count, 0); + assert_int_equal(key_count, 0); destroy_wallet(); - return NULL; } -static char *test_wallet_pool_add() { +static void test_wallet_pool_add(void **state) { Transaction *tx; mbedtls_ecdsa_context *key_pair; - tx = _make_tx(); - key_pair = gen_keys(); - - wallet_init_leveldb(TEST_DB_LOC); + tx = ((void**)*state)[0]; + key_pair = ((void**)*state)[1]; int ret_add = wallet_pool_build_add_leveldb(tx, 0, key_pair); unsigned int wallet_pool_sz; wallet_pool_count(&wallet_pool_sz); - mu_assert( - "Wallet pool add returned error status", - ret_add == 0 - ); - mu_assert( - "Wallet pool not correct size", - wallet_pool_sz == 1 - ); - // Note find tests if the found data is correct - _free_tx(tx); - mbedtls_ecp_keypair_free(key_pair); - destroy_wallet(); - return NULL; + + assert_int_equal(ret_add, 0); + assert_int_equal(wallet_pool_sz, 1); } -static char *test_wallet_pool_find() { +static void test_wallet_pool_find(void **state) { Transaction *tx; WalletEntry *ret_entry; mbedtls_ecdsa_context *key_pair; unsigned char hash[TX_HASH_LEN]; - tx = _make_tx(); + tx = ((void**)*state)[0]; + key_pair = ((void**)*state)[1]; hash_tx(hash, tx); - key_pair = gen_keys(); - - wallet_init_leveldb(TEST_DB_LOC); wallet_pool_build_add_leveldb(tx, 0, key_pair); int ret_find = wallet_pool_find_leveldb(&ret_entry, hash, 0); - mu_assert( - "Wallet pool find returned an error", - ret_find == 0 - ); - mu_assert( - "Wallet amt incorrect", - ret_entry->amt == tx->outputs[0].amt - ); - mu_assert( - "Wallet key pair private Q incorrect", - mbedtls_ecp_point_cmp(&ret_entry->key_pair->private_Q, &key_pair->private_Q) == 0 - ); - mu_assert( - "Wallet key pair private d incorrect", - mbedtls_mpi_cmp_mpi(&ret_entry->key_pair->private_d, &key_pair->private_d) == 0 + + assert_int_equal(ret_find, 0); + assert_int_equal(ret_entry->amt, tx->outputs[0].amt); + assert_int_equal(ret_entry->spent, 0); + assert_true( + mbedtls_ecp_point_cmp( + &ret_entry->key_pair->private_Q, + &key_pair->private_Q + ) == 0 ); - mu_assert( - "Wallet entry spent incorrect", - ret_entry->spent == 0 + assert_true( + mbedtls_mpi_cmp_mpi( + &ret_entry->key_pair->private_d, + &key_pair->private_d + ) == 0 ); - _free_tx(tx); - mbedtls_ecp_keypair_free(key_pair); - destroy_wallet(); - return NULL; + + free_wallet_entry(ret_entry); } -static char *test_wallet_pool_remove() { +static void test_wallet_pool_remove(void **state) { Transaction *tx; WalletEntry *ret_entry = NULL; mbedtls_ecdsa_context *key_pair; unsigned char hash[TX_HASH_LEN]; + unsigned int prev_count, count; - tx = _make_tx(); + tx = ((void**)*state)[0]; + key_pair = ((void**)*state)[1]; hash_tx(hash, tx); - key_pair = gen_keys(); - - wallet_init_leveldb(TEST_DB_LOC); wallet_pool_build_add_leveldb(tx, 0, key_pair); + wallet_pool_count(&prev_count); + int ret_rem = wallet_pool_remove_leveldb(hash, 0); + wallet_pool_count(&count); + assert_int_equal(ret_rem, 0); + assert_int_equal(count, prev_count - 1); + int ret_find = wallet_pool_find_leveldb(&ret_entry, hash, 0); - mu_assert( - "Wallet remove returned error code", - ret_rem == 0 - ); - mu_assert( - "Entry was not removed", - ret_entry == NULL - ); - mu_assert( - "Wallet found deleted entry", - ret_find != 0 - ); - _free_tx(tx); - mbedtls_ecp_keypair_free(key_pair); - destroy_wallet(); - return NULL; + assert_ptr_equal(ret_entry, NULL); + assert_int_not_equal(ret_find, 0); } -static char *test_key_pool_add() { +static void test_key_pool_add(void **state) { mbedtls_ecdsa_context *key_pair; - key_pair = gen_keys(); - - wallet_init_leveldb(TEST_DB_LOC); + key_pair = *state; int ret_add = key_pool_add_leveldb(key_pair); - mu_assert( - "Keypool add returned failure", - ret_add == 0 - ); - mbedtls_ecp_keypair_free(key_pair); - destroy_wallet(); - return NULL; + assert_int_equal(ret_add, 0); } -static char *test_key_pool_find() { +static void test_key_pool_find(void **state) { mbedtls_ecdsa_context *key_pair, *ret_pair; unsigned char hash[PUB_KEY_HASH_LEN]; - key_pair = gen_keys(); + key_pair = *state; hash_pub_key(hash, key_pair); - wallet_init_leveldb(TEST_DB_LOC); - key_pool_add_leveldb(key_pair); int ret_find = key_pool_find_leveldb(&ret_pair, hash); - mu_assert( - "Keypool find returned failure", - ret_find == 0 - ); - mu_assert( - "Key Pool pair private Q incorrect", - mbedtls_ecp_point_cmp(&ret_pair->private_Q, &key_pair->private_Q) == 0 + assert_int_equal(ret_find, 0); + assert_true( + mbedtls_ecp_point_cmp( + &ret_pair->private_Q, + &key_pair->private_Q + ) == 0 ); - mbedtls_ecp_keypair_free(key_pair); - destroy_wallet(); - return NULL; + + mbedtls_ecp_keypair_free(ret_pair); } -static char *test_output_unlockable() { +static void test_output_unlockable(void **state) { Transaction *tx; mbedtls_ecdsa_context *key_pair, *ret_pair; - tx = _make_tx(); - key_pair = gen_keys(); + tx = ((void**)*state)[0]; + key_pair = ((void**)*state)[1]; hash_pub_key(tx->outputs[0].public_key_hash, key_pair); - wallet_init_leveldb(TEST_DB_LOC); - key_pool_add_leveldb(key_pair); ret_pair = check_if_output_unlockable_leveldb(tx, 0); - mu_assert( - "Returned pair is NULL", - ret_pair != NULL + assert_ptr_not_equal(ret_pair, NULL); + assert_true( + mbedtls_ecp_point_cmp( + &ret_pair->private_Q, + &key_pair->private_Q + ) == 0 ); - mu_assert( - "Returned key pair private Q incorrect", - mbedtls_ecp_point_cmp(&ret_pair->private_Q, &key_pair->private_Q) == 0 - ); - mbedtls_ecp_keypair_free(key_pair); - destroy_wallet(); - return NULL; } -static char *test_key_pool_remove() { +static void test_key_pool_remove(void **state) { mbedtls_ecdsa_context *key_pair, *ret_pair = NULL; unsigned char hash[PUB_KEY_HASH_LEN]; + unsigned int prev_count, count; - key_pair = gen_keys(); + key_pair = *state; hash_pub_key(hash, key_pair); - wallet_init_leveldb(TEST_DB_LOC); - key_pool_add_leveldb(key_pair); - int ret_rem = key_pool_remove_leveldb(hash); - mu_assert( - "Remove returned error code", - ret_rem == 0 - ); - int ret_found = key_pool_find_leveldb(&ret_pair, hash); - mu_assert( - "Key pair was found after removed", - ret_found != 0 - ); - mu_assert( - "Key pair return value not null", - ret_pair == NULL - ); + key_pool_count(&prev_count); - mbedtls_ecp_keypair_free(key_pair); - destroy_wallet(); - return NULL; -} + int ret_rem = key_pool_remove_leveldb(hash); + key_pool_count(&count); + assert_int_equal(ret_rem, 0); + assert_int_equal(count, prev_count - 1); -static char *all_tests() { - mu_run_test(test_wallet_init); - mu_run_test(test_wallet_pool_add); - mu_run_test(test_wallet_pool_find); - mu_run_test(test_wallet_pool_remove); - mu_run_test(test_key_pool_add); - mu_run_test(test_key_pool_find); - mu_run_test(test_output_unlockable); - mu_run_test(test_key_pool_remove); - return NULL; + int ret_found = key_pool_find_leveldb(&ret_pair, hash); + assert_int_not_equal(ret_found, 0); + assert_ptr_equal(ret_pair, NULL); } int main() { - create_proj_folders(); - char *result = all_tests(); - if (result != NULL) { - printf("%s\n", result); - } else { - printf("wallet_pool.c passing!\n"); - } - printf("Tests run: %d\n", tests_run); - - return result != 0; + int (*wallet_pool_setup[])(void**) = { + fixture_setup_wallet, + fixture_setup_unlinked_tx_keypair, + NULL + }; + int (*wallet_pool_teardown[])(void**) = { + fixture_teardown_wallet, + fixture_teardown_unlinked_tx_keypair, + NULL + }; + ComposedFixture wallet_pool_composition; + wallet_pool_composition.setup = wallet_pool_setup; + wallet_pool_composition.teardown = wallet_pool_teardown; + + int (*key_pool_setup[])(void**) = { + fixture_setup_wallet, + fixture_setup_unlinked_keypair, + NULL + }; + int (*key_pool_teardown[])(void**) = { + fixture_teardown_wallet, + fixture_teardown_unlinked_keypair, + NULL + }; + ComposedFixture key_pool_composition; + key_pool_composition.setup = key_pool_setup; + key_pool_composition.teardown = key_pool_teardown; + + const struct CMUnitTest tests[] = { + cmocka_unit_test_prestate_setup_teardown( + test_wallet_init, + NULL, + NULL, + NULL + ), + cmocka_unit_test_prestate_setup_teardown( + test_wallet_pool_add, + fixture_setup_compose, + fixture_teardown_compose, + &wallet_pool_composition + ), + cmocka_unit_test_prestate_setup_teardown( + test_wallet_pool_find, + fixture_setup_compose, + fixture_teardown_compose, + &wallet_pool_composition + ), + cmocka_unit_test_prestate_setup_teardown( + test_wallet_pool_remove, + fixture_setup_compose, + fixture_teardown_compose, + &wallet_pool_composition + ), + cmocka_unit_test_prestate_setup_teardown( + test_key_pool_add, + fixture_setup_compose, + fixture_teardown_compose, + &key_pool_composition + ), + cmocka_unit_test_prestate_setup_teardown( + test_key_pool_find, + fixture_setup_compose, + fixture_teardown_compose, + &key_pool_composition + ), + cmocka_unit_test_prestate_setup_teardown( + test_output_unlockable, + fixture_setup_compose, + fixture_teardown_compose, + &wallet_pool_composition + ), + cmocka_unit_test_prestate_setup_teardown( + test_key_pool_remove, + fixture_setup_compose, + fixture_teardown_compose, + &key_pool_composition + ), + }; + + return cmocka_run_group_tests(tests, fixture_clear_wallet, NULL); } diff --git a/src/tests/core/utils/CMakeLists.txt b/src/tests/core/utils/CMakeLists.txt index b21d85e..51b9724 100644 --- a/src/tests/core/utils/CMakeLists.txt +++ b/src/tests/core/utils/CMakeLists.txt @@ -2,13 +2,17 @@ add_executable(test_crypto test_crypto.c) target_compile_options(test_crypto PRIVATE ${COMPILE_OPTIONS_STD}) target_include_directories(test_crypto PUBLIC ${INCLUDE_ALL}) target_link_libraries(test_crypto - crypto) + crypto + cmocka-static) add_executable(test_ser_key test_ser_key.c) target_compile_options(test_ser_key PRIVATE ${COMPILE_OPTIONS_STD}) -target_include_directories(test_ser_key PUBLIC ${INCLUDE_ALL}) +target_include_directories(test_ser_key + PUBLIC ${INCLUDE_ALL} + PUBLIC ${INCLUDE_FIXTURES}) target_link_libraries(test_ser_key - crypto) + crypto + fixtures) add_test(test_crypto ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_crypto) add_test(test_ser_key ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_ser_key) diff --git a/src/tests/core/utils/test_crypto.c b/src/tests/core/utils/test_crypto.c index 0659cde..b154ba9 100644 --- a/src/tests/core/utils/test_crypto.c +++ b/src/tests/core/utils/test_crypto.c @@ -1,67 +1,50 @@ #include #include #include -#include "minunit.h" + +#include +#include + #include "constants.h" #include "crypto.h" -int tests_run = 0; - -static char *test_hash_consistency() { +static void test_hash_consistency(void **state) { unsigned char dest[TX_HASH_LEN]; unsigned char dest2[TX_HASH_LEN]; unsigned char hash_buf[] = "THIS IS A TEST"; + (void)state; hash_sha256(dest, hash_buf, sizeof(hash_buf)); hash_sha256(dest2, hash_buf, sizeof(hash_buf)); - mu_assert( - "Hashing isn't consistent", - memcmp(dest,dest2 , TX_HASH_LEN) == 0 - ); - return NULL; + assert_memory_equal(dest, dest2, TX_HASH_LEN); } -static char *test_hash_consistency_long() { +static void test_hash_consistency_long(void **state) { unsigned char dest[TX_HASH_LEN]; unsigned char dest2[TX_HASH_LEN]; unsigned char hash_buf_long[] = "THdsaflkjkljjklsadflkasdfkjlasdfasdf32145234 b5234 3455432 3245"; + (void)state; hash_sha256(dest, hash_buf_long, sizeof(hash_buf_long)); hash_sha256(dest2, hash_buf_long, sizeof(hash_buf_long)); - mu_assert( - "Hashing isn't consistent with long", - memcmp(dest,dest2 , TX_HASH_LEN) == 0 - ); - return NULL; + assert_memory_equal(dest, dest2, TX_HASH_LEN); } -static char *test_hash_different() { +static void test_hash_different(void **state) { unsigned char dest[TX_HASH_LEN]; unsigned char dest2[TX_HASH_LEN]; unsigned char hash_buf_a[] = "test1"; unsigned char hash_buf_b[] = "test2"; + (void)state; hash_sha256(dest, hash_buf_a, sizeof(hash_buf_a)); hash_sha256(dest2, hash_buf_b, sizeof(hash_buf_b)); - mu_assert( - "Hash collision", - memcmp(dest,dest2 , TX_HASH_LEN) != 0 - ); - return NULL; -} - -static char *all_tests() { - mu_run_test(test_hash_consistency); - mu_run_test(test_hash_consistency_long); - mu_run_test(test_hash_different); - return NULL; + assert_memory_not_equal(dest, dest2, TX_HASH_LEN); } int main() { - char *result = all_tests(); - if (result != NULL) { - printf("%s\n", result); - } else { - printf("crypto.c passing!\n"); - } - printf("Tests run: %d\n", tests_run); + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_hash_consistency), + cmocka_unit_test(test_hash_consistency_long), + cmocka_unit_test(test_hash_different), + }; - return result != 0; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/src/tests/core/utils/test_ser_key.c b/src/tests/core/utils/test_ser_key.c index 4da5f50..d9b047d 100644 --- a/src/tests/core/utils/test_ser_key.c +++ b/src/tests/core/utils/test_ser_key.c @@ -2,158 +2,173 @@ #include #include -#include "minunit.h" -#include "crypto.h" -#include "ser_key.h" +#include +#include -int tests_run = 0; +#include "ser_key.h" +#include "fixtures_wallet.h" -static char *test_ser_pub_key() { +static void test_ser_pub_key_equal(void **state) { mbedtls_ecdsa_context *keypair; mbedtls_ecp_point *pub_key, *desered_pub_key; unsigned char *sered_pub_key; - unsigned char sered_pub_key_2[PUB_KEY_SER_LEN]; ssize_t read, written; - keypair = gen_keys(); + keypair = *state; pub_key = &keypair->private_Q; sered_pub_key = ser_pub_key_alloc(&read, pub_key); desered_pub_key = deser_pub_key_alloc(&written, sered_pub_key); - mu_assert( - "Num of bytes read incorrect", - read == PUB_KEY_SER_LEN - ); - mu_assert( - "Num of bytes read and written don't match up", - read == written - ); + assert_int_equal(read, PUB_KEY_SER_LEN); + assert_int_equal(read, written); + assert_true(mbedtls_ecp_point_cmp(pub_key, desered_pub_key) == 0); - mu_assert( - "Pub key isn't consistent after serialization", - mbedtls_ecp_point_cmp(pub_key, desered_pub_key) == 0 - ); + free(sered_pub_key); + free(desered_pub_key); +} + +static void test_ser_pub_key_fill_buff(void **state) { + mbedtls_ecdsa_context *keypair; + mbedtls_ecp_point *pub_key; + unsigned char *sered_pub_key; + unsigned char sered_fill_pub_key[PUB_KEY_SER_LEN]; + + keypair = *state; + pub_key = &keypair->private_Q; + sered_pub_key = ser_pub_key_alloc(NULL, pub_key); // Ensure that we are actually filling the serialization buffer for (int i = 0; i < 5; i++) { - memset(sered_pub_key_2, i, PUB_KEY_SER_LEN); - ser_pub_key(sered_pub_key_2, pub_key); - mu_assert( - "Entire serialization buffer is not being overwritten", - memcmp(sered_pub_key, sered_pub_key_2, PUB_KEY_SER_LEN) == 0 - ); + memset(sered_fill_pub_key, i, PUB_KEY_SER_LEN); + ser_pub_key(sered_fill_pub_key, pub_key); + assert_memory_equal(sered_pub_key, sered_fill_pub_key, PUB_KEY_SER_LEN); } - mbedtls_ecp_keypair_free(keypair); free(sered_pub_key); - free(desered_pub_key); - - return NULL; } -static char *test_ser_priv_key() { +static void test_ser_priv_key_equal(void **state) { mbedtls_ecdsa_context *keypair; mbedtls_mpi *priv_key, *desered_priv_key; unsigned char *sered_priv_key; - unsigned char sered_priv_key_2[PRIV_KEY_SER_LEN]; ssize_t read, written; - keypair = gen_keys(); + keypair = *state; priv_key = &keypair->private_d; sered_priv_key = ser_priv_key_alloc(&read, priv_key); desered_priv_key = deser_priv_key_alloc(&written, sered_priv_key); - mu_assert( - "Num of bytes read incorrect", - read == PRIV_KEY_SER_LEN - ); - mu_assert( - "Num of bytes read and written don't match up", - read == written - ); + assert_int_equal(read, PRIV_KEY_SER_LEN); + assert_int_equal(read, written); + assert_true(mbedtls_mpi_cmp_mpi(priv_key, desered_priv_key) == 0); - mu_assert( - "Priv key isn't consistent after serialization", - mbedtls_mpi_cmp_mpi(priv_key, desered_priv_key) == 0 - ); + free(sered_priv_key); + free(desered_priv_key); +} + +static void test_ser_priv_key_fill_buf(void **state) { + mbedtls_ecdsa_context *keypair; + mbedtls_mpi *priv_key; + unsigned char *sered_priv_key; + unsigned char sered_fill_priv_key[PRIV_KEY_SER_LEN]; + + keypair = *state; + priv_key = &keypair->private_d; + sered_priv_key = ser_priv_key_alloc(NULL, priv_key); // Ensure that we are actually filling the serialization buffer for (int i = 0; i < 5; i++) { - memset(sered_priv_key_2, i, PRIV_KEY_SER_LEN); - ser_priv_key(sered_priv_key_2, priv_key); - mu_assert( - "Entire serialization buffer is not being overwritten", - memcmp(sered_priv_key, sered_priv_key_2, PRIV_KEY_SER_LEN) == 0 - ); + memset(sered_fill_priv_key, i, PRIV_KEY_SER_LEN); + ser_priv_key(sered_fill_priv_key, priv_key); + assert_memory_equal(sered_priv_key, sered_fill_priv_key, PRIV_KEY_SER_LEN); } - mbedtls_ecp_keypair_free(keypair); free(sered_priv_key); - free(desered_priv_key); - - return NULL; } -static char *test_ser_keypair() { +static void test_ser_keypair_equal(void **state) { mbedtls_ecdsa_context *keypair, *desered_keypair; unsigned char *sered_keypair; - unsigned char sered_keypair_2[KEYPAIR_SER_LEN]; ssize_t read, written; - keypair = gen_keys(); + keypair = *state; sered_keypair = ser_keypair_alloc(&read, keypair); desered_keypair = deser_keypair_alloc(&written, sered_keypair); - mu_assert( - "Num of bytes read incorrect", - read == KEYPAIR_SER_LEN - ); - mu_assert( - "Num of bytes read and written don't match up", - read == written - ); + assert_int_equal(read, KEYPAIR_SER_LEN); + assert_int_equal(read, written); - mu_assert( - "Keypair pub key isn't consistent after serialization", - mbedtls_ecp_point_cmp(&keypair->private_Q, &keypair->private_Q) == 0 + assert_true(mbedtls_ecp_point_cmp( + &keypair->private_Q, + &keypair->private_Q + ) == 0 ); - mu_assert( - "Keypair priv key isn't consistent after serialization", - mbedtls_mpi_cmp_mpi(&keypair->private_d, &desered_keypair->private_d) == 0 + assert_true(mbedtls_mpi_cmp_mpi( + &keypair->private_d, + &desered_keypair->private_d + ) == 0 ); + assert_true(mbedtls_ecp_check_privkey( + &keypair->private_grp, + &keypair->private_d + ) == 0 + ); // Proxy for checking group, breaks if group if wrong + + free(sered_keypair); + free(desered_keypair); +} + +static void test_ser_keypair_fill_buf(void **state) { + mbedtls_ecdsa_context *keypair; + unsigned char *sered_keypair; + unsigned char sered_fill_keypair[KEYPAIR_SER_LEN]; + + keypair = *state; + sered_keypair = ser_keypair_alloc(NULL, keypair); // Ensure that we are actually filling the serialization buffer for (int i = 0; i < 5; i++) { - memset(sered_keypair_2, i, KEYPAIR_SER_LEN); - ser_keypair(sered_keypair_2, keypair); - mu_assert( - "Entire serialization buffer is not being overwritten", - memcmp(sered_keypair, sered_keypair_2, KEYPAIR_SER_LEN) == 0 - ); + memset(sered_fill_keypair, i, KEYPAIR_SER_LEN); + ser_keypair(sered_fill_keypair, keypair); + assert_memory_equal(sered_keypair, sered_fill_keypair, KEYPAIR_SER_LEN); } - mbedtls_ecp_keypair_free(keypair); free(sered_keypair); - free(desered_keypair); - - return NULL; -} - -static char *all_tests() { - mu_run_test(test_ser_pub_key); - mu_run_test(test_ser_priv_key); - mu_run_test(test_ser_keypair); - return NULL; } int main() { - char *result = all_tests(); - if (result != NULL) { - printf("%s\n", result); - } else { - printf("ser_key.c passing!\n"); - } - printf("Tests run: %d\n", tests_run); - - return result != 0; + const struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown( + test_ser_pub_key_equal, + fixture_setup_unlinked_keypair, + fixture_teardown_unlinked_keypair + ), + cmocka_unit_test_setup_teardown( + test_ser_pub_key_fill_buff, + fixture_setup_unlinked_keypair, + fixture_teardown_unlinked_keypair + ), + cmocka_unit_test_setup_teardown( + test_ser_priv_key_equal, + fixture_setup_unlinked_keypair, + fixture_teardown_unlinked_keypair + ), + cmocka_unit_test_setup_teardown( + test_ser_priv_key_fill_buf, + fixture_setup_unlinked_keypair, + fixture_teardown_unlinked_keypair + ), + cmocka_unit_test_setup_teardown( + test_ser_keypair_equal, + fixture_setup_unlinked_keypair, + fixture_teardown_unlinked_keypair + ), + cmocka_unit_test_setup_teardown( + test_ser_keypair_fill_buf, + fixture_setup_unlinked_keypair, + fixture_teardown_unlinked_keypair + ), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/src/tests/includes/CMakeLists.txt b/src/tests/includes/CMakeLists.txt new file mode 100644 index 0000000..2027818 --- /dev/null +++ b/src/tests/includes/CMakeLists.txt @@ -0,0 +1,2 @@ +message(STATUS "In test includes") +add_subdirectory(fixtures) diff --git a/src/tests/includes/fixtures/CMakeLists.txt b/src/tests/includes/fixtures/CMakeLists.txt new file mode 100644 index 0000000..622cbc5 --- /dev/null +++ b/src/tests/includes/fixtures/CMakeLists.txt @@ -0,0 +1,8 @@ +message(STATUS "In test includes fixtures") +set(INCLUDE_FIXTURES + ${CMAKE_CURRENT_SOURCE_DIR} + CACHE INTERNAL "used internally") +# set(INCLUDE_ALL +# ${INCLUDE_ALL}; +# ${CMAKE_CURRENT_SOURCE_DIR} +# CACHE INTERNAL "used internally") diff --git a/src/tests/includes/fixtures/fixtures_block.h b/src/tests/includes/fixtures/fixtures_block.h new file mode 100644 index 0000000..a00be6c --- /dev/null +++ b/src/tests/includes/fixtures/fixtures_block.h @@ -0,0 +1,6 @@ +#pragma once + +int fixture_setup_unlinked_header(void **state); +int fixture_teardown_unlinked_header(void **state); +int fixture_setup_unlinked_block(void **state); +int fixture_teardown_unlinked_block(void **state); diff --git a/src/tests/includes/fixtures/fixtures_global.h b/src/tests/includes/fixtures/fixtures_global.h new file mode 100644 index 0000000..2812246 --- /dev/null +++ b/src/tests/includes/fixtures/fixtures_global.h @@ -0,0 +1,27 @@ +#pragma once + +typedef struct { + int (**setup)(void**); + int (**teardown)(void**); +} ComposedFixture; + +int fixture_setup_compose(void **state); +int fixture_teardown_compose(void **state); + +int fixture_clear_utxo_pool(void **state); +int fixture_setup_utxo_pool(void **state); +int fixture_teardown_utxo_pool(void **state); + +int fixture_clear_wallet(void **state); +int fixture_setup_wallet(void **state); +int fixture_teardown_wallet(void **state); + +int fixture_clear_blockchain(void **state); +int fixture_setup_blockchain(void **state); +int fixture_teardown_blockchain(void **state); + +int fixture_setup_mempool(void **state); +int fixture_teardown_mempool(void **state); + +int fixture_setup_utxo_to_tx(void **state); +int fixture_teardown_utxo_to_tx(void **state); diff --git a/src/tests/includes/fixtures/fixtures_tx.h b/src/tests/includes/fixtures/fixtures_tx.h new file mode 100644 index 0000000..8bc84ce --- /dev/null +++ b/src/tests/includes/fixtures/fixtures_tx.h @@ -0,0 +1,6 @@ +#pragma once + +int fixture_setup_unlinked_tx(void **state); +int fixture_teardown_unlinked_tx(void **state); +int fixture_setup_unlinked_utxo(void **state); +int fixture_teardown_unlinked_utxo(void **state); diff --git a/src/tests/includes/fixtures/fixtures_wallet.h b/src/tests/includes/fixtures/fixtures_wallet.h new file mode 100644 index 0000000..3940ba8 --- /dev/null +++ b/src/tests/includes/fixtures/fixtures_wallet.h @@ -0,0 +1,8 @@ +#pragma once + +int fixture_setup_unlinked_wallet_entry(void **state); +int fixture_teardown_unlinked_wallet_entry(void **state); +int fixture_setup_unlinked_keypair(void **state); +int fixture_teardown_unlinked_keypair(void **state); +int fixture_setup_unlinked_tx_keypair(void **state); +int fixture_teardown_unlinked_tx_keypair(void **state);