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 #include "tool_system_test.h"
25
26 using namespace testing::ext;
27 using namespace OHOS;
28 using namespace OHOS::AAFwk;
29 using namespace OHOS::AccountSA;
30 using namespace OHOS::AccountSA::Constants;
31
32 namespace {
33 const std::string STRING_CONSTRAINT_INVALID = "constraint.invalid";
34 const std::string STRING_CONSTRAINT = "constraint.bluetooth";
35
36 constexpr std::size_t SIZE_ONE = 1;
37 } // namespace
38
39 class AccountCommandSetModuleTest : 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_ = "set";
47 };
48
SetUpTestCase()49 void AccountCommandSetModuleTest::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 AccountCommandSetModuleTest::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 AccountCommandSetModuleTest::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 AccountCommandSetModuleTest::TearDown()
79 {}
80
81 /**
82 * @tc.name: Acm_Command_Set_0100
83 * @tc.desc: Verify the "acm set -i <local-account-id> -c <constraints>" command.
84 * @tc.type: FUNC
85 * @tc.require: SR000GGVFO
86 */
87 HWTEST_F(AccountCommandSetModuleTest, Acm_Command_Set_0100, TestSize.Level1)
88 {
89 AccountCommandUtil::CreateOsAccount("Acm_Command_Set_0100");
90
91 std::vector<OsAccountInfo> osAccounts;
92 ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts);
93 ASSERT_EQ(result, ERR_OK);
94
95 ASSERT_GT(osAccounts.size(), SIZE_ONE);
96 std::string localAccountId = std::to_string(osAccounts.rbegin()->GetLocalId());
97
98 std::string command = TOOL_NAME + " set -i " + localAccountId + " -c " + STRING_CONSTRAINT_INVALID;
99 GTEST_LOG_(INFO) << "command = " << command;
100
101 std::string commandResult = ToolSystemTest::ExecuteCommand(command);
102 ASSERT_EQ(commandResult, STRING_SET_OS_ACCOUNT_CONSTRAINTS_NG + "\n");
103
104 commandResult = AccountCommandUtil::DeleteLastOsAccount();
105 ASSERT_NE(commandResult.find(STRING_DELETE_OS_ACCOUNT_OK), std::string::npos);
106 }
107
108 /**
109 * @tc.name: Acm_Command_Set_0200
110 * @tc.desc: Verify the "acm set -i <local-account-id> -c <constraints>" command.
111 * @tc.type: FUNC
112 * @tc.require: SR000GGVFO
113 */
114 HWTEST_F(AccountCommandSetModuleTest, Acm_Command_Set_0200, TestSize.Level1)
115 {
116 AccountCommandUtil::CreateOsAccount("Acm_Command_Set_0200");
117
118 std::vector<OsAccountInfo> osAccounts;
119 ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts);
120 ASSERT_EQ(result, ERR_OK);
121
122 ASSERT_GT(osAccounts.size(), SIZE_ONE);
123 std::string localAccountId = std::to_string(osAccounts.rbegin()->GetLocalId());
124
125 std::string command = TOOL_NAME + " set -i " + localAccountId + " -c " + STRING_CONSTRAINT + " -e";
126 GTEST_LOG_(INFO) << "command = " << command;
127
128 std::string commandResult = ToolSystemTest::ExecuteCommand(command);
129 ASSERT_EQ(commandResult, STRING_SET_OS_ACCOUNT_CONSTRAINTS_OK + "\n");
130
131 command = TOOL_NAME + " set -i " + localAccountId + " -c " + STRING_CONSTRAINT + " -e";
132 GTEST_LOG_(INFO) << "command = " << command;
133
134 commandResult = ToolSystemTest::ExecuteCommand(command);
135 ASSERT_EQ(commandResult, STRING_SET_OS_ACCOUNT_CONSTRAINTS_OK + "\n");
136
137 commandResult = AccountCommandUtil::DeleteLastOsAccount();
138 ASSERT_NE(commandResult.find(STRING_DELETE_OS_ACCOUNT_OK), std::string::npos);
139 }
140