Main_fuzz_test.c 6.2 KB

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