2016-12-19 14:03:28 +00:00
|
|
|
#include "ipfs/merkledag/node.h"
|
2016-12-05 11:13:20 +00:00
|
|
|
|
|
|
|
int test_node() {
|
|
|
|
//Variables of link:
|
|
|
|
char * name = "Alex";
|
2016-12-12 21:58:27 +00:00
|
|
|
unsigned char * ahash = (unsigned char*)"QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";
|
2016-12-12 20:06:17 +00:00
|
|
|
struct NodeLink * mylink;
|
2016-12-21 11:40:19 +00:00
|
|
|
int retVal = ipfs_node_link_create(name,ahash, strlen((char*)ahash), &mylink);
|
2016-12-14 11:39:06 +00:00
|
|
|
if (retVal == 0)
|
|
|
|
return 0;
|
2016-12-12 21:58:27 +00:00
|
|
|
|
2016-12-05 11:13:20 +00:00
|
|
|
//Link Two for testing purposes
|
|
|
|
char * name2 = "Simo";
|
2016-12-12 21:58:27 +00:00
|
|
|
unsigned char * ahash2 = (unsigned char*)"QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnSimo";
|
2016-12-12 20:06:17 +00:00
|
|
|
struct NodeLink * mylink2;
|
2016-12-21 11:40:19 +00:00
|
|
|
retVal = ipfs_node_link_create(name2, ahash2, strlen((char*)ahash2), &mylink2);
|
2016-12-12 21:58:27 +00:00
|
|
|
|
2016-12-05 11:13:20 +00:00
|
|
|
//Nodes
|
2017-04-20 22:56:03 +00:00
|
|
|
struct HashtableNode * Mynode;
|
|
|
|
retVal = ipfs_hashtable_node_new_from_link(mylink, &Mynode);
|
2016-12-12 21:58:27 +00:00
|
|
|
//mylink->name = "HAHA";//Testing for valid node creation
|
2017-04-20 22:56:03 +00:00
|
|
|
retVal = ipfs_hashtable_node_add_link(Mynode, mylink2);
|
2016-12-12 21:58:27 +00:00
|
|
|
//mylink2->name = "HAHA";//Testing for valid node creation
|
2016-12-14 11:25:09 +00:00
|
|
|
//struct NodeLink * ResultLink = ipfs_node_get_link_by_name(Mynode, "Simo");
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_remove_link_by_name("Simo", Mynode);
|
|
|
|
ipfs_hashtable_node_free(Mynode);
|
2016-12-05 23:17:17 +00:00
|
|
|
return 1;
|
2016-12-05 11:13:20 +00:00
|
|
|
}
|
2016-12-12 20:06:17 +00:00
|
|
|
|
|
|
|
int compare_link(struct NodeLink* link1, struct NodeLink* link2) {
|
|
|
|
if (strcmp(link1->name, link2->name) != 0) {
|
|
|
|
printf("Link Names are different %s vs. %s\n", link1->name, link2->name);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-12-21 11:40:19 +00:00
|
|
|
if (link1->hash_size != link2->hash_size) {
|
|
|
|
printf("Link cid hash lengths are different. Expected %d but got %d\n", (int)link1->hash_size, (int)link2->hash_size);
|
2016-12-12 20:06:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-12-21 11:40:19 +00:00
|
|
|
if (memcmp(link1->hash, link2->hash, link1->hash_size) != 0) {
|
2016-12-12 20:06:17 +00:00
|
|
|
printf("compare_link: The values of the hashes are different\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_node_link_encode_decode() {
|
|
|
|
struct NodeLink* control = NULL;
|
|
|
|
struct NodeLink* results = NULL;
|
|
|
|
size_t nl_size;
|
|
|
|
unsigned char* buffer = NULL;
|
|
|
|
int retVal = 0;
|
|
|
|
|
|
|
|
// make a NodeLink
|
2016-12-21 11:40:19 +00:00
|
|
|
if (ipfs_node_link_create("My Name", (unsigned char*)"QmMyHash", 8, &control) == 0)
|
2016-12-12 23:27:46 +00:00
|
|
|
goto l_exit;
|
2016-12-12 20:06:17 +00:00
|
|
|
|
|
|
|
// encode it
|
|
|
|
nl_size = ipfs_node_link_protobuf_encode_size(control);
|
|
|
|
buffer = malloc(nl_size);
|
|
|
|
if (buffer == NULL)
|
2016-12-12 23:27:46 +00:00
|
|
|
goto l_exit;
|
2016-12-12 20:06:17 +00:00
|
|
|
if (ipfs_node_link_protobuf_encode(control, buffer, nl_size, &nl_size) == 0) {
|
2016-12-12 23:27:46 +00:00
|
|
|
goto l_exit;
|
2016-12-12 20:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// decode it
|
2016-12-21 11:40:19 +00:00
|
|
|
if (ipfs_node_link_protobuf_decode(buffer, nl_size, &results) == 0) {
|
2016-12-12 23:27:46 +00:00
|
|
|
goto l_exit;
|
2016-12-12 20:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// verify it
|
|
|
|
if (compare_link(control, results) == 0)
|
2016-12-12 23:27:46 +00:00
|
|
|
goto l_exit;
|
2016-12-12 20:06:17 +00:00
|
|
|
retVal = 1;
|
2016-12-12 23:27:46 +00:00
|
|
|
l_exit:
|
2016-12-12 20:06:17 +00:00
|
|
|
if (control != NULL)
|
|
|
|
ipfs_node_link_free(control);
|
|
|
|
if (results != NULL)
|
|
|
|
ipfs_node_link_free(results);
|
2016-12-14 11:39:06 +00:00
|
|
|
if (buffer != NULL)
|
|
|
|
free(buffer);
|
2016-12-12 20:06:17 +00:00
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Test a node with 2 links
|
|
|
|
*/
|
|
|
|
int test_node_encode_decode() {
|
2017-04-20 22:56:03 +00:00
|
|
|
struct HashtableNode* control = NULL;
|
|
|
|
struct HashtableNode* results = NULL;
|
2016-12-12 20:06:17 +00:00
|
|
|
struct NodeLink* link1 = NULL;
|
|
|
|
struct NodeLink* link2 = NULL;
|
|
|
|
int retVal = 0;
|
|
|
|
size_t buffer_length = 0;
|
|
|
|
unsigned char* buffer = NULL;
|
|
|
|
|
|
|
|
// node
|
2017-04-20 22:56:03 +00:00
|
|
|
if (ipfs_hashtable_node_new(&control) == 0)
|
2016-12-12 23:27:46 +00:00
|
|
|
goto ed_exit;
|
2016-12-12 20:06:17 +00:00
|
|
|
|
|
|
|
// first link
|
2016-12-21 11:40:19 +00:00
|
|
|
if (ipfs_node_link_create((char*)"Link1", (unsigned char*)"QmLink1", 7, &link1) == 0)
|
2016-12-12 23:27:46 +00:00
|
|
|
goto ed_exit;
|
2016-12-12 20:06:17 +00:00
|
|
|
|
2017-04-20 22:56:03 +00:00
|
|
|
if ( ipfs_hashtable_node_add_link(control, link1) == 0)
|
2016-12-12 23:27:46 +00:00
|
|
|
goto ed_exit;
|
2016-12-12 20:06:17 +00:00
|
|
|
|
|
|
|
// second link
|
2016-12-21 11:40:19 +00:00
|
|
|
if (ipfs_node_link_create((char*)"Link2", (unsigned char*)"QmLink2", 7, &link2) == 0)
|
2016-12-12 23:27:46 +00:00
|
|
|
goto ed_exit;
|
2017-04-20 22:56:03 +00:00
|
|
|
if ( ipfs_hashtable_node_add_link(control, link2) == 0)
|
2016-12-12 23:27:46 +00:00
|
|
|
goto ed_exit;
|
2016-12-12 20:06:17 +00:00
|
|
|
// encode
|
2017-04-20 22:56:03 +00:00
|
|
|
buffer_length = ipfs_hashtable_node_protobuf_encode_size(control);
|
2016-12-12 20:06:17 +00:00
|
|
|
buffer = (unsigned char*)malloc(buffer_length);
|
2017-04-20 22:56:03 +00:00
|
|
|
if (ipfs_hashtable_node_protobuf_encode(control, buffer, buffer_length, &buffer_length) == 0)
|
2016-12-12 23:27:46 +00:00
|
|
|
goto ed_exit;
|
2016-12-12 20:06:17 +00:00
|
|
|
|
|
|
|
// decode
|
2017-04-20 22:56:03 +00:00
|
|
|
if (ipfs_hashtable_node_protobuf_decode(buffer, buffer_length, &results) == 0)
|
2016-12-12 23:27:46 +00:00
|
|
|
goto ed_exit;
|
2016-12-12 20:06:17 +00:00
|
|
|
|
|
|
|
// compare results
|
|
|
|
|
2016-12-12 21:58:27 +00:00
|
|
|
struct NodeLink* control_link = control->head_link;
|
|
|
|
struct NodeLink* results_link = results->head_link;
|
|
|
|
while(control_link != NULL) {
|
|
|
|
if (compare_link(control_link, results_link) == 0) {
|
|
|
|
printf("Error was on link %s\n", control_link->name);
|
2016-12-12 23:27:46 +00:00
|
|
|
goto ed_exit;
|
2016-12-12 20:06:17 +00:00
|
|
|
}
|
2016-12-12 21:58:27 +00:00
|
|
|
control_link = control_link->next;
|
|
|
|
results_link = results_link->next;
|
2016-12-12 20:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (control->data_size != results->data_size)
|
2016-12-12 23:27:46 +00:00
|
|
|
goto ed_exit;
|
2016-12-12 20:06:17 +00:00
|
|
|
|
|
|
|
if (memcmp(results->data, control->data, control->data_size) != 0) {
|
2016-12-12 23:27:46 +00:00
|
|
|
goto ed_exit;
|
2016-12-12 20:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
retVal = 1;
|
2016-12-12 23:27:46 +00:00
|
|
|
ed_exit:
|
2016-12-12 20:06:17 +00:00
|
|
|
// clean up
|
|
|
|
if (control != NULL)
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(control);
|
2016-12-12 20:06:17 +00:00
|
|
|
if (results != NULL)
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(results);
|
2016-12-12 20:06:17 +00:00
|
|
|
if (buffer != NULL)
|
|
|
|
free(buffer);
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|