• 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 <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 
24 using namespace testing::ext;
25 using namespace OHOS;
26 using namespace OHOS::AAFwk;
27 using namespace OHOS::AccountSA;
28 using namespace OHOS::AccountSA::Constants;
29 
30 namespace {
31 const std::string STRING_LOCAL_ACCOUNT_ID_INVALID = "local_account_id_invalid";
32 const std::string STRING_LOCAL_ACCOUNT_ID_INVALID_TWO = "1024";
33 }  // namespace
34 
35 class AccountCommandDeleteModuleTest : public testing::Test {
36 public:
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39     void SetUp() override;
40     void TearDown() override;
41 
42     std::string cmd_ = "delete";
43 };
44 
SetUpTestCase()45 void AccountCommandDeleteModuleTest::SetUpTestCase()
46 {
47     ASSERT_NE(GetAllAccountPermission(), 0);
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 AccountCommandDeleteModuleTest::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 AccountCommandDeleteModuleTest::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 AccountCommandDeleteModuleTest::TearDown()
75 {}
76 
ExecuteCommand(const std::string & command)77 static std::string ExecuteCommand(const std::string& command)
78 {
79     std::string result = "";
80     FILE* file = popen(command.c_str(), "r");
81 
82     if (file != nullptr) {
83         char commandResult[1024] = { 0 };
84         while ((fgets(commandResult, sizeof(commandResult), file)) != nullptr) {
85             result.append(commandResult);
86         }
87         pclose(file);
88         file = nullptr;
89     }
90 
91     return result;
92 }
93 
94 /**
95  * @tc.name: Acm_Command_Delete_0100
96  * @tc.desc: Verify the "acm delete -i <local-account-id>" command.
97  * @tc.type: FUNC
98  * @tc.require: SR000GGVFO
99  */
100 HWTEST_F(AccountCommandDeleteModuleTest, Acm_Command_Delete_0100, TestSize.Level1)
101 {
102     std::string command = TOOL_NAME + " " + cmd_ + " -i " + STRING_LOCAL_ACCOUNT_ID_INVALID;
103     GTEST_LOG_(INFO) << "command = " << command;
104 
105     std::string commandResult = ExecuteCommand(command);
106     EXPECT_EQ(commandResult, HELP_MSG_INVALID_ID_ARGUMENT + "\n" + HELP_MSG_DELETE);
107 }
108 
109 /**
110  * @tc.name: Acm_Command_Delete_0200
111  * @tc.desc: Verify the "acm delete -i <local-account-id>" command.
112  * @tc.type: FUNC
113  * @tc.require: SR000GGVFO
114  */
115 HWTEST_F(AccountCommandDeleteModuleTest, Acm_Command_Delete_0200, TestSize.Level1)
116 {
117     std::string command = TOOL_NAME + " " + cmd_ + " -i " + STRING_LOCAL_ACCOUNT_ID_INVALID_TWO;
118     GTEST_LOG_(INFO) << "command = " << command;
119 
120     std::string commandResult = ExecuteCommand(command);
121     EXPECT_EQ(commandResult, STRING_DELETE_OS_ACCOUNT_NG + "\n");
122 }
123 
124 /**
125  * @tc.name: Acm_Command_Delete_0300
126  * @tc.desc: Verify the "acm delete -i <local-account-id>" command.
127  * @tc.type: FUNC
128  * @tc.require: SR000GGVFO
129  */
130 HWTEST_F(AccountCommandDeleteModuleTest, Acm_Command_Delete_0300, TestSize.Level1)
131 {
132     std::string commandResult = AccountCommandUtil::CreateOsAccount("Acm_Command_Delete_0300");
133     ASSERT_NE(commandResult.find(STRING_CREATE_OS_ACCOUNT_OK), std::string::npos);
134 
135     commandResult = AccountCommandUtil::DeleteLastOsAccount();
136     ASSERT_NE(commandResult.find(STRING_DELETE_OS_ACCOUNT_OK), std::string::npos);
137 }
138