More work on unixfs protobuf

This commit is contained in:
jmjatlanta 2016-12-19 17:21:21 -05:00
parent 57ed4fd5e4
commit a654022d32
4 changed files with 55 additions and 5 deletions

View file

@ -41,8 +41,8 @@ struct UnixFSBlockSizeNode {
struct UnixFS {
enum UnixFSDataType data_type;
unsigned char* bytes; // an array of bytes
size_t bytes_size; // the size of the bytes array
unsigned char* bytes; // an array of bytes
size_t file_size; // the file size
struct UnixFSBlockSizeNode* block_size_head; // a linked list of block sizes
};
@ -84,7 +84,7 @@ size_t ipfs_unixfs_protobuf_encode_size(struct UnixFS* obj);
* @param bytes_written how many bytes were written in the buffer
* @returns true(1) on success
*/
int ipfs_unixfs_protobuf_encode(struct UnixFS* incoming, unsigned char* outgoing, size_t max_buffer_size, size_t* bytes_written);
int ipfs_unixfs_protobuf_encode(const struct UnixFS* incoming, unsigned char* outgoing, size_t max_buffer_size, size_t* bytes_written);
/***
* Decodes a protobuf array of bytes into a UnixFS object

View file

@ -36,3 +36,51 @@ int test_unixfs_encode_decode() {
return 1;
}
int test_unixfs_encode_smallfile() {
struct UnixFS* unixfs = NULL;
ipfs_unixfs_new(&unixfs);
unsigned char bytes[] = {
0x54, 0x68, 0x69, 0x73, 0x20,
0x69, 0x73, 0x20, 0x74, 0x65,
0x78, 0x74, 0x20, 0x77, 0x69,
0x74, 0x68, 0x69, 0x6e, 0x20,
0x48, 0x65, 0x6c, 0x6c, 0x6f,
0x57, 0x65, 0x72, 0x6c, 0x64,
0x2e, 0x74, 0x78, 0x74, 0x0a };
unsigned char expected_results[] = {
0x08, 0x02, 0x12, 0x23,
0x54, 0x68, 0x69, 0x73, 0x20,
0x69, 0x73, 0x20, 0x74, 0x65,
0x78, 0x74, 0x20, 0x77, 0x69,
0x74, 0x68, 0x69, 0x6e, 0x20,
0x48, 0x65, 0x6c, 0x6c, 0x6f,
0x57, 0x65, 0x72, 0x6c, 0x64,
0x2e, 0x74, 0x78, 0x74, 0x0a,
0x18, 0x23
};
unixfs->bytes = (unsigned char*)malloc(35);
memcpy(unixfs->bytes, bytes, 35);
unixfs->bytes_size = 35;
unixfs->data_type = UNIXFS_FILE;
unixfs->file_size = 35;
size_t protobuf_size = 43;
unsigned char protobuf[protobuf_size];
size_t bytes_written;
ipfs_unixfs_protobuf_encode(unixfs, protobuf, protobuf_size, &bytes_written);
if (bytes_written != 41) {
printf("Length should be %lu, but is %lu\n", 41LU, bytes_written);
}
for(int i = 0; i < bytes_written; i++) {
if (expected_results[i] != protobuf[i]) {
printf("Byte at position %d should be %02x but is %02x\n", i, expected_results[i], protobuf[i]);
}
}
return 1;
}

View file

@ -52,7 +52,8 @@ const char* names[] = {
"test_merkledag_get_data",
"test_merkledag_add_node",
"test_merkledag_add_node_with_links",
"test_unixfs_encode_decode"
"test_unixfs_encode_decode",
"test_unixfs_encode_smallfile"
};
int (*funcs[])(void) = {
@ -84,7 +85,8 @@ int (*funcs[])(void) = {
test_merkledag_get_data,
test_merkledag_add_node,
test_merkledag_add_node_with_links,
test_unixfs_encode_decode
test_unixfs_encode_decode,
test_unixfs_encode_smallfile
};
/**

View file

@ -93,7 +93,7 @@ size_t ipfs_unixfs_protobuf_encode_size(struct UnixFS* obj) {
* @param bytes_written how many bytes were written in the buffer
* @returns true(1) on success
*/
int ipfs_unixfs_protobuf_encode(struct UnixFS* incoming, unsigned char* outgoing, size_t max_buffer_size, size_t* bytes_written) {
int ipfs_unixfs_protobuf_encode(const struct UnixFS* incoming, unsigned char* outgoing, size_t max_buffer_size, size_t* bytes_written) {
size_t bytes_used = 0;
*bytes_written = 0;
int retVal = 0;