BeaconFramework.c 8.4 KB

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