aria2/test/SocketCoreTest.cc
Tatsuhiro Tsujikawa 2cc471ebf6 2008-02-20 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
IPv6 support for SocketCore class.
	TODO: In SocketCore::establishConnection(), this is insufficient 
to
	determin the failure of connect() here because the socket is
	non-blocking state. The next addresses should be tried after 
select().
	TODO: NameResolver still uses c-ares(<= 1.4) 
ares_gethostbyname().
	If c-ares 1.5 or newer is installed, ares_getaddrinfo() should 
be used
	instead which address family independent.
	TODO: DHTRoutingTable{Deserializer,Serializer} currently saves 
peer
	information in a compact peer format which is for IPv4 only.
	
	Some BitTorrent functions in PeerMessageUtil still depends on 
IPv4 but
	this is a spec of BitTorrent protocol.
	* src/SocketCore.{h, cc}
	* src/PeerMessageUtil.cc
	* test/SocketCoreTest.cc
	* test/PeerMessageUtilTest.cc
	* test/DHTConnectionImplTest.cc

	Handle IPv4-mapped addresses.
	* src/DHTNode.cc: Now identity is determined by node id.
	* src/DHTMessageTrackerEntry.cc

	Because now PeerMessageUtil::unpackcompact() could fail, the 
caller
	should handle it.
	* src/DHTRoutingTableDeserializer.cc
	* src/DHTMessageFactoryImpl.cc
2008-02-20 13:02:48 +00:00

62 lines
1.7 KiB
C++

#include "SocketCore.h"
#include "Exception.h"
#include <cppunit/extensions/HelperMacros.h>
namespace aria2 {
class SocketCoreTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(SocketCoreTest);
CPPUNIT_TEST(testWriteAndReadDatagram);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {}
void tearDown() {}
void testWriteAndReadDatagram();
};
CPPUNIT_TEST_SUITE_REGISTRATION(SocketCoreTest);
void SocketCoreTest::testWriteAndReadDatagram()
{
try {
SocketCore s(SOCK_DGRAM);
s.bind(0);
SocketCore c(SOCK_DGRAM);
c.bind(0);
std::pair<std::string, int32_t> svaddr;
s.getAddrInfo(svaddr);
std::string message1 = "hello world.";
c.writeData(message1.c_str(), message1.size(), "localhost", svaddr.second);
std::string message2 = "chocolate coated pie";
c.writeData(message2.c_str(), message2.size(), "localhost", svaddr.second);
char readbuffer[100];
std::pair<std::string, uint16_t> peer;
{
ssize_t rlength = s.readDataFrom(readbuffer, sizeof(readbuffer), peer);
// commented out because ip address may vary
//CPPUNIT_ASSERT_EQUAL(std::std::string("127.0.0.1"), peer.first);
CPPUNIT_ASSERT_EQUAL((ssize_t)message1.size(), rlength);
readbuffer[rlength] = '\0';
CPPUNIT_ASSERT_EQUAL(message1, std::string(readbuffer));
}
{
ssize_t rlength = s.readDataFrom(readbuffer, sizeof(readbuffer), peer);
CPPUNIT_ASSERT_EQUAL((ssize_t)message2.size(), rlength);
readbuffer[rlength] = '\0';
CPPUNIT_ASSERT_EQUAL(message2, std::string(readbuffer));
}
} catch(Exception* e) {
std::cerr << *e << std::endl;
delete e;
CPPUNIT_FAIL("exception thrown");
}
}
} // namespace aria2