2016-12-14 17:07:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2017-03-19 12:47:19 +00:00
|
|
|
#include "../test_helper.h"
|
2016-12-14 17:07:43 +00:00
|
|
|
#include "ipfs/importer/importer.h"
|
2016-12-15 17:38:08 +00:00
|
|
|
#include "ipfs/importer/exporter.h"
|
2016-12-14 17:07:43 +00:00
|
|
|
#include "ipfs/merkledag/merkledag.h"
|
2016-12-15 17:38:08 +00:00
|
|
|
#include "mh/hashes.h"
|
|
|
|
#include "mh/multihash.h"
|
|
|
|
#include "libp2p/crypto/encoding/base58.h"
|
2017-04-20 22:56:03 +00:00
|
|
|
#include "ipfs/core/ipfs_node.h"
|
2017-09-07 23:45:09 +00:00
|
|
|
#include "ipfs/repo/fsrepo/lmdb_cursor.h"
|
2016-12-14 17:07:43 +00:00
|
|
|
|
2016-12-15 10:40:24 +00:00
|
|
|
int test_import_large_file() {
|
|
|
|
size_t bytes_size = 1000000; //1mb
|
|
|
|
unsigned char file_bytes[bytes_size];
|
|
|
|
const char* fileName = "/tmp/test_import_large.tmp";
|
2017-04-20 22:56:03 +00:00
|
|
|
const char* repo_dir = "/tmp/.ipfs";
|
|
|
|
struct IpfsNode* local_node = NULL;
|
2017-05-11 12:04:54 +00:00
|
|
|
int retVal = 0;
|
|
|
|
// cid should be the same each time
|
|
|
|
unsigned char cid_test[10] = { 0xc1 ,0x69 ,0x68 ,0x22, 0xfa, 0x47, 0x16, 0xe2, 0x41, 0xa1 };
|
|
|
|
struct HashtableNode* read_node = NULL;
|
|
|
|
struct HashtableNode* write_node = NULL;
|
|
|
|
struct HashtableNode* read_node2 = NULL;
|
|
|
|
size_t bytes_written = 0;
|
|
|
|
size_t base58_size = 55;
|
|
|
|
unsigned char base58[base58_size];
|
|
|
|
size_t bytes_read1 = 1;
|
|
|
|
size_t bytes_read2 = 1;
|
|
|
|
unsigned char buf1[100];
|
|
|
|
unsigned char buf2[100];
|
2016-12-15 10:40:24 +00:00
|
|
|
|
|
|
|
// create the necessary file
|
|
|
|
create_bytes(file_bytes, bytes_size);
|
|
|
|
create_file(fileName, file_bytes, bytes_size);
|
|
|
|
|
|
|
|
// get the repo
|
2017-04-27 16:35:26 +00:00
|
|
|
if (!drop_and_build_repository(repo_dir, 4001, NULL, NULL)) {
|
|
|
|
fprintf(stderr, "Unable to drop and build test repository at %s\n", repo_dir);
|
2017-05-11 12:04:54 +00:00
|
|
|
goto exit;
|
2017-04-27 16:35:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 22:56:03 +00:00
|
|
|
if (!ipfs_node_online_new(repo_dir, &local_node)) {
|
2017-04-27 16:35:26 +00:00
|
|
|
fprintf(stderr, "Unable to create new IpfsNode\n");
|
2017-05-11 12:04:54 +00:00
|
|
|
goto exit;
|
2017-04-20 22:56:03 +00:00
|
|
|
}
|
2016-12-15 10:40:24 +00:00
|
|
|
|
|
|
|
// write to ipfs
|
2017-04-20 22:56:03 +00:00
|
|
|
if (ipfs_import_file("/tmp", fileName, &write_node, local_node, &bytes_written, 1) == 0) {
|
2017-05-11 12:04:54 +00:00
|
|
|
goto exit;
|
2016-12-15 10:40:24 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 17:38:08 +00:00
|
|
|
for(int i = 0; i < 10; i++) {
|
2016-12-21 11:40:19 +00:00
|
|
|
if (write_node->hash[i] != cid_test[i]) {
|
|
|
|
printf("Hashes should be the same each time, and do not match at position %d, should be %02x but is %02x\n", i, cid_test[i], write_node->hash[i]);
|
2017-05-11 12:04:54 +00:00
|
|
|
goto exit;
|
2016-12-15 17:38:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 10:40:24 +00:00
|
|
|
// make sure all went okay
|
2017-04-20 22:56:03 +00:00
|
|
|
if (ipfs_merkledag_get(write_node->hash, write_node->hash_size, &read_node, local_node->repo) == 0) {
|
2017-05-11 12:04:54 +00:00
|
|
|
goto exit;
|
2016-12-15 10:40:24 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 17:38:08 +00:00
|
|
|
// the second block should be there
|
2017-04-20 22:56:03 +00:00
|
|
|
if (ipfs_merkledag_get(read_node->head_link->hash, read_node->head_link->hash_size, &read_node2, local_node->repo) == 0) {
|
2016-12-15 17:38:08 +00:00
|
|
|
printf("Unable to find the linked node.\n");
|
2017-05-11 12:04:54 +00:00
|
|
|
goto exit;
|
2016-12-15 17:38:08 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 10:40:24 +00:00
|
|
|
// compare data
|
|
|
|
if (write_node->data_size != read_node->data_size) {
|
|
|
|
printf("Data size of nodes are not equal. Should be %lu but are %lu\n", write_node->data_size, read_node->data_size);
|
2017-05-11 12:04:54 +00:00
|
|
|
goto exit;
|
2016-12-15 10:40:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < write_node->data_size; i++) {
|
|
|
|
if (write_node->data[i] != read_node->data[i]) {
|
|
|
|
printf("Data within node is different at position %d. The value should be %02x, but was %02x.\n", i, write_node->data[i], read_node->data[i]);
|
2017-05-11 12:04:54 +00:00
|
|
|
goto exit;
|
2016-12-15 10:40:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 17:38:08 +00:00
|
|
|
// convert cid to multihash
|
2016-12-21 11:40:19 +00:00
|
|
|
if ( ipfs_cid_hash_to_base58(read_node->hash, read_node->hash_size, base58, base58_size) == 0) {
|
2016-12-15 17:38:08 +00:00
|
|
|
printf("Unable to convert cid to multihash\n");
|
2017-05-11 12:04:54 +00:00
|
|
|
goto exit;
|
2016-12-15 17:38:08 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 10:40:24 +00:00
|
|
|
// attempt to write file
|
2017-04-27 16:35:26 +00:00
|
|
|
if (ipfs_exporter_to_file(base58, "/tmp/test_import_large_file.rsl", local_node) == 0) {
|
2016-12-15 17:38:08 +00:00
|
|
|
printf("Unable to write file.\n");
|
2017-05-11 12:04:54 +00:00
|
|
|
goto exit;
|
2016-12-15 17:38:08 +00:00
|
|
|
}
|
2016-12-15 10:40:24 +00:00
|
|
|
|
|
|
|
// compare original with new
|
2016-12-15 17:38:08 +00:00
|
|
|
size_t new_file_size = os_utils_file_size("/tmp/test_import_large_file.rsl");
|
|
|
|
if (new_file_size != bytes_size) {
|
|
|
|
printf("File sizes are different. Should be %lu but the new one is %lu\n", bytes_size, new_file_size);
|
2017-05-11 12:04:54 +00:00
|
|
|
goto exit;
|
2016-12-19 14:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FILE* f1 = fopen("/tmp/test_import_large.tmp", "rb");
|
|
|
|
FILE* f2 = fopen("/tmp/test_import_large_file.rsl", "rb");
|
|
|
|
|
|
|
|
// compare bytes of files
|
|
|
|
while (bytes_read1 != 0 && bytes_read2 != 0) {
|
|
|
|
bytes_read1 = fread(buf1, 1, 100, f1);
|
|
|
|
bytes_read2 = fread(buf2, 1, 100, f2);
|
|
|
|
if (bytes_read1 != bytes_read2) {
|
|
|
|
printf("Error reading files for comparison. Read %lu bytes of file 1, but %lu bytes of file 2\n", bytes_read1, bytes_read2);
|
|
|
|
fclose(f1);
|
|
|
|
fclose(f2);
|
2017-05-11 12:04:54 +00:00
|
|
|
goto exit;
|
2016-12-19 14:03:28 +00:00
|
|
|
}
|
|
|
|
if (memcmp(buf1, buf2, bytes_read1) != 0) {
|
|
|
|
printf("The bytes between the files are different\n");
|
|
|
|
fclose(f1);
|
|
|
|
fclose(f2);
|
2017-05-11 12:04:54 +00:00
|
|
|
goto exit;
|
2016-12-19 14:03:28 +00:00
|
|
|
}
|
2016-12-15 17:38:08 +00:00
|
|
|
}
|
2016-12-15 10:40:24 +00:00
|
|
|
|
2017-05-11 12:13:13 +00:00
|
|
|
fclose(f1);
|
|
|
|
fclose(f2);
|
|
|
|
|
2017-05-11 12:04:54 +00:00
|
|
|
retVal = 1;
|
|
|
|
exit:
|
|
|
|
|
|
|
|
if (local_node != NULL)
|
|
|
|
ipfs_node_free(local_node);
|
|
|
|
if (write_node != NULL)
|
|
|
|
ipfs_hashtable_node_free(write_node);
|
|
|
|
if (read_node != NULL)
|
|
|
|
ipfs_hashtable_node_free(read_node);
|
|
|
|
if (read_node2 != NULL)
|
|
|
|
ipfs_hashtable_node_free(read_node2);
|
2016-12-15 10:40:24 +00:00
|
|
|
|
2017-05-11 12:04:54 +00:00
|
|
|
return retVal;
|
2016-12-15 10:40:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-12-14 17:07:43 +00:00
|
|
|
int test_import_small_file() {
|
|
|
|
size_t bytes_size = 1000;
|
|
|
|
unsigned char file_bytes[bytes_size];
|
|
|
|
const char* fileName = "/tmp/test_import_small.tmp";
|
2017-04-20 22:56:03 +00:00
|
|
|
const char* repo_path = "/tmp/.ipfs";
|
|
|
|
struct IpfsNode *local_node = NULL;
|
2016-12-14 17:07:43 +00:00
|
|
|
|
|
|
|
// create the necessary file
|
|
|
|
create_bytes(file_bytes, bytes_size);
|
|
|
|
create_file(fileName, file_bytes, bytes_size);
|
|
|
|
|
|
|
|
// get the repo
|
2017-04-20 22:56:03 +00:00
|
|
|
drop_and_build_repository(repo_path, 4001, NULL, NULL);
|
2017-09-07 16:05:56 +00:00
|
|
|
|
|
|
|
ipfs_node_offline_new(repo_path, &local_node);
|
2016-12-14 17:07:43 +00:00
|
|
|
|
|
|
|
// write to ipfs
|
2017-04-20 22:56:03 +00:00
|
|
|
struct HashtableNode* write_node;
|
2016-12-29 09:42:01 +00:00
|
|
|
size_t bytes_written;
|
2017-04-20 22:56:03 +00:00
|
|
|
if (ipfs_import_file("/tmp", fileName, &write_node, local_node, &bytes_written, 1) == 0) {
|
|
|
|
ipfs_node_free(local_node);
|
2016-12-14 17:07:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-15 17:38:08 +00:00
|
|
|
// cid should be the same each time
|
2017-01-02 04:48:09 +00:00
|
|
|
unsigned char cid_test[10] = { 0x1e, 0xcf, 0x04, 0xce, 0x6a, 0xe8, 0xbf, 0xc0, 0xeb, 0xe4 };
|
2016-12-15 17:38:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < 10; i++) {
|
2016-12-21 11:40:19 +00:00
|
|
|
if (write_node->hash[i] != cid_test[i]) {
|
|
|
|
printf("Hashes do not match at position %d, should be %02x but is %02x\n", i, cid_test[i], write_node->hash[i]);
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_node_free(local_node);
|
|
|
|
ipfs_hashtable_node_free(write_node);
|
2016-12-15 17:38:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 17:07:43 +00:00
|
|
|
// make sure all went okay
|
2017-04-20 22:56:03 +00:00
|
|
|
struct HashtableNode* read_node;
|
|
|
|
if (ipfs_merkledag_get(write_node->hash, write_node->hash_size, &read_node, local_node->repo) == 0) {
|
|
|
|
ipfs_node_free(local_node);
|
|
|
|
ipfs_hashtable_node_free(write_node);
|
2016-12-14 17:07:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// compare data
|
2017-01-02 04:48:09 +00:00
|
|
|
if (write_node->data_size != bytes_size + 8 || write_node->data_size != read_node->data_size) {
|
2016-12-14 17:07:43 +00:00
|
|
|
printf("Data size of nodes are not equal or are incorrect. Should be %lu but are %lu\n", write_node->data_size, read_node->data_size);
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_node_free(local_node);
|
|
|
|
ipfs_hashtable_node_free(write_node);
|
|
|
|
ipfs_hashtable_node_free(read_node);
|
2016-12-14 17:07:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < bytes_size; i++) {
|
|
|
|
if (write_node->data[i] != read_node->data[i]) {
|
|
|
|
printf("Data within node is different at position %d\n", i);
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_node_free(local_node);
|
|
|
|
ipfs_hashtable_node_free(write_node);
|
|
|
|
ipfs_hashtable_node_free(read_node);
|
2016-12-14 17:07:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-07 23:45:09 +00:00
|
|
|
// attempt to look in the journal for the entry
|
|
|
|
struct lmdb_context *context = (struct lmdb_context*)local_node->repo->config->datastore->datastore_context;
|
|
|
|
struct JournalRecord* record = NULL;
|
|
|
|
struct lmdb_trans_cursor *cursor = lmdb_trans_cursor_new();
|
|
|
|
cursor->environment = context->db_environment;
|
|
|
|
cursor->database = context->journal_db;
|
|
|
|
cursor->parent_transaction = context->current_transaction;
|
|
|
|
if (mdb_cursor_open(context->current_transaction, *cursor->database, &cursor->cursor) != 0) {
|
|
|
|
fprintf(stderr, "Unable to open cursor.\n");
|
|
|
|
} else if (!lmdb_journalstore_cursor_get(cursor, CURSOR_FIRST, &record)) {
|
|
|
|
fprintf(stderr, "Unable to find any records in the database.\n");
|
|
|
|
}
|
|
|
|
|
2017-04-20 22:56:03 +00:00
|
|
|
ipfs_node_free(local_node);
|
|
|
|
ipfs_hashtable_node_free(write_node);
|
|
|
|
ipfs_hashtable_node_free(read_node);
|
2016-12-14 17:24:44 +00:00
|
|
|
|
2016-12-14 17:07:43 +00:00
|
|
|
return 1;
|
|
|
|
}
|