Going through tests, verifying functionality

This commit is contained in:
jmjatlanta 2017-10-09 09:00:48 -05:00
parent 7f89e80d7b
commit c0855c9630
7 changed files with 284 additions and 193 deletions

View file

@ -34,4 +34,12 @@ int ipfs_merkledag_get(const unsigned char* hash, size_t hash_size, struct Hasht
*/ */
int ipfs_merkledag_get_by_multihash(const unsigned char* multihash, size_t multihash_length, struct HashtableNode** node, const struct FSRepo* fs_repo); int ipfs_merkledag_get_by_multihash(const unsigned char* multihash, size_t multihash_length, struct HashtableNode** node, const struct FSRepo* fs_repo);
/***
* Convert the data within a block to a HashtableNode
* @param block the block
* @param node_ptr where to put the results
* @returns true(1) on success, false(0) otherwise
*/
int ipfs_merkledag_convert_block_to_node(struct Block* block, struct HashtableNode** node_ptr);
#endif #endif

View file

@ -43,6 +43,16 @@ int ipfs_merkledag_convert_node_to_block(struct HashtableNode* node, struct Bloc
return 1; return 1;
} }
/***
* Convert the data within a block to a HashtableNode
* @param block the block
* @param node_ptr where to put the results
* @returns true(1) on success, false(0) otherwise
*/
int ipfs_merkledag_convert_block_to_node(struct Block* block, struct HashtableNode** node_ptr) {
return ipfs_hashtable_node_protobuf_decode(block->data, block->data_length, node_ptr);
}
/*** /***
* Adds a node to the dagService and blockService * Adds a node to the dagService and blockService
* @param node the node to add * @param node the node to add

View file

@ -111,14 +111,46 @@ int ipfs_routing_generic_get_value (ipfs_routing* routing, const unsigned char *
return retVal; return retVal;
} }
int ipfs_routing_offline_find_providers (ipfs_routing* offlineRouting, const unsigned char *key, size_t key_size, struct Libp2pVector** peers) // forward declaration
int ipfs_routing_online_find_remote_providers(struct IpfsRouting* routing, const unsigned char* key, size_t key_size, struct Libp2pVector** peers);
int ipfs_routing_offline_find_providers (ipfs_routing* routing, const unsigned char *key, size_t key_size, struct Libp2pVector** peers)
{ {
return ErrOffline; //if (routing->local_node->mode == MODE_API_AVAILABLE) {
//TODO: we need to ask the api to do this for us
//} else
//{
unsigned char* peer_id;
int peer_id_size;
struct Libp2pPeer *peer;
// see if we can find the key, and retrieve the peer who has it
if (!libp2p_providerstore_get(routing->local_node->providerstore, key, key_size, &peer_id, &peer_id_size)) {
libp2p_logger_debug("offline", "%s: Unable to find provider locally... Asking network\n", libp2p_peer_id_to_string(routing->local_node->identity->peer));
// we need to look remotely
return ipfs_routing_online_find_remote_providers(routing, key, key_size, peers);
}
libp2p_logger_debug("offline", "%s: Found provider locally. Searching for peer.\n", libp2p_peer_id_to_string(routing->local_node->identity->peer));
// now translate the peer id into a peer to get the multiaddresses
peer = libp2p_peerstore_get_peer(routing->local_node->peerstore, peer_id, peer_id_size);
free(peer_id);
if (peer == NULL) {
libp2p_logger_error("offline", "find_providers: We said we had the peer, but then we couldn't find it.\n");
return 0;
}
libp2p_logger_debug("offline", "%s: Found peer %s.\n", libp2p_peer_id_to_string(routing->local_node->identity->peer), libp2p_peer_id_to_string(peer));
*peers = libp2p_utils_vector_new(1);
libp2p_utils_vector_add(*peers, peer);
//}
return 1;
} }
int ipfs_routing_offline_find_peer (ipfs_routing* offlineRouting, const unsigned char *peer_id, size_t pid_size, struct Libp2pPeer **result) int ipfs_routing_offline_find_peer (ipfs_routing* offlineRouting, const unsigned char *peer_id, size_t pid_size, struct Libp2pPeer **result)
{ {
return ErrOffline; return 0;
} }
/** /**
@ -163,13 +195,63 @@ int ipfs_routing_offline_ping (ipfs_routing* offlineRouting, struct Libp2pPeer*
} }
/** /**
* For offline, this does nothing * Attempt to bootstrap, even if there is no API
* @param offlineRouting the interface * @param offlineRouting the interface
* @returns 0 * @returns 0
*/ */
int ipfs_routing_offline_bootstrap (ipfs_routing* offlineRouting) int ipfs_routing_offline_bootstrap (ipfs_routing* routing)
{ {
return 0; if (routing->local_node->mode == MODE_API_AVAILABLE) {
// do nothing
} else {
char* peer_id = NULL;
int peer_id_size = 0;
struct MultiAddress* address = NULL;
struct Libp2pPeer *peer = NULL;
// for each address in our bootstrap list, add info into the peerstore
struct Libp2pVector* bootstrap_peers = routing->local_node->repo->config->bootstrap_peers;
for(int i = 0; i < bootstrap_peers->total; i++) {
address = (struct MultiAddress*)libp2p_utils_vector_get(bootstrap_peers, i);
// attempt to get the peer ID
peer_id = multiaddress_get_peer_id(address);
if (peer_id != NULL) {
peer_id_size = strlen(peer_id);
peer = libp2p_peer_new();
peer->id_size = peer_id_size;
peer->id = malloc(peer->id_size + 1);
if (peer->id == NULL) { // out of memory?
libp2p_peer_free(peer);
free(peer_id);
return -1;
}
memcpy(peer->id, peer_id, peer->id_size);
peer->id[peer->id_size] = 0;
peer->addr_head = libp2p_utils_linked_list_new();
if (peer->addr_head == NULL) { // out of memory?
libp2p_peer_free(peer);
free(peer_id);
return -1;
}
peer->addr_head->item = multiaddress_copy(address);
libp2p_peerstore_add_peer(routing->local_node->peerstore, peer);
libp2p_peer_free(peer);
// now find it and attempt to connect
peer = libp2p_peerstore_get_peer(routing->local_node->peerstore, (const unsigned char*)peer_id, peer_id_size);
free(peer_id);
if (peer == NULL) {
return -1; // this should never happen
}
if (peer->sessionContext == NULL) { // should always be true unless we added it twice (TODO: we should prevent that earlier)
if (!libp2p_peer_connect(&routing->local_node->identity->private_key, peer, routing->local_node->peerstore, routing->local_node->repo->config->datastore, 2)) {
libp2p_logger_debug("online", "Attempted to bootstrap and connect to %s but failed. Continuing.\n", libp2p_peer_id_to_string(peer));
}
}
}
}
}
return 0;
} }
struct IpfsRouting* ipfs_routing_new_offline (struct IpfsNode* local_node, struct RsaPrivateKey *private_key) struct IpfsRouting* ipfs_routing_new_offline (struct IpfsNode* local_node, struct RsaPrivateKey *private_key)

2
test/blocks/test_block.h Normal file
View file

@ -0,0 +1,2 @@
#include "../test_helper.h"

View file

@ -4,6 +4,7 @@
#include "../routing/test_routing.h" // for test_routing_daemon_start #include "../routing/test_routing.h" // for test_routing_daemon_start
#include "libp2p/utils/vector.h" #include "libp2p/utils/vector.h"
#include "libp2p/utils/logger.h" #include "libp2p/utils/logger.h"
#include "ipfs/merkledag/merkledag.h" // for block to node conversion
#include "ipfs/exchange/bitswap/bitswap.h" #include "ipfs/exchange/bitswap/bitswap.h"
#include "ipfs/exchange/bitswap/message.h" #include "ipfs/exchange/bitswap/message.h"
#include "ipfs/importer/importer.h" #include "ipfs/importer/importer.h"
@ -127,13 +128,14 @@ int test_bitswap_retrieve_file()
struct Cid* cid = NULL; struct Cid* cid = NULL;
// build and open the new IPFS repository with no bootstrap peers // build and open the new IPFS repository with no bootstrap peers
os_utils_setenv("IPFS_PATH", ipfs_path, 1);
drop_and_build_repository(ipfs_path, 4001, NULL, NULL); drop_and_build_repository(ipfs_path, 4001, NULL, NULL);
ipfs_node_online_new(ipfs_path, &localNode); ipfs_node_offline_new(ipfs_path, &localNode);
// add a file // add a file
localNode->routing->Bootstrap(localNode->routing); unsigned char bytes[256];
ipfs_import_file(NULL, "/home/parallels/ipfstest/hello_world.txt", &node, localNode, &bytes_written, 0); create_bytes(&bytes[0], 256);
create_file("/tmp/test_file.bin", &bytes[0], 256);
ipfs_import_file(NULL, "/tmp/test_file.bin", &node, localNode, &bytes_written, 0);
// build the Cid from the node information // build the Cid from the node information
cid = ipfs_cid_new(0, node->hash, node->hash_size, CID_DAG_PROTOBUF); cid = ipfs_cid_new(0, node->hash, node->hash_size, CID_DAG_PROTOBUF);
@ -184,8 +186,9 @@ int test_bitswap_retrieve_file_remote() {
int thread1_started = 0; int thread1_started = 0;
struct MultiAddress* ma_peer1 = NULL; struct MultiAddress* ma_peer1 = NULL;
struct Libp2pVector* ma_vector2 = NULL; struct Libp2pVector* ma_vector2 = NULL;
struct HashtableNode* node = NULL; struct HashtableNode* node = NULL; // the file that was put in
struct Block* result = NULL; struct Block* result = NULL; // the block that comes back
struct HashtableNode* result_node = NULL; // the HashtableNode of the block that comes back
struct Cid* cid = NULL; struct Cid* cid = NULL;
// create peer 1 // create peer 1
@ -196,8 +199,11 @@ int test_bitswap_retrieve_file_remote() {
ma_peer1 = multiaddress_new_from_string(multiaddress_string); ma_peer1 = multiaddress_new_from_string(multiaddress_string);
// add a file // add a file
size_t bytes_written = 0; size_t bytes_written = 0;
ipfs_node_online_new(ipfs_path, &ipfs_node1); ipfs_node_offline_new(ipfs_path, &ipfs_node1);
ipfs_import_file(NULL, "/home/parallels/ipfstest/hello_world.txt", &node, ipfs_node1, &bytes_written, 0); unsigned char bytes[256];
create_bytes(bytes, 256);
create_file("/tmp/hello.bin", bytes, 256);
ipfs_import_file(NULL, "/tmp/hello.bin", &node, ipfs_node1, &bytes_written, 0);
// start the daemon in a separate thread // start the daemon in a separate thread
if (pthread_create(&thread1, NULL, test_daemon_start, (void*)ipfs_path) < 0) { if (pthread_create(&thread1, NULL, test_daemon_start, (void*)ipfs_path) < 0) {
libp2p_logger_error("test_bitswap", "Unable to start thread 1\n"); libp2p_logger_error("test_bitswap", "Unable to start thread 1\n");
@ -208,32 +214,33 @@ int test_bitswap_retrieve_file_remote() {
sleep(3); sleep(3);
// create my peer, peer 2 // create my peer, peer 2
libp2p_logger_debug("test_routing", "Firing up the client\n"); libp2p_logger_debug("test_bitswap", "Firing up the client\n");
ipfs_path = "/tmp/test2"; ipfs_path = "/tmp/test2";
ma_peer1 = multiaddress_new_from_string(multiaddress_string); ma_peer1 = multiaddress_new_from_string(multiaddress_string);
ma_vector2 = libp2p_utils_vector_new(1); ma_vector2 = libp2p_utils_vector_new(1);
libp2p_utils_vector_add(ma_vector2, ma_peer1); libp2p_utils_vector_add(ma_vector2, ma_peer1);
drop_and_build_repository(ipfs_path, 4002, ma_vector2, &peer_id_2); drop_and_build_repository(ipfs_path, 4002, ma_vector2, &peer_id_2);
multiaddress_free(ma_peer1); multiaddress_free(ma_peer1);
ipfs_node_online_new(ipfs_path, &ipfs_node2); ipfs_node_offline_new(ipfs_path, &ipfs_node2);
ipfs_node2->routing->Bootstrap(ipfs_node2->routing); ipfs_node2->routing->Bootstrap(ipfs_node2->routing);
// this does the heavy lifting... // this does the heavy lifting...
cid = ipfs_cid_new(0, node->hash, node->hash_size, CID_DAG_PROTOBUF); cid = ipfs_cid_new(0, node->hash, node->hash_size, CID_DAG_PROTOBUF);
if (!ipfs_node2->exchange->GetBlock(ipfs_node2->exchange, cid, &result)) { if (!ipfs_node2->exchange->GetBlock(ipfs_node2->exchange, cid, &result)) {
libp2p_logger_error("test_bitswap", "GetBlock returned false\n"); libp2p_logger_error("test_bitswap", "GetBlock returned false\n");
goto exit; goto exit;
} }
if (node->hash_size != result->cid->hash_length) { if (node->hash_size != result->cid->hash_length) {
libp2p_logger_error("test_bitswap", "Node hash sizes do not match. Should be %lu but is %lu\n", node->hash_size, result->cid->hash_length); libp2p_logger_error("test_bitswap", "Node hash sizes do not match. Should be %lu but is %lu\n", node->hash_size, result->cid->hash_length);
goto exit; goto exit;
} }
if (node->data_size != result->data_length) { // to compare sizes, we must convert the block back to a hashtable node
libp2p_logger_error("test_bitswap", "Result sizes do not match. Should be %lu but is %lu\n", node->data_size, result->data_length); ipfs_merkledag_convert_block_to_node(result, &result_node);
goto exit; if (node->data_size != result_node->data_size) {
libp2p_logger_error("test_bitswap", "Result sizes do not match. Should be %lu but is %lu\n", node->data_size, result->data_length);
goto exit;
} }
retVal = 1; retVal = 1;
@ -250,6 +257,8 @@ int test_bitswap_retrieve_file_remote() {
} }
if (node != NULL) if (node != NULL)
ipfs_hashtable_node_free(node); ipfs_hashtable_node_free(node);
if (result_node != NULL)
ipfs_hashtable_node_free(result_node);
if (result != NULL) if (result != NULL)
ipfs_block_free(result); ipfs_block_free(result);
if (cid != NULL) if (cid != NULL)
@ -421,7 +430,10 @@ int test_bitswap_retrieve_file_third_party() {
if (!ipfs_node_online_new(ipfs_path, &ipfs_node2)) if (!ipfs_node_online_new(ipfs_path, &ipfs_node2))
goto exit; goto exit;
ipfs_node2->routing->Bootstrap(ipfs_node2->routing); ipfs_node2->routing->Bootstrap(ipfs_node2->routing);
ipfs_import_file(NULL, "/home/parallels/ipfstest/hello_world.txt", &node, ipfs_node2, &bytes_written, 0); unsigned char bytes[256];
create_bytes(&bytes[0], 256);
create_file("/tmp/hello.bin", &bytes[0], 256);
ipfs_import_file(NULL, "/tmp/hello.bin", &node, ipfs_node2, &bytes_written, 0);
ipfs_node_free(ipfs_node2); ipfs_node_free(ipfs_node2);
// start the daemon in a separate thread // start the daemon in a separate thread
if (pthread_create(&thread2, NULL, test_daemon_start, (void*)ipfs_path) < 0) { if (pthread_create(&thread2, NULL, test_daemon_start, (void*)ipfs_path) < 0) {
@ -440,7 +452,7 @@ int test_bitswap_retrieve_file_third_party() {
libp2p_utils_vector_add(ma_vector3, ma_peer1); libp2p_utils_vector_add(ma_vector3, ma_peer1);
drop_and_build_repository(ipfs_path, 4003, ma_vector3, &peer_id_3); drop_and_build_repository(ipfs_path, 4003, ma_vector3, &peer_id_3);
multiaddress_free(ma_peer1); multiaddress_free(ma_peer1);
ipfs_node_online_new(ipfs_path, &ipfs_node3); ipfs_node_offline_new(ipfs_path, &ipfs_node3);
ipfs_node3->routing->Bootstrap(ipfs_node3->routing); ipfs_node3->routing->Bootstrap(ipfs_node3->routing);

View file

@ -4,4 +4,5 @@
./run_test.sh test_2.sh ./run_test.sh test_2.sh
./run_test.sh test_3.sh ./run_test.sh test_3.sh
./run_test.sh test_4.sh ./run_test.sh test_4.sh
./run_test.sh test_5.sh ./run_test.sh test_5.sh
./run_test.sh test_6.sh

View file

@ -27,7 +27,18 @@
#include "storage/test_unixfs.h" #include "storage/test_unixfs.h"
#include "libp2p/utils/logger.h" #include "libp2p/utils/logger.h"
#include "namesys/test_namesys.h" #include "namesys/test_namesys.h"
struct test {
int index;
const char* name;
int (*func)(void);
int part_of_suite;
struct test* next;
};
struct test* first_test = NULL;
struct test* last_test = NULL;
int testit(const char* name, int (*func)(void)) { int testit(const char* name, int (*func)(void)) {
fprintf(stderr, "TESTING %s...\n", name); fprintf(stderr, "TESTING %s...\n", name);
int retVal = func(); int retVal = func();
@ -38,145 +49,99 @@ int testit(const char* name, int (*func)(void)) {
return retVal == 0; return retVal == 0;
} }
const char* names[] = { int add_test(const char* name, int (*func)(void), int part_of_suite) {
"test_bitswap_new_free", // create a new test
"test_bitswap_peer_request_queue_new", struct test* t = (struct test*) malloc(sizeof(struct test));
"test_bitswap_retrieve_file", t->name = name;
"test_bitswap_retrieve_file_known_remote", t->func = func;
"test_bitswap_retrieve_file_remote", t->part_of_suite = part_of_suite;
"test_bitswap_retrieve_file_third_party", t->next = NULL;
"test_cid_new_free", if (last_test == NULL)
"test_cid_cast_multihash", t->index = 0;
"test_cid_cast_non_multihash", else
"test_cid_protobuf_encode_decode", t->index = last_test->index + 1;
"test_core_api_startup_shutdown", // place it in the collection
"test_core_api_object_cat", if (first_test == NULL) {
"test_core_api_name_resolve", first_test = t;
"test_core_api_name_resolve_1", } else {
"test_core_api_name_resolve_2", last_test->next = t;
"test_core_api_name_resolve_3", }
"test_daemon_startup_shutdown", last_test = t;
"test_datastore_list_journal",
"test_journal_db",
"test_journal_encode_decode",
"test_journal_server_1",
"test_journal_server_2",
"test_repo_config_new",
"test_repo_config_init",
"test_repo_config_write",
"test_repo_config_identity_new",
"test_repo_config_identity_private_key",
"test_repo_fsrepo_write_read_block",
"test_repo_fsrepo_build",
"test_routing_supernode_start",
"test_get_init_command",
"test_import_small_file",
"test_import_large_file",
"test_repo_fsrepo_open_config",
"test_flatfs_get_directory",
"test_flatfs_get_filename",
"test_flatfs_get_full_filename",
"test_ds_key_from_binary",
"test_blocks_new",
"test_repo_bootstrap_peers_init",
"test_ipfs_datastore_put",
"test_node",
"test_node_link_encode_decode",
"test_node_encode_decode",
"test_node_peerstore",
"test_merkledag_add_data",
"test_merkledag_get_data",
"test_merkledag_add_node",
"test_merkledag_add_node_with_links",
// 50 below
"test_namesys_publisher_publish",
"test_namesys_resolver_resolve",
"test_resolver_get",
"test_routing_find_peer",
"test_routing_provide",
"test_routing_find_providers",
"test_routing_put_value",
"test_routing_supernode_get_value",
"test_routing_supernode_get_remote_value",
"test_routing_retrieve_file_third_party",
"test_routing_retrieve_large_file",
"test_unixfs_encode_decode",
"test_unixfs_encode_smallfile",
"test_ping",
"test_ping_remote",
"test_null_add_provider",
"test_resolver_remote_get"
};
int (*funcs[])(void) = { if (last_test == NULL)
test_bitswap_new_free, return 0;
test_bitswap_peer_request_queue_new, return last_test->index;
test_bitswap_retrieve_file, }
test_bitswap_retrieve_file_known_remote,
test_bitswap_retrieve_file_remote, int build_test_collection() {
test_bitswap_retrieve_file_third_party, add_test("test_bitswap_new_free", test_bitswap_new_free, 1);
test_cid_new_free, add_test("test_bitswap_peer_request_queue_new", test_bitswap_peer_request_queue_new, 1);
test_cid_cast_multihash, add_test("test_bitswap_retrieve_file", test_bitswap_retrieve_file, 1);
test_cid_cast_non_multihash, add_test("test_bitswap_retrieve_file_known_remote", test_bitswap_retrieve_file_known_remote, 0);
test_cid_protobuf_encode_decode, add_test("test_bitswap_retrieve_file_remote", test_bitswap_retrieve_file_remote, 1);
test_core_api_startup_shutdown, add_test("test_bitswap_retrieve_file_third_party", test_bitswap_retrieve_file_third_party, 1);
test_core_api_object_cat, add_test("test_cid_new_free", test_cid_new_free, 1);
test_core_api_name_resolve, add_test("test_cid_cast_multihash", test_cid_cast_multihash, 1);
test_core_api_name_resolve_1, add_test("test_cid_cast_non_multihash", test_cid_cast_non_multihash, 1);
test_core_api_name_resolve_2, add_test("test_cid_protobuf_encode_decode", test_cid_protobuf_encode_decode, 1);
test_core_api_name_resolve_3, add_test("test_core_api_startup_shutdown", test_core_api_startup_shutdown, 1);
test_daemon_startup_shutdown, add_test("test_core_api_object_cat", test_core_api_object_cat, 1);
test_datastore_list_journal, add_test("test_core_api_name_resolve", test_core_api_name_resolve, 1);
test_journal_db, add_test("test_core_api_name_resolve_1", test_core_api_name_resolve_1, 1);
test_journal_encode_decode, add_test("test_core_api_name_resolve_2", test_core_api_name_resolve_2, 1);
test_journal_server_1, add_test("test_core_api_name_resolve_3", test_core_api_name_resolve_3, 1);
test_journal_server_2, add_test("test_daemon_startup_shutdown", test_daemon_startup_shutdown, 1);
test_repo_config_new, add_test("test_datastore_list_journal", test_datastore_list_journal, 1);
test_repo_config_init, add_test("test_journal_db", test_journal_db, 1);
test_repo_config_write, add_test("test_journal_encode_decode", test_journal_encode_decode, 1);
test_repo_config_identity_new, add_test("test_journal_server_1", test_journal_server_1, 1);
test_repo_config_identity_private_key, add_test("test_journal_server_2", test_journal_server_2, 1);
test_repo_fsrepo_write_read_block, add_test("test_repo_config_new", test_repo_config_new, 1);
test_repo_fsrepo_build, add_test("test_repo_config_init", test_repo_config_init, 1);
test_routing_supernode_start, add_test("test_repo_config_write", test_repo_config_write, 1);
test_get_init_command, add_test("test_repo_config_identity_new", test_repo_config_identity_new, 1);
test_import_small_file, add_test("test_repo_config_identity_private_key", test_repo_config_identity_private_key, 1);
test_import_large_file, add_test("test_repo_fsrepo_write_read_block", test_repo_fsrepo_write_read_block, 1);
test_repo_fsrepo_open_config, add_test("test_repo_fsrepo_build", test_repo_fsrepo_build, 1);
test_flatfs_get_directory, add_test("test_routing_supernode_start", test_routing_supernode_start, 1);
test_flatfs_get_filename, add_test("test_get_init_command", test_get_init_command, 1);
test_flatfs_get_full_filename, add_test("test_import_small_file", test_import_small_file, 1);
test_ds_key_from_binary, add_test("test_import_large_file", test_import_large_file, 1);
test_blocks_new, add_test("test_repo_fsrepo_open_config", test_repo_fsrepo_open_config, 1);
test_repo_bootstrap_peers_init, add_test("test_flatfs_get_directory", test_flatfs_get_directory, 1);
test_ipfs_datastore_put, add_test("test_flatfs_get_filename", test_flatfs_get_filename, 1);
test_node, add_test("test_flatfs_get_full_filename", test_flatfs_get_full_filename, 1);
test_node_link_encode_decode, add_test("test_ds_key_from_binary", test_ds_key_from_binary, 1);
test_node_encode_decode, add_test("test_blocks_new", test_blocks_new, 1);
test_node_peerstore, add_test("test_repo_bootstrap_peers_init", test_repo_bootstrap_peers_init, 1);
test_merkledag_add_data, add_test("test_ipfs_datastore_put", test_ipfs_datastore_put, 1);
test_merkledag_get_data, add_test("test_node", test_node, 1);
test_merkledag_add_node, add_test("test_node_link_encode_decode", test_node_link_encode_decode, 1);
test_merkledag_add_node_with_links, add_test("test_node_encode_decode", test_node_encode_decode, 1);
// 50 below add_test("test_node_peerstore", test_node_peerstore, 1);
test_namesys_publisher_publish, add_test("test_merkledag_add_data", test_merkledag_add_data, 1);
test_namesys_resolver_resolve, add_test("test_merkledag_get_data", test_merkledag_get_data, 1);
test_resolver_get, add_test("test_merkledag_add_node", test_merkledag_add_node, 1);
test_routing_find_peer, add_test("test_merkledag_add_node_with_links", test_merkledag_add_node_with_links, 1);
test_routing_provide, // 50 below
test_routing_find_providers, add_test("test_namesys_publisher_publish", test_namesys_publisher_publish, 1);
test_routing_put_value, add_test("test_namesys_resolver_resolve", test_namesys_resolver_resolve, 1);
test_routing_supernode_get_value, add_test("test_resolver_get", test_resolver_get, 1);
test_routing_supernode_get_remote_value, add_test("test_routing_find_peer", test_routing_find_peer, 1);
test_routing_retrieve_file_third_party, add_test("test_routing_provide", test_routing_provide, 1);
test_routing_retrieve_large_file, add_test("test_routing_find_providers", test_routing_find_providers, 1);
test_unixfs_encode_decode, add_test("test_routing_put_value", test_routing_put_value, 1);
test_unixfs_encode_smallfile, add_test("test_routing_supernode_get_value", test_routing_supernode_get_value, 1);
test_ping, add_test("test_routing_supernode_get_remote_value", test_routing_supernode_get_remote_value, 1);
test_ping_remote, add_test("test_routing_retrieve_file_third_party", test_routing_retrieve_file_third_party, 1);
test_null_add_provider, add_test("test_routing_retrieve_large_file", test_routing_retrieve_large_file, 1);
test_resolver_remote_get add_test("test_unixfs_encode_decode", test_unixfs_encode_decode, 1);
}; add_test("test_unixfs_encode_smallfile", test_unixfs_encode_smallfile, 1);
add_test("test_ping", test_ping, 1);
add_test("test_ping_remote", test_ping_remote, 1);
add_test("test_null_add_provider", test_null_add_provider, 1);
return add_test("test_resolver_remote_get", test_resolver_remote_get, 1);
}
/** /**
* Pull the next test name from the command line * Pull the next test name from the command line
@ -192,8 +157,7 @@ char* get_test(int argc, char** argv, int arg_number) {
ptr = argv[arg_number]; ptr = argv[arg_number];
if (ptr[0] == '\'') if (ptr[0] == '\'')
ptr++; ptr++;
retVal = malloc(strlen(ptr) + 1); retVal = ptr;
strcpy(retVal, ptr);
ptr = strchr(retVal, '\''); ptr = strchr(retVal, '\'');
if (ptr != NULL) if (ptr != NULL)
ptr[0] = 0; ptr[0] = 0;
@ -201,44 +165,56 @@ char* get_test(int argc, char** argv, int arg_number) {
return retVal; return retVal;
} }
struct test* get_test_by_index(int index) {
struct test* current = first_test;
while (current != NULL && current->index != index) {
current = current->next;
}
return current;
}
struct test* get_test_by_name(const char* name) {
struct test* current = first_test;
while (current != NULL && strcmp(current->name, name) != 0) {
current = current->next;
}
return current;
}
/** /**
* run certain tests or run all * run certain tests or run all
*/ */
int main(int argc, char** argv) { int main(int argc, char** argv) {
int counter = 0; int counter = 0;
int tests_ran = 0; int tests_ran = 0;
char* test_wanted = NULL; char* test_name_wanted = NULL;
int certain_tests = 0; int certain_tests = 0;
int current_test_arg = 1; int current_test_arg = 1;
if(argc > 1) { if(argc > 1) {
certain_tests = 1; certain_tests = 1;
} }
int array_length = sizeof(funcs) / sizeof(funcs[0]); build_test_collection();
int array2_length = sizeof(names) / sizeof(names[0]); if (certain_tests) {
if (array_length != array2_length) { // certain tests were passed on the command line
fprintf(stderr, "Test arrays are not of the same length. Funcs: %d, Names: %d\n", array_length, array2_length); test_name_wanted = get_test(argc, argv, current_test_arg);
} while (test_name_wanted != NULL) {
test_wanted = get_test(argc, argv, current_test_arg); struct test* t = get_test_by_name(test_name_wanted);
while (!certain_tests || test_wanted != NULL) { if (t != NULL) {
for (int i = 0; i < array_length; i++) { tests_ran++;
if (certain_tests) { counter += testit(t->name, t->func);
// get the test we currently want from the command line
const char* currName = names[i];
if (strcmp(currName, test_wanted) == 0) {
tests_ran++;
counter += testit(names[i], funcs[i]);
}
} }
else test_name_wanted = get_test(argc, argv, ++current_test_arg);
if (!certain_tests) { }
tests_ran++; } else {
counter += testit(names[i], funcs[i]); // run all tests that are part of this test suite
} struct test* current = first_test;
while (current != NULL) {
if (current->part_of_suite) {
tests_ran++;
counter += testit(current->name, current->func);
}
current = current->next;
} }
if (!certain_tests) // we did them all, not certain ones
break;
free(test_wanted);
test_wanted = get_test(argc, argv, ++current_test_arg);
} }
if (tests_ran == 0) if (tests_ran == 0)
fprintf(stderr, "***** No tests found *****\n"); fprintf(stderr, "***** No tests found *****\n");