aria2/test/MSEHandshakeTest.cc
Tatsuhiro Tsujikawa 7cf589e784 2008-11-03 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Made BtRegistry non-static object. Now DownloadEngine has a
	reference to its object.
	* src/AbstractBtMessage.cc
	* src/ActivePeerConnectionCommand.cc
	* src/ActivePeerConnectionCommand.h
	* src/BtContextAwareCommand.cc
	* src/BtContextAwareCommand.h
	* src/BtExtendedMessage.cc
	* src/BtExtendedMessage.h
	* src/BtInterestedMessage.cc
	* src/BtInterestedMessage.h
	* src/BtNotInterestedMessage.cc
	* src/BtNotInterestedMessage.h
	* src/BtRegistry.cc
	* src/BtRegistry.h
	* src/BtSeederStateChoke.cc
	* src/BtSetup.cc
	* src/DHTGetPeersCommand.cc
	* src/DHTGetPeersCommand.h
	* src/DHTPeerAnnounceEntry.cc
	* src/DHTPeerAnnounceEntry.h
	* src/DHTPeerAnnounceStorage.cc
	* src/DHTPeerAnnounceStorage.h
	* src/DHTPeerLookupTask.cc
	* src/DHTPeerLookupTask.h
	* src/DHTSetup.cc
	* src/DHTSetup.h
	* src/DHTTaskFactory.h
	* src/DHTTaskFactoryImpl.cc
	* src/DHTTaskFactoryImpl.h
	* src/DefaultBtAnnounce.cc
	* src/DefaultBtInteractive.cc
	* src/DefaultBtInteractive.h
	* src/DefaultBtMessageDispatcher.cc
	* src/DefaultBtMessageDispatcher.h
	* src/DefaultBtMessageFactory.cc
	* src/DefaultBtMessageFactory.h
	* src/DefaultBtMessageReceiver.cc
	* src/DefaultBtProgressInfoFile.cc
	* src/DefaultBtProgressInfoFile.h
	* src/DefaultBtRequestFactory.cc
	* src/DefaultBtRequestFactory.h
	* src/DefaultExtensionMessageFactory.cc
	* src/DefaultExtensionMessageFactory.h
	* src/DefaultPeerStorage.cc
	* src/DefaultPeerStorage.h
	* src/DownloadEngine.cc
	* src/DownloadEngine.h
	* src/InitiatorMSEHandshakeCommand.cc
	* src/InitiatorMSEHandshakeCommand.h
	* src/MSEHandshake.cc
	* src/MSEHandshake.h
	* src/Makefile.am
	* src/Makefile.in
	* src/PeerChokeCommand.cc
	* src/PeerChokeCommand.h
	* src/PeerInitiateConnectionCommand.cc
	* src/PeerInitiateConnectionCommand.h
	* src/PeerInteractionCommand.cc
	* src/PeerInteractionCommand.h
	* src/PeerObject.h
	* src/PeerReceiveHandshakeCommand.cc
	* src/PeerReceiveHandshakeCommand.h
	* src/ReceiverMSEHandshakeCommand.cc
	* src/RequestGroup.cc
	* src/RequestGroup.h
	* src/RequestGroupMan.cc
	* src/RequestGroupMan.h
	* src/SeedCheckCommand.cc
	* src/SeedCheckCommand.h
	* src/ShareRatioSeedCriteria.h
	* src/SimpleBtMessage.cc
	* src/TrackerWatcherCommand.cc
	* src/TrackerWatcherCommand.h
	* src/UTPexExtensionMessage.cc
	* src/UTPexExtensionMessage.h
	* test/BtCancelMessageTest.cc
	* test/BtChokeMessageTest.cc
	* test/BtExtendedMessageTest.cc
	* test/BtInterestedMessageTest.cc
	* test/BtNotInterestedMessageTest.cc
	* test/BtPieceMessageTest.cc
	* test/BtPortMessageTest.cc
	* test/BtRegistryTest.cc
	* test/BtRejectMessageTest.cc
	* test/BtRequestMessageTest.cc
	* test/DHTPeerAnnounceEntryTest.cc
	* test/DHTPeerAnnounceStorageTest.cc
	* test/DefaultBtMessageDispatcherTest.cc
	* test/DefaultBtMessageFactoryTest.cc
	* test/DefaultBtProgressInfoFileTest.cc
	* test/DefaultBtRequestFactoryTest.cc
	* test/DefaultExtensionMessageFactoryTest.cc
	* test/MSEHandshakeTest.cc
	* test/MockDHTTaskFactory.h
	* test/ShareRatioSeedCriteriaTest.cc
	* test/UTPexExtensionMessageTest.cc
2008-11-03 06:49:02 +00:00

128 lines
3.8 KiB
C++

#include "MSEHandshake.h"
#include <cppunit/extensions/HelperMacros.h>
#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()
{
_btctx.reset(new MockBtContext());
unsigned char infoHash[20];
memset(infoHash, 0, sizeof(infoHash));
_btctx->setInfoHash(infoHash);
}
void testHandshake();
};
CPPUNIT_TEST_SUITE_REGISTRATION(MSEHandshakeTest);
static std::pair<SharedHandle<SocketCore>,
SharedHandle<SocketCore> > createSocketPair()
{
SharedHandle<SocketCore> initiatorSock(new SocketCore());
SocketCore receiverServerSock;
receiverServerSock.bind(0);
receiverServerSock.beginListen();
std::pair<std::string, uint16_t> receiverAddrInfo;
receiverServerSock.getAddrInfo(receiverAddrInfo);
initiatorSock->establishConnection("127.0.0.1", receiverAddrInfo.second);
initiatorSock->setBlockingMode();
SharedHandle<SocketCore> receiverSock(receiverServerSock.acceptConnection());
receiverSock->setBlockingMode();
return std::pair<SharedHandle<SocketCore>,
SharedHandle<SocketCore> >(initiatorSock, receiverSock);
}
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());
std::deque<SharedHandle<BtContext> > btContexts;
btContexts.push_back(_btctx);
while(!receiver->receiveReceiverHashAndPadCLength(btContexts));
while(!receiver->receivePad());
while(!receiver->receiveReceiverIALength());
while(!receiver->receiveReceiverIA());
receiver->sendReceiverStep2();
while(!initiator->findInitiatorVCMarker());
while(!initiator->receiveInitiatorCryptoSelectAndPadDLength());
while(!initiator->receivePad());
}
static SharedHandle<MSEHandshake>
createMSEHandshake(SharedHandle<SocketCore> socket, bool initiator,
const Option* option)
{
SharedHandle<MSEHandshake> h(new MSEHandshake(1, socket, option));
h->initEncryptionFacility(initiator);
return h;
}
void MSEHandshakeTest::testHandshake()
{
{
Option op;
op.put(PREF_BT_MIN_CRYPTO_LEVEL, V_PLAIN);
std::pair<SharedHandle<SocketCore>, SharedHandle<SocketCore> > sockPair =
createSocketPair();
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);
std::pair<SharedHandle<SocketCore>, SharedHandle<SocketCore> > sockPair =
createSocketPair();
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