c-libp2p/test/test_conn.h

190 lines
7.1 KiB
C
Raw Permalink 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-04-27 04:56:18 +00:00
int retVal = 0;
2017-02-13 22:41:31 +00:00
char* peer_id = "QmQSDGgxSVTkHmtT25rTzQtc5C1Yg8SpGK3BTws8YsJ4x3";
2017-10-25 17:43:27 +00:00
struct RsaPrivateKey* private_key = NULL;
2017-10-23 21:21:03 +00:00
struct Libp2pPeer* peer = libp2p_peer_new();
2017-10-25 17:28:53 +00:00
peer->id = malloc(strlen(peer_id)+1);
strcpy(peer->id, peer_id);
2017-10-23 21:21:03 +00:00
peer->id_size = strlen(peer_id);
struct Dialer* dialer = libp2p_conn_dialer_new(peer, NULL, private_key, NULL);
2017-02-13 18:26:41 +00:00
if (dialer == NULL)
2017-04-27 04:56:18 +00:00
goto exit;
retVal = 1;
exit:
2017-10-23 21:21:03 +00:00
libp2p_peer_free(peer);
libp2p_conn_dialer_free(dialer);
2017-04-27 04:56:18 +00:00
return retVal;
2017-02-13 18:26:41 +00:00
}
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
struct Dialer* dialer = NULL;
struct MultiAddress* destination_address = NULL;
2017-10-25 17:28:53 +00:00
struct Stream* stream = NULL;
2017-02-13 22:41:31 +00:00
char* result = NULL;
size_t result_size = 0;
2017-10-23 21:21:03 +00:00
struct Libp2pPeer* peer = libp2p_peer_new();
2017-02-13 22:41:31 +00:00
2017-10-25 17:28:53 +00:00
test_helper_get_id_from_config(config_dir, NULL, &peer->id);
if (peer->id == NULL)
2017-02-13 22:41:31 +00:00
goto exit;
2017-10-23 21:21:03 +00:00
peer->id_size = strlen((char*)peer->id);
2017-02-13 22:41:31 +00:00
dialer = libp2p_conn_dialer_new(peer, NULL, NULL, NULL);
2017-02-13 22:41:31 +00:00
if (dialer == NULL)
goto exit;
destination_address = multiaddress_new_from_string(destination_string);
if (destination_address == NULL)
goto exit;
// now try to dial
2017-10-25 17:28:53 +00:00
stream = libp2p_conn_dialer_get_connection(dialer, destination_address);
if (stream == NULL)
2017-02-13 22:41:31 +00:00
goto exit;
2017-03-02 21:14:52 +00:00
// clean up resources
retVal = 1;
exit:
if (result != NULL)
free(result);
2017-10-25 17:28:53 +00:00
libp2p_peer_free(peer);
2017-03-02 21:14:52 +00:00
multiaddress_free(destination_address);
libp2p_conn_dialer_free(dialer);
2017-10-25 17:28:53 +00:00
stream->close(stream->stream_context);
libp2p_stream_free(stream);
2017-03-02 21:14:52 +00:00
return retVal;
}
2017-10-25 17:28:53 +00:00
int test_dialer_join_swarm() {
int retVal = 0;
libp2p_logger_add_class("secio");
libp2p_logger_add_class("multistream");
2017-10-25 17:28:53 +00:00
// we need a dialer and a peer
struct Dialer* dialer = NULL;
// this is a base64 encoded private key. It makes it easier to test if it is in base64 form
// these were pulled from the GO version of ipfs
char* orig_priv_key = "CAASqQkwggSlAgEAAoIBAQCuW+8vGUb2n4xOcfPZLmfVAy6GNJ0sYrD/hVXwxBU1aBas+8lfAuLwYJXPCVBg65wZWYEbbWCevLFjwB/oZyJA1J1g+HohggH8QvuDH164FtSbgyHFip2SPR7oUHgSWRqfKXRJsVW/SPCfEt59S8JH99Q747dU9fvZKpelE9aDLf5yI8nj29TDy3c1RpkxfUwfgnbeoCwsDnakFmVdoSEp3Lnt3JlI05qE0bgvkWAaelcXSNQCmZzDwXeMk9y221FnBkL4Vs3v2lKmjLx+Qr37P/t78T+VxsjnGHPhbZTIMIjwwON6568d0j25Bj9v6biiz8iXzBR4Fmz1CQ0mqU5BAgMBAAECggEAc6EYX/29Z/SrEaLUeiUiSsuPYQUnbrYMd4gv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KeEW3/yESySnUbzpxDEjzYNsK74VaXHKCGI40fDRUqZxU/+qCrFf3xDfYS4r4wfFd2Jh+tn4NzSV/EhIr9KR/ZJW+TvGks+pWUJ3mhjPEvNtlt3M64/j2D0RP2aBQtoSpeezQ==";
char* orig_peer_id = "QmRKm1d9kSCRpMFtLYpfhhCQ3DKuSSPJa3qn9wWXfwnWnY";
char* remote_peer_id = "QmRjLCELimPe7aUdYRVNLD7UmB1CiJdJf8HLovKAB4KwmA";
size_t orig_peer_id_size = strlen(orig_peer_id);
2017-10-25 17:43:27 +00:00
struct RsaPrivateKey* rsa_private_key = NULL;
struct PrivateKey* priv = NULL;
2017-10-25 17:28:53 +00:00
size_t decode_base64_size = 0;
uint8_t* decode_base64 = NULL;
struct Libp2pPeer* local_peer = NULL;
struct Peerstore* peerstore = NULL;
// we need connection information to an existing ipfs instance
char* remote_swarm = "/ip4/10.211.55.2/tcp/4001";
struct Libp2pPeer* remote_peer = NULL;
struct MultiAddress* remote_ma = NULL;
// 1) take the private key and turn it back into bytes (decode base 64)
decode_base64_size = libp2p_crypto_encoding_base64_decode_size(strlen(orig_priv_key));
decode_base64 = (unsigned char*)malloc(decode_base64_size);
memset(decode_base64, 0, decode_base64_size);
if (!libp2p_crypto_encoding_base64_decode((unsigned char*)orig_priv_key, strlen(orig_priv_key), &decode_base64[0], decode_base64_size, &decode_base64_size))
goto exit;
2017-10-25 17:43:27 +00:00
if (!libp2p_crypto_private_key_protobuf_decode(decode_base64, decode_base64_size, &priv))
2017-10-25 17:28:53 +00:00
goto exit;
rsa_private_key = libp2p_crypto_private_key_to_rsa(priv);
2017-10-25 17:43:27 +00:00
2017-10-25 17:28:53 +00:00
// 2) make the local peer
local_peer = libp2p_peer_new();
local_peer->id = orig_peer_id;
local_peer->id_size = orig_peer_id_size;
local_peer->is_local = 1;
peerstore = libp2p_peerstore_new(local_peer);
// 3) make the dialer
dialer = libp2p_conn_dialer_new(local_peer, peerstore, rsa_private_key, NULL);
2017-10-25 17:28:53 +00:00
// 4) make the remote peer
remote_ma = multiaddress_new_from_string(remote_swarm);
remote_peer = libp2p_peer_new();
remote_peer->id_size = strlen(remote_peer_id);
remote_peer->id = malloc(remote_peer->id_size);
memcpy(remote_peer->id, remote_peer_id, remote_peer->id_size);
remote_peer->addr_head = libp2p_utils_linked_list_new();
remote_peer->addr_head->item = remote_ma;
// 5) attempt to dial
if (!libp2p_conn_dialer_join_swarm(dialer, remote_peer, 10))
goto exit;
retVal = 1;
exit:
if (decode_base64 != NULL)
free(decode_base64);
//libp2p_peer_free(local_peer);
//libp2p_peerstore_free(peerstore);
//libp2p_conn_dialer_free(dialer);
//libp2p_crypto_private_key_free(priv);
2017-10-25 17:28:53 +00:00
return retVal;
}
2017-03-02 21:14:52 +00:00
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;
2017-10-23 21:21:03 +00:00
struct Libp2pPeer* peer = libp2p_peer_new();
2017-03-02 21:14:52 +00:00
2017-10-23 21:21:03 +00:00
test_helper_get_id_from_config(config_dir, &private_key, &peer->id);
2017-03-02 21:14:52 +00:00
if (private_key == NULL)
2017-02-13 22:41:31 +00:00
goto exit;
2017-10-23 21:21:03 +00:00
peer->id_size = strlen((char*)peer->id);
dialer = libp2p_conn_dialer_new(peer, NULL, NULL, NULL);
2017-03-02 21:14:52 +00:00
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
2017-10-23 23:03:38 +00:00
stream = libp2p_conn_dialer_get_stream(dialer, peer, "multistream");
2017-03-02 21:14:52 +00:00
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-04-27 04:56:18 +00:00
if (stream != NULL) {
struct SessionContext session_context;
session_context.insecure_stream = stream;
stream->close(stream);
2017-04-27 04:56:18 +00:00
libp2p_net_multistream_stream_free(stream);
}
2017-02-13 22:41:31 +00:00
return retVal;
}