FramingIface_fuzz_test.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 "interface/FramingIface.h"
  16. #include "crypto/random/Random.h"
  17. #include "memory/Allocator.h"
  18. #include "test/FuzzTest.h"
  19. #include "util/Identity.h"
  20. #define BUF_SZ 1024
  21. struct Context {
  22. struct Iface iface;
  23. struct Iface* fi;
  24. struct Iface outer;
  25. int success;
  26. struct Allocator* alloc;
  27. int messageLen;
  28. struct Message* buf;
  29. uint8_t* bufPtr;
  30. Identity
  31. } ctx;
  32. static Iface_DEFUN ifaceRecvMsg(struct Message* message, struct Iface* thisInterface)
  33. {
  34. struct Context* ctx = Identity_containerOf(thisInterface, struct Context, iface);
  35. Assert_true(!ctx->success);
  36. Assert_true(Message_getLength(message) == ctx->messageLen);
  37. Assert_true(Message_getLength(ctx->buf) == 0);
  38. Assert_true(!Bits_memcmp(ctx->bufPtr, message->msgbytes, ctx->messageLen));
  39. ctx->success = 1;
  40. return NULL;
  41. }
  42. void CJDNS_FUZZ_MAIN(void* vctx, struct Message* fuzz)
  43. {
  44. struct Context* ctx = Identity_check((struct Context*) vctx);
  45. if (Message_getLength(fuzz) <= 2) { return; }
  46. ctx->messageLen = Er_assert(Message_epop16be(fuzz)) % BUF_SZ;
  47. Er_assert(Message_truncate(ctx->buf, ctx->messageLen));
  48. Er_assert(Message_epush32be(ctx->buf, ctx->messageLen));
  49. for (int i = 0; ; i++) {
  50. uint8_t len = fuzz->msgbytes[i % Message_getLength(fuzz)] + 1;
  51. if (len > Message_getLength(ctx->buf)) {
  52. len = Message_getLength(ctx->buf);
  53. }
  54. struct Allocator* a = Allocator_child(ctx->alloc);
  55. struct Message* m = Message_new(len, 0, a);
  56. Er_assert(Message_epop(ctx->buf, m->msgbytes, len));
  57. Iface_send(&ctx->outer, m);
  58. Allocator_free(a);
  59. if (ctx->success) {
  60. return;
  61. }
  62. }
  63. }
  64. void* CJDNS_FUZZ_INIT(struct Allocator* alloc, struct Random* rand)
  65. {
  66. struct Context* ctx = Allocator_calloc(alloc, sizeof(struct Context), 1);
  67. ctx->iface.send = ifaceRecvMsg;
  68. ctx->fi = FramingIface_new(BUF_SZ, &ctx->outer, alloc);
  69. Iface_plumb(&ctx->iface, ctx->fi);
  70. ctx->alloc = alloc;
  71. ctx->buf = Message_new(BUF_SZ, 4, alloc);
  72. Random_bytes(rand, ctx->buf->msgbytes, BUF_SZ);
  73. ctx->bufPtr = ctx->buf->msgbytes;
  74. Identity_set(ctx);
  75. return ctx;
  76. }