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 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
76 .Times(1)
77 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
78 ErrCode ret = accountManagerProxy->DisallowAddLocalAccount(admin, true);
79 ASSERT_TRUE(ret == ERR_OK);
80 }
81
82 /**
83 * @tc.name: TestDisallowAddLocalAccountFail
84 * @tc.desc: Test DisallowAddLocalAccount without enable edm service func.
85 * @tc.type: FUNC
86 */
87 HWTEST_F(AccountManagerProxyTest, TestDisallowAddLocalAccountFail, TestSize.Level1)
88 {
89 Utils::SetEdmServiceDisable();
90 OHOS::AppExecFwk::ElementName admin;
91 ErrCode ret = accountManagerProxy->DisallowAddLocalAccount(admin, true);
92 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
93 }
94
95 /**
96 * @tc.name: TestIsAddLocalAccountDisallowedSuc
97 * @tc.desc: Test IsAddLocalAccountDisallowed success.
98 * @tc.type: FUNC
99 */
100 HWTEST_F(AccountManagerProxyTest, TestIsAddLocalAccountDisallowedSuc, TestSize.Level1)
101 {
102 OHOS::AppExecFwk::ElementName admin;
103 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
104 .Times(1)
105 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetPolicy));
106 bool isDisabled = false;
107 ErrCode ret = accountManagerProxy->IsAddLocalAccountDisallowed(&admin, isDisabled);
108 ASSERT_TRUE(ret == ERR_OK);
109 ASSERT_TRUE(isDisabled);
110 }
111
112 /**
113 * @tc.name: TestIsAddLocalAccountDisallowedFail
114 * @tc.desc: Test IsAddLocalAccountDisallowed without enable edm service.
115 * @tc.type: FUNC
116 */
117 HWTEST_F(AccountManagerProxyTest, TestIsAddLocalAccountDisallowedFail, TestSize.Level1)
118 {
119 Utils::SetEdmServiceDisable();
120 OHOS::AppExecFwk::ElementName admin;
121 bool isDisabled = false;
122 ErrCode ret = accountManagerProxy->IsAddLocalAccountDisallowed(&admin, isDisabled);
123 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
124 }
125
126 /**
127 * @tc.name: TestDisallowAddOsAccountByUserSuc
128 * @tc.desc: Test DisallowAddOsAccountByUser success.
129 * @tc.type: FUNC
130 */
131 HWTEST_F(AccountManagerProxyTest, TestDisallowAddOsAccountByUserSuc, TestSize.Level1)
132 {
133 OHOS::AppExecFwk::ElementName admin;
134 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
135 .Times(1)
136 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
137 ErrCode ret = accountManagerProxy->DisallowAddOsAccountByUser(admin, DEFAULT_USER_ID, true);
138 ASSERT_TRUE(ret == ERR_OK);
139 }
140
141 /**
142 * @tc.name: TestDisallowAddOsAccountByUserFail
143 * @tc.desc: Test DisallowAddOsAccountByUser without enable edm service.
144 * @tc.type: FUNC
145 */
146 HWTEST_F(AccountManagerProxyTest, TestDisallowAddOsAccountByUserFail, TestSize.Level1)
147 {
148 Utils::SetEdmServiceDisable();
149 OHOS::AppExecFwk::ElementName admin;
150 ErrCode ret = accountManagerProxy->DisallowAddOsAccountByUser(admin, DEFAULT_USER_ID, true);
151 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
152 }
153
154 /**
155 * @tc.name: TestIsAddOsAccountByUserDisallowedSuc
156 * @tc.desc: Test IsAddOsAccountByUserDisallowed success.
157 * @tc.type: FUNC
158 */
159 HWTEST_F(AccountManagerProxyTest, TestIsAddOsAccountByUserDisallowedSuc, TestSize.Level1)
160 {
161 OHOS::AppExecFwk::ElementName admin;
162 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
163 .Times(1)
164 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetPolicy));
165 bool isDisabled = false;
166 ErrCode ret = accountManagerProxy->IsAddOsAccountByUserDisallowed(&admin, DEFAULT_USER_ID, isDisabled);
167 ASSERT_TRUE(ret == ERR_OK);
168 ASSERT_TRUE(isDisabled);
169 }
170
171 /**
172 * @tc.name: TestIsAddOsAccountByUserDisallowedFail
173 * @tc.desc: Test IsAddOsAccountByUserDisallowed without enable edm service.
174 * @tc.type: FUNC
175 */
176 HWTEST_F(AccountManagerProxyTest, TestIsAddOsAccountByUserDisallowedFail, TestSize.Level1)
177 {
178 Utils::SetEdmServiceDisable();
179 OHOS::AppExecFwk::ElementName admin;
180 bool isDisabled = false;
181 ErrCode ret = accountManagerProxy->IsAddOsAccountByUserDisallowed(&admin, DEFAULT_USER_ID, isDisabled);
182 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
183 }
184
185 /**
186 * @tc.name: TestAddOsAccountSuc
187 * @tc.desc: Test AddOsAccount success.
188 * @tc.type: FUNC
189 */
190 HWTEST_F(AccountManagerProxyTest, TestAddOsAccountSuc, TestSize.Level1)
191 {
192 OHOS::AppExecFwk::ElementName admin;
193 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
194 .Times(1)
195 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeAccountProxySendRequestAddOsAccount));
196 std::string name = "ut_user_1";
197 int32_t type = 1;
198 OHOS::AccountSA::OsAccountInfo accountInfo;
199 std::string distributedInfoName;
200 std::string distributedInfoId;
201 ErrCode ret = accountManagerProxy->AddOsAccount(admin, name, type, accountInfo, distributedInfoName,
202 distributedInfoId);
203 ASSERT_TRUE(ret == ERR_OK);
204 GTEST_LOG_(INFO) << "AccountManagerProxyTest TestAddOsAccountSuc code :" << accountInfo.GetLocalName();
205 ASSERT_TRUE(accountInfo.GetLocalName() == RETURN_STRING);
206 ASSERT_TRUE(distributedInfoName == RETURN_STRING);
207 ASSERT_TRUE(distributedInfoId == RETURN_STRING);
208 }
209
210 /**
211 * @tc.name: TestAddOsAccountFail
212 * @tc.desc: Test AddOsAccount without enable edm service.
213 * @tc.type: FUNC
214 */
215 HWTEST_F(AccountManagerProxyTest, TestAddOsAccountFail, TestSize.Level1)
216 {
217 Utils::SetEdmServiceDisable();
218 OHOS::AppExecFwk::ElementName admin;
219 std::string name = "ut_user_2";
220 int32_t type = 1;
221 OHOS::AccountSA::OsAccountInfo accountInfo;
222 std::string distributedInfoName;
223 std::string distributedInfoId;
224 ErrCode ret = accountManagerProxy->AddOsAccount(admin, name, type, accountInfo, distributedInfoName,
225 distributedInfoId);
226 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
227 ASSERT_TRUE(accountInfo.GetLocalName().empty());
228 ASSERT_TRUE(distributedInfoName.empty());
229 ASSERT_TRUE(distributedInfoId.empty());
230 }
231 } // namespace TEST
232 } // namespace EDM
233 } // namespace OHOS
234