cjdroute_routerPing_test.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include "crypto/AddressCalc.h"
  16. #include "memory/MallocAllocator.h"
  17. #include "memory/Allocator.h"
  18. #include "util/Base32.h"
  19. #include "util/Checksum.h"
  20. #include "util/CString.h"
  21. #include "test/TestFramework.h"
  22. #include "net/Ducttape_pvt.h"
  23. #include "wire/SwitchHeader.h"
  24. #include <stdio.h>
  25. #define PADDING 512
  26. static uint8_t catchResponse(struct Message* msg, struct Interface* iface)
  27. {
  28. iface->receiverContext = msg;
  29. return 0;
  30. }
  31. int main()
  32. {
  33. return 0; // TODO(cjd): make this work again
  34. char* pingBenc = "d1:q4:ping4:txid4:abcd1:pi2ee";
  35. struct Allocator* alloc = MallocAllocator_new(1<<22);
  36. struct TestFramework* tf =
  37. TestFramework_setUp("0123456789abcdefghijklmnopqrstuv", alloc, NULL, NULL, NULL);
  38. struct Ducttape_pvt* dt = (struct Ducttape_pvt*) tf->ducttape;
  39. struct Allocator* allocator = MallocAllocator_new(85000);
  40. uint16_t buffLen = sizeof(struct Ducttape_IncomingForMe) + 8 + CString_strlen(pingBenc);
  41. uint8_t* buff = Allocator_calloc(allocator, buffLen+PADDING, 1);
  42. struct SwitchHeader* sh = (struct SwitchHeader*) (buff + PADDING);
  43. sh->label_be = Endian_hostToBigEndian64(4);
  44. struct Headers_IP6Header* ip6 = (struct Headers_IP6Header*) &sh[1];
  45. uint8_t herPublicKey[32];
  46. Base32_decode(herPublicKey, 32,
  47. (uint8_t*) "0z5tscp8td1sc6cv4htp7jbls79ltqxw9pbg190x0kbm1lguqtx0", 52);
  48. AddressCalc_addressForPublicKey(ip6->sourceAddr, herPublicKey);
  49. struct Headers_UDPHeader* udp = (struct Headers_UDPHeader*) &ip6[1];
  50. ip6->hopLimit = 0;
  51. ip6->nextHeader = 17;
  52. udp->srcPort_be = 0;
  53. udp->destPort_be = 0;
  54. udp->length_be = Endian_hostToBigEndian16(CString_strlen(pingBenc));
  55. CString_strncpy((char*)(udp + 1), pingBenc, CString_strlen(pingBenc));
  56. dt->switchInterface.receiveMessage = catchResponse;
  57. dt->switchInterface.receiverContext = NULL;
  58. // bad checksum
  59. udp->checksum_be = 1;
  60. struct Message m = {
  61. .bytes = buff+PADDING,
  62. .length = buffLen,
  63. .padding = PADDING,
  64. .alloc = alloc
  65. };
  66. Ducttape_injectIncomingForMe(&m, &dt->pub, herPublicKey);
  67. Assert_true(!dt->switchInterface.receiverContext);
  68. // zero checksum
  69. udp->checksum_be = 0;
  70. struct Message m2 = {
  71. .bytes = buff+PADDING,
  72. .length = buffLen,
  73. .padding = PADDING,
  74. .alloc = alloc
  75. };
  76. Ducttape_injectIncomingForMe(&m2, &dt->pub, herPublicKey);
  77. Assert_true(!dt->switchInterface.receiverContext);
  78. // good checksum
  79. udp->checksum_be =
  80. Checksum_udpIp6(ip6->sourceAddr,
  81. (uint8_t*)udp,
  82. CString_strlen(pingBenc) + Headers_UDPHeader_SIZE);
  83. struct Message m3 = {
  84. .bytes = buff+PADDING,
  85. .length = buffLen,
  86. .padding = PADDING,
  87. .alloc = alloc
  88. };
  89. Ducttape_injectIncomingForMe(&m3, &dt->pub, herPublicKey);
  90. Assert_true(dt->switchInterface.receiverContext);
  91. Allocator_free(alloc);
  92. Allocator_free(allocator);
  93. return 0;
  94. }