c-libp2p/test/testit.c

218 lines
7.1 KiB
C
Raw Permalink Normal View History

2016-11-03 16:15:18 +00:00
#include <stdio.h>
2017-03-07 00:03:04 +00:00
#include "crypto/test_aes.h"
2016-11-07 20:11:58 +00:00
#include "crypto/test_rsa.h"
#include "crypto/test_base58.h"
2016-12-01 18:05:14 +00:00
#include "crypto/test_base32.h"
#include "crypto/test_key.h"
#include "crypto/test_ephemeral.h"
#include "crypto/test_mac.h"
#include "test_secio.h"
#include "test_mbedtls.h"
#include "test_multistream.h"
2017-02-13 18:26:41 +00:00
#include "test_conn.h"
2017-02-20 12:08:53 +00:00
#include "test_record.h"
2017-02-20 23:53:20 +00:00
#include "test_peer.h"
#include "test_yamux.h"
2017-11-20 00:29:40 +00:00
#include "test_net.h"
2017-04-27 05:06:43 +00:00
#include "libp2p/utils/logger.h"
2016-11-07 20:11:58 +00:00
2017-10-25 17:28:53 +00:00
struct test {
int index;
const char* name;
int (*func)(void);
int part_of_suite;
struct test* next;
};
2017-10-25 17:28:53 +00:00
struct test* first_test = NULL;
struct test* last_test = NULL;
2016-11-03 14:18:32 +00:00
int testit(const char* name, int (*func)(void)) {
2017-10-25 17:28:53 +00:00
fprintf(stderr, "TESTING %s...\n", name);
2016-11-03 14:18:32 +00:00
int retVal = func();
if (retVal)
2017-10-25 17:28:53 +00:00
fprintf(stderr, "%s success!\n", name);
2016-11-03 14:18:32 +00:00
else
2017-10-25 17:28:53 +00:00
fprintf(stderr, "** Uh oh! %s failed.**\n", name);
return retVal == 0;
}
int tear_down_test_collection() {
struct test* current_test = first_test;
while (current_test != NULL) {
struct test* next_test = current_test->next;
free(current_test);
current_test = next_test;
}
return 1;
}
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_public_der_to_private_der", test_public_der_to_private_der, 1);
add_test("test_mbedtls_varint_128_binary", test_mbedtls_varint_128_binary, 1);
add_test("test_mbedtls_varint_128_string", test_mbedtls_varint_128_string,1);
add_test("test_crypto_rsa_private_key_der", test_crypto_rsa_private_key_der, 1);
add_test("test_crypto_rsa_signing", test_crypto_rsa_signing, 1);
add_test("test_crypto_rsa_public_key_to_peer_id", test_crypto_rsa_public_key_to_peer_id,1);
add_test("test_crypto_x509_der_to_private2", test_crypto_x509_der_to_private2, 1);
add_test("test_crypto_x509_der_to_private", test_crypto_x509_der_to_private,1);
add_test("test_crypto_hashing_sha256", test_crypto_hashing_sha256,1);
//add_test("test_multihash_encode", func,1);
//add_test("test_multihash_decode", func,1);
//add_test("test_multihash_base58_encode_decode", func,1);
//add_test("test_multihash_base58_decode", func,1);
//add_test("test_multihash_size", func,1);
add_test("test_base58_encode_decode", test_base58_encode_decode,1);
add_test("test_base58_size", test_base58_size,1);
add_test("test_base58_max_size", test_base58_max_size,1);
add_test("test_base58_peer_address", test_base58_peer_address,1);
//add_test("test_mbedtls_pk_write_key_der", func,1);
//add_test("test_crypto_rsa_sign", func,1);
add_test("test_crypto_encoding_base32_encode", test_crypto_encoding_base32_encode,1);
add_test("test_protobuf_private_key", test_protobuf_private_key,1);
add_test("test_secio_handshake", test_secio_handshake,1);
add_test("test_secio_handshake_go", test_secio_handshake_go,1);
add_test("test_secio_encrypt_decrypt", test_secio_encrypt_decrypt,1);
add_test("test_secio_exchange_protobuf_encode", test_secio_exchange_protobuf_encode,1);
add_test("test_secio_encrypt_like_go", test_secio_encrypt_like_go,1);
add_test("test_multistream_connect", test_multistream_connect,1);
add_test("test_multistream_get_list", test_multistream_get_list,1);
add_test("test_ephemeral_key_generate", test_ephemeral_key_generate,1);
add_test("test_ephemeral_key_sign", test_ephemeral_key_sign,1);
add_test("test_dialer_new", test_dialer_new,1);
add_test("test_dialer_dial", test_dialer_dial,1);
add_test("test_dialer_join_swarm", test_dialer_join_swarm, 1);
add_test("test_dialer_dial_multistream", test_dialer_dial_multistream,1);
add_test("test_record_protobuf", test_record_protobuf,1);
add_test("test_record_make_put_record", test_record_make_put_record,1);
add_test("test_record_peer_protobuf", test_record_peer_protobuf,1);
add_test("test_record_message_protobuf", test_record_message_protobuf,1);
add_test("test_peer", test_peer,1);
add_test("test_peer_protobuf", test_peer_protobuf,1);
add_test("test_peerstore", test_peerstore,1);
add_test("test_aes", test_aes, 1);
add_test("test_yamux_stream_new", test_yamux_stream_new, 1);
add_test("test_yamux_identify", test_yamux_identify, 1);
add_test("test_yamux_incoming_protocol_request", test_yamux_incoming_protocol_request, 1);
2017-11-20 00:29:40 +00:00
add_test("test_net_server_startup_shutdown", test_net_server_startup_shutdown, 1);
add_test("test_yamux_client_server_connect", test_yamux_client_server_connect, 1);
2017-11-23 11:23:50 +00:00
add_test("test_yamux_client_server_multistream", test_yamux_client_server_multistream, 1);
add_test("test_yamux_multistream_server", test_yamux_multistream_server, 0);
add_test("test_yamux_multistream_client", test_yamux_multistream_client, 0);
2017-10-25 17:28:53 +00:00
return 1;
};
/**
* 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;
ptr = strchr(retVal, '\'');
if (ptr != NULL)
ptr[0] = 0;
}
2016-11-03 14:18:32 +00:00
return retVal;
}
2017-10-25 17:28:53 +00:00
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
*/
2016-11-03 14:18:32 +00:00
int main(int argc, char** argv) {
int counter = 0;
int tests_ran = 0;
2017-10-25 17:28:53 +00:00
char* test_name_wanted = NULL;
int certain_tests = 0;
int current_test_arg = 1;
if(argc > 1) {
2017-10-25 17:28:53 +00:00
certain_tests = 1;
}
2017-10-25 17:28:53 +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++;
2017-10-25 17:28:53 +00:00
counter += testit(t->name, t->func);
}
2017-10-25 17:28:53 +00:00
test_name_wanted = get_test(argc, argv, ++current_test_arg);
}
2017-10-25 17:28:53 +00:00
} 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++;
2017-10-25 17:28:53 +00:00
counter += testit(current->name, current->func);
}
2017-10-25 17:28:53 +00:00
current = current->next;
}
}
if (tests_ran == 0)
2017-10-25 17:28:53 +00:00
fprintf(stderr, "***** No tests found *****\n");
else {
2017-10-25 17:28:53 +00:00
if (counter > 0) {
fprintf(stderr, "***** There were %d failed (out of %d) test(s) *****\n", counter, tests_ran);
} else {
2017-10-25 17:28:53 +00:00
fprintf(stderr, "All %d tests passed\n", tests_ran);
}
}
2017-10-25 17:28:53 +00:00
tear_down_test_collection();
2017-04-27 05:06:43 +00:00
libp2p_logger_free();
2017-10-25 17:28:53 +00:00
fclose(stdin);
fclose(stdout);
fclose(stderr);
2016-11-03 14:18:32 +00:00
return 1;
}