diff --git a/src/blockchain.c b/src/blockchain.c index 9fbe702..c35cf41 100644 --- a/src/blockchain.c +++ b/src/blockchain.c @@ -335,7 +335,7 @@ return_code_t blockchain_serialize( return_code_t blockchain_deserialize( blockchain_t **blockchain, unsigned char *buffer, - uint64_t buffer_size // TODO get rid of buffer_size? I don't think we need it. + uint64_t buffer_size // TODO if we ever read past buffer_size, fail ) { return_code_t return_code = SUCCESS; if (NULL == blockchain || NULL == buffer) { @@ -356,7 +356,6 @@ return_code_t blockchain_deserialize( uint64_t num_blocks = betoh64(*(uint64_t *)next_spot_in_buffer); next_spot_in_buffer += sizeof(num_blocks); for (uint64_t block_idx = 0; block_idx < num_blocks; block_idx++) { - // TODO deserialize block block_t *block = NULL; time_t block_created_at = betoh64(*(uint64_t *)next_spot_in_buffer); next_spot_in_buffer += sizeof(block_created_at); diff --git a/tests/main.c b/tests/main.c index 10c3805..8c054e5 100644 --- a/tests/main.c +++ b/tests/main.c @@ -146,6 +146,10 @@ int main(int argc, char **argv) { // TODO put these in the correct order cmocka_unit_test(test_blockchain_serialize_creates_nonempty_buffer), cmocka_unit_test(test_blockchain_serialize_fails_on_invalid_arguments), + cmocka_unit_test(test_blockchain_deserialize_reconstructs_blockchain), + cmocka_unit_test( + test_blockchain_deserialize_fails_on_attempted_read_past_buffer), + cmocka_unit_test(test_blockchain_deserialize_fails_on_invalid_input), cmocka_unit_test(test_blockchain_write_to_file_creates_nonempty_file), cmocka_unit_test(test_blockchain_write_to_file_fails_on_invalid_input), cmocka_unit_test( diff --git a/tests/test_blockchain.c b/tests/test_blockchain.c index 053880b..bfcc39d 100644 --- a/tests/test_blockchain.c +++ b/tests/test_blockchain.c @@ -324,11 +324,62 @@ void test_blockchain_serialize_fails_on_invalid_arguments() { } void test_blockchain_deserialize_reconstructs_blockchain() { - // TODO use fixture here so you have some actual data + // See test_blockchain_read_from_file_reconstructs_blockchain. Since + // blockchain_read_from_file calls blockchain_deserialize, the + // aforementioned test already covers deserialization fairly well. Here we + // will just test on a single-block blockchain to preserve compute and add + // diversity to the tests. + blockchain_t *blockchain = NULL; + return_code_t return_code = blockchain_create( + &blockchain, NUM_LEADING_ZERO_BYTES_IN_BLOCK_HASH); + assert_true(SUCCESS == return_code); + block_t *genesis_block = NULL; + return_code = block_create_genesis_block(&genesis_block); + assert_true(SUCCESS == return_code); + return_code = blockchain_add_block(blockchain, genesis_block); + assert_true(SUCCESS == return_code); + unsigned char *buffer = NULL; + uint64_t buffer_size = 0; + return_code = blockchain_serialize(blockchain, &buffer, &buffer_size); + assert_true(SUCCESS == return_code); + blockchain_t *deserialized_blockchain = NULL; + return_code = blockchain_deserialize( + &deserialized_blockchain, buffer, buffer_size); + assert_true(SUCCESS == return_code); + assert_true(NULL != deserialized_blockchain); + uint64_t length = 0; + return_code = linked_list_length( + deserialized_blockchain->block_list, &length); + assert_true(SUCCESS == return_code); + assert_true(1 == length); + block_t *deserialized_genesis_block = + (block_t *)deserialized_blockchain->block_list->head->data; + assert_true( + genesis_block->created_at == deserialized_genesis_block->created_at); + assert_true(0 == memcmp( + &genesis_block->previous_block_hash, + &deserialized_genesis_block->previous_block_hash, + sizeof(sha_256_t))); + assert_true( + genesis_block->proof_of_work == + deserialized_genesis_block->proof_of_work); + bool is_empty = false; + return_code = linked_list_is_empty( + deserialized_genesis_block->transaction_list, &is_empty); + assert_true(SUCCESS == return_code); + assert_true(is_empty); + free(buffer); + blockchain_destroy(blockchain); +} + +void test_blockchain_deserialize_fails_on_attempted_read_past_buffer() { + // TODO + assert_true(false); } void test_blockchain_deserialize_fails_on_invalid_input() { // TODO + assert_true(false); } void test_blockchain_write_to_file_creates_nonempty_file() { diff --git a/tests/test_blockchain.h b/tests/test_blockchain.h index 796ca28..80fec0e 100644 --- a/tests/test_blockchain.h +++ b/tests/test_blockchain.h @@ -35,6 +35,12 @@ void test_blockchain_serialize_creates_nonempty_buffer(); void test_blockchain_serialize_fails_on_invalid_arguments(); +void test_blockchain_deserialize_reconstructs_blockchain(); + +void test_blockchain_deserialize_fails_on_attempted_read_past_buffer(); + +void test_blockchain_deserialize_fails_on_invalid_input(); + void test_blockchain_write_to_file_creates_nonempty_file(); void test_blockchain_write_to_file_fails_on_invalid_input();