MSEHandshakeTest.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "MSEHandshake.h"
  2. #include "Exception.h"
  3. #include "Util.h"
  4. #include "prefs.h"
  5. #include "Socket.h"
  6. #include "Option.h"
  7. #include "BtRegistry.h"
  8. #include "MockBtContext.h"
  9. #include "FileEntry.h"
  10. #include <cppunit/extensions/HelperMacros.h>
  11. namespace aria2 {
  12. class MSEHandshakeTest:public CppUnit::TestFixture {
  13. CPPUNIT_TEST_SUITE(MSEHandshakeTest);
  14. CPPUNIT_TEST(testHandshake);
  15. CPPUNIT_TEST_SUITE_END();
  16. private:
  17. SharedHandle<MockBtContext> _btctx;
  18. void doHandshake(const SharedHandle<MSEHandshake>& initiator,
  19. const SharedHandle<MSEHandshake>& receiver);
  20. public:
  21. void setUp()
  22. {
  23. _btctx = new MockBtContext();
  24. unsigned char infoHash[20];
  25. memset(infoHash, 0, sizeof(infoHash));
  26. _btctx->setInfoHash(infoHash);
  27. BtRegistry::unregisterAll();
  28. BtRegistry::registerBtContext(_btctx->getInfoHashAsString(), _btctx);
  29. }
  30. void tearDown()
  31. {
  32. BtRegistry::unregisterAll();
  33. }
  34. void testHandshake();
  35. };
  36. CPPUNIT_TEST_SUITE_REGISTRATION(MSEHandshakeTest);
  37. static std::pair<SocketCore*, SocketCore*> createSocketPair()
  38. {
  39. SocketCore* initiatorSock = new SocketCore();
  40. SocketCore receiverServerSock;
  41. receiverServerSock.bind(0);
  42. receiverServerSock.beginListen();
  43. std::pair<std::string, uint16_t> receiverAddrInfo;
  44. receiverServerSock.getAddrInfo(receiverAddrInfo);
  45. initiatorSock->establishConnection("127.0.0.1", receiverAddrInfo.second);
  46. initiatorSock->setBlockingMode();
  47. SocketCore* receiverSock = receiverServerSock.acceptConnection();
  48. receiverSock->setBlockingMode();
  49. return std::pair<SocketCore*, SocketCore*>(initiatorSock, receiverSock);
  50. }
  51. void MSEHandshakeTest::doHandshake(const SharedHandle<MSEHandshake>& initiator, const SharedHandle<MSEHandshake>& receiver)
  52. {
  53. initiator->sendPublicKey();
  54. while(!receiver->receivePublicKey());
  55. receiver->sendPublicKey();
  56. while(!initiator->receivePublicKey());
  57. initiator->initCipher(_btctx->getInfoHash());
  58. initiator->sendInitiatorStep2();
  59. while(!receiver->findReceiverHashMarker());
  60. while(!receiver->receiveReceiverHashAndPadCLength());
  61. while(!receiver->receivePad());
  62. while(!receiver->receiveReceiverIALength());
  63. while(!receiver->receiveReceiverIA());
  64. receiver->sendReceiverStep2();
  65. while(!initiator->findInitiatorVCMarker());
  66. while(!initiator->receiveInitiatorCryptoSelectAndPadDLength());
  67. while(!initiator->receivePad());
  68. }
  69. static MSEHandshake* createMSEHandshake(SocketCore* socket, bool initiator, const Option* option)
  70. {
  71. MSEHandshake* h = new MSEHandshake(1, socket, option);
  72. h->initEncryptionFacility(initiator);
  73. return h;
  74. }
  75. void MSEHandshakeTest::testHandshake()
  76. {
  77. {
  78. Option op;
  79. op.put(PREF_BT_MIN_CRYPTO_LEVEL, V_PLAIN);
  80. std::pair<SocketCore*, SocketCore*> sockPair = createSocketPair();
  81. SharedHandle<MSEHandshake> initiator = createMSEHandshake(sockPair.first, true, &op);
  82. SharedHandle<MSEHandshake> receiver = createMSEHandshake(sockPair.second, false, &op);
  83. doHandshake(initiator, receiver);
  84. CPPUNIT_ASSERT_EQUAL(MSEHandshake::CRYPTO_PLAIN_TEXT, initiator->getNegotiatedCryptoType());
  85. CPPUNIT_ASSERT_EQUAL(MSEHandshake::CRYPTO_PLAIN_TEXT, receiver->getNegotiatedCryptoType());
  86. }
  87. {
  88. Option op;
  89. op.put(PREF_BT_MIN_CRYPTO_LEVEL, V_ARC4);
  90. std::pair<SocketCore*, SocketCore*> sockPair = createSocketPair();
  91. SharedHandle<MSEHandshake> initiator = createMSEHandshake(sockPair.first, true, &op);
  92. SharedHandle<MSEHandshake> receiver = createMSEHandshake(sockPair.second, false, &op);
  93. doHandshake(initiator, receiver);
  94. CPPUNIT_ASSERT_EQUAL(MSEHandshake::CRYPTO_ARC4, initiator->getNegotiatedCryptoType());
  95. CPPUNIT_ASSERT_EQUAL(MSEHandshake::CRYPTO_ARC4, receiver->getNegotiatedCryptoType());
  96. }
  97. }
  98. } // namespace aria2