Skip to content

Commit

Permalink
Added test deserialize
Browse files Browse the repository at this point in the history
Signed-off-by: leonard.kosta <[email protected]>
  • Loading branch information
kostaleonard committed Feb 16, 2024
1 parent 62b8b20 commit b1ce807
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/blockchain.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions tests/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
53 changes: 52 additions & 1 deletion tests/test_blockchain.c
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 6 additions & 0 deletions tests/test_blockchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit b1ce807

Please sign in to comment.