RustIface_test.c 3.3 KB

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