TUNInterface_ipv4_root_test.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/addressable/UDPAddrInterface.h"
  24. #include "interface/tuntap/TUNInterface.h"
  25. #include "interface/tuntap/TUNMessageType.h"
  26. #include "memory/Allocator.h"
  27. #include "memory/MallocAllocator.h"
  28. #include "interface/InterfaceController.h"
  29. #include "io/FileWriter.h"
  30. #include "io/Writer.h"
  31. #include "util/Assert.h"
  32. #include "util/log/Log.h"
  33. #include "util/log/WriterLog.h"
  34. #include "util/platform/libc/string.h"
  35. #include "util/events/Timeout.h"
  36. #include "wire/Ethernet.h"
  37. #include "wire/Headers.h"
  38. #include "util/platform/netdev/NetDev.h"
  39. #include "test/RootTest.h"
  40. #include <stdlib.h>
  41. // On loan from the DoD, thanks guys.
  42. static const uint8_t testAddrA[4] = {11, 0, 0, 1};
  43. static const uint8_t testAddrB[4] = {11, 0, 0, 2};
  44. /*
  45. * Setup a UDPAddrInterface and a TUNInterface, test sending traffic between them.
  46. */
  47. static int receivedMessageTUNCount = 0;
  48. static uint8_t receiveMessageTUN(struct Message* msg, struct Interface* iface)
  49. {
  50. receivedMessageTUNCount++;
  51. uint16_t ethertype = TUNMessageType_pop(msg, NULL);
  52. if (ethertype != Ethernet_TYPE_IP4) {
  53. printf("Spurious packet with ethertype [%u]\n", Endian_bigEndianToHost16(ethertype));
  54. return 0;
  55. }
  56. struct Headers_IP4Header* header = (struct Headers_IP4Header*) msg->bytes;
  57. Assert_always(msg->length == Headers_IP4Header_SIZE + Headers_UDPHeader_SIZE + 12);
  58. Assert_always(!Bits_memcmp(header->destAddr, testAddrB, 4));
  59. Assert_always(!Bits_memcmp(header->sourceAddr, testAddrA, 4));
  60. Bits_memcpyConst(header->destAddr, testAddrA, 4);
  61. Bits_memcpyConst(header->sourceAddr, testAddrB, 4);
  62. TUNMessageType_push(msg, ethertype, NULL);
  63. return iface->sendMessage(msg, iface);
  64. }
  65. static uint8_t receiveMessageUDP(struct Message* msg, struct Interface* iface)
  66. {
  67. if (!receivedMessageTUNCount) {
  68. return 0;
  69. }
  70. // Got the message, test successful, tear everything down by freeing the root alloc.
  71. struct Allocator* alloc = iface->receiverContext;
  72. Allocator_free(alloc);
  73. return 0;
  74. }
  75. static void fail(void* ignored)
  76. {
  77. Assert_always(!"timeout");
  78. }
  79. int main(int argc, char** argv)
  80. {
  81. // TODO: fix TUNConfigurator_addIp4Address() for Illumos, Darwin, BSD.
  82. #if defined(sunos) || defined(darwin) || defined(freebsd)
  83. return 0;
  84. #endif
  85. struct Allocator* alloc = MallocAllocator_new(1<<20);
  86. struct EventBase* base = EventBase_new(alloc);
  87. struct Writer* logWriter = FileWriter_new(stdout, alloc);
  88. struct Log* logger = WriterLog_new(logWriter, alloc);
  89. struct Sockaddr* addrA = Sockaddr_fromBytes(testAddrA, Sockaddr_AF_INET, alloc);
  90. char assignedIfName[TUNInterface_IFNAMSIZ];
  91. struct Interface* tun = TUNInterface_new(NULL, assignedIfName, base, logger, NULL, alloc);
  92. NetDev_addAddress(assignedIfName, addrA, 30, logger, NULL);
  93. struct Sockaddr_storage ss;
  94. Assert_always(!Sockaddr_parse("0.0.0.0", &ss));
  95. struct AddrInterface* udp = UDPAddrInterface_new(base, &ss.addr, alloc, NULL, logger);
  96. struct Sockaddr* dest = Sockaddr_clone(udp->addr, alloc);
  97. uint8_t* addr;
  98. Assert_always(4 == Sockaddr_getAddress(dest, &addr));
  99. Bits_memcpy(addr, testAddrB, 4);
  100. struct Message* msg;
  101. Message_STACK(msg, 0, 64);
  102. Message_push(msg, "Hello World", 12, NULL);
  103. Message_push(msg, dest, dest->addrLen, NULL);
  104. udp->generic.receiveMessage = receiveMessageUDP;
  105. udp->generic.receiverContext = alloc;
  106. tun->receiveMessage = receiveMessageTUN;
  107. udp->generic.sendMessage(msg, &udp->generic);
  108. Timeout_setTimeout(fail, NULL, 1000, base, alloc);
  109. EventBase_beginLoop(base);
  110. return 0;
  111. }