1 /*
2 * Copyright (c) 2021-2025 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
29 namespace {
30 const std::string STRING_LOCAL_ACCOUNT_ID_INVALID = "local_account_id_invalid";
31 const std::string STRING_LOCAL_ACCOUNT_ID_INVALID_TWO = "1024";
32 #ifndef ACCOUNT_TEST
33 const std::string USER_INFO_BASE = "/data/service/el1/public/account";
34 #else
35 const std::string USER_INFO_BASE = "/data/service/el1/public/account/test";
36 #endif // ACCOUNT_TEST
37 } // namespace
38
39 class AccountCommandDumpModuleTest : public testing::Test {
40 public:
41 static void SetUpTestCase();
42 static void TearDownTestCase();
43 void SetUp() override;
44 void TearDown() override;
45
46 std::string cmd_ = "dump";
47 };
48
SetUpTestCase()49 void AccountCommandDumpModuleTest::SetUpTestCase()
50 {
51 ASSERT_NE(GetAllAccountPermission(), 0);
52 #ifdef ACCOUNT_TEST
53 AccountFileOperator osAccountFileOperator;
54 osAccountFileOperator.DeleteDirOrFile(USER_INFO_BASE);
55 GTEST_LOG_(INFO) << "delete account test path " << USER_INFO_BASE;
56 #endif // ACCOUNT_TEST
57 }
58
TearDownTestCase()59 void AccountCommandDumpModuleTest::TearDownTestCase()
60 {
61 #ifdef ACCOUNT_TEST
62 AccountFileOperator osAccountFileOperator;
63 osAccountFileOperator.DeleteDirOrFile(USER_INFO_BASE);
64 GTEST_LOG_(INFO) << "delete account test path " << USER_INFO_BASE;
65 #endif // ACCOUNT_TEST
66 }
67
SetUp(void)68 void AccountCommandDumpModuleTest::SetUp(void) __attribute__((no_sanitize("cfi")))
69 {
70 testing::UnitTest *test = testing::UnitTest::GetInstance();
71 ASSERT_NE(test, nullptr);
72 const testing::TestInfo *testinfo = test->current_test_info();
73 ASSERT_NE(testinfo, nullptr);
74 string testCaseName = string(testinfo->name());
75 ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
76 }
77
TearDown()78 void AccountCommandDumpModuleTest::TearDown()
79 {}
80
ExecuteCommand(const std::string & command)81 static std::string ExecuteCommand(const std::string& command)
82 {
83 std::string result = "";
84 FILE* file = popen(command.c_str(), "r");
85
86 if (file != nullptr) {
87 char commandResult[1024] = { 0 };
88 while ((fgets(commandResult, sizeof(commandResult), file)) != nullptr) {
89 result.append(commandResult);
90 }
91 pclose(file);
92 file = nullptr;
93 }
94
95 return result;
96 }
97
98 /**
99 * @tc.name: Acm_Command_Dump_0100
100 * @tc.desc: Verify the "acm dump -i <local-account-id>" command.
101 * @tc.type: FUNC
102 * @tc.require: SR000GGVFO
103 */
104 HWTEST_F(AccountCommandDumpModuleTest, Acm_Command_Dump_0100, TestSize.Level1)
105 {
106 std::string command = TOOL_NAME + " " + cmd_ + " -i " + STRING_LOCAL_ACCOUNT_ID_INVALID;
107 GTEST_LOG_(INFO) << "command = " << command;
108
109 std::string commandResult = ExecuteCommand(command);
110 EXPECT_EQ(commandResult, HELP_MSG_INVALID_ID_ARGUMENT + "\n" + HELP_MSG_DUMP);
111 }
112
113 /**
114 * @tc.name: Acm_Command_Dump_0200
115 * @tc.desc: Verify the "acm dump -i <local-account-id>" command.
116 * @tc.type: FUNC
117 * @tc.require: SR000GGVFO
118 */
119 HWTEST_F(AccountCommandDumpModuleTest, Acm_Command_Dump_0200, TestSize.Level1)
120 {
121 std::string command = TOOL_NAME + " " + cmd_ + " -i " + STRING_LOCAL_ACCOUNT_ID_INVALID_TWO;
122 GTEST_LOG_(INFO) << "command = " << command;
123
124 std::string commandResult = ExecuteCommand(command);
125 EXPECT_EQ(commandResult, STRING_DUMP_OS_ACCOUNT_NG + "\n");
126 }
127
128 /**
129 * @tc.name: Acm_Command_Dump_0300
130 * @tc.desc: Verify the "acm dump -i <local-account-id>" command.
131 * @tc.type: FUNC
132 * @tc.require: SR000GGVFO
133 */
134 HWTEST_F(AccountCommandDumpModuleTest, Acm_Command_Dump_0300, TestSize.Level1)
135 {
136 std::string commandResult = AccountCommandUtil::CreateOsAccount("Acm_Command_Dump_0300");
137 ASSERT_NE(commandResult.find(STRING_CREATE_OS_ACCOUNT_OK), std::string::npos);
138
139 commandResult = AccountCommandUtil::DumpLastOsAccount();
140 ASSERT_NE(commandResult, STRING_DUMP_OS_ACCOUNT_NG);
141
142 commandResult = AccountCommandUtil::DeleteLastOsAccount();
143 ASSERT_NE(commandResult.find(STRING_DELETE_OS_ACCOUNT_OK), std::string::npos);
144 }
145