c-ipfs/test/testit.c

237 lines
9 KiB
C
Raw Permalink Normal View History

#include <pthread.h>
2016-12-05 11:13:20 +00:00
#include "cid/test_cid.h"
#include "cmd/ipfs/test_init.h"
2017-09-13 10:02:59 +00:00
#include "core/test_api.h"
#include "core/test_ping.h"
#include "core/test_null.h"
#include "core/test_daemon.h"
#include "core/test_node.h"
2017-11-23 12:45:06 +00:00
#include "core/test_compat_go.h"
#include "exchange/test_bitswap.h"
2017-07-24 14:09:22 +00:00
#include "exchange/test_bitswap_request_queue.h"
2016-12-05 11:13:20 +00:00
#include "flatfs/test_flatfs.h"
2017-08-24 15:08:27 +00:00
#include "journal/test_journal.h"
2016-12-05 15:50:17 +00:00
#include "merkledag/test_merkledag.h"
2016-12-05 11:13:20 +00:00
#include "node/test_node.h"
#include "node/test_importer.h"
#include "node/test_resolver.h"
2016-12-05 11:13:20 +00:00
#include "repo/test_repo_bootstrap_peers.h"
#include "repo/test_repo_config.h"
#include "repo/test_repo_fsrepo.h"
#include "repo/test_repo_identity.h"
#include "routing/test_routing.h"
#include "routing/test_supernode.h"
2016-12-05 11:13:20 +00:00
#include "storage/test_ds_helper.h"
#include "storage/test_datastore.h"
#include "storage/test_blocks.h"
2016-12-19 19:19:43 +00:00
#include "storage/test_unixfs.h"
2017-04-27 05:10:25 +00:00
#include "libp2p/utils/logger.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;
2016-12-05 11:13:20 +00:00
int testit(const char* name, int (*func)(void)) {
2017-09-25 20:11:44 +00:00
fprintf(stderr, "TESTING %s...\n", name);
2016-12-05 11:13:20 +00:00
int retVal = func();
if (retVal)
2017-09-25 20:11:44 +00:00
fprintf(stderr, "%s success!\n", name);
2016-12-05 11:13:20 +00:00
else
2017-09-25 20:11:44 +00:00
fprintf(stderr, "** Uh oh! %s failed.**\n", name);
2016-12-05 11:13:20 +00:00
return retVal == 0;
}
int add_test(const char* name, int (*func)(void), int part_of_suite) {
// create a new test
struct test* t = (struct test*) malloc(sizeof(struct test));
t->name = name;
t->func = func;
t->part_of_suite = part_of_suite;
t->next = NULL;
if (last_test == NULL)
t->index = 0;
else
t->index = last_test->index + 1;
// place it in the collection
if (first_test == NULL) {
first_test = t;
} else {
last_test->next = t;
}
last_test = t;
if (last_test == NULL)
return 0;
return last_test->index;
}
int build_test_collection() {
add_test("test_bitswap_new_free", test_bitswap_new_free, 1);
add_test("test_bitswap_peer_request_queue_new", test_bitswap_peer_request_queue_new, 1);
add_test("test_bitswap_retrieve_file", test_bitswap_retrieve_file, 1);
add_test("test_bitswap_retrieve_file_known_remote", test_bitswap_retrieve_file_known_remote, 0);
add_test("test_bitswap_retrieve_file_remote", test_bitswap_retrieve_file_remote, 1);
add_test("test_bitswap_retrieve_file_third_party", test_bitswap_retrieve_file_third_party, 1);
add_test("test_cid_new_free", test_cid_new_free, 1);
add_test("test_cid_cast_multihash", test_cid_cast_multihash, 1);
add_test("test_cid_cast_non_multihash", test_cid_cast_non_multihash, 1);
add_test("test_cid_protobuf_encode_decode", test_cid_protobuf_encode_decode, 1);
add_test("test_core_api_startup_shutdown", test_core_api_startup_shutdown, 1);
add_test("test_core_api_object_cat", test_core_api_object_cat, 1);
add_test("test_core_api_object_cat_binary", test_core_api_object_cat_binary, 1);
add_test("test_core_api_object_cat_large_binary", test_core_api_object_cat_large_binary, 1);
add_test("test_core_api_name_resolve", test_core_api_name_resolve, 1);
add_test("test_core_api_name_resolve_1", test_core_api_name_resolve_1, 0);
add_test("test_core_api_name_resolve_2", test_core_api_name_resolve_2, 0);
add_test("test_core_api_name_resolve_3", test_core_api_name_resolve_3, 0);
add_test("test_daemon_startup_shutdown", test_daemon_startup_shutdown, 1);
add_test("test_datastore_list_journal", test_datastore_list_journal, 1);
add_test("test_journal_db", test_journal_db, 1);
add_test("test_journal_encode_decode", test_journal_encode_decode, 1);
add_test("test_journal_server_1", test_journal_server_1, 0);
add_test("test_journal_server_2", test_journal_server_2, 0);
add_test("test_repo_config_new", test_repo_config_new, 1);
add_test("test_repo_config_init", test_repo_config_init, 1);
add_test("test_repo_config_write", test_repo_config_write, 1);
add_test("test_repo_config_identity_new", test_repo_config_identity_new, 1);
add_test("test_repo_config_identity_private_key", test_repo_config_identity_private_key, 1);
add_test("test_repo_fsrepo_write_read_block", test_repo_fsrepo_write_read_block, 1);
add_test("test_repo_fsrepo_build", test_repo_fsrepo_build, 1);
add_test("test_routing_supernode_start", test_routing_supernode_start, 1);
add_test("test_get_init_command", test_get_init_command, 1);
add_test("test_import_small_file", test_import_small_file, 1);
add_test("test_import_large_file", test_import_large_file, 1);
add_test("test_repo_fsrepo_open_config", test_repo_fsrepo_open_config, 1);
add_test("test_flatfs_get_directory", test_flatfs_get_directory, 1);
add_test("test_flatfs_get_filename", test_flatfs_get_filename, 1);
add_test("test_flatfs_get_full_filename", test_flatfs_get_full_filename, 1);
add_test("test_ds_key_from_binary", test_ds_key_from_binary, 1);
add_test("test_blocks_new", test_blocks_new, 1);
add_test("test_repo_bootstrap_peers_init", test_repo_bootstrap_peers_init, 1);
add_test("test_ipfs_datastore_put", test_ipfs_datastore_put, 1);
add_test("test_node", test_node, 1);
add_test("test_node_link_encode_decode", test_node_link_encode_decode, 1);
add_test("test_node_encode_decode", test_node_encode_decode, 1);
add_test("test_node_peerstore", test_node_peerstore, 1);
add_test("test_merkledag_add_data", test_merkledag_add_data, 1);
add_test("test_merkledag_get_data", test_merkledag_get_data, 1);
add_test("test_merkledag_add_node", test_merkledag_add_node, 1);
add_test("test_merkledag_add_node_with_links", test_merkledag_add_node_with_links, 1);
add_test("test_namesys_publisher_publish", test_namesys_publisher_publish, 1);
add_test("test_namesys_resolver_resolve", test_namesys_resolver_resolve, 1);
add_test("test_resolver_get", test_resolver_get, 0); // not working (test directory does not exist)
add_test("test_resolver_remote_get", test_resolver_remote_get, 0); // not working (test directory does not exist)
add_test("test_routing_find_peer", test_routing_find_peer, 1);
add_test("test_routing_provide", test_routing_provide, 1);
add_test("test_routing_find_providers", test_routing_find_providers, 1);
add_test("test_routing_put_value", test_routing_put_value, 1);
add_test("test_routing_supernode_get_value", test_routing_supernode_get_value, 1);
add_test("test_routing_supernode_get_remote_value", test_routing_supernode_get_remote_value, 1);
add_test("test_routing_retrieve_file_third_party", test_routing_retrieve_file_third_party, 1);
add_test("test_routing_retrieve_large_file", test_routing_retrieve_large_file, 1);
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, 0); // socket connect failed
add_test("test_ping_remote", test_ping_remote, 0); // need to test more
add_test("test_null_add_provider", test_null_add_provider, 0); // need to test more
2017-11-23 12:45:06 +00:00
add_test("test_compat_go_join_swarm", test_compat_go_join_swarm, 0); // remote must be running
return 1;
}
2016-12-05 11:13:20 +00:00
/**
2017-05-11 18:53:52 +00:00
* Pull the next test name from the command line
* @param the count of arguments on the command line
* @param argv the command line arguments
* @param arg_number the current argument we want
* @returns a null terminated string of the next test or NULL
*/
char* get_test(int argc, char** argv, int arg_number) {
char* retVal = NULL;
char* ptr = NULL;
if (argc > arg_number) {
ptr = argv[arg_number];
if (ptr[0] == '\'')
ptr++;
retVal = ptr;
2017-08-03 19:35:06 +00:00
ptr = strchr(retVal, '\'');
2017-05-11 18:53:52 +00:00
if (ptr != NULL)
ptr[0] = 0;
}
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;
}
2017-05-11 18:53:52 +00:00
/**
* run certain tests or run all
2016-12-05 11:13:20 +00:00
*/
int main(int argc, char** argv) {
int counter = 0;
2016-12-05 18:11:22 +00:00
int tests_ran = 0;
char* test_name_wanted = NULL;
2017-05-11 18:53:52 +00:00
int certain_tests = 0;
int current_test_arg = 1;
2016-12-05 11:13:20 +00:00
if(argc > 1) {
2017-05-11 18:53:52 +00:00
certain_tests = 1;
2016-12-05 11:13:20 +00:00
}
build_test_collection();
if (certain_tests) {
// certain tests were passed on the command line
test_name_wanted = get_test(argc, argv, current_test_arg);
while (test_name_wanted != NULL) {
struct test* t = get_test_by_name(test_name_wanted);
if (t != NULL) {
tests_ran++;
counter += testit(t->name, t->func);
}
test_name_wanted = get_test(argc, argv, ++current_test_arg);
}
} else {
// 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;
2016-12-05 18:11:22 +00:00
}
2016-12-05 11:13:20 +00:00
}
2016-12-05 18:11:22 +00:00
if (tests_ran == 0)
2017-09-25 20:11:44 +00:00
fprintf(stderr, "***** No tests found *****\n");
2016-12-05 18:11:22 +00:00
else {
if (counter > 0) {
2017-09-25 20:11:44 +00:00
fprintf(stderr, "***** There were %d failed (out of %d) test(s) *****\n", counter, tests_ran);
2016-12-05 18:11:22 +00:00
} else {
2017-09-25 20:11:44 +00:00
fprintf(stderr, "All %d tests passed\n", tests_ran);
2016-12-05 18:11:22 +00:00
}
2016-12-05 11:13:20 +00:00
}
2017-04-27 05:10:25 +00:00
libp2p_logger_free();
2017-05-11 19:30:52 +00:00
fclose(stdin);
fclose(stdout);
fclose(stderr);
2016-12-05 11:13:20 +00:00
return 1;
}