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