1 /*
2 * Copyright (c) 2022-2024 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 "access_token.h"
19 #include "accesstoken_kit.h"
20 #include "app_manager_access_client.h"
21 #include "app_state_data.h"
22 #define private public
23 #include "audio_manager_adapter.h"
24 #undef private
25 #ifdef AUDIO_FRAMEWORK_ENABLE
26 #include "audio_policy_ipc_interface_code.h"
27 #endif
28 #include "camera_manager_adapter.h"
29 #include "permission_record_manager.h"
30 #include "token_setproc.h"
31
32 using namespace testing::ext;
33
34 namespace OHOS {
35 namespace Security {
36 namespace AccessToken {
37 class SensitiveManagerServiceTest : public testing::Test {
38 public:
39 static void SetUpTestCase();
40 static void TearDownTestCase();
41 void SetUp();
42 void TearDown();
43 };
44 static AccessTokenID g_selfTokenId = 0;
45 static PermissionStateFull g_testState1 = {
46 .permissionName = "ohos.permission.RUNNING_STATE_OBSERVER",
47 .isGeneral = true,
48 .resDeviceID = {"local"},
49 .grantStatus = {PermissionState::PERMISSION_GRANTED},
50 .grantFlags = {1}
51 };
52
53 static PermissionStateFull g_testState2 = {
54 .permissionName = "ohos.permission.MANAGE_CAMERA_CONFIG",
55 .isGeneral = true,
56 .resDeviceID = {"local"},
57 .grantStatus = {PermissionState::PERMISSION_GRANTED},
58 .grantFlags = {1}
59 };
60
61 static PermissionStateFull g_testState3 = {
62 .permissionName = "ohos.permission.GET_RUNNING_INFO",
63 .isGeneral = true,
64 .resDeviceID = {"local"},
65 .grantStatus = {PermissionState::PERMISSION_GRANTED},
66 .grantFlags = {1}
67 };
68
69 static PermissionStateFull g_testState4 = {
70 .permissionName = "ohos.permission.MANAGE_AUDIO_CONFIG",
71 .isGeneral = true,
72 .resDeviceID = {"local"},
73 .grantStatus = {PermissionState::PERMISSION_GRANTED},
74 .grantFlags = {1}
75 };
76
77 static PermissionStateFull g_testState5 = {
78 .permissionName = "ohos.permission.MICROPHONE_CONTROL",
79 .isGeneral = true,
80 .resDeviceID = {"local"},
81 .grantStatus = {PermissionState::PERMISSION_GRANTED},
82 .grantFlags = {1}
83 };
84
85 static HapPolicyParams g_PolicyPrams1 = {
86 .apl = APL_NORMAL,
87 .domain = "test.domain.A",
88 .permList = {},
89 .permStateList = {g_testState1, g_testState2, g_testState3, g_testState4, g_testState5}
90 };
91
92 static HapInfoParams g_InfoParms1 = {
93 .userID = 1,
94 .bundleName = "ohos.privacy_test.bundleA",
95 .instIndex = 0,
96 .appIDDesc = "privacy_test.bundleA"
97 };
98
99 static HapInfoParams g_infoManagerTestSystemInfoParms = {
100 .userID = 1,
101 .bundleName = "ohos.privacy_test.bundleB",
102 .instIndex = 0,
103 .appIDDesc = "privacy_test.bundleB",
104 .isSystemApp = true
105 };
SetUpTestCase()106 void SensitiveManagerServiceTest::SetUpTestCase()
107 {
108 g_selfTokenId = GetSelfTokenID();
109 }
110
TearDownTestCase()111 void SensitiveManagerServiceTest::TearDownTestCase()
112 {
113 }
114
SetUp()115 void SensitiveManagerServiceTest::SetUp()
116 {
117 AccessTokenKit::AllocHapToken(g_InfoParms1, g_PolicyPrams1);
118 AccessTokenID tokenId = AccessTokenKit::GetHapTokenID(g_InfoParms1.userID,
119 g_InfoParms1.bundleName,
120 g_InfoParms1.instIndex);
121 EXPECT_EQ(0, SetSelfTokenID(tokenId));
122 }
123
TearDown()124 void SensitiveManagerServiceTest::TearDown()
125 {
126 AccessTokenID tokenID = AccessTokenKit::GetHapTokenID(g_InfoParms1.userID,
127 g_InfoParms1.bundleName,
128 g_InfoParms1.instIndex);
129 AccessTokenKit::DeleteToken(tokenID);
130 EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId));
131 }
132
133 /*
134 * @tc.name: RegisterAppObserverTest001
135 * @tc.desc: test RegisterApplicationStateObserver with Callback is nullptr.
136 * @tc.type: FUNC
137 * @tc.require: issueI5RWXF
138 */
139 HWTEST_F(SensitiveManagerServiceTest, RegisterAppObserverTest001, TestSize.Level1)
140 {
141 ASSERT_NE(0, AppManagerAccessClient::GetInstance().RegisterApplicationStateObserver(nullptr));
142 ASSERT_NE(0, AppManagerAccessClient::GetInstance().UnregisterApplicationStateObserver(nullptr));
143 }
144
145 /*
146 * @tc.name: RegisterAppObserverTest002
147 * @tc.desc: test RegisterApplicationStateObserver with callback is not nullptr
148 * @tc.type: FUNC
149 * @tc.require: issueI5RWXF
150 */
151 HWTEST_F(SensitiveManagerServiceTest, RegisterAppObserverTest002, TestSize.Level1)
152 {
153 AccessTokenID tokenId = AccessTokenKit::GetNativeTokenId("privacy_service");
154 EXPECT_EQ(0, SetSelfTokenID(tokenId));
155
156 sptr<ApplicationStateObserverStub> listener = new(std::nothrow) ApplicationStateObserverStub();
157 ASSERT_NE(listener, nullptr);
158 ASSERT_EQ(0, AppManagerAccessClient::GetInstance().RegisterApplicationStateObserver(listener));
159
160 std::vector<AppStateData> list;
161 ASSERT_EQ(0, AppManagerAccessClient::GetInstance().GetForegroundApplications(list));
162 for (size_t i = 0; i < list.size(); ++i) {
163 ASSERT_NE(tokenId, list[i].accessTokenId);
164 }
165
166 ASSERT_EQ(0, AppManagerAccessClient::GetInstance().UnregisterApplicationStateObserver(listener));
167 }
168 } // namespace AccessToken
169 } // namespace Security
170 } // namespace OHOS
171