1 /*
2 * Copyright (c) 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 #define private public
17 #include "add_os_account_plugin.h"
18 #undef private
19
20 #include "edm_ipc_interface_code.h"
21 #include "edm_os_account_manager_impl_mock.h"
22 #include "external_manager_factory_mock.h"
23 #include "os_account_info.h"
24 #include "os_account_manager.h"
25 #include "utils.h"
26 #include <gtest/gtest.h>
27
28 using namespace testing;
29 using namespace testing::ext;
30
31 namespace OHOS {
32 namespace EDM {
33 namespace TEST {
34 const int32_t ACCOUNT_NAME_LENGTH_MAX = 1024;
35
36 class AddOsAccountAccountPluginTest : public testing::Test {
37 public:
38 static void SetUpTestSuite(void);
39
40 static void TearDownTestSuite(void);
41
42 protected:
43 static std::shared_ptr<ExternalManagerFactoryMock> factoryMock_;
44
45 static std::shared_ptr<EdmOsAccountManagerImplMock> osAccountMgrMock_;
46 };
47
48 std::shared_ptr<ExternalManagerFactoryMock> AddOsAccountAccountPluginTest::factoryMock_ = nullptr;
49 std::shared_ptr<EdmOsAccountManagerImplMock> AddOsAccountAccountPluginTest::osAccountMgrMock_ = nullptr;
50
SetUpTestSuite(void)51 void AddOsAccountAccountPluginTest::SetUpTestSuite(void)
52 {
53 Utils::SetEdmInitialEnv();
54 osAccountMgrMock_ = std::make_shared<EdmOsAccountManagerImplMock>();
55 factoryMock_ = std::make_shared<ExternalManagerFactoryMock>();
56 }
57
TearDownTestSuite(void)58 void AddOsAccountAccountPluginTest::TearDownTestSuite(void)
59 {
60 Utils::ResetTokenTypeAndUid();
61 ASSERT_TRUE(Utils::IsOriginalUTEnv());
62 std::cout << "now ut process is original ut env : " << Utils::IsOriginalUTEnv() << std::endl;
63 }
64
65 /**
66 * @tc.name: TestOnSetPolicyEmpty
67 * @tc.desc: Test AddOsAccountPlugin::OnSetPolicy function when policy is empty.
68 * @tc.type: FUNC
69 */
70 HWTEST_F(AddOsAccountAccountPluginTest, TestOnSetPolicyEmpty, TestSize.Level1)
71 {
72 AddOsAccountPlugin plugin;
73 MessageParcel reply;
74 std::map<std::string, std::string> policies;
75 ErrCode ret = plugin.OnSetPolicy(policies, reply);
76 ASSERT_TRUE(ret == ERR_OK);
77 }
78
79 /**
80 * @tc.name: TestOnSetPolicyNameUnavailable
81 * @tc.desc: Test AddOsAccountPlugin::OnSetPolicy when account name value is empty.
82 * @tc.type: FUNC
83 */
84 HWTEST_F(AddOsAccountAccountPluginTest, TestOnSetPolicyNameUnavailable, TestSize.Level1)
85 {
86 AddOsAccountPlugin plugin;
87 MessageParcel reply;
88 std::map<std::string, std::string> policies;
89 std::string name = "abc";
90 for (int i = 0; i < ACCOUNT_NAME_LENGTH_MAX; i++) {
91 name.append("z");
92 }
93 policies.insert(std::make_pair(name, "1"));
94 ErrCode ret = plugin.OnSetPolicy(policies, reply);
95 ASSERT_TRUE(ret == EdmReturnErrCode::ADD_OS_ACCOUNT_FAILED);
96 }
97
98 /**
99 * @tc.name: TestOnSetPolicyTypeEmpty
100 * @tc.desc: Test AddOsAccountPlugin::OnSetPolicy when account type value is empty.
101 * @tc.type: FUNC
102 */
103 HWTEST_F(AddOsAccountAccountPluginTest, TestOnSetPolicyTypeEmpty, TestSize.Level1)
104 {
105 AddOsAccountPlugin plugin;
106 MessageParcel reply;
107 std::map<std::string, std::string> policies;
108 policies.insert(std::make_pair("ut_test_user_name", ""));
109 ErrCode ret = plugin.OnSetPolicy(policies, reply);
110 ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
111 }
112
113 /**
114 * @tc.name: TestOnSetPolicyTypeUnavailable
115 * @tc.desc: Test AddOsAccountPlugin::OnSetPolicy when account type value is empty.
116 * @tc.type: FUNC
117 */
118 HWTEST_F(AddOsAccountAccountPluginTest, TestOnSetPolicyTypeUnavailable, TestSize.Level1)
119 {
120 AddOsAccountPlugin plugin;
121 MessageParcel reply;
122 std::map<std::string, std::string> policies;
123 policies.insert(std::make_pair("ut_test_user_name",
124 std::to_string(static_cast<int>(AccountSA::OsAccountType::END))));
125 ErrCode ret = plugin.OnSetPolicy(policies, reply);
126 ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
127 }
128
129 /**
130 * @tc.name: TestOnSetPolicyAddAdmin
131 * @tc.desc: Test AddOsAccountPlugin::OnSetPolicy to add an admin account.
132 * @tc.type: FUNC
133 */
134 HWTEST_F(AddOsAccountAccountPluginTest, TestOnSetPolicyAddAdmin, TestSize.Level1)
135 {
136 AddOsAccountPlugin plugin;
137 auto factory = std::make_shared<ExternalManagerFactoryMock>();
138 auto osAccountMgrMock = std::make_shared<EdmOsAccountManagerImplMock>();
139 plugin.externalManagerFactory_ = factory;
140 MessageParcel reply;
141 std::map<std::string, std::string> policies;
142 policies.insert(std::make_pair("ut_test_user_name1", "0"));
143 EXPECT_CALL(*factory, CreateOsAccountManager).Times(1).WillOnce(Return(osAccountMgrMock));
144 EXPECT_CALL(*osAccountMgrMock, CreateOsAccount).Times(1).WillOnce(Return(ERR_OK));
145 ErrCode ret = plugin.OnSetPolicy(policies, reply);
146 ASSERT_TRUE(ret == ERR_OK);
147 }
148
149 /**
150 * @tc.name: TestOnSetPolicyAddNormal
151 * @tc.desc: Test AddOsAccountPlugin::OnSetPolicy to add an normal account.
152 * @tc.type: FUNC
153 */
154 HWTEST_F(AddOsAccountAccountPluginTest, TestOnSetPolicyAddNormal, TestSize.Level1)
155 {
156 AddOsAccountPlugin plugin;
157 auto factory = std::make_shared<ExternalManagerFactoryMock>();
158 auto osAccountMgrMock = std::make_shared<EdmOsAccountManagerImplMock>();
159 plugin.externalManagerFactory_ = factory;
160 MessageParcel reply;
161 std::map<std::string, std::string> policies;
162 policies.insert(std::make_pair("ut_test_user_name2", "1"));
163 EXPECT_CALL(*factory, CreateOsAccountManager).Times(1).WillOnce(Return(osAccountMgrMock));
164 EXPECT_CALL(*osAccountMgrMock, CreateOsAccount).Times(1).WillOnce(Return(ERR_OK));
165 ErrCode ret = plugin.OnSetPolicy(policies, reply);
166 ASSERT_TRUE(ret == ERR_OK);
167 }
168
169 /**
170 * @tc.name: TestOnSetPolicyAddGuest
171 * @tc.desc: Test AddOsAccountPlugin::OnSetPolicy to add an guest account.
172 * @tc.type: FUNC
173 */
174 HWTEST_F(AddOsAccountAccountPluginTest, TestOnSetPolicyAddGuest, TestSize.Level1)
175 {
176 AddOsAccountPlugin plugin;
177 auto factory = std::make_shared<ExternalManagerFactoryMock>();
178 auto osAccountMgrMock = std::make_shared<EdmOsAccountManagerImplMock>();
179 plugin.externalManagerFactory_ = factory;
180 MessageParcel reply;
181 std::map<std::string, std::string> policies;
182 policies.insert(std::make_pair("ut_test_user_name3", "2"));
183 EXPECT_CALL(*factory, CreateOsAccountManager).Times(1).WillOnce(Return(osAccountMgrMock));
184 EXPECT_CALL(*osAccountMgrMock, CreateOsAccount).Times(1).WillOnce(Return(ERR_OK));
185 ErrCode ret = plugin.OnSetPolicy(policies, reply);
186 ASSERT_TRUE(ret == ERR_OK);
187 }
188
189 /**
190 * @tc.name: TestOnSetPolicyAddFail
191 * @tc.desc: Test AddOsAccountPlugin::OnSetPolicy to add an guest account fail.
192 * @tc.type: FUNC
193 */
194 HWTEST_F(AddOsAccountAccountPluginTest, TestOnSetPolicyAddFail, TestSize.Level1)
195 {
196 AddOsAccountPlugin plugin;
197 auto factory = std::make_shared<ExternalManagerFactoryMock>();
198 auto osAccountMgrMock = std::make_shared<EdmOsAccountManagerImplMock>();
199 plugin.externalManagerFactory_ = factory;
200 MessageParcel reply;
201 std::map<std::string, std::string> policies;
202 policies.insert(std::make_pair("ut_test_user_name3", "2"));
203 EXPECT_CALL(*factory, CreateOsAccountManager).Times(1).WillOnce(Return(osAccountMgrMock));
204 EXPECT_CALL(*osAccountMgrMock, CreateOsAccount).Times(1).WillOnce(Return(EdmReturnErrCode::ADD_OS_ACCOUNT_FAILED));
205 ErrCode ret = plugin.OnSetPolicy(policies, reply);
206 ASSERT_TRUE(ret == EdmReturnErrCode::ADD_OS_ACCOUNT_FAILED);
207 }
208 } // namespace TEST
209 } // namespace EDM
210 } // namespace OHOS