2016-11-17 20:07:59 +00:00
|
|
|
#include "ipfs/repo/fsrepo/fs_repo.h"
|
2016-12-14 17:07:43 +00:00
|
|
|
#include "../test_helper.h"
|
2016-11-17 20:07:59 +00:00
|
|
|
|
|
|
|
int test_repo_fsrepo_open_config() {
|
2016-11-28 13:09:00 +00:00
|
|
|
struct FSRepo* fs_repo = NULL;
|
2016-11-17 20:07:59 +00:00
|
|
|
|
|
|
|
const char* path = "/tmp/.ipfs";
|
|
|
|
|
2017-04-17 16:58:47 +00:00
|
|
|
if (!drop_build_and_open_repo(path, &fs_repo))
|
2016-11-17 20:07:59 +00:00
|
|
|
return 0;
|
|
|
|
|
2017-04-17 16:58:47 +00:00
|
|
|
if (!ipfs_repo_fsrepo_free(fs_repo))
|
2016-11-17 20:07:59 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2016-12-14 17:07:43 +00:00
|
|
|
|
2017-04-17 16:58:47 +00:00
|
|
|
int test_repo_fsrepo_build() {
|
|
|
|
const char* path = "/tmp/.ipfs";
|
|
|
|
char* peer_id = NULL;
|
|
|
|
|
|
|
|
int retVal = drop_and_build_repository(path, 4001, NULL, &peer_id);
|
|
|
|
if (peer_id != NULL)
|
|
|
|
free(peer_id);
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2016-12-14 17:07:43 +00:00
|
|
|
int test_repo_fsrepo_write_read_block() {
|
|
|
|
struct Block* block = NULL;
|
|
|
|
struct FSRepo* fs_repo = NULL;
|
|
|
|
int retVal = 0;
|
|
|
|
|
|
|
|
// freshen the repository
|
|
|
|
retVal = drop_build_and_open_repo("/tmp/.ipfs", &fs_repo);
|
|
|
|
if (retVal == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// make some data
|
2016-12-15 10:40:24 +00:00
|
|
|
size_t data_size = 10000;
|
2016-12-14 17:07:43 +00:00
|
|
|
unsigned char data[data_size];
|
|
|
|
|
|
|
|
int counter = 0;
|
|
|
|
for(int i = 0; i < data_size; i++) {
|
|
|
|
data[i] = counter++;
|
|
|
|
if (counter > 15)
|
|
|
|
counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create and write the block
|
2017-07-27 17:05:41 +00:00
|
|
|
block = ipfs_block_new();
|
2017-07-20 20:03:49 +00:00
|
|
|
if (block == NULL) {
|
2016-12-14 17:07:43 +00:00
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
retVal = ipfs_blocks_block_add_data(data, data_size, block);
|
|
|
|
if (retVal == 0) {
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-13 17:40:48 +00:00
|
|
|
size_t bytes_written;
|
|
|
|
retVal = ipfs_repo_fsrepo_block_write(block, fs_repo, &bytes_written);
|
2016-12-14 17:07:43 +00:00
|
|
|
if (retVal == 0) {
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
2017-07-24 22:58:39 +00:00
|
|
|
ipfs_block_free(block);
|
2016-12-14 17:07:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve the block
|
|
|
|
struct Block* results;
|
2016-12-21 11:40:19 +00:00
|
|
|
retVal = ipfs_repo_fsrepo_block_read(block->cid->hash, block->cid->hash_length, &results, fs_repo);
|
2016-12-14 17:07:43 +00:00
|
|
|
if (retVal == 0) {
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
2017-07-24 22:58:39 +00:00
|
|
|
ipfs_block_free(block);
|
2016-12-14 17:07:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// compare the two blocks
|
|
|
|
retVal = 1;
|
|
|
|
if (block->data_length != results->data_length || block->data_length != data_size) {
|
|
|
|
printf("block data is of different length: %lu vs %lu\n", results->data_length, block->data_length);
|
|
|
|
retVal = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(size_t i = 0; i < block->data_length; i++) {
|
|
|
|
if (block->data[i] != results->data[i]) {
|
|
|
|
printf("Data is different at position %lu. Should be %02x but is %02x\n", i, block->data[i], results->data[i]);
|
|
|
|
retVal = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
2017-07-24 22:58:39 +00:00
|
|
|
ipfs_block_free(block);
|
|
|
|
ipfs_block_free(results);
|
2016-12-14 17:07:43 +00:00
|
|
|
return retVal;
|
|
|
|
}
|