1 /*
2 * Copyright (c) 2024 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 #define protected public
17 #include "disable_usb_plugin.h"
18 #undef protected
19
20 #include <gtest/gtest.h>
21
22 #include "edm_ipc_interface_code.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 DisableUsbPluginTest : public testing::Test {
32 protected:
33 static void SetUpTestSuite(void);
34
35 static void TearDownTestSuite(void);
36 };
37
SetUpTestSuite(void)38 void DisableUsbPluginTest::SetUpTestSuite(void)
39 {
40 Utils::SetEdmInitialEnv();
41 }
42
TearDownTestSuite(void)43 void DisableUsbPluginTest::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: TestOnSetPolicyTrue
52 * @tc.desc: Test DisableUsbPlugin::OnSetPolicy function to set true.
53 * @tc.type: FUNC
54 */
55 HWTEST_F(DisableUsbPluginTest, TestOnSetPolicyTrue, TestSize.Level1)
56 {
57 MessageParcel data;
58 MessageParcel reply;
59 data.WriteBool(true);
60 std::shared_ptr<IPlugin> plugin = DisableUsbPlugin::GetPlugin();
61 std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISABLE_USB);
62 HandlePolicyData handlePolicyData{"false", "", false};
63 ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
64 ASSERT_TRUE(ret == ERR_OK);
65 ASSERT_TRUE(handlePolicyData.isChanged);
66 }
67
68 /**
69 * @tc.name: TestOnSetPolicyFalse
70 * @tc.desc: Test DisableUsbPlugin::OnSetPolicy function to set false.
71 * @tc.type: FUNC
72 */
73 HWTEST_F(DisableUsbPluginTest, TestOnSetPolicyFalse, TestSize.Level1)
74 {
75 MessageParcel data;
76 MessageParcel reply;
77 data.WriteBool(false);
78 std::shared_ptr<IPlugin> plugin = DisableUsbPlugin::GetPlugin();
79 std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISABLE_USB);
80 HandlePolicyData handlePolicyData{"false", "", false};
81 ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
82 ASSERT_TRUE(ret == ERR_OK);
83 }
84
85 /**
86 * @tc.name: TestOnAdminRemoveTrue
87 * @tc.desc: Test DisableUsbPlugin::OnAdminRemove function when policy is true.
88 * @tc.type: FUNC
89 */
90 HWTEST_F(DisableUsbPluginTest, TestOnAdminRemoveTrue, TestSize.Level1)
91 {
92 DisableUsbPlugin plugin;
93 std::string adminName{"testAdminName"};
94 bool policyData = true;
95 bool mergeData = false;
96 ErrCode ret = plugin.OnAdminRemove(adminName, policyData, mergeData, DEFAULT_USER_ID);
97 ASSERT_TRUE(ret == ERR_OK);
98 }
99
100 /**
101 * @tc.name: TestOnAdminRemoveFalse
102 * @tc.desc: Test DisableUsbPlugin::OnAdminRemove function when policy is disabled.
103 * @tc.type: FUNC
104 */
105 HWTEST_F(DisableUsbPluginTest, TestOnAdminRemoveFalse, TestSize.Level1)
106 {
107 DisableUsbPlugin plugin;
108 std::string adminName{"testAdminName"};
109 bool policyData = false;
110 bool mergeData = false;
111 ErrCode ret = plugin.OnAdminRemove(adminName, policyData, mergeData, DEFAULT_USER_ID);
112 ASSERT_TRUE(ret == ERR_OK);
113 }
114 } // namespace TEST
115 } // namespace EDM
116 } // namespace OHOS