1 /*
2 * Copyright (c) 2022-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 "mock_domain_plugin.h"
17 #include "account_log_wrapper.h"
18 #include "string_wrapper.h"
19
20 namespace OHOS {
21 namespace AccountSA {
22 namespace {
23 const std::vector<uint8_t> TOKEN = {1, 2, 3, 4, 5};
24 const std::vector<uint8_t> DEFAULT_PASSWORD = {49, 50, 51, 52, 53};
25 const int32_t DEFAULT_REMAINING_TIMES = 5;
26 const int32_t DEFAULT_FREEZING_TIME = 6000;
27 const int32_t INVALID_CODE = -1;
28 const std::string VALID_DOMAIN = "china.example.com";
29 const std::string VALID_ACCOUNT_NAME = "zhangsan";
30 const std::string STRING_NAME_NEW = "zhangsan777";
31 const std::string STRING_NAME_INVALID = "zhangsan55";
32 const std::string STRING_NAME = "zhangsan666";
33 const std::string STRING_NAME_BIND_INVALID = "lisi";
34 const std::string ACCOUNT_NAME = "zhangsan5";
35 const std::string BUNDLE_NAME = "osaccount_test";
36 const std::string STRING_DOMAIN_NEW = "test.example.com";
37 }
MockDomainPlugin()38 MockDomainPlugin::MockDomainPlugin() : remainingTimes_(DEFAULT_REMAINING_TIMES), freezingTime_(0)
39 {}
40
~MockDomainPlugin()41 MockDomainPlugin::~MockDomainPlugin()
42 {}
43
AuthCommonInterface(const DomainAccountInfo & info,const std::vector<uint8_t> & authData,const std::shared_ptr<DomainAccountCallback> & callback,AuthMode authMode)44 void MockDomainPlugin::AuthCommonInterface(const DomainAccountInfo &info, const std::vector<uint8_t> &authData,
45 const std::shared_ptr<DomainAccountCallback> &callback, AuthMode authMode)
46 {
47 if (callback == nullptr) {
48 ACCOUNT_LOGE("callback is nullptr");
49 return;
50 }
51 Parcel emptyParcel;
52 AccountSA::DomainAuthResult emptyResult;
53 if (!emptyResult.Marshalling(emptyParcel)) {
54 return;
55 }
56 if ((info.domain_ == STRING_DOMAIN_NEW)) {
57 callback->OnResult(0, emptyParcel);
58 return;
59 }
60 if ((info.domain_ != VALID_DOMAIN) || (info.accountName_ != VALID_ACCOUNT_NAME)) {
61 callback->OnResult(1, emptyParcel);
62 return;
63 }
64 bool isCorrect = true;
65 if (authData.size() == DEFAULT_PASSWORD.size()) {
66 for (size_t i = 0; i < authData.size(); ++i) {
67 if (authData[i] != DEFAULT_PASSWORD[i]) {
68 isCorrect = false;
69 break;
70 }
71 }
72 } else {
73 isCorrect = false;
74 }
75 if (authMode == AUTH_WITH_POPUP_MODE) {
76 isCorrect = true;
77 }
78 if (isCorrect) {
79 remainingTimes_ = DEFAULT_REMAINING_TIMES;
80 freezingTime_ = 0;
81 } else {
82 remainingTimes_ = remainingTimes_ > 0 ? remainingTimes_ - 1 : 0;
83 freezingTime_ = remainingTimes_ > 0 ? 0 : DEFAULT_FREEZING_TIME;
84 }
85 AccountSA::DomainAuthResult result;
86 result.authStatusInfo.remainingTimes = remainingTimes_;
87 result.authStatusInfo.freezingTime = freezingTime_;
88 result.token = TOKEN;
89 Parcel resultParcel;
90 if (!result.Marshalling(resultParcel)) {
91 return;
92 }
93 callback->OnResult(!isCorrect, resultParcel);
94 }
95
Auth(const DomainAccountInfo & info,const std::vector<uint8_t> & password,const std::shared_ptr<DomainAccountCallback> & callback)96 void MockDomainPlugin::Auth(const DomainAccountInfo &info, const std::vector<uint8_t> &password,
97 const std::shared_ptr<DomainAccountCallback> &callback)
98 {
99 AuthCommonInterface(info, password, callback, AUTH_WITH_CREDENTIAL_MODE);
100 }
101
AuthWithPopup(const DomainAccountInfo & info,const std::shared_ptr<DomainAccountCallback> & callback)102 void MockDomainPlugin::AuthWithPopup(
103 const DomainAccountInfo &info, const std::shared_ptr<DomainAccountCallback> &callback)
104 {
105 AuthCommonInterface(info, {}, callback, AUTH_WITH_POPUP_MODE);
106 }
107
AuthWithToken(const DomainAccountInfo & info,const std::vector<uint8_t> & token,const std::shared_ptr<DomainAccountCallback> & callback)108 void MockDomainPlugin::AuthWithToken(const DomainAccountInfo &info, const std::vector<uint8_t> &token,
109 const std::shared_ptr<DomainAccountCallback> &callback)
110 {
111 AuthCommonInterface(info, token, callback, AUTH_WITH_TOKEN_MODE);
112 }
113
GetAuthStatusInfo(const DomainAccountInfo & info,const std::shared_ptr<DomainAccountCallback> & callback)114 void MockDomainPlugin::GetAuthStatusInfo(
115 const DomainAccountInfo &info, const std::shared_ptr<DomainAccountCallback> &callback)
116 {
117 AuthStatusInfo authStatusInfo;
118 if ((info.accountName_ == VALID_ACCOUNT_NAME) && (info.domain_ == VALID_DOMAIN)) {
119 authStatusInfo.remainingTimes = remainingTimes_;
120 authStatusInfo.freezingTime = freezingTime_;
121 } else {
122 authStatusInfo.remainingTimes = -1;
123 authStatusInfo.freezingTime = -1;
124 }
125 Parcel parcel;
126 authStatusInfo.Marshalling(parcel);
127 callback->OnResult(0, parcel);
128 }
129
GetDomainAccountInfo(const GetDomainAccountInfoOptions & options,const std::shared_ptr<DomainAccountCallback> & callback)130 void MockDomainPlugin::GetDomainAccountInfo(
131 const GetDomainAccountInfoOptions &options, const std::shared_ptr<DomainAccountCallback> &callback)
132 {
133 Parcel parcel;
134 if (options.accountInfo.accountName_ == ACCOUNT_NAME) {
135 AAFwk::WantParams parameters;
136 parameters.SetParam("domain", OHOS::AAFwk::String::Box(options.accountInfo.domain_));
137 parameters.SetParam("accountName", OHOS::AAFwk::String::Box(options.accountInfo.accountName_));
138 parameters.SetParam("accountId", OHOS::AAFwk::String::Box("222"));
139 parameters.Marshalling(parcel);
140 callback->OnResult(0, parcel);
141 }
142 if (options.accountInfo.accountName_ == STRING_NAME) {
143 AAFwk::WantParams parameters;
144 parameters.SetParam("domain", OHOS::AAFwk::String::Box(options.accountInfo.domain_));
145 parameters.SetParam("accountName", OHOS::AAFwk::String::Box(options.accountInfo.accountName_));
146 parameters.SetParam("accountId", OHOS::AAFwk::String::Box("555"));
147 parameters.Marshalling(parcel);
148 callback->OnResult(0, parcel);
149 }
150 if (options.accountInfo.accountName_ == STRING_NAME_NEW) {
151 AAFwk::WantParams parameters;
152 parameters.SetParam("domain", OHOS::AAFwk::String::Box(options.accountInfo.domain_));
153 parameters.SetParam("accountName", OHOS::AAFwk::String::Box(options.accountInfo.accountName_));
154 parameters.SetParam("accountId", OHOS::AAFwk::String::Box("444"));
155 parameters.Marshalling(parcel);
156 callback->OnResult(0, parcel);
157 }
158 if (options.accountInfo.accountName_ == VALID_ACCOUNT_NAME) {
159 AAFwk::WantParams parameters;
160 parameters.SetParam("domain", OHOS::AAFwk::String::Box(options.accountInfo.domain_));
161 parameters.SetParam("accountName", OHOS::AAFwk::String::Box(options.accountInfo.accountName_));
162 parameters.SetParam("accountId", OHOS::AAFwk::String::Box("3333"));
163 parameters.Marshalling(parcel);
164 callback->OnResult(0, parcel);
165 }
166 if (options.accountInfo.accountName_ == STRING_NAME_BIND_INVALID) {
167 AAFwk::WantParams parameters;
168 parameters.SetParam("domain", OHOS::AAFwk::String::Box(options.accountInfo.domain_));
169 parameters.SetParam("accountName", OHOS::AAFwk::String::Box(options.accountInfo.accountName_));
170 parameters.SetParam("accountId", OHOS::AAFwk::String::Box("666"));
171 parameters.Marshalling(parcel);
172 callback->OnResult(0, parcel);
173 }
174 if (options.accountInfo.accountName_ == STRING_NAME_INVALID) {
175 AAFwk::WantParams parameters;
176 parameters.SetParam("accountName", OHOS::AAFwk::String::Box(options.accountInfo.accountName_));
177 parameters.Marshalling(parcel);
178 callback->OnResult(INVALID_CODE, parcel);
179 }
180 }
181
OnAccountBound(const DomainAccountInfo & info,const int32_t localId,const std::shared_ptr<DomainAccountCallback> & callback)182 void MockDomainPlugin::OnAccountBound(
183 const DomainAccountInfo &info, const int32_t localId, const std::shared_ptr<DomainAccountCallback> &callback)
184 {
185 DomainAccountInfo testInfo;
186 Parcel parcel;
187 if ((info.accountName_ == VALID_ACCOUNT_NAME) || (info.accountName_ == ACCOUNT_NAME) ||
188 (info.accountName_ == STRING_NAME) || (info.accountName_ == STRING_NAME_NEW)) {
189 testInfo = info;
190 testInfo.Marshalling(parcel);
191 callback->OnResult(0, parcel);
192 } else {
193 testInfo.accountName_ = info.accountName_;
194 testInfo.Marshalling(parcel);
195 callback->OnResult(INVALID_CODE, parcel);
196 }
197 }
198
OnAccountUnBound(const DomainAccountInfo & info,const std::shared_ptr<DomainAccountCallback> & callback)199 void MockDomainPlugin::OnAccountUnBound(
200 const DomainAccountInfo &info, const std::shared_ptr<DomainAccountCallback> &callback)
201 {
202 DomainAccountInfo testInfo;
203 Parcel parcel;
204 if (info.accountName_ == VALID_ACCOUNT_NAME) {
205 testInfo = info;
206 testInfo.Marshalling(parcel);
207 callback->OnResult(0, parcel);
208 } else {
209 testInfo.accountName_ = info.accountName_;
210 testInfo.Marshalling(parcel);
211 callback->OnResult(INVALID_CODE, parcel);
212 }
213 }
214
IsAccountTokenValid(const DomainAccountInfo & info,const std::vector<uint8_t> & token,const std::shared_ptr<DomainAccountCallback> & callback)215 void MockDomainPlugin::IsAccountTokenValid(const DomainAccountInfo &info, const std::vector<uint8_t> &token,
216 const std::shared_ptr<DomainAccountCallback> &callback)
217 {
218 ACCOUNT_LOGI("mock IsAccountTokenValid called");
219 Parcel parcel;
220 parcel.WriteBool(true);
221 callback->OnResult(0, parcel);
222 }
223
GetAccessToken(const DomainAccountInfo & domainInfo,const std::vector<uint8_t> & accountToken,const GetAccessTokenOptions & option,const std::shared_ptr<DomainAccountCallback> & callback)224 void MockDomainPlugin::GetAccessToken(const DomainAccountInfo &domainInfo, const std::vector<uint8_t> &accountToken,
225 const GetAccessTokenOptions &option, const std::shared_ptr<DomainAccountCallback> &callback)
226 {
227 Parcel parcel;
228 if ((domainInfo.accountName_ == STRING_NAME) || (domainInfo.accountId_ == "555")) {
229 parcel.WriteUInt8Vector(DEFAULT_PASSWORD);
230 callback->OnResult(0, parcel);
231 }
232 if (domainInfo.accountName_ == STRING_NAME_NEW) {
233 std::vector<uint8_t> token;
234 parcel.WriteUInt8Vector(token);
235 callback->OnResult(INVALID_CODE, parcel);
236 }
237 }
238 } // AccountSA
239 } // OHOS