c-ipfs/test/core/test_api.h

533 lines
16 KiB
C
Raw Permalink Normal View History

2017-09-20 15:50:32 +00:00
#include <pthread.h>
2017-09-20 12:53:36 +00:00
#include "../test_helper.h"
2017-09-13 10:02:59 +00:00
#include "libp2p/utils/logger.h"
2017-09-25 22:56:10 +00:00
#include "ipfs/cmd/cli.h"
2017-09-20 14:11:01 +00:00
#include "ipfs/core/client_api.h"
#include "ipfs/core/daemon.h"
2017-09-20 15:50:32 +00:00
#include "ipfs/importer/exporter.h"
#include "ipfs/importer/importer.h"
2017-09-21 20:27:16 +00:00
#include "ipfs/namesys/name.h"
2017-09-13 10:02:59 +00:00
int test_core_api_startup_shutdown() {
2017-09-25 11:57:17 +00:00
char* repo_path = "/tmp/ipfs_1";
2017-09-20 12:32:12 +00:00
char* peer_id = NULL;
int retVal = 0;
2017-09-25 18:37:16 +00:00
pthread_t daemon_thread = 0;
struct IpfsNode* client_node = NULL;
2017-09-25 15:00:49 +00:00
//libp2p_logger_add_class("api");
2017-09-20 12:32:12 +00:00
if (!drop_and_build_repository(repo_path, 4001, NULL, &peer_id))
goto exit;
// this should start the api
2017-09-25 15:00:49 +00:00
pthread_create(&daemon_thread, NULL, test_daemon_start, repo_path);
2017-09-20 14:11:01 +00:00
sleep(3);
2017-09-20 12:32:12 +00:00
2017-09-25 14:20:51 +00:00
// make a client to the api
if (!ipfs_node_offline_new(repo_path, &client_node)) {
2017-09-20 14:11:01 +00:00
goto exit;
}
2017-09-25 15:00:49 +00:00
2017-09-20 14:11:01 +00:00
// test to see if it is working
2017-09-25 15:00:49 +00:00
if (client_node->mode != MODE_API_AVAILABLE) {
libp2p_logger_error("test_api", "API Not available.\n");
2017-09-20 14:11:01 +00:00
goto exit;
2017-09-25 15:00:49 +00:00
}
2017-09-20 12:32:12 +00:00
retVal = 1;
2017-09-20 14:11:01 +00:00
// cleanup
2017-09-20 12:32:12 +00:00
exit:
2017-09-20 14:11:01 +00:00
ipfs_daemon_stop();
2017-09-25 15:00:49 +00:00
pthread_join(daemon_thread, NULL);
2017-09-20 14:11:01 +00:00
if (peer_id != NULL)
free(peer_id);
2017-09-20 12:32:12 +00:00
return retVal;
2017-09-13 10:02:59 +00:00
}
2017-09-20 14:11:01 +00:00
2017-09-20 15:18:27 +00:00
/***
* Attempt to get a file over the "network" using the api
*/
int test_core_api_object_cat() {
int retVal = 0;
pthread_t daemon_thread1;
pthread_t daemon_thread2;
int thread_started1 = 0;
int thread_started2 = 0;
2017-09-25 11:57:17 +00:00
char* ipfs_path1 = "/tmp/ipfs_1";
2017-09-25 22:56:10 +00:00
char* config_file1 = "config.test1.wo_journal";
2017-09-25 11:57:17 +00:00
char* ipfs_path2 = "/tmp/ipfs_2";
2017-09-25 22:56:10 +00:00
char* config_file2 = "config.test2.wo_journal";
2017-09-20 15:18:27 +00:00
struct FSRepo* fs_repo = NULL;
char hash[256] = "";
char* args[] = {"ipfs", "--config", ipfs_path2, "cat", hash };
struct CliArguments* arguments = NULL;
2017-09-20 15:18:27 +00:00
// logging
2017-09-25 22:56:10 +00:00
libp2p_logger_add_class("test_api");
2017-09-20 15:18:27 +00:00
libp2p_logger_add_class("journal");
libp2p_logger_add_class("daemon");
libp2p_logger_add_class("online");
libp2p_logger_add_class("peer");
libp2p_logger_add_class("null");
libp2p_logger_add_class("replication");
libp2p_logger_add_class("fs_repo");
libp2p_logger_add_class("lmdb_journalstore");
libp2p_logger_add_class("lmdb_datastore");
libp2p_logger_add_class("secio");
libp2p_logger_add_class("socket");
libp2p_logger_add_class("protocol");
libp2p_logger_add_class("dht_protocol");
libp2p_logger_add_class("resolver");
libp2p_logger_add_class("unixfs");
libp2p_logger_add_class("bitswap_engine");
libp2p_logger_add_class("bitswap_network");
2017-09-25 22:56:10 +00:00
libp2p_logger_add_class("exporter");
2017-09-20 15:18:27 +00:00
// build 2 repos
if (!drop_build_open_repo(ipfs_path1, &fs_repo, config_file1)) {
ipfs_repo_fsrepo_free(fs_repo);
libp2p_logger_error("test_api", "Unable to drop and build repository at %s\n", ipfs_path1);
goto exit;
}
libp2p_logger_debug("test_api", "Changed the server id to %s.\n", fs_repo->config->identity->peer->id);
ipfs_repo_fsrepo_free(fs_repo);
if (!drop_build_open_repo(ipfs_path2, &fs_repo, config_file2)) {
ipfs_repo_fsrepo_free(fs_repo);
libp2p_logger_error("test_api", "Unable to drop and build repository at %s\n", ipfs_path2);
goto exit;
}
libp2p_logger_debug("test_api", "Changed the server id to %s.\n", fs_repo->config->identity->peer->id);
ipfs_repo_fsrepo_free(fs_repo);
2017-09-25 22:56:10 +00:00
// add a file to the first repo
2017-09-20 15:18:27 +00:00
uint8_t *bytes = (unsigned char*)"hello, world!\n";
char* filename = "/tmp/test1.txt";
2017-09-20 15:18:27 +00:00
create_file(filename, bytes, strlen((char*)bytes));
struct HashtableNode* node;
size_t bytes_written;
struct IpfsNode *local_node = NULL;
ipfs_node_offline_new(ipfs_path1, &local_node);
2017-09-20 15:18:27 +00:00
ipfs_import_file(NULL, filename, &node, local_node, &bytes_written, 0);
memset(hash, 0, 256);
2017-09-20 15:50:32 +00:00
ipfs_cid_hash_to_base58(node->hash, node->hash_size, (unsigned char*)hash, 256);
2017-09-25 22:56:10 +00:00
libp2p_logger_debug("test_api", "Inserted file with hash %s.\n", hash);
2017-09-25 13:55:42 +00:00
ipfs_node_free(local_node);
2017-09-20 15:18:27 +00:00
ipfs_hashtable_node_free(node);
libp2p_logger_debug("test_api", "*** Firing up daemons ***\n");
pthread_create(&daemon_thread1, NULL, test_daemon_start, (void*)ipfs_path1);
thread_started1 = 1;
2017-09-25 22:56:10 +00:00
sleep(3);
2017-09-20 15:18:27 +00:00
pthread_create(&daemon_thread2, NULL, test_daemon_start, (void*)ipfs_path2);
thread_started2 = 1;
sleep(3);
// use a client to ask for the file on server 1
arguments = cli_arguments_new(5, args);
if (ipfs_exporter_object_cat(arguments, stdout) == 0) {
2017-09-25 22:56:10 +00:00
libp2p_logger_error("test_api", "ipfs_exporter_object_cat returned false.\n");
2017-09-20 15:18:27 +00:00
goto exit;
}
retVal = 1;
exit:
ipfs_daemon_stop();
if (thread_started1)
pthread_join(daemon_thread1, NULL);
if (thread_started2)
pthread_join(daemon_thread2, NULL);
2017-09-25 22:56:10 +00:00
cli_arguments_free(arguments);
2017-09-20 15:18:27 +00:00
return retVal;
2017-09-20 14:11:01 +00:00
}
2017-09-21 20:27:16 +00:00
2017-10-04 11:34:37 +00:00
/***
* Attempt to get a binary file over the api
*/
int test_core_api_object_cat_binary() {
int retVal = 0;
pthread_t daemon_thread1;
int thread_started1 = 0;
char* ipfs_path1 = "/tmp/ipfs_1";
char* config_file1 = "config.test1.wo_journal";
struct FSRepo* fs_repo = NULL;
char hash[256] = "";
char* args[] = {"ipfs", "--config", ipfs_path1, "cat", hash };
struct CliArguments* arguments = NULL;
// logging
libp2p_logger_add_class("test_api");
libp2p_logger_add_class("journal");
libp2p_logger_add_class("daemon");
libp2p_logger_add_class("online");
libp2p_logger_add_class("peer");
libp2p_logger_add_class("null");
libp2p_logger_add_class("replication");
libp2p_logger_add_class("fs_repo");
libp2p_logger_add_class("lmdb_journalstore");
libp2p_logger_add_class("lmdb_datastore");
libp2p_logger_add_class("secio");
libp2p_logger_add_class("socket");
libp2p_logger_add_class("protocol");
libp2p_logger_add_class("dht_protocol");
libp2p_logger_add_class("resolver");
libp2p_logger_add_class("unixfs");
libp2p_logger_add_class("bitswap_engine");
libp2p_logger_add_class("bitswap_network");
libp2p_logger_add_class("exporter");
libp2p_logger_add_class("api");
2017-10-04 11:34:37 +00:00
// build repo
if (!drop_build_open_repo(ipfs_path1, &fs_repo, config_file1)) {
ipfs_repo_fsrepo_free(fs_repo);
libp2p_logger_error("test_api", "Unable to drop and build repository at %s\n", ipfs_path1);
goto exit;
}
libp2p_logger_debug("test_api", "Changed the server id to %s.\n", fs_repo->config->identity->peer->id);
ipfs_repo_fsrepo_free(fs_repo);
// add a file to the repo
uint8_t bytes[256];
for(int i = 0; i < 256; i++)
bytes[i] = i;
char* filename = "test1.txt";
2017-10-04 11:38:38 +00:00
create_file(filename, bytes, 256);
2017-10-04 11:34:37 +00:00
struct HashtableNode* node;
size_t bytes_written;
struct IpfsNode *local_node = NULL;
ipfs_node_offline_new(ipfs_path1, &local_node);
ipfs_import_file(NULL, filename, &node, local_node, &bytes_written, 0);
memset(hash, 0, 256);
ipfs_cid_hash_to_base58(node->hash, node->hash_size, (unsigned char*)hash, 256);
libp2p_logger_debug("test_api", "Inserted file with hash %s.\n", hash);
ipfs_node_free(local_node);
ipfs_hashtable_node_free(node);
libp2p_logger_debug("test_api", "*** Firing up daemons ***\n");
pthread_create(&daemon_thread1, NULL, test_daemon_start, (void*)ipfs_path1);
thread_started1 = 1;
sleep(3);
// use a client to ask for the file
arguments = cli_arguments_new(5, args);
if (ipfs_exporter_object_cat(arguments, stdout) == 0) {
2017-10-04 11:34:37 +00:00
libp2p_logger_error("test_api", "ipfs_exporter_object_cat returned false.\n");
goto exit;
}
retVal = 1;
exit:
ipfs_daemon_stop();
if (thread_started1)
pthread_join(daemon_thread1, NULL);
cli_arguments_free(arguments);
return retVal;
}
/***
* Attempt to get a large (>256K) binary file over the api
*/
int test_core_api_object_cat_large_binary() {
int retVal = 0;
pthread_t daemon_thread1;
int thread_started1 = 0;
char* ipfs_path1 = "/tmp/ipfs_1";
char* config_file1 = "config.test1.wo_journal";
struct FSRepo* fs_repo = NULL;
char hash[256] = "";
char* args[] = {"ipfs", "--config", ipfs_path1, "cat", hash };
struct CliArguments* arguments = NULL;
// logging
libp2p_logger_add_class("test_api");
libp2p_logger_add_class("journal");
libp2p_logger_add_class("daemon");
libp2p_logger_add_class("online");
libp2p_logger_add_class("peer");
libp2p_logger_add_class("null");
libp2p_logger_add_class("replication");
libp2p_logger_add_class("fs_repo");
libp2p_logger_add_class("lmdb_journalstore");
libp2p_logger_add_class("lmdb_datastore");
libp2p_logger_add_class("secio");
libp2p_logger_add_class("socket");
libp2p_logger_add_class("protocol");
libp2p_logger_add_class("dht_protocol");
libp2p_logger_add_class("resolver");
libp2p_logger_add_class("unixfs");
libp2p_logger_add_class("bitswap_engine");
libp2p_logger_add_class("bitswap_network");
libp2p_logger_add_class("exporter");
libp2p_logger_add_class("api");
// build repo
if (!drop_build_open_repo(ipfs_path1, &fs_repo, config_file1)) {
ipfs_repo_fsrepo_free(fs_repo);
libp2p_logger_error("test_api", "Unable to drop and build repository at %s\n", ipfs_path1);
goto exit;
}
libp2p_logger_debug("test_api", "Changed the server id to %s.\n", fs_repo->config->identity->peer->id);
ipfs_repo_fsrepo_free(fs_repo);
// add a file to the repo
uint8_t bytes[300000];
for(int i = 0; i < 300000; i++)
bytes[i] = i % 255;
char* filename = "test1.txt";
create_file(filename, bytes, 300000);
struct HashtableNode* node;
size_t bytes_written;
struct IpfsNode *local_node = NULL;
ipfs_node_offline_new(ipfs_path1, &local_node);
ipfs_import_file(NULL, filename, &node, local_node, &bytes_written, 0);
memset(hash, 0, 256);
ipfs_cid_hash_to_base58(node->hash, node->hash_size, (unsigned char*)hash, 256);
libp2p_logger_debug("test_api", "Inserted file with hash %s.\n", hash);
ipfs_node_free(local_node);
ipfs_hashtable_node_free(node);
libp2p_logger_debug("test_api", "*** Firing up daemons ***\n");
pthread_create(&daemon_thread1, NULL, test_daemon_start, (void*)ipfs_path1);
thread_started1 = 1;
sleep(3);
// use a client to ask for the file
arguments = cli_arguments_new(5, args);
char* filename2 = "test2.txt";
unlink(filename2);
FILE* fd = fopen(filename2, "w" );
if (ipfs_exporter_object_cat(arguments, fd) == 0) {
libp2p_logger_error("test_api", "ipfs_exporter_object_cat returned false.\n");
fclose(fd);
goto exit;
}
fclose(fd);
retVal = 1;
exit:
ipfs_daemon_stop();
if (thread_started1)
pthread_join(daemon_thread1, NULL);
cli_arguments_free(arguments);
return retVal;
}
2017-09-21 20:27:16 +00:00
int test_core_api_name_resolve() {
int retVal = 0;
pthread_t daemon_thread1;
pthread_t daemon_thread2;
int thread_started1 = 0;
int thread_started2 = 0;
char* ipfs_path1 = "/tmp/ipfs_1";
2017-09-25 11:57:17 +00:00
char* config_file1 = "config.test1.wo_journal";
2017-09-21 20:27:16 +00:00
char* ipfs_path2 = "/tmp/ipfs_2";
2017-09-25 11:57:17 +00:00
char* config_file2 = "config.test2.wo_journal";
2017-09-21 20:27:16 +00:00
struct FSRepo* fs_repo = NULL;
char hash[256] = "";
char peer_id1[256] = "";
char* publish_args[] = {"ipfs", "--config", ipfs_path1, "name", "publish", hash };
char* resolve_args[] = {"ipfs", "--config", ipfs_path2, "name", "resolve", peer_id1 };
struct CliArguments* args = NULL;
2017-09-25 15:00:49 +00:00
//libp2p_logger_add_class("api");
2017-09-25 13:55:42 +00:00
2017-09-21 20:27:16 +00:00
// build 2 repos... repo 1
if (!drop_build_open_repo(ipfs_path1, &fs_repo, config_file1)) {
ipfs_repo_fsrepo_free(fs_repo);
libp2p_logger_error("test_api", "Unable to drop and build repository at %s\n", ipfs_path1);
goto exit;
}
sprintf(peer_id1, "/ipns/%s", fs_repo->config->identity->peer->id);
ipfs_repo_fsrepo_free(fs_repo);
// build 2 repos... repo 2
if (!drop_build_open_repo(ipfs_path2, &fs_repo, config_file2)) {
ipfs_repo_fsrepo_free(fs_repo);
libp2p_logger_error("test_api", "Unable to drop and build repository at %s\n", ipfs_path2);
goto exit;
}
libp2p_logger_debug("test_api", "Changed the server id to %s.\n", fs_repo->config->identity->peer->id);
ipfs_repo_fsrepo_free(fs_repo);
// add a file to the first repo
uint8_t *bytes = (unsigned char*)"hello, world!\n";
char* filename = "test1.txt";
create_file(filename, bytes, strlen((char*)bytes));
struct HashtableNode* node;
size_t bytes_written;
struct IpfsNode *local_node = NULL;
ipfs_node_offline_new(ipfs_path1, &local_node);
ipfs_import_file(NULL, filename, &node, local_node, &bytes_written, 0);
memset(hash, 0, 256);
ipfs_cid_hash_to_base58(node->hash, node->hash_size, (unsigned char*)hash, 256);
2017-09-25 13:55:42 +00:00
ipfs_node_free(local_node);
2017-09-21 20:27:16 +00:00
ipfs_hashtable_node_free(node);
libp2p_logger_debug("test_api", "*** Firing up daemons ***\n");
pthread_create(&daemon_thread1, NULL, test_daemon_start, (void*)ipfs_path1);
thread_started1 = 1;
2017-09-25 20:11:44 +00:00
sleep(5);
2017-09-21 20:27:16 +00:00
pthread_create(&daemon_thread2, NULL, test_daemon_start, (void*)ipfs_path2);
thread_started2 = 1;
2017-09-25 20:11:44 +00:00
sleep(10);
2017-09-21 20:27:16 +00:00
2017-09-25 11:57:17 +00:00
// publish name on server 1
args = cli_arguments_new(6, publish_args);
ipfs_name(args);
cli_arguments_free(args);
args = NULL;
2017-09-25 20:11:44 +00:00
sleep(3);
2017-09-21 20:27:16 +00:00
// use a client of server2 to to ask for the "name resolve" on server 1
args = cli_arguments_new(6, resolve_args);
if (ipfs_name(args) == 0) {
goto exit;
}
2017-09-25 20:11:44 +00:00
sleep(5);
2017-09-21 20:27:16 +00:00
retVal = 1;
exit:
cli_arguments_free(args);
ipfs_daemon_stop();
if (thread_started1)
pthread_join(daemon_thread1, NULL);
if (thread_started2)
pthread_join(daemon_thread2, NULL);
return retVal;
}
/**
* Like test_core_api_name_resolve, but split into 2 pieces
*/
int test_core_api_name_resolve_1() {
int retVal = 0;
pthread_t daemon_thread1;
int thread_started1 = 0;
char* ipfs_path1 = "/tmp/ipfs_1";
char* config_file1 = "config.test1.wo_journal";
struct FSRepo* fs_repo = NULL;
char hash[256] = "";
char peer_id1[256] = "";
struct CliArguments* args = NULL;
libp2p_logger_add_class("api");
libp2p_logger_add_class("test_api");
2017-09-25 18:37:16 +00:00
libp2p_logger_add_class("protocol");
// repo 1
if (!drop_build_open_repo(ipfs_path1, &fs_repo, config_file1)) {
ipfs_repo_fsrepo_free(fs_repo);
libp2p_logger_error("test_api", "Unable to drop and build repository at %s\n", ipfs_path1);
goto exit;
}
sprintf(peer_id1, "/ipns/%s", fs_repo->config->identity->peer->id);
ipfs_repo_fsrepo_free(fs_repo);
// add a file to the first repo
uint8_t *bytes = (unsigned char*)"hello, world!\n";
char* filename = "test1.txt";
create_file(filename, bytes, strlen((char*)bytes));
struct HashtableNode* node;
size_t bytes_written;
struct IpfsNode *local_node = NULL;
ipfs_node_offline_new(ipfs_path1, &local_node);
ipfs_import_file(NULL, filename, &node, local_node, &bytes_written, 0);
memset(hash, 0, 256);
ipfs_cid_hash_to_base58(node->hash, node->hash_size, (unsigned char*)hash, 256);
libp2p_logger_debug("test_api", "The hash is %s.\n", hash);
ipfs_node_free(local_node);
ipfs_hashtable_node_free(node);
libp2p_logger_debug("test_api", "*** Firing up daemon ***\n");
pthread_create(&daemon_thread1, NULL, test_daemon_start, (void*)ipfs_path1);
thread_started1 = 1;
sleep(45);
retVal = 1;
exit:
cli_arguments_free(args);
ipfs_daemon_stop();
if (thread_started1)
pthread_join(daemon_thread1, NULL);
return retVal;
}
/***
* This should already be running before you run test_core_api_name_resolve_2
*/
int test_core_api_name_resolve_2() {
int retVal = 0;
pthread_t daemon_thread2;
int thread_started2 = 0;
char* ipfs_path2 = "/tmp/ipfs_2";
char* config_file2 = "config.test2.wo_journal";
struct FSRepo* fs_repo = NULL;
2017-09-25 18:37:16 +00:00
char peer_id1[256] = "/ipns/QmZVoAZGFfinB7MQQiDzB84kWaDPQ95GLuXdemJFM2r9b4";
char* resolve_args[] = {"ipfs", "--config", ipfs_path2, "name", "resolve", peer_id1 };
struct CliArguments* args = NULL;
libp2p_logger_add_class("test_api");
libp2p_logger_add_class("api");
2017-09-25 18:37:16 +00:00
libp2p_logger_add_class("bitswap_engine");
libp2p_logger_add_class("dht_protocol");
// build 2 repos... repo 2
if (!drop_build_open_repo(ipfs_path2, &fs_repo, config_file2)) {
ipfs_repo_fsrepo_free(fs_repo);
libp2p_logger_error("test_api", "Unable to drop and build repository at %s\n", ipfs_path2);
goto exit;
}
libp2p_logger_debug("test_api", "Changed the server id to %s.\n", fs_repo->config->identity->peer->id);
ipfs_repo_fsrepo_free(fs_repo);
libp2p_logger_debug("test_api", "*** Firing up daemon ***\n");
pthread_create(&daemon_thread2, NULL, test_daemon_start, (void*)ipfs_path2);
thread_started2 = 1;
libp2p_logger_debug("test_api", "Waiting 45 secs...\n");
sleep(45);
libp2p_logger_debug("test_api", "Wait is over\n");
// use a client of server2 to to ask for the "name resolve" on server 1
args = cli_arguments_new(6, resolve_args);
if (ipfs_name(args) == 0) {
goto exit;
}
retVal = 1;
exit:
cli_arguments_free(args);
ipfs_daemon_stop();
if (thread_started2)
pthread_join(daemon_thread2, NULL);
return retVal;
}
// publish a name to server 1
int test_core_api_name_resolve_3()
{
char* hash = "QmcbMZW8hcd46AfUsJUxQYTanHYDpeUq3pBuX5nihPEKD9";
char* publish_args[] = {"ipfs", "--config", "/tmp/ipfs_1", "name", "publish", hash };
struct CliArguments* args = NULL;
libp2p_logger_add_class("api");
libp2p_logger_add_class("test_api");
2017-09-25 17:07:16 +00:00
libp2p_logger_add_class("http_request");
// publish name on server 1
args = cli_arguments_new(6, publish_args);
int retVal = ipfs_name(args);
cli_arguments_free(args);
args = NULL;
return retVal;
}