test_multiaddr.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #pragma once
  2. #include "multiaddr/multiaddr.h"
  3. #include "multiaddr/varhexutils.h"
  4. int test_new_like_libp2p() {
  5. int retVal = 0;
  6. char* ip = "10.211.55.2";
  7. int port = 4001;
  8. char str[strlen(ip) + 50];
  9. sprintf(str, "/ip4/%s/tcp/%d/", ip, port);
  10. struct MultiAddress* ma_string = multiaddress_new_from_string(str);
  11. // convert to binary
  12. struct MultiAddress* ma_binary = multiaddress_new_from_bytes(ma_string->bytes, ma_string->bsize);
  13. if (strcmp(ma_string->string, ma_binary->string) != 0) {
  14. fprintf(stderr, "%s does not equal %s\n", ma_string->string, ma_binary->string);
  15. goto exit;
  16. }
  17. retVal = 1;
  18. exit:
  19. multiaddress_free(ma_string);
  20. multiaddress_free(ma_binary);
  21. return retVal;
  22. }
  23. int test_new_from_string() {
  24. struct MultiAddress* a = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/8080/");
  25. printf("Number of Bytes: %lu, Bytes: ", a->bsize);
  26. for(int i = 0; i < a->bsize; i++) {
  27. printf("%02x ", a->bytes[i]);
  28. }
  29. printf(" End of bytes\n");
  30. multiaddress_free(a);
  31. return 1;
  32. }
  33. int test_full() {
  34. char addrstr[100];
  35. strcpy(addrstr,"/ip4/192.168.1.1/");
  36. printf("INITIAL: %s\n",addrstr);
  37. struct MultiAddress* a;
  38. a= multiaddress_new_from_string(addrstr);
  39. unsigned char* tmp = Var_To_Hex((char*)a->bytes, a->bsize);
  40. printf("TEST BYTES: %s\n", tmp);
  41. free(tmp);
  42. //Remember, Decapsulation happens from right to left, never in reverse!
  43. printf("A STRING:%s\n",a->string);
  44. multiaddress_encapsulate(a,"/udp/3333/");
  45. printf("A STRING ENCAPSULATED:%s\n",a->string);
  46. tmp = Var_To_Hex((char*)a->bytes, a->bsize);
  47. printf("TEST BYTES: %s\n", tmp);
  48. free(tmp);
  49. multiaddress_decapsulate(a,"udp");
  50. printf("A STRING DECAPSULATED UDP:%s\n",a->string);
  51. tmp = Var_To_Hex((char*)a->bytes, a->bsize);
  52. printf("TEST BYTES: %s\n", tmp);
  53. free(tmp);
  54. multiaddress_encapsulate(a,"/udp/3333/");
  55. printf("A STRING ENCAPSULATED UDP: %s\n",a->string);
  56. multiaddress_encapsulate(a,"/ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG");
  57. printf("A STRING ENCAPSULATED IPFS:%s\n",a->string);
  58. tmp = Var_To_Hex((char*)a->bytes, a->bsize);
  59. printf("TEST BYTES: %s\n", tmp);
  60. free(tmp);
  61. printf("TEST BYTE SIZE: %lu\n",a->bsize);
  62. struct MultiAddress* beta;
  63. beta = multiaddress_new_from_bytes(a->bytes,a->bsize);
  64. printf("B STRING: %s\n",beta->string);
  65. multiaddress_free(a);
  66. multiaddress_free(beta);
  67. return 1;
  68. }
  69. int test_hex_to_var() {
  70. size_t d;
  71. unsigned char* result = Hex_To_Var("04", &d);
  72. if (d != 1)
  73. return 0;
  74. if (result[0] != 4)
  75. return 0;
  76. if (result != NULL)
  77. free(result);
  78. return 1;
  79. }
  80. int test_int_to_hex() {
  81. int val = 2555351;
  82. char* result = Int_To_Hex(val);
  83. int retVal = Hex_To_Int(result);
  84. if (retVal != val)
  85. return 0;
  86. return 1;
  87. }
  88. int test_multiaddr_utils() {
  89. int retVal = 0;
  90. struct MultiAddress* addr = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/4001/");
  91. if (!multiaddress_is_ip(addr)) {
  92. fprintf(stderr, "The address should be an IP\n");
  93. return 0;
  94. }
  95. char* ip = NULL;
  96. multiaddress_get_ip_address(addr, &ip);
  97. if (ip == NULL) {
  98. fprintf(stderr, "get_ip_address returned NULL\n");
  99. goto exit;
  100. }
  101. if(strcmp(ip, "127.0.0.1") != 0) {
  102. fprintf(stderr, "ip addresses are not equal\n");
  103. goto exit;
  104. }
  105. int port = multiaddress_get_ip_port(addr);
  106. if (port != 4001) {
  107. fprintf(stderr, "port incorrect. %d was returned instead of %d\n", port, 4001);
  108. goto exit;
  109. }
  110. retVal = 1;
  111. exit:
  112. if (ip != NULL)
  113. free(ip);
  114. if (addr != NULL)
  115. multiaddress_free(addr);
  116. return retVal;
  117. }
  118. int test_multiaddr_peer_id() {
  119. char* orig_address = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";
  120. char full_string[255];
  121. char* result = NULL;
  122. char* bytes = NULL;
  123. int retVal = 0, port = 0;
  124. struct MultiAddress *addr = NULL, *addr2 = NULL;
  125. sprintf(full_string, "/ip4/127.0.0.1/tcp/4001/ipfs/%s/", orig_address);
  126. addr = multiaddress_new_from_string(full_string);
  127. result = multiaddress_get_peer_id(addr);
  128. if (result == NULL || strcmp(result, orig_address) != 0)
  129. goto exit;
  130. free(result);
  131. result = NULL;
  132. // switch to bytes and back again to verify the peer id follows...
  133. // 1. display the original bytes
  134. result = (char*)Var_To_Hex((char*)addr->bytes, addr->bsize);
  135. fprintf(stderr, "Original Bytes: %s\n", result);
  136. free(result);
  137. result = NULL;
  138. // make a new MultiAddress from bytes
  139. bytes = malloc(addr->bsize);
  140. memcpy(bytes, addr->bytes, addr->bsize);
  141. addr2 = multiaddress_new_from_bytes((unsigned char*)bytes, addr->bsize);
  142. free(bytes);
  143. bytes = NULL;
  144. // 2. Display the resultant bytes
  145. result = (char*)Var_To_Hex((char*)addr2->bytes, addr2->bsize);
  146. fprintf(stderr, "New Bytes: %s\n", result);
  147. free(result);
  148. result = NULL;
  149. if (strcmp(full_string, addr2->string) != 0) {
  150. fprintf(stderr, "Original string was %s but new string is %s\n", full_string, addr2->string);
  151. goto exit;
  152. }
  153. port = multiaddress_get_ip_port(addr2);
  154. if (port != 4001) {
  155. fprintf(stderr, "Original string had port 4001, but now reporting %d\n", port);
  156. goto exit;
  157. }
  158. result = multiaddress_get_peer_id(addr2);
  159. if (strcmp(result, orig_address) != 0) {
  160. fprintf(stderr, "New peer id %s does not match %s", result, orig_address);
  161. goto exit;
  162. }
  163. free(result);
  164. result = NULL;
  165. retVal = 1;
  166. exit:
  167. if (addr != NULL)
  168. multiaddress_free(addr);
  169. if (addr2 != NULL)
  170. multiaddress_free(addr2);
  171. if (result != NULL)
  172. free(result);
  173. if (bytes != NULL)
  174. free(bytes);
  175. return retVal;
  176. }
  177. int test_multiaddr_get_peer_id() {
  178. const char* orig_address = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";
  179. char full_string[255] = "";
  180. char* result = NULL;
  181. int retVal = 0;
  182. struct MultiAddress *addr = NULL;
  183. sprintf(full_string, "/ip4/127.0.0.1/tcp/4001/ipfs/%s/", orig_address);
  184. addr = multiaddress_new_from_string(full_string);
  185. result = multiaddress_get_peer_id(addr);
  186. if (result == NULL)
  187. goto exit;
  188. if (strcmp(orig_address, result) != 0)
  189. goto exit;
  190. retVal = 1;
  191. exit:
  192. multiaddress_free(addr);
  193. free(result);
  194. result = NULL;
  195. return retVal;
  196. }
  197. int test_multiaddr_bytes() {
  198. int retVal = 0;
  199. char* orig_address = "/ip4/127.0.0.1/tcp/4001/";
  200. struct MultiAddress *orig = NULL, *result = NULL;
  201. orig = multiaddress_new_from_string(orig_address);
  202. result = multiaddress_new_from_bytes(orig->bytes, orig->bsize);
  203. if (strcmp(orig_address, result->string) != 0) {
  204. fprintf(stderr, "%s does not equal %s\n", orig_address, result->string);
  205. goto exit;
  206. }
  207. retVal = 1;
  208. exit:
  209. if (orig != NULL)
  210. multiaddress_free(orig);
  211. if (result != NULL)
  212. multiaddress_free(result);
  213. return retVal;
  214. }