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 "access_token.h"
19 #include "accesstoken_kit.h"
20 #include "app_manager_access_client.h"
21 #include "app_manager_access_proxy.h"
22 #include "app_state_data.h"
23 #define private public
24 #include "audio_manager_privacy_client.h"
25 #undef private
26 #include "audio_manager_privacy_proxy.h"
27 #ifdef AUDIO_FRAMEWORK_ENABLE
28 #include "audio_policy_ipc_interface_code.h"
29 #endif
30 #include "camera_manager_privacy_client.h"
31 #include "camera_manager_privacy_proxy.h"
32 #include "token_setproc.h"
33
34 using namespace testing::ext;
35
36 namespace OHOS {
37 namespace Security {
38 namespace AccessToken {
39 class SensitiveManagerServiceTest : public testing::Test {
40 public:
41 static void SetUpTestCase();
42 static void TearDownTestCase();
43 void SetUp();
44 void TearDown();
45 };
46 static AccessTokenID g_selfTokenId = 0;
47 static PermissionStateFull g_testState1 = {
48 .permissionName = "ohos.permission.RUNNING_STATE_OBSERVER",
49 .isGeneral = true,
50 .resDeviceID = {"local"},
51 .grantStatus = {PermissionState::PERMISSION_GRANTED},
52 .grantFlags = {1}
53 };
54
55 static PermissionStateFull g_testState2 = {
56 .permissionName = "ohos.permission.MANAGE_CAMERA_CONFIG",
57 .isGeneral = true,
58 .resDeviceID = {"local"},
59 .grantStatus = {PermissionState::PERMISSION_GRANTED},
60 .grantFlags = {1}
61 };
62
63 static PermissionStateFull g_testState3 = {
64 .permissionName = "ohos.permission.GET_RUNNING_INFO",
65 .isGeneral = true,
66 .resDeviceID = {"local"},
67 .grantStatus = {PermissionState::PERMISSION_GRANTED},
68 .grantFlags = {1}
69 };
70
71 static PermissionStateFull g_testState4 = {
72 .permissionName = "ohos.permission.MANAGE_AUDIO_CONFIG",
73 .isGeneral = true,
74 .resDeviceID = {"local"},
75 .grantStatus = {PermissionState::PERMISSION_GRANTED},
76 .grantFlags = {1}
77 };
78
79 static PermissionStateFull g_testState5 = {
80 .permissionName = "ohos.permission.MICROPHONE_CONTROL",
81 .isGeneral = true,
82 .resDeviceID = {"local"},
83 .grantStatus = {PermissionState::PERMISSION_GRANTED},
84 .grantFlags = {1}
85 };
86
87 static HapPolicyParams g_PolicyPrams1 = {
88 .apl = APL_NORMAL,
89 .domain = "test.domain.A",
90 .permList = {},
91 .permStateList = {g_testState1, g_testState2, g_testState3, g_testState4, g_testState5}
92 };
93
94 static HapInfoParams g_InfoParms1 = {
95 .userID = 1,
96 .bundleName = "ohos.privacy_test.bundleA",
97 .instIndex = 0,
98 .appIDDesc = "privacy_test.bundleA"
99 };
100
101 static HapInfoParams g_infoManagerTestSystemInfoParms = {
102 .userID = 1,
103 .bundleName = "ohos.privacy_test.bundleB",
104 .instIndex = 0,
105 .appIDDesc = "privacy_test.bundleB",
106 .isSystemApp = true
107 };
SetUpTestCase()108 void SensitiveManagerServiceTest::SetUpTestCase()
109 {
110 g_selfTokenId = GetSelfTokenID();
111 }
112
TearDownTestCase()113 void SensitiveManagerServiceTest::TearDownTestCase()
114 {
115 }
116
SetUp()117 void SensitiveManagerServiceTest::SetUp()
118 {
119 AccessTokenKit::AllocHapToken(g_InfoParms1, g_PolicyPrams1);
120 AccessTokenID tokenId = AccessTokenKit::GetHapTokenID(g_InfoParms1.userID,
121 g_InfoParms1.bundleName,
122 g_InfoParms1.instIndex);
123 EXPECT_EQ(0, SetSelfTokenID(tokenId));
124 }
125
TearDown()126 void SensitiveManagerServiceTest::TearDown()
127 {
128 AccessTokenID tokenID = AccessTokenKit::GetHapTokenID(g_InfoParms1.userID,
129 g_InfoParms1.bundleName,
130 g_InfoParms1.instIndex);
131 AccessTokenKit::DeleteToken(tokenID);
132 EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId));
133 }
134
135 /*
136 * @tc.name: SetMicroMuteTest001
137 * @tc.desc: test set/get mute staus of microphone
138 * @tc.type: FUNC
139 * @tc.require: issueI5RWXF
140 */
141 HWTEST_F(SensitiveManagerServiceTest, SetMicroMuteTest001, TestSize.Level1)
142 {
143 bool initMute = AudioManagerPrivacyClient::GetInstance().GetPersistentMicMuteState();
144
145 AudioManagerPrivacyClient::GetInstance().SetMicrophoneMutePersistent(false, PolicyType::PRIVACY);
146 EXPECT_EQ(false, AudioManagerPrivacyClient::GetInstance().GetPersistentMicMuteState());
147
148 AudioManagerPrivacyClient::GetInstance().SetMicrophoneMutePersistent(true, PolicyType::PRIVACY);
149 EXPECT_EQ(true, AudioManagerPrivacyClient::GetInstance().GetPersistentMicMuteState());
150
151 AudioManagerPrivacyClient::GetInstance().SetMicrophoneMutePersistent(false, PolicyType::PRIVACY);
152 EXPECT_EQ(false, AudioManagerPrivacyClient::GetInstance().GetPersistentMicMuteState());
153
154 AudioManagerPrivacyClient::GetInstance().SetMicrophoneMutePersistent(initMute, PolicyType::PRIVACY);
155 }
156
157 /*
158 * @tc.name: SetCameraMuteTest001
159 * @tc.desc: test set/get mute staus of camera
160 * @tc.type: FUNC
161 * @tc.require: issueI5RWXF
162 */
163 HWTEST_F(SensitiveManagerServiceTest, SetCameraMuteTest001, TestSize.Level1)
164 {
165 AccessTokenID tokenId = AccessTokenKit::GetNativeTokenId("privacy_service");
166 EXPECT_EQ(0, SetSelfTokenID(tokenId));
167
168 bool initMute = CameraManagerPrivacyClient::GetInstance().IsCameraMuted();
169
170 CameraManagerPrivacyClient::GetInstance().MuteCameraPersist(PolicyType::PRIVACY, false);
171 EXPECT_EQ(false, CameraManagerPrivacyClient::GetInstance().IsCameraMuted());
172
173 CameraManagerPrivacyClient::GetInstance().MuteCameraPersist(PolicyType::PRIVACY, true);
174 EXPECT_EQ(true, CameraManagerPrivacyClient::GetInstance().IsCameraMuted());
175
176 CameraManagerPrivacyClient::GetInstance().MuteCameraPersist(PolicyType::PRIVACY, false);
177 EXPECT_EQ(false, CameraManagerPrivacyClient::GetInstance().IsCameraMuted());
178
179 CameraManagerPrivacyClient::GetInstance().MuteCameraPersist(PolicyType::PRIVACY, initMute);
180 }
181
182 /*
183 * @tc.name: RegisterAppObserverTest001
184 * @tc.desc: test RegisterApplicationStateObserver with Callback is nullptr.
185 * @tc.type: FUNC
186 * @tc.require: issueI5RWXF
187 */
188 HWTEST_F(SensitiveManagerServiceTest, RegisterAppObserverTest001, TestSize.Level1)
189 {
190 ASSERT_NE(0, AppManagerAccessClient::GetInstance().RegisterApplicationStateObserver(nullptr));
191 ASSERT_NE(0, AppManagerAccessClient::GetInstance().UnregisterApplicationStateObserver(nullptr));
192 }
193
194 /*
195 * @tc.name: RegisterAppObserverTest002
196 * @tc.desc: test RegisterApplicationStateObserver with callback is not nullptr
197 * @tc.type: FUNC
198 * @tc.require: issueI5RWXF
199 */
200 HWTEST_F(SensitiveManagerServiceTest, RegisterAppObserverTest002, TestSize.Level1)
201 {
202 AccessTokenID tokenId = AccessTokenKit::GetNativeTokenId("privacy_service");
203 EXPECT_EQ(0, SetSelfTokenID(tokenId));
204
205 sptr<ApplicationStateObserverStub> listener = new(std::nothrow) ApplicationStateObserverStub();
206 ASSERT_NE(listener, nullptr);
207 ASSERT_EQ(0, AppManagerAccessClient::GetInstance().RegisterApplicationStateObserver(listener));
208
209 std::vector<AppStateData> list;
210 ASSERT_EQ(0, AppManagerAccessClient::GetInstance().GetForegroundApplications(list));
211 for (size_t i = 0; i < list.size(); ++i) {
212 ASSERT_NE(tokenId, list[i].accessTokenId);
213 }
214
215 ASSERT_EQ(0, AppManagerAccessClient::GetInstance().UnregisterApplicationStateObserver(listener));
216 }
217 } // namespace AccessToken
218 } // namespace Security
219 } // namespace OHOS
220