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 <filesystem>
17 #include <gtest/gtest.h>
18 #include "account_log_wrapper.h"
19 #include "bundle_mgr_interface.h"
20 #include "if_system_ability_manager.h"
21 #include "ipc_skeleton.h"
22 #include "iservice_registry.h"
23 #define private public
24 #include "os_account_info.h"
25 #include "os_account_manager.h"
26 #undef private
27
28 #include "system_ability_definition.h"
29
30 using namespace testing::ext;
31 using namespace OHOS;
32 using namespace OHOS::AccountSA;
33 using namespace OHOS::AccountSA::Constants;
34 namespace {
CheckBundleName(std::vector<AppExecFwk::BundleInfo> checkList,string tarName)35 static bool CheckBundleName(std::vector<AppExecFwk::BundleInfo> checkList, string tarName)
36 {
37 for (auto i : checkList) {
38 if (i.name == tarName) {
39 return true;
40 }
41 }
42 return false;
43 }
44 }
45
46 class OsAccountManagerBmsTest : 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 OsAccountManagerBmsTest::SetUpTestCase(void)
55 {
56 #ifdef ACCOUNT_TEST
57 AccountFileOperator osAccountFileOperator;
58 osAccountFileOperator.DeleteDirOrFile(USER_INFO_BASE);
59 GTEST_LOG_(INFO) << "delete account test path " << USER_INFO_BASE;
60 #endif // ACCOUNT_TEST
61 }
62
TearDownTestCase(void)63 void OsAccountManagerBmsTest::TearDownTestCase(void)
64 {
65 #ifdef ACCOUNT_TEST
66 AccountFileOperator osAccountFileOperator;
67 osAccountFileOperator.DeleteDirOrFile(USER_INFO_BASE);
68 GTEST_LOG_(INFO) << "delete account test path " << USER_INFO_BASE;
69 #endif // ACCOUNT_TEST
70 }
71
SetUp(void)72 void OsAccountManagerBmsTest::SetUp(void) __attribute__((no_sanitize("cfi")))
73 {
74 testing::UnitTest *test = testing::UnitTest::GetInstance();
75 ASSERT_NE(test, nullptr);
76 const testing::TestInfo *testinfo = test->current_test_info();
77 ASSERT_NE(testinfo, nullptr);
78 string testCaseName = string(testinfo->name());
79 ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
80 }
81
TearDown(void)82 void OsAccountManagerBmsTest::TearDown(void)
83 {}
84
85 /**
86 * @tc.name: OsAccountManagerBmsTest001
87 * @tc.desc: create os account with disallowed hap list
88 * @tc.type: FUNC
89 * @tc.require:
90 */
91 HWTEST_F(OsAccountManagerBmsTest, OsAccountManagerBmsTest001, TestSize.Level1)
92 {
93 OsAccountInfo osAccountInfoOne;
94 CreateOsAccountOptions options;
95 options.disallowedHapList = {
96 "cn.openharmony.inputmethodchoosedialog",
97 "cn.openharmony.pasteboarddialog",
98 "com.example.kikakeyboard",
99 "com.ohos.UserFile.ExternalFileManager",
100 "com.ohos.adminprovisioning",
101 "com.ohos.amsdialog",
102 "com.ohos.calendardata",
103 "com.ohos.callui",
104 "com.ohos.camera",
105 };
106 EXPECT_EQ(ERR_OK,
107 OsAccountManager::CreateOsAccount("test", "test", OsAccountType::NORMAL, options, osAccountInfoOne));
108 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
109 ASSERT_NE(samgr, nullptr);
110 auto bundleObj = samgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
111 ASSERT_NE(bundleObj, nullptr);
112 auto bundleMgrProxy = iface_cast<AppExecFwk::IBundleMgr>(bundleObj);
113 ASSERT_NE(bundleMgrProxy, nullptr);
114 std::vector<AppExecFwk::BundleInfo> bundleInfos;
115 EXPECT_EQ(true, bundleMgrProxy->GetBundleInfos(
116 OHOS::AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, bundleInfos, osAccountInfoOne.GetLocalId()));
117 std::vector<AppExecFwk::BundleInfo> user0BundleInfos;
118 EXPECT_EQ(true,
119 bundleMgrProxy->GetBundleInfos(OHOS::AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, user0BundleInfos, 0));
120 bool flag = true;
121 for (auto j : options.disallowedHapList) {
122 if (!CheckBundleName(bundleInfos, j)) {
123 continue;
124 }
125 if (!CheckBundleName(user0BundleInfos, j)) {
126 flag = false;
127 break;
128 }
129 }
130 EXPECT_EQ(flag, true);
131 OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId());
132 }