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 "account_command_util.h" 17 18 #include <gtest/gtest.h> 19 20 #include "account_command.h" 21 #include "os_account_manager.h" 22 #include "tool_system_test.h" 23 24 using namespace testing::ext; 25 using namespace OHOS; 26 using namespace OHOS::AAFwk; 27 28 namespace OHOS { 29 namespace AccountSA { 30 namespace { 31 const std::string STRING_LOCAL_ACCOUNT_NAME = "local_account_name"; 32 const std::string STRING_TYPE = "normal"; 33 const std::string STRING_EMPTY = ""; 34 } // namespace 35 CreateOsAccount()36std::string AccountCommandUtil::CreateOsAccount() 37 { 38 std::string command = TOOL_NAME + " create -n " + STRING_LOCAL_ACCOUNT_NAME + " -t " + STRING_TYPE; 39 GTEST_LOG_(INFO) << "command = " << command; 40 41 std::string commandResult = ToolSystemTest::ExecuteCommand(command); 42 GTEST_LOG_(INFO) << "AccountCommandUtil::CreateOsAccount commandResult = " << commandResult; 43 return commandResult; 44 } 45 CreateOsAccount(const std::string & name)46std::string AccountCommandUtil::CreateOsAccount(const std::string &name) 47 { 48 std::string command = TOOL_NAME + " create -n " + name + " -t " + STRING_TYPE; 49 GTEST_LOG_(INFO) << "command = " << command; 50 51 std::string commandResult = ToolSystemTest::ExecuteCommand(command); 52 GTEST_LOG_(INFO) << "AccountCommandUtil::CreateOsAccount commandResult = " << commandResult; 53 return commandResult; 54 } 55 DeleteLastOsAccount()56std::string AccountCommandUtil::DeleteLastOsAccount() 57 { 58 std::vector<OsAccountInfo> osAccounts; 59 ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts); 60 GTEST_LOG_(INFO) << "AccountCommandUtil::DeleteLastOsAccount result = " << result; 61 GTEST_LOG_(INFO) << "AccountCommandUtil::DeleteLastOsAccount osAccounts size = " << osAccounts.size(); 62 if (osAccounts.empty()) { 63 return STRING_EMPTY; 64 } 65 66 std::string localAccountId = std::to_string(osAccounts.rbegin()->GetLocalId()); 67 68 std::string command = TOOL_NAME + " delete -i " + localAccountId; 69 GTEST_LOG_(INFO) << "command = " << command; 70 71 std::string commandResult = ToolSystemTest::ExecuteCommand(command); 72 GTEST_LOG_(INFO) << "commandResult = " << commandResult; 73 return commandResult; 74 } 75 DumpLastOsAccount()76std::string AccountCommandUtil::DumpLastOsAccount() 77 { 78 std::vector<OsAccountInfo> osAccounts; 79 ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts); 80 GTEST_LOG_(INFO) << "AccountCommandUtil::DumpLastOsAccount result = " << result; 81 GTEST_LOG_(INFO) << "AccountCommandUtil::DumpLastOsAccount osAccounts size = " << osAccounts.size(); 82 83 if (osAccounts.empty()) { 84 return STRING_EMPTY; 85 } 86 87 std::string localAccountId = std::to_string(osAccounts.rbegin()->GetLocalId()); 88 89 std::string command = TOOL_NAME + " dump -i " + localAccountId; 90 GTEST_LOG_(INFO) << "command = " << command; 91 92 std::string commandResult = ToolSystemTest::ExecuteCommand(command); 93 GTEST_LOG_(INFO) << "AccountCommandUtil::DumpLastOsAccount commandResult " << commandResult; 94 return commandResult; 95 } 96 SwitchToFirstOsAccount()97std::string AccountCommandUtil::SwitchToFirstOsAccount() 98 { 99 std::vector<OsAccountInfo> osAccounts; 100 ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts); 101 GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToFirstOsAccount result = " << result; 102 GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToFirstOsAccount osAccounts size = " << osAccounts.size(); 103 104 if (osAccounts.empty()) { 105 return STRING_EMPTY; 106 } 107 108 std::string localAccountId = std::to_string(osAccounts.begin()->GetLocalId()); 109 110 std::string command = TOOL_NAME + " switch -i " + localAccountId; 111 GTEST_LOG_(INFO) << "command = " << command; 112 113 std::string commandResult = ToolSystemTest::ExecuteCommand(command); 114 GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToFirstOsAccount commandResult = " << commandResult; 115 return commandResult; 116 } 117 SwitchToLastOsAccount()118std::string AccountCommandUtil::SwitchToLastOsAccount() 119 { 120 std::vector<OsAccountInfo> osAccounts; 121 ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts); 122 GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToLastOsAccount result = " << result; 123 GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToLastOsAccount osAccounts size = " << osAccounts.size(); 124 125 if (osAccounts.empty()) { 126 return STRING_EMPTY; 127 } 128 129 std::string localAccountId = std::to_string(osAccounts.rbegin()->GetLocalId()); 130 131 std::string command = TOOL_NAME + " switch -i " + localAccountId; 132 GTEST_LOG_(INFO) << "command = " << command; 133 134 std::string commandResult = ToolSystemTest::ExecuteCommand(command); 135 GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToLastOsAccount commandResult = " << commandResult; 136 return commandResult; 137 } 138 } // namespace AccountSA 139 } // namespace OHOS 140