Parcourir la source

[2157] throw an exception instead of assert for invalid parameter

Yoshitaka Aharen il y a 12 ans
Parent
commit
76759ae07d
2 fichiers modifiés avec 14 ajouts et 11 suppressions
  1. 8 4
      src/bin/auth/statistics.h
  2. 6 7
      src/bin/auth/tests/statistics_unittest.cc.pre

+ 8 - 4
src/bin/auth/statistics.h

@@ -103,9 +103,11 @@ public:
 
     /// \brief Set IP version carrying the request.
     /// \param ip_version IP version carrying the request
-    /// \throw None
+    /// \throw isc::InvalidParameter ip_version is invalid
     void setRequestIPVersion(const IPVersionType ip_version) {
-        assert(ip_version != IP_VERSION_UNSPEC);
+        if (ip_version == IP_VERSION_UNSPEC) {
+            isc_throw(isc::InvalidParameter, "Invalid IP version");
+        }
         req_ip_version_ = ip_version;
     }
 
@@ -118,11 +120,13 @@ public:
 
     /// \brief Set transport protocol carrying the request.
     /// \param transport_protocol Transport protocol carrying the request
-    /// \throw None
+    /// \throw isc::InvalidParameter transport_protocol is invalid
     void setRequestTransportProtocol(
         const TransportProtocolType transport_protocol)
     {
-        assert(transport_protocol != TRANSPORT_UNSPEC);
+        if (transport_protocol == TRANSPORT_UNSPEC) {
+            isc_throw(isc::InvalidParameter, "Invalid transport protocol");
+        }
         req_transport_protocol_ = transport_protocol;
     }
 

+ 6 - 7
src/bin/auth/tests/statistics_unittest.cc.pre

@@ -112,16 +112,15 @@ buildSkeletonMessage(MessageAttributes& msgattrs) {
 TEST_F(CountersTest, invalidParameter) {
     MessageAttributes msgattrs;
 
-    // Passing *_UNSPEC should trigger an assertion failure.
-    // Note that we just check that it dies - we don't check what message is
-    // output.
-    EXPECT_DEATH_IF_SUPPORTED(
+    // Passing *_UNSPEC should trigger throwing an exception
+    // isc::InvalidParameter.
+    EXPECT_THROW(
         msgattrs.setRequestIPVersion(MessageAttributes::IP_VERSION_UNSPEC),
-        ".*");
-    EXPECT_DEATH_IF_SUPPORTED(
+        isc::InvalidParameter);
+    EXPECT_THROW(
         msgattrs.setRequestTransportProtocol(
             MessageAttributes::TRANSPORT_UNSPEC),
-        ".*");
+        isc::InvalidParameter);
 }
 
 TEST_F(CountersTest, invalidOperationForGetRequestOpCode) {