Refactor tests for exceptions, requests and responses 99/322899/1 tizen_9.0
authorFilip Skrzeczkowski <f.skrzeczkow@samsung.com>
Thu, 17 Apr 2025 09:43:22 +0000 (11:43 +0200)
committerFilip Skrzeczkowski <f.skrzeczkow@samsung.com>
Thu, 17 Apr 2025 09:51:05 +0000 (11:51 +0200)
 * Use unique pointers in cynara unit tests for exceptions, requests and
   responses in order to increase coverage of deleting destructor
   symbols generated by the compiler due to the use of virtual
   destructors in these classes.
 * Refactor exception tests

Change-Id: Ifdcb6c5a593ed4af6c0aacea4b6bcf2390b9474e

test/common/exceptions/trycatch.cpp
test/common/protocols/RequestTestHelper.h
test/common/protocols/ResponseTestHelper.h

index 788a8bc5ede06aa9171f91d92fd51ed9339dbf25..8b5989238a844a1bb756b0c0ad6593e8cfb4cd2f 100644 (file)
@@ -25,9 +25,6 @@
  * @brief       Tests for tryCatch function
  */
 
-#include <common/exceptions/TryCatch.h>
-#include <common/exceptions/Exception.h>
-
 #include <gmock/gmock.h>
 #include <gtest/gtest.h>
 
@@ -93,6 +90,26 @@ public:
     };
 };
 
+template<typename T, typename... Args>
+void testException(const std::string &expectedMessage, int expectedError, Args&&... args)
+{
+    // Note: because Cynara exceptions' destructors are marked as virtual, the compiler
+    // generates deleting destructors for these classes. Naturally, under normal circumstances
+    // exceptions are always allocated on the stack so these destructors won't ever be called
+    // thus decreasing our test coverage. Therefore explicit testing is performed here by using
+    // unique pointers.
+    using ::testing::StartsWith;
+
+    std::unique_ptr<T> e = std::make_unique<T>(std::forward<Args>(args)...);
+    ASSERT_THAT(e->what(), StartsWith(expectedMessage));
+
+    int ret = CYNARA_API_SUCCESS;
+    auto throwFun = [&]() { throw *e; return ret; };
+
+    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
+    ASSERT_EQ(ret, expectedError);
+}
+
 } // namespace anonymous
 
 TEST(TryCatch, forcedUnwindNegative)
@@ -148,283 +165,158 @@ TEST(TryCatch, derivedExceptionNegative)
 
 TEST(TryCatch, accessDeniedExceptionNegative)
 {
-    using ::testing::StartsWith;
-
-    Cynara::AccessDeniedException e(SOME_ERROR);
-    ASSERT_THAT(e.what(), StartsWith("Access denied from: " + SOME_ERROR));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_PERMISSION_DENIED);
+    testException<Cynara::AccessDeniedException>(
+        "Access denied from: " + SOME_ERROR,
+        CYNARA_API_PERMISSION_DENIED,
+        SOME_ERROR);
 }
 
 TEST(TryCatch, bucketDeserializationExceptionNegative)
 {
-    using ::testing::StartsWith;
-
-    Cynara::BucketDeserializationException e(SOME_BUCKET);
-    ASSERT_THAT(e.what(), StartsWith("Could not deserialize bucket " + SOME_BUCKET));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::BucketDeserializationException>(
+        "Could not deserialize bucket " + SOME_BUCKET,
+        CYNARA_API_UNKNOWN_ERROR,
+        SOME_BUCKET);
 }
 
 TEST(TryCatch, bucketNotExistsExceptionNegative)
 {
-    using ::testing::StartsWith;
-
-    Cynara::BucketNotExistsException e(SOME_BUCKET);
-    ASSERT_THAT(e.what(), StartsWith("Bucket " + SOME_BUCKET + " does not exist"));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::BucketNotExistsException>(
+        "Bucket " + SOME_BUCKET + " does not exist",
+        CYNARA_API_UNKNOWN_ERROR,
+        SOME_BUCKET);
 }
 
 TEST(TryCatch, bucketRecordCorruptionExceptionNegative)
 {
-    using ::testing::StartsWith;
-
-    Cynara::BucketRecordCorruptedException e(SOME_LINE);
-    ASSERT_THAT(e.what(), StartsWith("Bucket record corrupted at line: <" + SOME_LINE + ">"));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::BucketRecordCorruptedException>(
+        "Bucket record corrupted at line: <" + SOME_LINE + ">",
+        CYNARA_API_UNKNOWN_ERROR,
+        SOME_LINE);
 }
 
 TEST(TryCatch, bucketSerializationExceptionNegative)
 {
-    using ::testing::StartsWith;
-
-    Cynara::BucketSerializationException e(SOME_BUCKET);
-    ASSERT_THAT(e.what(), StartsWith("Could not serialize bucket " + SOME_BUCKET));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::BucketSerializationException>(
+        "Could not serialize bucket " + SOME_BUCKET,
+        CYNARA_API_UNKNOWN_ERROR,
+        SOME_BUCKET);
 }
 
 TEST(TryCatch, cannotCreateFileExceptionNegative)
 {
-    using ::testing::StartsWith;
-
-    Cynara::CannotCreateFileException e(SOME_FILE);
-    ASSERT_THAT(e.what(), StartsWith("File " + SOME_FILE + " cannot be created"));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::CannotCreateFileException>(
+        "File " + SOME_FILE + " cannot be created",
+        CYNARA_API_UNKNOWN_ERROR,
+        SOME_FILE);
 }
 
 TEST(TryCatch, checksumRecordCorruptedExceptionNegative)
 {
-    using ::testing::StartsWith;
-
-    Cynara::ChecksumRecordCorruptedException e(SOME_LINE);
-    ASSERT_THAT(e.what(), StartsWith("Checksum record corrupted at line: <" + SOME_LINE + ">"));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::ChecksumRecordCorruptedException>(
+        "Checksum record corrupted at line: <" + SOME_LINE + ">",
+        CYNARA_API_UNKNOWN_ERROR,
+        SOME_LINE);
 }
 
 TEST(TryCatch, contextErrorExceptionNegative)
 {
-    using ::testing::StartsWith;
-
-    Cynara::ContextErrorException e;
-    ASSERT_THAT(e.what(), StartsWith("ContextErrorException"));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::ContextErrorException>(
+        "ContextErrorException",
+        CYNARA_API_UNKNOWN_ERROR);
 }
 
 TEST(TryCatch, databaseBusyExceptionNegative)
 {
-    using ::testing::StartsWith;
-
-    Cynara::DatabaseBusyException e;
-    ASSERT_THAT(e.what(), StartsWith("Database busy"));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::DatabaseBusyException>(
+        "Database busy",
+        CYNARA_API_UNKNOWN_ERROR);
 }
 
 TEST(TryCatch, databaseCorruptedExceptionNegative)
 {
-    using ::testing::StartsWith;
-
-    Cynara::DatabaseCorruptedException e;
-    ASSERT_THAT(e.what(), StartsWith("DatabaseCorruptedException"));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::DatabaseCorruptedException>(
+        "DatabaseCorruptedException",
+        CYNARA_API_UNKNOWN_ERROR);
 }
 
 TEST(TryCatch, databaseReadExceptionNegative)
 {
-    using ::testing::StartsWith;
     const std::string SOME_OP = "some operation";
 
-    Cynara::DatabaseReadException e(SOME_FILE, SOME_OP);
-    ASSERT_THAT(e.what(),
-                StartsWith("Loading the database from the file <" + SOME_FILE
-                    + "> failed during " + SOME_OP));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::DatabaseReadException>(
+        "Loading the database from the file <" + SOME_FILE + "> failed during " + SOME_OP,
+        CYNARA_API_UNKNOWN_ERROR,
+        SOME_FILE, SOME_OP);
 }
 
 TEST(TryCatch, databaseWriteExceptionNegative)
 {
-    using ::testing::StartsWith;
     const std::string SOME_OP = "some operation";
 
-    Cynara::DatabaseWriteException e(SOME_FILE, SOME_OP);
-    ASSERT_THAT(e.what(),
-                StartsWith("Saving the database to file <" + SOME_FILE
-                    + "> failed during " + SOME_OP));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::DatabaseWriteException>(
+        "Saving the database to file <" + SOME_FILE + "> failed during " + SOME_OP,
+        CYNARA_API_UNKNOWN_ERROR,
+        SOME_FILE, SOME_OP);
 }
 
 TEST(TryCatch, defaultBucketDeletionExceptionNegative)
 {
-    using ::testing::StartsWith;
-
-    Cynara::DefaultBucketDeletionException e;
-    ASSERT_THAT(e.what(), StartsWith("DefaultBucketDeletionException"));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::DefaultBucketDeletionException>(
+        "DefaultBucketDeletionException",
+        CYNARA_API_UNKNOWN_ERROR);
 }
 
 TEST(TryCatch, defaultBucketSetNoneExceptionNegative)
 {
-    using ::testing::StartsWith;
-
-    Cynara::DefaultBucketSetNoneException e;
-    ASSERT_THAT(e.what(), StartsWith("DefaultBucketSetNoneException"));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+   testException<Cynara::DefaultBucketSetNoneException>(
+        "DefaultBucketSetNoneException",
+        CYNARA_API_UNKNOWN_ERROR);
 }
 
 TEST(TryCatch, descriptorNotExistsExceptionNegative)
 {
-    using ::testing::StartsWith;
     const int SOME_DSCR = 42;
 
-    Cynara::DescriptorNotExistsException e(SOME_DSCR);
-    ASSERT_THAT(e.what(), StartsWith("trying access not existing descriptor ["
-                                     + std::to_string(SOME_DSCR) + "]"));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::DescriptorNotExistsException>(
+        "trying access not existing descriptor [" + std::to_string(SOME_DSCR) + "]",
+        CYNARA_API_UNKNOWN_ERROR,
+        SOME_DSCR);
 }
 
 TEST(TryCatch, fileLockAcquiringExceptionNegative)
 {
-    using ::testing::StartsWith;
     const int SOME_ERRNO = 42;
     const std::string SOME_ERRNO_STR = strerror(SOME_ERRNO);
 
-    Cynara::FileLockAcquiringException e(SOME_ERRNO);
-
-    ASSERT_THAT(e.what(),
-                StartsWith("File lock acquiring error [" + std::to_string(SOME_ERRNO) + "]"
-                    + " <" + SOME_ERRNO_STR + ">"));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::FileLockAcquiringException>(
+        "File lock acquiring error [" + std::to_string(SOME_ERRNO) + "]" +
+            " <" + SOME_ERRNO_STR + ">",
+        CYNARA_API_UNKNOWN_ERROR,
+        SOME_ERRNO);
 }
 
 TEST(TryCatch, fileNotFoundExceptionNegative)
 {
-    using ::testing::StartsWith;
-
-    Cynara::FileNotFoundException e(SOME_FILE);
-    ASSERT_THAT(e.what(),
-                StartsWith("File " + SOME_FILE + " not found or corrupted badly"));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::FileNotFoundException>(
+        "File " + SOME_FILE + " not found or corrupted badly",
+        CYNARA_API_UNKNOWN_ERROR,
+        SOME_FILE);
 }
 
 TEST(TryCatch, initExceptionNegative)
 {
-    using ::testing::StartsWith;
-
-    Cynara::InitException e;
-    ASSERT_THAT(e.what(), StartsWith("InitException"));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::InitException>(
+        "InitException",
+        CYNARA_API_UNKNOWN_ERROR);
 }
 
 TEST(TryCatch, invalidBucketIdExceptionNegative)
 {
-    using ::testing::StartsWith;
-
-    Cynara::InvalidBucketIdException e(SOME_BUCKET);
-    ASSERT_THAT(e.what(),
-                StartsWith("Bucket ID " + SOME_BUCKET + " contains forbidden characters"));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::InvalidBucketIdException>(
+        "Bucket ID " + SOME_BUCKET + " contains forbidden characters",
+        CYNARA_API_UNKNOWN_ERROR,
+        SOME_BUCKET);
 }
 
 TEST(TryCatch, invalidProtocolExceptionNegative)
@@ -443,8 +335,9 @@ TEST(TryCatch, invalidProtocolExceptionNegative)
     };
 
     for (auto& typeToString : INVALID_PROTOCOL_EXCEPTION_TYPE_TO_STRING) {
-        Cynara::InvalidProtocolException e(typeToString.first);
-        ASSERT_THAT(e.what(), StartsWith(typeToString.second));
+        std::unique_ptr<Cynara::InvalidProtocolException> e =
+            std::make_unique<Cynara::InvalidProtocolException>(typeToString.first);
+        ASSERT_THAT(e->what(), StartsWith(typeToString.second));
     }
 
     int ret = CYNARA_API_SUCCESS;
@@ -460,149 +353,99 @@ TEST(TryCatch, invalidProtocolExceptionNegative)
 
 TEST(TryCatch, noMemoryExceptionNegative)
 {
-    using ::testing::StartsWith;
-
-    Cynara::NoMemoryException e(SOME_ERROR);
-    ASSERT_THAT(e.what(), StartsWith("NoMemoryException with message <" + SOME_ERROR + ">"));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_OUT_OF_MEMORY);
+    testException<Cynara::NoMemoryException>(
+        "NoMemoryException with message <" + SOME_ERROR + ">",
+        CYNARA_API_OUT_OF_MEMORY,
+        SOME_ERROR);
 }
 
 TEST(TryCatch, notImplementedExceptionNegative)
 {
-    using ::testing::StartsWith;
-
-    Cynara::NotImplementedException e;
-    ASSERT_THAT(e.what(), StartsWith("NotImplementedException"));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::NotImplementedException>(
+        "NotImplementedException",
+        CYNARA_API_UNKNOWN_ERROR);
 }
 
 TEST(TryCatch, nullPointerExceptionNegative)
 {
-    using ::testing::StartsWith;
     const std::string SOME_VAR = "someVar";
 
-    Cynara::NullPointerException e(SOME_VAR.c_str());
-    ASSERT_THAT(e.what(),
-                StartsWith("unexpected null value in variable <" + SOME_VAR + ">"));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::NullPointerException>(
+        "unexpected null value in variable <" + SOME_VAR + ">",
+        CYNARA_API_UNKNOWN_ERROR,
+        SOME_VAR.c_str());
 }
 
 TEST(TryCatch, outOfDataExceptionNegative)
 {
-    using ::testing::StartsWith;
-
     const int SOME_RANGE = 42;
     const int SOME_ACCESS = 4242;
 
-    Cynara::OutOfDataException e(SOME_RANGE, SOME_ACCESS);
-    ASSERT_THAT(e.what(),
-                StartsWith("trying access [" + std::to_string(SOME_ACCESS) + "]"
-                           + " which exceeds data range [" + std::to_string(SOME_RANGE) + "]"));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::OutOfDataException>(
+        "trying access [" + std::to_string(SOME_ACCESS) + "]"
+            + " which exceeds data range [" + std::to_string(SOME_RANGE) + "]",
+        CYNARA_API_UNKNOWN_ERROR,
+        SOME_RANGE, SOME_ACCESS);
 }
 
 TEST(TryCatch, pluginErrorExceptionNegative)
 {
-    using ::testing::StartsWith;
-
     const Cynara::PolicyKey SOME_KEY("someClient", "someUser", "somePrivilege");
 
-    Cynara::PluginErrorException e(SOME_KEY);
-    ASSERT_THAT(e.what(),
-                StartsWith("Plugin couldn't get result for user <"
-                           + SOME_KEY.user().toString() + ">, "
-                           "client <" + SOME_KEY.client().toString() + ">, "
-                           "privilege <" + SOME_KEY.privilege().toString() + ">"));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::PluginErrorException>(
+        "Plugin couldn't get result for user <"
+            + SOME_KEY.user().toString() + ">, "
+            "client <" + SOME_KEY.client().toString() + ">, "
+            "privilege <" + SOME_KEY.privilege().toString() + ">",
+        CYNARA_API_UNKNOWN_ERROR,
+        SOME_KEY);
 }
 
 TEST(TryCatch, pluginNotFoundExceptionNegative)
 {
-    using ::testing::StartsWith;
-
     const Cynara::PolicyResult SOME_RESULT(42);
 
-    Cynara::PluginNotFoundException e(SOME_RESULT);
-    ASSERT_THAT(e.what(),
-                StartsWith("No proper plugin found to interprete PolicyResult {type = ["
-                  + std::to_string(SOME_RESULT.policyType()) + "], metadata = <"
-                  + SOME_RESULT.metadata().substr(0, 20) + ">}"));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::PluginNotFoundException>(
+        "No proper plugin found to interprete PolicyResult {type = ["
+            + std::to_string(SOME_RESULT.policyType()) + "], metadata = <"
+            + SOME_RESULT.metadata().substr(0, 20) + ">}",
+        CYNARA_API_UNKNOWN_ERROR,
+        SOME_RESULT);
 }
 
 TEST(TryCatch, unexpectedErrorExceptionNegative)
 {
-    using ::testing::StartsWith;
-
     const int SOME_ERROR_CODE = 42;
     const char *SOME_ERROR_CSTR = "some error cstr";
 
-    Cynara::UnexpectedErrorException e1(SOME_ERROR_CODE, SOME_ERROR_CSTR);
-    ASSERT_THAT(e1.what(),
-                StartsWith("UnexpectedErrorException with errorCode =["
-                           + std::to_string(SOME_ERROR_CODE)
-                           + "] and message <" + SOME_ERROR_CSTR + ">"));
-    Cynara::UnexpectedErrorException e2(SOME_ERROR_CODE, SOME_ERROR);
-    ASSERT_THAT(e2.what(),
-                StartsWith("UnexpectedErrorException with errorCode =["
-                           + std::to_string(SOME_ERROR_CODE)
-                           + "] and message <" + SOME_ERROR + ">"));
-    Cynara::UnexpectedErrorException e3(SOME_ERROR);
-    ASSERT_THAT(e3.what(),
-                StartsWith("UnexpectedErrorException with message <" + SOME_ERROR + ">"));
+    testException<Cynara::UnexpectedErrorException>(
+        "UnexpectedErrorException with errorCode =["
+            + std::to_string(SOME_ERROR_CODE)
+            + "] and message <" + SOME_ERROR_CSTR + ">",
+        CYNARA_API_UNKNOWN_ERROR,
+        SOME_ERROR_CODE, SOME_ERROR_CSTR);
 
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e1; return ret; };
+    testException<Cynara::UnexpectedErrorException>(
+        "UnexpectedErrorException with errorCode =["
+            + std::to_string(SOME_ERROR_CODE)
+            + "] and message <" + SOME_ERROR + ">",
+        CYNARA_API_UNKNOWN_ERROR,
+        SOME_ERROR_CODE, SOME_ERROR);
 
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::UnexpectedErrorException>(
+        "UnexpectedErrorException with message <" + SOME_ERROR + ">",
+        CYNARA_API_UNKNOWN_ERROR,
+        SOME_ERROR);
 }
 
 TEST(TryCatch, unknownPolicyTypeExceptionNegative)
 {
-    using ::testing::StartsWith;
-
     const Cynara::PolicyType SOME_POLICY_TYPE = 42;
 
-    Cynara::UnknownPolicyTypeException e(SOME_POLICY_TYPE);
-    ASSERT_THAT(e.what(),
-                StartsWith("Unknown PolicyType [" + std::to_string(SOME_POLICY_TYPE) + "]"));
-
-    int ret = CYNARA_API_SUCCESS;
-    auto throwFun = [&]() { throw e; return ret; };
-
-    ASSERT_NO_THROW(ret = Cynara::tryCatch(throwFun));
-    ASSERT_EQ(ret, CYNARA_API_UNKNOWN_ERROR);
+    testException<Cynara::UnknownPolicyTypeException>(
+        "Unknown PolicyType [" + std::to_string(SOME_POLICY_TYPE) + "]",
+        CYNARA_API_UNKNOWN_ERROR,
+        SOME_POLICY_TYPE);
 }
 
 TEST(TryCatch, throwSomeNegative)
index 95be4bbf59c6eb6f7318655608ce41c2d07b1e48..99af38f203dbfa553efed08797774627b0047edc 100644 (file)
@@ -46,6 +46,11 @@ namespace RequestTestHelper {
 
 template <typename R>
 void testRequest(std::shared_ptr<R> request, Cynara::ProtocolPtr protocol) {
+    // Allocate a copy as a unique_ptr in order to test the deleting destructor
+    std::unique_ptr<R> requestCopy = std::make_unique<R>(*request);
+    auto name = requestCopy->name();
+    ASSERT_TRUE(name && name[0] != '\0');
+
     auto queue = std::make_shared<Cynara::MutexedBinaryQueue>();
     Cynara::RequestContext context(Cynara::ResponseTakerPtr(), queue);
 
index 821705f76c2c84214866f48b4cdd18f34b3e720b..5be5ab4771874df1ab45beac24c3c0b142d7086a 100644 (file)
@@ -55,7 +55,9 @@ void testResponse(std::shared_ptr<R> response, Cynara::ProtocolPtr protocol) {
     ASSERT_TRUE(bool(extractedResponse));
     ASSERT_EQ(queue->lock()->size(), static_cast<size_t>(0));
 
-    compare(*response, dynamic_cast<R &>(*extractedResponse));
+    // Allocate a copy as a unique_ptr in order to test the deleting destructor
+    std::unique_ptr<R> responseCopy = std::make_unique<R>(*response);
+    compare(*responseCopy, dynamic_cast<R &>(*extractedResponse));
 }
 
 void binaryTestResponse(Cynara::ResponsePtr response, Cynara::ProtocolPtr protocol) {