[svn] commit: r443 - in /branches/jinmei-dnsrrparams/src/lib/dns/cpp: rrclass.cc rrclass.h rrclass_unittest.cc rrtype.cc rrtype.h rrtype_unittest.cc testdata/rrcode16_fromWire1 testdata/rrcode16_fromWire2
BIND 10 source code commits
bind10-changes at lists.isc.org
Tue Jan 12 02:57:59 UTC 2010
Author: jinmei
Date: Tue Jan 12 02:57:59 2010
New Revision: 443
Log:
added "from wire" constructors
Added:
branches/jinmei-dnsrrparams/src/lib/dns/cpp/testdata/rrcode16_fromWire1
branches/jinmei-dnsrrparams/src/lib/dns/cpp/testdata/rrcode16_fromWire2
Modified:
branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrclass.cc
branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrclass.h
branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrclass_unittest.cc
branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrtype.cc
branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrtype.h
branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrtype_unittest.cc
Modified: branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrclass.cc
==============================================================================
--- branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrclass.cc (original)
+++ branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrclass.cc Tue Jan 12 02:57:59 2010
@@ -32,6 +32,14 @@
classcode_ = RRParamRegistry::getRegistry().getClassCode(classstr);
}
+RRClass::RRClass(InputBuffer& buffer)
+{
+ if (buffer.getLength() - buffer.getPosition() < sizeof(uint16_t)) {
+ dns_throw(IncompleteRRClass, "incomplete wire-format RR class");
+ }
+ classcode_ = buffer.readUint16();
+}
+
const string
RRClass::toText() const
{
Modified: branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrclass.h
==============================================================================
--- branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrclass.h (original)
+++ branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrclass.h Tue Jan 12 02:57:59 2010
@@ -23,6 +23,7 @@
namespace dns {
// forward declarations
+class InputBuffer;
class OutputBuffer;
class MessageRenderer;
@@ -35,14 +36,39 @@
isc::dns::Exception(file, line, what) {}
};
+///
+/// \brief A standard DNS module exception that is thrown if an RRClass object
+/// is being constructed from a incomplete (too short) wire-format data.
+///
+class IncompleteRRClass : public Exception {
+public:
+ IncompleteRRClass(const char* file, size_t line, const char* what) :
+ isc::dns::Exception(file, line, what) {}
+};
+
class RRClass {
public:
///
/// \name Constructors and Destructor
///
//@{
+ /// Constructor from an integer type code.
+ ///
explicit RRClass(uint16_t classcode) : classcode_(classcode) {}
+ /// Constructor from a string.
+ ///
explicit RRClass(const std::string& classstr);
+ /// Constructor from wire-format data.
+ ///
+ /// The \c buffer parameter normally stores a complete DNS message
+ /// containing the RRClass to be constructed. The current read position of
+ /// the buffer points to the head of the class.
+ ///
+ /// If the given data does not large enough to contain a 16-bit integer,
+ /// an exception of class \c IncompleteRRClass will be thrown.
+ ///
+ /// \param buffer A buffer storing the wire format data.
+ explicit RRClass(InputBuffer& buffer);
///
/// We use the default copy constructor intentionally.
//@}
Modified: branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrclass_unittest.cc
==============================================================================
--- branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrclass_unittest.cc (original)
+++ branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrclass_unittest.cc Tue Jan 12 02:57:59 2010
@@ -16,18 +16,34 @@
#include <gtest/gtest.h>
+#include "buffer.h"
#include "rrparamregistry.h"
#include "rrclass.h"
+#include "unittest_util.h"
+
using namespace std;
-using isc::dns::RRClass;
+using namespace isc;
+using namespace isc::dns;
namespace {
class RRClassTest : public ::testing::Test {
protected:
+ static RRClass rrclassFactoryFromWire(const char* datafile);
};
-TEST_F(RRClassTest, construct)
+RRClass
+RRClassTest::rrclassFactoryFromWire(const char* datafile)
+{
+ std::vector<unsigned char> data;
+ UnitTestUtil::readWireData(datafile, data);
+
+ InputBuffer buffer(&data[0], data.size());
+
+ return (RRClass(buffer));
+}
+
+TEST_F(RRClassTest, fromText)
{
EXPECT_EQ("IN", RRClass("IN").toText());
EXPECT_EQ("CH", RRClass("CH").toText());
@@ -36,15 +52,23 @@
// some uncommon cases: see the corresponding RRType tests.
EXPECT_EQ(53, RRClass("CLASS00053").getCode());
- EXPECT_THROW(RRClass("CLASS000053"), isc::dns::InvalidRRClass);
+ EXPECT_THROW(RRClass("CLASS000053"), InvalidRRClass);
// bogus CLASSnnn representations: should trigger an exception
- EXPECT_THROW(RRClass("CLASS"), isc::dns::InvalidRRClass);
- EXPECT_THROW(RRClass("CLASS-1"), isc::dns::InvalidRRClass);
- EXPECT_THROW(RRClass("CLASSxxx"), isc::dns::InvalidRRClass);
- EXPECT_THROW(RRClass("CLASS65536"), isc::dns::InvalidRRClass);
- EXPECT_THROW(RRClass("CLASS6500x"), isc::dns::InvalidRRClass);
- EXPECT_THROW(RRClass("CLASS65000 "), isc::dns::InvalidRRClass);
+ EXPECT_THROW(RRClass("CLASS"), InvalidRRClass);
+ EXPECT_THROW(RRClass("CLASS-1"), InvalidRRClass);
+ EXPECT_THROW(RRClass("CLASSxxx"), InvalidRRClass);
+ EXPECT_THROW(RRClass("CLASS65536"), InvalidRRClass);
+ EXPECT_THROW(RRClass("CLASS6500x"), InvalidRRClass);
+ EXPECT_THROW(RRClass("CLASS65000 "), InvalidRRClass);
+}
+
+TEST_F(RRClassTest, fromWire)
+{
+ EXPECT_EQ(0x1234,
+ rrclassFactoryFromWire("testdata/rrcode16_fromWire1").getCode());
+ EXPECT_THROW(rrclassFactoryFromWire("testdata/rrcode16_fromWire2"),
+ IncompleteRRClass);
}
TEST_F(RRClassTest, caseConstruct)
Modified: branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrtype.cc
==============================================================================
--- branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrtype.cc (original)
+++ branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrtype.cc Tue Jan 12 02:57:59 2010
@@ -33,6 +33,14 @@
typecode_ = RRParamRegistry::getRegistry().getTypeCode(typestr);
}
+RRType::RRType(InputBuffer& buffer)
+{
+ if (buffer.getLength() - buffer.getPosition() < sizeof(uint16_t)) {
+ dns_throw(IncompleteRRType, "incomplete wire-format RR type");
+ }
+ typecode_ = buffer.readUint16();
+}
+
const string
RRType::toText() const
{
Modified: branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrtype.h
==============================================================================
--- branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrtype.h (original)
+++ branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrtype.h Tue Jan 12 02:57:59 2010
@@ -23,6 +23,7 @@
namespace dns {
// forward declarations
+class InputBuffer;
class OutputBuffer;
class MessageRenderer;
@@ -35,14 +36,39 @@
isc::dns::Exception(file, line, what) {}
};
+///
+/// \brief A standard DNS module exception that is thrown if an RRType object
+/// is being constructed from a incomplete (too short) wire-format data.
+///
+class IncompleteRRType : public Exception {
+public:
+ IncompleteRRType(const char* file, size_t line, const char* what) :
+ isc::dns::Exception(file, line, what) {}
+};
+
class RRType {
public:
///
/// \name Constructors and Destructor
///
//@{
+ /// Constructor from an integer type code.
+ ///
explicit RRType(uint16_t typecode) : typecode_(typecode) {}
+ /// Constructor from a string.
+ ///
explicit RRType(const std::string& typestr);
+ /// Constructor from wire-format data.
+ ///
+ /// The \c buffer parameter normally stores a complete DNS message
+ /// containing the RRType to be constructed. The current read position of
+ /// the buffer points to the head of the type.
+ ///
+ /// If the given data does not large enough to contain a 16-bit integer,
+ /// an exception of class \c IncompleteRRType will be thrown.
+ ///
+ /// \param buffer A buffer storing the wire format data.
+ explicit RRType(InputBuffer& buffer);
///
/// We use the default copy constructor intentionally.
//@}
Modified: branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrtype_unittest.cc
==============================================================================
--- branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrtype_unittest.cc (original)
+++ branches/jinmei-dnsrrparams/src/lib/dns/cpp/rrtype_unittest.cc Tue Jan 12 02:57:59 2010
@@ -16,18 +16,34 @@
#include <gtest/gtest.h>
+#include "buffer.h"
#include "rrparamregistry.h"
#include "rrtype.h"
+#include "unittest_util.h"
+
using namespace std;
-using isc::dns::RRType;
+using namespace isc;
+using namespace isc::dns;
namespace {
class RRTypeTest : public ::testing::Test {
protected:
+ static RRType rrtypeFactoryFromWire(const char* datafile);
};
-TEST_F(RRTypeTest, construct)
+RRType
+RRTypeTest::rrtypeFactoryFromWire(const char* datafile)
+{
+ std::vector<unsigned char> data;
+ UnitTestUtil::readWireData(datafile, data);
+
+ InputBuffer buffer(&data[0], data.size());
+
+ return (RRType(buffer));
+}
+
+TEST_F(RRTypeTest, fromText)
{
EXPECT_EQ("A", RRType("A").toText());
EXPECT_EQ("NS", RRType("NS").toText());
@@ -40,15 +56,23 @@
// again, unusual, and the majority of other implementations reject it.
// In any case, there should be no reasonable reason to accept such a
// ridiculously long input.
- EXPECT_THROW(RRType("TYPE000053"), isc::dns::InvalidRRType);
+ EXPECT_THROW(RRType("TYPE000053"), InvalidRRType);
// bogus TYPEnnn representations: should trigger an exception
- EXPECT_THROW(RRType("TYPE"), isc::dns::InvalidRRType);
- EXPECT_THROW(RRType("TYPE-1"), isc::dns::InvalidRRType);
- EXPECT_THROW(RRType("TYPExxx"), isc::dns::InvalidRRType);
- EXPECT_THROW(RRType("TYPE65536"), isc::dns::InvalidRRType);
- EXPECT_THROW(RRType("TYPE6500x"), isc::dns::InvalidRRType);
- EXPECT_THROW(RRType("TYPE65000 "), isc::dns::InvalidRRType);
+ EXPECT_THROW(RRType("TYPE"), InvalidRRType);
+ EXPECT_THROW(RRType("TYPE-1"), InvalidRRType);
+ EXPECT_THROW(RRType("TYPExxx"), InvalidRRType);
+ EXPECT_THROW(RRType("TYPE65536"), InvalidRRType);
+ EXPECT_THROW(RRType("TYPE6500x"), InvalidRRType);
+ EXPECT_THROW(RRType("TYPE65000 "), InvalidRRType);
+}
+
+TEST_F(RRTypeTest, fromWire)
+{
+ EXPECT_EQ(0x1234,
+ rrtypeFactoryFromWire("testdata/rrcode16_fromWire1").getCode());
+ EXPECT_THROW(rrtypeFactoryFromWire("testdata/rrcode16_fromWire2"),
+ IncompleteRRType);
}
// from string, lower case
More information about the bind10-changes
mailing list