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