MSEHandshakeTest.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "MSEHandshake.h"
  2. #include <cstring>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "Exception.h"
  5. #include "util.h"
  6. #include "prefs.h"
  7. #include "SocketCore.h"
  8. #include "Option.h"
  9. #include "DownloadContext.h"
  10. #include "FileEntry.h"
  11. #include "array_fun.h"
  12. #include "bittorrent_helper.h"
  13. namespace aria2 {
  14. class MSEHandshakeTest : public CppUnit::TestFixture {
  15. CPPUNIT_TEST_SUITE(MSEHandshakeTest);
  16. CPPUNIT_TEST(testHandshake);
  17. CPPUNIT_TEST_SUITE_END();
  18. private:
  19. std::shared_ptr<DownloadContext> dctx_;
  20. void doHandshake(const std::shared_ptr<MSEHandshake>& initiator,
  21. const std::shared_ptr<MSEHandshake>& receiver);
  22. public:
  23. void setUp()
  24. {
  25. dctx_.reset(new DownloadContext());
  26. unsigned char infoHash[20];
  27. memset(infoHash, 0, sizeof(infoHash));
  28. {
  29. auto torrentAttrs = make_unique<TorrentAttribute>();
  30. torrentAttrs->infoHash.assign(std::begin(infoHash), std::end(infoHash));
  31. dctx_->setAttribute(CTX_ATTR_BT, std::move(torrentAttrs));
  32. }
  33. }
  34. void testHandshake();
  35. };
  36. CPPUNIT_TEST_SUITE_REGISTRATION(MSEHandshakeTest);
  37. namespace {
  38. std::pair<std::shared_ptr<SocketCore>, std::shared_ptr<SocketCore>>
  39. createSocketPair()
  40. {
  41. std::shared_ptr<SocketCore> initiatorSock(new SocketCore());
  42. SocketCore receiverServerSock;
  43. receiverServerSock.bind(0);
  44. receiverServerSock.beginListen();
  45. receiverServerSock.setBlockingMode();
  46. auto endpoint = receiverServerSock.getAddrInfo();
  47. initiatorSock->establishConnection("localhost", endpoint.port);
  48. initiatorSock->setBlockingMode();
  49. std::shared_ptr<SocketCore> receiverSock(
  50. receiverServerSock.acceptConnection());
  51. receiverSock->setBlockingMode();
  52. return std::pair<std::shared_ptr<SocketCore>, std::shared_ptr<SocketCore>>(
  53. initiatorSock, receiverSock);
  54. }
  55. } // namespace
  56. void MSEHandshakeTest::doHandshake(
  57. const std::shared_ptr<MSEHandshake>& initiator,
  58. const std::shared_ptr<MSEHandshake>& receiver)
  59. {
  60. initiator->sendPublicKey();
  61. while (initiator->getWantWrite()) {
  62. initiator->send();
  63. }
  64. while (!receiver->receivePublicKey()) {
  65. receiver->read();
  66. }
  67. receiver->sendPublicKey();
  68. while (receiver->getWantWrite()) {
  69. receiver->send();
  70. }
  71. while (!initiator->receivePublicKey()) {
  72. initiator->read();
  73. }
  74. initiator->initCipher(bittorrent::getInfoHash(dctx_));
  75. initiator->sendInitiatorStep2();
  76. while (initiator->getWantWrite()) {
  77. initiator->send();
  78. }
  79. while (!receiver->findReceiverHashMarker()) {
  80. receiver->read();
  81. }
  82. std::vector<std::shared_ptr<DownloadContext>> contexts;
  83. contexts.push_back(dctx_);
  84. while (!receiver->receiveReceiverHashAndPadCLength(contexts)) {
  85. receiver->read();
  86. }
  87. while (!receiver->receivePad()) {
  88. receiver->read();
  89. }
  90. while (!receiver->receiveReceiverIALength()) {
  91. receiver->read();
  92. }
  93. while (!receiver->receiveReceiverIA()) {
  94. receiver->read();
  95. }
  96. receiver->sendReceiverStep2();
  97. while (receiver->getWantWrite()) {
  98. receiver->send();
  99. }
  100. while (!initiator->findInitiatorVCMarker()) {
  101. initiator->read();
  102. }
  103. while (!initiator->receiveInitiatorCryptoSelectAndPadDLength()) {
  104. initiator->read();
  105. }
  106. while (!initiator->receivePad()) {
  107. initiator->read();
  108. }
  109. }
  110. namespace {
  111. std::shared_ptr<MSEHandshake>
  112. createMSEHandshake(std::shared_ptr<SocketCore> socket, bool initiator,
  113. const Option* option)
  114. {
  115. std::shared_ptr<MSEHandshake> h(new MSEHandshake(1, socket, option));
  116. h->initEncryptionFacility(initiator);
  117. return h;
  118. }
  119. } // namespace
  120. void MSEHandshakeTest::testHandshake()
  121. {
  122. {
  123. Option op;
  124. op.put(PREF_BT_MIN_CRYPTO_LEVEL, V_PLAIN);
  125. std::pair<std::shared_ptr<SocketCore>, std::shared_ptr<SocketCore>>
  126. sockPair = createSocketPair();
  127. std::shared_ptr<MSEHandshake> initiator =
  128. createMSEHandshake(sockPair.first, true, &op);
  129. std::shared_ptr<MSEHandshake> receiver =
  130. createMSEHandshake(sockPair.second, false, &op);
  131. doHandshake(initiator, receiver);
  132. CPPUNIT_ASSERT_EQUAL(MSEHandshake::CRYPTO_PLAIN_TEXT,
  133. initiator->getNegotiatedCryptoType());
  134. CPPUNIT_ASSERT_EQUAL(MSEHandshake::CRYPTO_PLAIN_TEXT,
  135. receiver->getNegotiatedCryptoType());
  136. }
  137. {
  138. Option op;
  139. op.put(PREF_BT_MIN_CRYPTO_LEVEL, V_ARC4);
  140. std::pair<std::shared_ptr<SocketCore>, std::shared_ptr<SocketCore>>
  141. sockPair = createSocketPair();
  142. std::shared_ptr<MSEHandshake> initiator =
  143. createMSEHandshake(sockPair.first, true, &op);
  144. std::shared_ptr<MSEHandshake> receiver =
  145. createMSEHandshake(sockPair.second, false, &op);
  146. doHandshake(initiator, receiver);
  147. CPPUNIT_ASSERT_EQUAL(MSEHandshake::CRYPTO_ARC4,
  148. initiator->getNegotiatedCryptoType());
  149. CPPUNIT_ASSERT_EQUAL(MSEHandshake::CRYPTO_ARC4,
  150. receiver->getNegotiatedCryptoType());
  151. }
  152. }
  153. } // namespace aria2