Add unit tests for utils
authorYonggoo Kang <ygace.kang@samsung.com>
Tue, 23 Apr 2024 03:39:48 +0000 (12:39 +0900)
committerDongsun Lee <ds73.lee@samsung.com>
Wed, 24 Apr 2024 07:35:48 +0000 (16:35 +0900)
Change-Id: Ia838f962ee2ffa783f99dbe0d1fbadb46ca2db83

tests/CMakeLists.txt
tests/client-request-test.cpp
tests/utils-tests.cpp [new file with mode: 0644]

index bafcee378573a122a03c63226799e193b0a3f782..6a773da20cb8f556ee4261fdd6f86e3918b6c7ef 100644 (file)
@@ -14,6 +14,7 @@ SET(UNIT_TESTS_SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/serialization-test.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/dl-loader-test.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/socket-manager-test.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/utils-tests.cpp
 
     ${PRJ_SRC_SERVER_PATH}/dl-loader.cpp
     ${PRJ_SRC_SERVER_PATH}/service.cpp
index 4d865bd63e971fb24d935565b1e4354bea840802..dbd2d52b1b8934c4655c67ecd47d191b922d5248 100644 (file)
@@ -289,7 +289,7 @@ TEST_F(ClientRequestTest, request_to_not_supported_server_N)
 
             EXPECT_EQ(retVal, WAUTHN_ERROR_NOT_SUPPORTED)
                 << "[wauthn_process<TestClientRequestMC>] failed. "
-                << "retVal=" << wauthn_error_to_string(ret) << std::endl;
+                << "retVal=" << wauthn_error_to_string(retVal) << std::endl;
             sleep(1);
             EXPECT_EQ(userdata->called_qr, false)
                 << "The QR callback should not be called by cancel" << std::endl;
@@ -339,7 +339,7 @@ TEST_F(ClientRequestTest, MC_with_LD_and_not_call_QR_callback_P)
 
             EXPECT_EQ(retVal, WAUTHN_ERROR_NONE)
                 << "[wauthn_process<TestClientRequestMC>] failed. "
-                << "retVal=" << wauthn_error_to_string(ret) << std::endl;
+                << "retVal=" << wauthn_error_to_string(retVal) << std::endl;
 
             // Waiting for called the callbacks
             sleep(g_target_times_update_linked_data + 5);
@@ -390,7 +390,7 @@ TEST_F(ClientRequestTest, MC_without_LD_and_call_QR_callback_P)
 
             EXPECT_EQ(retVal, WAUTHN_ERROR_NONE)
                 << "[wauthn_process<TestClientRequestMC>] failed. "
-                << "retVal=" << wauthn_error_to_string(ret) << std::endl;
+                << "retVal=" << wauthn_error_to_string(retVal) << std::endl;
 
             // Waiting for called the callbacks
             sleep(g_target_times_update_linked_data + 5);
@@ -444,7 +444,7 @@ TEST_F(ClientRequestTest, GA_with_LD_and_not_call_QR_callback_P)
 
             EXPECT_EQ(retVal, WAUTHN_ERROR_NONE)
                 << "[wauthn_process<TestClientRequestGA>] failed. "
-                << "retVal=" << wauthn_error_to_string(ret) << std::endl;
+                << "retVal=" << wauthn_error_to_string(retVal) << std::endl;
 
             // Waiting for called the callbacks
             sleep(g_target_times_update_linked_data + 5);
@@ -497,7 +497,7 @@ TEST_F(ClientRequestTest, GA_without_LD_and_call_QR_callback_P)
 
             EXPECT_EQ(retVal, WAUTHN_ERROR_NONE)
                 << "[wauthn_process<TestClientRequestGA>] failed. "
-                << "retVal=" << wauthn_error_to_string(ret) << std::endl;
+                << "retVal=" << wauthn_error_to_string(retVal) << std::endl;
 
             // Waiting for called the callbacks
             sleep(g_target_times_update_linked_data + 5);
@@ -550,7 +550,7 @@ TEST_F(ClientRequestTest, not_allowed_N)
 
             EXPECT_EQ(retVal, WAUTHN_ERROR_NONE)
                 << "[wauthn_process<TestClientRequestMC>] failed. "
-                << "retVal=" << wauthn_error_to_string(ret) << std::endl;
+                << "retVal=" << wauthn_error_to_string(retVal) << std::endl;
 
             sleep(1);
 
@@ -634,7 +634,7 @@ TEST_F(ClientRequestTest, cancel_P)
 
             EXPECT_EQ(retVal, WAUTHN_ERROR_NONE)
                 << "[wauthn_process<TestClientRequestMC>] failed. "
-                << "retVal=" << wauthn_error_to_string(ret) << std::endl;
+                << "retVal=" << wauthn_error_to_string(retVal) << std::endl;
 
             TestClientRequest request(WebAuthnCall::CANCEL);
             request.Init();
diff --git a/tests/utils-tests.cpp b/tests/utils-tests.cpp
new file mode 100644 (file)
index 0000000..35d622f
--- /dev/null
@@ -0,0 +1,116 @@
+/*
+ *  Copyright (c) 2024 Samsung Electronics Co., Ltd. All rights reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ *
+ *
+ * @file        dl-loader-test.cpp
+ * @version     1.0
+ * @brief       unit tests for dl-loader
+ */
+
+#include <gtest/gtest.h>
+#include <iostream>
+#include <utils.h>
+
+
+namespace WA {
+
+class UtilsTest : public ::testing::Test {
+protected:
+    void SetUp() override {
+        // Do initialization if needed.
+    }
+
+    void TearDown() override {
+        // Do deinitialization if needed.
+    }
+};
+
+TEST_F(UtilsTest, handle_exceptions_by_try_catch_function_P)
+{
+    int ret = try_catch([&]() -> int {
+        Throw(SerializationException::InvalidStreamData);
+    });
+    EXPECT_EQ(ret, WAUTHN_ERROR_INVALID_PARAMETER);
+    std::cout << "ret: " << wauthn_error_to_string(ret) << std::endl;
+
+    ret = try_catch([&]() -> int {
+        Throw(MessageBufferException::OutOfData);
+    });
+    EXPECT_EQ(ret, WAUTHN_ERROR_MEMORY);
+    std::cout << "ret: " << wauthn_error_to_string(ret) << std::endl;
+
+    ret = try_catch([&]() -> int {
+        Throw(ServiceException::InvalidAction);
+    });
+    EXPECT_EQ(ret, WAUTHN_ERROR_INVALID_STATE);
+    std::cout << "ret: " << wauthn_error_to_string(ret) << std::endl;
+
+    ret = try_catch([&]() -> int {
+        Throw(ServiceException::InActive);
+    });
+    EXPECT_EQ(ret, WAUTHN_ERROR_SOCKET);
+    std::cout << "ret: " << wauthn_error_to_string(ret) << std::endl;
+
+    ret = try_catch([&]() -> int {
+        Throw(FileLockerException::LockFailed);
+    });
+    EXPECT_EQ(ret, WAUTHN_ERROR_INVALID_STATE);
+    std::cout << "ret: " << wauthn_error_to_string(ret) << std::endl;
+
+    ret = try_catch([&]() -> int {
+        Throw(SocketManagerException::InitFailed);
+    });
+    EXPECT_EQ(ret, WAUTHN_ERROR_INVALID_STATE);
+    std::cout << "ret: " << wauthn_error_to_string(ret) << std::endl;
+
+    ret = try_catch([&]() -> int {
+        throw std::logic_error("logic error");
+    });
+    EXPECT_EQ(ret, WAUTHN_ERROR_INVALID_STATE);
+    std::cout << "ret: " << wauthn_error_to_string(ret) << std::endl;
+
+    ret = try_catch([&]() -> int {
+        throw std::bad_alloc();
+    });
+    EXPECT_EQ(ret, WAUTHN_ERROR_MEMORY);
+    std::cout << "ret: " << wauthn_error_to_string(ret) << std::endl;
+
+    ret = try_catch([&]() -> int {
+        throw std::system_error();
+    });
+    EXPECT_EQ(ret, WAUTHN_ERROR_UNKNOWN);
+    std::cout << "ret: " << wauthn_error_to_string(ret) << std::endl;
+
+    ret = try_catch([&]() -> int {
+        throw std::exception();
+    });
+    EXPECT_EQ(ret, WAUTHN_ERROR_UNKNOWN);
+    std::cout << "ret: " << wauthn_error_to_string(ret) << std::endl;
+
+    ret = try_catch([&]() -> int {
+        throw std::overflow_error("overflow_error");
+    });
+    EXPECT_EQ(ret, WAUTHN_ERROR_UNKNOWN);
+    std::cout << "ret: " << wauthn_error_to_string(ret) << std::endl;
+
+    ret = try_catch([&]() -> int {
+        return WAUTHN_ERROR_NONE;
+    });
+    EXPECT_EQ(ret, WAUTHN_ERROR_NONE);
+    std::cout << "ret: " << wauthn_error_to_string(ret) << std::endl;
+
+}
+
+} // namespace WebAuthn