From 596a811d5e5cd81f019e10121c0938fd254f5fe3 Mon Sep 17 00:00:00 2001 From: John Jones Date: Wed, 4 Oct 2017 06:34:37 -0500 Subject: [PATCH] testing of using api for binary files --- core/api.c | 2 +- include/ipfs/namesys/pb.h | 2 + namesys/pb.c | 2 + test/config.test1 | 2 +- test/config.test1.wo_journal | 2 +- test/config.test2 | 2 +- test/config.test2.wo_journal | 2 +- test/core/test_api.h | 82 ++++++++++++++++++ test/exchange/test_bitswap.h | 163 +++++++++++++++++++++++++++++++++++ test/scripts/test_1.sh | 33 +++++-- test/scripts/test_helpers.sh | 11 +++ test/testit.c | 6 ++ 12 files changed, 299 insertions(+), 10 deletions(-) diff --git a/core/api.c b/core/api.c index a1093a3..abf8c8a 100644 --- a/core/api.c +++ b/core/api.c @@ -360,7 +360,7 @@ int api_send_resp_chunks(int fd, void *buf, size_t size) iov[2].iov_base = "\r\n0\r\n\r\n"; iov[2].iov_len = 7; } - libp2p_logger_error("api", "writing chunk block of %d bytes\n", s); + libp2p_logger_debug("api", "writing chunk block of %d bytes\n", s); if (writev(fd, iov, 3) == -1) return 0; // fail writing. } diff --git a/include/ipfs/namesys/pb.h b/include/ipfs/namesys/pb.h index 5cb29f6..49615d5 100644 --- a/include/ipfs/namesys/pb.h +++ b/include/ipfs/namesys/pb.h @@ -21,10 +21,12 @@ struct namesys_pb { // setting an EOL says "this record is valid until..." const static IpnsEntry_ValidityType IpnsEntry_EOL = 0; +/* static char *IpnsEntry_ValidityType_name[] = { "EOL", NULL }; +*/ int IpnsEntry_ValidityType_value (char *s); struct ipns_entry* ipfs_namesys_pb_new_ipns_entry (); diff --git a/namesys/pb.c b/namesys/pb.c index 3fc43a0..ccc5e0b 100644 --- a/namesys/pb.c +++ b/namesys/pb.c @@ -3,6 +3,7 @@ #include "ipfs/namesys/routing.h" #include "ipfs/namesys/pb.h" +/* int IpnsEntry_ValidityType_value (char *s) { int r; @@ -19,6 +20,7 @@ int IpnsEntry_ValidityType_value (char *s) return -1; // not found. } +*/ struct ipns_entry* ipfs_namesys_pb_new_ipns_entry () { diff --git a/test/config.test1 b/test/config.test1 index f0296b8..33e4fe5 100644 --- a/test/config.test1 +++ b/test/config.test1 @@ -20,7 +20,7 @@ "/ip6/::/tcp/4001" ], "API": "/ip4/127.0.0.1/tcp/5002", - "Gateway": "(null)" + "Gateway": "/ip4/127.0.0.1/tcp/8080" }, "Mounts": { "IPFS": "/ipfs", diff --git a/test/config.test1.wo_journal b/test/config.test1.wo_journal index 44e442e..29e2dee 100644 --- a/test/config.test1.wo_journal +++ b/test/config.test1.wo_journal @@ -20,7 +20,7 @@ "/ip6/::/tcp/4001" ], "API": "/ip4/127.0.0.1/tcp/5001", - "Gateway": "(null)" + "Gateway": "/ip4/127.0.0.1/tcp/8080" }, "Mounts": { "IPFS": "/ipfs", diff --git a/test/config.test2 b/test/config.test2 index 87e9467..47e1336 100644 --- a/test/config.test2 +++ b/test/config.test2 @@ -20,7 +20,7 @@ "/ip6/::/tcp/4002" ], "API": "/ip4/127.0.0.1/tcp/5002", - "Gateway": "(null)" + "Gateway": "/ip4/127.0.0.1/tcp/8080" }, "Mounts": { "IPFS": "/ipfs", diff --git a/test/config.test2.wo_journal b/test/config.test2.wo_journal index d21ed94..266d45f 100644 --- a/test/config.test2.wo_journal +++ b/test/config.test2.wo_journal @@ -20,7 +20,7 @@ "/ip6/::/tcp/4002" ], "API": "/ip4/127.0.0.1/tcp/5002", - "Gateway": "(null)" + "Gateway": "/ip4/127.0.0.1/tcp/8080" }, "Mounts": { "IPFS": "/ipfs", diff --git a/test/core/test_api.h b/test/core/test_api.h index c57f198..74cab25 100644 --- a/test/core/test_api.h +++ b/test/core/test_api.h @@ -144,6 +144,88 @@ int test_core_api_object_cat() { return retVal; } +/*** + * 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"); + + // 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"; + 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", "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) == 0) { + 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; +} + int test_core_api_name_resolve() { int retVal = 0; pthread_t daemon_thread1; diff --git a/test/exchange/test_bitswap.h b/test/exchange/test_bitswap.h index ecfa607..b875d86 100644 --- a/test/exchange/test_bitswap.h +++ b/test/exchange/test_bitswap.h @@ -492,3 +492,166 @@ int test_bitswap_retrieve_file_third_party() { } +/*** + * Attempt to retrieve a file from a known node + */ +int test_bitswap_retrieve_file_go_remote() { + int retVal = 0; + signal(SIGPIPE, SIG_IGN); + /*** + * This assumes a remote server with the hello_world.txt file already in its database + */ + int remote_port = 4001; + // mac + char* remote_peer_id = "QmNgGtj3Dk7RPRFgyp9exmq2WjaUf3gkwhMF1vkmcyXcBD"; + char* remote_ip = "10.211.55.2"; + // linux + //char* remote_peer_id = "QmRKm1d9kSCRpMFtLYpfhhCQ3DKuSSPJa3qn9wWXfwnWnY"; + //char* remote_ip = "10.211.55.4"; + char* hello_world_hash = "QmYAXgX8ARiriupMQsbGXtKdDyGzWry1YV3sycKw1qqmgH"; + + /* + libp2p_logger_add_class("dht_protocol"); + libp2p_logger_add_class("providerstore"); + libp2p_logger_add_class("peerstore"); + libp2p_logger_add_class("exporter"); + libp2p_logger_add_class("peer"); + */ + libp2p_logger_add_class("test_bitswap"); + libp2p_logger_add_class("null"); + libp2p_logger_add_class("online"); + libp2p_logger_add_class("multistream"); + libp2p_logger_add_class("secio"); + libp2p_logger_add_class("bitswap"); + libp2p_logger_add_class("bitswap_engine"); + libp2p_logger_add_class("bitswap_network"); + + char* ipfs_path = "/tmp/test1"; + char peer_ma_1[80] = "", *peer_id_2 = NULL; + struct IpfsNode* ipfs_node2 = NULL; + struct MultiAddress* ma_peer1 = NULL; + struct Libp2pVector* ma_vector2 = NULL; + struct Block* result = NULL; + struct Cid* cid = NULL; + + // create a MultiAddress of the GO peer (peer 1) + sprintf(peer_ma_1, "/ip4/%s/tcp/%d/ipfs/%s", remote_ip, remote_port, remote_peer_id); + ma_peer1 = multiaddress_new_from_string(peer_ma_1); + + // create my peer, peer 2 + libp2p_logger_debug("test_routing", "Firing up the client\n"); + ipfs_path = "/tmp/test2"; + ma_vector2 = libp2p_utils_vector_new(1); + libp2p_utils_vector_add(ma_vector2, ma_peer1); + drop_and_build_repository(ipfs_path, 4002, ma_vector2, &peer_id_2); + multiaddress_free(ma_peer1); + ipfs_node_online_new(ipfs_path, &ipfs_node2); + + if (!ipfs_cid_decode_hash_from_base58((unsigned char*)hello_world_hash, strlen(hello_world_hash), &cid)) + goto exit; + + sleep(3); + + // this does the heavy lifting... + if (!ipfs_node2->exchange->GetBlock(ipfs_node2->exchange, cid, &result)) { + libp2p_logger_error("test_bitswap", "GetBlock returned false\n"); + goto exit; + } + + if (result == NULL) { + libp2p_logger_error("test_bitswap", "GetBlock returned NULL"); + goto exit; + } + + if (result->cid == NULL) { + libp2p_logger_error("test_bitswap", "GetBlock returned an object with no CID"); + goto exit; + } + + if (cid->hash_length != result->cid->hash_length) { + libp2p_logger_error("test_bitswap", "Node hash sizes do not match. Should be %lu but is %lu\n", strlen(hello_world_hash), result->cid->hash_length); + goto exit; + } + + retVal = 1; + exit: + if (peer_id_2 != NULL) + free(peer_id_2); + if (ma_vector2 != NULL) { + libp2p_utils_vector_free(ma_vector2); + } + if (cid != NULL) + ipfs_cid_free(cid); + ipfs_node_free(ipfs_node2); + if (ma_peer1 != NULL) + multiaddress_free(ma_peer1); + return retVal; +} + +/*** + * Attempt to retrieve a file from a known node + */ +int test_bitswap_serve_file_go_remote() { + int retVal = 0; + signal(SIGPIPE, SIG_IGN); + /*** + * This assumes a remote client asking for hello.txt + */ + // linux + //char* remote_peer_id = "QmRKm1d9kSCRpMFtLYpfhhCQ3DKuSSPJa3qn9wWXfwnWnY"; + //char* remote_ip = "10.211.55.4"; + //char* hello_world_hash = "QmYAXgX8ARiriupMQsbGXtKdDyGzWry1YV3sycKw1qqmgH"; + + /* + libp2p_logger_add_class("dht_protocol"); + libp2p_logger_add_class("providerstore"); + libp2p_logger_add_class("peerstore"); + libp2p_logger_add_class("exporter"); + libp2p_logger_add_class("peer"); + */ + libp2p_logger_add_class("test_bitswap"); + libp2p_logger_add_class("null"); + libp2p_logger_add_class("online"); + libp2p_logger_add_class("multistream"); + libp2p_logger_add_class("secio"); + libp2p_logger_add_class("bitswap"); + libp2p_logger_add_class("bitswap_engine"); + libp2p_logger_add_class("bitswap_network"); + + char* ipfs_path = "/tmp/ipfs_2"; + char *peer_id_2 = NULL; + struct FSRepo* fs_repo; + pthread_t thread2; + + // create my peer, peer 2 + libp2p_logger_debug("test_bitswap", "Firing up the client\n"); + drop_build_open_repo(ipfs_path, &fs_repo, "config.test2.wo_journal"); + ipfs_repo_fsrepo_free(fs_repo); + + // add file + char* bytes = "Hello, World!"; + char* filename = "hello.txt"; + create_file(filename, (unsigned char*)bytes, strlen(bytes)); + char* argv[] = { "ipfs" , "--config", ipfs_path, "add", filename}; + struct CliArguments args; + args.argc = 5; + args.argv = argv; + args.config_dir = ipfs_path; + args.verb_index = 3; + ipfs_import_files(&args); + + if (pthread_create(&thread2, NULL, test_daemon_start, (void*)ipfs_path) < 0) { + libp2p_logger_error("test_bitswap", "Unable to start thread 2\n"); + goto exit; + } + + sleep(120); + + retVal = 1; + exit: + ipfs_daemon_stop(); + if (peer_id_2 != NULL) + free(peer_id_2); + return retVal; +} + diff --git a/test/scripts/test_1.sh b/test/scripts/test_1.sh index 3acbd4f..e14faf3 100755 --- a/test/scripts/test_1.sh +++ b/test/scripts/test_1.sh @@ -1,15 +1,38 @@ #!/bin/bash +#### +# Attempt to retrieve binary file from running daemon +# +#### + +source ./test_helpers.sh + +IPFS="../../main/ipfs --config /tmp/ipfs_1" + function pre { -echo 'in pre'; + rm -Rf /tmp/ipfs_1 + eval "$IPFS" init; + check_failure "pre" $? + cp ../config.test1.wo_journal /tmp/ipfs_1/config } function post { -echo 'in post'; + rm -Rf /tmp/ipfs_1; +# rm hello.bin; } function body { - echo 'in body'; + create_binary_file; + eval "$IPFS" add hello.bin + check_failure "add hello.bin" $? + + #start the daemon + eval "../../main/ipfs --config /tmp/ipfs_1 daemon &" + daemon_id=$! + sleep 5 + + eval "$IPFS" cat QmSsV5T26CnCk3Yt7gtf6Bgyqwe4UtiaLiPbe9uUyr1nHd > hello2.bin + check_failure "cat" $? + + kill -9 $daemon_id } - - diff --git a/test/scripts/test_helpers.sh b/test/scripts/test_helpers.sh index 1fc699f..cfd858a 100644 --- a/test/scripts/test_helpers.sh +++ b/test/scripts/test_helpers.sh @@ -11,6 +11,17 @@ function create_hello_world { echo 'Hello, World!' > hello.txt } +#### +# Create a binary file +### +function create_binary_file { + rm hello.bin + for byte in `seq 0 256`; + do + printf "\\$(printf "%o" 0x$byte)" >> hello.bin + done +} + #### # Checks the return code and displays message if return code is not 0 # Param $1 name of function diff --git a/test/testit.c b/test/testit.c index e9cd413..43fcddc 100644 --- a/test/testit.c +++ b/test/testit.c @@ -42,15 +42,18 @@ const char* names[] = { "test_bitswap_new_free", "test_bitswap_peer_request_queue_new", "test_bitswap_retrieve_file", + "test_bitswap_retrieve_file_go_remote", "test_bitswap_retrieve_file_known_remote", "test_bitswap_retrieve_file_remote", "test_bitswap_retrieve_file_third_party", + "test_bitswap_serve_file_go_remote", "test_cid_new_free", "test_cid_cast_multihash", "test_cid_cast_non_multihash", "test_cid_protobuf_encode_decode", "test_core_api_startup_shutdown", "test_core_api_object_cat", + "test_core_api_object_cat_binary", "test_core_api_name_resolve", "test_core_api_name_resolve_1", "test_core_api_name_resolve_2", @@ -112,15 +115,18 @@ int (*funcs[])(void) = { test_bitswap_new_free, test_bitswap_peer_request_queue_new, test_bitswap_retrieve_file, + test_bitswap_retrieve_file_go_remote, test_bitswap_retrieve_file_known_remote, test_bitswap_retrieve_file_remote, test_bitswap_retrieve_file_third_party, + test_bitswap_serve_file_go_remote, test_cid_new_free, test_cid_cast_multihash, test_cid_cast_non_multihash, test_cid_protobuf_encode_decode, test_core_api_startup_shutdown, test_core_api_object_cat, + test_core_api_object_cat_binary, test_core_api_name_resolve, test_core_api_name_resolve_1, test_core_api_name_resolve_2,