1
0

DefaultInterfaceController_test.c 6.3 KB

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