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 namespace {
NativeTokenGet(const char * perms[],int size)29 void NativeTokenGet(const char* perms[], int size)
30 {
31 uint64_t tokenId;
32 NativeTokenInfoParams infoInstance = {
33 .dcapsNum = 0,
34 .permsNum = size,
35 .aclsNum = 0,
36 .dcaps = nullptr,
37 .perms = perms,
38 .acls = nullptr,
39 .aplStr = "system_basic",
40 };
41
42 infoInstance.processName = "UCollectionUtilityUnitTest";
43 tokenId = GetAccessTokenId(&infoInstance);
44 SetSelfTokenID(tokenId);
45 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
46 }
47
EnablePermissionAccess()48 void EnablePermissionAccess()
49 {
50 const char* perms[] = {
51 "ohos.permission.DUMP",
52 };
53 NativeTokenGet(perms, 1); // 1 is the size of the array which consists of required permissions.
54 }
55
DisablePermissionAccess()56 void DisablePermissionAccess()
57 {
58 NativeTokenGet(nullptr, 0); // empty permission array.
59 }
60 }
61
62 class WmCollectorTest : public testing::Test {
63 public:
SetUp()64 void SetUp() {};
TearDown()65 void TearDown() {};
SetUpTestCase()66 static void SetUpTestCase() {};
TearDownTestCase()67 static void TearDownTestCase() {};
68 };
69
70 /**
71 * @tc.name: WmCollectorTest001
72 * @tc.desc: used to test WmCollector.ExportWindowsInfo
73 * @tc.type: FUNC
74 */
75 HWTEST_F(WmCollectorTest, WmCollectorTest001, TestSize.Level1)
76 {
77 EnablePermissionAccess();
78 std::shared_ptr<WmCollector> collector = WmCollector::Create();
79 auto result = collector->ExportWindowsInfo();
80 std::cout << "export windows info result " << result.retCode << std::endl;
81 ASSERT_TRUE(result.retCode == UcError::SUCCESS);
82 DisablePermissionAccess();
83 }
84
85 /**
86 * @tc.name: WmCollectorTest002
87 * @tc.desc: used to test WmCollector.ExportWindowsMemory
88 * @tc.type: FUNC
89 */
90 HWTEST_F(WmCollectorTest, WmCollectorTest002, TestSize.Level1)
91 {
92 EnablePermissionAccess();
93 std::shared_ptr<WmCollector> collector = WmCollector::Create();
94 auto result = collector->ExportWindowsMemory();
95 std::cout << "export windows memory result " << result.retCode << std::endl;
96 ASSERT_TRUE(result.retCode == UcError::SUCCESS);
97 DisablePermissionAccess();
98 }
99
100 /**
101 * @tc.name: WmCollectorTest003
102 * @tc.desc: used to test WmCollector.ExportGpuMemory
103 * @tc.type: FUNC
104 */
105 HWTEST_F(WmCollectorTest, WmCollectorTest003, TestSize.Level1)
106 {
107 std::shared_ptr<WmCollector> collector = WmCollector::Create();
108 auto result = collector->ExportGpuMemory();
109 std::cout << "export Gpu memory result " << result.retCode << std::endl;
110 ASSERT_TRUE(result.retCode == UcError::SUCCESS || result.retCode == UcError::UNSUPPORT);
111 }
112