Adding basic unit tests for Bitswap protobuf

This commit is contained in:
jmjatlanta 2017-07-20 15:16:59 -05:00
parent 2232d03854
commit f47a6116f0
3 changed files with 182 additions and 0 deletions

View file

@ -40,3 +40,164 @@ struct BitswapMessage {
struct Libp2pVector* payload;
};
/***
* Allocate memory for a struct BitswapBlock
* @returns a new BitswapBlock
*/
struct BitswapBlock* ipfs_bitswap_block_new();
/**
* Deallocate memory for a struct BitswapBlock
* @param block the block to deallocate
* @returns true(1)
*/
int ipfs_bitswap_block_free(struct BitswapBlock* block);
/**
* Retrieve an estimate of the size of a protobuf'd BitswapBlock
* @returns the approximate (maximum actually) size of a protobuf'd BitswapBlock
*/
size_t ipfs_bitswap_message_block_protobuf_size(struct BitswapBlock* block);
/***
* Encode a BitswapBlock
* @param incoming the block to encode
* @param outgoing where to place the results
* @param max_size the maximum allocated space for outgoing
* @param bytes_written the number of bytes written to outgoing
*/
int ipfs_bitswap_message_block_protobuf_encode(struct BitswapBlock* incoming, uint8_t* outgoing, size_t max_size, size_t* bytes_written);
/***
* Decode a protobuf to a BitswapBlock
* @param buffer the incoming protobuf
* @param buffer_length the length of the incoming protobuf buffer
* @param output a pointer to the BitswapBlock that will be allocated
* @returns true(1) on success, false(0) if not. If false, any memory was deallocated
*/
int ipfs_bitswap_message_block_protobuf_decode(uint8_t* buffer, size_t buffer_length, struct BitswapBlock** output);
/***
* Allocate memory for a new WantlistEntry
* @returns the newly allocated WantlistEntry
*/
struct WantlistEntry* ipfs_bitswap_wantlist_entry_new();
/***
* Free allocations of a WantlistEntry
* @param entry the WantlistEntry
* @returns true(1)
*/
int ipfs_bitswap_wantlist_entry_free(struct WantlistEntry* entry);
/**
* Retrieve an estimate of the size of a protobuf'd WantlistEntry
* @param entry the struct to examine
* @returns the approximate (maximum actually) size of a protobuf'd WantlistEntry
*/
size_t ipfs_bitswap_wantlist_entry_protobuf_encode_size(struct WantlistEntry* entry);
/***
* Encode a WantlistEntry into a Protobuf
* @param entry the WantlistEntry to encode
* @param buffer where to put the results
* @param buffer_length the maximum size of the buffer
* @param bytes_written the number of bytes written into the buffer
* @returns true(1) on success, false(0) otherwise
*/
int ipfs_bitswap_wantlist_entry_protobuf_encode(struct WantlistEntry* entry, unsigned char* buffer, size_t buffer_length, size_t* bytes_written);
/***
* Decode a protobuf into a struct WantlistEntry
* @param buffer the protobuf buffer
* @param buffer_length the length of the data in the protobuf buffer
* @param output the resultant WantlistEntry
* @returns true(1) on success, otherwise false(0)
*/
int ipfs_bitswap_wantlist_entry_protobuf_decode(unsigned char* buffer, size_t buffer_length, struct WantlistEntry** output);
/***
* Allocate memory for a new Bitswap Message WantList
* @returns the allocated struct BitswapWantlist
*/
struct BitswapWantlist* ipfs_bitswap_wantlist_new();
/**
* Free the resources used by a Wantlist
* @param list the list to free
* @returns true(1)
*/
int ipfs_bitswap_wantlist_free(struct BitswapWantlist* list);
/***
* Calculate the maximum size of a protobuf'd BitswapWantlist
* @param list the Wantlist
* @returns the maximum size of the protobuf'd list
*/
size_t ipfs_bitswap_wantlist_protobuf_encode_size(struct BitswapWantlist* list);
/***
* Encode a BitswapWantlist into a protobuf buffer
* @param list the list to encode
* @param buffer the buffer to fill
* @param buffer_length the length of the allocated buffer
* @param bytes_written the total number of bytes written to the buffer
* @returns true(1) on success, otherwise false(0)
*/
int ipfs_bitswap_wantlist_protobuf_encode(struct BitswapWantlist* list, unsigned char* buffer, size_t buffer_length, size_t* bytes_written);
/***
* Decode a Wantlist from a protobuf
* @param buffer the protobuf
* @param buffer_length the length of the protobuf
* @param output the newly allocated BitswapWantlist
* @returns true(1) on success, otherwise false(0)
*/
int ipfs_bitswap_wantlist_protobuf_decode(unsigned char* buffer, size_t buffer_length, struct BitswapWantlist** output);
/***
* Bitswap Message
*
*/
/***
* Allocate memory for a new Bitswap Message
* @returns the allocated struct BitswapMessage
*/
struct BitswapMessage* ipfs_bitswap_message_new();
/**
* Free the resources used by a BitswapMessage
* @param message the BitswapMessage to free
* @returns true(1)
*/
int ipfs_bitswap_message_free(struct BitswapMessage* message);
/***
* Calculate the maximum size of a protobuf'd BitswapMessage
* @param message the BitswapMessage
* @returns the maximum size of the protobuf'd BitswapMessage
*/
size_t ipfs_bitswap_message_protobuf_encode_size(struct BitswapMessage* message);
/***
* Encode a BitswapMessage into a protobuf buffer
* @param message the message to encode
* @param buffer the buffer to fill
* @param buffer_length the length of the allocated buffer
* @param bytes_written the total number of bytes written to the buffer
* @returns true(1) on success, otherwise false(0)
*/
int ipfs_bitswap_message_protobuf_encode(struct BitswapMessage* message, unsigned char* buffer, size_t buffer_length, size_t* bytes_written);
/***
* Decode a BitswapMessage from a protobuf
* @param buffer the protobuf
* @param buffer_length the length of the protobuf
* @param output the newly allocated BitswapMessage
* @returns true(1) on success, otherwise false(0)
*/
int ipfs_bitswap_message_protobuf_decode(unsigned char* buffer, size_t buffer_length, struct BitswapMessage** output);

View file

@ -0,0 +1,18 @@
#include "ipfs/exchange/bitswap/message.h"
/**
* Test the protobufing of a BitswapMessage
* this should be run with valgrind
*/
int test_bitswap_new_free() {
int retVal = 0;
// create a simple BitswapMessage
struct BitswapMessage* message = ipfs_bitswap_message_new();
ipfs_bitswap_message_free(message);
retVal = 1;
exit:
return retVal;
}

View file

@ -1,5 +1,6 @@
#include "cid/test_cid.h"
#include "cmd/ipfs/test_init.h"
#include "exchange/test_bitswap.h"
#include "flatfs/test_flatfs.h"
#include "merkledag/test_merkledag.h"
#include "node/test_node.h"
@ -32,6 +33,7 @@ int testit(const char* name, int (*func)(void)) {
}
const char* names[] = {
"test_bitswap_new_free",
"test_cid_new_free",
"test_cid_cast_multihash",
"test_cid_cast_non_multihash",
@ -82,6 +84,7 @@ const char* names[] = {
};
int (*funcs[])(void) = {
test_bitswap_new_free,
test_cid_new_free,
test_cid_cast_multihash,
test_cid_cast_non_multihash,