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 #include "disable_usb_plugin.h"
17 
18 #include <gtest/gtest.h>
19 
20 #include "edm_ipc_interface_code.h"
21 #include "utils.h"
22 
23 using namespace testing::ext;
24 using namespace testing;
25 
26 namespace OHOS {
27 namespace EDM {
28 namespace TEST {
29 class DisableUsbPluginTest : public testing::Test {
30 protected:
31     static void SetUpTestSuite(void);
32 
33     static void TearDownTestSuite(void);
34 };
35 
SetUpTestSuite(void)36 void DisableUsbPluginTest::SetUpTestSuite(void)
37 {
38     Utils::SetEdmInitialEnv();
39 }
40 
TearDownTestSuite(void)41 void DisableUsbPluginTest::TearDownTestSuite(void)
42 {
43     Utils::ResetTokenTypeAndUid();
44     ASSERT_TRUE(Utils::IsOriginalUTEnv());
45     std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
46 }
47 
48 /**
49  * @tc.name: TestOnSetPolicyTrue
50  * @tc.desc: Test DisableUsbPlugin::OnSetPolicy function to set true.
51  * @tc.type: FUNC
52  */
53 HWTEST_F(DisableUsbPluginTest, TestOnSetPolicyTrue, TestSize.Level1)
54 {
55     MessageParcel data;
56     MessageParcel reply;
57     data.WriteBool(true);
58     std::shared_ptr<IPlugin> plugin = DisableUsbPlugin::GetPlugin();
59     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISABLE_USB);
60     HandlePolicyData handlePolicyData{"false", false};
61     ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
62     ASSERT_TRUE(ret == ERR_OK);
63     ASSERT_TRUE(handlePolicyData.isChanged);
64 }
65 
66 /**
67  * @tc.name: TestOnSetPolicyFalse
68  * @tc.desc: Test DisableUsbPlugin::OnSetPolicy function to set false.
69  * @tc.type: FUNC
70  */
71 HWTEST_F(DisableUsbPluginTest, TestOnSetPolicyFalse, TestSize.Level1)
72 {
73     MessageParcel data;
74     MessageParcel reply;
75     data.WriteBool(false);
76     std::shared_ptr<IPlugin> plugin = DisableUsbPlugin::GetPlugin();
77     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISABLE_USB);
78     HandlePolicyData handlePolicyData{"false", false};
79     ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
80     ASSERT_TRUE(ret == ERR_OK);
81 }
82 
83 /**
84  * @tc.name: TestOnGetPolicy
85  * @tc.desc: Test DisableUsbPlugin::OnGetPolicy function.
86  * @tc.type: FUNC
87  */
88 HWTEST_F(DisableUsbPluginTest, TestOnGetPolicy, TestSize.Level1)
89 {
90     std::shared_ptr<IPlugin> plugin = DisableUsbPlugin::GetPlugin();
91     std::string policyData{"false"};
92     MessageParcel data;
93     MessageParcel reply;
94     ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
95     int32_t flag = ERR_INVALID_VALUE;
96     ASSERT_TRUE(reply.ReadInt32(flag) && (flag == ERR_OK));
97     bool result = false;
98     reply.ReadBool(result);
99     ASSERT_TRUE(ret == ERR_OK);
100     ASSERT_FALSE(result);
101 }
102 
103 /**
104  * @tc.name: TestOnAdminRemoveTrue
105  * @tc.desc: Test DisableUsbPlugin::OnAdminRemove function when policy is true.
106  * @tc.type: FUNC
107  */
108 HWTEST_F(DisableUsbPluginTest, TestOnAdminRemoveTrue, TestSize.Level1)
109 {
110     DisableUsbPlugin plugin;
111     std::string adminName{"testAdminName"};
112     bool policyData = true;
113     ErrCode ret = plugin.OnAdminRemove(adminName, policyData, DEFAULT_USER_ID);
114     ASSERT_TRUE(ret == ERR_OK);
115 }
116 
117 /**
118  * @tc.name: TestOnAdminRemoveFalse
119  * @tc.desc: Test DisableUsbPlugin::OnAdminRemove function when policy is disabled.
120  * @tc.type: FUNC
121  */
122 HWTEST_F(DisableUsbPluginTest, TestOnAdminRemoveFalse, TestSize.Level1)
123 {
124     DisableUsbPlugin plugin;
125     std::string adminName{"testAdminName"};
126     bool policyData = false;
127     ErrCode ret = plugin.OnAdminRemove(adminName, policyData, DEFAULT_USER_ID);
128     ASSERT_TRUE(ret == ERR_OK);
129 }
130 } // namespace TEST
131 } // namespace EDM
132 } // namespace OHOS