1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17 #include "disallowed_usb_storage_device_write_plugin.h"
18 #include "edm_errors.h"
19 #include "edm_ipc_interface_code.h"
20 #include "iplugin_manager.h"
21 #include "parameters.h"
22 #include "plugin_singleton.h"
23 #include "utils.h"
24
25 using namespace testing::ext;
26 using namespace testing;
27
28 namespace OHOS {
29 namespace EDM {
30 namespace TEST {
31 class DisableUsbStorageDeviceWritePluginTest : public testing::Test {
32 protected:
33 static void SetUpTestSuite(void);
34
35 static void TearDownTestSuite(void);
36 };
37
SetUpTestSuite(void)38 void DisableUsbStorageDeviceWritePluginTest::SetUpTestSuite(void)
39 {
40 Utils::SetEdmInitialEnv();
41 }
42
TearDownTestSuite(void)43 void DisableUsbStorageDeviceWritePluginTest::TearDownTestSuite(void)
44 {
45 Utils::ResetTokenTypeAndUid();
46 ASSERT_TRUE(Utils::IsOriginalUTEnv());
47 std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
48 }
49
50 /**
51 * @tc.name: TestDisableUsbStorageDeviceWritePluginTestSet
52 * @tc.desc: Test DisableUsbStorageDeviceWritePluginTest::OnSetPolicy function.
53 * @tc.type: FUNC
54 */
55 HWTEST_F(DisableUsbStorageDeviceWritePluginTest, TestDisableUsbStorageDeviceWritePluginTestSet, TestSize.Level1)
56 {
57 MessageParcel data;
58 MessageParcel reply;
59 data.WriteBool(true);
60 std::shared_ptr<IPlugin> plugin = DisallowedUsbStorageDeviceWritePlugin::GetPlugin();
61 HandlePolicyData handlePolicyData{"false", "", false};
62 std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET,
63 EdmInterfaceCode::DISALLOWED_USB_STORAGE_DEVICE_WRITE);
64 ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
65 std::string value = system::GetParameter(
66 EdmConstants::CONST_ENTERPRISE_EXTERNAL_STORAGE_DEVICE_MANAGE_ENABLE, "false");
67 if (value == "false") {
68 EDMLOGE("TestDisableUsbStorageDeviceWritePluginTestSet OnHandlePolicy failed, interface unsupported");
69 ASSERT_TRUE(ret == EdmReturnErrCode::INTERFACE_UNSUPPORTED);
70 } else {
71 ASSERT_TRUE(ret == ERR_OK);
72 ASSERT_TRUE(handlePolicyData.policyData == "true");
73 ASSERT_TRUE(handlePolicyData.isChanged);
74
75 // 恢复环境,取消禁用
76 MessageParcel dataFalse;
77 dataFalse.WriteBool(false);
78 HandlePolicyData handlePolicyDataFalse{"true", "", false};
79 ret = plugin->OnHandlePolicy(funcCode, dataFalse, reply, handlePolicyDataFalse, DEFAULT_USER_ID);
80 ASSERT_TRUE(ret == ERR_OK);
81 }
82 }
83 } // namespace TEST
84 } // namespace EDM
85 } // namespace OHOS