2016-12-05 15:50:17 +00:00
|
|
|
#include "ipfs/merkledag/merkledag.h"
|
|
|
|
#include "ipfs/node/node.h"
|
2016-12-05 18:11:22 +00:00
|
|
|
#include "../test_helper.h"
|
2016-12-05 15:50:17 +00:00
|
|
|
|
2016-12-05 22:23:58 +00:00
|
|
|
int test_merkledag_get_data() {
|
|
|
|
int retVal = 0;
|
|
|
|
|
|
|
|
// create a fresh repo
|
|
|
|
retVal = drop_and_build_repository("/tmp/.ipfs");
|
|
|
|
if (retVal == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// open the fs repo
|
|
|
|
struct RepoConfig* repo_config = NULL;
|
|
|
|
struct FSRepo* fs_repo;
|
|
|
|
const char* path = "/tmp/.ipfs";
|
|
|
|
|
|
|
|
// create the struct
|
|
|
|
retVal = ipfs_repo_fsrepo_new((char*)path, repo_config, &fs_repo);
|
|
|
|
if (retVal == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// open the repository and read the config file
|
|
|
|
retVal = ipfs_repo_fsrepo_open(fs_repo);
|
|
|
|
if (retVal == 0) {
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the size of the database
|
|
|
|
int start_file_size = os_utils_file_size("/tmp/.ipfs/datastore/data.mdb");
|
|
|
|
|
|
|
|
// 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
|
|
|
|
struct Node* node1 = N_Create_From_Data(binary_data, 256);
|
|
|
|
|
|
|
|
retVal = ipfs_merkledag_add(node1, fs_repo);
|
|
|
|
if (retVal == 0) {
|
|
|
|
Node_Delete(node1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now retrieve it
|
|
|
|
struct Node* results_node;
|
|
|
|
retVal = ipfs_merkledag_get(node1->cached, &results_node, fs_repo);
|
|
|
|
if (retVal == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (results_node->data_size != 256)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// the data should be the same
|
|
|
|
for(int i = 0; i < results_node->data_size; i++) {
|
|
|
|
if (results_node->data[i] != node1->data[i])
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2016-12-05 15:50:17 +00:00
|
|
|
int test_merkledag_add_data() {
|
|
|
|
int retVal = 0;
|
|
|
|
|
2016-12-05 18:11:22 +00:00
|
|
|
// create a fresh repo
|
|
|
|
retVal = drop_and_build_repository("/tmp/.ipfs");
|
|
|
|
if (retVal == 0)
|
|
|
|
return 0;
|
|
|
|
|
2016-12-05 15:50:17 +00:00
|
|
|
// open the fs repo
|
2016-12-05 18:11:22 +00:00
|
|
|
struct RepoConfig* repo_config = NULL;
|
2016-12-05 15:50:17 +00:00
|
|
|
struct FSRepo* fs_repo;
|
|
|
|
const char* path = "/tmp/.ipfs";
|
|
|
|
|
|
|
|
// create the struct
|
|
|
|
retVal = ipfs_repo_fsrepo_new((char*)path, repo_config, &fs_repo);
|
|
|
|
if (retVal == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// open the repository and read the config file
|
|
|
|
retVal = ipfs_repo_fsrepo_open(fs_repo);
|
|
|
|
if (retVal == 0) {
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-05 18:11:22 +00:00
|
|
|
// 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
|
2016-12-05 22:23:58 +00:00
|
|
|
struct Node* node1 = N_Create_From_Data(binary_data, 256);
|
2016-12-05 18:11:22 +00:00
|
|
|
|
|
|
|
retVal = ipfs_merkledag_add(node1, fs_repo);
|
2016-12-05 19:34:10 +00:00
|
|
|
if (retVal == 0) {
|
|
|
|
Node_Delete(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
|
|
|
|
if (node1->cached == NULL)
|
|
|
|
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
|
2016-12-05 19:34:10 +00:00
|
|
|
Node_Delete(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)
|
|
|
|
struct Node* node2 = N_Create_From_Data(binary_data, 255);
|
|
|
|
retVal = ipfs_merkledag_add(node2, fs_repo);
|
2016-12-05 19:34:10 +00:00
|
|
|
if (retVal == 0) {
|
|
|
|
Node_Delete(node1);
|
|
|
|
Node_Delete(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-05 19:34:10 +00:00
|
|
|
if (node2->cached == NULL) {
|
|
|
|
Node_Delete(node1);
|
|
|
|
Node_Delete(node2);
|
2016-12-05 15:50:17 +00:00
|
|
|
return 0;
|
2016-12-05 19:34:10 +00:00
|
|
|
}
|
2016-12-05 18:11:22 +00:00
|
|
|
for(int i = 0; i < node1->cached->hash_length; i++) {
|
|
|
|
if (node1->cached->hash[i] != node2->cached->hash[i]) {
|
|
|
|
printf("hash of node1 does not match node2 at position %d\n", i);
|
2016-12-05 19:34:10 +00:00
|
|
|
Node_Delete(node1);
|
|
|
|
Node_Delete(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);
|
|
|
|
Node_Delete(node1);
|
|
|
|
Node_Delete(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
|
|
|
|
struct Node* node3 = N_Create_From_Data(binary_data, 255);
|
|
|
|
|
|
|
|
retVal = ipfs_merkledag_add(node3, fs_repo);
|
2016-12-05 19:34:10 +00:00
|
|
|
if (retVal == 0) {
|
|
|
|
Node_Delete(node1);
|
|
|
|
Node_Delete(node2);
|
|
|
|
Node_Delete(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-05 19:34:10 +00:00
|
|
|
if (node3->cached == NULL) {
|
|
|
|
Node_Delete(node1);
|
|
|
|
Node_Delete(node2);
|
|
|
|
Node_Delete(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
|
|
|
|
|
|
|
Node_Delete(node1);
|
|
|
|
Node_Delete(node2);
|
|
|
|
Node_Delete(node3);
|
|
|
|
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;
|
|
|
|
}
|