Main_fuzz_test.c 6.3 KB

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