1 /*
2 * Copyright (c) 2021 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 <gtest/gtest.h>
17
18 #define private public
19 #include "common_event_command.h"
20 #undef private
21 #include "common_event_manager.h"
22 #include "common_event_subscriber.h"
23
24 using namespace testing::ext;
25 using namespace OHOS;
26 using namespace OHOS::AAFwk;
27 using namespace OHOS::EventFwk;
28
Concatenate(const std::string & first,const std::string & second)29 static std::string Concatenate(const std::string &first, const std::string &second)
30 {
31 return first + second;
32 }
33
34 class CemCommandTest : public testing::Test {
35 public:
36 static void SetUpTestCase();
37 static void TearDownTestCase();
38 void SetUp() override;
39 void TearDown() override;
40
41 std::string toolName_ = TOOL_NAME;
42 };
43
SetUpTestCase()44 void CemCommandTest::SetUpTestCase()
45 {}
46
TearDownTestCase()47 void CemCommandTest::TearDownTestCase()
48 {}
49
SetUp()50 void CemCommandTest::SetUp()
51 {
52 // reset optind to 0
53 optind = 0;
54 }
55
TearDown()56 void CemCommandTest::TearDown()
57 {}
58
59 /**
60 * @tc.number: Cem_Command_0100
61 * @tc.name: ExecCommand
62 * @tc.desc: Verify the "cem" command.
63 */
64 HWTEST_F(CemCommandTest, Cem_Command_0100, Function | MediumTest | Level1)
65 {
66 char *argv[] = {
67 (char *)toolName_.c_str(),
68 (char *)"",
69 };
70 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
71
72 CommonEventCommand cmd(argc, argv);
73 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG);
74 }
75
76 /**
77 * @tc.number: Cem_Command_0200
78 * @tc.name: ExecCommand
79 * @tc.desc: Verify the "cem xxx" command.
80 */
81 HWTEST_F(CemCommandTest, Cem_Command_0200, Function | MediumTest | Level1)
82 {
83 char *argv[] = {
84 (char *)toolName_.c_str(),
85 (char *)"xxx",
86 (char *)"",
87 };
88 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
89
90 CommonEventCommand cmd(argc, argv);
91 EXPECT_EQ(cmd.ExecCommand(), Concatenate("cem: 'xxx' is not a valid cem command. See 'cem help'.\n", HELP_MSG));
92 }
93
94 /**
95 * @tc.number: Cem_Command_0300
96 * @tc.name: ExecCommand
97 * @tc.desc: Verify the "cem -x" command.
98 */
99 HWTEST_F(CemCommandTest, Cem_Command_0300, Function | MediumTest | Level1)
100 {
101 char *argv[] = {
102 (char *)toolName_.c_str(),
103 (char *)"-x",
104 (char *)"",
105 };
106 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
107
108 CommonEventCommand cmd(argc, argv);
109 EXPECT_EQ(cmd.ExecCommand(), cmd.GetCommandErrorMsg() + HELP_MSG);
110 }
111
112 /**
113 * @tc.number: Cem_Command_0400
114 * @tc.name: ExecCommand
115 * @tc.desc: Verify the "cem -xxx" command.
116 */
117 HWTEST_F(CemCommandTest, Cem_Command_0400, Function | MediumTest | Level1)
118 {
119 char *argv[] = {
120 (char *)toolName_.c_str(),
121 (char *)"-xxx",
122 (char *)"",
123 };
124 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
125
126 CommonEventCommand cmd(argc, argv);
127 EXPECT_EQ(cmd.ExecCommand(), Concatenate(cmd.GetCommandErrorMsg(), HELP_MSG));
128 }
129
130 /**
131 * @tc.number: Cem_Command_0500
132 * @tc.name: ExecCommand
133 * @tc.desc: Verify the "cem help" command.
134 */
135 HWTEST_F(CemCommandTest, Cem_Command_0500, Function | MediumTest | Level1)
136 {
137 char *argv[] = {
138 (char *)toolName_.c_str(),
139 (char *)"help",
140 (char *)"",
141 };
142 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
143
144 CommonEventCommand cmd(argc, argv);
145 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG);
146 }