Browse Source

[2786] Use OptionString class to represent options with a string.

Marcin Siodelski 12 years ago
parent
commit
18a4180734

+ 4 - 0
src/lib/dhcp/option_definition.cc

@@ -22,6 +22,7 @@
 #include <dhcp/option_int.h>
 #include <dhcp/option_int_array.h>
 #include <dhcp/option_space.h>
+#include <dhcp/option_string.h>
 #include <util/encode/hex.h>
 #include <util/strutil.h>
 #include <boost/algorithm/string/classification.hpp>
@@ -160,6 +161,9 @@ OptionDefinition::optionFactory(Option::Universe u, uint16_t type,
             }
             break;
 
+        case OPT_STRING_TYPE:
+            return (OptionPtr(new OptionString(u, type, begin, end)));
+
         default:
             if (u == Option::V6) {
                 if ((code_ == D6O_IA_NA || code_ == D6O_IA_PD) &&

+ 8 - 0
src/lib/dhcp/option_string.h

@@ -18,6 +18,7 @@
 #include <dhcp/option.h>
 #include <util/buffer.h>
 
+#include <boost/shared_ptr.hpp>
 #include <string>
 
 namespace isc {
@@ -103,8 +104,15 @@ private:
     /// String value being held by the option.
     std::string value_;
 
+    // Change scope of the getData function to private as we want
+    // getValue is called instead.
+    using Option::getData;
+
 };
 
+/// Pointer to the OptionString object.
+typedef boost::shared_ptr<OptionString> OptionStringPtr;
+
 } // namespace isc::dhcp
 } // namespace isc
 

+ 26 - 20
src/lib/dhcp/tests/libdhcp++_unittest.cc

@@ -24,6 +24,7 @@
 #include <dhcp/option_custom.h>
 #include <dhcp/option_int.h>
 #include <dhcp/option_int_array.h>
+#include <dhcp/option_string.h>
 #include <util/buffer.h>
 
 #include <gtest/gtest.h>
@@ -423,10 +424,13 @@ TEST_F(LibDhcpTest, unpackOptions4) {
 
     isc::dhcp::Option::OptionCollection::const_iterator x = options.find(12);
     ASSERT_FALSE(x == options.end()); // option 1 should exist
-    EXPECT_EQ(12, x->second->getType());  // this should be option 12
-    ASSERT_EQ(3, x->second->getData().size()); // it should be of length 3
-    EXPECT_EQ(5, x->second->len()); // total option length 5
-    EXPECT_EQ(0, memcmp(&x->second->getData()[0], v4Opts+2, 3)); // data len=3
+    // Option 12 holds a string so let's cast it to an appropriate type.
+    OptionStringPtr option12 = boost::static_pointer_cast<OptionString>(x->second);
+    ASSERT_TRUE(option12);
+    EXPECT_EQ(12, option12->getType());  // this should be option 12
+    ASSERT_EQ(3, option12->getValue().length()); // it should be of length 3
+    EXPECT_EQ(5, option12->len()); // total option length 5
+    EXPECT_EQ(0, memcmp(&option12->getValue()[0], v4Opts+2, 3)); // data len=3
 
     x = options.find(60);
     ASSERT_FALSE(x == options.end()); // option 2 should exist
@@ -437,10 +441,12 @@ TEST_F(LibDhcpTest, unpackOptions4) {
 
     x = options.find(14);
     ASSERT_FALSE(x == options.end()); // option 3 should exist
-    EXPECT_EQ(14, x->second->getType());  // this should be option 14
-    ASSERT_EQ(3, x->second->getData().size()); // it should be of length 3
-    EXPECT_EQ(5, x->second->len()); // total option length 5
-    EXPECT_EQ(0, memcmp(&x->second->getData()[0], v4Opts+12, 3)); // data len=3
+    OptionStringPtr option14 = boost::static_pointer_cast<OptionString>(x->second);
+    ASSERT_TRUE(option14);
+    EXPECT_EQ(14, option14->getType());  // this should be option 14
+    ASSERT_EQ(3, option14->getValue().length()); // it should be of length 3
+    EXPECT_EQ(5, option14->len()); // total option length 5
+    EXPECT_EQ(0, memcmp(&option14->getValue()[0], v4Opts+12, 3)); // data len=3
 
     x = options.find(254);
     ASSERT_FALSE(x == options.end()); // option 3 should exist
@@ -532,7 +538,7 @@ TEST_F(LibDhcpTest, stdOptionDefs4) {
     // It will be used to create most of the options.
     std::vector<uint8_t> buf(48, 1);
     OptionBufferConstIter begin = buf.begin();
-    OptionBufferConstIter end = buf.begin();
+    OptionBufferConstIter end = buf.end();
 
     LibDhcpTest::testStdOptionDefs4(DHO_SUBNET_MASK, begin, end,
                                     typeid(OptionCustom));
@@ -568,25 +574,25 @@ TEST_F(LibDhcpTest, stdOptionDefs4) {
                                     typeid(Option4AddrLst));
 
     LibDhcpTest::testStdOptionDefs4(DHO_HOST_NAME, begin, end,
-                                    typeid(OptionCustom));
+                                    typeid(OptionString));
 
     LibDhcpTest::testStdOptionDefs4(DHO_BOOT_SIZE, begin, begin + 2,
                                     typeid(OptionInt<uint16_t>));
 
     LibDhcpTest::testStdOptionDefs4(DHO_MERIT_DUMP, begin, end,
-                                    typeid(OptionCustom));
+                                    typeid(OptionString));
 
     LibDhcpTest::testStdOptionDefs4(DHO_DOMAIN_NAME, begin, end,
-                                    typeid(OptionCustom));
+                                    typeid(OptionString));
 
     LibDhcpTest::testStdOptionDefs4(DHO_SWAP_SERVER, begin, end,
                                     typeid(OptionCustom));
 
     LibDhcpTest::testStdOptionDefs4(DHO_ROOT_PATH, begin, end,
-                                    typeid(OptionCustom));
+                                    typeid(OptionString));
 
     LibDhcpTest::testStdOptionDefs4(DHO_EXTENSIONS_PATH, begin, end,
-                                    typeid(OptionCustom));
+                                    typeid(OptionString));
 
     LibDhcpTest::testStdOptionDefs4(DHO_IP_FORWARDING, begin, end,
                                     typeid(OptionCustom));
@@ -652,7 +658,7 @@ TEST_F(LibDhcpTest, stdOptionDefs4) {
                                     typeid(OptionCustom));
 
     LibDhcpTest::testStdOptionDefs4(DHO_NIS_DOMAIN, begin, end,
-                                    typeid(OptionCustom));
+                                    typeid(OptionString));
 
     LibDhcpTest::testStdOptionDefs4(DHO_NIS_SERVERS, begin, end,
                                     typeid(Option4AddrLst));
@@ -674,7 +680,7 @@ TEST_F(LibDhcpTest, stdOptionDefs4) {
                                     typeid(OptionInt<uint8_t>));
 
     LibDhcpTest::testStdOptionDefs4(DHO_NETBIOS_SCOPE, begin, end,
-                                    typeid(OptionCustom));
+                                    typeid(OptionString));
 
     LibDhcpTest::testStdOptionDefs4(DHO_FONT_SERVERS, begin, end,
                                     typeid(Option4AddrLst));
@@ -701,7 +707,7 @@ TEST_F(LibDhcpTest, stdOptionDefs4) {
                                     typeid(Option));
 
     LibDhcpTest::testStdOptionDefs4(DHO_DHCP_MESSAGE, begin, end,
-                                    typeid(OptionCustom));
+                                    typeid(OptionString));
 
     LibDhcpTest::testStdOptionDefs4(DHO_DHCP_MAX_MESSAGE_SIZE, begin, begin + 2,
                                     typeid(OptionInt<uint16_t>));
@@ -719,7 +725,7 @@ TEST_F(LibDhcpTest, stdOptionDefs4) {
                                     typeid(Option));
 
     LibDhcpTest::testStdOptionDefs4(DHO_NWIP_DOMAIN_NAME, begin, end,
-                                    typeid(OptionCustom));
+                                    typeid(OptionString));
 
     LibDhcpTest::testStdOptionDefs4(DHO_NWIP_SUBOPTIONS, begin, end,
                                     typeid(Option));
@@ -908,10 +914,10 @@ TEST_F(LibDhcpTest, stdOptionDefs6) {
                                     typeid(Option6AddrLst));
 
     LibDhcpTest::testStdOptionDefs6(D6O_NEW_POSIX_TIMEZONE, begin, end,
-                                    typeid(OptionCustom));
+                                    typeid(OptionString));
 
     LibDhcpTest::testStdOptionDefs6(D6O_NEW_TZDB_TIMEZONE, begin, end,
-                                    typeid(OptionCustom));
+                                    typeid(OptionString));
 
     LibDhcpTest::testStdOptionDefs6(D6O_ERO, begin, end,
                                     typeid(OptionIntArray<uint16_t>));

+ 5 - 5
src/lib/dhcp/tests/option_definition_unittest.cc

@@ -25,6 +25,7 @@
 #include <dhcp/option_definition.h>
 #include <dhcp/option_int.h>
 #include <dhcp/option_int_array.h>
+#include <dhcp/option_string.h>
 #include <exceptions/exceptions.h>
 
 #include <boost/pointer_cast.hpp>
@@ -936,11 +937,10 @@ TEST_F(OptionDefinitionTest, utf8StringTokenized) {
         option_v6 = opt_def.optionFactory(Option::V6, opt_code, values);
     );
     ASSERT_TRUE(option_v6);
-    ASSERT_TRUE(typeid(*option_v6) == typeid(OptionCustom));
-    std::vector<uint8_t> data = option_v6->getData();
-    std::vector<uint8_t> ref_data(values[0].c_str(), values[0].c_str()
-                                  + values[0].length());
-    EXPECT_TRUE(std::equal(ref_data.begin(), ref_data.end(), data.begin()));
+    ASSERT_TRUE(typeid(*option_v6) == typeid(OptionString));
+    OptionStringPtr option_v6_string =
+        boost::static_pointer_cast<OptionString>(option_v6);
+    EXPECT_TRUE(values[0] == option_v6_string->getValue());
 }
 
 // The purpose of this test is to check that non-integer data type can't

+ 17 - 8
src/lib/dhcp/tests/pkt4_unittest.cc

@@ -16,6 +16,7 @@
 
 #include <asiolink/io_address.h>
 #include <dhcp/dhcp4.h>
+#include <dhcp/option_string.h>
 #include <dhcp/pkt4.h>
 #include <exceptions/exceptions.h>
 #include <util/buffer.h>
@@ -549,17 +550,25 @@ TEST(Pkt4Test, unpackOptions) {
 
     boost::shared_ptr<Option> x = pkt->getOption(12);
     ASSERT_TRUE(x); // option 1 should exist
-    EXPECT_EQ(12, x->getType());  // this should be option 12
-    ASSERT_EQ(3, x->getData().size()); // it should be of length 3
-    EXPECT_EQ(5, x->len()); // total option length 5
-    EXPECT_EQ(0, memcmp(&x->getData()[0], v4Opts + 2, 3)); // data len=3
+    // Option 12 is represented by the OptionString class so let's do
+    // the appropriate conversion.
+    OptionStringPtr option12 = boost::static_pointer_cast<OptionString>(x);
+    ASSERT_TRUE(option12);
+    EXPECT_EQ(12, option12->getType());  // this should be option 12
+    ASSERT_EQ(3, option12->getValue().length()); // it should be of length 3
+    EXPECT_EQ(5, option12->len()); // total option length 5
+    EXPECT_EQ(0, memcmp(&option12->getValue()[0], v4Opts + 2, 3)); // data len=3
 
     x = pkt->getOption(14);
     ASSERT_TRUE(x); // option 13 should exist
-    EXPECT_EQ(14, x->getType());  // this should be option 13
-    ASSERT_EQ(3, x->getData().size()); // it should be of length 3
-    EXPECT_EQ(5, x->len()); // total option length 5
-    EXPECT_EQ(0, memcmp(&x->getData()[0], v4Opts + 7, 3)); // data len=3
+    // Option 14 is represented by the OptionString class so let's do
+    // the appropriate conversion.
+    OptionStringPtr option14 = boost::static_pointer_cast<OptionString>(x);
+    ASSERT_TRUE(option14);
+    EXPECT_EQ(14, option14->getType());  // this should be option 13
+    ASSERT_EQ(3, option14->getValue().length()); // it should be of length 3
+    EXPECT_EQ(5, option14->len()); // total option length 5
+    EXPECT_EQ(0, memcmp(&option14->getValue()[0], v4Opts + 7, 3)); // data len=3
 
     x = pkt->getOption(60);
     ASSERT_TRUE(x); // option 60 should exist