Refactor SocketCore::getPeerInfo, getAddrInfo to return Endpoint object

This commit is contained in:
Tatsuhiro Tsujikawa 2016-01-09 18:16:08 +09:00
parent e899eba76f
commit ea4d99ea08
19 changed files with 91 additions and 109 deletions

View file

@ -43,26 +43,30 @@ void SocketCoreTest::testWriteAndReadDatagram()
SocketCore c(SOCK_DGRAM);
c.bind(0);
std::pair<std::string, uint16_t> svaddr;
s.getAddrInfo(svaddr);
auto remoteEndpoint = s.getAddrInfo();
std::string message1 = "hello world.";
c.writeData(message1.c_str(), message1.size(), "localhost", svaddr.second);
c.writeData(message1.c_str(), message1.size(), "localhost",
remoteEndpoint.port);
std::string message2 = "chocolate coated pie";
c.writeData(message2.c_str(), message2.size(), "localhost", svaddr.second);
c.writeData(message2.c_str(), message2.size(), "localhost",
remoteEndpoint.port);
char readbuffer[100];
std::pair<std::string, uint16_t> peer;
{
ssize_t rlength = s.readDataFrom(readbuffer, sizeof(readbuffer), peer);
ssize_t rlength =
s.readDataFrom(readbuffer, sizeof(readbuffer), remoteEndpoint);
// commented out because ip address may vary
// CPPUNIT_ASSERT_EQUAL(std::std::string("127.0.0.1"), peer.first);
// CPPUNIT_ASSERT_EQUAL(std::std::string("127.0.0.1"),
// remoteEndpoint.addr);
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);
ssize_t rlength =
s.readDataFrom(readbuffer, sizeof(readbuffer), remoteEndpoint);
CPPUNIT_ASSERT_EQUAL((ssize_t)message2.size(), rlength);
readbuffer[rlength] = '\0';
CPPUNIT_ASSERT_EQUAL(message2, std::string(readbuffer));