RustIface_test.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 "memory/Allocator.h"
  16. #include "rust/cjdns_sys/Rffi.h"
  17. #include "interface/Iface.h"
  18. #include "memory/MallocAllocator.h"
  19. #include "util/Identity.h"
  20. #include "wire/Message.h"
  21. #include "util/Assert.h"
  22. #include "interface/test/RustIface_test.h"
  23. #include <stdio.h>
  24. struct Context
  25. {
  26. struct Iface outside;
  27. struct Iface inside;
  28. int received;
  29. Identity
  30. };
  31. static int incomingCount = 0;
  32. static int outgoingCount = 0;
  33. static int dropped = 0;
  34. void RustIface_gotIncoming()
  35. {
  36. incomingCount++;
  37. }
  38. void RustIface_gotOutgoing()
  39. {
  40. outgoingCount++;
  41. }
  42. void RustIface_dropped()
  43. {
  44. dropped++;
  45. }
  46. static Iface_DEFUN sendOutside(struct Message* msg, struct Iface* outside)
  47. {
  48. printf("sendOutside called\n");
  49. Assert_true(incomingCount == 1 && outgoingCount == 1 && dropped == 0);
  50. struct Context* ctx = Identity_containerOf(outside, struct Context, outside);
  51. uint32_t top = Er_assert(Message_epop32be(msg));
  52. Assert_true(top == 0x00000800);
  53. Assert_true(!(ctx->received & 1));
  54. ctx->received |= 1;
  55. return NULL;
  56. }
  57. static Iface_DEFUN sendInside(struct Message* msg, struct Iface* inside)
  58. {
  59. printf("sendInside called\n");
  60. Assert_true(incomingCount == 1 && outgoingCount == 0 && dropped == 0);
  61. struct Context* ctx = Identity_containerOf(inside, struct Context, inside);
  62. Assert_true(!(ctx->received & 1<<1));
  63. ctx->received |= 1<<1;
  64. return Iface_next(inside, msg);
  65. }
  66. // Message goes:
  67. // 1. Rust incoming
  68. // 2. sendInside -> bounces the message back with Iface_next()
  69. // 3. Rust outgoing
  70. // 4. sendOutside -> eats the message with Error(NONE)
  71. int main()
  72. {
  73. struct Allocator* alloc = MallocAllocator_new(20000);
  74. struct Context* ctx = Allocator_calloc(alloc, sizeof(struct Context), 1);
  75. Identity_set(ctx);
  76. ctx->outside.send = sendOutside;
  77. ctx->inside.send = sendInside;
  78. RTypes_IfWrapper_t wrapper = Rffi_testwrapper_create(alloc);
  79. Iface_plumb(&ctx->inside, wrapper.internal);
  80. Iface_plumb(&ctx->outside, wrapper.external);
  81. Assert_true(incomingCount == 0 && outgoingCount == 0 && dropped == 0);
  82. struct Message* msg = Message_new(256, 256, alloc);
  83. Er_assert(Message_epush32be(msg, 0x00000800));
  84. Iface_send(&ctx->outside, msg);
  85. Assert_true(ctx->received == 3);
  86. Assert_true(incomingCount == 1 && outgoingCount == 1 && dropped == 0);
  87. Allocator_free(alloc);
  88. // Expect RustIface_dropped() to be called 3 times because there are
  89. // 3 different structures which are dropped
  90. Assert_true(incomingCount == 1 && outgoingCount == 1 && dropped == 3);
  91. return 0;
  92. }