1 /*
2  * Copyright (c) 2021-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 <gtest/gtest.h>
17 
18 #define private public
19 #include "account_command.h"
20 #undef private
21 #include "account_log_wrapper.h"
22 #include "singleton.h"
23 
24 using namespace testing::ext;
25 using namespace OHOS;
26 using namespace OHOS::AAFwk;
27 using namespace OHOS::AccountSA;
28 
29 namespace {
30 const std::string HELP_MSG_UNKNOWN_OPTION = "fail: unknown option.";
31 }  // namespace
32 class AccountCommandTest : public testing::Test {
33 public:
34     static void SetUpTestCase();
35     static void TearDownTestCase();
36     void SetUp() override;
37     void TearDown() override;
38     std::string cmd_ = "stop";
39 };
40 
SetUpTestCase()41 void AccountCommandTest::SetUpTestCase()
42 {}
43 
TearDownTestCase()44 void AccountCommandTest::TearDownTestCase()
45 {}
46 
SetUp(void)47 void AccountCommandTest::SetUp(void) __attribute__((no_sanitize("cfi")))
48 {
49     testing::UnitTest *test = testing::UnitTest::GetInstance();
50     ASSERT_NE(test, nullptr);
51     const testing::TestInfo *testinfo = test->current_test_info();
52     ASSERT_NE(testinfo, nullptr);
53     string testCaseName = string(testinfo->name());
54     ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
55 
56     // reset optind to 0
57     optind = 0;
58 }
59 
TearDown()60 void AccountCommandTest::TearDown()
61 {}
62 
63 /**
64  * @tc.name: Acm_Command_0100
65  * @tc.desc: Verify the "acm" command.
66  * @tc.type: FUNC
67  * @tc.require: SR000GGVFO
68  */
69 HWTEST_F(AccountCommandTest, Acm_Command_0100, TestSize.Level1)
70 {
71     char *argv[] = {
72         const_cast<char *>(TOOL_NAME.c_str()),
73         const_cast<char *>(""),
74     };
75     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
76 
77     AccountCommand cmd(argc, argv);
78     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG);
79 }
80 
81 /**
82  * @tc.name: Acm_Command_0200
83  * @tc.desc: Verify the "acm xxx" command.
84  * @tc.type: FUNC
85  * @tc.require: SR000GGVFO
86  */
87 HWTEST_F(AccountCommandTest, Acm_Command_0200, TestSize.Level1)
88 {
89     char *argv[] = {
90         const_cast<char *>(TOOL_NAME.c_str()),
91         const_cast<char *>("xxx"),
92         const_cast<char *>(""),
93     };
94     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
95 
96     AccountCommand cmd(argc, argv);
97     EXPECT_EQ(cmd.ExecCommand(), cmd.GetCommandErrorMsg() + HELP_MSG);
98 }
99 
100 /**
101  * @tc.name: Acm_Command_0300
102  * @tc.desc: Verify the "acm -xxx" command.
103  * @tc.type: FUNC
104  * @tc.require: SR000GGVFO
105  */
106 HWTEST_F(AccountCommandTest, Acm_Command_0300, TestSize.Level1)
107 {
108     char *argv[] = {
109         const_cast<char *>(TOOL_NAME.c_str()),
110         const_cast<char *>("-xxx"),
111         const_cast<char *>(""),
112     };
113     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
114 
115     AccountCommand cmd(argc, argv);
116     EXPECT_EQ(cmd.ExecCommand(), cmd.GetCommandErrorMsg() + HELP_MSG);
117 }
118 
119 /**
120  * @tc.name: Acm_Command_0400
121  * @tc.desc: Verify the "acm --xxx" command.
122  * @tc.type: FUNC
123  * @tc.require: SR000GGVFO
124  */
125 HWTEST_F(AccountCommandTest, Acm_Command_0400, TestSize.Level1)
126 {
127     char *argv[] = {
128         const_cast<char *>(TOOL_NAME.c_str()),
129         const_cast<char *>("--xxx"),
130         const_cast<char *>(""),
131     };
132     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
133 
134     AccountCommand cmd(argc, argv);
135     EXPECT_EQ(cmd.ExecCommand(), cmd.GetCommandErrorMsg() + HELP_MSG);
136 }
137 
138 /**
139  * @tc.name: Acm_Command_0500
140  * @tc.desc: Verify the "acm help" command.
141  * @tc.type: FUNC
142  * @tc.require: SR000GGVFO
143  */
144 HWTEST_F(AccountCommandTest, Acm_Command_0500, TestSize.Level1)
145 {
146     char *argv[] = {
147         const_cast<char *>(TOOL_NAME.c_str()),
148         const_cast<char *>("help"),
149         const_cast<char *>(""),
150     };
151     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
152 
153     AccountCommand cmd(argc, argv);
154     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG);
155 }