1
0

UDPInterface_communication_test.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <http://www.gnu.org/licenses/>.
  14. */
  15. #define string_strcmp
  16. #define string_strlen
  17. #include "admin/testframework/AdminTestFramework.h"
  18. #include "admin/Admin.h"
  19. #include "admin/AdminClient.h"
  20. #include "benc/Dict.h"
  21. #include "benc/String.h"
  22. #include "benc/Int.h"
  23. #include "interface/UDPInterface_pvt.h"
  24. #include "memory/Allocator.h"
  25. #include "memory/MallocAllocator.h"
  26. #include "interface/InterfaceController.h"
  27. #include "io/FileWriter.h"
  28. #include "io/Writer.h"
  29. #include "util/Assert.h"
  30. #include "util/log/Log.h"
  31. #include "util/log/WriterLog.h"
  32. #include "util/platform/libc/string.h"
  33. #include "util/events/Timeout.h"
  34. #include <stdlib.h>
  35. /*
  36. * Setup 2 UDPInterface's, test sending traffic between them.
  37. */
  38. static int registerPeer(struct InterfaceController* ic,
  39. uint8_t herPublicKey[32],
  40. String* password,
  41. bool requireAuth,
  42. bool transient,
  43. struct Interface* iface)
  44. {
  45. return 0;
  46. }
  47. int receiveMessageACount = 0;
  48. static uint8_t receiveMessageA(struct Message* msg, struct Interface* iface)
  49. {
  50. // pong
  51. receiveMessageACount++;
  52. return iface->sendMessage(msg, iface);
  53. }
  54. static uint8_t receiveMessageB(struct Message* msg, struct Interface* iface)
  55. {
  56. if (receiveMessageACount) {
  57. // Got the message, test successful.
  58. struct Allocator* alloc = iface->receiverContext;
  59. Allocator_free(alloc);
  60. }
  61. return 0;
  62. }
  63. static void fail(void* ignored)
  64. {
  65. Assert_true(!"timeout");
  66. }
  67. int main(int argc, char** argv)
  68. {
  69. struct Allocator* alloc = MallocAllocator_new(1<<20);
  70. struct EventBase* base = EventBase_new(alloc);
  71. struct Writer* logWriter = FileWriter_new(stdout, alloc);
  72. struct Log* logger = WriterLog_new(logWriter, alloc);
  73. // mock interface controller.
  74. struct InterfaceController ic = {
  75. .registerPeer = registerPeer
  76. };
  77. struct Sockaddr_storage addr;
  78. Assert_true(!Sockaddr_parse("127.0.0.1", &addr));
  79. struct UDPInterface* udpA = UDPInterface_new(base, &addr.addr, alloc, NULL, logger, &ic);
  80. struct UDPInterface* udpB = UDPInterface_new(base, &addr.addr, alloc, NULL, logger, &ic);
  81. struct Message* msg;
  82. Message_STACK(msg, 0, 128);
  83. Message_push(msg, "Hello World", 12);
  84. Message_push(msg, udpA->addr, udpA->addr->addrLen);
  85. struct Interface* ifA = &((struct UDPInterface_pvt*) udpA)->udpBase->generic;
  86. struct Interface* ifB = &((struct UDPInterface_pvt*) udpB)->udpBase->generic;
  87. ifA->receiveMessage = receiveMessageA;
  88. ifB->receiveMessage = receiveMessageB;
  89. ifB->receiverContext = alloc;
  90. struct Allocator* child = Allocator_child(alloc);
  91. msg = Message_clone(msg, child);
  92. ifB->sendMessage(msg, ifB);
  93. Allocator_free(child);
  94. Timeout_setTimeout(fail, NULL, 1000, base, alloc);
  95. EventBase_beginLoop(base);
  96. }