Browse Source

[trac1113] update minfo unittest, avoid using hardcoded test data

chenzhengzhang 14 years ago
parent
commit
d7834356a3

+ 9 - 2
src/lib/dns/rdata/generic/minfo_14.cc

@@ -79,8 +79,7 @@ MINFO::MINFO(const std::string& minfo_str) :
 /// names in the wire is invalid.
 MINFO::MINFO(InputBuffer& buffer, size_t) :
     rmailbox_(buffer), emailbox_(buffer)
-{
-}
+{}
 
 /// \brief Copy constructor.
 ///
@@ -114,6 +113,14 @@ MINFO::toWire(OutputBuffer& buffer) const {
     emailbox_.toWire(buffer);
 }
 
+MINFO&
+MINFO::operator=(const MINFO& source) {
+    rmailbox_ = source.rmailbox_;
+    emailbox_ = source.emailbox_;
+
+    return (*this);
+}
+
 /// \brief Render the \c MINFO in the wire format with taking into account
 /// compression.
 ///

+ 5 - 0
src/lib/dns/rdata/generic/minfo_14.h

@@ -37,6 +37,11 @@ public:
     // BEGIN_COMMON_MEMBERS
     // END_COMMON_MEMBERS
 
+    /// \brief Define the assignment operator.
+    ///
+    /// This method never throws an exception.
+    MINFO& operator=(const MINFO& source);
+
     /// \brief Return the value of the rmailbox field.
     ///
     /// \exception std::bad_alloc If resource allocation for the returned

+ 72 - 26
src/lib/dns/tests/rdata_minfo_unittest.cc

@@ -31,21 +31,30 @@ using namespace isc::dns;
 using namespace isc::util;
 using namespace isc::dns::rdata;
 
-namespace {
-class Rdata_MINFO_Test : public RdataTest {
-    // there's nothing to specialize
-};
-
 // minfo text
 const char* const minfo_txt = "rmailbox.example.com. emailbox.example.com.";
+const char* const minfo_txt2 = "root.example.com. emailbox.example.com.";
 const char* const too_long_label = "01234567890123456789012345678901234567"
                                    "89012345678901234567890123";
 
-const generic::MINFO rdata_minfo((string(minfo_txt)));
+namespace {
+class Rdata_MINFO_Test : public RdataTest {
+    // there's nothing to specialize
+public:
+    Rdata_MINFO_Test():
+        rdata_minfo(string(minfo_txt)), rdata_minfo2(string(minfo_txt2)) {}
+
+    const generic::MINFO rdata_minfo;
+    const generic::MINFO rdata_minfo2;
+};
+
 
 TEST_F(Rdata_MINFO_Test, createFromText) {
     EXPECT_EQ(Name("rmailbox.example.com."), rdata_minfo.getRmailbox());
     EXPECT_EQ(Name("emailbox.example.com."), rdata_minfo.getEmailbox());
+
+    EXPECT_EQ(Name("root.example.com."), rdata_minfo2.getRmailbox());
+    EXPECT_EQ(Name("emailbox.example.com."), rdata_minfo2.getEmailbox());
 }
 
 TEST_F(Rdata_MINFO_Test, badText) {
@@ -57,60 +66,97 @@ TEST_F(Rdata_MINFO_Test, badText) {
                                 "example.com."),
                  InvalidRdataText);
     // bad rmailbox name
-    EXPECT_THROW(generic::MINFO("root.example.com. emailbx.example.com." +
+    EXPECT_THROW(generic::MINFO("root.example.com. emailbox.example.com." +
                                 string(too_long_label)),
                  TooLongLabel);
     // bad emailbox name
     EXPECT_THROW(generic::MINFO("root.example.com."  +
-                          string(too_long_label) + " emailbx.example.com."),
+                          string(too_long_label) + " emailbox.example.com."),
                  TooLongLabel);
 }
 
 TEST_F(Rdata_MINFO_Test, createFromWire) {
-    // compressed emailbx name
+    // uncompressed names
     EXPECT_EQ(0, rdata_minfo.compare(
                   *rdataFactoryFromFile(RRType::MINFO(), RRClass::IN(),
-                                        "rdata_minfo_fromWire")));
-    // compressed rmailbx and emailbx name
+                                     "rdata_minfo_fromWire1.wire")));
+    // compressed names
     EXPECT_EQ(0, rdata_minfo.compare(
-                  *rdataFactoryFromFile(RRType("MINFO"), RRClass::IN(),
-                                        "rdata_minfo_fromWire", 35)));
+                  *rdataFactoryFromFile(RRType::MINFO(), RRClass::IN(),
+                                     "rdata_minfo_fromWire2.wire", 15)));
     // RDLENGTH is too short
     EXPECT_THROW(rdataFactoryFromFile(RRType::MINFO(), RRClass::IN(),
-                                      "rdata_minfo_fromWire", 41),
+                                     "rdata_minfo_fromWire3.wire"),
                  InvalidRdataLength);
     // RDLENGTH is too long
     EXPECT_THROW(rdataFactoryFromFile(RRType::MINFO(), RRClass::IN(),
-                                      "rdata_minfo_fromWire", 47),
+                                      "rdata_minfo_fromWire4.wire"),
                  InvalidRdataLength);
-    // incomplete name.  the error should be detected in the name constructor
-    EXPECT_THROW(rdataFactoryFromFile(RRType("MINFO"), RRClass::IN(),
-                                      "rdata_minfo_fromWire", 53),
+    // bogus rmailbox name, the error should be detected in the name
+    // constructor
+    EXPECT_THROW(rdataFactoryFromFile(RRType::MINFO(), RRClass::IN(),
+                                      "rdata_minfo_fromWire5.wire"),
+                 DNSMessageFORMERR);
+    // bogus emailbox name, the error should be detected in the name
+    // constructor
+    EXPECT_THROW(rdataFactoryFromFile(RRType::MINFO(), RRClass::IN(),
+                                      "rdata_minfo_fromWire6.wire"),
                  DNSMessageFORMERR);
 }
 
+TEST_F(Rdata_MINFO_Test, assignment) {
+    generic::MINFO copy((string(minfo_txt2)));
+    copy = rdata_minfo;
+    EXPECT_EQ(0, copy.compare(rdata_minfo));
+
+    // Check if the copied data is valid even after the original is deleted
+    generic::MINFO* copy2 = new generic::MINFO(rdata_minfo);
+    generic::MINFO copy3((string(minfo_txt2)));
+    copy3 = *copy2;
+    delete copy2;
+    EXPECT_EQ(0, copy3.compare(rdata_minfo));
+
+    // Self assignment
+    copy = copy;
+    EXPECT_EQ(0, copy.compare(rdata_minfo));
+}
+
 TEST_F(Rdata_MINFO_Test, toWireBuffer) {
-    obuffer.skip(2);
     rdata_minfo.toWire(obuffer);
     vector<unsigned char> data;
-    UnitTestUtil::readWireData("rdata_minfo_toWireUncompressed.wire", data);
+    UnitTestUtil::readWireData("rdata_minfo_toWireUncompressed1.wire", data);
     EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
-                        static_cast<const uint8_t *>(obuffer.getData()) + 2,
-                        obuffer.getLength() - 2, &data[2], data.size() - 2);
+                        static_cast<const uint8_t *>(obuffer.getData()),
+                        obuffer.getLength(), &data[0], data.size());
+
+    obuffer.clear();
+    rdata_minfo2.toWire(obuffer);
+    vector<unsigned char> data2;
+    UnitTestUtil::readWireData("rdata_minfo_toWireUncompressed2.wire", data2);
+    EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
+                        static_cast<const uint8_t *>(obuffer.getData()),
+                        obuffer.getLength(), &data2[0], data2.size());
 }
 
 TEST_F(Rdata_MINFO_Test, toWireRenderer) {
-    obuffer.skip(2);
     rdata_minfo.toWire(renderer);
     vector<unsigned char> data;
-    UnitTestUtil::readWireData("rdata_minfo_toWire", data);
+    UnitTestUtil::readWireData("rdata_minfo_toWire1.wire", data);
+    EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
+                        static_cast<const uint8_t *>(obuffer.getData()),
+                        obuffer.getLength(), &data[0], data.size());
+    renderer.clear();
+    rdata_minfo2.toWire(renderer);
+    vector<unsigned char> data2;
+    UnitTestUtil::readWireData("rdata_minfo_toWire2.wire", data2);
     EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
-                        static_cast<const uint8_t *>(obuffer.getData()) + 2,
-                        obuffer.getLength() - 2, &data[2], data.size() - 2);
+                        static_cast<const uint8_t *>(obuffer.getData()),
+                        obuffer.getLength(), &data2[0], data2.size());
 }
 
 TEST_F(Rdata_MINFO_Test, toText) {
     EXPECT_EQ(minfo_txt, rdata_minfo.toText());
+    EXPECT_EQ(minfo_txt2, rdata_minfo2.toText());
 }
 
 TEST_F(Rdata_MINFO_Test, compare) {

+ 12 - 2
src/lib/dns/tests/testdata/Makefile.am

@@ -26,6 +26,12 @@ BUILT_SOURCES += rdata_nsec3_fromWire10.wire rdata_nsec3_fromWire11.wire
 BUILT_SOURCES += rdata_nsec3_fromWire12.wire rdata_nsec3_fromWire13.wire
 BUILT_SOURCES += rdata_nsec3_fromWire14.wire rdata_nsec3_fromWire15.wire
 BUILT_SOURCES += rdata_rrsig_fromWire2.wire
+BUILT_SOURCES += rdata_minfo_fromWire1.wire rdata_minfo_fromWire2.wire
+BUILT_SOURCES += rdata_minfo_fromWire3.wire rdata_minfo_fromWire4.wire
+BUILT_SOURCES += rdata_minfo_fromWire5.wire rdata_minfo_fromWire6.wire
+BUILT_SOURCES += rdata_minfo_toWire1.wire rdata_minfo_toWire2.wire
+BUILT_SOURCES += rdata_minfo_toWireUncompressed1.wire
+BUILT_SOURCES += rdata_minfo_toWireUncompressed2.wire
 BUILT_SOURCES += rdata_rp_fromWire1.wire rdata_rp_fromWire2.wire
 BUILT_SOURCES += rdata_rp_fromWire3.wire rdata_rp_fromWire4.wire
 BUILT_SOURCES += rdata_rp_fromWire5.wire rdata_rp_fromWire6.wire
@@ -101,8 +107,12 @@ EXTRA_DIST += rdata_rp_fromWire5.spec rdata_rp_fromWire6.spec
 EXTRA_DIST += rdata_rp_toWire1.spec rdata_rp_toWire2.spec
 EXTRA_DIST += rdata_soa_fromWire rdata_soa_toWireUncompressed.spec
 EXTRA_DIST += rdata_srv_fromWire
-EXTRA_DIST += rdata_minfo_fromWire rdata_minfo_toWireUncompressed.spec
-EXTRA_DIST += rdata_minfo_toWireUncompressed.wire rdata_minfo_toWire
+EXTRA_DIST += rdata_minfo_fromWire1.spec rdata_minfo_fromWire2.spec
+EXTRA_DIST += rdata_minfo_fromWire3.spec rdata_minfo_fromWire4.spec
+EXTRA_DIST += rdata_minfo_fromWire5.spec rdata_minfo_fromWire6.spec
+EXTRA_DIST += rdata_minfo_toWire1.spec rdata_minfo_toWire2.spec
+EXTRA_DIST += rdata_minfo_toWireUncompressed1.spec
+EXTRA_DIST += rdata_minfo_toWireUncompressed2.spec
 EXTRA_DIST += rdata_txt_fromWire1 rdata_txt_fromWire2.spec
 EXTRA_DIST += rdata_txt_fromWire3.spec rdata_txt_fromWire4.spec
 EXTRA_DIST += rdata_txt_fromWire5.spec rdata_unknown_fromWire

+ 0 - 47
src/lib/dns/tests/testdata/rdata_minfo_fromWire

@@ -1,47 +0,0 @@
-#
-# various kinds of MINFO RDATA stored in an input buffer
-# 
-# Valid compressed RDATA for "(rmailbox.example.com. emailbox.example.com.)"
-# RDLENGHT=32 bytes
-# 0  1
- 00 21
-# RMAILBOX: non compressed
-# 2  3  4  5  6  7  8  9 10  1  2  3  4  5  6  7  8  9 20  1  2  3(bytes)
-#(8) r  m  a  i  l  b  o  x (7) e  x  a  m  p  l  e (3) c  o  m  .
- 08 72 6d 61 69 6c 62 6f 78 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00
-# EMAILBOX: compressed
-# 4  5  6  7  8  9 30  1  2  3  4
-#(8) e  m  a  i  l  b  o  x ptr=11
- 08 65 6d 61 69 6c 62 6f 78 c0 0b
-#
-# Both RMAILBOX and EMAILBOX compressed
-# RDLENGHT=04 bytes
-# 5  6
- 00 04
-# 7  8  9 40(bytes)
-#ptr=02 ptr=24
- c0 02 c0 18
-#
-# rdlength too short
-# RDLENGHT=03 bytes
-# 1  2
- 00 03
-# 3  4  5  6(bytes)
-#ptr=02 ptr=24
- c0 02 c0 18
-#
-# rdlength too long
-# RDLENGHT=25 bytes
-# 7  8
- 00 19
-# 9 50  1  2(bytes)
-#ptr=02 ptr=24
- c0 02 c0 18
-#
-# incomplete RMAILBOX NAME
-# RDLENGHT=19 bytes
-# 3  4
- 00 13
-# 5  6  7  8  9 60  1  2  3  4  5  6  7  8  9 60  1  2  3 (bytes)
-#(8) r  m  a  i  l  b  o  x (7) e  x  a  m  p  l  e (3) c
- 08 72 6d 61 69 6c 62 6f 78 07 65 78 61 6d 70 6c 65 03 63

+ 3 - 0
src/lib/dns/tests/testdata/rdata_minfo_fromWire1.spec

@@ -0,0 +1,3 @@
+[custom]
+sections: minfo
+[minfo]

+ 7 - 0
src/lib/dns/tests/testdata/rdata_minfo_fromWire2.spec

@@ -0,0 +1,7 @@
+[custom]
+sections: name:minfo
+[name]
+name: a.example.com.
+[minfo]
+rmailbox: rmailbox.ptr=02
+emailbox: emailbox.ptr=02

+ 6 - 0
src/lib/dns/tests/testdata/rdata_minfo_fromWire3.spec

@@ -0,0 +1,6 @@
+[custom]
+sections: minfo
+# rdlength too short
+[minfo]
+emailbox: emailbox.ptr=11
+rdlen: 3

+ 6 - 0
src/lib/dns/tests/testdata/rdata_minfo_fromWire4.spec

@@ -0,0 +1,6 @@
+[custom]
+sections: minfo
+# rdlength too long
+[minfo]
+emailbox: emailbox.ptr=11
+rdlen: 80

+ 5 - 0
src/lib/dns/tests/testdata/rdata_minfo_fromWire5.spec

@@ -0,0 +1,5 @@
+[custom]
+sections: minfo
+# bogus rmailbox name
+[minfo]
+rmailbox: "01234567890123456789012345678901234567890123456789012345678901234"

+ 5 - 0
src/lib/dns/tests/testdata/rdata_minfo_fromWire6.spec

@@ -0,0 +1,5 @@
+[custom]
+sections: minfo
+# bogus emailbox name
+[minfo]
+emailbox: "01234567890123456789012345678901234567890123456789012345678901234"

+ 0 - 15
src/lib/dns/tests/testdata/rdata_minfo_toWire

@@ -1,15 +0,0 @@
-#
-# various kinds of MINFO RDATA stored in an input buffer
-# 
-# Valid compressed RDATA for "(rmailbox.example.com. emailbox.example.com.)"
-# RDLENGHT=32 bytes
-# 0  1
- 00 21
-# RMAILBOX: non compressed
-# 2  3  4  5  6  7  8  9 10  1  2  3  4  5  6  7  8  9 20  1  2  3(bytes)
-#(8) r  m  a  i  l  b  o  x (7) e  x  a  m  p  l  e (3) c  o  m  .
- 08 72 6d 61 69 6c 62 6f 78 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00
-# EMAILBOX: compressed
-# 4  5  6  7  8  9 30  1  2  3  4
-#(8) e  m  a  i  l  b  o  x ptr=11
- 08 65 6d 61 69 6c 62 6f 78 c0 0b

+ 5 - 0
src/lib/dns/tests/testdata/rdata_minfo_toWire1.spec

@@ -0,0 +1,5 @@
+[custom]
+sections: minfo
+[minfo]
+emailbox: emailbox.ptr=09
+rdlen: -1

+ 6 - 0
src/lib/dns/tests/testdata/rdata_minfo_toWire2.spec

@@ -0,0 +1,6 @@
+[custom]
+sections: minfo
+[minfo]
+rmailbox: root.example.com.
+emailbox: emailbox.ptr=05
+rdlen: -1

+ 0 - 8
src/lib/dns/tests/testdata/rdata_minfo_toWireUncompressed.wire

@@ -1,8 +0,0 @@
-###
-### This data file was auto-generated from rdata_minfo_toWireUncompressed.spec
-###
-
-# MINFO RDATA, RDLEN=44
-002c
-# RMAILBOX=rmailbox.example.com EMAILBOX=emailbox.example.com
-08726d61696c626f78076578616d706c6503636f6d00 08656d61696c626f78076578616d706c6503636f6d00

+ 1 - 0
src/lib/dns/tests/testdata/rdata_minfo_toWireUncompressed.spec

@@ -4,3 +4,4 @@
 [custom]
 sections: minfo
 [minfo]
+rdlen: -1

+ 8 - 0
src/lib/dns/tests/testdata/rdata_minfo_toWireUncompressed2.spec

@@ -0,0 +1,8 @@
+#
+# A simplest form of MINFO: all default parameters
+#
+[custom]
+sections: minfo
+[minfo]
+rmailbox: root.example.com.
+rdlen: -1