Browse Source

[2414] toText() and get() implemented in Subnet

Tomek Mrugalski 12 years ago
parent
commit
23d0855687
3 changed files with 39 additions and 0 deletions
  1. 7 0
      src/lib/dhcp/subnet.cc
  2. 6 0
      src/lib/dhcp/subnet.h
  3. 26 0
      src/lib/dhcp/tests/subnet_unittest.cc

+ 7 - 0
src/lib/dhcp/subnet.cc

@@ -15,6 +15,7 @@
 #include <dhcp/addr_utilities.h>
 #include <asiolink/io_address.h>
 #include <dhcp/subnet.h>
+#include <sstream>
 
 using namespace isc::asiolink;
 
@@ -52,6 +53,12 @@ Subnet::delOptions() {
     options_.clear();
 }
 
+std::string Subnet::toText() {
+    std::stringstream tmp;
+    tmp << prefix_.toText() << "/" << static_cast<unsigned int>(prefix_len_);
+    return (tmp.str());
+}
+
 Subnet4::Subnet4(const isc::asiolink::IOAddress& prefix, uint8_t length,
                  const Triplet<uint32_t>& t1,
                  const Triplet<uint32_t>& t2,

+ 6 - 0
src/lib/dhcp/subnet.h

@@ -279,6 +279,12 @@ public:
     /// @return unique ID for that subnet
     SubnetID getID() const { return (id_); }
 
+    std::pair<isc::asiolink::IOAddress, uint8_t> get() {
+        return (std::pair<isc::asiolink::IOAddress, uint8_t>(prefix_, prefix_len_));
+    }
+
+    virtual std::string toText();
+
 protected:
     /// @brief protected constructor
     //

+ 26 - 0
src/lib/dhcp/tests/subnet_unittest.cc

@@ -158,6 +158,19 @@ TEST(Subnet4Test, inRangeinPool) {
     EXPECT_FALSE(subnet->inPool(IOAddress("192.3.0.0")));
 }
 
+// This test checks if the toText() method returns text representation
+TEST(Subnet4Test, toText) {
+    Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 24, 1, 2, 3));
+    EXPECT_EQ("192.0.2.0/24", subnet->toText());
+}
+
+// This test checks if the get() method returns proper parameters
+TEST(Subnet4Test, get) {
+    Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 28, 1, 2, 3));
+    EXPECT_EQ("192.0.2.0", subnet->get().first.toText());
+    EXPECT_EQ(28, subnet->get().second);
+}
+
 // Tests for Subnet6
 
 TEST(Subnet6Test, constructor) {
@@ -418,4 +431,17 @@ TEST(Subnet6Test, inRangeinPool) {
     EXPECT_FALSE(subnet->inPool(IOAddress("2001:db8::21")));
 }
 
+// This test checks if the toText() method returns text representation
+TEST(Subnet6Test, toText) {
+    Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8::"), 32, 1, 2, 3, 4));
+    EXPECT_EQ("2001:db8::/32", subnet->toText());
+}
+
+// This test checks if the get() method returns proper parameters
+TEST(Subnet6Test, get) {
+    Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8::"), 32, 1, 2, 3, 4));
+    EXPECT_EQ("2001:db8::", subnet->get().first.toText());
+    EXPECT_EQ(32, subnet->get().second);
+}
+
 };