Some tweaks to avoid potential problems found during unit testing
This commit is contained in:
parent
f47a6116f0
commit
2bb70b01be
4 changed files with 103 additions and 12 deletions
|
@ -156,9 +156,11 @@ int ipfs_blocks_block_add_data(const unsigned char* data, size_t data_size, stru
|
||||||
* @returns true(1) on success
|
* @returns true(1) on success
|
||||||
*/
|
*/
|
||||||
int ipfs_blocks_block_free(struct Block* block) {
|
int ipfs_blocks_block_free(struct Block* block) {
|
||||||
ipfs_cid_free(block->cid);
|
if (block != NULL) {
|
||||||
if (block->data != NULL)
|
ipfs_cid_free(block->cid);
|
||||||
free(block->data);
|
if (block->data != NULL)
|
||||||
free(block);
|
free(block->data);
|
||||||
|
free(block);
|
||||||
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -129,9 +129,11 @@ int ipfs_cid_new(int version, unsigned char* hash, size_t hash_length, const cha
|
||||||
* @returns 1
|
* @returns 1
|
||||||
*/
|
*/
|
||||||
int ipfs_cid_free(struct Cid* cid) {
|
int ipfs_cid_free(struct Cid* cid) {
|
||||||
if (cid->hash != NULL)
|
if (cid != NULL) {
|
||||||
free(cid->hash);
|
if (cid->hash != NULL)
|
||||||
free(cid);
|
free(cid->hash);
|
||||||
|
free(cid);
|
||||||
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -391,7 +391,6 @@ int ipfs_bitswap_wantlist_protobuf_decode(unsigned char* buffer, size_t buffer_l
|
||||||
list->entries = libp2p_utils_vector_new(1);
|
list->entries = libp2p_utils_vector_new(1);
|
||||||
}
|
}
|
||||||
libp2p_utils_vector_add(list->entries, (void*)entry);
|
libp2p_utils_vector_add(list->entries, (void*)entry);
|
||||||
free(entry);
|
|
||||||
pos += bytes_read;
|
pos += bytes_read;
|
||||||
break;
|
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);
|
message->blocks = libp2p_utils_vector_new(1);
|
||||||
}
|
}
|
||||||
libp2p_utils_vector_add(message->blocks, (void*)temp);
|
libp2p_utils_vector_add(message->blocks, (void*)temp);
|
||||||
ipfs_blocks_block_free(temp);
|
|
||||||
pos += bytes_read;
|
pos += bytes_read;
|
||||||
break;
|
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);
|
message->payload = libp2p_utils_vector_new(1);
|
||||||
}
|
}
|
||||||
libp2p_utils_vector_add(message->payload, (void*)block);
|
libp2p_utils_vector_add(message->payload, (void*)block);
|
||||||
ipfs_blocks_block_free(block);
|
|
||||||
pos += bytes_read;
|
pos += bytes_read;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,106 @@
|
||||||
#include "ipfs/exchange/bitswap/message.h"
|
#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
|
* Test the protobufing of a BitswapMessage
|
||||||
* this should be run with valgrind
|
* this should be run with valgrind
|
||||||
*/
|
*/
|
||||||
int test_bitswap_new_free() {
|
int test_bitswap_new_free() {
|
||||||
int retVal = 0;
|
int retVal = 0;
|
||||||
|
struct BitswapMessage* message = NULL;
|
||||||
|
struct WantlistEntry* wantlist_entry = NULL;
|
||||||
|
struct Block* block = NULL;
|
||||||
|
|
||||||
// create a simple BitswapMessage
|
// Test 1, create a simple BitswapMessage
|
||||||
struct BitswapMessage* message = ipfs_bitswap_message_new();
|
message = ipfs_bitswap_message_new();
|
||||||
ipfs_bitswap_message_free(message);
|
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;
|
retVal = 1;
|
||||||
exit:
|
exit:
|
||||||
|
|
Loading…
Reference in a new issue