c-multiaddr/test_multiaddr.h

201 lines
4.9 KiB
C
Raw Normal View History

2017-02-20 20:58:19 +00:00
#pragma once
#include "multiaddr/multiaddr.h"
2017-04-17 04:46:07 +00:00
#include "multiaddr/varhexutils.h"
2017-02-20 20:58:19 +00:00
int test_new_from_string() {
struct MultiAddress* a = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/8080/");
printf("Number of Bytes: %lu, Bytes: ", a->bsize);
for(int i = 0; i < a->bsize; i++) {
printf("%02x ", a->bytes[i]);
}
printf(" End of bytes\n");
multiaddress_free(a);
return 1;
}
int test_full() {
char addrstr[100];
strcpy(addrstr,"/ip4/192.168.1.1/");
2017-02-20 20:58:19 +00:00
printf("INITIAL: %s\n",addrstr);
struct MultiAddress* a;
a= multiaddress_new_from_string(addrstr);
2017-04-17 04:46:07 +00:00
unsigned char* tmp = Var_To_Hex(a->bytes, a->bsize);
printf("TEST BYTES: %s\n", tmp);
free(tmp);
2017-02-20 20:58:19 +00:00
//Remember, Decapsulation happens from right to left, never in reverse!
printf("A STRING:%s\n",a->string);
multiaddress_encapsulate(a,"/udp/3333/");
2017-02-20 20:58:19 +00:00
printf("A STRING ENCAPSULATED:%s\n",a->string);
2017-04-17 04:46:07 +00:00
tmp = Var_To_Hex(a->bytes, a->bsize);
printf("TEST BYTES: %s\n", tmp);
free(tmp);
2017-02-20 20:58:19 +00:00
multiaddress_decapsulate(a,"udp");
printf("A STRING DECAPSULATED UDP:%s\n",a->string);
2017-04-17 04:46:07 +00:00
tmp = Var_To_Hex(a->bytes, a->bsize);
printf("TEST BYTES: %s\n", tmp);
free(tmp);
multiaddress_encapsulate(a,"/udp/3333/");
printf("A STRING ENCAPSULATED UDP: %s\n",a->string);
multiaddress_encapsulate(a,"/ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG");
printf("A STRING ENCAPSULATED IPFS:%s\n",a->string);
2017-04-17 04:46:07 +00:00
tmp = Var_To_Hex(a->bytes, a->bsize);
printf("TEST BYTES: %s\n", tmp);
free(tmp);
printf("TEST BYTE SIZE: %lu\n",a->bsize);
2017-02-20 20:58:19 +00:00
struct MultiAddress* beta;
beta = multiaddress_new_from_bytes(a->bytes,a->bsize);
printf("B STRING: %s\n",beta->string);
multiaddress_free(a);
multiaddress_free(beta);
return 1;
}
2017-02-20 22:45:48 +00:00
int test_hex_to_var() {
size_t d;
unsigned char* result = Hex_To_Var("04", &d);
if (d != 1)
return 0;
if (result[0] != 4)
return 0;
if (result != NULL)
free(result);
return 1;
}
int test_int_to_hex() {
int val = 2555351;
char* result = Int_To_Hex(val);
int retVal = Hex_To_Int(result);
if (retVal != val)
return 0;
return 1;
2017-02-20 20:58:19 +00:00
}
int test_multiaddr_utils() {
2017-04-17 04:46:07 +00:00
int retVal = 0;
struct MultiAddress* addr = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/4001/");
if (!multiaddress_is_ip(addr)) {
fprintf(stderr, "The address should be an IP\n");
return 0;
}
char* ip = NULL;
multiaddress_get_ip_address(addr, &ip);
if (ip == NULL) {
fprintf(stderr, "get_ip_address returned NULL\n");
2017-04-17 04:46:07 +00:00
goto exit;
}
if(strcmp(ip, "127.0.0.1") != 0) {
fprintf(stderr, "ip addresses are not equal\n");
2017-04-17 04:46:07 +00:00
goto exit;
}
int port = multiaddress_get_ip_port(addr);
if (port != 4001) {
fprintf(stderr, "port incorrect. %d was returned instead of %d\n", port, 4001);
2017-04-17 04:46:07 +00:00
goto exit;
}
2017-04-17 04:46:07 +00:00
retVal = 1;
exit:
if (ip != NULL)
free(ip);
if (addr != NULL)
multiaddress_free(addr);
return retVal;
}
2017-04-03 16:54:15 +00:00
int test_multiaddr_peer_id() {
2017-04-17 04:46:07 +00:00
char* orig_address = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";
2017-04-03 16:54:15 +00:00
char full_string[255];
char* result = NULL;
2017-04-17 04:46:07 +00:00
char* bytes = NULL;
2017-04-03 16:54:15 +00:00
int retVal = 0;
2017-04-17 04:46:07 +00:00
struct MultiAddress *addr = NULL, *addr2 = NULL;
2017-04-03 16:54:15 +00:00
2017-04-17 04:46:07 +00:00
sprintf(full_string, "/ip4/127.0.0.1/tcp/4001/ipfs/%s/", orig_address);
2017-04-03 16:54:15 +00:00
addr = multiaddress_new_from_string(full_string);
result = multiaddress_get_peer_id(addr);
if (result == NULL || strncmp(result, orig_address, strlen(orig_address)) != 0)
goto exit;
2017-04-17 04:46:07 +00:00
result = NULL;
// switch to bytes and back again to verify the peer id follows...
// 1. display the original bytes
result = Var_To_Hex(addr->bytes, addr->bsize);
fprintf(stderr, "Original Bytes: %s\n", result);
free(result);
result = NULL;
// make a new MultiAddress from bytes
bytes = malloc(addr->bsize);
memcpy(bytes, addr->bytes, addr->bsize);
addr2 = multiaddress_new_from_bytes(bytes, addr->bsize);
free(bytes);
bytes = NULL;
// 2. Display the resultant bytes
result = Var_To_Hex(addr2->bytes, addr2->bsize);
fprintf(stderr, "New Bytes: %s\n", result);
free(result);
result = NULL;
if (strcmp(full_string, addr2->string) != 0) {
fprintf(stderr, "Original string was %s but new string is %s\n", full_string, addr2->string);
goto exit;
}
int port = multiaddress_get_ip_port(addr2);
if (port != 4001) {
fprintf(stderr, "Original string had port 4001, but now reporting %d\n", port);
goto exit;
}
2017-04-03 16:54:15 +00:00
retVal = 1;
exit:
if (addr != NULL)
multiaddress_free(addr);
2017-04-17 04:46:07 +00:00
if (addr2 != NULL)
multiaddress_free(addr2);
if (result != NULL)
free(result);
if (bytes != NULL)
free(bytes);
2017-04-03 16:54:15 +00:00
return retVal;
}
int test_multiaddr_bytes() {
int retVal = 0;
char* orig_address = "/ip4/127.0.0.1/tcp/4001/";
struct MultiAddress *orig = NULL, *result = NULL;
orig = multiaddress_new_from_string(orig_address);
result = multiaddress_new_from_bytes(orig->bytes, orig->bsize);
if (strcmp(orig_address, result->string) != 0) {
fprintf(stderr, "%s does not equal %s\n", orig_address, result->string);
goto exit;
}
retVal = 1;
exit:
if (orig != NULL)
multiaddress_free(orig);
if (result != NULL)
multiaddress_free(result);
return retVal;
}