myserver-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myserver-commit] [2923] add more features to IP address range + tests


From: Alexandru IANCU
Subject: [myserver-commit] [2923] add more features to IP address range + tests
Date: Wed, 29 Oct 2008 21:27:41 +0000

Revision: 2923
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2923
Author:   andu
Date:     2008-10-29 21:27:40 +0000 (Wed, 29 Oct 2008)

Log Message:
-----------
add more features to IP address range + tests

Modified Paths:
--------------
    trunk/myserver/include/conf/vhost/ip.h
    trunk/myserver/src/conf/vhost/ip.cpp
    trunk/myserver/tests/test_ip.cpp

Modified: trunk/myserver/include/conf/vhost/ip.h
===================================================================
--- trunk/myserver/include/conf/vhost/ip.h      2008-10-29 16:21:20 UTC (rev 
2922)
+++ trunk/myserver/include/conf/vhost/ip.h      2008-10-29 21:27:40 UTC (rev 
2923)
@@ -27,14 +27,20 @@
  public:
   virtual bool InRange(const std::string &ip) = 0;
   virtual bool InRange(const IpRange *pRange) = 0;
+
+  static IpRange *RangeFactory(const std::string &ipRange);
 };
 
 class Ipv4Range : public IpRange
 {
 public:
+  Ipv4Range();
   Ipv4Range(const std::string &sRange);
   Ipv4Range(const std::string &sStartHost, const std::string &sEndHost);
 
+  bool SetRange(const std::string &sRange);
+  bool SetRange(const std::string &sStartHost, const std::string &sEndHost);
+
   virtual ~Ipv4Range();
   virtual bool InRange(const std::string &ip);
   virtual bool InRange(const IpRange *pRange);
@@ -44,8 +50,6 @@
 protected:
   bool Init();
   bool InRange(const unsigned char addr[4]);
-  bool SetRange(const std::string &sRange);
-  bool SetRange(const std::string &sStartHost, const std::string &sEndHost);
   unsigned char m_nStart[4], m_nEnd[4], m_nMask[4];
 };
 

Modified: trunk/myserver/src/conf/vhost/ip.cpp
===================================================================
--- trunk/myserver/src/conf/vhost/ip.cpp        2008-10-29 16:21:20 UTC (rev 
2922)
+++ trunk/myserver/src/conf/vhost/ip.cpp        2008-10-29 21:27:40 UTC (rev 
2923)
@@ -19,11 +19,36 @@
 #include <sstream>
 
 /*!
+ * comment here
+ */
+IpRange *IpRange::RangeFactory(const std::string &ipRange)
+{
+  Ipv4Range *pV4 = new Ipv4Range();
+  if ( pV4->SetRange(ipRange) )
+    return pV4;
+
+  /* when will be implemented
+  Ipv6Range *pV6 = new Ipv6Range();
+  if ( pV6->SetRange(ipRange) )
+    return pV6;
+  */
+
+  return NULL;
+}
+
+/*!
  * Ipv4Range c-tor
  */
+Ipv4Range::Ipv4Range()
+{
+  Init();
+}
+
+/*!
+ * Ipv4Range c-tor
+ */
 Ipv4Range::Ipv4Range(const std::string &sRange)
 {
-  Init();
   SetRange(sRange);
 }
 
@@ -32,12 +57,16 @@
  */
 bool Ipv4Range::SetRange(const std::string &sRange)
 {
+  if ( !Init() )
+    return false;
+  if ( sRange.empty() )
+    return true;//just init
   std::string::size_type nPos = sRange.find('-');
   if ( nPos != std::string::npos )// x.x.x.x-y.y.y.y form
     {
       std::string start(sRange.substr(0, nPos));
       std::string end(sRange.substr(nPos + 1));
-      SetRange(start, end);
+      return SetRange(start, end);
     }
   else// x.x.x.x/y form
     {
@@ -51,9 +80,14 @@
          nAddr[i] = nTemp;
          istream >> nSep;
        }
+
+      nTemp = 32;
       if ( nSep == '/' )
-       istream >> nTemp;
+         istream >> nTemp;
 
+      if ( !istream.eof() )
+       return false;
+
       for ( int i = 0, nByte = 0; i < nTemp && nByte < 4; i++ )
        {
          m_nMask[nByte] += (1 << (i+1)%8);
@@ -89,6 +123,8 @@
       m_nEnd[i] = nTemp;
       end >> nSep;
     }
+  if ( !start.eof() || !end.eof() )
+    return false;
 
   // get mask lenght(max common addr part)
   char bs = 0;

Modified: trunk/myserver/tests/test_ip.cpp
===================================================================
--- trunk/myserver/tests/test_ip.cpp    2008-10-29 16:21:20 UTC (rev 2922)
+++ trunk/myserver/tests/test_ip.cpp    2008-10-29 21:27:40 UTC (rev 2923)
@@ -26,6 +26,9 @@
   CPPUNIT_TEST_SUITE( TestIpRange );
   CPPUNIT_TEST( testRangeInclusion );
   CPPUNIT_TEST( testIpInRange );
+  CPPUNIT_TEST( testSingleIpRange );
+  CPPUNIT_TEST( testEmptyRange );
+  CPPUNIT_TEST( testRangeFactory );
   CPPUNIT_TEST_SUITE_END();
 
 public:
@@ -41,6 +44,24 @@
     Ipv4Range testRange("192.168.0.0/24");
     CPPUNIT_ASSERT( testRange.InRange("192.168.0.127") );
   }
+  void testSingleIpRange()
+  {
+    Ipv4Range singleIpRange("192.168.0.100");
+    CPPUNIT_ASSERT( singleIpRange.InRange(&singleIpRange) );
+  }
+  void testEmptyRange()
+  {
+    Ipv4Range emptyRange("");
+    CPPUNIT_ASSERT( emptyRange.InRange("10.0.0.0") );//anyy IP addr
+  }
+  void testRangeFactory()
+  {
+    IpRange *pRange = IpRange::RangeFactory("192.168.51.0/23");
+    CPPUNIT_ASSERT( pRange->InRange("192.168.51.100") );
+
+    delete pRange;
+    pRange = NULL;
+  }
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION( TestIpRange );






reply via email to

[Prev in Thread] Current Thread [Next in Thread]