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 <filesystem>
17 #include <gtest/gtest.h>
18 
19 #include "account_command.h"
20 #include "account_command_util.h"
21 #include "account_file_operator.h"
22 #include "account_log_wrapper.h"
23 #include "tool_system_test.h"
24 
25 using namespace testing::ext;
26 using namespace OHOS;
27 using namespace OHOS::AAFwk;
28 using namespace OHOS::AccountSA;
29 using namespace OHOS::AccountSA::Constants;
30 
31 namespace {
32 const std::string STRING_LOCAL_ACCOUNT_ID_INVALID = "local_account_id_invalid";
33 const std::string STRING_LOCAL_ACCOUNT_ID_INVALID_TWO = "1024";
34 }  // namespace
35 
36 class AccountCommandDumpModuleTest : public testing::Test {
37 public:
38     static void SetUpTestCase();
39     static void TearDownTestCase();
40     void SetUp() override;
41     void TearDown() override;
42 
43     std::string cmd_ = "dump";
44 };
45 
SetUpTestCase()46 void AccountCommandDumpModuleTest::SetUpTestCase()
47 {
48 #ifdef ACCOUNT_TEST
49     AccountFileOperator osAccountFileOperator;
50     osAccountFileOperator.DeleteDirOrFile(USER_INFO_BASE);
51     GTEST_LOG_(INFO) << "delete account test path " << USER_INFO_BASE;
52 #endif  // ACCOUNT_TEST
53 }
54 
TearDownTestCase()55 void AccountCommandDumpModuleTest::TearDownTestCase()
56 {
57 #ifdef ACCOUNT_TEST
58     AccountFileOperator osAccountFileOperator;
59     osAccountFileOperator.DeleteDirOrFile(USER_INFO_BASE);
60     GTEST_LOG_(INFO) << "delete account test path " << USER_INFO_BASE;
61 #endif  // ACCOUNT_TEST
62 }
63 
SetUp(void)64 void AccountCommandDumpModuleTest::SetUp(void) __attribute__((no_sanitize("cfi")))
65 {
66     testing::UnitTest *test = testing::UnitTest::GetInstance();
67     ASSERT_NE(test, nullptr);
68     const testing::TestInfo *testinfo = test->current_test_info();
69     ASSERT_NE(testinfo, nullptr);
70     string testCaseName = string(testinfo->name());
71     ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
72 }
73 
TearDown()74 void AccountCommandDumpModuleTest::TearDown()
75 {}
76 
77 /**
78  * @tc.name: Acm_Command_Dump_0100
79  * @tc.desc: Verify the "acm dump -i <local-account-id>" command.
80  * @tc.type: FUNC
81  * @tc.require: SR000GGVFO
82  */
83 HWTEST_F(AccountCommandDumpModuleTest, Acm_Command_Dump_0100, TestSize.Level1)
84 {
85     std::string command = TOOL_NAME + " " + cmd_ + " -i " + STRING_LOCAL_ACCOUNT_ID_INVALID;
86     GTEST_LOG_(INFO) << "command = " << command;
87 
88     std::string commandResult = ToolSystemTest::ExecuteCommand(command);
89     EXPECT_EQ(commandResult, HELP_MSG_INVALID_ID_ARGUMENT + "\n" + HELP_MSG_DUMP);
90 }
91 
92 /**
93  * @tc.name: Acm_Command_Dump_0200
94  * @tc.desc: Verify the "acm dump -i <local-account-id>" command.
95  * @tc.type: FUNC
96  * @tc.require: SR000GGVFO
97  */
98 HWTEST_F(AccountCommandDumpModuleTest, Acm_Command_Dump_0200, TestSize.Level1)
99 {
100     std::string command = TOOL_NAME + " " + cmd_ + " -i " + STRING_LOCAL_ACCOUNT_ID_INVALID_TWO;
101     GTEST_LOG_(INFO) << "command = " << command;
102 
103     std::string commandResult = ToolSystemTest::ExecuteCommand(command);
104     EXPECT_EQ(commandResult, STRING_DUMP_OS_ACCOUNT_NG + "\n");
105 }
106 
107 /**
108  * @tc.name: Acm_Command_Dump_0300
109  * @tc.desc: Verify the "acm dump -i <local-account-id>" command.
110  * @tc.type: FUNC
111  * @tc.require: SR000GGVFO
112  */
113 HWTEST_F(AccountCommandDumpModuleTest, Acm_Command_Dump_0300, TestSize.Level1)
114 {
115     std::string commandResult = AccountCommandUtil::CreateOsAccount("Acm_Command_Dump_0300");
116     ASSERT_NE(commandResult.find(STRING_CREATE_OS_ACCOUNT_OK), std::string::npos);
117 
118     commandResult = AccountCommandUtil::DumpLastOsAccount();
119     ASSERT_NE(commandResult, STRING_DUMP_OS_ACCOUNT_NG);
120 
121     commandResult = AccountCommandUtil::DeleteLastOsAccount();
122     ASSERT_NE(commandResult.find(STRING_DELETE_OS_ACCOUNT_OK), std::string::npos);
123 }
124