• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <gtest/gtest.h>
16 #include <gmock/gmock.h>
17 
18 #include "account_log_wrapper.h"
19 #include "account_error_no.h"
20 #include "app_account_common.h"
21 #include "string_wrapper.h"
22 
23 using namespace testing::ext;
24 using namespace OHOS;
25 using namespace OHOS::AccountSA;
26 
27 constexpr int32_t MAX_VEC_SIZE = 1030;
28 constexpr int32_t MAX_CUSTOM_DATA_SIZE = 1024;
29 constexpr int32_t INVALID_ERR_CODE = -1;
30 namespace {
31 const std::string STRING_OWNER = "com.example.owner";
32 const std::string STRING_NAME = "name";
33 const std::string STRING_ACCOUNT_ID = "accountId";
34 const std::string STRING_WANTPARAMS_KEY = "key";
35 const std::string STRING_WANTPARAMS_VALUE = "value";
36 const std::string STRING_MESSAGE = "message";
37 const uint32_t UINT32_ID = 1;
38 } // namespace
39 
40 class AppAccountCommonTest : public testing::Test {
41 public:
42     static void SetUpTestCase(void);
43     static void TearDownTestCase(void);
44     void SetUp(void) override;
45     void TearDown(void) override;
46 };
47 
SetUpTestCase(void)48 void AppAccountCommonTest::SetUpTestCase(void)
49 {}
50 
TearDownTestCase(void)51 void AppAccountCommonTest::TearDownTestCase(void)
52 {}
53 
SetUp(void)54 void AppAccountCommonTest::SetUp(void) __attribute__((no_sanitize("cfi")))
55 {
56     testing::UnitTest *test = testing::UnitTest::GetInstance();
57     ASSERT_NE(test, nullptr);
58     const testing::TestInfo *testinfo = test->current_test_info();
59     ASSERT_NE(testinfo, nullptr);
60     string testCaseName = string(testinfo->name());
61     ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
62 }
63 
TearDown(void)64 void AppAccountCommonTest::TearDown(void)
65 {}
66 
67 /**
68  * @tc.name: AuthenticatorInfo_Marshalling001
69  * @tc.desc: Func Marshalling and Unmarshalling.
70  * @tc.type: FUNC
71  * @tc.require issueI5RWXN
72  */
73 HWTEST_F(AppAccountCommonTest, AuthenticatorInfo_Marshalling001, TestSize.Level3)
74 {
75     ACCOUNT_LOGI("AuthenticatorInfo_Marshalling001");
76     Parcel Parcel;
77     AuthenticatorInfo option1;
78     option1.owner = STRING_OWNER;
79     option1.abilityName = STRING_NAME;
80     option1.iconId = UINT32_ID;
81     option1.labelId = UINT32_ID;
82 
83     EXPECT_EQ(option1.Marshalling(Parcel), true);
84     AuthenticatorInfo *option2 = option1.Unmarshalling(Parcel);
85     EXPECT_NE(option2, nullptr);
86 
87     EXPECT_EQ(option2->owner, STRING_OWNER);
88     EXPECT_EQ(option2->abilityName, STRING_NAME);
89     EXPECT_EQ(option2->iconId, UINT32_ID);
90     EXPECT_EQ(option2->labelId, UINT32_ID);
91 }
92 
93 /**
94  * @tc.name: SelectAccountsOptions Marshalling test
95  * @tc.desc: Func Marshalling.
96  * @tc.type: FUNC
97  * @tc.require issueI5RWXN
98  */
99 HWTEST_F(AppAccountCommonTest, Marshalling001, TestSize.Level3)
100 {
101     ACCOUNT_LOGI("Marshalling001");
102     Parcel Parcel;
103     SelectAccountsOptions option1;
104     option1.hasAccounts = true;
105     option1.hasOwners = true;
106     option1.hasLabels = true;
107     option1.allowedOwners.emplace_back("test1");
108     option1.requiredLabels.emplace_back("test2");
109 
110     EXPECT_EQ(option1.Marshalling(Parcel), true);
111     SelectAccountsOptions *option2 = option1.Unmarshalling(Parcel);
112     EXPECT_NE(option2, nullptr);
113 
114     EXPECT_EQ(option2->hasAccounts, true);
115     EXPECT_EQ(option2->hasOwners, true);
116     EXPECT_EQ(option2->hasLabels, true);
117     EXPECT_EQ(option2->allowedOwners[0], "test1");
118     EXPECT_EQ(option2->requiredLabels[0], "test2");
119 }
120 
121 /**
122  * @tc.name: VerifyCredentialOptions Marshalling test
123  * @tc.desc: Func Marshalling.
124  * @tc.type: FUNC
125  * @tc.require issueI5RWXN
126  */
127 HWTEST_F(AppAccountCommonTest, Marshalling002, TestSize.Level3)
128 {
129     ACCOUNT_LOGI("Marshalling002");
130     Parcel Parcel;
131     VerifyCredentialOptions option1;
132     option1.credentialType = "test1";
133     option1.credential = "test2";
134 
135     EXPECT_EQ(option1.Marshalling(Parcel), true);
136     VerifyCredentialOptions *option2 = option1.Unmarshalling(Parcel);
137     EXPECT_NE(option2, nullptr);
138 
139     EXPECT_EQ(option2->credentialType, "test1");
140     EXPECT_EQ(option2->credential, "test2");
141 }
142 
143 /**
144  * @tc.name: CreateAccountOptions Marshalling test
145  * @tc.desc: Func Marshalling.
146  * @tc.type: FUNC
147  * @tc.require issueI5RWXN
148  */
149 HWTEST_F(AppAccountCommonTest, Marshalling003, TestSize.Level3)
150 {
151     ACCOUNT_LOGI("Marshalling003");
152     Parcel Parcel;
153     CreateAccountOptions option1;
154     option1.customData["test"] = "test2";
155 
156     EXPECT_EQ(option1.Marshalling(Parcel), true);
157     CreateAccountOptions *option2 = option1.Unmarshalling(Parcel);
158     EXPECT_NE(option2, nullptr);
159 
160     EXPECT_EQ(option2->customData["test"], "test2");
161 }
162 
163 /**
164  * @tc.name: CreateAccountImplicitlyOptions Marshalling test
165  * @tc.desc: Func Marshalling.
166  * @tc.type: FUNC
167  * @tc.require issueI5RWXN
168  */
169 HWTEST_F(AppAccountCommonTest, Marshalling004, TestSize.Level3)
170 {
171     ACCOUNT_LOGI("Marshalling004");
172     Parcel Parcel;
173     CreateAccountImplicitlyOptions option1;
174     option1.hasAuthType = true;
175     option1.hasRequiredLabels = true;
176     option1.authType = "test1";
177     option1.requiredLabels.emplace_back("test2");
178 
179     EXPECT_EQ(option1.Marshalling(Parcel), true);
180     CreateAccountImplicitlyOptions *option2 = option1.Unmarshalling(Parcel);
181     EXPECT_NE(option2, nullptr);
182 
183     EXPECT_EQ(option2->hasAuthType, true);
184     EXPECT_EQ(option2->hasRequiredLabels, true);
185     EXPECT_EQ(option2->authType, "test1");
186     EXPECT_EQ(option2->requiredLabels[0], "test2");
187 }
188 
189 /**
190  * @tc.name: CreateAccountOptions Marshalling test
191  * @tc.desc: test ReadFromParcel of oversize customData.
192  * @tc.type: FUNC
193  * @tc.require issueI5RWXN
194  */
195 HWTEST_F(AppAccountCommonTest, Marshalling005, TestSize.Level3)
196 {
197     ACCOUNT_LOGI("Marshalling005");
198     Parcel Parcel;
199     CreateAccountOptions srcOptions;
200 
201     for (int i = 0; i < MAX_CUSTOM_DATA_SIZE + 1; i++) {
202         std::string test_key = "test_key" + std::to_string(i);
203         std::string test_value = "test_value" + std::to_string(i);
204         srcOptions.customData.emplace(test_key, test_value);
205     }
206 
207     EXPECT_EQ(srcOptions.Marshalling(Parcel), true);
208     CreateAccountOptions *testOptions = srcOptions.Unmarshalling(Parcel);
209     EXPECT_EQ(testOptions, nullptr);
210 }
211 
212 /**
213  * @tc.name: SelectAccountsOptions Marshalling test
214  * @tc.desc: test ReadFromParcel.
215  * @tc.type: FUNC
216  * @tc.require issueI5RWXN
217  */
218 HWTEST_F(AppAccountCommonTest, Marshalling006, TestSize.Level3)
219 {
220     ACCOUNT_LOGI("Marshalling006");
221     Parcel testParcel;
222     SetPropertiesOptions options;
223     bool result = options.Marshalling(testParcel);
224     ASSERT_EQ(result, true);
225     result = options.Unmarshalling(testParcel);
226     ASSERT_EQ(result, true);
227 }
228 
229 /**
230  * @tc.name: SelectAccountsOptions Marshalling test
231  * @tc.desc: Func Marshalling allowedAccounts is oversize.
232  * @tc.type: FUNC
233  * @tc.require issueI5RWXN
234  */
235 HWTEST_F(AppAccountCommonTest, Marshalling007, TestSize.Level3)
236 {
237     Parcel Parcel;
238     SelectAccountsOptions option1;
239     option1.hasAccounts = true;
240     option1.hasOwners = true;
241     option1.hasLabels = true;
242     for (int i = 0; i <= MAX_VEC_SIZE; i++) {
243         std::string key = std::to_string(i);
244         std::string value = "test" + std::to_string(i);
245         option1.allowedAccounts.emplace_back(std::pair<std::string, std::string>(key, value));
246     }
247     option1.requiredLabels.emplace_back("test2");
248 
249     EXPECT_EQ(option1.Marshalling(Parcel), true);
250     SelectAccountsOptions *option2 = option1.Unmarshalling(Parcel);
251     EXPECT_EQ(option2, nullptr);
252 }
253 
254 /**
255  * @tc.name: ConvertOtherJSErrCodeV8 test
256  * @tc.desc: Func ConvertOtherJSErrCodeV8.
257  * @tc.type: FUNC
258  * @tc.require issueI5RWXN
259  */
260 HWTEST_F(AppAccountCommonTest, ConvertOtherJSErrCodeV8001, TestSize.Level3)
261 {
262     ACCOUNT_LOGI("ConvertOtherJSErrCodeV8001");
263     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_OK), ERR_JS_SUCCESS_V8);
264     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_ACCOUNT_NOT_EXIST), ERR_JS_ACCOUNT_NOT_EXIST);
265     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_OAUTH_AUTHENTICATOR_NOT_EXIST),
266         ERR_JS_OAUTH_AUTHENTICATOR_NOT_EXIST);
267     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_OAUTH_BUSY), ERR_JS_OAUTH_SERVICE_BUSY);
268     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_OAUTH_LIST_MAX_SIZE), ERR_JS_OAUTH_LIST_TOO_LARGE);
269     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_OAUTH_SESSION_NOT_EXIST), ERR_JS_OAUTH_SESSION_NOT_EXIST);
270     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_NOT_EXIST), ERR_JS_OAUTH_TOKEN_NOT_EXIST);
271     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_MAX_SIZE), ERR_JS_OAUTH_TOKEN_TOO_MANY);
272     EXPECT_EQ(ConvertToJSErrCodeV8(INVALID_ERR_CODE), ERR_JS_APP_ACCOUNT_SERVICE_EXCEPTION);
273 }
274 
275 /**
276  * @tc.name: ConvertToJSErrCodeV8 test
277  * @tc.desc: Func ConvertOtherJSErrCodeV8.
278  * @tc.type: FUNC
279  * @tc.require issueI5RWXN
280  */
281 HWTEST_F(AppAccountCommonTest, ConvertToJSErrCodeV8001, TestSize.Level3)
282 {
283     ACCOUNT_LOGI("ConvertToJSErrCodeV8001");
284     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_ADD_EXISTING_ACCOUNT), ERR_JS_INVALID_REQUEST);
285     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_KIT_READ_PARCELABLE_APP_ACCOUNT_INFO), ERR_JS_INVALID_RESPONSE);
286     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_OAUTH_INVALID_RESPONSE), ERR_JS_INVALID_RESPONSE);
287     EXPECT_EQ(
288         ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_OAUTH_AUTHENTICATOR_CALLBACK_NOT_EXIST), ERR_JS_INVALID_RESPONSE);
289     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_ACCOUNT_COMMON_PERMISSION_DENIED), ERR_JS_PERMISSION_DENIED_V8);
290 }
291 
292 /**
293  * @tc.name: AccountCapabilityRequest test
294  * @tc.desc: Func AccountCapabilityRequest.
295  * @tc.type: FUNC
296  * @tc.require issueI7AVZ5
297  */
298 HWTEST_F(AppAccountCommonTest, AccountCapabilityRequest001, TestSize.Level3)
299 {
300     AccountCapabilityRequest testRequest;
301     testRequest.bundleName = "testBundleName";
302     testRequest.abilityName = "testAbilityName";
303     Parcel parcel;
304     EXPECT_EQ(testRequest.Marshalling(parcel), true);
305     AccountCapabilityRequest *retRequest = testRequest.Unmarshalling(parcel);
306     ASSERT_NE(retRequest, nullptr);
307     EXPECT_EQ(retRequest->bundleName, "testBundleName");
308     EXPECT_EQ(retRequest->abilityName, "testAbilityName");
309 }
310