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 #include "account_data_storage.h"
16 #include <memory>
17 #include <unistd.h>
18 #include "account_log_wrapper.h"
19 #include "account_hisysevent_adapter.h"
20 
21 namespace OHOS {
22 namespace AccountSA {
23 
AccountDataStorage(const std::string & appId,const std::string & storeId,const AccountDataStorageOptions & options)24 AccountDataStorage::AccountDataStorage(const std::string &appId, const std::string &storeId,
25     const AccountDataStorageOptions &options)
26 {
27     ACCOUNT_LOGI("mock enter");
28 }
29 
~AccountDataStorage()30 AccountDataStorage::~AccountDataStorage()
31 {
32     ACCOUNT_LOGI("mock enter");
33 }
34 
TryTwice(const std::function<DistributedKv::Status ()> & func) const35 void AccountDataStorage::TryTwice(const std::function<DistributedKv::Status()> &func) const
36 {
37     ACCOUNT_LOGI("mock enter");
38 }
39 
GetKvStore()40 OHOS::DistributedKv::Status AccountDataStorage::GetKvStore()
41 {
42     ACCOUNT_LOGI("mock enter");
43     return OHOS::DistributedKv::Status::SUCCESS;
44 }
45 
CheckKvStore()46 bool AccountDataStorage::CheckKvStore()
47 {
48     ACCOUNT_LOGI("mock enter");
49     return true;
50 }
51 
LoadAllData(std::map<std::string,std::shared_ptr<IAccountInfo>> & infos)52 ErrCode AccountDataStorage::LoadAllData(std::map<std::string, std::shared_ptr<IAccountInfo>> &infos)
53 {
54     ACCOUNT_LOGI("mock enter");
55     return ERR_OK;
56 }
57 
AddAccountInfo(const IAccountInfo & iAccountInfo)58 ErrCode AccountDataStorage::AddAccountInfo(const IAccountInfo &iAccountInfo)
59 {
60     ACCOUNT_LOGI("mock enter");
61     return ERR_OK;
62 }
63 
SaveAccountInfo(const IAccountInfo & iAccountInfo)64 ErrCode AccountDataStorage::SaveAccountInfo(const IAccountInfo &iAccountInfo)
65 {
66     ACCOUNT_LOGI("mock enter");
67     return ERR_OK;
68 }
69 
RemoveValueFromKvStore(const std::string & keyStr)70 ErrCode AccountDataStorage::RemoveValueFromKvStore(const std::string &keyStr)
71 {
72     ACCOUNT_LOGI("mock enter");
73     return ERR_OK;
74 }
75 
GetEntries(std::string subId,std::vector<OHOS::DistributedKv::Entry> & allEntries) const76 OHOS::DistributedKv::Status AccountDataStorage::GetEntries(
77     std::string subId, std::vector<OHOS::DistributedKv::Entry> &allEntries) const
78 {
79     ACCOUNT_LOGI("mock enter");
80     return OHOS::DistributedKv::Status::SUCCESS;
81 }
82 
DeleteKvStore()83 ErrCode AccountDataStorage::DeleteKvStore()
84 {
85     ACCOUNT_LOGI("mock enter");
86     return ERR_OK;
87 }
88 
89 struct OAuthTokenInfo {
90     std::string authType;
91     std::string token;
92     std::set<std::string> authList;
93     bool status = true;
94 };
95 class AccountInfoMOCK : public IAccountInfo {
96 private:
97     std::string name;
98     std::string primeKey;
99 
100 public:
AccountInfoMOCK(const std::string & name,const std::string & key)101     AccountInfoMOCK(const std::string &name, const std::string &key) : name(name), primeKey(key)
102     {}
103 
ToJson() const104     Json ToJson() const override
105     {
106         ACCOUNT_LOGI("mock enter");
107         auto tokenArray = Json::array();
108         for (auto it = oauthTokens_.begin(); it != oauthTokens_.end(); ++it) {
109             if (!it->second.status && it->second.authList.empty()) {
110                 continue;
111             }
112             auto tokenObject = Json {
113                 {"authType", it->first},
114                 {"oauthToken", it->second.token},
115                 {"status", it->second.status},
116                 {"authList", it->second.authList}
117             };
118             tokenArray.push_back(tokenObject);
119         }
120         auto jsonObject = Json {
121             {"owner", owner_},
122             {"name", name_},
123             {"alias", alias_},
124             {"extraInfo", extraInfo_},
125             {"authorizedApps", authorizedApps_},
126             {"syncEnable", syncEnable_},
127             {"associatedData", associatedData_},
128             {"accountCredential", accountCredential_},
129             {"tokenInfos", tokenArray},
130         };
131         return jsonObject;
132     }
133 
FromJson(const Json & jsonObject)134     bool FromJson(const Json &jsonObject) override
135     {
136         ACCOUNT_LOGI("mock enter");
137         name = jsonObject["name"];
138         primeKey = jsonObject["primeKey"];
139         return true;
140     }
141 
ToString() const142     std::string ToString() const override
143     {
144         ACCOUNT_LOGI("mock enter");
145         auto jsonObject = ToJson();
146         try {
147             return jsonObject.dump();
148         } catch (Json::type_error& err) {
149             ACCOUNT_LOGE("failed to dump json object, reason: %{public}s", err.what());
150             return "";
151         }
152     }
153 
GetPrimeKey() const154     std::string GetPrimeKey() const override
155     {
156         ACCOUNT_LOGI("mock enter");
157         return (owner_ + "#" + std::to_string(appIndex_) + "#" + name_ + "#");
158     }
SetOAuthToken(const std::string & authType,const std::string & token)159     ErrCode SetOAuthToken(const std::string &authType, const std::string &token)
160     {
161         ACCOUNT_LOGI("mock enter");
162         OAuthTokenInfo tokenInfo;
163         tokenInfo.status = !token.empty();
164         tokenInfo.token = token;
165         tokenInfo.authType = authType;
166         tokenInfo.authList.emplace("bundlename");
167         oauthTokens_.emplace(authType, tokenInfo);
168         return ERR_OK;
169     }
~AccountInfoMOCK()170     virtual ~AccountInfoMOCK() {}
171 
172     std::string owner_;
173     std::string name_;
174     std::string alias_;
175     uint32_t appIndex_ = 0;
176     std::string extraInfo_;
177     std::set<std::string> authorizedApps_;
178     bool syncEnable_ = false;
179     std::string associatedData_;
180     std::string accountCredential_;
181     std::map<std::string, OAuthTokenInfo> oauthTokens_;
182 };
183 
GetAccountInfoById(const std::string id,IAccountInfo & iAccountInfo)184 ErrCode AccountDataStorage::GetAccountInfoById(const std::string id, IAccountInfo &iAccountInfo)
185 {
186     ACCOUNT_LOGI("mock enter");
187     if (id != "com.example.ownermax#0#name#") {
188         AccountInfoMOCK appAccountInfo("name", "key");
189         appAccountInfo.SetOAuthToken("test_authType1", "test_authToken1");
190 
191         Json mkckJson = appAccountInfo.ToJson();
192         iAccountInfo.FromJson(mkckJson);
193         return ERR_OK;
194     } else {
195         return ERR_APPACCOUNT_SERVICE_DATA_STORAGE_PTR_IS_NULLPTR;
196     }
197 }
198 
LoadDataByLocalFuzzyQuery(std::string subId,std::map<std::string,std::shared_ptr<IAccountInfo>> & infos)199 ErrCode AccountDataStorage::LoadDataByLocalFuzzyQuery(
200     std::string subId, std::map<std::string, std::shared_ptr<IAccountInfo>> &infos)
201 {
202     ACCOUNT_LOGI("mock enter");
203     if (subId == "com.example.ownermax#0") {
204         for (int i = 0; i < 1002; ++i) { // 1002 is the maximum number created
205             std::string accountKey = "Account" + std::to_string(i);
206             infos[accountKey] = std::make_shared<AccountInfoMOCK>(accountKey + "name", accountKey + "kkk");
207         }
208         return ERR_OK;
209     } else {
210         return ERR_OK;
211     }
212 }
213 
PutValueToKvStore(const std::string & keyStr,const std::string & valueStr)214 ErrCode AccountDataStorage::PutValueToKvStore(const std::string &keyStr, const std::string &valueStr)
215 {
216     ACCOUNT_LOGI("mock enter");
217     return ERR_OK;
218 }
219 
GetValueFromKvStore(const std::string & keyStr,std::string & valueStr)220 ErrCode AccountDataStorage::GetValueFromKvStore(const std::string &keyStr, std::string &valueStr)
221 {
222     ACCOUNT_LOGI("mock enter");
223     valueStr = "aaaaa";
224     return ERR_OK;
225 }
226 
IsKeyExists(const std::string keyStr)227 bool AccountDataStorage::IsKeyExists(const std::string keyStr)
228 {
229     ACCOUNT_LOGI("mock enter");
230     return true;
231 }
232 
MoveData(const std::shared_ptr<AccountDataStorage> & ptr)233 ErrCode AccountDataStorage::MoveData(const std::shared_ptr<AccountDataStorage> &ptr)
234 {
235     ACCOUNT_LOGI("mock enter");
236     return ERR_OK;
237 }
238 }  // namespace AccountSA
239 }  // namespace OHOS
240