From 215af9cfce18b4df648b26cbe9e2c6b82c98295e Mon Sep 17 00:00:00 2001 From: jmjatlanta Date: Wed, 20 Sep 2017 10:18:27 -0500 Subject: [PATCH] Buildout of test_core_api_object_cat --- test/config.test1 | 2 +- test/config.test2 | 2 +- test/core/test_api.h | 91 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 90 insertions(+), 5 deletions(-) diff --git a/test/config.test1 b/test/config.test1 index 1ecb358..f0296b8 100644 --- a/test/config.test1 +++ b/test/config.test1 @@ -19,7 +19,7 @@ "/ip4/0.0.0.0/tcp/4001", "/ip6/::/tcp/4001" ], - "API": "(null)", + "API": "/ip4/127.0.0.1/tcp/5002", "Gateway": "(null)" }, "Mounts": { diff --git a/test/config.test2 b/test/config.test2 index 5d234c5..87e9467 100644 --- a/test/config.test2 +++ b/test/config.test2 @@ -19,7 +19,7 @@ "/ip4/0.0.0.0/tcp/4002", "/ip6/::/tcp/4002" ], - "API": "(null)", + "API": "/ip4/127.0.0.1/tcp/5002", "Gateway": "(null)" }, "Mounts": { diff --git a/test/core/test_api.h b/test/core/test_api.h index dd781b0..fe5b45e 100644 --- a/test/core/test_api.h +++ b/test/core/test_api.h @@ -33,7 +33,92 @@ int test_core_api_startup_shutdown() { return retVal; } -int test_core_api_local_object_cat() { - // add a file to the store, then use the api to get it - return 0; +/*** + * 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; + char* ipfs_path1 = "/tmp/ipfs_1"; + char* config_file1 = "config.test1"; + char* ipfs_path2 = "/tmp/ipfs_2"; + char* config_file2 = "config.test2"; + struct FSRepo* fs_repo = NULL; + char hash[256] = ""; + char* args[] = {"ipfs", "--config", ipfs_path2, "cat", hash }; + + // logging + libp2p_logger_add_class("test_journal"); + 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"); + + // 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); + + // add some files 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, hash, 256); + 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; + 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 + if (ipfs_exporter_object_cat(5, args) == 0) { + goto exit; + } + + retVal = 1; + exit: + ipfs_daemon_stop(); + if (thread_started1) + pthread_join(daemon_thread1, NULL); + if (thread_started2) + pthread_join(daemon_thread2, NULL); + return retVal; }