Browse Source

[3145] Added negative test case for IAADDR option

Tomek Mrugalski 11 years ago
parent
commit
8f3591204b

+ 3 - 0
src/lib/dhcp/option6_iaaddr.cc

@@ -35,6 +35,9 @@ Option6IAAddr::Option6IAAddr(uint16_t type, const isc::asiolink::IOAddress& addr
                              uint32_t pref, uint32_t valid)
     :Option(V6, type), addr_(addr), preferred_(pref),
      valid_(valid) {
+    if (!addr.isV6()) {
+        isc_throw(isc::BadValue, addr_.toText() << " is not an IPv6 address");
+    }
 }
 
 Option6IAAddr::Option6IAAddr(uint32_t type, OptionBuffer::const_iterator begin,

+ 6 - 2
src/lib/dhcp/option6_iaaddr.h

@@ -33,7 +33,9 @@ public:
     /// length of the fixed part of the IAADDR option
     static const size_t OPTION6_IAADDR_LEN = 24;
 
-    /// @brief Ctor, used for options constructed (during transmission).
+    /// @brief Constructor, used for options constructed (during transmission).
+    ///
+    /// @throw BadValue if specified addr is not IPv6
     ///
     /// @param type option type
     /// @param addr reference to an address
@@ -42,7 +44,9 @@ public:
     Option6IAAddr(uint16_t type, const isc::asiolink::IOAddress& addr,
                   uint32_t preferred, uint32_t valid);
 
-    /// @brief ctor, used for received options.
+    /// @brief Constructor, used for received options.
+    ///
+    /// @throw OutOfRange if specified option is truncated
     ///
     /// @param type option type
     /// @param begin iterator to first byte of option data

+ 15 - 0
src/lib/dhcp/tests/option6_iaaddr_unittest.cc

@@ -106,4 +106,19 @@ TEST_F(Option6IAAddrTest, basic) {
     EXPECT_NO_THROW(opt.reset());
 }
 
+/// @todo: Write test for (type, addr, pref, valid) constructor
+/// See option6_iaprefix_unittest.cc for similar test
+
+// Tests if broken usage causes exception to be thrown
+TEST_F(Option6IAAddrTest, negative) {
+
+    // Too short. Minimum length is 24
+    EXPECT_THROW(Option6IAAddr(D6O_IAADDR, buf_.begin(), buf_.begin() + 23),
+                 OutOfRange);
+
+    // This option is for IPv6 addresses only
+    EXPECT_THROW(Option6IAAddr(D6O_IAADDR, isc::asiolink::IOAddress("192.0.2.1"),
+                               1000, 2000), BadValue);
+}
+
 }