• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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()36 std::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 
DeleteLastOsAccount()46 std::string AccountCommandUtil::DeleteLastOsAccount()
47 {
48     std::vector<OsAccountInfo> osAccounts;
49     ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts);
50     GTEST_LOG_(INFO) << "AccountCommandUtil::DeleteLastOsAccount result = " << result;
51     GTEST_LOG_(INFO) << "AccountCommandUtil::DeleteLastOsAccount osAccounts size = " << osAccounts.size();
52     if (osAccounts.empty()) {
53         return STRING_EMPTY;
54     }
55 
56     std::string localAccountId = std::to_string(osAccounts.rbegin()->GetLocalId());
57 
58     std::string command = TOOL_NAME + " delete -i " + localAccountId;
59     GTEST_LOG_(INFO) << "command = " << command;
60 
61     std::string commandResult = ToolSystemTest::ExecuteCommand(command);
62     GTEST_LOG_(INFO) << "commandResult = " << commandResult;
63     return commandResult;
64 }
65 
DumpLastOsAccount()66 std::string AccountCommandUtil::DumpLastOsAccount()
67 {
68     std::vector<OsAccountInfo> osAccounts;
69     ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts);
70     GTEST_LOG_(INFO) << "AccountCommandUtil::DumpLastOsAccount result = " << result;
71     GTEST_LOG_(INFO) << "AccountCommandUtil::DumpLastOsAccount osAccounts size = " << osAccounts.size();
72 
73     if (osAccounts.empty()) {
74         return STRING_EMPTY;
75     }
76 
77     std::string localAccountId = std::to_string(osAccounts.rbegin()->GetLocalId());
78 
79     std::string command = TOOL_NAME + " dump -i " + localAccountId;
80     GTEST_LOG_(INFO) << "command = " << command;
81 
82     std::string commandResult = ToolSystemTest::ExecuteCommand(command);
83     GTEST_LOG_(INFO) << "AccountCommandUtil::DumpLastOsAccount commandResult " << commandResult;
84     return commandResult;
85 }
86 
SwitchToFirstOsAccount()87 std::string AccountCommandUtil::SwitchToFirstOsAccount()
88 {
89     std::vector<OsAccountInfo> osAccounts;
90     ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts);
91     GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToFirstOsAccount result = " << result;
92     GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToFirstOsAccount osAccounts size = " << osAccounts.size();
93 
94     if (osAccounts.empty()) {
95         return STRING_EMPTY;
96     }
97 
98     std::string localAccountId = std::to_string(osAccounts.begin()->GetLocalId());
99 
100     std::string command = TOOL_NAME + " switch -i " + localAccountId;
101     GTEST_LOG_(INFO) << "command = " << command;
102 
103     std::string commandResult = ToolSystemTest::ExecuteCommand(command);
104     GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToFirstOsAccount commandResult = " << commandResult;
105     return commandResult;
106 }
107 
SwitchToLastOsAccount()108 std::string AccountCommandUtil::SwitchToLastOsAccount()
109 {
110     std::vector<OsAccountInfo> osAccounts;
111     ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts);
112     GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToLastOsAccount result = " << result;
113     GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToLastOsAccount osAccounts size = " << osAccounts.size();
114 
115     if (osAccounts.empty()) {
116         return STRING_EMPTY;
117     }
118 
119     std::string localAccountId = std::to_string(osAccounts.rbegin()->GetLocalId());
120 
121     std::string command = TOOL_NAME + " switch -i " + localAccountId;
122     GTEST_LOG_(INFO) << "command = " << command;
123 
124     std::string commandResult = ToolSystemTest::ExecuteCommand(command);
125     GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToLastOsAccount commandResult = " << commandResult;
126     return commandResult;
127 }
128 }  // namespace AccountSA
129 }  // namespace OHOS
130