c-ipfs/test/node/test_resolver.h

163 lines
4.5 KiB
C
Raw Permalink Normal View History

2017-02-27 17:27:40 +00:00
#include <pthread.h>
#include "ipfs/cmd/cli.h"
#include "ipfs/importer/resolver.h"
#include "libp2p/os/utils.h"
2017-02-27 17:27:40 +00:00
#include "multiaddr/multiaddr.h"
#include "ipfs/core/daemon.h"
int test_resolver_get() {
2017-04-27 20:52:20 +00:00
int retVal = 0;
char* home_dir = os_utils_get_homedir();
char* test_dir = malloc(strlen(home_dir) + 10);
2017-04-27 20:52:20 +00:00
struct FSRepo* fs_repo = NULL;
struct HashtableNode* result = NULL;
int argc = 6;
char* argv[argc];
const char* work_path = "/tmp";
char ipfs_path[12];
struct CliArguments* arguments = NULL;
2017-04-27 20:52:20 +00:00
sprintf(&ipfs_path[0], "%s/%s", work_path, ".ipfs");
os_utils_filepath_join(home_dir, "ipfstest", test_dir, strlen(home_dir) + 10);
2017-04-27 20:52:20 +00:00
// clean out repository
if (!drop_and_build_repository(ipfs_path, 4001, NULL, NULL))
goto exit;
argv[0] = "ipfs";
argv[1] = "add";
argv[2] = "-r";
argv[3] = test_dir;
2017-04-27 20:52:20 +00:00
argv[4] = "-c";
argv[5] = (char*)work_path;
arguments = cli_arguments_new(6, (char**)&argv);
ipfs_import_files(arguments);
ipfs_repo_fsrepo_new(ipfs_path, NULL, &fs_repo);
ipfs_repo_fsrepo_open(fs_repo);
2017-02-27 17:27:40 +00:00
struct IpfsNode ipfs_node;
ipfs_node.repo = fs_repo;
// find something that is already in the repository
2017-04-27 20:52:20 +00:00
result = ipfs_resolver_get("QmbMecmXESf96ZNry7hRuzaRkEBhjqXpoYfPCwgFzVGDzB", NULL, &ipfs_node);
if (result == NULL) {
2017-04-27 20:52:20 +00:00
goto exit;
}
2017-04-27 20:52:20 +00:00
// clean up to try something else
ipfs_hashtable_node_free(result);
2017-04-27 20:52:20 +00:00
result = NULL;
2017-02-27 17:27:40 +00:00
// find something where path includes the local node
char path[255];
strcpy(path, "/ipfs/");
2017-07-31 17:50:12 +00:00
strcat(path, fs_repo->config->identity->peer->id);
2017-02-27 17:27:40 +00:00
strcat(path, "/QmbMecmXESf96ZNry7hRuzaRkEBhjqXpoYfPCwgFzVGDzB");
result = ipfs_resolver_get(path, NULL, &ipfs_node);
if (result == NULL) {
2017-04-27 20:52:20 +00:00
goto exit;
2017-02-27 17:27:40 +00:00
}
2017-04-27 20:52:20 +00:00
// clean up to try something else
ipfs_hashtable_node_free(result);
2017-04-27 20:52:20 +00:00
result = NULL;
2017-02-27 17:27:40 +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);
if (result == NULL) {
2017-04-27 20:52:20 +00:00
goto exit;
}
2017-04-27 20:52:20 +00:00
retVal = 1;
exit:
free(test_dir);
ipfs_repo_fsrepo_free(fs_repo);
2017-04-27 20:52:20 +00:00
ipfs_hashtable_node_free(result);
cli_arguments_free(arguments);
2017-04-27 20:52:20 +00:00
return retVal;
}
2017-02-27 17:27:40 +00:00
int test_resolver_remote_get() {
// clean out repository
const char* ipfs_path = "/tmp";
2017-02-27 17:27:40 +00:00
os_utils_setenv("IPFS_PATH", ipfs_path, 1);
char remote_peer_id[255];
char path[255];
pthread_t thread;
2017-04-27 20:52:20 +00:00
int thread_started = 0;
int retVal = 0;
struct FSRepo* fs_repo = NULL;
2017-07-17 21:14:40 +00:00
struct HashtableNode* result = NULL;
struct CliArguments* arguments;
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);
int argc = 4;
char* argv[argc];
argv[0] = "ipfs";
argv[1] = "add";
argv[2] = "-r";
argv[3] = test_dir;
arguments = cli_arguments_new(4, (char**)&argv);
2017-04-27 20:52:20 +00:00
drop_and_build_repository(ipfs_path, 4001, NULL, NULL);
// start the daemon in a separate thread
2017-09-19 00:04:52 +00:00
if (pthread_create(&thread, NULL, test_daemon_start, (void*)ipfs_path) < 0)
2017-04-27 20:52:20 +00:00
goto exit;
thread_started = 1;
os_utils_filepath_join(home_dir, "ipfstest", test_dir, strlen(home_dir) + 10);
ipfs_import_files(arguments);
2017-02-27 17:27:40 +00:00
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)
2017-07-31 17:50:12 +00:00
strcpy(remote_peer_id, fs_repo->config->identity->peer->id);
char multiaddress_string[100];
sprintf(multiaddress_string, "/ip4/127.0.0.1/tcp/4001/ipfs/%s", remote_peer_id);
struct MultiAddress* remote_addr = multiaddress_new_from_string(multiaddress_string);
2017-07-31 17:50:12 +00:00
struct Peerstore* peerstore = libp2p_peerstore_new(fs_repo->config->identity->peer);
struct Libp2pPeer* peer = libp2p_peer_new_from_multiaddress(remote_addr);
2017-02-27 17:27:40 +00:00
libp2p_peerstore_add_peer(peerstore, peer);
2017-07-31 17:50:12 +00:00
strcpy(fs_repo->config->identity->peer->id, "QmABCD");
2017-02-27 17:27:40 +00:00
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");
2017-07-17 21:14:40 +00:00
result = ipfs_resolver_get(path, NULL, &local_node);
2017-02-27 17:27:40 +00:00
if (result == NULL) {
2017-04-27 20:52:20 +00:00
goto exit;
2017-02-27 17:27:40 +00:00
}
2017-04-27 20:52:20 +00:00
// cleanup
retVal = 1;
exit:
ipfs_daemon_stop();
if (thread_started)
pthread_join(thread, NULL);
ipfs_hashtable_node_free(result);
2017-04-27 20:52:20 +00:00
if (fs_repo != NULL)
ipfs_repo_fsrepo_free(fs_repo);
if (local_node.peerstore != NULL)
libp2p_peerstore_free(local_node.peerstore);
cli_arguments_free(arguments);
2017-04-27 20:52:20 +00:00
return retVal;
2017-02-27 17:27:40 +00:00
}