Main_fuzz_test.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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_sessionCount(ctx->nodeA) < 1) {
  68. notLinkedYet(ctx);
  69. return;
  70. }
  71. Log_debug(ctx->logger, "A seems to be linked with B");
  72. if (TestFramework_sessionCount(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, struct EventBase* base)
  83. {
  84. struct Writer* logwriter = FileWriter_new(stdout, alloc);
  85. struct Log* logger = WriterLog_new(logwriter, alloc);
  86. struct Context* ctx = Allocator_calloc(alloc, sizeof(struct Context), 1);
  87. Identity_set(ctx);
  88. ctx->base = base;
  89. uint8_t address[16];
  90. uint8_t publicKey[32];
  91. uint8_t privateKeyA[32];
  92. Key_gen(address, publicKey, privateKeyA, rand);
  93. struct TestFramework* a =
  94. TestFramework_setUp((char*) privateKeyA, alloc, base, rand, logger);
  95. uint8_t privateKeyB[32];
  96. Key_gen(address, publicKey, privateKeyB, rand);
  97. struct TestFramework* b =
  98. TestFramework_setUp((char*) privateKeyB, alloc, base, rand, logger);
  99. ctx->tunB.send = incomingTun;
  100. ctx->tunA.send = incomingTun;
  101. Iface_plumb(&ctx->tunB, b->tunIf);
  102. Iface_plumb(&ctx->tunA, a->tunIf);
  103. ctx->nodeB = b;
  104. ctx->nodeA = a;
  105. ctx->logger = logger;
  106. ctx->checkLinkageTimeout = Timeout_setInterval(checkLinkage, ctx, 1, base, alloc);
  107. ctx->base = base;
  108. ctx->startTime = Time_currentTimeMilliseconds(base);
  109. Log_debug(a->logger, "Waiting for nodes to link asynchronously...");
  110. EventBase_beginLoop(base);
  111. return ctx;
  112. }
  113. void CJDNS_FUZZ_MAIN(void* vctx, struct Message* msg)
  114. {
  115. if (msg->length > 2048) { return; }
  116. struct Context* ctx = Identity_check((struct Context*) vctx);
  117. struct TestFramework* from = ctx->nodeA;
  118. struct TestFramework* to = ctx->nodeB;
  119. // forget it, it's gonna get killed in the Upper
  120. if (msg->length < RouteHeader_SIZE) { return; }
  121. // Lets fill in the ipv6, pubkey & label so that any
  122. // old packet dump will work fine for testing
  123. {
  124. struct RouteHeader* rh = (struct RouteHeader*) msg->bytes;
  125. Bits_memcpy(rh->ip6, to->ip, 16);
  126. Bits_memcpy(rh->publicKey, to->publicKey, 32);
  127. rh->version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
  128. uint64_t label = EncodingScheme_serializeDirector(from->scheme, 0, -1);
  129. int f = EncodingScheme_getFormNum(from->scheme, label);
  130. label |= 1 << (from->scheme->forms[f].prefixLen + from->scheme->forms[f].bitCount);
  131. rh->sh.label_be = Endian_hostToBigEndian64(label);
  132. SwitchHeader_setLabelShift(&rh->sh, 0);
  133. }
  134. // We're not limited to sending data types which we have registered for
  135. Assert_true(!UpperDistributor_registerHandler(ctx->nodeA->nc->upper, 0, 0xfcfc));
  136. struct Headers_UDPHeader udp = {
  137. .srcPort_be = 0xfcfc,
  138. .destPort_be = Endian_hostToBigEndian16(1), // UpperDistributor MAGIC_PORT
  139. .length_be = Endian_hostToBigEndian16(msg->length + Headers_UDPHeader_SIZE),
  140. .checksum_be = 0,
  141. };
  142. Message_push(msg, &udp, Headers_UDPHeader_SIZE, NULL);
  143. uint8_t srcAndDest[32] = { [31] = 1 };
  144. // fc00::1
  145. AddressCalc_makeValidAddress(&srcAndDest[16]);
  146. Bits_memcpy(&srcAndDest, from->ip, 16);
  147. uint16_t checksum = Checksum_udpIp6(srcAndDest, msg->bytes, msg->length);
  148. ((struct Headers_UDPHeader*)msg->bytes)->checksum_be = checksum;
  149. TestFramework_craftIPHeader(msg, srcAndDest, &srcAndDest[16]);
  150. ((struct Headers_IP6Header*) msg->bytes)->nextHeader = 17;
  151. TUNMessageType_push(msg, Ethernet_TYPE_IP6, NULL);
  152. Iface_send(&ctx->tunA, Message_clone(msg, from->alloc));
  153. TestFramework_assertLastMessageUnaltered(ctx->nodeA);
  154. EventBase_beginLoop(ctx->base);
  155. }