1 /*
2  * Copyright (c) 2023 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 
18 #include "os_account_manager.h"
19 
20 #include "edm_constants.h"
21 #include "edm_ipc_interface_code.h"
22 #include "operate_device_plugin.h"
23 #include "plugin_singleton.h"
24 #include "utils.h"
25 
26 using namespace testing::ext;
27 
28 namespace OHOS {
29 namespace EDM {
30 namespace TEST {
31 class OperateDevicePluginTest : public testing::Test {
32 protected:
33     static void SetUpTestSuite(void);
34 
35     static void TearDownTestSuite(void);
36 };
37 
SetUpTestSuite(void)38 void OperateDevicePluginTest::SetUpTestSuite(void)
39 {
40     Utils::SetEdmInitialEnv();
41 }
42 
TearDownTestSuite(void)43 void OperateDevicePluginTest::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: TestLockScreenFail
52  * @tc.desc: Test OperateDevicePlugin::OnSetPolicy function fail.
53  * @tc.type: FUNC
54  */
55 HWTEST_F(OperateDevicePluginTest, TestLockScreenFail, TestSize.Level1)
56 {
57     OperateDevicePlugin plugin;
58     int32_t userId = 0;
59     OperateDeviceParam param{EdmConstants::DeviceControl::LOCK_SCREEN, "", userId};
60     MessageParcel reply;
61     AccountSA::OsAccountManager::GetOsAccountLocalIdFromProcess(userId);
62     uint64_t selfTokenId = GetSelfTokenID();
63     SetSelfTokenID(0);
64     ErrCode ret = plugin.OnSetPolicy(param, reply);
65     SetSelfTokenID(selfTokenId);
66     ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
67 }
68 /**
69  * @tc.name: TestLockScreenSuccess
70  * @tc.desc: Test OperateDevicePlugin::OnSetPolicy function success.
71  * @tc.type: FUNC
72  */
73 HWTEST_F(OperateDevicePluginTest, TestLockScreenSuccess, TestSize.Level1)
74 {
75     OperateDevicePlugin plugin;
76     int32_t userId = 0;
77     OperateDeviceParam param{EdmConstants::DeviceControl::LOCK_SCREEN, "", userId};
78     MessageParcel reply;
79     AccountSA::OsAccountManager::GetOsAccountLocalIdFromProcess(userId);
80     ErrCode ret = plugin.OnSetPolicy(param, reply);
81     ASSERT_TRUE(ret == ERR_OK);
82 }
83 
84 /**
85  * @tc.name: TestShutdown
86  * @tc.desc: Test OperateDevicePlugin::OnSetPolicy function fail.
87  * @tc.type: FUNC
88  */
89 HWTEST_F(OperateDevicePluginTest, TestShutdown, TestSize.Level1)
90 {
91     uint64_t selfTokenId = GetSelfTokenID();
92     SetSelfTokenID(0);
93     OperateDevicePlugin plugin;
94     int32_t userId = 0;
95     OperateDeviceParam param{EdmConstants::DeviceControl::SHUT_DOWN, "", userId};
96     MessageParcel reply;
97     ErrCode ret = plugin.OnSetPolicy(param, reply);
98     ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
99     SetSelfTokenID(selfTokenId);
100 }
101 
102 /**
103  * @tc.name: TestReboot
104  * @tc.desc: Test OperateDevicePlugin::OnSetPolicy function fail.
105  * @tc.type: FUNC
106  */
107 HWTEST_F(OperateDevicePluginTest, TestReboot, TestSize.Level1)
108 {
109     uint64_t selfTokenId = GetSelfTokenID();
110     SetSelfTokenID(0);
111     OperateDevicePlugin plugin;
112     int32_t userId = 0;
113     OperateDeviceParam param{EdmConstants::DeviceControl::REBOOT, "", userId};
114     MessageParcel reply;
115     ErrCode ret = plugin.OnSetPolicy(param, reply);
116     ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
117     SetSelfTokenID(selfTokenId);
118 }
119 
120 /**
121  * @tc.name: TestResetFactory
122  * @tc.desc: Test OperateDevicePlugin::OnSetPolicy function fail.
123  * @tc.type: FUNC
124  */
125 HWTEST_F(OperateDevicePluginTest, TestResetFactory, TestSize.Level1)
126 {
127     uint64_t selfTokenId = GetSelfTokenID();
128     SetSelfTokenID(0);
129     OperateDevicePlugin plugin;
130     int32_t userId = 0;
131     OperateDeviceParam param{EdmConstants::DeviceControl::RESET_FACTORY, "", userId};
132     MessageParcel reply;
133     ErrCode ret = plugin.OnSetPolicy(param, reply);
134     ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
135     SetSelfTokenID(selfTokenId);
136 }
137 } // namespace TEST
138 } // namespace EDM
139 } // namespace OHOS
140