Browse Source

[1186] unittests implemented for LibDHCP.

Tomek Mrugalski 13 years ago
parent
commit
54ba29f03a
3 changed files with 83 additions and 2 deletions
  1. 10 0
      src/lib/dhcp/option.cc
  2. 4 0
      src/lib/dhcp/option.h
  3. 69 2
      src/lib/dhcp/tests/libdhcp_unittest.cc

+ 10 - 0
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() {
 
 }

+ 4 - 0
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.

+ 69 - 2
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
 }
 
 }