1
0

Beacon_test.c 8.2 KB

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