1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 <cinttypes>
17 #include <gtest/gtest.h>
18 #include <vector>
19 #include <memory>
20 #include <fcntl.h>
21 #include <string>
22 #include <regex>
23 #include <unistd.h>
24
25 #include "arkts_plugin.h"
26 #include "common.h"
27 #include "plugin_module_api.h"
28
29 using namespace testing::ext;
30
31 namespace {
32 constexpr int SLEEP_TIME = 5;
33 class ArkTSPluginTest : public ::testing::Test {
34 public:
ArkTSPluginTest()35 ArkTSPluginTest()
36 {
37 sleep(SLEEP_TIME); // Wait for the application to start successfully.
38 const std::string processName = "cn.openharmony.rebound_project";
39 COMMON::IsProcessExist(processName, pid_);
40 HILOG_INFO(LOG_CORE, "ArkTSPluginTest pid: %d", pid_);
41 }
~ArkTSPluginTest()42 ~ArkTSPluginTest() {}
SetUpTestCase()43 static void SetUpTestCase() {}
TearDownTestCase()44 static void TearDownTestCase() {}
45 int32_t pid_{0};
46 };
47
SetArkTSConfig(ArkTSConfig & protoConfig,int32_t pid,ArkTSConfig::HeapType type,uint32_t interval,bool capture_numeric_value,bool track_allocations,bool enable_cpu_profiler,uint32_t cpu_profiler_interval=1000)48 std::vector<uint8_t> SetArkTSConfig(
49 ArkTSConfig &protoConfig, int32_t pid, ArkTSConfig::HeapType type,
50 uint32_t interval, bool capture_numeric_value, bool track_allocations,
51 bool enable_cpu_profiler, uint32_t cpu_profiler_interval = 1000)
52 {
53 protoConfig.set_pid(pid);
54 protoConfig.set_type(type);
55 protoConfig.set_interval(interval);
56 protoConfig.set_capture_numeric_value(capture_numeric_value);
57 protoConfig.set_track_allocations(track_allocations);
58 protoConfig.set_enable_cpu_profiler(enable_cpu_profiler);
59 protoConfig.set_cpu_profiler_interval(cpu_profiler_interval);
60
61 std::vector<uint8_t> configData(protoConfig.ByteSizeLong());
62 protoConfig.SerializeToArray(configData.data(), configData.size());
63 return configData;
64 }
65
WriteFunc(WriterStruct * writer,const void * data,size_t size)66 long WriteFunc(WriterStruct* writer, const void* data, size_t size)
67 {
68 if (writer == nullptr || data == nullptr || size == 0) {
69 return -1;
70 }
71 return 0;
72 }
73
FlushFunc(WriterStruct * writer)74 bool FlushFunc(WriterStruct* writer)
75 {
76 if (writer == nullptr) {
77 return false;
78 }
79 return true;
80 }
81
82 /**
83 * @tc.name: arkts plugin
84 * @tc.desc: arkts plugin test boundary values.
85 * @tc.type: FUNC
86 */
87 HWTEST_F(ArkTSPluginTest, TestStartFunction, TestSize.Level1)
88 {
89 ArkTSPlugin arkTSPlugin;
90 ArkTSConfig protoConfig;
91
92 std::vector<uint8_t> configData = SetArkTSConfig(protoConfig, -1, ArkTSConfig::INVALID, 0, false, false, false);
93 arkTSPlugin.Start(configData.data(), configData.size());
94
95 configData.clear();
96 configData = SetArkTSConfig(protoConfig, 1, ArkTSConfig::INVALID, 0, false, false, false);
97 arkTSPlugin.Start(configData.data(), configData.size());
98
99 configData.clear();
100 configData = SetArkTSConfig(protoConfig, pid_, ArkTSConfig::INVALID, 0, false, false, false);
101 arkTSPlugin.Start(configData.data(), configData.size());
102 }
103
104 /**
105 * @tc.name: arkts plugin
106 * @tc.desc: arkts plugin test memory timeline.
107 * @tc.type: FUNC
108 */
109 HWTEST_F(ArkTSPluginTest, TestTimeline, TestSize.Level1)
110 {
111 ArkTSPlugin arkTSPlugin;
112 ArkTSConfig protoConfig;
113 WriterStruct writer = {WriteFunc, FlushFunc};
114
115 std::vector<uint8_t> configData = SetArkTSConfig(protoConfig, pid_, ArkTSConfig::TIMELINE, 0, false, true, false);
116 arkTSPlugin.SetWriter(&writer);
117 }
118
119 /**
120 * @tc.name: arkts plugin
121 * @tc.desc: arkts plugin test cpu profiler.
122 * @tc.type: FUNC
123 */
124 HWTEST_F(ArkTSPluginTest, TestCpuProfiler, TestSize.Level1)
125 {
126 ArkTSPlugin arkTSPlugin;
127 ArkTSConfig protoConfig;
128 WriterStruct writer = {WriteFunc, FlushFunc};
129 std::vector<uint8_t> configData = SetArkTSConfig(protoConfig, pid_, ArkTSConfig::INVALID, 0, false, false, true);
130 arkTSPlugin.SetWriter(&writer);
131 }
132 } // namespace
133