1 /*
2 * Copyright (c) 2021-2022 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
18 #include <thread>
19 #include "account_log_wrapper.h"
20 #define private public
21 #include "app_account_manager_service.h"
22 #undef private
23 #include "event_handler.h"
24
25 using namespace testing::ext;
26 using namespace OHOS;
27 using namespace OHOS::AccountSA;
28
29 namespace {
30 const std::string STRING_NAME = "name";
31 const std::string STRING_EXTRA_INFO = "extra_info";
32 const std::string STRING_OWNER = "com.example.owner";
33 const std::string STRING_EMPTY = "";
34 const std::string STRING_KEY = "key";
35 const std::string STRING_KEY_TWO = "key_two";
36 const std::string STRING_VALUE = "value";
37 const std::string STRING_VALUE_TWO = "value_two";
38 constexpr std::size_t SIZE_ZERO = 0;
39 constexpr std::size_t SIZE_ONE = 1;
40 constexpr std::int32_t DELAY_FOR_OPERATION = 3000;
41 } // namespace
42
43 class AppAccountManagerServiceThreadModuleTest : public testing::Test {
44 public:
45 using Callback = OHOS::AppExecFwk::InnerEvent::Callback;
46
47 static void SetUpTestCase(void);
48 static void TearDownTestCase(void);
49 void SetUp(void) override;
50 void TearDown(void) override;
51 std::shared_ptr<AppAccountManagerService>
52 appAccountManagerServicePtr_ = std::make_shared<AppAccountManagerService>();
53
54 void AddAccount(const std::shared_ptr<AppAccountManagerService> &servicePtr);
55 void DeleteAccount(const std::shared_ptr<AppAccountManagerService> &servicePtr);
56 void SetAssociatedData(const std::shared_ptr<AppAccountManagerService> &servicePtr);
57 void SetAssociatedDataTwo(const std::shared_ptr<AppAccountManagerService> &servicePtr);
58 };
59
SetUpTestCase(void)60 void AppAccountManagerServiceThreadModuleTest::SetUpTestCase(void)
61 {
62 GTEST_LOG_(INFO) << "SetUpTestCase";
63 }
64
TearDownTestCase(void)65 void AppAccountManagerServiceThreadModuleTest::TearDownTestCase(void)
66 {
67 GTEST_LOG_(INFO) << "TearDownTestCase enter";
68 }
69
SetUp(void)70 void AppAccountManagerServiceThreadModuleTest::SetUp(void) __attribute__((no_sanitize("cfi")))
71 {
72 testing::UnitTest *test = testing::UnitTest::GetInstance();
73 ASSERT_NE(test, nullptr);
74 const testing::TestInfo *testinfo = test->current_test_info();
75 ASSERT_NE(testinfo, nullptr);
76 string testCaseName = string(testinfo->name());
77 ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
78 }
79
TearDown(void)80 void AppAccountManagerServiceThreadModuleTest::TearDown(void)
81 {}
82
AddAccount(const std::shared_ptr<AppAccountManagerService> & servicePtr)83 void AppAccountManagerServiceThreadModuleTest::AddAccount(const std::shared_ptr<AppAccountManagerService> &servicePtr)
84 {
85 ACCOUNT_LOGI("enter");
86
87 ErrCode result = servicePtr->AddAccount(STRING_NAME, STRING_EXTRA_INFO);
88 ACCOUNT_LOGI("result = %{public}d", result);
89
90 GTEST_LOG_(INFO) << "after AddAccount, result = " << result;
91 }
92
DeleteAccount(const std::shared_ptr<AppAccountManagerService> & servicePtr)93 void AppAccountManagerServiceThreadModuleTest::DeleteAccount(
94 const std::shared_ptr<AppAccountManagerService> &servicePtr)
95 {
96 ACCOUNT_LOGI("enter");
97
98 ErrCode result = servicePtr->DeleteAccount(STRING_NAME);
99 ACCOUNT_LOGI("result = %{public}d", result);
100
101 GTEST_LOG_(INFO) << "after DeleteAccount, result = " << result;
102 }
103
SetAssociatedData(const std::shared_ptr<AppAccountManagerService> & servicePtr)104 void AppAccountManagerServiceThreadModuleTest::SetAssociatedData(
105 const std::shared_ptr<AppAccountManagerService> &servicePtr)
106 {
107 ACCOUNT_LOGI("enter");
108
109 ErrCode result = servicePtr->SetAssociatedData(STRING_NAME, STRING_KEY, STRING_VALUE);
110 ACCOUNT_LOGI("result = %{public}d", result);
111
112 GTEST_LOG_(INFO) << "after SetAssociatedData, result = " << result;
113 }
114
SetAssociatedDataTwo(const std::shared_ptr<AppAccountManagerService> & servicePtr)115 void AppAccountManagerServiceThreadModuleTest::SetAssociatedDataTwo(
116 const std::shared_ptr<AppAccountManagerService> &servicePtr)
117 {
118 ACCOUNT_LOGI("enter");
119
120 ErrCode result = servicePtr->SetAssociatedData(STRING_NAME, STRING_KEY_TWO, STRING_VALUE_TWO);
121 ACCOUNT_LOGI("result = %{public}d", result);
122
123 GTEST_LOG_(INFO) << "after SetAssociatedData, result = " << result;
124 }
125
126 /**
127 * @tc.name: AppAccountManagerServiceThread_AddAccount_0100
128 * @tc.desc: Add an account with valid data.
129 * @tc.type: FUNC
130 * @tc.require: SR000GGVFV
131 */
132 HWTEST_F(AppAccountManagerServiceThreadModuleTest, AppAccountManagerServiceThread_AddAccount_0100, TestSize.Level1)
133 {
134 ACCOUNT_LOGI("AppAccountManagerServiceThread_AddAccount_0100");
135
__anon240987bc0202null136 auto callbackAdd = [this] { this->AddAccount(this->appAccountManagerServicePtr_); };
137 std::thread thread1(callbackAdd);
138 thread1.detach();
139
140 std::this_thread::sleep_for(std::chrono::milliseconds(DELAY_FOR_OPERATION));
141
142 std::vector<AppAccountInfo> appAccounts;
143 ErrCode result = appAccountManagerServicePtr_->GetAllAccounts(STRING_OWNER, appAccounts);
144 EXPECT_EQ(result, ERR_OK);
145 ASSERT_EQ(appAccounts.size(), SIZE_ONE);
146
__anon240987bc0302null147 auto callbackDel = [this] { this->DeleteAccount(this->appAccountManagerServicePtr_); };
148 std::thread thread2(callbackDel);
149 thread2.detach();
150
151 std::this_thread::sleep_for(std::chrono::milliseconds(DELAY_FOR_OPERATION));
152
153 result = appAccountManagerServicePtr_->GetAllAccounts(STRING_OWNER, appAccounts);
154 EXPECT_EQ(result, ERR_OK);
155 ASSERT_EQ(appAccounts.size(), SIZE_ZERO);
156 }
157
158 /**
159 * @tc.name: AppAccountManagerServiceThread_DeleteAccount_0100
160 * @tc.desc: Delete an account with valid data.
161 * @tc.type: FUNC
162 * @tc.require: SR000GGVFV
163 */
164 HWTEST_F(AppAccountManagerServiceThreadModuleTest, AppAccountManagerServiceThread_DeleteAccount_0100, TestSize.Level1)
165 {
166 ACCOUNT_LOGI("AppAccountManagerServiceThread_DeleteAccount_0100");
167
168 ErrCode result = appAccountManagerServicePtr_->AddAccount(STRING_NAME, STRING_EXTRA_INFO);
169 EXPECT_EQ(result, ERR_OK);
170
__anon240987bc0402null171 auto callback = [this] { this->DeleteAccount(this->appAccountManagerServicePtr_); };
172 std::thread taskThread(callback);
173 taskThread.detach();
174
175
176 std::this_thread::sleep_for(std::chrono::milliseconds(DELAY_FOR_OPERATION));
177
178 std::string extraInfo;
179 result = appAccountManagerServicePtr_->GetAccountExtraInfo(STRING_NAME, extraInfo);
180 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_ACCOUNT_INFO_BY_ID);
181 EXPECT_EQ(extraInfo, STRING_EMPTY);
182 }
183
184 /**
185 * @tc.name: AppAccountManagerServiceThread_SetAssociatedData_0100
186 * @tc.desc: Set associated data with valid data.
187 * @tc.type: FUNC
188 * @tc.require: SR000GGVFV
189 */
190 HWTEST_F(
191 AppAccountManagerServiceThreadModuleTest, AppAccountManagerServiceThread_SetAssociatedData_0100, TestSize.Level1)
192 {
193 ACCOUNT_LOGI("AppAccountManagerServiceThread_SetAssociatedData_0100");
194
195 ErrCode result = appAccountManagerServicePtr_->AddAccount(STRING_NAME, STRING_EXTRA_INFO);
196 EXPECT_EQ(result, ERR_OK);
197
__anon240987bc0502null198 auto callbackSetAss = [this] { this->SetAssociatedData(this->appAccountManagerServicePtr_); };
199 std::thread thread1(callbackSetAss);
200 thread1.detach();
201
__anon240987bc0602null202 Callback callbackSetTwo = [this] { this->SetAssociatedDataTwo(this->appAccountManagerServicePtr_); };
203 std::thread thread2(callbackSetTwo);
204 thread2.detach();
205
206 std::this_thread::sleep_for(std::chrono::milliseconds(DELAY_FOR_OPERATION));
207
208 std::string value;
209 result = appAccountManagerServicePtr_->GetAssociatedData(STRING_NAME, STRING_KEY, value);
210 EXPECT_EQ(result, ERR_OK);
211 EXPECT_EQ(value, STRING_VALUE);
212
213 result = appAccountManagerServicePtr_->GetAssociatedData(STRING_NAME, STRING_KEY_TWO, value);
214 EXPECT_EQ(result, ERR_OK);
215 EXPECT_EQ(value, STRING_VALUE_TWO);
216
__anon240987bc0702null217 Callback callbackDel = [this] { this->DeleteAccount(this->appAccountManagerServicePtr_); };
218 std::thread thread3(callbackDel);
219 thread3.detach();
220
221 std::this_thread::sleep_for(std::chrono::milliseconds(DELAY_FOR_OPERATION));
222
223 std::vector<AppAccountInfo> appAccounts;
224 result = appAccountManagerServicePtr_->GetAllAccounts(STRING_OWNER, appAccounts);
225 EXPECT_EQ(result, ERR_OK);
226 ASSERT_EQ(appAccounts.size(), SIZE_ZERO);
227 }
228