threeNodes_test.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/Key.h"
  16. #include "io/FileWriter.h"
  17. #include "memory/MallocAllocator.h"
  18. #include "memory/Allocator.h"
  19. #include "util/Base32.h"
  20. #include "util/Checksum.h"
  21. #include "util/log/WriterLog.h"
  22. #include "test/TestFramework.h"
  23. #include "net/Ducttape_pvt.h"
  24. #include "wire/Headers.h"
  25. #include "wire/Ethernet.h"
  26. #include "interface/tuntap/TUNMessageType.h"
  27. #include "util/Hex.h"
  28. #include "util/events/Time.h"
  29. #include "util/events/Timeout.h"
  30. #include "dht/dhtcore/NodeStore.h"
  31. #include <stdio.h>
  32. #define TUNC 3
  33. #define TUNB 2
  34. #define TUNA 1
  35. static uint8_t incomingTunC(struct Message* msg, struct Interface* iface)
  36. {
  37. Assert_true(TUNMessageType_pop(msg, NULL) == Ethernet_TYPE_IP6);
  38. Message_shift(msg, -Headers_IP6Header_SIZE, NULL);
  39. printf("Message from TUN in node C [%s] [%d]\n", msg->bytes, msg->length);
  40. *((int*)iface->senderContext) = TUNC;
  41. return 0;
  42. }
  43. static uint8_t incomingTunB(struct Message* msg, struct Interface* iface)
  44. {
  45. Assert_true(TUNMessageType_pop(msg, NULL) == Ethernet_TYPE_IP6);
  46. Message_shift(msg, -Headers_IP6Header_SIZE, NULL);
  47. printf("Message from TUN in node B [%s]\n", msg->bytes);
  48. *((int*)iface->senderContext) = TUNB;
  49. return 0;
  50. }
  51. static uint8_t incomingTunA(struct Message* msg, struct Interface* iface)
  52. {
  53. Assert_true(TUNMessageType_pop(msg, NULL) == Ethernet_TYPE_IP6);
  54. Message_shift(msg, -Headers_IP6Header_SIZE, NULL);
  55. uint8_t buff[1024];
  56. Hex_encode(buff, 1024, msg->bytes, msg->length);
  57. printf("Message from TUN in node A [%s] [%d] [%s]\n", msg->bytes, msg->length, buff);
  58. *((int*)iface->senderContext) = TUNA;
  59. return 0;
  60. }
  61. struct ThreeNodes;
  62. typedef void (RunTest)(struct ThreeNodes* ctx);
  63. struct ThreeNodes
  64. {
  65. struct Interface tunIfC;
  66. struct TestFramework* nodeC;
  67. struct Interface tunIfB;
  68. struct TestFramework* nodeB;
  69. struct Interface tunIfA;
  70. struct TestFramework* nodeA;
  71. int messageFrom;
  72. struct Timeout* checkLinkageTimeout;
  73. struct Log* logger;
  74. struct EventBase* base;
  75. #define ThreeNodes_state_AB_COMPLETE 1
  76. #define ThreeNodes_state_CB_COMPLETE 2
  77. int state;
  78. uint64_t startTime;
  79. RunTest* runTest;
  80. Identity
  81. };
  82. static void notLinkedYet(struct ThreeNodes* ctx)
  83. {
  84. uint64_t now = Time_currentTimeMilliseconds(ctx->base);
  85. if ((now - ctx->startTime) > 5000) {
  86. Assert_failure("three nodes failed to link in 5 seconds");
  87. }
  88. }
  89. static void checkLinkage(void* vThreeNodes)
  90. {
  91. struct ThreeNodes* ctx = Identity_check((struct ThreeNodes*) vThreeNodes);
  92. if (ctx->nodeA->nodeStore->nodeCount < 1) { notLinkedYet(ctx); return; }
  93. if (ctx->state < ThreeNodes_state_AB_COMPLETE) {
  94. ctx->state = ThreeNodes_state_AB_COMPLETE;
  95. Log_debug(ctx->logger, "Link A and B complete");
  96. }
  97. if (ctx->nodeC->nodeStore->nodeCount < 1) { notLinkedYet(ctx); return; }
  98. if (ctx->state < ThreeNodes_state_CB_COMPLETE) {
  99. ctx->state = ThreeNodes_state_CB_COMPLETE;
  100. Log_debug(ctx->logger, "Link C and B complete");
  101. }
  102. if (ctx->nodeB->nodeStore->nodeCount < 2) { notLinkedYet(ctx); return; }
  103. Log_debug(ctx->logger, "Link C with B and A complete");
  104. Log_debug(ctx->logger, "\n\nSetup Complete\n\n");
  105. Timeout_clearTimeout(ctx->checkLinkageTimeout);
  106. ctx->runTest(ctx);
  107. }
  108. static void start(struct Allocator* alloc,
  109. struct Log* logger,
  110. struct EventBase* base,
  111. struct Random* rand,
  112. RunTest* runTest)
  113. {
  114. struct TestFramework* a =
  115. TestFramework_setUp("\xad\x7e\xa3\x26\xaa\x01\x94\x0a\x25\xbc\x9e\x01\x26\x22\xdb\x69"
  116. "\x4f\xd9\xb4\x17\x7c\xf3\xf8\x91\x16\xf3\xcf\xe8\x5c\x80\xe1\x4a",
  117. alloc, base, rand, logger);
  118. //"publicKey": "kmzm4w0kj9bswd5qmx74nu7kusv5pj40vcsmp781j6xxgpd59z00.k",
  119. //"ipv6": "fc41:94b5:0925:7ba9:3959:11ab:a006:367a",
  120. struct TestFramework* b =
  121. TestFramework_setUp("\xea\x8d\x34\x04\xa9\x7c\xe4\xf9\xca\x7e\x24\xe6\xf1\x85\xb9\x3f"
  122. "\x01\x37\xb7\xa1\xf5\x2c\xce\xc0\x2c\xae\x03\xf1\x83\x38\x13\x24",
  123. alloc, base, rand, logger);
  124. // This address was found by brute force for one which falls between A and C without being
  125. // closer in either direction (XOR is bidirectional address space distance)
  126. // ipv6: fc2e:3273:644e:426f:283d:e3c7:c87c:41c1
  127. struct TestFramework* c =
  128. TestFramework_setUp("\xd8\x54\x3e\x70\xb9\xae\x7c\x41\xbc\x18\xa4\x9a\x9c\xee\xca\x9c"
  129. "\xdc\x45\x01\x96\x6b\xbd\x7e\x76\xcf\x3a\x9f\xbc\x12\xed\x8b\xb4",
  130. alloc, base, rand, logger);
  131. //"publicKey": "vz21tg07061s8v9mckrvgtfds7j2u5lst8cwl6nqhp81njrh5wg0.k",
  132. //"ipv6": "fc1f:5b96:e1c5:625d:afde:2523:a7fa:383a",
  133. Log_debug(a->logger, "Linking A and B");
  134. TestFramework_linkNodes(b, a);
  135. Log_debug(a->logger, "Linking B and C");
  136. TestFramework_linkNodes(c, b);
  137. struct ThreeNodes* out = Allocator_calloc(alloc, sizeof(struct ThreeNodes), 1);
  138. Identity_set(out);
  139. out->tunIfC.allocator = alloc;
  140. out->tunIfB.allocator = alloc;
  141. out->tunIfA.allocator = alloc;
  142. out->tunIfC.sendMessage = incomingTunC;
  143. out->tunIfB.sendMessage = incomingTunB;
  144. out->tunIfA.sendMessage = incomingTunA;
  145. out->tunIfC.senderContext = &out->messageFrom;
  146. out->tunIfB.senderContext = &out->messageFrom;
  147. out->tunIfA.senderContext = &out->messageFrom;
  148. out->nodeC = c;
  149. out->nodeB = b;
  150. out->nodeA = a;
  151. out->logger = logger;
  152. out->checkLinkageTimeout = Timeout_setInterval(checkLinkage, out, 1, base, alloc);
  153. out->base = base;
  154. out->startTime = Time_currentTimeMilliseconds(base);
  155. out->runTest = runTest;
  156. Ducttape_setUserInterface(c->ducttape, &out->tunIfC);
  157. Ducttape_setUserInterface(b->ducttape, &out->tunIfB);
  158. Ducttape_setUserInterface(a->ducttape, &out->tunIfA);
  159. Log_debug(a->logger, "Waiting for nodes to link asynchronously...");
  160. }
  161. static void sendMessage(struct ThreeNodes* tn,
  162. char* message,
  163. struct TestFramework* from,
  164. struct TestFramework* to)
  165. {
  166. struct Message* msg;
  167. Message_STACK(msg, 64, 512);
  168. Bits_memcpy(msg->bytes, message, CString_strlen(message) + 1);
  169. msg->length = CString_strlen(message) + 1;
  170. TestFramework_craftIPHeader(msg, from->ip, to->ip);
  171. msg = Message_clone(msg, from->alloc);
  172. struct Interface* fromIf;
  173. if (from == tn->nodeA) {
  174. fromIf = &tn->tunIfA;
  175. } else if (from == tn->nodeB) {
  176. fromIf = &tn->tunIfB;
  177. } else if (from == tn->nodeC) {
  178. fromIf = &tn->tunIfC;
  179. } else {
  180. Assert_true(false);
  181. }
  182. TUNMessageType_push(msg, Ethernet_TYPE_IP6, NULL);
  183. fromIf->receiveMessage(msg, fromIf);
  184. if (to == tn->nodeA) {
  185. Assert_true(tn->messageFrom == TUNA);
  186. } else if (to == tn->nodeB) {
  187. Assert_true(tn->messageFrom == TUNB);
  188. } else if (to == tn->nodeC) {
  189. Assert_true(tn->messageFrom == TUNC);
  190. } else {
  191. Assert_true(false);
  192. }
  193. TestFramework_assertLastMessageUnaltered(tn->nodeA);
  194. TestFramework_assertLastMessageUnaltered(tn->nodeB);
  195. TestFramework_assertLastMessageUnaltered(tn->nodeC);
  196. tn->messageFrom = 0;
  197. }
  198. static void runTest(struct ThreeNodes* tn)
  199. {
  200. sendMessage(tn, "Hello World!", tn->nodeA, tn->nodeC);
  201. sendMessage(tn, "Hello cjdns!", tn->nodeC, tn->nodeA);
  202. sendMessage(tn, "send", tn->nodeA, tn->nodeC);
  203. sendMessage(tn, "a", tn->nodeC, tn->nodeA);
  204. sendMessage(tn, "few", tn->nodeA, tn->nodeC);
  205. sendMessage(tn, "packets", tn->nodeC, tn->nodeA);
  206. sendMessage(tn, "to", tn->nodeA, tn->nodeC);
  207. sendMessage(tn, "make", tn->nodeC, tn->nodeA);
  208. sendMessage(tn, "sure", tn->nodeA, tn->nodeC);
  209. sendMessage(tn, "the", tn->nodeC, tn->nodeA);
  210. sendMessage(tn, "cryptoauth", tn->nodeA, tn->nodeC);
  211. sendMessage(tn, "can", tn->nodeC, tn->nodeA);
  212. sendMessage(tn, "establish", tn->nodeA, tn->nodeC);
  213. Log_debug(tn->logger, "\n\nTest passed, shutting down\n\n");
  214. EventBase_endLoop(tn->base);
  215. }
  216. /** Check if nodes A and C can communicate via B without A knowing that C exists. */
  217. int main()
  218. {
  219. struct Allocator* alloc = MallocAllocator_new(1<<22);
  220. struct Writer* logwriter = FileWriter_new(stdout, alloc);
  221. struct Log* logger = WriterLog_new(logwriter, alloc);
  222. struct Random* rand = Random_new(alloc, logger, NULL);
  223. struct EventBase* base = EventBase_new(alloc);
  224. start(alloc, logger, base, rand, runTest);
  225. EventBase_beginLoop(base);
  226. Allocator_free(alloc);
  227. return 0;
  228. }