Commit ffd4fcba authored by JINMEI Tatuya's avatar JINMEI Tatuya
Browse files

added more unittests for RRset


git-svn-id: svn://bind10.isc.org/svn/bind10/branches/jinmei-dnsrrset@873 e5f2f494-b856-4b98-b285-d166d9295462
parent ab794b88
......@@ -14,6 +14,8 @@
// $Id$
#include <stdexcept>
#include "buffer.h"
#include "messagerenderer.h"
#include "name.h"
......@@ -42,9 +44,16 @@ protected:
test_domain("example.com"),
test_nsname("ns.example.com"),
rrset_a(test_name, RRClass::IN(), RRType::A(), RRTTL(3600)),
rrset_a_empty(test_name, RRClass::IN(), RRType::A(),
RRTTL(3600)),
rrset_ns(test_domain, RRClass::IN(), RRType::NS(),
RRTTL(86400))
{}
RRTTL(86400)),
rrset_ch_txt(test_domain, RRClass::CH(), RRType::TXT(),
RRTTL(0))
{
rrset_a.addRdata(in::A("192.0.2.1"));
rrset_a.addRdata(in::A("192.0.2.2"));
}
OutputBuffer buffer;
MessageRenderer renderer;
......@@ -52,33 +61,124 @@ protected:
Name test_domain;
Name test_nsname;
RRset rrset_a;
RRset rrset_a_empty;
RRset rrset_ns;
RRset rrset_ch_txt;
std::vector<unsigned char> wiredata;
// max number of Rdata objects added to a test RRset object.
// this is an arbitrary chosen limit, but should be sufficiently large
// in practice and reasonable even as an extreme test case.
static const int MAX_RDATA_COUNT = 100;
};
TEST_F(RRsetTest, addRdata)
TEST_F(RRsetTest, getRdataCount)
{
for (int i = 0; i < MAX_RDATA_COUNT; ++i) {
EXPECT_EQ(i, rrset_a_empty.getRdataCount());
rrset_a_empty.addRdata(in::A("192.0.2.1"));
}
}
TEST_F(RRsetTest, getName)
{
EXPECT_EQ(test_name, rrset_a.getName());
EXPECT_EQ(test_domain, rrset_ns.getName());
}
TEST_F(RRsetTest, getClass)
{
EXPECT_EQ(RRClass("IN"), rrset_a.getClass());
EXPECT_EQ(RRClass("CH"), rrset_ch_txt.getClass());
}
TEST_F(RRsetTest, getType)
{
EXPECT_EQ(RRType("A"), rrset_a.getType());
EXPECT_EQ(RRType("NS"), rrset_ns.getType());
EXPECT_EQ(RRType("TXT"), rrset_ch_txt.getType());
}
TEST_F(RRsetTest, getTTL)
{
rrset_a.addRdata(in::A("192.0.2.1"));
rrset_a.addRdata(in::A("192.0.2.2"));
EXPECT_EQ(RRTTL(3600), rrset_a.getTTL());
EXPECT_EQ(RRTTL(86400), rrset_ns.getTTL());
EXPECT_EQ(RRTTL(0), rrset_ch_txt.getTTL());
}
RdataIteratorPtr it = rrset_a.getRdataIterator();
TEST_F(RRsetTest, setTTL)
{
rrset_a.setTTL(RRTTL(86400));
EXPECT_EQ(RRTTL(86400), rrset_a.getTTL());
rrset_a.setTTL(RRTTL(0));
EXPECT_EQ(RRTTL(0), rrset_a.getTTL());
}
void
addRdataTestCommon(const RRset& rrset)
{
EXPECT_EQ(2, rrset.getRdataCount());
RdataIteratorPtr it = rrset.getRdataIterator();
it->first();
EXPECT_FALSE(it->isLast());
EXPECT_EQ("192.0.2.1", it->getCurrent().toText());
EXPECT_EQ(0, it->getCurrent().compare(in::A("192.0.2.1")));
it->next();
EXPECT_FALSE(it->isLast());
EXPECT_EQ("192.0.2.2", it->getCurrent().toText());
EXPECT_EQ(0, it->getCurrent().compare(in::A("192.0.2.2")));
it->next();
EXPECT_TRUE(it->isLast());
}
TEST_F(RRsetTest, addRdata)
{
addRdataTestCommon(rrset_a);
// Reference version of addRdata() doesn't allow to add a different
// type of Rdata.
EXPECT_THROW(rrset_a.addRdata(generic::NS(test_nsname)), std::bad_cast);
}
TEST_F(RRsetTest, addRdataPtr)
{
rrset_a_empty.addRdata(createRdata(rrset_a_empty.getType(),
rrset_a_empty.getClass(),
"192.0.2.1"));
rrset_a_empty.addRdata(createRdata(rrset_a_empty.getType(),
rrset_a_empty.getClass(),
"192.0.2.2"));
addRdataTestCommon(rrset_a);
// Pointer version of addRdata() doesn't type check and does allow to
//add a different type of Rdata as a result.
rrset_a_empty.addRdata(createRdata(RRType::NS(), RRClass::IN(),
"ns.example.com"));
EXPECT_EQ(3, rrset_a_empty.getRdataCount());
}
TEST_F(RRsetTest, iterator)
{
// Iterator for an empty RRset.
RdataIteratorPtr it = rrset_a_empty.getRdataIterator();
it->first();
EXPECT_TRUE(it->isLast());
// Normal case (already tested, but do it again just in case)
rrset_a_empty.addRdata(in::A("192.0.2.1"));
rrset_a_empty.addRdata(in::A("192.0.2.2"));
addRdataTestCommon(rrset_a_empty);
}
TEST_F(RRsetTest, toText)
{
EXPECT_EQ("test.example.com. 3600 IN A 192.0.2.1\n"
"test.example.com. 3600 IN A 192.0.2.2\n", rrset_a.toText());
"test.example.com. 3600 IN A 192.0.2.2\n",
rrset_a.toText());
}
TEST_F(RRsetTest, toWireBuffer)
{
rrset_a.addRdata(in::A("192.0.2.1"));
rrset_a.addRdata(in::A("192.0.2.2"));
rrset_a.toWire(buffer);
UnitTestUtil::readWireData("testdata/rrset_toWire1", wiredata);
......@@ -88,8 +188,6 @@ TEST_F(RRsetTest, toWireBuffer)
TEST_F(RRsetTest, toWireRenderer)
{
rrset_a.addRdata(in::A("192.0.2.1"));
rrset_a.addRdata(in::A("192.0.2.2"));
rrset_ns.addRdata(generic::NS(test_nsname));
rrset_a.toWire(renderer);
......@@ -100,4 +198,11 @@ TEST_F(RRsetTest, toWireRenderer)
buffer.getLength(), &wiredata[0], wiredata.size());
}
// test operator<<. We simply confirm it appends the result of toText().
TEST_F(RRsetTest, LeftShiftOperator)
{
ostringstream oss;
oss << rrset_a;
EXPECT_EQ(rrset_a.toText(), oss.str());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment