• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 "privacy_test_common.h"
31 #include "token_setproc.h"
32 
33 using namespace testing::ext;
34 
35 namespace OHOS {
36 namespace Security {
37 namespace AccessToken {
38 class SensitiveManagerServiceTest : public testing::Test {
39 public:
40     static void SetUpTestCase();
41     static void TearDownTestCase();
42     void SetUp();
43     void TearDown();
44 };
45 static MockHapToken* g_mock = nullptr;
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     PrivacyTestCommon::SetTestEvironment(g_selfTokenId);
112 
113     std::vector<std::string> reqPerm;
114     reqPerm.emplace_back("ohos.permission.RUNNING_STATE_OBSERVER");
115     reqPerm.emplace_back("ohos.permission.MANAGE_CAMERA_CONFIG");
116     reqPerm.emplace_back("ohos.permission.GET_RUNNING_INFO");
117     reqPerm.emplace_back("ohos.permission.MANAGE_AUDIO_CONFIG");
118     reqPerm.emplace_back("ohos.permission.MICROPHONE_CONTROL");
119     g_mock = new (std::nothrow) MockHapToken("SensitiveManagerServiceTest", reqPerm);
120 }
121 
TearDownTestCase()122 void SensitiveManagerServiceTest::TearDownTestCase()
123 {
124     if (g_mock != nullptr) {
125         delete g_mock;
126         g_mock = nullptr;
127     }
128     PrivacyTestCommon::ResetTestEvironment();
129 }
130 
SetUp()131 void SensitiveManagerServiceTest::SetUp()
132 {
133 }
134 
TearDown()135 void SensitiveManagerServiceTest::TearDown()
136 {
137 }
138 
139 /*
140  * @tc.name: RegisterAppObserverTest001
141  * @tc.desc: test RegisterApplicationStateObserver with Callback is nullptr.
142  * @tc.type: FUNC
143  * @tc.require: issueI5RWXF
144  */
145 HWTEST_F(SensitiveManagerServiceTest, RegisterAppObserverTest001, TestSize.Level1)
146 {
147     ASSERT_NE(0, AppManagerAccessClient::GetInstance().RegisterApplicationStateObserver(nullptr));
148     ASSERT_NE(0, AppManagerAccessClient::GetInstance().UnregisterApplicationStateObserver(nullptr));
149 }
150 
151 /*
152  * @tc.name: RegisterAppObserverTest002
153  * @tc.desc: test RegisterApplicationStateObserver with callback is not nullptr
154  * @tc.type: FUNC
155  * @tc.require: issueI5RWXF
156  */
157 HWTEST_F(SensitiveManagerServiceTest, RegisterAppObserverTest002, TestSize.Level1)
158 {
159     MockNativeToken("privacy_service");
160     AccessTokenID tokenId = GetSelfTokenID();
161 
162     sptr<ApplicationStateObserverStub> listener = new(std::nothrow) ApplicationStateObserverStub();
163     ASSERT_NE(listener, nullptr);
164     ASSERT_EQ(0, AppManagerAccessClient::GetInstance().RegisterApplicationStateObserver(listener));
165 
166     std::vector<AppStateData> list;
167     ASSERT_EQ(0, AppManagerAccessClient::GetInstance().GetForegroundApplications(list));
168     for (size_t i = 0; i < list.size(); ++i) {
169         ASSERT_NE(tokenId, list[i].accessTokenId);
170     }
171 
172     ASSERT_EQ(0, AppManagerAccessClient::GetInstance().UnregisterApplicationStateObserver(listener));
173 }
174 } // namespace AccessToken
175 } // namespace Security
176 } // namespace OHOS
177