Browse Source

[2157] add parameter checks for setRequest(IPVersion|TransportProtocol)

Yoshitaka Aharen 12 years ago
parent
commit
fb2a1a9621

+ 0 - 6
src/bin/auth/statistics.cc.pre

@@ -26,12 +26,6 @@
 
 #include <boost/optional.hpp>
 
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netdb.h>
-
 using namespace isc::dns;
 using namespace isc::auth;
 using namespace isc::statistics;

+ 14 - 0
src/bin/auth/statistics.h

@@ -29,6 +29,12 @@
 
 #include <stdint.h>
 
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+
 namespace isc {
 namespace auth {
 namespace statistics {
@@ -104,6 +110,9 @@ public:
     /// \param ip_version AF_INET or AF_INET6
     /// \throw None
     void setRequestIPVersion(const int ip_version) {
+        if (ip_version != AF_INET && ip_version != AF_INET6) {
+            isc_throw(isc::InvalidParameter, "Unknown address family");
+        }
         req_ip_version_ = ip_version;
     }
 
@@ -121,6 +130,11 @@ public:
     /// \param transport_protocol IPPROTO_UDP or IPPROTO_TCP
     /// \throw None
     void setRequestTransportProtocol(const int transport_protocol) {
+        if (transport_protocol != IPPROTO_UDP &&
+            transport_protocol != IPPROTO_TCP)
+        {
+            isc_throw(isc::InvalidParameter, "Unknown transport protocol");
+        }
         req_transport_protocol_ = transport_protocol;
     }
 

+ 25 - 0
src/bin/auth/tests/statistics_unittest.cc.pre

@@ -63,6 +63,31 @@ buildSkeletonMessage(MessageAttributes& msgattrs) {
     msgattrs.setRequestDO(true);
 }
 
+TEST_F(CountersTest, invalidParameterForSetRequestIPVersion) {
+    MessageAttributes msgattrs;
+
+    // It should not throw if the parameter is AF_INET or AF_INET6.
+    EXPECT_NO_THROW(msgattrs.setRequestIPVersion(AF_INET));
+    EXPECT_NO_THROW(msgattrs.setRequestIPVersion(AF_INET6));
+
+    // It should throw isc::InvalidParameter if the parameter is not AF_INET
+    // nor AF_INET6.
+    EXPECT_THROW(msgattrs.setRequestIPVersion(AF_UNIX), isc::InvalidParameter);
+}
+
+TEST_F(CountersTest, invalidParameterForSetRequestTransportProtocol) {
+    MessageAttributes msgattrs;
+
+    // It should not throw if the parameter is IPPROTO_UDP or IPPROTO_TCP.
+    EXPECT_NO_THROW(msgattrs.setRequestTransportProtocol(IPPROTO_UDP));
+    EXPECT_NO_THROW(msgattrs.setRequestTransportProtocol(IPPROTO_TCP));
+
+    // It should throw isc::InvalidParameter if the parameter is not
+    // IPPROTO_UDP nor IPPROTO_TCP.
+    EXPECT_THROW(msgattrs.setRequestTransportProtocol(IPPROTO_IP),
+                 isc::InvalidParameter);
+}
+
 TEST_F(CountersTest, invalidOperationForGetRequestOpCode) {
     MessageAttributes msgattrs;