Main_fuzz_test.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "crypto/Key.h"
  16. #include "io/FileWriter.h"
  17. #include "memory/Allocator.h"
  18. #include "crypto/random/Random.h"
  19. #include "interface/Iface.h"
  20. #include "util/Checksum.h"
  21. #include "util/log/WriterLog.h"
  22. #include "test/TestFramework.h"
  23. #include "wire/Headers.h"
  24. #include "wire/Ethernet.h"
  25. #include "interface/tuntap/TUNMessageType.h"
  26. #include "util/events/Time.h"
  27. #include "util/events/Timeout.h"
  28. #include "util/version/Version.h"
  29. #include "test/FuzzTest.h"
  30. #include <stdio.h>
  31. struct Context
  32. {
  33. struct TestFramework* nodeB;
  34. struct Iface tunB;
  35. struct TestFramework* nodeA;
  36. struct Iface tunA;
  37. int messageFrom;
  38. bool beaconsSent;
  39. struct Timeout* checkLinkageTimeout;
  40. struct Log* logger;
  41. struct EventBase* base;
  42. uint64_t startTime;
  43. Identity
  44. };
  45. #define TUNB 2
  46. #define TUNA 1
  47. static Iface_DEFUN incomingTun(struct Message* msg, struct Iface* tunB)
  48. {
  49. return 0;
  50. }
  51. static void notLinkedYet(struct Context* ctx)
  52. {
  53. uint64_t now = Time_currentTimeMilliseconds(ctx->base);
  54. if ((now - ctx->startTime) > 5000) {
  55. Assert_failure("Failed to link in 5 seconds");
  56. }
  57. }
  58. static void checkLinkage(void* vContext)
  59. {
  60. struct Context* ctx = Identity_check((struct Context*) vContext);
  61. if (!ctx->beaconsSent) {
  62. Log_debug(ctx->logger, "Linking A and B");
  63. TestFramework_linkNodes(ctx->nodeB, ctx->nodeA, true);
  64. ctx->beaconsSent = true;
  65. return;
  66. }
  67. if (TestFramework_peerCount(ctx->nodeA) < 1) {
  68. notLinkedYet(ctx);
  69. return;
  70. }
  71. Log_debug(ctx->logger, "A seems to be linked with B");
  72. if (TestFramework_peerCount(ctx->nodeB) < 1) {
  73. notLinkedYet(ctx);
  74. return;
  75. }
  76. Log_debug(ctx->logger, "B seems to be linked with A");
  77. Log_debug(ctx->logger, "\n\nSetup Complete\n\n");
  78. //Timeout_clearTimeout(ctx->checkLinkageTimeout);
  79. //EventBase_endLoop(ctx->base);
  80. Timeout_clearAll(ctx->base);
  81. }
  82. void* CJDNS_FUZZ_INIT(struct Allocator* alloc, struct Random* rand)
  83. {
  84. struct Writer* logwriter = FileWriter_new(stdout, alloc);
  85. struct Log* logger = WriterLog_new(logwriter, alloc);
  86. struct EventBase* base = EventBase_new(alloc);
  87. struct Context* ctx = Allocator_calloc(alloc, sizeof(struct Context), 1);
  88. Identity_set(ctx);
  89. ctx->base = base;
  90. uint8_t address[16];
  91. uint8_t publicKey[32];
  92. uint8_t privateKeyA[32];
  93. Key_gen(address, publicKey, privateKeyA, rand);
  94. struct TestFramework* a =
  95. TestFramework_setUp((char*) privateKeyA, alloc, base, rand, logger);
  96. uint8_t privateKeyB[32];
  97. Key_gen(address, publicKey, privateKeyB, rand);
  98. struct TestFramework* b =
  99. TestFramework_setUp((char*) privateKeyB, alloc, base, rand, logger);
  100. ctx->tunB.send = incomingTun;
  101. ctx->tunA.send = incomingTun;
  102. Iface_plumb(&ctx->tunB, b->tunIf);
  103. Iface_plumb(&ctx->tunA, a->tunIf);
  104. ctx->nodeB = b;
  105. ctx->nodeA = a;
  106. ctx->logger = logger;
  107. ctx->checkLinkageTimeout = Timeout_setInterval(checkLinkage, ctx, 1, base, alloc);
  108. ctx->base = base;
  109. ctx->startTime = Time_currentTimeMilliseconds(base);
  110. Log_debug(a->logger, "Waiting for nodes to link asynchronously...");
  111. EventBase_beginLoop(base);
  112. return ctx;
  113. }
  114. void CJDNS_FUZZ_MAIN(void* vctx, struct Message* msg)
  115. {
  116. printf("\n\nEnter main\n\n");
  117. struct Context* ctx = Identity_check((struct Context*) vctx);
  118. struct TestFramework* from = ctx->nodeA;
  119. struct TestFramework* to = ctx->nodeB;
  120. // forget it, it's gonna get killed in the Upper
  121. if (msg->length < RouteHeader_SIZE) { return; }
  122. // Lets fill in the ipv6, pubkey & label so that any
  123. // old packet dump will work fine for testing
  124. {
  125. struct RouteHeader* rh = (struct RouteHeader*) msg->bytes;
  126. Bits_memcpy(rh->ip6, to->ip, 16);
  127. Bits_memcpy(rh->publicKey, to->publicKey, 32);
  128. rh->version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
  129. uint64_t label = EncodingScheme_serializeDirector(from->scheme, 0, -1);
  130. int f = EncodingScheme_getFormNum(from->scheme, label);
  131. label |= 1 << (from->scheme->forms[f].prefixLen + from->scheme->forms[f].bitCount);
  132. rh->sh.label_be = Endian_hostToBigEndian64(label);
  133. SwitchHeader_setLabelShift(&rh->sh, 0);
  134. }
  135. // We're not limited to sending data types which we have registered for
  136. Assert_true(!UpperDistributor_registerHandler(ctx->nodeA->nc->upper, 0, 0xfcfc));
  137. struct Headers_UDPHeader udp = {
  138. .srcPort_be = 0xfcfc,
  139. .destPort_be = Endian_hostToBigEndian16(1), // UpperDistributor MAGIC_PORT
  140. .length_be = Endian_hostToBigEndian16(msg->length + Headers_UDPHeader_SIZE),
  141. .checksum_be = 0,
  142. };
  143. Message_push(msg, &udp, Headers_UDPHeader_SIZE, NULL);
  144. uint8_t srcAndDest[32] = { [31] = 1 };
  145. // fc00::1
  146. AddressCalc_makeValidAddress(&srcAndDest[16]);
  147. Bits_memcpy(&srcAndDest, from->ip, 16);
  148. uint16_t checksum = Checksum_udpIp6(srcAndDest, msg->bytes, msg->length);
  149. ((struct Headers_UDPHeader*)msg->bytes)->checksum_be = checksum;
  150. TestFramework_craftIPHeader(msg, srcAndDest, &srcAndDest[16]);
  151. ((struct Headers_IP6Header*) msg->bytes)->nextHeader = 17;
  152. TUNMessageType_push(msg, Ethernet_TYPE_IP6, NULL);
  153. Iface_send(&ctx->tunA, Message_clone(msg, from->alloc));
  154. TestFramework_assertLastMessageUnaltered(ctx->nodeA);
  155. EventBase_beginLoop(ctx->base);
  156. }