1 /*
2 * Copyright (c) 2023-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 <gtest/gtest.h>
17 #include <string>
18 #include <system_ability_definition.h>
19
20 #include "account_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 const std::string ADMIN_PACKAGENAME = "com.edm.test.demo";
32 class AccountManagerProxyTest : public testing::Test {
33 protected:
34 void SetUp() override;
35
36 void TearDown() override;
37
38 static void TearDownTestSuite(void);
39 std::shared_ptr<AccountManagerProxy> accountManagerProxy = nullptr;
40 std::shared_ptr<EdmSysManager> edmSysManager_ = nullptr;
41 sptr<EnterpriseDeviceMgrStubMock> object_ = nullptr;
42 };
43
SetUp()44 void AccountManagerProxyTest::SetUp()
45 {
46 accountManagerProxy = AccountManagerProxy::GetAccountManagerProxy();
47 edmSysManager_ = std::make_shared<EdmSysManager>();
48 object_ = new (std::nothrow) EnterpriseDeviceMgrStubMock();
49 edmSysManager_->RegisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID, object_);
50 Utils::SetEdmServiceEnable();
51 }
52
TearDown()53 void AccountManagerProxyTest::TearDown()
54 {
55 accountManagerProxy.reset();
56 edmSysManager_->UnregisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID);
57 object_ = nullptr;
58 Utils::SetEdmServiceDisable();
59 }
60
TearDownTestSuite()61 void AccountManagerProxyTest::TearDownTestSuite()
62 {
63 ASSERT_FALSE(Utils::GetEdmServiceState());
64 std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl;
65 }
66
67 /**
68 * @tc.name: TestDisallowAddLocalAccountSuc
69 * @tc.desc: Test DisallowAddLocalAccount success func.
70 * @tc.type: FUNC
71 */
72 HWTEST_F(AccountManagerProxyTest, TestDisallowAddLocalAccountSuc, TestSize.Level1)
73 {
74 OHOS::AppExecFwk::ElementName admin;
75 bool isDisallow = true;
76 MessageParcel data;
77 data.WriteParcelable(&admin);
78 data.WriteBool(isDisallow);
79 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
80 .Times(1)
81 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
82 ErrCode ret = accountManagerProxy->DisallowAddLocalAccount(data);
83 ASSERT_TRUE(ret == ERR_OK);
84 }
85
86 /**
87 * @tc.name: TestDisallowAddLocalAccountFail
88 * @tc.desc: Test DisallowAddLocalAccount without enable edm service func.
89 * @tc.type: FUNC
90 */
91 HWTEST_F(AccountManagerProxyTest, TestDisallowAddLocalAccountFail, TestSize.Level1)
92 {
93 Utils::SetEdmServiceDisable();
94 OHOS::AppExecFwk::ElementName admin;
95 bool isDisallow = true;
96 MessageParcel data;
97 data.WriteParcelable(&admin);
98 data.WriteBool(isDisallow);
99 ErrCode ret = accountManagerProxy->DisallowAddLocalAccount(data);
100 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
101 }
102
103 /**
104 * @tc.name: TestIsAddLocalAccountDisallowedSuc
105 * @tc.desc: Test IsAddLocalAccountDisallowed success.
106 * @tc.type: FUNC
107 */
108 HWTEST_F(AccountManagerProxyTest, TestIsAddLocalAccountDisallowedSuc, TestSize.Level1)
109 {
110 OHOS::AppExecFwk::ElementName admin;
111 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
112 .Times(1)
113 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetPolicy));
114 bool isDisabled = false;
115 ErrCode ret = accountManagerProxy->IsAddLocalAccountDisallowed(&admin, isDisabled);
116 ASSERT_TRUE(ret == ERR_OK);
117 ASSERT_TRUE(isDisabled);
118 }
119
120 /**
121 * @tc.name: TestIsAddLocalAccountDisallowedFail
122 * @tc.desc: Test IsAddLocalAccountDisallowed without enable edm service.
123 * @tc.type: FUNC
124 */
125 HWTEST_F(AccountManagerProxyTest, TestIsAddLocalAccountDisallowedFail, TestSize.Level1)
126 {
127 Utils::SetEdmServiceDisable();
128 OHOS::AppExecFwk::ElementName admin;
129 bool isDisabled = false;
130 ErrCode ret = accountManagerProxy->IsAddLocalAccountDisallowed(&admin, isDisabled);
131 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
132 }
133
134 /**
135 * @tc.name: TestDisallowAddOsAccountByUserSuc
136 * @tc.desc: Test DisallowAddOsAccountByUser success.
137 * @tc.type: FUNC
138 */
139 HWTEST_F(AccountManagerProxyTest, TestDisallowAddOsAccountByUserSuc, TestSize.Level1)
140 {
141 OHOS::AppExecFwk::ElementName admin;
142 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
143 .Times(1)
144 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
145 std::vector<std::string> key {std::to_string(DEFAULT_USER_ID)};
146 std::vector<std::string> value {"true"};
147 MessageParcel data;
148 data.WriteParcelable(&admin);
149 data.WriteStringVector(key);
150 data.WriteStringVector(value);
151 ErrCode ret = accountManagerProxy->DisallowAddOsAccountByUser(data);
152 ASSERT_TRUE(ret == ERR_OK);
153 }
154
155 /**
156 * @tc.name: TestDisallowAddOsAccountByUserFail
157 * @tc.desc: Test DisallowAddOsAccountByUser without enable edm service.
158 * @tc.type: FUNC
159 */
160 HWTEST_F(AccountManagerProxyTest, TestDisallowAddOsAccountByUserFail, TestSize.Level1)
161 {
162 Utils::SetEdmServiceDisable();
163 OHOS::AppExecFwk::ElementName admin;
164 std::vector<std::string> key {std::to_string(DEFAULT_USER_ID)};
165 std::vector<std::string> value {"true"};
166 MessageParcel data;
167 data.WriteParcelable(&admin);
168 data.WriteStringVector(key);
169 data.WriteStringVector(value);
170 ErrCode ret = accountManagerProxy->DisallowAddOsAccountByUser(data);
171 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
172 }
173
174 /**
175 * @tc.name: TestIsAddOsAccountByUserDisallowedSuc
176 * @tc.desc: Test IsAddOsAccountByUserDisallowed success.
177 * @tc.type: FUNC
178 */
179 HWTEST_F(AccountManagerProxyTest, TestIsAddOsAccountByUserDisallowedSuc, TestSize.Level1)
180 {
181 OHOS::AppExecFwk::ElementName admin;
182 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
183 .Times(1)
184 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetPolicy));
185 bool isDisabled = false;
186 ErrCode ret = accountManagerProxy->IsAddOsAccountByUserDisallowed(&admin, DEFAULT_USER_ID, isDisabled);
187 ASSERT_TRUE(ret == ERR_OK);
188 ASSERT_TRUE(isDisabled);
189 }
190
191 /**
192 * @tc.name: TestIsAddOsAccountByUserDisallowedFail
193 * @tc.desc: Test IsAddOsAccountByUserDisallowed without enable edm service.
194 * @tc.type: FUNC
195 */
196 HWTEST_F(AccountManagerProxyTest, TestIsAddOsAccountByUserDisallowedFail, TestSize.Level1)
197 {
198 Utils::SetEdmServiceDisable();
199 OHOS::AppExecFwk::ElementName admin;
200 bool isDisabled = false;
201 ErrCode ret = accountManagerProxy->IsAddOsAccountByUserDisallowed(&admin, DEFAULT_USER_ID, isDisabled);
202 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
203 }
204
205 /**
206 * @tc.name: TestIsAddOsAccountByUserDisallowedSuc
207 * @tc.desc: Test IsAddOsAccountByUserDisallowed success.
208 * @tc.type: FUNC
209 */
210 HWTEST_F(AccountManagerProxyTest, TestIsAddOsAccountByUserDisallowedSuc001, TestSize.Level1)
211 {
212 OHOS::AppExecFwk::ElementName admin;
213 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
214 .Times(1)
215 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetPolicy));
216 MessageParcel data;
217 data.WriteParcelable(&admin);
218 data.WriteInt32(DEFAULT_USER_ID);
219 bool isDisabled = false;
220 ErrCode ret = accountManagerProxy->IsAddOsAccountByUserDisallowed(data, isDisabled);
221 ASSERT_TRUE(ret == ERR_OK);
222 ASSERT_TRUE(isDisabled);
223 }
224
225 /**
226 * @tc.name: TestIsAddOsAccountByUserDisallowedFail
227 * @tc.desc: Test IsAddOsAccountByUserDisallowed without enable edm service.
228 * @tc.type: FUNC
229 */
230 HWTEST_F(AccountManagerProxyTest, TestIsAddOsAccountByUserDisallowedFail001, TestSize.Level1)
231 {
232 Utils::SetEdmServiceDisable();
233 OHOS::AppExecFwk::ElementName admin;
234 MessageParcel data;
235 data.WriteParcelable(&admin);
236 data.WriteInt32(DEFAULT_USER_ID);
237 bool isDisabled = false;
238 ErrCode ret = accountManagerProxy->IsAddOsAccountByUserDisallowed(data, isDisabled);
239 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
240 }
241
242 /**
243 * @tc.name: TestIsAddOsAccountByUserDisallowedFail
244 * @tc.desc: Test IsAddOsAccountByUserDisallowed func if does not have Admin parameter without enable edm service.
245 * @tc.type: FUNC
246 */
247 HWTEST_F(AccountManagerProxyTest, TestIsAddOsAccountByUserDisallowedFail002, TestSize.Level1)
248 {
249 Utils::SetEdmServiceDisable();
250 bool result = false;
251 MessageParcel data;
252 const std::u16string descriptor = u"ohos.edm.testdemo";
253 data.WriteInterfaceToken(descriptor);
254 data.WriteInt32(WITHOUT_USERID);
255 data.WriteString(WITHOUT_PERMISSION_TAG);
256 data.WriteInt32(WITHOUT_ADMIN);
257 ErrCode ret = accountManagerProxy->IsAddOsAccountByUserDisallowed(data, result);
258 ASSERT_TRUE(ret == ERR_OK);
259 ASSERT_FALSE(result);
260 }
261
262 /**
263 * @tc.name: TestAddOsAccountSuc
264 * @tc.desc: Test AddOsAccount success.
265 * @tc.type: FUNC
266 */
267 HWTEST_F(AccountManagerProxyTest, TestAddOsAccountSuc, TestSize.Level1)
268 {
269 OHOS::AppExecFwk::ElementName admin;
270 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
271 .Times(1)
272 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeAccountProxySendRequestAddOsAccount));
273 std::string name = "ut_user_1";
274 int32_t type = 1;
275 OHOS::AccountSA::OsAccountInfo accountInfo;
276 ErrCode ret = accountManagerProxy->AddOsAccount(admin, name, type, accountInfo);
277 ASSERT_TRUE(ret == ERR_OK);
278 GTEST_LOG_(INFO) << "AccountManagerProxyTest TestAddOsAccountSuc code :" << accountInfo.GetLocalName();
279 ASSERT_TRUE(accountInfo.GetLocalName() == RETURN_STRING);
280 }
281
282 /**
283 * @tc.name: TestAddOsAccountFail
284 * @tc.desc: Test AddOsAccount without enable edm service.
285 * @tc.type: FUNC
286 */
287 HWTEST_F(AccountManagerProxyTest, TestAddOsAccountFail, TestSize.Level1)
288 {
289 Utils::SetEdmServiceDisable();
290 OHOS::AppExecFwk::ElementName admin;
291 std::string name = "ut_user_2";
292 int32_t type = 1;
293 OHOS::AccountSA::OsAccountInfo accountInfo;
294 ErrCode ret = accountManagerProxy->AddOsAccount(admin, name, type, accountInfo);
295 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
296 ASSERT_TRUE(accountInfo.GetLocalName().empty());
297 }
298 } // namespace TEST
299 } // namespace EDM
300 } // namespace OHOS
301