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 "account_error_no.h"
19 #include "account_log_wrapper.h"
20 #include "app_account_data_storage.h"
21 #define private public
22 #include "app_account_info.h"
23 #undef private
24 #include "app_account_control_manager.h"
25 
26 using namespace testing::ext;
27 using namespace OHOS;
28 using namespace OHOS::AccountSA;
29 
30 namespace {
31 const std::string STRING_NAME = "name";
32 const std::string STRING_OWNER = "com.example.owner";
33 const std::string STRING_EXTRA_INFO = "extra_info";
34 const std::string STRING_EXTRA_INFO_TWO = "extra_info_two";
35 const std::string STRING_BUNDLE_NAME = "com.example.third_party";
36 const std::string STRING_ACCOUNT_ID = "100";
37 const std::string STRING_STORE_ID = STRING_ACCOUNT_ID;
38 const std::string EL2_DATA_STORE_PREFIX = "account_";
39 const std::string DATA_STORAGE_PATH = "/data/service/el2/100/account/app_account/database/";
40 constexpr std::size_t SIZE_ZERO = 0;
41 constexpr std::size_t SIZE_ONE = 1;
42 AccountDataStorageOptions gOptions;
43 }  // namespace
44 
45 class AppAccountDataStorageTest : public testing::Test {
46 public:
47     static void SetUpTestCase(void);
48     static void TearDownTestCase(void);
49     void SetUp(void) override;
50     void TearDown(void) override;
51     void ClearDataStorage(void);
52 };
53 
SetUpTestCase(void)54 void AppAccountDataStorageTest::SetUpTestCase(void)
55 {}
56 
TearDownTestCase(void)57 void AppAccountDataStorageTest::TearDownTestCase(void)
58 {
59     GTEST_LOG_(INFO) << "TearDownTestCase!";
60 }
61 
SetUp(void)62 void AppAccountDataStorageTest::SetUp(void) __attribute__((no_sanitize("cfi")))
63 {
64     testing::UnitTest *test = testing::UnitTest::GetInstance();
65     ASSERT_NE(test, nullptr);
66     const testing::TestInfo *testinfo = test->current_test_info();
67     ASSERT_NE(testinfo, nullptr);
68     string testCaseName = string(testinfo->name());
69     ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
70 
71     GTEST_LOG_(INFO) << "SetUp enter!";
72     gOptions.autoSync = false;
73     gOptions.securityLevel = DistributedKv::SecurityLevel::S1;
74     gOptions.area = DistributedKv::EL2;
75     gOptions.baseDir = DATA_STORAGE_PATH;
76     ClearDataStorage();
77 }
78 
TearDown(void)79 void AppAccountDataStorageTest::TearDown(void)
80 {}
81 
ClearDataStorage(void)82 void AppAccountDataStorageTest::ClearDataStorage(void)
83 {
84     auto dataStoragePtr = std::make_shared<AppAccountDataStorage>(EL2_DATA_STORE_PREFIX + STRING_STORE_ID, gOptions);
85     std::map<std::string, std::shared_ptr<IAccountInfo>> accounts;
86     ErrCode result = dataStoragePtr->LoadAllData(accounts);
87     if (!accounts.empty()) {
88         for (auto accountPtr : accounts) {
89             result = dataStoragePtr->RemoveValueFromKvStore(accountPtr.first);
90         }
91     }
92     result = dataStoragePtr->LoadAllData(accounts);
93     GTEST_LOG_(INFO) << "AppAccountDataStorageTest ClearDataStorage end, accounts.size =" << accounts.size();
94 }
95 
96 /**
97  * @tc.name: AppAccountDataStorage_AddAccountInfo_0100
98  * @tc.desc: Add app account info with valid data.
99  * @tc.type: FUNC
100  * @tc.require: SR000GGV11
101  */
102 HWTEST_F(AppAccountDataStorageTest, AppAccountDataStorage_AddAccountInfo_0100, TestSize.Level0)
103 {
104     ACCOUNT_LOGI("AppAccountDataStorage_AddAccountInfo_0100");
105 
106     auto dataStoragePtr = std::make_shared<AppAccountDataStorage>(EL2_DATA_STORE_PREFIX + STRING_STORE_ID, gOptions);
107     EXPECT_NE(dataStoragePtr, nullptr);
108 
109     std::map<std::string, std::shared_ptr<IAccountInfo>> accounts;
110     EXPECT_EQ(accounts.size(), SIZE_ZERO);
111 
112     int result = dataStoragePtr->LoadAllData(accounts);
113     EXPECT_EQ(result, ERR_OK);
114     EXPECT_EQ(accounts.size(), SIZE_ZERO);
115 
116     AppAccountInfo appAccountInfo(STRING_NAME, STRING_OWNER);
117     appAccountInfo.SetExtraInfo(STRING_EXTRA_INFO);
118     appAccountInfo.EnableAppAccess(STRING_BUNDLE_NAME);
119     result = dataStoragePtr->AddAccountInfo(appAccountInfo);
120     EXPECT_EQ(result, ERR_OK);
121 
122     result = dataStoragePtr->LoadAllData(accounts);
123     EXPECT_EQ(result, ERR_OK);
124     EXPECT_EQ(accounts.size(), SIZE_ONE);
125 
126     auto accountPtr = accounts.begin();
127     EXPECT_NE(accountPtr, accounts.end());
128 
129     auto appAccountInfoPtr = std::static_pointer_cast<AppAccountInfo>(accountPtr->second);
130     EXPECT_NE(appAccountInfoPtr, nullptr);
131 
132     // get name
133     std::string name;
134     appAccountInfoPtr->GetName(name);
135     EXPECT_EQ(name, STRING_NAME);
136 
137     // get extraInfo
138     std::string extraInfo;
139     appAccountInfoPtr->GetExtraInfo(extraInfo);
140     EXPECT_EQ(extraInfo, STRING_EXTRA_INFO);
141 
142     // get AuthorizedApps
143     std::set<std::string> apps;
144     appAccountInfoPtr->GetAuthorizedApps(apps);
145     EXPECT_EQ(apps.size(), SIZE_ONE);
146     EXPECT_EQ(*(apps.begin()), STRING_BUNDLE_NAME);
147 
148     // delete account
149     const std::string id = appAccountInfo.GetPrimeKey();
150     result = dataStoragePtr->RemoveValueFromKvStore(id);
151     EXPECT_EQ(result, ERR_OK);
152 }
153 
154 /**
155  * @tc.name: AppAccountDataStorage_SaveAccountInfo_0100
156  * @tc.desc: Save app account info with valid data.
157  * @tc.type: FUNC
158  * @tc.require: SR000GGV11
159  */
160 HWTEST_F(AppAccountDataStorageTest, AppAccountDataStorage_SaveAccountInfo_0100, TestSize.Level1)
161 {
162     ACCOUNT_LOGI("AppAccountDataStorage_SaveAccountInfo_0100");
163 
164     auto dataStoragePtr = std::make_shared<AppAccountDataStorage>(EL2_DATA_STORE_PREFIX + STRING_STORE_ID, gOptions);
165     EXPECT_NE(dataStoragePtr, nullptr);
166 
167     std::map<std::string, std::shared_ptr<IAccountInfo>> accounts;
168     EXPECT_EQ(accounts.size(), SIZE_ZERO);
169 
170     int result = dataStoragePtr->LoadAllData(accounts);
171     EXPECT_EQ(result, ERR_OK);
172     EXPECT_EQ(accounts.size(), SIZE_ZERO);
173 
174     AppAccountInfo appAccountInfo(STRING_NAME, STRING_OWNER);
175     appAccountInfo.SetExtraInfo(STRING_EXTRA_INFO);
176     result = dataStoragePtr->AddAccountInfo(appAccountInfo);
177     EXPECT_EQ(result, ERR_OK);
178 
179     // save another extra info with the same name
180     appAccountInfo.SetExtraInfo(STRING_EXTRA_INFO_TWO);
181     result = dataStoragePtr->SaveAccountInfo(appAccountInfo);
182     EXPECT_EQ(result, ERR_OK);
183 
184     result = dataStoragePtr->LoadAllData(accounts);
185     EXPECT_EQ(result, ERR_OK);
186     EXPECT_EQ(accounts.size(), SIZE_ONE);
187 
188     auto accountPtr = accounts.begin();
189     EXPECT_NE(accountPtr, accounts.end());
190 
191     auto appAccountInfoPtr = std::static_pointer_cast<AppAccountInfo>(accountPtr->second);
192     EXPECT_NE(appAccountInfoPtr, nullptr);
193 
194     std::string name;
195     appAccountInfoPtr->GetName(name);
196     EXPECT_EQ(name, STRING_NAME);
197 
198     std::string extraInfo;
199     appAccountInfoPtr->GetExtraInfo(extraInfo);
200     EXPECT_EQ(extraInfo, STRING_EXTRA_INFO_TWO);
201 
202     // delete account
203     const std::string id = appAccountInfo.GetPrimeKey();
204     result = dataStoragePtr->RemoveValueFromKvStore(id);
205     EXPECT_EQ(result, ERR_OK);
206 }
207 
208 /**
209  * @tc.name: AppAccountDataStorage_DeleteAccount_0100
210  * @tc.desc: Delete an app account with valid data.
211  * @tc.type: FUNC
212  * @tc.require: SR000GGV11
213  */
214 HWTEST_F(AppAccountDataStorageTest, AppAccountDataStorage_DeleteAccount_0100, TestSize.Level1)
215 {
216     ACCOUNT_LOGI("AppAccountDataStorage_DeleteAccount_0100");
217 
218     auto dataStoragePtr = std::make_shared<AppAccountDataStorage>(EL2_DATA_STORE_PREFIX + STRING_STORE_ID, gOptions);
219     EXPECT_NE(dataStoragePtr, nullptr);
220 
221     std::map<std::string, std::shared_ptr<IAccountInfo>> accounts;
222     EXPECT_EQ(accounts.size(), SIZE_ZERO);
223 
224     int result = dataStoragePtr->LoadAllData(accounts);
225     EXPECT_EQ(result, ERR_OK);
226     EXPECT_EQ(accounts.size(), SIZE_ZERO);
227 
228     AppAccountInfo appAccountInfo(STRING_NAME, STRING_OWNER);
229     result = dataStoragePtr->AddAccountInfo(appAccountInfo);
230     EXPECT_EQ(result, ERR_OK);
231 
232     result = dataStoragePtr->LoadAllData(accounts);
233     EXPECT_EQ(result, ERR_OK);
234     EXPECT_EQ(accounts.size(), SIZE_ONE);
235 
236     auto accountPtr = accounts.begin();
237     EXPECT_NE(accountPtr, accounts.end());
238 
239     auto appAccountInfoPtr = std::static_pointer_cast<AppAccountInfo>(accountPtr->second);
240     EXPECT_NE(appAccountInfoPtr, nullptr);
241 
242     std::string name;
243     appAccountInfoPtr->GetName(name);
244     EXPECT_EQ(name, STRING_NAME);
245 
246     const std::string id = appAccountInfo.GetPrimeKey();
247     result = dataStoragePtr->RemoveValueFromKvStore(id);
248     EXPECT_EQ(result, ERR_OK);
249 
250     result = dataStoragePtr->LoadAllData(accounts);
251     EXPECT_EQ(result, ERR_OK);
252     EXPECT_EQ(accounts.size(), SIZE_ZERO);
253 }