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 
16 #include <gtest/gtest.h>
17 
18 #include "account_log_wrapper.h"
19 #define private public
20 #include "app_account_control_manager.h"
21 #include "app_account_manager_service.h"
22 #undef private
23 #include "ipc_skeleton.h"
24 #include "app_account_manager_service.h"
25 
26 using namespace testing::ext;
27 using namespace OHOS;
28 using namespace OHOS::AccountSA;
29 using namespace OHOS::AppExecFwk;
30 
31 class AppAccountManagerServiceModuleTest : public testing::Test {
32 public:
33     static void SetUpTestCase(void);
34     static void TearDownTestCase(void);
35     void SetUp(void) override;
36     void TearDown(void) override;
37 };
38 
39 namespace {
40 std::shared_ptr<AppAccountManagerService> g_accountManagerService = std::make_shared<AppAccountManagerService>();
41 } // namespace
42 
SetUpTestCase(void)43 void AppAccountManagerServiceModuleTest::SetUpTestCase(void)
44 {}
45 
TearDownTestCase(void)46 void AppAccountManagerServiceModuleTest::TearDownTestCase(void)
47 {}
48 
SetUp(void)49 void AppAccountManagerServiceModuleTest::SetUp(void) __attribute__((no_sanitize("cfi")))
50 {
51     testing::UnitTest *test = testing::UnitTest::GetInstance();
52     ASSERT_NE(test, nullptr);
53     const testing::TestInfo *testinfo = test->current_test_info();
54     ASSERT_NE(testinfo, nullptr);
55     string testCaseName = string(testinfo->name());
56     ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
57 }
58 
TearDown(void)59 void AppAccountManagerServiceModuleTest::TearDown(void)
60 {}
61 
62 /**
63  * @tc.name: AppAccountManagerService_GetBundleNameAndCheckPerm_0100
64  * @tc.desc: test GetBundleNameAndCheckPerm VerifyPermission failed
65  * @tc.type: FUNC
66  * @tc.require: issueI5N90B
67  */
68 
69 HWTEST_F(AppAccountManagerServiceModuleTest, AppAccountManagerService_GetBundleNameAndCheckPerm_0100, TestSize.Level1)
70 {
71     ACCOUNT_LOGI("AppAccountManagerService_GetBundleNameAndCheckPerm_0100");
72 
73     int32_t callingUid;
74     std::string bundleName;
75     std::string permName = "";
76     int result = g_accountManagerService->GetBundleNameAndCheckPerm(callingUid, bundleName, permName);
77     EXPECT_EQ(result, ERR_ACCOUNT_COMMON_PERMISSION_DENIED);
78 }
79 
80 /**
81  * @tc.name: AppAccountManagerService_GetCallingInfo_0100
82  * @tc.desc: test GetCallingInfo GetCallingTokenInfoAndAppIndex faliled
83  * @tc.type: FUNC
84  * @tc.require: issueI5N90B
85  */
86 
87 HWTEST_F(AppAccountManagerServiceModuleTest, AppAccountManagerService_GetCallingInfo_0100, TestSize.Level1)
88 {
89     ACCOUNT_LOGI("AppAccountManagerService_GetCallingInfo_0100");
90 
91     int32_t callingUid;
92     std::string bundleName;
93     uint32_t appIndex;
94     int result = g_accountManagerService->GetCallingInfo(callingUid, bundleName, appIndex);
95     EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_APP_INDEX);
96 }
97 
98 /**
99  * @tc.name: AppAccountManagerService_GetAllAccounts_0100
100  * @tc.desc: test GetAllAccounts VerifyPermission faliled
101  * @tc.type: FUNC
102  * @tc.require: issueI5N90B
103  */
104 
105 HWTEST_F(AppAccountManagerServiceModuleTest, AppAccountManagerService_GetAllAccounts_0100, TestSize.Level1)
106 {
107     ACCOUNT_LOGI("AppAccountManagerService_GetAllAccounts_0100");
108 
109     std::string owner = "test";
110     std::vector<AppAccountInfo> appAccounts;
111     int result = g_accountManagerService->GetAllAccounts(owner, appAccounts);
112     EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_APP_INDEX);
113 }