2017-02-20 23:53:20 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "libp2p/peer/peer.h"
|
|
|
|
#include "libp2p/peer/peerstore.h"
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Includes Libp2pPeer, PeerEntry, Peerstore
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the basics of peer
|
|
|
|
*/
|
|
|
|
int test_peer() {
|
|
|
|
struct Libp2pPeer* obj = libp2p_peer_new();
|
|
|
|
if (obj == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
libp2p_peer_free(obj);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the peerstore
|
|
|
|
*/
|
|
|
|
int test_peerstore() {
|
2017-07-31 19:51:29 +00:00
|
|
|
|
|
|
|
// create a Peer
|
2017-07-31 17:49:41 +00:00
|
|
|
struct Libp2pPeer* peer = libp2p_peer_new();
|
2017-07-31 19:51:29 +00:00
|
|
|
peer->id = malloc(10);
|
|
|
|
strcpy(peer->id, "Qmabcdefg");
|
2017-07-31 17:49:41 +00:00
|
|
|
peer->id_size = strlen(peer->id);
|
2017-07-31 19:51:29 +00:00
|
|
|
|
|
|
|
// create a PeerStore
|
2017-07-31 17:49:41 +00:00
|
|
|
struct Peerstore* peerstore = libp2p_peerstore_new(peer);
|
2017-02-20 23:53:20 +00:00
|
|
|
struct PeerEntry* peer_entry = NULL;
|
|
|
|
struct PeerEntry* results = NULL;
|
|
|
|
int retVal = 0;
|
|
|
|
|
|
|
|
if (peerstore == NULL)
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
// add a peer entry to the peerstore
|
2017-07-31 19:51:29 +00:00
|
|
|
/*
|
2017-02-20 23:53:20 +00:00
|
|
|
peer_entry = libp2p_peer_entry_new();
|
|
|
|
peer_entry->peer = libp2p_peer_new();
|
|
|
|
peer_entry->peer->id_size = 6;
|
|
|
|
peer_entry->peer->id = malloc(peer_entry->peer->id_size);
|
|
|
|
memcpy(peer_entry->peer->id, "ABC123", peer_entry->peer->id_size);
|
|
|
|
peer_entry->peer->connection_type = CONNECTION_TYPE_NOT_CONNECTED;
|
2017-07-31 19:51:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!libp2p_peerstore_add_peer(peerstore, peer)) {
|
|
|
|
fprintf(stderr, "libp2p_peerstore_add_peer returned false\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
2017-02-20 23:53:20 +00:00
|
|
|
|
2017-07-31 19:51:29 +00:00
|
|
|
/*
|
2017-02-20 23:53:20 +00:00
|
|
|
if (!libp2p_peerstore_add_peer_entry(peerstore, peer_entry))
|
|
|
|
goto exit;
|
2017-07-31 19:51:29 +00:00
|
|
|
*/
|
2017-02-20 23:53:20 +00:00
|
|
|
|
|
|
|
// now try to retrieve it
|
2017-07-31 19:51:29 +00:00
|
|
|
results = libp2p_peerstore_get_peer_entry(peerstore, (unsigned char*)"Qmabcdefg", 9);
|
2017-02-20 23:53:20 +00:00
|
|
|
|
2017-07-31 19:51:29 +00:00
|
|
|
if (results == NULL || results->peer->id_size != 9) {
|
|
|
|
fprintf(stderr, "libp2p_peerstore_get_peer_entry returned NULL or was the wrong size\n");
|
2017-02-20 23:53:20 +00:00
|
|
|
goto exit;
|
2017-07-31 19:51:29 +00:00
|
|
|
}
|
2017-02-20 23:53:20 +00:00
|
|
|
|
|
|
|
// cleanup
|
|
|
|
retVal = 1;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
if (peerstore != NULL)
|
|
|
|
libp2p_peerstore_free(peerstore);
|
2017-07-31 17:49:41 +00:00
|
|
|
if (peer != NULL)
|
|
|
|
libp2p_peer_free(peer);
|
2017-02-20 23:53:20 +00:00
|
|
|
return retVal;
|
|
|
|
}
|
2017-04-17 04:46:52 +00:00
|
|
|
|
|
|
|
int test_peer_protobuf() {
|
2017-04-27 04:56:18 +00:00
|
|
|
int retVal = 0;
|
2017-04-17 04:46:52 +00:00
|
|
|
struct Libp2pPeer *peer = NULL, *peer_result = NULL;
|
|
|
|
struct MultiAddress* ma = NULL, *ma_result = NULL;
|
|
|
|
char* peer_id = "QmW8CYQuoJhgfxTeNVFWktGFnTRzdUAimerSsHaE4rUXk8";
|
2017-04-27 04:56:18 +00:00
|
|
|
unsigned char* protobuf = NULL;
|
2017-04-17 04:46:52 +00:00
|
|
|
size_t protobuf_size;
|
|
|
|
|
|
|
|
peer = libp2p_peer_new();
|
|
|
|
peer->id_size = strlen(peer_id);
|
|
|
|
peer->id = malloc(peer->id_size);
|
|
|
|
memcpy(peer->id, peer_id, peer->id_size);
|
|
|
|
peer->addr_head = libp2p_utils_linked_list_new();
|
|
|
|
ma = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/4001/ipfs/QmW8CYQuoJhgfxTeNVFWktGFnTRzdUAimerSsHaE4rUXk8/");
|
|
|
|
peer->addr_head->item = ma;
|
|
|
|
|
|
|
|
// protobuf
|
|
|
|
libp2p_peer_protobuf_encode_with_alloc(peer, &protobuf, &protobuf_size);
|
|
|
|
|
|
|
|
// unprotobuf
|
|
|
|
libp2p_peer_protobuf_decode(protobuf, protobuf_size, &peer_result);
|
|
|
|
ma_result = peer_result->addr_head->item;
|
|
|
|
|
|
|
|
if (strcmp(ma->string, ma_result->string) != 0) {
|
|
|
|
fprintf(stderr, "Results to not match: %s vs %s\n", ma->string, ma_result->string);
|
2017-04-27 04:56:18 +00:00
|
|
|
goto exit;
|
2017-04-17 04:46:52 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 04:56:18 +00:00
|
|
|
retVal = 1;
|
|
|
|
exit:
|
2017-04-27 13:14:19 +00:00
|
|
|
//multiaddress_free(ma);
|
2017-04-27 04:56:18 +00:00
|
|
|
libp2p_peer_free(peer);
|
|
|
|
libp2p_peer_free(peer_result);
|
|
|
|
if (protobuf != NULL)
|
|
|
|
free(protobuf);
|
|
|
|
return retVal;
|
2017-04-17 04:46:52 +00:00
|
|
|
}
|