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 "disallow_modify_datetime_plugin_test.h"
17 #include "disallow_modify_datetime_plugin.h"
18 #include "edm_ipc_interface_code.h"
19 #include "iplugin.h"
20 #include "utils.h"
21 
22 using namespace testing::ext;
23 
24 namespace OHOS {
25 namespace EDM {
26 namespace TEST {
SetUpTestSuite(void)27 void DisallowModifyDateTimePluginTest::SetUpTestSuite(void)
28 {
29     Utils::SetEdmInitialEnv();
30 }
31 
TearDownTestSuite(void)32 void DisallowModifyDateTimePluginTest::TearDownTestSuite(void)
33 {
34     Utils::ResetTokenTypeAndUid();
35     ASSERT_TRUE(Utils::IsOriginalUTEnv());
36     std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
37 }
38 
39 /**
40  * @tc.name: TestDisallowModifyDateTimePlugin
41  * @tc.desc: Test TestDisallowModifyDateTimePlugin::OnSetPolicy function.
42  * @tc.type: FUNC
43  */
44 HWTEST_F(DisallowModifyDateTimePluginTest, TestDisallowModifyDateTimePlugin001, TestSize.Level1)
45 {
46     MessageParcel data;
47     MessageParcel reply;
48     // want to disallow to modify date time.
49     data.WriteBool(true);
50     std::shared_ptr<IPlugin> plugin = DisallModifyDateTimePlugin::GetPlugin();
51     // origin policy is allow to modify date time.
52     std::uint32_t funcCode =
53         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISALLOW_MODIFY_DATETIME);
54     HandlePolicyData handlePolicyData{"false", false};
55     ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
56     ASSERT_TRUE(ret == ERR_OK);
57     // current policy is disallow to modify date time.
58     ASSERT_TRUE(handlePolicyData.policyData == "true");
59     ASSERT_TRUE(handlePolicyData.isChanged);
60 }
61 
62 /**
63  * @tc.name: TestDisallowModifyDateTimePlugin
64  * @tc.desc: Test TestDisallowModifyDateTimePlugin::OnGetPolicy function.
65  * @tc.type: FUNC
66  */
67 HWTEST_F(DisallowModifyDateTimePluginTest, TestDisallowModifyDateTimePlugin002, TestSize.Level1)
68 {
69     std::shared_ptr<IPlugin> plugin = DisallModifyDateTimePlugin::GetPlugin();
70     // origin policy is disallow to modify date time.
71     std::string policyData{"true"};
72     MessageParcel data;
73     MessageParcel reply;
74     ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
75     int32_t flag = ERR_INVALID_VALUE;
76     ASSERT_TRUE(reply.ReadInt32(flag) && (flag == ERR_OK));
77     bool result = false;
78     reply.ReadBool(result);
79     ASSERT_TRUE(ret == ERR_OK);
80     // get policy is disallow to modify date time.
81     ASSERT_TRUE(result);
82 }
83 } // namespace TEST
84 } // namespace EDM
85 } // namespace OHOS