106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci
1606f6ba60Sopenharmony_ci#include <iostream>
1706f6ba60Sopenharmony_ci#include <vector>
1806f6ba60Sopenharmony_ci#include <gtest/gtest.h>
1906f6ba60Sopenharmony_ci#include "google/protobuf/text_format.h"
2006f6ba60Sopenharmony_ci#include "parse_plugin_config.h"
2106f6ba60Sopenharmony_ci#include "logging.h"
2206f6ba60Sopenharmony_ciusing namespace testing::ext;
2306f6ba60Sopenharmony_ci
2406f6ba60Sopenharmony_cinamespace {
2506f6ba60Sopenharmony_ciclass ParsePluginConfigTest : public ::testing::Test {
2606f6ba60Sopenharmony_cipublic:
2706f6ba60Sopenharmony_ci    static void SetUpTestCase() {}
2806f6ba60Sopenharmony_ci    static void TearDownTestCase() {}
2906f6ba60Sopenharmony_ci
3006f6ba60Sopenharmony_ci    void CreateCommand(string ConfigName, ParsePluginConfig &parseConfig, string &config) const
3106f6ba60Sopenharmony_ci    {
3206f6ba60Sopenharmony_ci        std::string cmdStr = " request_id: 1"
3306f6ba60Sopenharmony_ci            " session_config {"
3406f6ba60Sopenharmony_ci            "  buffers {"
3506f6ba60Sopenharmony_ci            "   pages: 16384"
3606f6ba60Sopenharmony_ci            "  }"
3706f6ba60Sopenharmony_ci            "  result_file: \"/data/local/tmp/hiprofiler_data.htrace\""
3806f6ba60Sopenharmony_ci            "  sample_duration: 50000"
3906f6ba60Sopenharmony_ci            " }"
4006f6ba60Sopenharmony_ci            " plugin_configs {"
4106f6ba60Sopenharmony_ci            "  plugin_name: \"" + ConfigName + "\""
4206f6ba60Sopenharmony_ci            "  sample_interval: 1000"
4306f6ba60Sopenharmony_ci            "  config_data {"
4406f6ba60Sopenharmony_ci            "  }"
4506f6ba60Sopenharmony_ci            " }";
4606f6ba60Sopenharmony_ci        config = parseConfig.GetPluginsConfig(cmdStr);
4706f6ba60Sopenharmony_ci    }
4806f6ba60Sopenharmony_ci
4906f6ba60Sopenharmony_ci    ProfilerPluginConfig GetProfilerPluginConfig(string config)
5006f6ba60Sopenharmony_ci    {
5106f6ba60Sopenharmony_ci        ProfilerPluginConfig profilerPluginConfig;
5206f6ba60Sopenharmony_ci        auto request = std::make_unique<CreateSessionRequest>();
5306f6ba60Sopenharmony_ci        if (!google::protobuf::TextFormat::ParseFromString(config, request.get())) {
5406f6ba60Sopenharmony_ci            printf("%s\n", config.c_str());
5506f6ba60Sopenharmony_ci            return profilerPluginConfig;
5606f6ba60Sopenharmony_ci        }
5706f6ba60Sopenharmony_ci        profilerPluginConfig = *(request->mutable_plugin_configs(0));
5806f6ba60Sopenharmony_ci        return profilerPluginConfig;
5906f6ba60Sopenharmony_ci    }
6006f6ba60Sopenharmony_ci};
6106f6ba60Sopenharmony_ci
6206f6ba60Sopenharmony_ci/**
6306f6ba60Sopenharmony_ci * @tc.name: ParsePluginConfig
6406f6ba60Sopenharmony_ci * @tc.desc: Test parse plugin config.
6506f6ba60Sopenharmony_ci * @tc.type: FUNC
6606f6ba60Sopenharmony_ci */
6706f6ba60Sopenharmony_ciHWTEST_F(ParsePluginConfigTest, TestParsePluginConfig, TestSize.Level1)
6806f6ba60Sopenharmony_ci{
6906f6ba60Sopenharmony_ci    ParsePluginConfig parseConfig;
7006f6ba60Sopenharmony_ci    std::string config;
7106f6ba60Sopenharmony_ci    vector<std::string> pluginNames{
7206f6ba60Sopenharmony_ci        "cpu-plugin",
7306f6ba60Sopenharmony_ci        "diskio-plugin",
7406f6ba60Sopenharmony_ci        "ftrace-plugin",
7506f6ba60Sopenharmony_ci        "hidump-plugin",
7606f6ba60Sopenharmony_ci        "hilog-plugin",
7706f6ba60Sopenharmony_ci        "memory-plugin",
7806f6ba60Sopenharmony_ci        "nativehook",
7906f6ba60Sopenharmony_ci        "network-plugin",
8006f6ba60Sopenharmony_ci        "process-plugin",
8106f6ba60Sopenharmony_ci        "hiperf-plugin",
8206f6ba60Sopenharmony_ci        "hisysevent-plugin",
8306f6ba60Sopenharmony_ci        "hiebpf-plugin",
8406f6ba60Sopenharmony_ci        "invalid-plugin",
8506f6ba60Sopenharmony_ci    };
8606f6ba60Sopenharmony_ci    for (const std::string &pluginName : pluginNames) {
8706f6ba60Sopenharmony_ci        CreateCommand(pluginName, parseConfig, config);
8806f6ba60Sopenharmony_ci        auto profilerConfig = GetProfilerPluginConfig(config);
8906f6ba60Sopenharmony_ci        bool res = parseConfig.SetSerializePluginsConfig(pluginName, profilerConfig);
9006f6ba60Sopenharmony_ci        if (pluginName == "invalid-plugin") {
9106f6ba60Sopenharmony_ci            EXPECT_FALSE(res);
9206f6ba60Sopenharmony_ci        } else {
9306f6ba60Sopenharmony_ci            EXPECT_TRUE(res);
9406f6ba60Sopenharmony_ci        }
9506f6ba60Sopenharmony_ci    }
9606f6ba60Sopenharmony_ci    ProfilerPluginConfig profilerConfig;
9706f6ba60Sopenharmony_ci    bool res = parseConfig.SetSerializePluginsConfig("testplugin", profilerConfig);
9806f6ba60Sopenharmony_ci    EXPECT_TRUE(!res);
9906f6ba60Sopenharmony_ci}
10006f6ba60Sopenharmony_ci}
101