BIND 10 trac1186, updated. 2befaae14e4a27fbd0f217c39692720bf0fde3c7 [1186] unittests implemented for LibDHCP.

BIND 10 source code commits bind10-changes at lists.isc.org
Mon Sep 5 18:46:18 UTC 2011


The branch, trac1186 has been updated
       via  2befaae14e4a27fbd0f217c39692720bf0fde3c7 (commit)
       via  c6639b477efa14625a31adc3b5824ff17a2a8feb (commit)
      from  9ed40007c0befb247ee1570b49aff02a6784e04e (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 2befaae14e4a27fbd0f217c39692720bf0fde3c7
Author: Tomek Mrugalski <tomasz at isc.org>
Date:   Mon Sep 5 20:46:02 2011 +0200

    [1186] unittests implemented for LibDHCP.

commit c6639b477efa14625a31adc3b5824ff17a2a8feb
Author: Tomek Mrugalski <tomasz at isc.org>
Date:   Mon Sep 5 20:45:39 2011 +0200

    [1186] IfaceMgr tests should now work on both Linux and BSD.

-----------------------------------------------------------------------

Summary of changes:
 src/bin/dhcp6/tests/dhcp6_srv_unittest.cc |    2 +-
 src/bin/dhcp6/tests/iface_mgr_unittest.cc |   43 +++++++++++++++++-
 src/lib/dhcp/option.cc                    |   10 ++++
 src/lib/dhcp/option.h                     |    4 ++
 src/lib/dhcp/tests/libdhcp_unittest.cc    |   71 ++++++++++++++++++++++++++++-
 5 files changed, 126 insertions(+), 4 deletions(-)

-----------------------------------------------------------------------
diff --git a/src/bin/dhcp6/tests/dhcp6_srv_unittest.cc b/src/bin/dhcp6/tests/dhcp6_srv_unittest.cc
index a7719cc..842ad31 100644
--- a/src/bin/dhcp6/tests/dhcp6_srv_unittest.cc
+++ b/src/bin/dhcp6/tests/dhcp6_srv_unittest.cc
@@ -37,7 +37,7 @@ public:
     }
 };
 
-Test_F(Dhcpv6SrvTest, basic) {
+TEST_F(Dhcpv6SrvTest, basic) {
     // there's almost no code now. What's there provides echo capability
     // that is just a proof of concept and will be removed soon
     // No need to thoroughly test it
diff --git a/src/bin/dhcp6/tests/iface_mgr_unittest.cc b/src/bin/dhcp6/tests/iface_mgr_unittest.cc
index c9d4256..07ce0c6 100644
--- a/src/bin/dhcp6/tests/iface_mgr_unittest.cc
+++ b/src/bin/dhcp6/tests/iface_mgr_unittest.cc
@@ -28,7 +28,8 @@ using namespace std;
 using namespace isc;
 using namespace isc::asiolink;
 
-#define LOOPBACK "lo0"
+// name of loopback interface detection
+char LOOPBACK[32] = "lo";
 
 namespace {
 class NakedIfaceMgr: public IfaceMgr {
@@ -54,6 +55,46 @@ public:
     }
 };
 
+// We need some known interface to work reliably. Loopback interface
+// is named lo on Linux and lo0 on BSD boxes. We need to find out
+// which is available. This is not a real test, but rather a workaround
+// that will go away when interface detection is implemented.
+TEST_F(IfaceMgrTest, loDetect) {
+
+    unlink("interfaces.txt");
+
+    ofstream interfaces("interfaces.txt", ios::ate);
+    interfaces << "lo ::1";
+    interfaces.close();
+
+    NakedIfaceMgr * ifacemgr = new NakedIfaceMgr();
+    IOAddress loAddr("::1");
+
+    // bind multicast socket to port 10547
+    int socket1 = ifacemgr->openSocket("lo", loAddr, 10547);
+
+    // poor man's interface dection
+    // it will go away as soon as proper interface detection
+    // is implemented
+    if (socket1>0) {
+        cout << "This is Linux, using lo as loopback." << endl;
+        close(socket1);
+    } else {
+        socket1 = ifacemgr->openSocket("lo0", loAddr, 10547);
+        if (socket1>0) {
+            sprintf(LOOPBACK, "lo0");
+            cout << "This is BSD, using lo0 as loopback." << endl;
+            close(socket1);
+        } else {
+            cout << "Failed to detect loopback interface. Neither "
+                 << "lo or lo0 worked. I give up." << endl;
+            ASSERT_TRUE(false);
+        }
+    }
+
+    delete ifacemgr;
+}
+
 // uncomment this test to create packet writer. It will
 // write incoming DHCPv6 packets as C arrays. That is useful
 // for generating test sequences based on actual traffic
diff --git a/src/lib/dhcp/option.cc b/src/lib/dhcp/option.cc
index fdaf258..fedc557 100644
--- a/src/lib/dhcp/option.cc
+++ b/src/lib/dhcp/option.cc
@@ -187,6 +187,16 @@ Option::getType() {
     return type_;
 }
 
+char*
+Option::getData() {
+    if (data_len_) {
+        return (&data_[offset_]);
+    } else {
+        return (NULL);
+    }
+}
+
+
 Option::~Option() {
 
 }
diff --git a/src/lib/dhcp/option.h b/src/lib/dhcp/option.h
index 1cad655..ceeb03a 100644
--- a/src/lib/dhcp/option.h
+++ b/src/lib/dhcp/option.h
@@ -105,6 +105,10 @@ public:
     virtual bool
     valid();
 
+    // returns pointer to actual data
+    virtual char*
+    getData();
+
     /// Adds a sub-option.
     ///
     /// @param opt shared pointer to a suboption that is going to be added.
diff --git a/src/lib/dhcp/tests/libdhcp_unittest.cc b/src/lib/dhcp/tests/libdhcp_unittest.cc
index 0354f64..20a1848 100644
--- a/src/lib/dhcp/tests/libdhcp_unittest.cc
+++ b/src/lib/dhcp/tests/libdhcp_unittest.cc
@@ -69,8 +69,7 @@ TEST_F(LibDhcpTest, packOptions6) {
     opts.insert(pair<int, boost::shared_ptr<Option> >(opt1->getType(), opt5));
 
     unsigned int offset;
-    EXPECT_NO_THROW (
-    {
+    EXPECT_NO_THROW ({
          offset = LibDHCP::packOptions6(buf, 512, 100, opts);
     });
     EXPECT_EQ(135, offset); // options should take 35 bytes
@@ -79,6 +78,74 @@ TEST_F(LibDhcpTest, packOptions6) {
 
 TEST_F(LibDhcpTest, unpackOptions6) {
 
+    // just couple of random options
+    char packed[] = {
+        0, 12, 0, 5, 100, 101, 102, 103, 104, // opt1 (9 bytes)
+        0, 13, 0, 3, 105, 106, 107, // opt2 (7 bytes)
+        0, 14, 0, 2, 108, 109, // opt3 (6 bytes)
+        1,  0, 0, 4, 110, 111, 112, 113, // opt4 (8 bytes)
+        1,  1, 0, 1, 114 // opt5 (5 bytes)
+    };
+    // Option is used as a simple option implementation
+    // More advanced classes are used in tests dedicated for
+    // specific options.
+
+    isc::dhcp::Option::Option6Lst options; // list of options
+
+    // we can't use packed directly, as shared_array would try to
+    // free it eventually
+    boost::shared_array<char> buf(new char[512]);
+    memcpy(&buf[0], packed, 35);
+
+    unsigned int offset;
+    EXPECT_NO_THROW ({
+        offset = LibDHCP::unpackOptions6(buf, 512, 0, 35, options);
+    });
+
+    EXPECT_EQ(35, offset); // parsed first 35 bytes (offset 0..34)
+    EXPECT_EQ(options.size(), 5); // there should be 5 options
+
+    isc::dhcp::Option::Option6Lst::const_iterator x = options.find(12);
+    ASSERT_NE(x, options.end()); // option 1 should exist
+    EXPECT_EQ(12, x->second->getType());  // this should be option 12
+    ASSERT_EQ(9, x->second->len()); // it should be of length 9
+    EXPECT_EQ(0, memcmp(x->second->getData(), packed+4, 5)); // data len = 5
+
+    x = options.find(13);
+    ASSERT_NE(x, options.end()); // option 13 should exist
+    EXPECT_EQ(13, x->second->getType());  // this should be option 13
+    ASSERT_EQ(7, x->second->len()); // it should be of length 7
+    EXPECT_EQ(0, memcmp(x->second->getData(), packed+13, 3)); // data length = 3
+
+    x = options.find(14);
+    ASSERT_NE(x, options.end()); // option 3 should exist
+    EXPECT_EQ(14, x->second->getType());  // this should be option 14
+    ASSERT_EQ(6, x->second->len()); // it should be of length 6
+    EXPECT_EQ(0, memcmp(x->second->getData(), packed+20, 2)); // data length = 2
+
+    x = options.find(256);
+    ASSERT_NE(x, options.end()); // option 256 should exist
+    EXPECT_EQ(256, x->second->getType());  // this should be option 256
+    ASSERT_EQ(8, x->second->len()); // it should be of length 7
+    EXPECT_EQ(0, memcmp(x->second->getData(), packed+26, 4)); // data length = 4
+
+    x = options.find(257);
+    ASSERT_NE(x, options.end()); // option 257 should exist
+    EXPECT_EQ(257, x->second->getType());  // this should be option 257
+    ASSERT_EQ(5, x->second->len()); // it should be of length 5
+    EXPECT_EQ(0, memcmp(x->second->getData(), packed+34, 1)); // data length = 2
+
+    x = options.find(0);
+    EXPECT_EQ(x, options.end()); // option 0 not found
+
+    x = options.find(1); // 1 is htons(256). Worth checking
+    EXPECT_EQ(x, options.end()); // option 1 not found
+
+    x = options.find(2);
+    EXPECT_EQ(x, options.end()); // option 2 not found
+
+    x = options.find(32000);
+    EXPECT_EQ(x, options.end()); // option 32000 not found
 }
 
 }




More information about the bind10-changes mailing list