Browse Source

[1955] Added setData() unit test for dhcp::Option.

Marcin Siodelski 13 years ago
parent
commit
8cecc1ec80
2 changed files with 30 additions and 7 deletions
  1. 2 6
      src/lib/dhcp/option.cc
  2. 28 1
      src/lib/dhcp/tests/option_unittest.cc

+ 2 - 6
src/lib/dhcp/option.cc

@@ -273,12 +273,8 @@ void Option::setUint32(uint32_t value) {
 void Option::setData(const OptionBufferConstIter first,
 void Option::setData(const OptionBufferConstIter first,
                      const OptionBufferConstIter last) {
                      const OptionBufferConstIter last) {
     // We will copy entire option buffer, so we have to resize data_.
     // We will copy entire option buffer, so we have to resize data_.
-    data_.resize(last - first);
-    OptionBufferConstIter x = first;
-    while (x != last) {
-        data_[x - first] = *x;
-        ++x;
-    }
+    data_.resize(std::distance(first, last));
+    std::copy(first, last, data_.begin());
 }
 }
 
 
 
 

+ 28 - 1
src/lib/dhcp/tests/option_unittest.cc

@@ -485,7 +485,7 @@ TEST_F(OptionTest, setUintX) {
     uint8_t exp2[] = {125, 2, 12345/256, 12345%256};
     uint8_t exp2[] = {125, 2, 12345/256, 12345%256};
     EXPECT_TRUE(0 == memcmp(exp2, outBuf_.getData(), 4));
     EXPECT_TRUE(0 == memcmp(exp2, outBuf_.getData(), 4));
 
 
-    // verity getUint32
+    // verify getUint32
     outBuf_.clear();
     outBuf_.clear();
     opt4->setUint32(0x12345678);
     opt4->setUint32(0x12345678);
     opt4->pack4(outBuf_);
     opt4->pack4(outBuf_);
@@ -495,4 +495,31 @@ TEST_F(OptionTest, setUintX) {
     uint8_t exp4[] = {125, 4, 0x12, 0x34, 0x56, 0x78};
     uint8_t exp4[] = {125, 4, 0x12, 0x34, 0x56, 0x78};
     EXPECT_TRUE(0 == memcmp(exp4, outBuf_.getData(), 6));
     EXPECT_TRUE(0 == memcmp(exp4, outBuf_.getData(), 6));
 }
 }
+
+TEST_F(OptionTest, setData) {
+    // verify data override with new buffer larger than
+    // initial option buffer size
+    OptionPtr opt1(new Option(Option::V4, 125,
+                              buf_.begin(), buf_.begin() + 10));
+    buf_.resize(20, 1);
+    opt1->setData(buf_.begin(), buf_.end());
+    opt1->pack4(outBuf_);
+    ASSERT_EQ(outBuf_.getLength() - opt1->getHeaderLen(), buf_.size());
+    const uint8_t* test_data = static_cast<const uint8_t*>(outBuf_.getData());
+    EXPECT_TRUE(0 == memcmp(&buf_[0], test_data + opt1->getHeaderLen(),
+                            buf_.size()));
+
+    // verify data override with new buffer shorter than
+    // initial option buffer size
+    OptionPtr opt2(new Option(Option::V4, 125,
+                              buf_.begin(), buf_.begin() + 10));
+    outBuf_.clear();
+    buf_.resize(5, 1);
+    opt2->setData(buf_.begin(), buf_.end());
+    opt2->pack4(outBuf_);
+    ASSERT_EQ(outBuf_.getLength() - opt1->getHeaderLen(), buf_.size());
+    test_data = static_cast<const uint8_t*>(outBuf_.getData());
+    EXPECT_TRUE(0 == memcmp(&buf_[0], test_data + opt1->getHeaderLen(),
+                            buf_.size()));
+}
 }
 }