2017-02-27 17:27:40 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
|
2016-12-30 00:05:44 +00:00
|
|
|
#include "ipfs/importer/resolver.h"
|
2017-04-06 14:33:28 +00:00
|
|
|
#include "libp2p/os/utils.h"
|
2017-02-27 17:27:40 +00:00
|
|
|
#include "multiaddr/multiaddr.h"
|
|
|
|
#include "ipfs/core/daemon.h"
|
2016-12-30 00:05:44 +00:00
|
|
|
|
|
|
|
int test_resolver_get() {
|
|
|
|
// clean out repository
|
2017-01-02 04:48:09 +00:00
|
|
|
const char* ipfs_path = "/tmp/.ipfs";
|
|
|
|
os_utils_setenv("IPFS_PATH", ipfs_path, 1);
|
2016-12-30 00:05:44 +00:00
|
|
|
|
2017-01-02 04:48:09 +00:00
|
|
|
drop_and_build_repository(ipfs_path);
|
|
|
|
|
2017-01-02 05:38:09 +00:00
|
|
|
// this should point to a test directory with files and directories
|
|
|
|
char* home_dir = os_utils_get_homedir();
|
|
|
|
char* test_dir = malloc(strlen(home_dir) + 10);
|
|
|
|
|
|
|
|
os_utils_filepath_join(home_dir, "ipfstest", test_dir, strlen(home_dir) + 10);
|
|
|
|
|
2017-01-02 04:48:09 +00:00
|
|
|
int argc = 4;
|
2016-12-30 00:05:44 +00:00
|
|
|
char* argv[argc];
|
|
|
|
argv[0] = "ipfs";
|
|
|
|
argv[1] = "add";
|
2017-01-02 04:48:09 +00:00
|
|
|
argv[2] = "-r";
|
2017-01-02 05:38:09 +00:00
|
|
|
argv[3] = test_dir;
|
2016-12-30 00:05:44 +00:00
|
|
|
|
|
|
|
ipfs_import_files(argc, (char**)argv);
|
|
|
|
|
|
|
|
struct FSRepo* fs_repo;
|
2017-01-02 04:48:09 +00:00
|
|
|
ipfs_repo_fsrepo_new(ipfs_path, NULL, &fs_repo);
|
2016-12-30 00:05:44 +00:00
|
|
|
ipfs_repo_fsrepo_open(fs_repo);
|
|
|
|
|
2017-02-27 17:27:40 +00:00
|
|
|
struct IpfsNode ipfs_node;
|
|
|
|
ipfs_node.repo = fs_repo;
|
|
|
|
|
2016-12-30 00:05:44 +00:00
|
|
|
// find something that is already in the repository
|
2017-02-27 17:27:40 +00:00
|
|
|
struct Node* result = ipfs_resolver_get("QmbMecmXESf96ZNry7hRuzaRkEBhjqXpoYfPCwgFzVGDzB", NULL, &ipfs_node);
|
2016-12-30 00:05:44 +00:00
|
|
|
if (result == NULL) {
|
2017-01-02 05:38:09 +00:00
|
|
|
free(test_dir);
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
2016-12-30 00:05:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ipfs_node_free(result);
|
|
|
|
|
2017-02-27 17:27:40 +00:00
|
|
|
// find something where path includes the local node
|
|
|
|
char path[255];
|
|
|
|
strcpy(path, "/ipfs/");
|
|
|
|
strcat(path, fs_repo->config->identity->peer_id);
|
|
|
|
strcat(path, "/QmbMecmXESf96ZNry7hRuzaRkEBhjqXpoYfPCwgFzVGDzB");
|
|
|
|
result = ipfs_resolver_get(path, NULL, &ipfs_node);
|
|
|
|
if (result == NULL) {
|
|
|
|
free(test_dir);
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ipfs_node_free(result);
|
|
|
|
|
2016-12-30 00:05:44 +00:00
|
|
|
// find something by path
|
2017-02-27 17:27:40 +00:00
|
|
|
result = ipfs_resolver_get("QmZBvycPAYScBoPEzm35zXHt6gYYV5t9PyWmr4sksLPNFS/hello_world.txt", NULL, &ipfs_node);
|
2016-12-30 00:05:44 +00:00
|
|
|
if (result == NULL) {
|
2017-01-02 05:38:09 +00:00
|
|
|
free(test_dir);
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
2016-12-30 00:05:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-02 05:38:09 +00:00
|
|
|
ipfs_node_free(result);
|
|
|
|
free(test_dir);
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
|
|
|
|
2016-12-30 00:05:44 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2017-02-27 17:27:40 +00:00
|
|
|
|
|
|
|
void* test_resolver_daemon_start(void* arg) {
|
|
|
|
ipfs_daemon_start((char*)arg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_resolver_remote_get() {
|
|
|
|
// clean out repository
|
|
|
|
const char* ipfs_path = "/tmp/.ipfs";
|
|
|
|
os_utils_setenv("IPFS_PATH", ipfs_path, 1);
|
|
|
|
char remote_peer_id[255];
|
|
|
|
char path[255];
|
|
|
|
drop_and_build_repository(ipfs_path);
|
|
|
|
|
|
|
|
// start the daemon in a separate thread
|
|
|
|
pthread_t thread;
|
2017-03-09 15:01:09 +00:00
|
|
|
if (pthread_create(&thread, NULL, test_resolver_daemon_start, (void*)ipfs_path) < 0)
|
|
|
|
return 0;
|
2017-02-27 17:27:40 +00:00
|
|
|
|
|
|
|
// this should point to a test directory with files and directories
|
|
|
|
char* home_dir = os_utils_get_homedir();
|
|
|
|
char* test_dir = malloc(strlen(home_dir) + 10);
|
|
|
|
|
|
|
|
os_utils_filepath_join(home_dir, "ipfstest", test_dir, strlen(home_dir) + 10);
|
|
|
|
|
|
|
|
int argc = 4;
|
|
|
|
char* argv[argc];
|
|
|
|
argv[0] = "ipfs";
|
|
|
|
argv[1] = "add";
|
|
|
|
argv[2] = "-r";
|
|
|
|
argv[3] = test_dir;
|
|
|
|
|
|
|
|
ipfs_import_files(argc, (char**)argv);
|
|
|
|
|
|
|
|
struct FSRepo* fs_repo;
|
|
|
|
ipfs_repo_fsrepo_new(ipfs_path, NULL, &fs_repo);
|
|
|
|
ipfs_repo_fsrepo_open(fs_repo);
|
|
|
|
|
|
|
|
// put the server in the peer store and change our peer id so we think it is remote (hack for now)
|
|
|
|
strcpy(remote_peer_id, fs_repo->config->identity->peer_id);
|
|
|
|
struct MultiAddress* remote_addr = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/4001");
|
|
|
|
struct Peerstore* peerstore = libp2p_peerstore_new();
|
|
|
|
struct Libp2pPeer* peer = libp2p_peer_new_from_data(remote_peer_id, strlen(remote_peer_id), remote_addr);
|
|
|
|
libp2p_peerstore_add_peer(peerstore, peer);
|
|
|
|
strcpy(fs_repo->config->identity->peer_id, "QmABCD");
|
|
|
|
|
|
|
|
struct IpfsNode local_node;
|
|
|
|
local_node.mode = MODE_ONLINE;
|
|
|
|
local_node.peerstore = peerstore;
|
|
|
|
local_node.repo = fs_repo;
|
|
|
|
local_node.identity = fs_repo->config->identity;
|
|
|
|
|
|
|
|
// find something by remote path
|
|
|
|
strcpy(path, "/ipfs/");
|
|
|
|
strcat(path, remote_peer_id);
|
|
|
|
strcat(path, "/QmZBvycPAYScBoPEzm35zXHt6gYYV5t9PyWmr4sksLPNFS/hello_world.txt");
|
|
|
|
struct Node* result = ipfs_resolver_get(path, NULL, &local_node);
|
|
|
|
if (result == NULL) {
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
|
|
|
pthread_cancel(thread);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ipfs_node_free(result);
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
|
|
|
pthread_cancel(thread);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|