Handle not supported client's api call when shared library does not linked 44/308544/3
authorYonggoo Kang <ygace.kang@samsung.com>
Wed, 27 Mar 2024 07:45:51 +0000 (16:45 +0900)
committerYonggoo Kang <ygace.kang@samsung.com>
Wed, 27 Mar 2024 07:51:05 +0000 (16:51 +0900)
- Patch for the Change 308485
- Remove throwing exception in the constructor of DLLoader
- Add funtions for check if the webauthn-ble shared lib is linked
- Minor fix

Change-Id: I46a2d98e5808741334f388ed5163b2804ea09bca

srcs/server/dl-loader.cpp
srcs/server/dl-loader.h
srcs/server/main.cpp
srcs/server/service.cpp
srcs/server/service.h
tests/CMakeLists.txt
tests/client-request-test.cpp
tests/dl-loader-test.cpp
tests/socket-manager-test.cpp

index 11d279f60e1c39dda6a1f7152b3112b171be3c0f..b42c2313fed56350e40f245fc781f260f0d5c609 100644 (file)
@@ -28,11 +28,10 @@ DLLoader::DLLoader(std::string path) : m_libraryPath(std::move(path))
 {
     LogDebug("Loading library: " << m_libraryPath);
     m_libraryHandle = dlopen(m_libraryPath.c_str(), RTLD_LAZY | RTLD_LOCAL);
-    if (!m_libraryHandle)
-    {
-        ThrowMsg(ServiceException::InvalidSharedLib,
-            "Unable to load library(" << m_libraryPath << "): " << dlerror());
-    }
+    if (m_libraryPath.empty() || !m_libraryHandle)
+        LogError("Unable to load library(" << m_libraryPath << "): " << dlerror());
+    else
+        m_libraryEnabled = true;
 }
 DLLoader::~DLLoader()
 {
@@ -50,4 +49,14 @@ void *DLLoader::ResolveFunction(const std::string &name) noexcept
     return sym;
 }
 
+void DLLoader::SetLibraryEnabled(bool enable)
+{
+    m_libraryEnabled = enable;
+}
+
+bool DLLoader::LibraryEnabled()
+{
+    return m_libraryEnabled;
+}
+
 } // namespace WebAuthn
\ No newline at end of file
index b801fc597071092b0508298c85cbd74d3241dee1..5f83b20f130164c3cb3d8bd68936dd469b73df96 100644 (file)
@@ -32,6 +32,7 @@ public:
     virtual ~DLLoader();
 
     virtual void *ResolveFunction(const std::string &name) noexcept;
+    bool LibraryEnabled();
 
     template<typename ReturnValue, typename... Args>
     ReturnValue Invoke(const std::string &name, Args... args)
@@ -46,9 +47,13 @@ public:
         return func(args...);
     }
 
+protected:
+    void SetLibraryEnabled(bool enable);
+
 private:
     std::string m_libraryPath;
     void* m_libraryHandle = nullptr;
+    bool m_libraryEnabled = false;
 };
 
 } // namespace WebAuthn
\ No newline at end of file
index 729a0f428ebddba366f7943ead7208434a62b6d2..50c4fef559dcb164bb660ea5286895e85c6fa8a2 100644 (file)
@@ -43,8 +43,6 @@ 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 9a603fe2f8c4bd13dfb25ae632ea641164c2f51a..06a338cfa8b9465cd78b327f7f721d4c85ad9ec8 100644 (file)
@@ -42,6 +42,8 @@ void GenericService::ProcessEvent(Event &&msg)
     LogDebug("Processing message for socket " << msg.connectionID.sock <<
              " counter " << msg.connectionID.counter);
     int ret = try_catch([&]() -> int {
+        if (!PluginEnabled())
+            return HandleNotSupported(std::move(msg));
         // deserialize API call type
         int call_type_int;
         Deserialization::Deserialize(msg.buffer, call_type_int);
@@ -170,6 +172,21 @@ void GenericService::UnsetBusy()
     m_isBusy = false;
 }
 
+bool GenericService::PluginEnabled()
+{
+    return m_pluginHybrid->LibraryEnabled();
+}
+
+int GenericService::HandleNotSupported(Event &&msg)
+{
+    LogDebug("Shared library is not enabled");
+    MessageBuffer responseBuffer(m_serviceManager->newMessage());
+    responseBuffer.ModeStreaming();
+    Serialization::Serialize(responseBuffer, WAUTHN_ERROR_NOT_SUPPORTED);
+    m_serviceManager->Write(msg.connectionID, std::move(responseBuffer));
+    return WAUTHN_ERROR_NONE;
+}
+
 int GenericService::CheckBusyAndCred(SocketManager::ConnectionID connectionID)
 {
     std::lock_guard<std::mutex> ulock(m_isBusyMutex);
index 6b2483b393f93a94be020fee1f45af483b0121b7..b4b3bc0141e14362804c9fb8fd30915270ce7363 100644 (file)
@@ -122,6 +122,10 @@ private:
      */
     void UnsetBusy();
 
+    bool PluginEnabled();
+
+    int HandleNotSupported(Event &&msg);
+
     SocketManager *m_serviceManager = nullptr;
     bool m_isBusy = false;
     std::mutex m_isBusyMutex;
index a85b451ec2399a57176d9017a907bc24b60f6b12..63c1c73d3064a2508d429815d61594e2ecdb9366 100644 (file)
@@ -7,13 +7,13 @@ PKG_CHECK_MODULES(UNIT_TESTS_DEPS
 
 SET(UNIT_TESTS_SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/unittests.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/test-common.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/webauthn-client-test.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/client-request-test.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/file-lock-test.cpp
     ${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}/test-common.cpp
 
     ${PRJ_SRC_SERVER_PATH}/dl-loader.cpp
     ${PRJ_SRC_SERVER_PATH}/service.cpp
index b7d6325e78bf49f391342f273242dd1554c89811..321af87797663291a06027ef43f88e9822707673 100644 (file)
@@ -94,10 +94,10 @@ class TestDLLoader : public DLLoader {
 public:
     explicit TestDLLoader ()
     {
-        try{
-            DLLoader("");
-        }catch (...) {}
+        DLLoader("");
+        SetLibraryEnabled(true);
     }
+
     void* ResolveFunction(const std::string &name) noexcept override
     {
         if (name == WAH_API_MC)
@@ -164,6 +164,42 @@ protected:
     }
 };
 
+TEST_F(ClientRequestTest, request_to_not_supported_server_N)
+{
+    int ret = 1;
+    WA::SocketManager manager;
+    try{
+        auto service = std::make_unique<WA::TestService>(std::make_shared<DLLoader>(""));
+        manager.RegisterSocketService(std::move(service));
+        SocketManagerLoop loop(manager);
+        {
+            int retVal = WAUTHN_ERROR_NONE;
+            wauthn_mc_callbacks_s *callbacks = nullptr;
+            callbacks = (wauthn_mc_callbacks_s*) calloc(1, sizeof(wauthn_mc_callbacks_s));
+            callbacks->qrcode_callback = test_cb_display_qrcode;
+            callbacks->response_callback = test_cb_mc_response;
+            test_user_data_s *userdata = (test_user_data_s*) calloc(1, sizeof(test_user_data_s));
+            userdata->test_data = (char*)("user data");
+            userdata->test_int = 999;
+            callbacks->user_data = userdata;
+            retVal = wauthn_process<TestClientRequestMC>(&TestCommonData::clientData,
+                &TestCommonData::pubkeyCredCreationOptions, callbacks);
+
+            EXPECT_EQ(retVal, WAUTHN_ERROR_NOT_SUPPORTED)
+                    << "[wauthn_process<TestClientRequestMC>] failed. "
+                    << "retVal=" << wauthn_error_to_string(ret) << std::endl;
+            sleep(1);
+            free(userdata);
+            free(callbacks);
+        }
+        sleep(1);
+        ret = 0;
+    } catch (...) {
+        std::cout << "Error in starting service, unknown exception occured" << std::endl;
+        ret = -1;
+    }
+    EXPECT_EQ(ret, 0);
+}
 
 TEST_F(ClientRequestTest, MC_without_QR_callback_P)
 {
@@ -173,7 +209,6 @@ TEST_F(ClientRequestTest, MC_without_QR_callback_P)
         auto service = std::make_unique<WA::TestService>(std::make_shared<TestDLLoader>());
         manager.RegisterSocketService(std::move(service));
         SocketManagerLoop loop(manager);
-
         {
             int retVal = WAUTHN_ERROR_NONE;
             wauthn_mc_callbacks_s *callbacks = nullptr;
@@ -211,7 +246,6 @@ TEST_F(ClientRequestTest, MC_with_QR_callback_P)
         auto service = std::make_unique<WA::TestService>(std::make_shared<TestDLLoader>());
         manager.RegisterSocketService(std::move(service));
         SocketManagerLoop loop(manager);
-
         {
             int retVal = WAUTHN_ERROR_NONE;
             wauthn_mc_callbacks_s *callbacks = nullptr;
@@ -250,7 +284,6 @@ TEST_F(ClientRequestTest, GA_without_QR_callback_P)
         auto service = std::make_unique<WA::TestService>(std::make_shared<TestDLLoader>());
         manager.RegisterSocketService(std::move(service));
         SocketManagerLoop loop(manager);
-
         {
             int retVal = WAUTHN_ERROR_NONE;
             wauthn_ga_callbacks_s *callbacks = nullptr;
@@ -288,7 +321,6 @@ TEST_F(ClientRequestTest, GA_with_QR_callback_P)
         auto service = std::make_unique<WA::TestService>(std::make_shared<TestDLLoader>());
         manager.RegisterSocketService(std::move(service));
         SocketManagerLoop loop(manager);
-
         {
             int retVal = WAUTHN_ERROR_NONE;
             wauthn_ga_callbacks_s *callbacks = nullptr;
@@ -326,7 +358,6 @@ TEST_F(ClientRequestTest, not_allowed_N)
         auto service = std::make_unique<WA::TestService>(std::make_shared<TestDLLoader>());
         manager.RegisterSocketService(std::move(service));
         SocketManagerLoop loop(manager);
-
         {
             int retVal = WAUTHN_ERROR_NONE;
             wauthn_mc_callbacks_s *mc_callbacks = nullptr;
@@ -385,7 +416,6 @@ TEST_F(ClientRequestTest, cancel_P)
         auto service = std::make_unique<WA::TestService>(std::make_shared<TestDLLoader>());
         manager.RegisterSocketService(std::move(service));
         SocketManagerLoop loop(manager);
-
         {
             int retVal = WAUTHN_ERROR_NONE;
             wauthn_mc_callbacks_s *mc_callbacks = nullptr;
index a83f27c1b7351499a3f1645bcf4daad019ca9571..67eb37bbdf8eb73221d076f998226a299be5a1ad 100644 (file)
@@ -44,6 +44,7 @@ TEST_F(DLLoaderTest, DLLoader_P)
     int ret = 0;
     try{
         auto dlLoader = std::make_unique<DLLoader>(WAH_PLUGIN_SO_PATH_HYBRID);
+        EXPECT_EQ(dlLoader->LibraryEnabled(), true);
     } catch (...)
     {
         ret = -1;
@@ -52,20 +53,32 @@ TEST_F(DLLoaderTest, DLLoader_P)
     EXPECT_EQ(ret, 0);
 }
 
-TEST_F(DLLoaderTest, DLLoader_N)
+TEST_F(DLLoaderTest, DLLoader_empty_path_N)
 {
     int ret = 0;
     try{
-        auto dlLoader = std::make_unique<DLLoader>("invalid_so_path");
-    } catch (const ServiceException::InvalidSharedLib &e)
+        auto dlLoader = std::make_unique<DLLoader>("");
+        EXPECT_EQ(dlLoader->LibraryEnabled(), false);
+    } catch (...)
     {
         ret = -1;
+    }
+
+    EXPECT_EQ(ret, 0);
+}
+
+TEST_F(DLLoaderTest, DLLoader_invalid_path_N)
+{
+    int ret = 0;
+    try{
+        auto dlLoader = std::make_unique<DLLoader>("invalid_so_path");
+        EXPECT_EQ(dlLoader->LibraryEnabled(), false);
     } catch (...)
     {
-        ret = -2;
+        ret = -1;
     }
 
-    EXPECT_EQ(ret, -1);
+    EXPECT_EQ(ret, 0);
 }
 
 TEST_F(DLLoaderTest, Invoke_P)
@@ -73,6 +86,7 @@ TEST_F(DLLoaderTest, Invoke_P)
     int ret = 0;
     try{
         auto dlLoader = std::make_unique<DLLoader>(WAH_PLUGIN_SO_PATH_HYBRID);
+        EXPECT_EQ(dlLoader->LibraryEnabled(), true);
         dlLoader->Invoke<int>(WAH_API_CANCEL);
     } catch (...)
     {
@@ -87,6 +101,7 @@ TEST_F(DLLoaderTest, Invoke_N)
     int ret = 0;
     try{
         auto dlLoader = std::make_unique<DLLoader>(WAH_PLUGIN_SO_PATH_HYBRID);
+        EXPECT_EQ(dlLoader->LibraryEnabled(), true);
         dlLoader->Invoke<int>("invalid_api_name");
     } catch (const ServiceException::InvalidSharedLib &e)
     {
index bd645670d8d3d012566a6d93dda921838b280fab..5d15d58e2190fb974fe21f1802395fab35a936f4 100644 (file)
@@ -88,7 +88,8 @@ TEST_F(SocketManagerTest, mainloop_P)
     int ret = 1;
     WA::SocketManager manager;
     try{
-        auto service = std::make_unique<WA::TestService>(std::make_shared<DLLoader>(WAH_PLUGIN_SO_PATH_HYBRID));
+        auto service = std::make_unique<WA::TestService>(
+            std::make_shared<DLLoader>(WAH_PLUGIN_SO_PATH_HYBRID));
         manager.RegisterSocketService(std::move(service));
         SocketManagerLoop loop(manager);
         sleep(1);