Make exception type for invalid shared library 85/308485/1
authorYonggoo Kang <ygace.kang@samsung.com>
Tue, 26 Mar 2024 08:55:43 +0000 (17:55 +0900)
committerYonggoo Kang <ygace.kang@samsung.com>
Tue, 26 Mar 2024 08:55:43 +0000 (17:55 +0900)
Change-Id: If79f76ffb5ea1abab8ff80f80037c57ad2b9002b

srcs/common/exception.h
srcs/server/dl-loader.cpp
srcs/server/dl-loader.h
srcs/server/main.cpp
tests/dl-loader-test.cpp

index dc6955105036a3fa899ebd32f5b5059cecae8e6e..11f0cfdce499300084eff0b164a15615ab984b18 100644 (file)
  */
 #pragma once
 
-#include <string>
-#include <cstring>
 #include <cstdio>
-#include <exception>
 #include <cstdlib>
+#include <cstring>
+#include <exception>
 #include <sstream>
-
+#include <string>
 
 #define Try try
 
@@ -378,8 +377,9 @@ public:
 class ServiceException {
 public:
     DECLARE_EXCEPTION_TYPE(WA::Exception, Base)
-    DECLARE_EXCEPTION_TYPE(Base, InvalidAction)
     DECLARE_EXCEPTION_TYPE(Base, InActive)
+    DECLARE_EXCEPTION_TYPE(Base, InvalidAction)
+    DECLARE_EXCEPTION_TYPE(Base, InvalidSharedLib)
 };
 class ClientException {
 public:
index ac1d7702073cd39b7a0a7e6ffaf068f5e78dc891..fe789358c6131226e7e6d4007c9866d8cb701cb1 100644 (file)
@@ -28,8 +28,8 @@ DLLoader::DLLoader(std::string path) : m_libraryPath(std::move(path))
     m_libraryHandle = dlopen(m_libraryPath.c_str(), RTLD_LAZY | RTLD_LOCAL);
     if (!m_libraryHandle)
     {
-        LogError("Unable to load library(" << m_libraryPath << "): " << dlerror());
-        throw std::runtime_error("Unable to load library");
+        ThrowMsg(ServiceException::InvalidSharedLib,
+            "Unable to load library(" << m_libraryPath << "): " << dlerror());
     }
 }
 DLLoader::~DLLoader()
index c285b3217cdf1d8bca565cb7643f11eb68ccdafc..0d4cc2c5cc7e183eb965ec4bb811e924630229ae 100644 (file)
 
 #pragma once
 
+#include <dlfcn.h>
 #include <string>
-#include <stdexcept>
+#include <exception.h>
 #include <webauthn-log.h>
-#include <dlfcn.h>
 
 namespace WA {
 
@@ -49,7 +49,8 @@ public:
         function_t func = (function_t)ResolveFunction(name);
         if (!func)
         {
-            throw std::runtime_error("Trying to call unresolved function");
+            ThrowMsg(ServiceException::InvalidSharedLib,
+                "Trying to call unresolved function");
         }
         return func(args...);
     }
index dd84b944868e77c94f0230543c6090acbf9a7537..729a0f428ebddba366f7943ead7208434a62b6d2 100644 (file)
  * @brief       Implementation of webauthn
  */
 
-#include <stdlib.h>
 #include <signal.h>
-#include <webauthn-types.h>
-#include <webauthn-log.h>
+#include <stdlib.h>
 #include <exception.h>
 #include <service.h>
 #include <service-file-locker.h>
+#include <webauthn-log.h>
+#include <webauthn-types.h>
 
 #ifdef GCOV_BUILD
 extern "C" void __gcov_flush(void);
@@ -43,6 +43,8 @@ int webauthn_manager()
         manager.RegisterSocketService(std::move(service));
         manager.MainLoop();
         return EXIT_SUCCESS;
+    } catch (const ServiceException::InvalidSharedLib &e) {
+        LogError("Error in starting service, not supported: " << e.DumpToString());
     } catch (const std::exception &e) {
         LogError("Error in starting service, details:\n" << e.what());
     } catch (...) {
index df0ce4600a6459b6a8d8a969d8a709d1022526a8..a83f27c1b7351499a3f1645bcf4daad019ca9571 100644 (file)
  * @brief       unit tests for dl-loader
  */
 
-#include "test-common.h"
-
-#include <dl-loader.h>
 #include <gtest/gtest.h>
-#include <webauthn-hal.h>
 #include <iostream>
+#include <webauthn-hal.h>
+#include <dl-loader.h>
+#include <exception.h>
+#include "test-common.h"
 
 namespace WA {
 
@@ -57,9 +57,12 @@ TEST_F(DLLoaderTest, DLLoader_N)
     int ret = 0;
     try{
         auto dlLoader = std::make_unique<DLLoader>("invalid_so_path");
-    } catch (...)
+    } catch (const ServiceException::InvalidSharedLib &e)
     {
         ret = -1;
+    } catch (...)
+    {
+        ret = -2;
     }
 
     EXPECT_EQ(ret, -1);
@@ -85,9 +88,12 @@ TEST_F(DLLoaderTest, Invoke_N)
     try{
         auto dlLoader = std::make_unique<DLLoader>(WAH_PLUGIN_SO_PATH_HYBRID);
         dlLoader->Invoke<int>("invalid_api_name");
-    } catch (...)
+    } catch (const ServiceException::InvalidSharedLib &e)
     {
         ret = -1;
+    } catch (...)
+    {
+        ret = -2;
     }
 
     EXPECT_EQ(ret, -1);