1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2022. 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 <iostream>
17 #include <vector>
18 #include <gtest/gtest.h>
19 #include "google/protobuf/text_format.h"
20 #include "parse_plugin_config.h"
21 #include "logging.h"
22 using namespace testing::ext;
23
24 namespace {
25 class ParsePluginConfigTest : public ::testing::Test {
26 public:
SetUpTestCase()27 static void SetUpTestCase() {}
TearDownTestCase()28 static void TearDownTestCase() {}
29
CreateCommand(string ConfigName, ParsePluginConfig &parseConfig, string &config) const30 void CreateCommand(string ConfigName, ParsePluginConfig &parseConfig, string &config) const
31 {
32 std::string cmdStr = " request_id: 1"
33 " session_config {"
34 " buffers {"
35 " pages: 16384"
36 " }"
37 " result_file: \"/data/local/tmp/hiprofiler_data.htrace\""
38 " sample_duration: 50000"
39 " }"
40 " plugin_configs {"
41 " plugin_name: \"" + ConfigName + "\""
42 " sample_interval: 1000"
43 " config_data {"
44 " }"
45 " }";
46 config = parseConfig.GetPluginsConfig(cmdStr);
47 }
48
GetProfilerPluginConfig(string config)49 ProfilerPluginConfig GetProfilerPluginConfig(string config)
50 {
51 ProfilerPluginConfig profilerPluginConfig;
52 auto request = std::make_unique<CreateSessionRequest>();
53 if (!google::protobuf::TextFormat::ParseFromString(config, request.get())) {
54 printf("%s\n", config.c_str());
55 return profilerPluginConfig;
56 }
57 profilerPluginConfig = *(request->mutable_plugin_configs(0));
58 return profilerPluginConfig;
59 }
60 };
61
62 /**
63 * @tc.name: ParsePluginConfig
64 * @tc.desc: Test parse plugin config.
65 * @tc.type: FUNC
66 */
HWTEST_F(ParsePluginConfigTest, TestParsePluginConfig, TestSize.Level1)67 HWTEST_F(ParsePluginConfigTest, TestParsePluginConfig, TestSize.Level1)
68 {
69 ParsePluginConfig parseConfig;
70 std::string config;
71 vector<std::string> pluginNames{
72 "cpu-plugin",
73 "diskio-plugin",
74 "ftrace-plugin",
75 "hidump-plugin",
76 "hilog-plugin",
77 "memory-plugin",
78 "nativehook",
79 "network-plugin",
80 "process-plugin",
81 "hiperf-plugin",
82 "hisysevent-plugin",
83 "hiebpf-plugin",
84 "invalid-plugin",
85 };
86 for (const std::string &pluginName : pluginNames) {
87 CreateCommand(pluginName, parseConfig, config);
88 auto profilerConfig = GetProfilerPluginConfig(config);
89 bool res = parseConfig.SetSerializePluginsConfig(pluginName, profilerConfig);
90 if (pluginName == "invalid-plugin") {
91 EXPECT_FALSE(res);
92 } else {
93 EXPECT_TRUE(res);
94 }
95 }
96 ProfilerPluginConfig profilerConfig;
97 bool res = parseConfig.SetSerializePluginsConfig("testplugin", profilerConfig);
98 EXPECT_TRUE(!res);
99 }
100 }
101