c-ipfs/test/node/test_importer.h

216 lines
5.8 KiB
C
Raw Normal View History

#include <stdio.h>
#include "ipfs/importer/importer.h"
2016-12-15 17:38:08 +00:00
#include "ipfs/importer/exporter.h"
#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"
/***
* Helper to create a test file in the OS
*/
int create_file(const char* fileName, unsigned char* bytes, size_t num_bytes) {
FILE* file = fopen(fileName, "wb");
fwrite(bytes, num_bytes, 1, file);
fclose(file);
return 1;
}
int create_bytes(unsigned char* buffer, size_t num_bytes) {
int counter = 0;
for(int i = 0; i < num_bytes; i++) {
buffer[i] = counter++;
if (counter > 15)
counter = 0;
}
return 1;
}
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";
// create the necessary file
create_bytes(file_bytes, bytes_size);
create_file(fileName, file_bytes, bytes_size);
// get the repo
drop_and_build_repository("/tmp/.ipfs");
struct FSRepo* fs_repo;
ipfs_repo_fsrepo_new("/tmp/.ipfs", NULL, &fs_repo);
ipfs_repo_fsrepo_open(fs_repo);
// write to ipfs
struct Node* write_node;
if (ipfs_import_file(fileName, &write_node, fs_repo) == 0) {
ipfs_repo_fsrepo_free(fs_repo);
return 0;
}
2016-12-15 17:38:08 +00:00
// cid should be the same each time
unsigned char cid_test[10] = { 0xec ,0x79 ,0x18 ,0x4c, 0xe8, 0xb0, 0x66, 0x39, 0xaa, 0xed };
/*
for (int i = 0; i < 10; i++) {
printf(" %02x ", write_node->cached->hash[i]);
}
printf("\n");
*/
for(int i = 0; i < 10; i++) {
if (write_node->cached->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->cached->hash[i]);
ipfs_repo_fsrepo_free(fs_repo);
return 0;
}
}
// make sure all went okay
struct Node* read_node;
if (ipfs_merkledag_get(write_node->cached, &read_node, fs_repo) == 0) {
ipfs_repo_fsrepo_free(fs_repo);
ipfs_node_free(write_node);
return 0;
}
2016-12-15 17:38:08 +00:00
// the second block should be there
struct Node* read_node2;
if (ipfs_merkledag_get(read_node->head_link->cid, &read_node2, fs_repo) == 0) {
printf("Unable to find the linked node.\n");
ipfs_repo_fsrepo_free(fs_repo);
ipfs_node_free(write_node);
return 0;
}
ipfs_node_free(read_node2);
// 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);
ipfs_repo_fsrepo_free(fs_repo);
ipfs_node_free(write_node);
ipfs_node_free(read_node);
return 0;
}
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]);
ipfs_repo_fsrepo_free(fs_repo);
ipfs_node_free(write_node);
ipfs_node_free(read_node);
return 0;
}
}
2016-12-15 17:38:08 +00:00
// convert cid to multihash
size_t base58_size = 55;
unsigned char base58[base58_size];
if ( ipfs_cid_hash_to_base58(read_node->cached, base58, base58_size) == 0) {
printf("Unable to convert cid to multihash\n");
ipfs_repo_fsrepo_free(fs_repo);
ipfs_node_free(write_node);
ipfs_node_free(read_node);
return 0;
}
// attempt to write file
2016-12-15 17:38:08 +00:00
if (ipfs_exporter_to_file(base58, "/tmp/test_import_large_file.rsl", fs_repo) == 0) {
printf("Unable to write file.\n");
ipfs_repo_fsrepo_free(fs_repo);
ipfs_node_free(write_node);
ipfs_node_free(read_node);
return 0;
}
// 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);
}
ipfs_repo_fsrepo_free(fs_repo);
ipfs_node_free(write_node);
ipfs_node_free(read_node);
return 1;
}
int test_import_small_file() {
size_t bytes_size = 1000;
unsigned char file_bytes[bytes_size];
const char* fileName = "/tmp/test_import_small.tmp";
// create the necessary file
create_bytes(file_bytes, bytes_size);
create_file(fileName, file_bytes, bytes_size);
// get the repo
drop_and_build_repository("/tmp/.ipfs");
struct FSRepo* fs_repo;
ipfs_repo_fsrepo_new("/tmp/.ipfs", NULL, &fs_repo);
ipfs_repo_fsrepo_open(fs_repo);
// write to ipfs
struct Node* write_node;
if (ipfs_import_file(fileName, &write_node, fs_repo) == 0) {
ipfs_repo_fsrepo_free(fs_repo);
return 0;
}
2016-12-15 17:38:08 +00:00
// cid should be the same each time
unsigned char cid_test[10] = { 0x47,0x51,0x40,0x0a, 0xdf, 0x62, 0xf9, 0xcc, 0x8d, 0xbb };
/**
for (int i = 0; i < 10; i++) {
printf("%02x\n", write_node->cached->hash[i]);
}
*/
for(int i = 0; i < 10; i++) {
if (write_node->cached->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->cached->hash[i]);
ipfs_repo_fsrepo_free(fs_repo);
return 0;
}
}
// make sure all went okay
struct Node* read_node;
if (ipfs_merkledag_get(write_node->cached, &read_node, fs_repo) == 0) {
ipfs_repo_fsrepo_free(fs_repo);
ipfs_node_free(write_node);
return 0;
}
// compare data
if (write_node->data_size != bytes_size || write_node->data_size != read_node->data_size) {
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);
ipfs_repo_fsrepo_free(fs_repo);
ipfs_node_free(write_node);
ipfs_node_free(read_node);
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);
ipfs_repo_fsrepo_free(fs_repo);
ipfs_node_free(write_node);
ipfs_node_free(read_node);
return 0;
}
}
2016-12-14 17:24:44 +00:00
ipfs_repo_fsrepo_free(fs_repo);
ipfs_node_free(write_node);
ipfs_node_free(read_node);
return 1;
}