2008-02-18 13:41:58 +00:00
|
|
|
#include "MSEHandshake.h"
|
2008-11-03 06:49:02 +00:00
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2008-02-18 13:41:58 +00:00
|
|
|
#include "Exception.h"
|
|
|
|
#include "Util.h"
|
|
|
|
#include "prefs.h"
|
|
|
|
#include "Socket.h"
|
|
|
|
#include "Option.h"
|
|
|
|
#include "MockBtContext.h"
|
|
|
|
#include "FileEntry.h"
|
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
|
|
|
class MSEHandshakeTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(MSEHandshakeTest);
|
|
|
|
CPPUNIT_TEST(testHandshake);
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
private:
|
|
|
|
SharedHandle<MockBtContext> _btctx;
|
|
|
|
|
|
|
|
void doHandshake(const SharedHandle<MSEHandshake>& initiator,
|
|
|
|
const SharedHandle<MSEHandshake>& receiver);
|
|
|
|
|
|
|
|
public:
|
|
|
|
void setUp()
|
|
|
|
{
|
2008-04-20 00:50:22 +00:00
|
|
|
_btctx.reset(new MockBtContext());
|
2008-02-18 13:41:58 +00:00
|
|
|
unsigned char infoHash[20];
|
|
|
|
memset(infoHash, 0, sizeof(infoHash));
|
|
|
|
_btctx->setInfoHash(infoHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
void testHandshake();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(MSEHandshakeTest);
|
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
static std::pair<SharedHandle<SocketCore>,
|
|
|
|
SharedHandle<SocketCore> > createSocketPair()
|
2008-02-18 13:41:58 +00:00
|
|
|
{
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<SocketCore> initiatorSock(new SocketCore());
|
2008-02-18 13:41:58 +00:00
|
|
|
|
|
|
|
SocketCore receiverServerSock;
|
|
|
|
receiverServerSock.bind(0);
|
|
|
|
receiverServerSock.beginListen();
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
std::pair<std::string, uint16_t> receiverAddrInfo;
|
2008-02-18 13:41:58 +00:00
|
|
|
receiverServerSock.getAddrInfo(receiverAddrInfo);
|
|
|
|
initiatorSock->establishConnection("127.0.0.1", receiverAddrInfo.second);
|
|
|
|
initiatorSock->setBlockingMode();
|
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<SocketCore> receiverSock(receiverServerSock.acceptConnection());
|
2008-02-18 13:41:58 +00:00
|
|
|
receiverSock->setBlockingMode();
|
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
return std::pair<SharedHandle<SocketCore>,
|
|
|
|
SharedHandle<SocketCore> >(initiatorSock, receiverSock);
|
2008-02-18 13:41:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MSEHandshakeTest::doHandshake(const SharedHandle<MSEHandshake>& initiator, const SharedHandle<MSEHandshake>& receiver)
|
|
|
|
{
|
|
|
|
initiator->sendPublicKey();
|
|
|
|
|
|
|
|
while(!receiver->receivePublicKey());
|
|
|
|
receiver->sendPublicKey();
|
|
|
|
|
|
|
|
while(!initiator->receivePublicKey());
|
|
|
|
initiator->initCipher(_btctx->getInfoHash());
|
|
|
|
initiator->sendInitiatorStep2();
|
|
|
|
|
|
|
|
while(!receiver->findReceiverHashMarker());
|
2008-11-03 06:49:02 +00:00
|
|
|
std::deque<SharedHandle<BtContext> > btContexts;
|
|
|
|
btContexts.push_back(_btctx);
|
|
|
|
while(!receiver->receiveReceiverHashAndPadCLength(btContexts));
|
2008-02-18 13:41:58 +00:00
|
|
|
while(!receiver->receivePad());
|
|
|
|
while(!receiver->receiveReceiverIALength());
|
|
|
|
while(!receiver->receiveReceiverIA());
|
|
|
|
receiver->sendReceiverStep2();
|
|
|
|
|
|
|
|
while(!initiator->findInitiatorVCMarker());
|
|
|
|
while(!initiator->receiveInitiatorCryptoSelectAndPadDLength());
|
|
|
|
while(!initiator->receivePad());
|
|
|
|
}
|
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
static SharedHandle<MSEHandshake>
|
|
|
|
createMSEHandshake(SharedHandle<SocketCore> socket, bool initiator,
|
|
|
|
const Option* option)
|
2008-02-18 13:41:58 +00:00
|
|
|
{
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MSEHandshake> h(new MSEHandshake(1, socket, option));
|
2008-02-18 13:41:58 +00:00
|
|
|
h->initEncryptionFacility(initiator);
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MSEHandshakeTest::testHandshake()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Option op;
|
|
|
|
op.put(PREF_BT_MIN_CRYPTO_LEVEL, V_PLAIN);
|
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
std::pair<SharedHandle<SocketCore>, SharedHandle<SocketCore> > sockPair =
|
|
|
|
createSocketPair();
|
2008-02-18 13:41:58 +00:00
|
|
|
SharedHandle<MSEHandshake> initiator = createMSEHandshake(sockPair.first, true, &op);
|
|
|
|
SharedHandle<MSEHandshake> receiver = createMSEHandshake(sockPair.second, false, &op);
|
|
|
|
|
|
|
|
doHandshake(initiator, receiver);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(MSEHandshake::CRYPTO_PLAIN_TEXT, initiator->getNegotiatedCryptoType());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(MSEHandshake::CRYPTO_PLAIN_TEXT, receiver->getNegotiatedCryptoType());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
Option op;
|
|
|
|
op.put(PREF_BT_MIN_CRYPTO_LEVEL, V_ARC4);
|
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
std::pair<SharedHandle<SocketCore>, SharedHandle<SocketCore> > sockPair =
|
|
|
|
createSocketPair();
|
2008-02-18 13:41:58 +00:00
|
|
|
SharedHandle<MSEHandshake> initiator = createMSEHandshake(sockPair.first, true, &op);
|
|
|
|
SharedHandle<MSEHandshake> receiver = createMSEHandshake(sockPair.second, false, &op);
|
|
|
|
|
|
|
|
doHandshake(initiator, receiver);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(MSEHandshake::CRYPTO_ARC4, initiator->getNegotiatedCryptoType());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(MSEHandshake::CRYPTO_ARC4, receiver->getNegotiatedCryptoType());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace aria2
|