• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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)
44 {}
45 
TearDown(void)46 void AppAccountCommonTest::TearDown(void)
47 {}
48 /**
49  * @tc.name: SelectAccountsOptions Marshalling test
50  * @tc.desc: Func Marshalling.
51  * @tc.type: FUNC
52  * @tc.require issueI5RWXN
53  */
54 HWTEST_F(AppAccountCommonTest, Marshalling001, TestSize.Level0)
55 {
56     ACCOUNT_LOGI("Marshalling001");
57     Parcel Parcel;
58     SelectAccountsOptions option1;
59     option1.hasAccounts = true;
60     option1.hasOwners = true;
61     option1.hasLabels = true;
62     option1.allowedOwners.emplace_back("test1");
63     option1.requiredLabels.emplace_back("test2");
64 
65     EXPECT_EQ(option1.Marshalling(Parcel), true);
66     SelectAccountsOptions *option2 = option1.Unmarshalling(Parcel);
67     EXPECT_NE(option2, nullptr);
68 
69     EXPECT_EQ(option2->hasAccounts, true);
70     EXPECT_EQ(option2->hasOwners, true);
71     EXPECT_EQ(option2->hasLabels, true);
72     EXPECT_EQ(option2->allowedOwners[0], "test1");
73     EXPECT_EQ(option2->requiredLabels[0], "test2");
74 }
75 
76 /**
77  * @tc.name: VerifyCredentialOptions Marshalling test
78  * @tc.desc: Func Marshalling.
79  * @tc.type: FUNC
80  * @tc.require issueI5RWXN
81  */
82 HWTEST_F(AppAccountCommonTest, Marshalling002, TestSize.Level0)
83 {
84     ACCOUNT_LOGI("Marshalling002");
85     Parcel Parcel;
86     VerifyCredentialOptions option1;
87     option1.credentialType = "test1";
88     option1.credential = "test2";
89 
90     EXPECT_EQ(option1.Marshalling(Parcel), true);
91     VerifyCredentialOptions *option2 = option1.Unmarshalling(Parcel);
92     EXPECT_NE(option2, nullptr);
93 
94     EXPECT_EQ(option2->credentialType, "test1");
95     EXPECT_EQ(option2->credential, "test2");
96 }
97 
98 /**
99  * @tc.name: CreateAccountOptions Marshalling test
100  * @tc.desc: Func Marshalling.
101  * @tc.type: FUNC
102  * @tc.require issueI5RWXN
103  */
104 HWTEST_F(AppAccountCommonTest, Marshalling003, TestSize.Level0)
105 {
106     ACCOUNT_LOGI("Marshalling003");
107     Parcel Parcel;
108     CreateAccountOptions option1;
109     option1.customData["test"] = "test2";
110 
111     EXPECT_EQ(option1.Marshalling(Parcel), true);
112     CreateAccountOptions *option2 = option1.Unmarshalling(Parcel);
113     EXPECT_NE(option2, nullptr);
114 
115     EXPECT_EQ(option2->customData["test"], "test2");
116 }
117 
118 /**
119  * @tc.name: CreateAccountImplicitlyOptions Marshalling test
120  * @tc.desc: Func Marshalling.
121  * @tc.type: FUNC
122  * @tc.require issueI5RWXN
123  */
124 HWTEST_F(AppAccountCommonTest, Marshalling004, TestSize.Level0)
125 {
126     ACCOUNT_LOGI("Marshalling004");
127     Parcel Parcel;
128     CreateAccountImplicitlyOptions option1;
129     option1.hasAuthType = true;
130     option1.hasRequiredLabels = true;
131     option1.authType = "test1";
132     option1.requiredLabels.emplace_back("test2");
133 
134     EXPECT_EQ(option1.Marshalling(Parcel), true);
135     CreateAccountImplicitlyOptions *option2 = option1.Unmarshalling(Parcel);
136     EXPECT_NE(option2, nullptr);
137 
138     EXPECT_EQ(option2->hasAuthType, true);
139     EXPECT_EQ(option2->hasRequiredLabels, true);
140     EXPECT_EQ(option2->authType, "test1");
141     EXPECT_EQ(option2->requiredLabels[0], "test2");
142 }
143 
144 /**
145  * @tc.name: CreateAccountOptions Marshalling test
146  * @tc.desc: test ReadFromParcel of oversize customData.
147  * @tc.type: FUNC
148  * @tc.require issueI5RWXN
149  */
150 HWTEST_F(AppAccountCommonTest, Marshalling005, TestSize.Level0)
151 {
152     ACCOUNT_LOGI("Marshalling005");
153     Parcel Parcel;
154     CreateAccountOptions srcOptions;
155 
156     for (int i = 0; i < MAX_CUSTOM_DATA_SIZE + 1; i++) {
157         std::string test_key = "test_key" + std::to_string(i);
158         std::string test_value = "test_value" + std::to_string(i);
159         srcOptions.customData.emplace(test_key, test_value);
160     }
161 
162     EXPECT_EQ(srcOptions.Marshalling(Parcel), true);
163     CreateAccountOptions *testOptions = srcOptions.Unmarshalling(Parcel);
164     EXPECT_EQ(testOptions, nullptr);
165 }
166 
167 /**
168  * @tc.name: SelectAccountsOptions Marshalling test
169  * @tc.desc: test ReadFromParcel.
170  * @tc.type: FUNC
171  * @tc.require issueI5RWXN
172  */
173 HWTEST_F(AppAccountCommonTest, Marshalling006, TestSize.Level0)
174 {
175     ACCOUNT_LOGI("Marshalling006");
176     Parcel testParcel;
177     SetPropertiesOptions options;
178     bool result = options.Marshalling(testParcel);
179     ASSERT_EQ(result, true);
180     result = options.Unmarshalling(testParcel);
181     ASSERT_EQ(result, true);
182 }
183 
184 /**
185  * @tc.name: SelectAccountsOptions Marshalling test
186  * @tc.desc: Func Marshalling allowedAccounts is oversize.
187  * @tc.type: FUNC
188  * @tc.require issueI5RWXN
189  */
190 HWTEST_F(AppAccountCommonTest, Marshalling007, TestSize.Level0)
191 {
192     Parcel Parcel;
193     SelectAccountsOptions option1;
194     option1.hasAccounts = true;
195     option1.hasOwners = true;
196     option1.hasLabels = true;
197     for (int i = 0; i <= MAX_VEC_SIZE; i++) {
198         std::string key = std::to_string(i);
199         std::string value = "test" + std::to_string(i);
200         option1.allowedAccounts.emplace_back(std::pair<std::string, std::string>(key, value));
201     }
202     option1.requiredLabels.emplace_back("test2");
203 
204     EXPECT_EQ(option1.Marshalling(Parcel), true);
205     SelectAccountsOptions *option2 = option1.Unmarshalling(Parcel);
206     EXPECT_EQ(option2, nullptr);
207 }
208 
209 /**
210  * @tc.name: ConvertOtherJSErrCodeV8 test
211  * @tc.desc: Func ConvertOtherJSErrCodeV8.
212  * @tc.type: FUNC
213  * @tc.require issueI5RWXN
214  */
215 HWTEST_F(AppAccountCommonTest, ConvertOtherJSErrCodeV8001, TestSize.Level0)
216 {
217     ACCOUNT_LOGI("ConvertOtherJSErrCodeV8001");
218     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_OK), ERR_JS_SUCCESS_V8);
219     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_ACCOUNT_NOT_EXIST), ERR_JS_ACCOUNT_NOT_EXIST);
220     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_OAUTH_AUTHENTICATOR_NOT_EXIST),
221         ERR_JS_OAUTH_AUTHENTICATOR_NOT_EXIST);
222     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_OAUTH_BUSY), ERR_JS_OAUTH_SERVICE_BUSY);
223     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_OAUTH_LIST_MAX_SIZE), ERR_JS_OAUTH_LIST_TOO_LARGE);
224     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_OAUTH_SESSION_NOT_EXIST), ERR_JS_OAUTH_SESSION_NOT_EXIST);
225     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_NOT_EXIST), ERR_JS_OAUTH_TOKEN_NOT_EXIST);
226     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_MAX_SIZE), ERR_JS_OAUTH_TOKEN_TOO_MANY);
227     EXPECT_EQ(ConvertToJSErrCodeV8(INVALID_ERR_CODE), ERR_JS_APP_ACCOUNT_SERVICE_EXCEPTION);
228 }
229 
230 /**
231  * @tc.name: ConvertToJSErrCodeV8 test
232  * @tc.desc: Func ConvertOtherJSErrCodeV8.
233  * @tc.type: FUNC
234  * @tc.require issueI5RWXN
235  */
236 HWTEST_F(AppAccountCommonTest, ConvertToJSErrCodeV8001, TestSize.Level0)
237 {
238     ACCOUNT_LOGI("ConvertToJSErrCodeV8001");
239     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_KIT_NAME_IS_EMPTY), ERR_JS_INVALID_REQUEST);
240     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_NAME_IS_EMPTY), ERR_JS_INVALID_REQUEST);
241     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_ADD_EXISTING_ACCOUNT), ERR_JS_INVALID_REQUEST);
242     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_KIT_READ_PARCELABLE_APP_ACCOUNT_INFO), ERR_JS_INVALID_RESPONSE);
243     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_OAUTH_INVALID_RESPONSE), ERR_JS_INVALID_RESPONSE);
244     EXPECT_EQ(
245         ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_OAUTH_AUTHENTICATOR_CALLBACK_NOT_EXIST), ERR_JS_INVALID_RESPONSE);
246     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_PERMISSION_DENIED), ERR_JS_PERMISSION_DENIED_V8);
247     EXPECT_EQ(ConvertToJSErrCodeV8(ERR_APPACCOUNT_SERVICE_SUBSCRIBE_PERMISSION_DENIED), ERR_JS_PERMISSION_DENIED_V8);
248 }