1
0

Beacon_test.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "crypto/random/Random.h"
  20. #include "interface/Iface.h"
  21. #include "util/Base32.h"
  22. #include "util/Checksum.h"
  23. #include "util/log/WriterLog.h"
  24. #include "test/TestFramework.h"
  25. #include "wire/Headers.h"
  26. #include "wire/Ethernet.h"
  27. #include "interface/tuntap/TUNMessageType.h"
  28. #include "util/Hex.h"
  29. #include "util/events/Time.h"
  30. #include "util/events/Timeout.h"
  31. #include "dht/dhtcore/NodeStore.h"
  32. #include "dht/Pathfinder_pvt.h"
  33. #include <stdio.h>
  34. struct TwoNodes;
  35. typedef void (RunTest)(struct TwoNodes* ctx);
  36. struct TwoNodes
  37. {
  38. struct TestFramework* nodeB;
  39. struct Iface tunB;
  40. struct TestFramework* nodeA;
  41. struct Iface tunA;
  42. int messageFrom;
  43. bool beaconsSent;
  44. struct Timeout* checkLinkageTimeout;
  45. struct Log* logger;
  46. struct EventBase* base;
  47. uint64_t startTime;
  48. RunTest* runTest;
  49. Identity
  50. };
  51. #define TUNB 2
  52. #define TUNA 1
  53. static Iface_DEFUN incomingTunB(struct Message* msg, struct Iface* tunB)
  54. {
  55. struct TwoNodes* tn = Identity_containerOf(tunB, struct TwoNodes, tunB);
  56. Assert_true(TUNMessageType_pop(msg, NULL) == Ethernet_TYPE_IP6);
  57. Message_shift(msg, -Headers_IP6Header_SIZE, NULL);
  58. printf("Message from TUN in node B [%s]\n", msg->bytes);
  59. tn->messageFrom = TUNB;
  60. return 0;
  61. }
  62. static Iface_DEFUN incomingTunA(struct Message* msg, struct Iface* tunA)
  63. {
  64. struct TwoNodes* tn = Identity_containerOf(tunA, struct TwoNodes, tunA);
  65. Assert_true(TUNMessageType_pop(msg, NULL) == Ethernet_TYPE_IP6);
  66. Message_shift(msg, -Headers_IP6Header_SIZE, NULL);
  67. uint8_t buff[1024];
  68. Hex_encode(buff, 1024, msg->bytes, msg->length);
  69. printf("Message from TUN in node A [%s] [%d] [%s]\n", msg->bytes, msg->length, buff);
  70. tn->messageFrom = TUNA;
  71. return 0;
  72. }
  73. static void notLinkedYet(struct TwoNodes* ctx)
  74. {
  75. uint64_t now = Time_currentTimeMilliseconds(ctx->base);
  76. if ((now - ctx->startTime) > 5000) {
  77. Assert_failure("Failed to link in 5 seconds");
  78. }
  79. }
  80. static void checkLinkage(void* vTwoNodes)
  81. {
  82. struct TwoNodes* ctx = Identity_check((struct TwoNodes*) vTwoNodes);
  83. if (!ctx->beaconsSent) {
  84. if (Pathfinder_getNodeStore(ctx->nodeA->pathfinder) &&
  85. Pathfinder_getNodeStore(ctx->nodeB->pathfinder))
  86. {
  87. Log_debug(ctx->logger, "Linking A and B");
  88. TestFramework_linkNodes(ctx->nodeB, ctx->nodeA, true);
  89. ctx->beaconsSent = true;
  90. }
  91. return;
  92. }
  93. if (Pathfinder_getNodeStore(ctx->nodeA->pathfinder)->nodeCount < 1) {
  94. notLinkedYet(ctx);
  95. return;
  96. }
  97. Log_debug(ctx->logger, "A seems to be linked with B");
  98. if (Pathfinder_getNodeStore(ctx->nodeB->pathfinder)->nodeCount < 1) {
  99. notLinkedYet(ctx);
  100. return;
  101. }
  102. Log_debug(ctx->logger, "B seems to be linked with A");
  103. Log_debug(ctx->logger, "\n\nSetup Complete\n\n");
  104. Timeout_clearTimeout(ctx->checkLinkageTimeout);
  105. ctx->runTest(ctx);
  106. }
  107. static void start(struct Allocator* alloc,
  108. struct Log* logger,
  109. struct EventBase* base,
  110. struct Random* rand,
  111. RunTest* runTest)
  112. {
  113. struct TestFramework* a =
  114. TestFramework_setUp("\xad\x7e\xa3\x26\xaa\x01\x94\x0a\x25\xbc\x9e\x01\x26\x22\xdb\x69"
  115. "\x4f\xd9\xb4\x17\x7c\xf3\xf8\x91\x16\xf3\xcf\xe8\x5c\x80\xe1\x4a",
  116. alloc, base, rand, logger);
  117. //"publicKey": "kmzm4w0kj9bswd5qmx74nu7kusv5pj40vcsmp781j6xxgpd59z00.k",
  118. //"ipv6": "fc41:94b5:0925:7ba9:3959:11ab:a006:367a",
  119. struct TestFramework* b =
  120. TestFramework_setUp("\xd8\x54\x3e\x70\xb9\xae\x7c\x41\xbc\x18\xa4\x9a\x9c\xee\xca\x9c"
  121. "\xdc\x45\x01\x96\x6b\xbd\x7e\x76\xcf\x3a\x9f\xbc\x12\xed\x8b\xb4",
  122. alloc, base, rand, logger);
  123. //"publicKey": "vz21tg07061s8v9mckrvgtfds7j2u5lst8cwl6nqhp81njrh5wg0.k",
  124. //"ipv6": "fc1f:5b96:e1c5:625d:afde:2523:a7fa:383a",
  125. struct TwoNodes* out = Allocator_calloc(alloc, sizeof(struct TwoNodes), 1);
  126. Identity_set(out);
  127. out->tunB.send = incomingTunB;
  128. out->tunA.send = incomingTunA;
  129. Iface_plumb(&out->tunB, b->tunIf);
  130. Iface_plumb(&out->tunA, a->tunIf);
  131. out->nodeB = b;
  132. out->nodeA = a;
  133. out->logger = logger;
  134. out->checkLinkageTimeout = Timeout_setInterval(checkLinkage, out, 1, base, alloc);
  135. out->base = base;
  136. out->startTime = Time_currentTimeMilliseconds(base);
  137. out->runTest = runTest;
  138. Log_debug(a->logger, "Waiting for nodes to link asynchronously...");
  139. }
  140. static void sendMessage(struct TwoNodes* tn,
  141. char* message,
  142. struct TestFramework* from,
  143. struct TestFramework* to)
  144. {
  145. struct Message* msg;
  146. Message_STACK(msg, 64, 512);
  147. Bits_memcpy(msg->bytes, message, CString_strlen(message) + 1);
  148. msg->length = CString_strlen(message) + 1;
  149. TestFramework_craftIPHeader(msg, from->ip, to->ip);
  150. msg = Message_clone(msg, from->alloc);
  151. struct Iface* fromIf;
  152. if (from == tn->nodeA) {
  153. fromIf = &tn->tunA;
  154. } else if (from == tn->nodeB) {
  155. fromIf = &tn->tunB;
  156. } else {
  157. Assert_true(false);
  158. }
  159. TUNMessageType_push(msg, Ethernet_TYPE_IP6, NULL);
  160. Iface_send(fromIf, msg);
  161. if (to == tn->nodeA) {
  162. Assert_true(tn->messageFrom == TUNA);
  163. } else if (to == tn->nodeB) {
  164. Assert_true(tn->messageFrom == TUNB);
  165. } else {
  166. Assert_true(false);
  167. }
  168. TestFramework_assertLastMessageUnaltered(tn->nodeA);
  169. TestFramework_assertLastMessageUnaltered(tn->nodeB);
  170. tn->messageFrom = 0;
  171. }
  172. static void runTest(struct TwoNodes* tn)
  173. {
  174. sendMessage(tn, "Hello World!", tn->nodeA, tn->nodeB);
  175. sendMessage(tn, "Hello cjdns!", tn->nodeB, tn->nodeA);
  176. sendMessage(tn, "send", tn->nodeA, tn->nodeB);
  177. sendMessage(tn, "a", tn->nodeB, tn->nodeA);
  178. sendMessage(tn, "few", tn->nodeA, tn->nodeB);
  179. sendMessage(tn, "packets", tn->nodeB, tn->nodeA);
  180. sendMessage(tn, "to", tn->nodeA, tn->nodeB);
  181. sendMessage(tn, "make", tn->nodeB, tn->nodeA);
  182. sendMessage(tn, "sure", tn->nodeA, tn->nodeB);
  183. sendMessage(tn, "the", tn->nodeB, tn->nodeA);
  184. sendMessage(tn, "cryptoauth", tn->nodeA, tn->nodeB);
  185. sendMessage(tn, "can", tn->nodeB, tn->nodeA);
  186. sendMessage(tn, "establish", tn->nodeA, tn->nodeB);
  187. Log_debug(tn->logger, "\n\nTest passed, shutting down\n\n");
  188. EventBase_endLoop(tn->base);
  189. }
  190. /** Check if nodes A and C can communicate via B without A knowing that C exists. */
  191. int main()
  192. {
  193. struct Allocator* alloc = MallocAllocator_new(1<<22);
  194. struct Writer* logwriter = FileWriter_new(stdout, alloc);
  195. struct Log* logger = WriterLog_new(logwriter, alloc);
  196. struct Random* rand = Random_new(alloc, logger, NULL);
  197. struct EventBase* base = EventBase_new(alloc);
  198. start(alloc, logger, base, rand, runTest);
  199. EventBase_beginLoop(base);
  200. Allocator_free(alloc);
  201. return 0;
  202. }