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 #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
80 /**
81 * @tc.name: Acm_Command_Set_0100
82 * @tc.desc: Verify the "acm set -i <local-account-id> -c <constraints>" command.
83 * @tc.type: FUNC
84 * @tc.require: SR000GGVFO
85 */
86 HWTEST_F(AccountCommandSetModuleTest, Acm_Command_Set_0100, TestSize.Level1)
87 {
88 AccountCommandUtil::CreateOsAccount("Acm_Command_Set_0100");
89
90 std::vector<OsAccountInfo> osAccounts;
91 ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts);
92 ASSERT_EQ(result, ERR_OK);
93
94 ASSERT_GT(osAccounts.size(), SIZE_ONE);
95 std::string localAccountId = std::to_string(osAccounts.rbegin()->GetLocalId());
96
97 std::string command = TOOL_NAME + " set -i " + localAccountId + " -c " + STRING_CONSTRAINT_INVALID;
98 GTEST_LOG_(INFO) << "command = " << command;
99
100 std::string commandResult = ToolSystemTest::ExecuteCommand(command);
101 ASSERT_EQ(commandResult, STRING_SET_OS_ACCOUNT_CONSTRAINTS_NG + "\n");
102
103 commandResult = AccountCommandUtil::DeleteLastOsAccount();
104 ASSERT_NE(commandResult.find(STRING_DELETE_OS_ACCOUNT_OK), std::string::npos);
105 }
106
107 /**
108 * @tc.name: Acm_Command_Set_0200
109 * @tc.desc: Verify the "acm set -i <local-account-id> -c <constraints>" command.
110 * @tc.type: FUNC
111 * @tc.require: SR000GGVFO
112 */
113 HWTEST_F(AccountCommandSetModuleTest, Acm_Command_Set_0200, TestSize.Level1)
114 {
115 AccountCommandUtil::CreateOsAccount("Acm_Command_Set_0200");
116
117 std::vector<OsAccountInfo> osAccounts;
118 ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts);
119 ASSERT_EQ(result, ERR_OK);
120
121 ASSERT_GT(osAccounts.size(), SIZE_ONE);
122 std::string localAccountId = std::to_string(osAccounts.rbegin()->GetLocalId());
123
124 std::string command = TOOL_NAME + " set -i " + localAccountId + " -c " + STRING_CONSTRAINT + " -e";
125 GTEST_LOG_(INFO) << "command = " << command;
126
127 std::string commandResult = ToolSystemTest::ExecuteCommand(command);
128 ASSERT_EQ(commandResult, STRING_SET_OS_ACCOUNT_CONSTRAINTS_OK + "\n");
129
130 command = TOOL_NAME + " set -i " + localAccountId + " -c " + STRING_CONSTRAINT + " -e";
131 GTEST_LOG_(INFO) << "command = " << command;
132
133 commandResult = ToolSystemTest::ExecuteCommand(command);
134 ASSERT_EQ(commandResult, STRING_SET_OS_ACCOUNT_CONSTRAINTS_OK + "\n");
135
136 commandResult = AccountCommandUtil::DeleteLastOsAccount();
137 ASSERT_NE(commandResult.find(STRING_DELETE_OS_ACCOUNT_OK), std::string::npos);
138 }
139