DefaultInterfaceController_test.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "crypto/random/Random.h"
  18. #include "crypto/CryptoAuth.h"
  19. #include "dht/ReplyModule.h"
  20. #include "dht/dhtcore/RouterModule.h"
  21. #include "dht/SerializationModule.h"
  22. #include "net/DefaultInterfaceController.h"
  23. #include "io/Writer.h"
  24. #include "io/FileWriter.h"
  25. #include "util/log/Log.h"
  26. #include "util/log/WriterLog.h"
  27. #include "util/events/EventBase.h"
  28. #include "util/platform/libc/string.h"
  29. #include "memory/MallocAllocator.h"
  30. #include "memory/Allocator.h"
  31. #include "switch/NumberCompress.h"
  32. #include "switch/SwitchCore.h"
  33. #include "wire/Headers.h"
  34. #include "test/TestFramework.h"
  35. static uint8_t messageFromInterface(struct Message* message, struct Interface* thisIf)
  36. {
  37. *((struct Message**) thisIf->senderContext) = message;
  38. return 0;
  39. }
  40. static int reconnectionNewEndpointTest(struct InterfaceController* ifController,
  41. uint8_t* pk,
  42. struct Message** fromSwitchPtr,
  43. struct Allocator* alloc,
  44. struct EventBase* eventBase,
  45. struct Log* logger,
  46. struct Interface* routerIf,
  47. struct Random* rand)
  48. {
  49. struct Message* message;
  50. struct Interface iface = {
  51. .sendMessage = messageFromInterface,
  52. .senderContext = &message,
  53. .allocator = alloc
  54. };
  55. uint8_t* buffer = Allocator_malloc(alloc, 512);
  56. struct Message* outgoing =
  57. &(struct Message) { .length = 0, .padding = 512, .bytes = buffer + 512 };
  58. struct CryptoAuth* externalCa = CryptoAuth_new(alloc, NULL, eventBase, logger, rand);
  59. struct Interface* wrapped = CryptoAuth_wrapInterface(&iface, pk, NULL, false, "", externalCa);
  60. CryptoAuth_setAuth(String_CONST("passwd"), 1, wrapped);
  61. struct Interface icIface = {
  62. .allocator = alloc,
  63. .sendMessage = messageFromInterface,
  64. .senderContext = &message
  65. };
  66. InterfaceController_registerPeer(ifController, NULL, NULL, true, false, &icIface);
  67. uint8_t hexBuffer[1025];
  68. for (int i = 0; i < 4; i++) {
  69. outgoing->length = 0;
  70. outgoing->padding = 512;
  71. outgoing->bytes = buffer + 512;
  72. Message_shift(outgoing, 12, NULL);
  73. Bits_memcpyConst(outgoing->bytes, "hello world", 12);
  74. Message_shift(outgoing, Headers_SwitchHeader_SIZE, NULL);
  75. Bits_memcpyConst(outgoing->bytes, (&(struct Headers_SwitchHeader) {
  76. .label_be = Endian_hostToBigEndian64(1),
  77. .lowBits_be = 0
  78. }), Headers_SwitchHeader_SIZE);
  79. wrapped->sendMessage(outgoing, wrapped);
  80. *fromSwitchPtr = NULL;
  81. icIface.receiveMessage(outgoing, &icIface);
  82. message = *fromSwitchPtr;
  83. Assert_always(message);
  84. Assert_always(message->length == 24);
  85. Hex_encode(hexBuffer, 1025, message->bytes, message->length);
  86. printf("%s\n", hexBuffer);
  87. // Need to bounce the packets back when connecting after the first try.
  88. // This is needed to establish the CryptoAuth session and make the InterfaceController
  89. // merge the endpoints.
  90. if (i > 0) {
  91. // Reverse the bits to reverse the path:
  92. uint64_t path;
  93. Bits_memcpyConst(&path, message->bytes, 8);
  94. path = Bits_bitReverse64(path);
  95. Bits_memcpyConst(message->bytes, &path, 8);
  96. printf("sending back response.\n");
  97. routerIf->receiveMessage(message, routerIf);
  98. printf("forwarding response to external cryptoAuth.\n");
  99. iface.receiveMessage(message, &iface);
  100. printf("forwarded.\n");
  101. } else {
  102. printf("not responding because we don't want to establish a connection yet.\n");
  103. }
  104. }
  105. // check everything except the label
  106. Assert_always(!strcmp((char*)hexBuffer+16, "0000000068656c6c6f20776f726c6400"));
  107. // check label: make sure the interface has been switched back into position 0.
  108. uint64_t label_be;
  109. Hex_decode((uint8_t*) &label_be, 8, hexBuffer, 16);
  110. uint64_t rev_label = Bits_bitReverse64(Endian_bigEndianToHost64(label_be));
  111. // check label is decoded to 0
  112. Assert_always(0 == NumberCompress_getDecompressed(rev_label,
  113. NumberCompress_bitsUsedForLabel(rev_label)));
  114. // check no other bits are set
  115. uint64_t out = NumberCompress_getCompressed(0, NumberCompress_bitsUsedForLabel(rev_label));
  116. Assert_always(rev_label == out);
  117. return 0;
  118. }
  119. int main()
  120. {
  121. struct Allocator* alloc = MallocAllocator_new(1<<20);
  122. struct TestFramework* tf =
  123. TestFramework_setUp("\xad\x7e\xa3\x26\xaa\x01\x94\x0a\x25\xbc\x9e\x01\x26\x22\xdb\x69"
  124. "\x4f\xd9\xb4\x17\x7c\xf3\xf8\x91\x16\xf3\xcf\xe8\x5c\x80\xe1\x4a",
  125. alloc, NULL);
  126. CryptoAuth_addUser(String_CONST("passwd"), 1, String_CONST("TEST"), tf->cryptoAuth);
  127. struct Message* message;
  128. struct Interface iface = {
  129. .sendMessage = messageFromInterface,
  130. .senderContext = &message,
  131. .allocator = alloc
  132. };
  133. SwitchCore_setRouterInterface(&iface, tf->switchCore);
  134. ////////////////////////
  135. int ret = reconnectionNewEndpointTest(tf->ifController,
  136. tf->publicKey,
  137. &message,
  138. alloc,
  139. tf->eventBase,
  140. tf->logger,
  141. &iface,
  142. tf->rand);
  143. Allocator_free(alloc);
  144. return ret;
  145. }