2016-12-05 15:50:17 +00:00
|
|
|
#include "ipfs/merkledag/merkledag.h"
|
2016-12-19 14:03:28 +00:00
|
|
|
#include "ipfs/merkledag/node.h"
|
2016-12-05 18:11:22 +00:00
|
|
|
#include "../test_helper.h"
|
2016-12-05 15:50:17 +00:00
|
|
|
|
2016-12-12 11:27:06 +00:00
|
|
|
struct FSRepo* createAndOpenRepo(const char* dir) {
|
|
|
|
int retVal = 1;
|
2016-12-05 22:23:58 +00:00
|
|
|
// create a fresh repo
|
2017-04-13 14:31:58 +00:00
|
|
|
retVal = drop_and_build_repository(dir, 4001, NULL, NULL);
|
2016-12-05 22:23:58 +00:00
|
|
|
if (retVal == 0)
|
2016-12-12 11:27:06 +00:00
|
|
|
return NULL;
|
2016-12-05 22:23:58 +00:00
|
|
|
|
|
|
|
// open the fs repo
|
|
|
|
struct RepoConfig* repo_config = NULL;
|
|
|
|
struct FSRepo* fs_repo;
|
|
|
|
|
|
|
|
// create the struct
|
2016-12-12 11:27:06 +00:00
|
|
|
retVal = ipfs_repo_fsrepo_new(dir, repo_config, &fs_repo);
|
2016-12-05 22:23:58 +00:00
|
|
|
if (retVal == 0)
|
2016-12-12 11:27:06 +00:00
|
|
|
return NULL;
|
2016-12-05 22:23:58 +00:00
|
|
|
|
|
|
|
// open the repository and read the config file
|
|
|
|
retVal = ipfs_repo_fsrepo_open(fs_repo);
|
|
|
|
if (retVal == 0) {
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
2016-12-12 11:27:06 +00:00
|
|
|
return NULL;
|
2016-12-05 22:23:58 +00:00
|
|
|
}
|
2016-12-12 11:27:06 +00:00
|
|
|
return fs_repo;
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_merkledag_get_data() {
|
|
|
|
int retVal = 0;
|
|
|
|
|
|
|
|
struct FSRepo* fs_repo = createAndOpenRepo("/tmp/.ipfs");
|
2016-12-05 22:23:58 +00:00
|
|
|
|
|
|
|
// create data for node
|
|
|
|
size_t binary_data_size = 256;
|
|
|
|
unsigned char binary_data[binary_data_size];
|
|
|
|
for(int i = 0; i < binary_data_size; i++) {
|
|
|
|
binary_data[i] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a node
|
2017-04-20 22:56:03 +00:00
|
|
|
struct HashtableNode* node1;
|
|
|
|
retVal = ipfs_hashtable_node_new_from_data(binary_data, binary_data_size, &node1);
|
2016-12-23 14:37:43 +00:00
|
|
|
size_t bytes_written = 0;
|
|
|
|
retVal = ipfs_merkledag_add(node1, fs_repo, &bytes_written);
|
2016-12-05 22:23:58 +00:00
|
|
|
if (retVal == 0) {
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(node1);
|
2016-12-07 16:53:17 +00:00
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
2016-12-05 22:23:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now retrieve it
|
2017-04-20 22:56:03 +00:00
|
|
|
struct HashtableNode* results_node;
|
2016-12-21 11:40:19 +00:00
|
|
|
retVal = ipfs_merkledag_get(node1->hash, node1->hash_size, &results_node, fs_repo);
|
2016-12-07 16:53:17 +00:00
|
|
|
if (retVal == 0) {
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(node1);
|
|
|
|
ipfs_hashtable_node_free(results_node);
|
2016-12-07 16:53:17 +00:00
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
2016-12-05 22:23:58 +00:00
|
|
|
return 0;
|
2016-12-07 16:53:17 +00:00
|
|
|
}
|
2016-12-05 22:23:58 +00:00
|
|
|
|
2016-12-07 16:53:17 +00:00
|
|
|
if (results_node->data_size != 256) {
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(node1);
|
|
|
|
ipfs_hashtable_node_free(results_node);
|
2016-12-07 16:53:17 +00:00
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
2016-12-05 22:23:58 +00:00
|
|
|
return 0;
|
2016-12-07 16:53:17 +00:00
|
|
|
}
|
2016-12-05 22:23:58 +00:00
|
|
|
|
|
|
|
// the data should be the same
|
|
|
|
for(int i = 0; i < results_node->data_size; i++) {
|
2016-12-07 16:53:17 +00:00
|
|
|
if (results_node->data[i] != node1->data[i]) {
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(node1);
|
|
|
|
ipfs_hashtable_node_free(results_node);
|
2016-12-07 16:53:17 +00:00
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
2016-12-05 22:23:58 +00:00
|
|
|
return 0;
|
2016-12-07 16:53:17 +00:00
|
|
|
}
|
2016-12-05 22:23:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(node1);
|
|
|
|
ipfs_hashtable_node_free(results_node);
|
2016-12-07 16:53:17 +00:00
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
|
|
|
|
2016-12-05 22:23:58 +00:00
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2016-12-05 15:50:17 +00:00
|
|
|
int test_merkledag_add_data() {
|
|
|
|
int retVal = 0;
|
|
|
|
|
2016-12-12 20:06:17 +00:00
|
|
|
struct FSRepo* fs_repo = createAndOpenRepo("/tmp/.ipfs");
|
2016-12-12 11:27:06 +00:00
|
|
|
if (fs_repo == NULL)
|
2016-12-05 18:11:22 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// get the size of the database
|
|
|
|
int start_file_size = os_utils_file_size("/tmp/.ipfs/datastore/data.mdb");
|
|
|
|
|
2016-12-05 15:50:17 +00:00
|
|
|
// create data for node
|
2016-12-05 22:23:58 +00:00
|
|
|
size_t binary_data_size = 256;
|
2016-12-05 15:50:17 +00:00
|
|
|
unsigned char binary_data[binary_data_size];
|
|
|
|
for(int i = 0; i < binary_data_size; i++) {
|
|
|
|
binary_data[i] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a node
|
2017-04-20 22:56:03 +00:00
|
|
|
struct HashtableNode* node1;
|
|
|
|
retVal = ipfs_hashtable_node_new_from_data(binary_data, binary_data_size, &node1);
|
2016-12-05 18:11:22 +00:00
|
|
|
|
2016-12-23 14:37:43 +00:00
|
|
|
size_t bytes_written = 0;
|
|
|
|
retVal = ipfs_merkledag_add(node1, fs_repo, &bytes_written);
|
2016-12-05 19:34:10 +00:00
|
|
|
if (retVal == 0) {
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(node1);
|
2016-12-05 18:11:22 +00:00
|
|
|
return 0;
|
2016-12-05 19:34:10 +00:00
|
|
|
}
|
2016-12-05 15:50:17 +00:00
|
|
|
|
2016-12-05 18:11:22 +00:00
|
|
|
// make sure everything is correct
|
2016-12-21 11:40:19 +00:00
|
|
|
if (node1->hash == NULL)
|
2016-12-05 18:11:22 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
int first_add_size = os_utils_file_size("/tmp/.ipfs/datastore/data.mdb");
|
|
|
|
if (first_add_size == start_file_size) { // uh oh, database should have increased in size
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(node1);
|
2016-12-05 18:11:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// adding the same binary again should do nothing (the hash should be the same)
|
2017-04-20 22:56:03 +00:00
|
|
|
struct HashtableNode* node2;
|
|
|
|
retVal = ipfs_hashtable_node_new_from_data(binary_data, binary_data_size, &node2);
|
2016-12-23 14:37:43 +00:00
|
|
|
retVal = ipfs_merkledag_add(node2, fs_repo, &bytes_written);
|
2016-12-05 19:34:10 +00:00
|
|
|
if (retVal == 0) {
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(node1);
|
|
|
|
ipfs_hashtable_node_free(node2);
|
2016-12-05 15:50:17 +00:00
|
|
|
return 0;
|
2016-12-05 19:34:10 +00:00
|
|
|
}
|
2016-12-05 15:50:17 +00:00
|
|
|
|
|
|
|
// make sure everything is correct
|
2016-12-21 11:40:19 +00:00
|
|
|
if (node2->hash == NULL) {
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(node1);
|
|
|
|
ipfs_hashtable_node_free(node2);
|
2016-12-05 15:50:17 +00:00
|
|
|
return 0;
|
2016-12-05 19:34:10 +00:00
|
|
|
}
|
2016-12-21 11:40:19 +00:00
|
|
|
for(int i = 0; i < node1->hash_size; i++) {
|
|
|
|
if (node1->hash[i] != node2->hash[i]) {
|
2016-12-05 18:11:22 +00:00
|
|
|
printf("hash of node1 does not match node2 at position %d\n", i);
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(node1);
|
|
|
|
ipfs_hashtable_node_free(node2);
|
2016-12-05 18:11:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2016-12-05 15:50:17 +00:00
|
|
|
|
2016-12-05 18:11:22 +00:00
|
|
|
int second_add_size = os_utils_file_size("/tmp/.ipfs/datastore/data.mdb");
|
|
|
|
if (first_add_size != second_add_size) { // uh oh, the database shouldn't have changed size
|
2016-12-05 19:34:10 +00:00
|
|
|
printf("looks as if a new record was added when it shouldn't have. Old file size: %d, new file size: %d\n", first_add_size, second_add_size);
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(node1);
|
|
|
|
ipfs_hashtable_node_free(node2);
|
2016-12-05 18:11:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now change 1 byte, which should change the hash
|
|
|
|
binary_data[10] = 0;
|
|
|
|
// create a node
|
2017-04-20 22:56:03 +00:00
|
|
|
struct HashtableNode* node3;
|
|
|
|
retVal = ipfs_hashtable_node_new_from_data(binary_data, binary_data_size, &node3);
|
2016-12-05 18:11:22 +00:00
|
|
|
|
2016-12-23 14:37:43 +00:00
|
|
|
retVal = ipfs_merkledag_add(node3, fs_repo, &bytes_written);
|
2016-12-05 19:34:10 +00:00
|
|
|
if (retVal == 0) {
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(node1);
|
|
|
|
ipfs_hashtable_node_free(node2);
|
|
|
|
ipfs_hashtable_node_free(node3);
|
2016-12-05 18:11:22 +00:00
|
|
|
return 0;
|
2016-12-05 19:34:10 +00:00
|
|
|
}
|
2016-12-05 18:11:22 +00:00
|
|
|
|
|
|
|
// make sure everything is correct
|
2016-12-21 11:40:19 +00:00
|
|
|
if (node3->hash == NULL) {
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(node1);
|
|
|
|
ipfs_hashtable_node_free(node2);
|
|
|
|
ipfs_hashtable_node_free(node3);
|
2016-12-05 18:11:22 +00:00
|
|
|
return 0;
|
2016-12-05 19:34:10 +00:00
|
|
|
}
|
2016-12-05 18:11:22 +00:00
|
|
|
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(node1);
|
|
|
|
ipfs_hashtable_node_free(node2);
|
|
|
|
ipfs_hashtable_node_free(node3);
|
2016-12-05 18:11:22 +00:00
|
|
|
int third_add_size = os_utils_file_size("/tmp/.ipfs/datastore/data.mdb");
|
|
|
|
if (third_add_size == second_add_size || third_add_size < second_add_size) {// uh oh, it didn't add it
|
|
|
|
printf("Node 3 should have been added, but the file size did not change from %d.\n", third_add_size);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-12-05 15:50:17 +00:00
|
|
|
|
2016-12-05 19:34:10 +00:00
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
|
|
|
|
2016-12-05 15:50:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2016-12-12 11:27:06 +00:00
|
|
|
|
2016-12-15 10:40:24 +00:00
|
|
|
int test_merkledag_add_node() {
|
|
|
|
int retVal = 0;
|
2017-04-20 22:56:03 +00:00
|
|
|
struct HashtableNode* node1 = NULL;
|
2016-12-15 10:40:24 +00:00
|
|
|
|
|
|
|
struct FSRepo* fs_repo = createAndOpenRepo("/tmp/.ipfs");
|
|
|
|
if (fs_repo == NULL) {
|
|
|
|
printf("Unable to create repo\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-20 22:56:03 +00:00
|
|
|
retVal = ipfs_hashtable_node_new(&node1);
|
2016-12-15 10:40:24 +00:00
|
|
|
if (retVal == 0) {
|
|
|
|
printf("Unable to make node\n");
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-23 14:37:43 +00:00
|
|
|
size_t bytes_written = 0;
|
|
|
|
retVal = ipfs_merkledag_add(node1, fs_repo, &bytes_written);
|
2016-12-15 10:40:24 +00:00
|
|
|
if (retVal == 0) {
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(node1);
|
2016-12-15 10:40:24 +00:00
|
|
|
printf("Unable to add node\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(node1);
|
2016-12-15 10:40:24 +00:00
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-12-12 11:27:06 +00:00
|
|
|
/**
|
|
|
|
* Should save links
|
|
|
|
*/
|
|
|
|
int test_merkledag_add_node_with_links() {
|
|
|
|
int retVal = 0;
|
2016-12-14 11:25:09 +00:00
|
|
|
struct NodeLink* link = NULL;
|
2017-04-20 22:56:03 +00:00
|
|
|
struct HashtableNode* node1 = NULL;
|
|
|
|
struct HashtableNode* node2 = NULL;
|
2016-12-12 11:27:06 +00:00
|
|
|
|
|
|
|
struct FSRepo* fs_repo = createAndOpenRepo("/tmp/.ipfs");
|
|
|
|
if (fs_repo == NULL) {
|
|
|
|
printf("Unable to create repo\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make link
|
2016-12-21 11:40:19 +00:00
|
|
|
retVal = ipfs_node_link_create("", (unsigned char*)"abc123", 6, &link);
|
2016-12-15 10:40:24 +00:00
|
|
|
if (retVal == 0) {
|
|
|
|
printf("Unable to make new link\n");
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-04-20 22:56:03 +00:00
|
|
|
retVal = ipfs_hashtable_node_new_from_link(link, &node1);
|
2016-12-15 10:40:24 +00:00
|
|
|
if (retVal == 0) {
|
|
|
|
printf("Unable to make node\n");
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-12-12 11:27:06 +00:00
|
|
|
|
2016-12-23 14:37:43 +00:00
|
|
|
size_t bytes_written = 0;
|
|
|
|
retVal = ipfs_merkledag_add(node1, fs_repo, &bytes_written);
|
2016-12-12 11:27:06 +00:00
|
|
|
if (retVal == 0) {
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(node1);
|
2016-12-12 11:27:06 +00:00
|
|
|
printf("Unable to add node\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2016-12-12 20:06:17 +00:00
|
|
|
|
2016-12-12 11:27:06 +00:00
|
|
|
// now look for it
|
2016-12-21 11:40:19 +00:00
|
|
|
retVal = ipfs_merkledag_get(node1->hash, node1->hash_size, &node2, fs_repo);
|
2016-12-12 11:27:06 +00:00
|
|
|
if (retVal == 0) {
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(node1);
|
2016-12-12 11:27:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-12 21:58:27 +00:00
|
|
|
struct NodeLink* node1_link = node1->head_link;
|
|
|
|
struct NodeLink* node2_link = node2->head_link;
|
2016-12-15 10:40:24 +00:00
|
|
|
|
2016-12-21 11:40:19 +00:00
|
|
|
if (node1_link->hash_size != node2_link->hash_size) {
|
|
|
|
printf("Hashes are not of the same length. Hash1: %lu, Hash2: %lu\n", node1_link->hash_size, node2_link->hash_size);
|
2016-12-15 10:40:24 +00:00
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(node1);
|
|
|
|
ipfs_hashtable_node_free(node2);
|
2016-12-15 10:40:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-12-12 21:58:27 +00:00
|
|
|
while(node1_link != NULL) {
|
2016-12-21 11:40:19 +00:00
|
|
|
for(int i = 0; i < node1_link->hash_size; i++) {
|
|
|
|
if(node1_link->hash[i] != node2_link->hash[i]) {
|
2016-12-12 21:58:27 +00:00
|
|
|
printf("Hashes do not match for node %s\n", node1_link->name);
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(node1);
|
|
|
|
ipfs_hashtable_node_free(node2);
|
2016-12-12 21:58:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-12-12 11:27:06 +00:00
|
|
|
}
|
2016-12-12 21:58:27 +00:00
|
|
|
node1_link = node1_link->next;
|
|
|
|
node2_link = node2_link->next;
|
2016-12-12 11:27:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_hashtable_node_free(node1);
|
|
|
|
ipfs_hashtable_node_free(node2);
|
2016-12-12 11:27:06 +00:00
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|