• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include <iostream>
16 
17 #include "accesstoken_kit.h"
18 #include "nativetoken_kit.h"
19 #include "token_setproc.h"
20 #include "wm_collector.h"
21 
22 #include <gtest/gtest.h>
23 
24 using namespace testing::ext;
25 using namespace OHOS::HiviewDFX;
26 using namespace OHOS::HiviewDFX::UCollectUtil;
27 using namespace OHOS::HiviewDFX::UCollect;
28 
29 class WmCollectorTest : public testing::Test {
30 public:
SetUp()31     void SetUp() {};
TearDown()32     void TearDown() {};
SetUpTestCase()33     static void SetUpTestCase() {};
TearDownTestCase()34     static void TearDownTestCase() {};
35 };
36 
37 namespace {
NativeTokenGet(const char * perms[],int size)38 void NativeTokenGet(const char* perms[], int size)
39 {
40     uint64_t tokenId;
41     NativeTokenInfoParams infoInstance = {
42         .dcapsNum = 0,
43         .permsNum = size,
44         .aclsNum = 0,
45         .dcaps = nullptr,
46         .perms = perms,
47         .acls = nullptr,
48         .aplStr = "system_basic",
49     };
50 
51     infoInstance.processName = "UCollectionUtilityUnitTest";
52     tokenId = GetAccessTokenId(&infoInstance);
53     SetSelfTokenID(tokenId);
54     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
55 }
56 
EnablePermissionAccess()57 void EnablePermissionAccess()
58 {
59     const char* perms[] = {
60         "ohos.permission.DUMP",
61     };
62     NativeTokenGet(perms, 1); // 1 is the size of the array which consists of required permissions.
63 }
64 
DisablePermissionAccess()65 void DisablePermissionAccess()
66 {
67     NativeTokenGet(nullptr, 0); // empty permission array.
68 }
69 }
70 
71 #ifdef UNIFIED_COLLECTOR_WM_ENABLE
72 /**
73  * @tc.name: WmCollectorTest001
74  * @tc.desc: used to test WmCollector.ExportWindowsInfo
75  * @tc.type: FUNC
76 */
77 HWTEST_F(WmCollectorTest, WmCollectorTest001, TestSize.Level1)
78 {
79     EnablePermissionAccess();
80     std::shared_ptr<WmCollector> collector = WmCollector::Create();
81     auto result = collector->ExportWindowsInfo();
82     std::cout << "export windows info result " << result.retCode << std::endl;
83     ASSERT_TRUE(result.retCode == UcError::SUCCESS);
84     DisablePermissionAccess();
85 }
86 
87 /**
88  * @tc.name: WmCollectorTest002
89  * @tc.desc: used to test WmCollector.ExportWindowsMemory
90  * @tc.type: FUNC
91 */
92 HWTEST_F(WmCollectorTest, WmCollectorTest002, TestSize.Level1)
93 {
94     EnablePermissionAccess();
95     std::shared_ptr<WmCollector> collector = WmCollector::Create();
96     auto result = collector->ExportWindowsMemory();
97     std::cout << "export windows memory result " << result.retCode << std::endl;
98     ASSERT_TRUE(result.retCode == UcError::SUCCESS);
99     DisablePermissionAccess();
100 }
101 
102 /**
103  * @tc.name: WmCollectorTest003
104  * @tc.desc: used to test WmCollector.ExportGpuMemory
105  * @tc.type: FUNC
106 */
107 HWTEST_F(WmCollectorTest, WmCollectorTest003, TestSize.Level1)
108 {
109     std::shared_ptr<WmCollector> collector = WmCollector::Create();
110     auto result = collector->ExportGpuMemory();
111     std::cout << "export Gpu memory result " << result.retCode << std::endl;
112     ASSERT_TRUE(result.retCode == UcError::SUCCESS || result.retCode == UcError::UNSUPPORT);
113 }
114 #else
115 /**
116  * @tc.name: WmCollectorTest001
117  * @tc.desc: used to test empty WmCollector
118  * @tc.type: FUNC
119 */
120 HWTEST_F(WmCollectorTest, WmCollectorTest001, TestSize.Level1)
121 {
122     std::shared_ptr<WmCollector> collector = WmCollector::Create();
123     auto result1 = collector->ExportWindowsInfo();
124     ASSERT_TRUE(result1.retCode == UcError::FEATURE_CLOSED);
125 
126     auto result2 = collector->ExportWindowsMemory();
127     ASSERT_TRUE(result2.retCode == UcError::FEATURE_CLOSED);
128 
129     auto result3 = collector->ExportGpuMemory();
130     ASSERT_TRUE(result3.retCode == UcError::FEATURE_CLOSED);
131 }
132 #endif
133