1 /*
2 * Copyright (c) 2022-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 #include <string>
18 #include <system_ability_definition.h>
19
20 #include "datetime_manager_proxy.h"
21 #include "edm_sys_manager_mock.h"
22 #include "enterprise_device_mgr_stub_mock.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 DatetimeManagerProxyTest : public testing::Test {
32 protected:
33 void SetUp() override;
34
35 void TearDown() override;
36
37 static void TearDownTestSuite(void);
38 std::shared_ptr<DatetimeManagerProxy> dateTimeManagerProxy;
39 std::shared_ptr<EdmSysManager> edmSysManager_ = nullptr;
40 sptr<EnterpriseDeviceMgrStubMock> object_ = nullptr;
41 };
42
SetUp()43 void DatetimeManagerProxyTest::SetUp()
44 {
45 dateTimeManagerProxy = DatetimeManagerProxy::GetDatetimeManagerProxy();
46 edmSysManager_ = std::make_shared<EdmSysManager>();
47 object_ = new (std::nothrow) EnterpriseDeviceMgrStubMock();
48 edmSysManager_->RegisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID, object_);
49 Utils::SetEdmServiceEnable();
50 }
51
TearDown()52 void DatetimeManagerProxyTest::TearDown()
53 {
54 if (dateTimeManagerProxy) {
55 dateTimeManagerProxy.reset();
56 }
57 edmSysManager_->UnregisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID);
58 object_ = nullptr;
59 Utils::SetEdmServiceDisable();
60 }
61
TearDownTestSuite()62 void DatetimeManagerProxyTest::TearDownTestSuite()
63 {
64 ASSERT_FALSE(Utils::GetEdmServiceState());
65 std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl;
66 }
67
68 /**
69 * @tc.name: TestSetDateTimeSuc
70 * @tc.desc: Test SetDateTime success func.
71 * @tc.type: FUNC
72 */
73 HWTEST_F(DatetimeManagerProxyTest, TestSetDateTimeSuc, TestSize.Level1)
74 {
75 AppExecFwk::ElementName admin;
76 admin.SetBundleName("com.edm.test.demo");
77 admin.SetAbilityName("com.edm.test.demo.Ability");
78 int64_t time = 1674365400000;
79 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
80 .Times(1)
81 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
82 MessageParcel data;
83 data.WriteParcelable(&admin);
84 data.WriteString(EdmConstants::PERMISSION_TAG_VERSION_11);
85 data.WriteInt64(time);
86 int32_t ret = dateTimeManagerProxy->SetDateTime(data);
87 EXPECT_TRUE(ret == ERR_OK);
88 }
89
90 /**
91 * @tc.name: TestSetDateTimeFail
92 * @tc.desc: Test SetDateTime without enable edm service func.
93 * @tc.type: FUNC
94 */
95 HWTEST_F(DatetimeManagerProxyTest, TestSetDateTimeFail, TestSize.Level1)
96 {
97 Utils::SetEdmServiceDisable();
98 AppExecFwk::ElementName admin;
99 admin.SetBundleName("com.edm.test.demo");
100 admin.SetAbilityName("com.edm.test.demo.Ability");
101 int64_t time = 1674365400000;
102 MessageParcel data;
103 data.WriteParcelable(&admin);
104 data.WriteString(EdmConstants::PERMISSION_TAG_VERSION_11);
105 data.WriteInt64(time);
106 int32_t ret = dateTimeManagerProxy->SetDateTime(data);
107 EXPECT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
108 }
109
110 /**
111 * @tc.name: TestDisallowModifyDateTimeSuc
112 * @tc.desc: Test DisallowModifyDateTime success func.
113 * @tc.type: FUNC
114 */
115 HWTEST_F(DatetimeManagerProxyTest, TestDisallowModifyDateTimeSuc, TestSize.Level1)
116 {
117 OHOS::AppExecFwk::ElementName admin;
118 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
119 .Times(1)
120 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
121 MessageParcel data;
122 data.WriteParcelable(&admin);
123 data.WriteString(EdmConstants::PERMISSION_TAG_VERSION_11);
124 data.WriteBool(true);
125 ErrCode ret = dateTimeManagerProxy->DisallowModifyDateTime(data);
126 ASSERT_TRUE(ret == ERR_OK);
127 }
128
129 /**
130 * @tc.name: TestDisallowModifyDateTimeFail
131 * @tc.desc: Test DisallowModifyDateTime without enable edm service func.
132 * @tc.type: FUNC
133 */
134 HWTEST_F(DatetimeManagerProxyTest, TestDisallowModifyDateTimeFail, TestSize.Level1)
135 {
136 Utils::SetEdmServiceDisable();
137 MessageParcel data;
138 ErrCode ret = dateTimeManagerProxy->DisallowModifyDateTime(data);
139 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
140 }
141
142 /**
143 * @tc.name: TestIsModifyDateTimeDisallowed
144 * @tc.desc: Test IsModifyDateTimeDisallowed success func if have Admin parameter.
145 * @tc.type: FUNC
146 */
147 HWTEST_F(DatetimeManagerProxyTest, TestIsModifyDateTimeDisallowed001, TestSize.Level1)
148 {
149 OHOS::AppExecFwk::ElementName admin;
150 bool result = true;
151 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
152 .Times(1)
153 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestGetPolicy));
154 MessageParcel data;
155 data.WriteParcelable(&admin);
156 data.WriteString(EdmConstants::PERMISSION_TAG_VERSION_11);
157 ErrCode ret = dateTimeManagerProxy->IsModifyDateTimeDisallowed(data, result);
158 ASSERT_TRUE(ret == ERR_OK);
159 }
160
161 /**
162 * @tc.name: TestIsModifyDateTimeDisallowed
163 * @tc.desc: Test IsModifyDateTimeDisallowed func if have Admin parameter without enable edm service.
164 * @tc.type: FUNC
165 */
166 HWTEST_F(DatetimeManagerProxyTest, TestIsModifyDateTimeDisallowed002, TestSize.Level1)
167 {
168 Utils::SetEdmServiceDisable();
169 OHOS::AppExecFwk::ElementName admin;
170 bool result = true;
171 MessageParcel data;
172 ErrCode ret = dateTimeManagerProxy->IsModifyDateTimeDisallowed(data, result);
173 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
174 }
175
176 /**
177 * @tc.name: TestIsModifyDateTimeDisallowed
178 * @tc.desc: Test IsModifyDateTimeDisallowed success func if does not have Admin parameter.
179 * @tc.type: FUNC
180 */
181 HWTEST_F(DatetimeManagerProxyTest, TestIsModifyDateTimeDisallowed003, TestSize.Level1)
182 {
183 bool result = false;
184 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
185 .Times(1)
186 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetPolicy));
187 MessageParcel data;
188 data.WriteInt32(WITHOUT_ADMIN);
189 ErrCode ret = dateTimeManagerProxy->IsModifyDateTimeDisallowed(data, result);
190 ASSERT_TRUE(ret == ERR_OK);
191 ASSERT_TRUE(result);
192 }
193
194 /**
195 * @tc.name: TestIsModifyDateTimeDisallowed
196 * @tc.desc: Test IsModifyDateTimeDisallowed func if does not have Admin parameter without enable edm service.
197 * @tc.type: FUNC
198 */
199 HWTEST_F(DatetimeManagerProxyTest, TestIsModifyDateTimeDisallowed004, TestSize.Level1)
200 {
201 Utils::SetEdmServiceDisable();
202 bool result = false;
203 MessageParcel data;
204 const std::u16string descriptor = u"ohos.edm.testdemo";
205 data.WriteInterfaceToken(descriptor);
206 data.WriteInt32(WITHOUT_USERID);
207 data.WriteString(EdmConstants::PERMISSION_TAG_VERSION_11);
208 data.WriteInt32(WITHOUT_ADMIN);
209 ErrCode ret = dateTimeManagerProxy->IsModifyDateTimeDisallowed(data, result);
210 ASSERT_TRUE(ret == ERR_OK);
211 ASSERT_FALSE(result);
212 }
213 } // namespace TEST
214 } // namespace EDM
215 } // namespace OHOS
216