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