Rewritten util::inSameCidrBlock() to support IPv6 address.

We also introduced union sockaddr_union in a2netcompat.h.
This commit is contained in:
Tatsuhiro Tsujikawa 2011-10-19 23:14:13 +09:00
parent 2b68f72fa6
commit 74e5aa0ace
7 changed files with 102 additions and 58 deletions

View file

@ -15,6 +15,7 @@ class SocketCoreTest:public CppUnit::TestFixture {
CPPUNIT_TEST(testWriteAndReadDatagram);
CPPUNIT_TEST(testGetSocketError);
CPPUNIT_TEST(testInetNtop);
CPPUNIT_TEST(testGetBinAddr);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {}
@ -24,6 +25,7 @@ public:
void testWriteAndReadDatagram();
void testGetSocketError();
void testInetNtop();
void testGetBinAddr();
};
@ -104,4 +106,21 @@ void SocketCoreTest::testInetNtop()
}
}
void SocketCoreTest::testGetBinAddr()
{
unsigned char dest[16];
unsigned char ans1[] = { 192, 168, 0, 1 };
CPPUNIT_ASSERT_EQUAL((size_t)4, net::getBinAddr(dest, "192.168.0.1"));
CPPUNIT_ASSERT(std::equal(&dest[0], &dest[4], &ans1[0]));
unsigned char ans2[] = { 0x20u, 0x01u, 0x0du, 0xb8u,
0x00u, 0x00u, 0x00u, 0x00u,
0x00u, 0x00u, 0x00u, 0x00u,
0x00u, 0x02u, 0x00u, 0x01u };
CPPUNIT_ASSERT_EQUAL((size_t)16, net::getBinAddr(dest, "2001:db8::2:1"));
CPPUNIT_ASSERT(std::equal(&dest[0], &dest[16], &ans2[0]));
CPPUNIT_ASSERT_EQUAL((size_t)0, net::getBinAddr(dest, "localhost"));
}
} // namespace aria2