1
0

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