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 AccountCommandDeleteModuleTest : 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_ = "delete";
44 };
45
SetUpTestCase()46 void AccountCommandDeleteModuleTest::SetUpTestCase()
47 {
48 ASSERT_NE(GetAllAccountPermission(), 0);
49 #ifdef ACCOUNT_TEST
50 AccountFileOperator osAccountFileOperator;
51 osAccountFileOperator.DeleteDirOrFile(USER_INFO_BASE);
52 GTEST_LOG_(INFO) << "delete account test path " << USER_INFO_BASE;
53 #endif // ACCOUNT_TEST
54 }
55
TearDownTestCase()56 void AccountCommandDeleteModuleTest::TearDownTestCase()
57 {
58 #ifdef ACCOUNT_TEST
59 AccountFileOperator osAccountFileOperator;
60 osAccountFileOperator.DeleteDirOrFile(USER_INFO_BASE);
61 GTEST_LOG_(INFO) << "delete account test path " << USER_INFO_BASE;
62 #endif // ACCOUNT_TEST
63 }
64
SetUp(void)65 void AccountCommandDeleteModuleTest::SetUp(void) __attribute__((no_sanitize("cfi")))
66 {
67 testing::UnitTest *test = testing::UnitTest::GetInstance();
68 ASSERT_NE(test, nullptr);
69 const testing::TestInfo *testinfo = test->current_test_info();
70 ASSERT_NE(testinfo, nullptr);
71 string testCaseName = string(testinfo->name());
72 ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
73 }
74
TearDown()75 void AccountCommandDeleteModuleTest::TearDown()
76 {}
77
78 /**
79 * @tc.name: Acm_Command_Delete_0100
80 * @tc.desc: Verify the "acm delete -i <local-account-id>" command.
81 * @tc.type: FUNC
82 * @tc.require: SR000GGVFO
83 */
84 HWTEST_F(AccountCommandDeleteModuleTest, Acm_Command_Delete_0100, TestSize.Level1)
85 {
86 std::string command = TOOL_NAME + " " + cmd_ + " -i " + STRING_LOCAL_ACCOUNT_ID_INVALID;
87 GTEST_LOG_(INFO) << "command = " << command;
88
89 std::string commandResult = ToolSystemTest::ExecuteCommand(command);
90 EXPECT_EQ(commandResult, HELP_MSG_INVALID_ID_ARGUMENT + "\n" + HELP_MSG_DELETE);
91 }
92
93 /**
94 * @tc.name: Acm_Command_Delete_0200
95 * @tc.desc: Verify the "acm delete -i <local-account-id>" command.
96 * @tc.type: FUNC
97 * @tc.require: SR000GGVFO
98 */
99 HWTEST_F(AccountCommandDeleteModuleTest, Acm_Command_Delete_0200, TestSize.Level1)
100 {
101 std::string command = TOOL_NAME + " " + cmd_ + " -i " + STRING_LOCAL_ACCOUNT_ID_INVALID_TWO;
102 GTEST_LOG_(INFO) << "command = " << command;
103
104 std::string commandResult = ToolSystemTest::ExecuteCommand(command);
105 EXPECT_EQ(commandResult, STRING_DELETE_OS_ACCOUNT_NG + "\n");
106 }
107
108 /**
109 * @tc.name: Acm_Command_Delete_0300
110 * @tc.desc: Verify the "acm delete -i <local-account-id>" command.
111 * @tc.type: FUNC
112 * @tc.require: SR000GGVFO
113 */
114 HWTEST_F(AccountCommandDeleteModuleTest, Acm_Command_Delete_0300, TestSize.Level1)
115 {
116 std::string commandResult = AccountCommandUtil::CreateOsAccount("Acm_Command_Delete_0300");
117 ASSERT_NE(commandResult.find(STRING_CREATE_OS_ACCOUNT_OK), std::string::npos);
118
119 commandResult = AccountCommandUtil::DeleteLastOsAccount();
120 ASSERT_NE(commandResult.find(STRING_DELETE_OS_ACCOUNT_OK), std::string::npos);
121 }
122