diff --git a/services/dbms/test/unittest/dbms_services_kit_test/dbms_services_kit_test.cpp b/services/dbms/test/unittest/dbms_services_kit_test/dbms_services_kit_test.cpp index 894f828e6017d239a07aa7ccde47edf308d6dcb2..2b71ce85d7206e01db509f962a3e001f36827339 100644 --- a/services/dbms/test/unittest/dbms_services_kit_test/dbms_services_kit_test.cpp +++ b/services/dbms/test/unittest/dbms_services_kit_test/dbms_services_kit_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. * 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 @@ -41,9 +41,9 @@ #endif #include "iservice_registry.h" #include "json_util.h" +#include "mock_scope_guard.h" #include "nativetoken_kit.h" #include "token_setproc.h" -#include "scope_guard.h" #include "service_control.h" #include "softbus_common.h" #include "status_receiver_host.h" @@ -238,7 +238,7 @@ sptr DbmsServicesKitTest::GetInstallerProxy() bool DbmsServicesKitTest::InstallBundle(const std::string &bundlePath) const { setuid(Constants::FOUNDATION_UID); - ScopeGuard uidGuard([&] { setuid(Constants::ROOT_UID); }); + MockScopeGuard uidGuard(std::bind(setuid, Constants::ROOT_UID)); sptr installerProxy = GetInstallerProxy(); if (!installerProxy) { APP_LOGE("get bundle installer failed."); @@ -257,7 +257,7 @@ bool DbmsServicesKitTest::InstallBundle(const std::string &bundlePath) const bool DbmsServicesKitTest::UninstallBundle(const std::string &bundleName) const { setuid(Constants::FOUNDATION_UID); - ScopeGuard uidGuard([&] { setuid(Constants::ROOT_UID); }); + MockScopeGuard uidGuard(std::bind(setuid, Constants::ROOT_UID)); sptr installerProxy = GetInstallerProxy(); if (!installerProxy) { APP_LOGE("get bundle installer failed."); diff --git a/services/dbms/test/unittest/dbms_services_kit_test/mock_scope_guard.h b/services/dbms/test/unittest/dbms_services_kit_test/mock_scope_guard.h new file mode 100644 index 0000000000000000000000000000000000000000..d499e558f8aee447b52b3c61aee07daf37115826 --- /dev/null +++ b/services/dbms/test/unittest/dbms_services_kit_test/mock_scope_guard.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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. + */ +#ifndef FOUNDATION_APPEXECFWK_MOCK_BUNDLEMGR_INCLUDE_SCOPE_GUARD +#define FOUNDATION_APPEXECFWK_MOCK_BUNDLEMGR_INCLUDE_SCOPE_GUARD + +#include + +namespace OHOS { +namespace AppExecFwk { +class MockScopeGuard final { +public: + using Function = std::function; + explicit MockScopeGuard(Function fn) : fn_(fn), dismissed_(false) + {} + + ~MockScopeGuard() + { + if (!dismissed_) { + fn_(); + } + } + + void Dismiss() + { + dismissed_ = true; + } + +private: + Function fn_; + bool dismissed_; +}; +} +} +#endif // FOUNDATION_APPEXECFWK_MOCK_BUNDLEMGR_INCLUDE_SCOPE_GUARD \ No newline at end of file