Some tweaks to avoid potential problems found during unit testing

This commit is contained in:
John Jones 2017-07-20 16:10:31 -05:00
parent f47a6116f0
commit 2bb70b01be
4 changed files with 103 additions and 12 deletions

View file

@ -156,9 +156,11 @@ int ipfs_blocks_block_add_data(const unsigned char* data, size_t data_size, stru
* @returns true(1) on success
*/
int ipfs_blocks_block_free(struct Block* block) {
if (block != NULL) {
ipfs_cid_free(block->cid);
if (block->data != NULL)
free(block->data);
free(block);
}
return 1;
}

View file

@ -129,9 +129,11 @@ int ipfs_cid_new(int version, unsigned char* hash, size_t hash_length, const cha
* @returns 1
*/
int ipfs_cid_free(struct Cid* cid) {
if (cid != NULL) {
if (cid->hash != NULL)
free(cid->hash);
free(cid);
}
return 1;
}

View file

@ -391,7 +391,6 @@ int ipfs_bitswap_wantlist_protobuf_decode(unsigned char* buffer, size_t buffer_l
list->entries = libp2p_utils_vector_new(1);
}
libp2p_utils_vector_add(list->entries, (void*)entry);
free(entry);
pos += bytes_read;
break;
}
@ -593,7 +592,6 @@ int ipfs_bitswap_message_protobuf_decode(unsigned char* buffer, size_t buffer_le
message->blocks = libp2p_utils_vector_new(1);
}
libp2p_utils_vector_add(message->blocks, (void*)temp);
ipfs_blocks_block_free(temp);
pos += bytes_read;
break;
}
@ -615,7 +613,6 @@ int ipfs_bitswap_message_protobuf_decode(unsigned char* buffer, size_t buffer_le
message->payload = libp2p_utils_vector_new(1);
}
libp2p_utils_vector_add(message->payload, (void*)block);
ipfs_blocks_block_free(block);
pos += bytes_read;
break;
}

View file

@ -1,16 +1,106 @@
#include "ipfs/exchange/bitswap/message.h"
#include "libp2p/utils/vector.h"
uint8_t* generate_bytes(size_t size) {
uint8_t* buffer = (uint8_t*) malloc(size);
for(size_t i = 0; i < size; i++) {
buffer[i] = i % 255;
}
return buffer;
}
int compare_generated_bytes(uint8_t* buffer, size_t size) {
for(size_t i = 0; i < size; i++) {
if (buffer[i] != (i % 255)) {
fprintf(stderr, "compare_generated_bytes: Mismatch in position %lu", i);
return 0;
}
}
return 1;
}
/**
* Test the protobufing of a BitswapMessage
* this should be run with valgrind
*/
int test_bitswap_new_free() {
int retVal = 0;
struct BitswapMessage* message = NULL;
struct WantlistEntry* wantlist_entry = NULL;
struct Block* block = NULL;
// create a simple BitswapMessage
struct BitswapMessage* message = ipfs_bitswap_message_new();
// Test 1, create a simple BitswapMessage
message = ipfs_bitswap_message_new();
ipfs_bitswap_message_free(message);
message = NULL;
// Test 2, okay, that worked, now make one more complicated
message = ipfs_bitswap_message_new();
message->wantlist = ipfs_bitswap_wantlist_new();
ipfs_bitswap_message_free(message);
message = NULL;
// Test 3, now add some more
message = ipfs_bitswap_message_new();
// wantlist
message->wantlist = ipfs_bitswap_wantlist_new();
wantlist_entry = ipfs_bitswap_wantlist_entry_new();
wantlist_entry->priority = 24;
message->wantlist->entries = libp2p_utils_vector_new(1);
libp2p_utils_vector_add(message->wantlist->entries, wantlist_entry);
wantlist_entry = NULL;
wantlist_entry = libp2p_utils_vector_get(message->wantlist->entries, 0);
if (wantlist_entry == NULL) {
fprintf(stderr, "Vector didn't have entry\n");
goto exit;
}
if (wantlist_entry->priority != 24) {
fprintf(stderr, "Unable to retrieve item from vector after an external free\n");
goto exit;
}
// payload
message->payload = libp2p_utils_vector_new(1);
block = ipfs_blocks_block_new();
block->data_length = 25;
libp2p_utils_vector_add(message->payload, block);
block = libp2p_utils_vector_get(message->payload, 0);
if (block == NULL) {
fprintf(stderr, "Vector didn't have payload entry\n");
goto exit;
}
if (block->data_length != 25) {
fprintf(stderr, "Unable to retrieve block->data_length\n");
}
retVal = 1;
exit:
if (message != NULL)
{
ipfs_bitswap_message_free(message);
}
return retVal;
}
int test_bitswap_protobuf() {
int retVal = 0;
// create a complicated BitswapMessage
// first the basics...
struct BitswapMessage* message = ipfs_bitswap_message_new();
// add a WantList
struct BitswapWantlist* want_list = ipfs_bitswap_wantlist_new();
message->wantlist = want_list;
// add something to the WantList
message->wantlist->entries = libp2p_utils_vector_new(1);
struct WantlistEntry* wantlist_entry = ipfs_bitswap_wantlist_entry_new();
wantlist_entry->block_size = 1;
wantlist_entry->priority = 100;
wantlist_entry->block = generate_bytes(100);
wantlist_entry->block_size = 100;
libp2p_utils_vector_add(message->wantlist->entries, wantlist_entry);
message->wantlist->full = 1;
retVal = 1;
exit: