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 #include "os_account_manager.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 STRING_CONSTRAINT_INVALID = "constraint.invalid";
32 const std::string STRING_CONSTRAINT = "constraint.bluetooth";
33
34 constexpr std::size_t SIZE_ONE = 1;
35
36 #ifndef ACCOUNT_TEST
37 const std::string USER_INFO_BASE = "/data/service/el1/public/account";
38 #else
39 const std::string USER_INFO_BASE = "/data/service/el1/public/account/test";
40 #endif // ACCOUNT_TEST
41 } // namespace
42
43 class AccountCommandSetModuleTest : public testing::Test {
44 public:
45 static void SetUpTestCase();
46 static void TearDownTestCase();
47 void SetUp() override;
48 void TearDown() override;
49
50 std::string cmd_ = "set";
51 };
52
SetUpTestCase()53 void AccountCommandSetModuleTest::SetUpTestCase()
54 {
55 ASSERT_NE(GetAllAccountPermission(), 0);
56 #ifdef ACCOUNT_TEST
57 AccountFileOperator osAccountFileOperator;
58 osAccountFileOperator.DeleteDirOrFile(USER_INFO_BASE);
59 GTEST_LOG_(INFO) << "delete account test path " << USER_INFO_BASE;
60 #endif // ACCOUNT_TEST
61 }
62
TearDownTestCase()63 void AccountCommandSetModuleTest::TearDownTestCase()
64 {
65 #ifdef ACCOUNT_TEST
66 AccountFileOperator osAccountFileOperator;
67 osAccountFileOperator.DeleteDirOrFile(USER_INFO_BASE);
68 GTEST_LOG_(INFO) << "delete account test path " << USER_INFO_BASE;
69 #endif // ACCOUNT_TEST
70 }
71
SetUp(void)72 void AccountCommandSetModuleTest::SetUp(void) __attribute__((no_sanitize("cfi")))
73 {
74 testing::UnitTest *test = testing::UnitTest::GetInstance();
75 ASSERT_NE(test, nullptr);
76 const testing::TestInfo *testinfo = test->current_test_info();
77 ASSERT_NE(testinfo, nullptr);
78 string testCaseName = string(testinfo->name());
79 ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
80 }
81
TearDown()82 void AccountCommandSetModuleTest::TearDown()
83 {}
84
ExecuteCommand(const std::string & command)85 static std::string ExecuteCommand(const std::string& command)
86 {
87 std::string result = "";
88 FILE* file = popen(command.c_str(), "r");
89
90 if (file != nullptr) {
91 char commandResult[1024] = { 0 };
92 while ((fgets(commandResult, sizeof(commandResult), file)) != nullptr) {
93 result.append(commandResult);
94 }
95 pclose(file);
96 file = nullptr;
97 }
98
99 return result;
100 }
101
102 /**
103 * @tc.name: Acm_Command_Set_0100
104 * @tc.desc: Verify the "acm set -i <local-account-id> -c <constraints>" command.
105 * @tc.type: FUNC
106 * @tc.require: SR000GGVFO
107 */
108 HWTEST_F(AccountCommandSetModuleTest, Acm_Command_Set_0100, TestSize.Level1)
109 {
110 AccountCommandUtil::CreateOsAccount("Acm_Command_Set_0100");
111
112 std::vector<OsAccountInfo> osAccounts;
113 ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts);
114 ASSERT_EQ(result, ERR_OK);
115
116 ASSERT_GT(osAccounts.size(), SIZE_ONE);
117 std::string localAccountId = std::to_string(osAccounts.rbegin()->GetLocalId());
118
119 std::string command = TOOL_NAME + " set -i " + localAccountId + " -c " + STRING_CONSTRAINT_INVALID;
120 GTEST_LOG_(INFO) << "command = " << command;
121
122 std::string commandResult = ExecuteCommand(command);
123 ASSERT_EQ(commandResult, STRING_SET_OS_ACCOUNT_CONSTRAINTS_NG + "\n");
124
125 commandResult = AccountCommandUtil::DeleteLastOsAccount();
126 ASSERT_NE(commandResult.find(STRING_DELETE_OS_ACCOUNT_OK), std::string::npos);
127 }
128
129 /**
130 * @tc.name: Acm_Command_Set_0200
131 * @tc.desc: Verify the "acm set -i <local-account-id> -c <constraints>" command.
132 * @tc.type: FUNC
133 * @tc.require: SR000GGVFO
134 */
135 HWTEST_F(AccountCommandSetModuleTest, Acm_Command_Set_0200, TestSize.Level1)
136 {
137 AccountCommandUtil::CreateOsAccount("Acm_Command_Set_0200");
138
139 std::vector<OsAccountInfo> osAccounts;
140 ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts);
141 ASSERT_EQ(result, ERR_OK);
142
143 ASSERT_GT(osAccounts.size(), SIZE_ONE);
144 std::string localAccountId = std::to_string(osAccounts.rbegin()->GetLocalId());
145
146 std::string command = TOOL_NAME + " set -i " + localAccountId + " -c " + STRING_CONSTRAINT + " -e";
147 GTEST_LOG_(INFO) << "command = " << command;
148
149 std::string commandResult = ExecuteCommand(command);
150 ASSERT_EQ(commandResult, STRING_SET_OS_ACCOUNT_CONSTRAINTS_OK + "\n");
151
152 command = TOOL_NAME + " set -i " + localAccountId + " -c " + STRING_CONSTRAINT + " -e";
153 GTEST_LOG_(INFO) << "command = " << command;
154
155 commandResult = ExecuteCommand(command);
156 ASSERT_EQ(commandResult, STRING_SET_OS_ACCOUNT_CONSTRAINTS_OK + "\n");
157
158 commandResult = AccountCommandUtil::DeleteLastOsAccount();
159 ASSERT_NE(commandResult.find(STRING_DELETE_OS_ACCOUNT_OK), std::string::npos);
160 }
161