c-libp2p/test/test_conn.h

102 lines
2.6 KiB
C
Raw Normal View History

2017-02-13 18:26:41 +00:00
#include <stdlib.h>
#include "libp2p/conn/dialer.h"
2017-03-02 21:14:52 +00:00
#include "libp2p/net/stream.h"
2017-02-13 22:41:31 +00:00
#include "test_helper.h"
2017-02-13 18:26:41 +00:00
int test_dialer_new() {
2017-02-13 22:41:31 +00:00
char* peer_id = "QmQSDGgxSVTkHmtT25rTzQtc5C1Yg8SpGK3BTws8YsJ4x3";
struct PrivateKey* private_key = libp2p_crypto_private_key_new();
struct Dialer* dialer = libp2p_conn_dialer_new(peer_id, private_key);
2017-02-13 18:26:41 +00:00
if (dialer == NULL)
return 0;
libp2p_conn_dialer_free(dialer);
return 1;
}
2017-02-13 22:41:31 +00:00
int test_dialer_dial() {
int retVal = 0;
char* config_dir = "/home/parallels/.ipfs/config";
char* destination_string = "/ip4/192.210.179.217/tcp/4001/";
2017-02-13 22:41:31 +00:00
char* peer_id = NULL;
struct PrivateKey* private_key = NULL;
struct Dialer* dialer = NULL;
struct MultiAddress* destination_address = NULL;
struct Connection* conn = NULL;
char* result = NULL;
size_t result_size = 0;
test_helper_get_id_from_config(config_dir, &private_key, &peer_id);
if (private_key == NULL)
goto exit;
dialer = libp2p_conn_dialer_new(peer_id, private_key);
if (dialer == NULL)
goto exit;
destination_address = multiaddress_new_from_string(destination_string);
if (destination_address == NULL)
goto exit;
// now try to dial
conn = libp2p_conn_dialer_get_connection(dialer, destination_address);
if (conn == NULL)
goto exit;
2017-03-02 21:14:52 +00:00
// clean up resources
retVal = 1;
exit:
if (result != NULL)
free(result);
free(peer_id);
multiaddress_free(destination_address);
libp2p_conn_dialer_free(dialer);
libp2p_crypto_private_key_free(private_key);
libp2p_conn_connection_free(conn);
return retVal;
}
int test_dialer_dial_multistream() {
int retVal = 0;
char* config_dir = "/home/parallels/.ipfs/config";
char* destination_string = "/ip4/192.210.179.217/tcp/4001/";
char* peer_id = NULL;
struct PrivateKey* private_key = NULL;
struct Dialer* dialer = NULL;
struct MultiAddress* destination_address = NULL;
struct Stream* stream = NULL;
char* result = NULL;
size_t result_size = 0;
test_helper_get_id_from_config(config_dir, &private_key, &peer_id);
if (private_key == NULL)
2017-02-13 22:41:31 +00:00
goto exit;
2017-03-02 21:14:52 +00:00
dialer = libp2p_conn_dialer_new(peer_id, private_key);
if (dialer == NULL)
goto exit;
destination_address = multiaddress_new_from_string(destination_string);
if (destination_address == NULL)
2017-02-13 22:41:31 +00:00
goto exit;
2017-03-02 21:14:52 +00:00
// now try to dial
stream = libp2p_conn_dialer_get_stream(dialer, destination_address, "multistream");
if (stream == NULL)
2017-02-13 22:41:31 +00:00
goto exit;
2017-03-02 21:14:52 +00:00
// now ping
2017-02-13 22:41:31 +00:00
// clean up resources
retVal = 1;
exit:
if (result != NULL)
free(result);
free(peer_id);
2017-02-13 22:41:31 +00:00
multiaddress_free(destination_address);
libp2p_conn_dialer_free(dialer);
libp2p_crypto_private_key_free(private_key);
2017-03-02 21:14:52 +00:00
stream->close(stream);
2017-02-13 22:41:31 +00:00
return retVal;
}