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 #include <ctime>
17 #include <securec.h>
18 #include "account_log_wrapper.h"
19 #include "mock_domain_so_plugin.h"
20 
21 namespace OHOS {
22 namespace AccountSA {
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 static int32_t g_authenicationValidityPeriod = -1;
27 static int32_t g_authTime = 0;
28 
SetPluginString(const std::string & str,PluginString & pStr)29 static void SetPluginString(const std::string &str, PluginString &pStr)
30 {
31     if (str.empty()) {
32         ACCOUNT_LOGE("Str is empty.");
33         pStr.data = nullptr;
34         pStr.length = 0;
35         return;
36     }
37     pStr.data = strdup(str.c_str());
38     if (pStr.data == nullptr) {
39         ACCOUNT_LOGE("Failed to duplicate string.");
40         pStr.length = 0;
41         return;
42     }
43     pStr.length = str.length();
44 }
45 
SetPluginUint8Vector(const std::vector<uint8_t> & vector,PluginUint8Vector & pVector)46 static bool SetPluginUint8Vector(const std::vector<uint8_t> &vector, PluginUint8Vector &pVector)
47 {
48     if (vector.empty()) {
49         ACCOUNT_LOGE("Vector is empty.");
50         pVector.data = nullptr;
51         return true;
52     }
53     pVector.data = (uint8_t *)malloc(vector.size());
54     (void)memcpy_s(pVector.data, vector.size(), (uint8_t *)vector.data(), vector.size());
55     pVector.capcity = vector.size();
56     pVector.size = vector.size();
57     return true;
58 }
59 
Auth(const PluginDomainAccountInfo * domainAccountInfo,const PluginUint8Vector * credential,const int32_t callerLocalId,PluginAuthResultInfo ** authResultInfo)60 PluginBussnessError *Auth(const PluginDomainAccountInfo *domainAccountInfo, const PluginUint8Vector *credential,
61                           const int32_t callerLocalId, PluginAuthResultInfo **authResultInfo)
62 {
63     ACCOUNT_LOGI("Mock Auth enter.");
64     PluginBussnessError *error = (PluginBussnessError *)malloc(sizeof(PluginBussnessError));
65     if (error == nullptr) {
66         return nullptr;
67     }
68 
69     time_t authTime;
70     (void)time(&authTime);
71     g_authTime = authTime;
72     ACCOUNT_LOGI("Mock Auth time: %{public}d.", g_authTime);
73 
74     error->code = 0;
75     error->msg.data = nullptr;
76 
77     *authResultInfo = (PluginAuthResultInfo *)malloc(sizeof(PluginAuthResultInfo));
78     SetPluginUint8Vector({1, 2}, (*authResultInfo)->accountToken);
79     (*authResultInfo)->remainTimes = 1;
80     (*authResultInfo)->freezingTime = 1;
81     return error;
82 }
83 
BindAccount(const PluginDomainAccountInfo * domainAccountInfo,const int32_t localId)84 PluginBussnessError *BindAccount(const PluginDomainAccountInfo *domainAccountInfo, const int32_t localId)
85 {
86     ACCOUNT_LOGI("Mock BindAccount enter.");
87     PluginBussnessError *error = (PluginBussnessError *)malloc(sizeof(PluginBussnessError));
88     if (error == nullptr) {
89         return nullptr;
90     }
91     error->code = 0;
92     error->msg.data = nullptr;
93     return error;
94 }
95 
GetAccountInfo(const PluginGetDomainAccountInfoOptions * options,const int32_t callerLocalId,PluginDomainAccountInfo ** domainAccountInfo)96 PluginBussnessError *GetAccountInfo(const PluginGetDomainAccountInfoOptions *options, const int32_t callerLocalId,
97                                     PluginDomainAccountInfo **domainAccountInfo)
98 {
99     ACCOUNT_LOGI("Mock GetAccountInfo enter.");
100     PluginBussnessError *error = (PluginBussnessError *)malloc(sizeof(PluginBussnessError));
101     if (error == nullptr) {
102         return nullptr;
103     }
104     if (strcmp(options->domainAccountInfo.accountName.data, "testNewAccountInvalid") == 0) {
105         error->code = 12300003; // 12300003 is ERR_JS_ACCOUNT_NOT_FOUND
106         error->msg.data = nullptr;
107         return error;
108     }
109     error->code = 0;
110     error->msg.data = nullptr;
111     *domainAccountInfo = (PluginDomainAccountInfo *)malloc(sizeof(PluginDomainAccountInfo));
112     if (*domainAccountInfo == NULL) {
113         free(error);
114         return nullptr;
115     }
116 
117     (*domainAccountInfo)->serverConfigId.data = nullptr;
118     SetPluginString(options->domainAccountInfo.domain.data, (*domainAccountInfo)->domain);
119     SetPluginString(options->domainAccountInfo.accountName.data, (*domainAccountInfo)->accountName);
120     SetPluginString(options->domainAccountInfo.accountId.data, (*domainAccountInfo)->accountId);
121     return error;
122 }
123 
IsAuthenticationExpired(const PluginDomainAccountInfo * domainAccountInfo,const PluginUint8Vector * token,int32_t * isValid)124 PluginBussnessError *IsAuthenticationExpired(const PluginDomainAccountInfo *domainAccountInfo,
125                                              const PluginUint8Vector *token, int32_t *isValid)
126 {
127     ACCOUNT_LOGI("Mock IsAuthenticationExpired enter.");
128     PluginBussnessError *error = (PluginBussnessError *)malloc(sizeof(PluginBussnessError));
129     if (error == nullptr) {
130         return nullptr;
131     }
132     if (domainAccountInfo == nullptr) {
133         return nullptr;
134     }
135 
136     if (g_authenicationValidityPeriod == -1) {
137         ACCOUNT_LOGI("Mock Auth not set.");
138         *isValid = 1;
139         error->code = 0;
140         error->msg.data = nullptr;
141         return error;
142     }
143 
144     time_t curTime;
145     (void)time(&curTime);
146     int32_t calTime = static_cast<int32_t>(curTime - g_authTime);
147     if (calTime > g_authenicationValidityPeriod) {
148         ACCOUNT_LOGI("Mock Auth expired: %{public}d.", calTime);
149         *isValid = 0;
150     } else {
151         ACCOUNT_LOGI("Mock Auth not expired: %{public}d.", calTime);
152         *isValid = 1;
153     }
154 
155     error->code = 0;
156     error->msg.data = nullptr;
157     return error;
158 }
159 
SetAccountPolicy(PluginDomainAccountPolicy * domainAccountPolicy)160 PluginBussnessError *SetAccountPolicy(PluginDomainAccountPolicy *domainAccountPolicy)
161 {
162     ACCOUNT_LOGI("Mock SetAccountPolicy enter.");
163     PluginBussnessError *error = (PluginBussnessError *)malloc(sizeof(PluginBussnessError));
164     if (error == nullptr) {
165         return nullptr;
166     }
167     g_authenicationValidityPeriod = domainAccountPolicy->authenicationValidityPeriod;
168     error->code = 0;
169     error->msg.data = nullptr;
170     return error;
171 }
172 
UpdateAccountInfo(const PluginDomainAccountInfo * domainAccountInfo,const PluginDomainAccountInfo * newDomainAccountInfo)173 PluginBussnessError* UpdateAccountInfo(const PluginDomainAccountInfo *domainAccountInfo,
174     const PluginDomainAccountInfo *newDomainAccountInfo)
175 {
176     PluginBussnessError* error = (PluginBussnessError *)malloc(sizeof(PluginBussnessError));
177     if (error == nullptr) {
178         return nullptr;
179     }
180     error->code = 0;
181     error->msg.data = nullptr;
182     return error;
183 }
184 #ifdef __cplusplus
185 }
186 #endif
187 } // namespace AccountSA
188 } // namespace OHOS