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 #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: TestDisallowAddOsAccountByUserSuc
97 * @tc.desc: Test DisallowAddOsAccountByUser success.
98 * @tc.type: FUNC
99 */
100 HWTEST_F(AccountManagerProxyTest, TestDisallowAddOsAccountByUserSuc, TestSize.Level1)
101 {
102 OHOS::AppExecFwk::ElementName admin;
103 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
104 .Times(1)
105 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
106 ErrCode ret = accountManagerProxy->DisallowAddOsAccountByUser(admin, DEFAULT_USER_ID, true);
107 ASSERT_TRUE(ret == ERR_OK);
108 }
109
110 /**
111 * @tc.name: TestDisallowAddOsAccountByUserFail
112 * @tc.desc: Test DisallowAddOsAccountByUser without enable edm service.
113 * @tc.type: FUNC
114 */
115 HWTEST_F(AccountManagerProxyTest, TestDisallowAddOsAccountByUserFail, TestSize.Level1)
116 {
117 Utils::SetEdmServiceDisable();
118 OHOS::AppExecFwk::ElementName admin;
119 ErrCode ret = accountManagerProxy->DisallowAddOsAccountByUser(admin, DEFAULT_USER_ID, true);
120 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
121 }
122
123 /**
124 * @tc.name: TestIsAddOsAccountByUserDisallowedSuc
125 * @tc.desc: Test IsAddOsAccountByUserDisallowed success.
126 * @tc.type: FUNC
127 */
128 HWTEST_F(AccountManagerProxyTest, TestIsAddOsAccountByUserDisallowedSuc, TestSize.Level1)
129 {
130 OHOS::AppExecFwk::ElementName admin;
131 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
132 .Times(1)
133 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetPolicy));
134 bool isDisabled = false;
135 ErrCode ret = accountManagerProxy->IsAddOsAccountByUserDisallowed(&admin, DEFAULT_USER_ID, isDisabled);
136 ASSERT_TRUE(ret == ERR_OK);
137 ASSERT_TRUE(isDisabled);
138 }
139
140 /**
141 * @tc.name: TestIsAddOsAccountByUserDisallowedFail
142 * @tc.desc: Test IsAddOsAccountByUserDisallowed without enable edm service.
143 * @tc.type: FUNC
144 */
145 HWTEST_F(AccountManagerProxyTest, TestIsAddOsAccountByUserDisallowedFail, TestSize.Level1)
146 {
147 Utils::SetEdmServiceDisable();
148 OHOS::AppExecFwk::ElementName admin;
149 bool isDisabled = false;
150 ErrCode ret = accountManagerProxy->IsAddOsAccountByUserDisallowed(&admin, DEFAULT_USER_ID, isDisabled);
151 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
152 }
153
154 /**
155 * @tc.name: TestAddOsAccountSuc
156 * @tc.desc: Test AddOsAccount success.
157 * @tc.type: FUNC
158 */
159 HWTEST_F(AccountManagerProxyTest, TestAddOsAccountSuc, TestSize.Level1)
160 {
161 OHOS::AppExecFwk::ElementName admin;
162 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
163 .Times(1)
164 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeAccountProxySendRequestAddOsAccount));
165 std::string name = "ut_user_1";
166 int32_t type = 1;
167 OHOS::AccountSA::OsAccountInfo accountInfo;
168 std::string distributedInfoName;
169 std::string distributedInfoId;
170 ErrCode ret = accountManagerProxy->AddOsAccount(admin, name, type, accountInfo, distributedInfoName,
171 distributedInfoId);
172 ASSERT_TRUE(ret == ERR_OK);
173 GTEST_LOG_(INFO) << "AccountManagerProxyTest TestAddOsAccountSuc code :" << accountInfo.GetLocalName();
174 ASSERT_TRUE(accountInfo.GetLocalName() == RETURN_STRING);
175 ASSERT_TRUE(distributedInfoName == RETURN_STRING);
176 ASSERT_TRUE(distributedInfoId == RETURN_STRING);
177 }
178
179 /**
180 * @tc.name: TestAddOsAccountFail
181 * @tc.desc: Test AddOsAccount without enable edm service.
182 * @tc.type: FUNC
183 */
184 HWTEST_F(AccountManagerProxyTest, TestAddOsAccountFail, TestSize.Level1)
185 {
186 Utils::SetEdmServiceDisable();
187 OHOS::AppExecFwk::ElementName admin;
188 std::string name = "ut_user_2";
189 int32_t type = 1;
190 OHOS::AccountSA::OsAccountInfo accountInfo;
191 std::string distributedInfoName;
192 std::string distributedInfoId;
193 ErrCode ret = accountManagerProxy->AddOsAccount(admin, name, type, accountInfo, distributedInfoName,
194 distributedInfoId);
195 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
196 ASSERT_TRUE(accountInfo.GetLocalName().empty());
197 ASSERT_TRUE(distributedInfoName.empty());
198 ASSERT_TRUE(distributedInfoId.empty());
199 }
200 } // namespace TEST
201 } // namespace EDM
202 } // namespace OHOS
203