1 /*
2  * Copyright (c) 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 <gtest/gtest.h>
17 
18 #include <thread>
19 #include <gmock/gmock.h>
20 #include "account_log_wrapper.h"
21 #include "datetime_ex.h"
22 #define private public
23 #include "app_account_common.h"
24 #include "app_account_manager_service.h"
25 #undef private
26 
27 using namespace testing::ext;
28 using namespace OHOS;
29 using namespace OHOS::AccountSA;
30 using namespace OHOS::AppExecFwk;
31 #ifdef HAS_CES_PART
32 using namespace OHOS::EventFwk;
33 #endif // HAS_CES_PART
34 namespace {
35 const std::string STRING_NAME = "name";
36 const std::string AUTH_TYPE = "type";
37 const std::string STRING_CREDENTIAL_TYPE = "password";
38 const std::string KEY = "testkey";
39 const std::string OWNER = "owner";
40 const std::string STRING_EXTRA_INFO = "extra_info";
41 const std::string STRING_BUNDLE_NAME = "com.example.third_party";
42 std::shared_ptr<AppAccountManagerService> g_accountManagerService =
43     std::make_shared<AppAccountManagerService>();
44 }  // namespace
45 
46 class AppAccountManagerServiceNotMockModuleTest : public testing::Test {
47 public:
48     static void SetUpTestCase(void);
49     static void TearDownTestCase(void);
50     void SetUp(void) override;
51     void TearDown(void) override;
52 };
53 
SetUpTestCase(void)54 void AppAccountManagerServiceNotMockModuleTest::SetUpTestCase(void)
55 {}
56 
TearDownTestCase(void)57 void AppAccountManagerServiceNotMockModuleTest::TearDownTestCase(void)
58 {}
59 
SetUp(void)60 void AppAccountManagerServiceNotMockModuleTest::SetUp(void) __attribute__((no_sanitize("cfi")))
61 {
62     testing::UnitTest *test = testing::UnitTest::GetInstance();
63     ASSERT_NE(test, nullptr);
64     const testing::TestInfo *testinfo = test->current_test_info();
65     ASSERT_NE(testinfo, nullptr);
66     string testCaseName = string(testinfo->name());
67     ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
68 }
69 
TearDown(void)70 void AppAccountManagerServiceNotMockModuleTest::TearDown(void)
71 {}
72 
73 /**
74  * @tc.name: AppAccountManagerService_CreateAccount_001
75  * @tc.desc: test CreateAccount failed get calling info
76  * @tc.type: FUNC
77  * @tc.require:
78  */
79 
80 HWTEST_F(AppAccountManagerServiceNotMockModuleTest, AppAccountManagerService_CreateAccount_001, TestSize.Level1)
81 {
82     CreateAccountOptions option;
83     ErrCode result = g_accountManagerService->CreateAccount(STRING_NAME, option);
84     EXPECT_NE(result, ERR_OK);
85 }
86 
87 /**
88  * @tc.name: AppAccountManagerService_AppAccountManagerService_001
89  * @tc.desc: test AppAccountManagerService failed get calling info
90  * @tc.type: FUNC
91  * @tc.require:
92  */
93 
94 HWTEST_F(
95     AppAccountManagerServiceNotMockModuleTest, AppAccountManagerService_AppAccountManagerService_001, TestSize.Level1)
96 {
97     bool syncEnable = false;
98     ErrCode result = g_accountManagerService->CheckAppAccountSyncEnable(STRING_NAME, syncEnable);
99     EXPECT_NE(result, ERR_OK);
100 
101     result = g_accountManagerService->SetAppAccess(STRING_NAME, STRING_BUNDLE_NAME, false);
102     EXPECT_NE(result, ERR_OK);
103 
104     result = g_accountManagerService->EnableAppAccess(STRING_NAME, STRING_BUNDLE_NAME);
105     EXPECT_NE(result, ERR_OK);
106 
107     result = g_accountManagerService->DisableAppAccess(STRING_NAME, STRING_BUNDLE_NAME);
108     EXPECT_NE(result, ERR_OK);
109 
110     std::string extraInfo;
111     result = g_accountManagerService->GetAccountExtraInfo(STRING_NAME, extraInfo);
112     EXPECT_NE(result, ERR_OK);
113 
114     result = g_accountManagerService->DeleteAccount(STRING_NAME);
115     EXPECT_NE(result, ERR_OK);
116 
117     CreateAccountOptions option;
118     result = g_accountManagerService->CreateAccount(STRING_NAME, option);
119     EXPECT_NE(result, ERR_OK);
120 
121     AppAccountSubscribeInfo subscribeInfo;
122     result = g_accountManagerService->SubscribeAppAccount(subscribeInfo, nullptr);
123     EXPECT_NE(result, ERR_OK);
124 }
125 
126 /**
127  * @tc.name: AppAccountManagerService_SetAppAccountSyncEnable_002
128  * @tc.desc: test appAccountManagerService failed get calling info
129  * @tc.type: FUNC
130  * @tc.require:
131  */
132 
133 HWTEST_F(
134     AppAccountManagerServiceNotMockModuleTest, AppAccountManagerService_AppAccountManagerService_002, TestSize.Level1)
135 {
136     ErrCode result = g_accountManagerService->SetAppAccountSyncEnable(STRING_NAME, false);
137     EXPECT_NE(result, ERR_OK);
138 
139     std::string value;
140     result = g_accountManagerService->GetAssociatedData(STRING_NAME, KEY, value);
141     EXPECT_NE(result, ERR_OK);
142 
143     result = g_accountManagerService->SetAssociatedData(STRING_NAME, KEY, value);
144     EXPECT_NE(result, ERR_OK);
145 
146     std::string credential;
147     result = g_accountManagerService->GetAccountCredential(STRING_NAME, STRING_CREDENTIAL_TYPE, credential);
148     EXPECT_NE(result, ERR_OK);
149 
150     result = g_accountManagerService->SetAccountCredential(STRING_NAME, STRING_CREDENTIAL_TYPE, credential);
151     EXPECT_NE(result, ERR_OK);
152 
153     AuthenticatorSessionRequest request;
154     result = g_accountManagerService->GetTokenVisibilityParam(STRING_NAME, AUTH_TYPE, STRING_BUNDLE_NAME, request);
155     EXPECT_NE(result, ERR_OK);
156 
157     result = g_accountManagerService->SetOAuthTokenVisibility(STRING_NAME, AUTH_TYPE, STRING_BUNDLE_NAME, false);
158     EXPECT_NE(result, ERR_OK);
159 
160     bool isVisible = false;
161     result = g_accountManagerService->SetAuthTokenVisibility(STRING_NAME, AUTH_TYPE, STRING_BUNDLE_NAME, isVisible);
162     EXPECT_NE(result, ERR_OK);
163 
164     result = g_accountManagerService->CheckOAuthTokenVisibility(STRING_NAME, AUTH_TYPE, STRING_BUNDLE_NAME, isVisible);
165     EXPECT_NE(result, ERR_OK);
166 
167     result = g_accountManagerService->CheckAuthTokenVisibility(STRING_NAME, AUTH_TYPE, STRING_BUNDLE_NAME, isVisible);
168     EXPECT_NE(result, ERR_OK);
169 
170     AuthenticatorInfo info;
171     result = g_accountManagerService->GetAuthenticatorInfo(OWNER, info);
172     EXPECT_NE(result, ERR_OK);
173 
174     std::vector<OAuthTokenInfo> tokenInfos;
175     result = g_accountManagerService->GetAllOAuthTokens(STRING_NAME, OWNER, tokenInfos);
176     EXPECT_NE(result, ERR_OK);
177 }
178 
179 /**
180  * @tc.name: AppAccountManagerService_SetAppAccountSyncEnable_003
181  * @tc.desc: test appAccountManagerService failed get calling info
182  * @tc.type: FUNC
183  * @tc.require:
184  */
185 
186 HWTEST_F(
187     AppAccountManagerServiceNotMockModuleTest, AppAccountManagerService_AppAccountManagerService_003, TestSize.Level1)
188 {
189     std::set<std::string> oauthList;
190     ErrCode result = g_accountManagerService->GetOAuthList(STRING_NAME, AUTH_TYPE, oauthList);
191     EXPECT_NE(result, ERR_OK);
192 
193     result = g_accountManagerService->GetAuthList(STRING_NAME, AUTH_TYPE, oauthList);
194     EXPECT_NE(result, ERR_OK);
195 
196     result = g_accountManagerService->GetAuthList(STRING_NAME, AUTH_TYPE, oauthList);
197     EXPECT_NE(result, ERR_OK);
198 
199     std::vector<AppAccountInfo> appAccounts;
200     result = g_accountManagerService->GetAllAccounts(OWNER, appAccounts);
201     EXPECT_NE(result, ERR_OK);
202 
203     result = g_accountManagerService->GetAllAccessibleAccounts(appAccounts);
204     EXPECT_NE(result, ERR_OK);
205 
206     result = g_accountManagerService->QueryAllAccessibleAccounts(OWNER, appAccounts);
207     EXPECT_NE(result, ERR_OK);
208 
209     bool isAccessible = false;
210     result = g_accountManagerService->CheckAppAccess(STRING_NAME, STRING_BUNDLE_NAME, isAccessible);
211     EXPECT_NE(result, ERR_OK);
212 
213     result = g_accountManagerService->DeleteAccountCredential(STRING_NAME, AUTH_TYPE);
214     EXPECT_NE(result, ERR_OK);
215 
216     SelectAccountsOptions option;
217     result = g_accountManagerService->SelectAccountsByOptions(option, nullptr);
218     EXPECT_NE(result, ERR_OK);
219 
220     VerifyCredentialOptions options;
221     result = g_accountManagerService->VerifyCredential(STRING_NAME, OWNER, options, nullptr);
222     EXPECT_NE(result, ERR_OK);
223 
224     std::vector<std::string> labels;
225     result = g_accountManagerService->CheckAccountLabels(STRING_NAME, OWNER, labels, nullptr);
226     EXPECT_NE(result, ERR_OK);
227 
228     SetPropertiesOptions setOptions;
229     result = g_accountManagerService->SetAuthenticatorProperties(OWNER, setOptions, nullptr);
230     EXPECT_NE(result, ERR_OK);
231 }