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 "disable_sudo_plugin.h"
18 #include "edm_errors.h"
19 #include "edm_ipc_interface_code.h"
20 #include "iplugin_manager.h"
21 #include "plugin_singleton.h"
22 #include "utils.h"
23
24 using namespace testing::ext;
25 using namespace testing;
26
27 namespace OHOS {
28 namespace EDM {
29 namespace TEST {
30 class DisableSudoPluginTest : public testing::Test {
31 protected:
32 static void SetUpTestSuite(void);
33
34 static void TearDownTestSuite(void);
35 };
36
SetUpTestSuite(void)37 void DisableSudoPluginTest::SetUpTestSuite(void)
38 {
39 Utils::SetEdmInitialEnv();
40 }
41
TearDownTestSuite(void)42 void DisableSudoPluginTest::TearDownTestSuite(void)
43 {
44 Utils::ResetTokenTypeAndUid();
45 ASSERT_TRUE(Utils::IsOriginalUTEnv());
46 std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
47 }
48
49 /**
50 * @tc.name: TestDisableSudoPluginTestSet
51 * @tc.desc: Test DisableSudoPluginTest::OnSetPolicy function.
52 * @tc.type: FUNC
53 */
54 HWTEST_F(DisableSudoPluginTest, TestDisableSudoPluginTestSet, TestSize.Level1)
55 {
56 MessageParcel data;
57 MessageParcel reply;
58 data.WriteBool(true);
59 std::shared_ptr<IPlugin> plugin = DisableSudoPlugin::GetPlugin();
60 HandlePolicyData handlePolicyData{"false", "", false};
61 std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET,
62 EdmInterfaceCode::DISALLOWED_SUDO);
63 ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
64
65 ASSERT_TRUE(ret == ERR_OK);
66 ASSERT_TRUE(handlePolicyData.policyData == "true");
67 ASSERT_TRUE(handlePolicyData.isChanged);
68
69 // 恢复环境,取消禁用
70 MessageParcel dataFalse;
71 dataFalse.WriteBool(false);
72 HandlePolicyData handlePolicyDataFalse{"true", "", false};
73 ret = plugin->OnHandlePolicy(funcCode, dataFalse, reply, handlePolicyDataFalse, DEFAULT_USER_ID);
74 ASSERT_TRUE(ret == ERR_OK);
75 ASSERT_TRUE(handlePolicyDataFalse.policyData == "false");
76 ASSERT_FALSE(handlePolicyDataFalse.isChanged);
77 }
78 } // namespace TEST
79 } // namespace EDM
80 } // namespace OHOS