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