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 <cstdio>
17 
18 #include <gtest/gtest.h>
19 
20 #include "command_helper.h"
21 
22 using namespace testing::ext;
23 using namespace std;
24 namespace OHOS {
25 namespace Developtools {
26 namespace Hiebpf {
27 class CommandHelperTest : public ::testing::Test {
28 public:
SetUpTestCase()29     static void SetUpTestCase() {};
TearDownTestCase()30     static void TearDownTestCase() {};
31 
SetUp()32     void SetUp() {}
TearDown()33     void TearDown() {}
34 };
35 
36 /**
37  * @tc.name: Start
38  * @tc.desc: Test CommandHelper Start
39  * @tc.type: FUNC
40  */
HWTEST_F(CommandHelperTest, Start, TestSize.Level1)41 HWTEST_F(CommandHelperTest, Start, TestSize.Level1)
42 {
43     int argc = 1;
44     char arg1[] = "hiebpf";
45     char* argv1[] = {arg1};
46     char **argv = argv1;
47     auto commandHelper = CommandHelper::GetInstance();
48     int ret = commandHelper.Start(&argc, &argv, "help");
49     EXPECT_EQ(ret, 0);
50 
51     argc++;
52     char arg2[] = "h";
53     char* argv2[] = {arg1, arg2};
54     argv = argv2;
55     ret = commandHelper.Start(&argc, &argv, "none");
56     EXPECT_EQ(ret, 0);
57 
58     ret = commandHelper.Start(&argc, &argv, "help");
59     EXPECT_EQ(ret, -1);
60 }
61 
62 /**
63  * @tc.name: DoHelp
64  * @tc.desc: Test CommandHelper DoHelp
65  * @tc.type: FUNC
66  */
HWTEST_F(CommandHelperTest, DoHelp, TestSize.Level1)67 HWTEST_F(CommandHelperTest, DoHelp, TestSize.Level1)
68 {
69     testing::internal::CaptureStdout();
70     auto commandHelper = CommandHelper::GetInstance();
71     commandHelper.DoHelp(commandHelper.SUPPORTED_ARGS[CommandHelper::ARG_EXCLUDE_TRACER]);
72     commandHelper.DoHelp(commandHelper.SUPPORTED_ARGS[CommandHelper::ARG_MAX_STACK_DEPTH]);
73     commandHelper.DoHelp(commandHelper.SUPPORTED_ARGS[CommandHelper::ARG_DURATION]);
74     commandHelper.DoHelp(commandHelper.SUPPORTED_ARGS[CommandHelper::ARG_EVENTS]);
75     commandHelper.DoHelp(commandHelper.SUPPORTED_ARGS[CommandHelper::ARG_PIDS]);
76     commandHelper.DoHelp(commandHelper.SUPPORTED_ARGS[CommandHelper::ARG_DUMP_EVENTS]);
77     commandHelper.DoHelp(commandHelper.SUPPORTED_ARGS[CommandHelper::ARG_UNWIND_STACK]);
78     commandHelper.DoHelp(commandHelper.SUPPORTED_ARGS[CommandHelper::ARG_BPF_LOG_LEVEL]);
79     commandHelper.DoHelp(commandHelper.SUPPORTED_ARGS[CommandHelper::ARG_BPF_LOG_FILE]);
80     commandHelper.DoHelp(commandHelper.SUPPORTED_ARGS[CommandHelper::ARG_LIBBPF_LOG_LEVEL]);
81     commandHelper.DoHelp(commandHelper.SUPPORTED_ARGS[CommandHelper::ARG_LIBBPF_LOG_FILE]);
82     commandHelper.DoHelp(commandHelper.SUPPORTED_ARGS[CommandHelper::ARG_HHLOG_LEVEL]);
83     commandHelper.DoHelp(commandHelper.SUPPORTED_ARGS[CommandHelper::ARG_HHLOG_FILE]);
84     commandHelper.DoHelp(commandHelper.SUPPORTED_ARGS[CommandHelper::ARG_SERVER_START]);
85     commandHelper.DoHelp(commandHelper.SUPPORTED_ARGS[CommandHelper::ARG_SERVER_STOP]);
86     commandHelper.DoHelp(commandHelper.SUPPORTED_ARGS[CommandHelper::NR_SUPPORTED_ARGS]);
87     std::string out = testing::internal::GetCapturedStdout();
88     EXPECT_EQ(out.empty(), false);
89     EXPECT_TRUE(out.find(commandHelper.SUPPORTED_ARGS[CommandHelper::NR_SUPPORTED_ARGS]) != std::string::npos);
90 }
91 } // namespace Hiebpf
92 } // namespace Developtools
93 } // namespace OHOS
94