• 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 
23 using namespace testing::ext;
24 using namespace OHOS;
25 using namespace OHOS::AAFwk;
26 
27 namespace OHOS {
28 namespace AccountSA {
29 namespace {
30 const std::string STRING_LOCAL_ACCOUNT_NAME = "local_account_name";
31 const std::string STRING_TYPE = "normal";
32 const std::string STRING_EMPTY = "";
33 }  // namespace
34 
ExecuteCommand(const std::string & command)35 static std::string ExecuteCommand(const std::string& command)
36 {
37     std::string result = "";
38     FILE* file = popen(command.c_str(), "r");
39 
40     if (file != nullptr) {
41         char commandResult[1024] = { 0 };
42         while ((fgets(commandResult, sizeof(commandResult), file)) != nullptr) {
43             result.append(commandResult);
44         }
45         pclose(file);
46         file = nullptr;
47     }
48 
49     return result;
50 }
51 
CreateOsAccount()52 std::string AccountCommandUtil::CreateOsAccount()
53 {
54     std::string command = TOOL_NAME + " create -n " + STRING_LOCAL_ACCOUNT_NAME + " -t " + STRING_TYPE;
55     GTEST_LOG_(INFO) << "command = " << command;
56 
57     std::string commandResult = ExecuteCommand(command);
58     GTEST_LOG_(INFO) << "AccountCommandUtil::CreateOsAccount commandResult = " << commandResult;
59     return commandResult;
60 }
61 
CreateOsAccount(const std::string & name)62 std::string AccountCommandUtil::CreateOsAccount(const std::string &name)
63 {
64     std::string command = TOOL_NAME + " create -n " + name + " -t " + STRING_TYPE;
65     GTEST_LOG_(INFO) << "command = " << command;
66 
67     std::string commandResult = ExecuteCommand(command);
68     GTEST_LOG_(INFO) << "AccountCommandUtil::CreateOsAccount commandResult = " << commandResult;
69     return commandResult;
70 }
71 
CreateOsAccount(const std::string & name,const std::string & type)72 std::string AccountCommandUtil::CreateOsAccount(const std::string &name, const std::string &type)
73 {
74     std::string command = TOOL_NAME + " create -n " + name + " -t " + type;
75     GTEST_LOG_(INFO) << "command = " << command;
76 
77     std::string commandResult = ExecuteCommand(command);
78     GTEST_LOG_(INFO) << "AccountCommandUtil::CreateOsAccount commandResult = " << commandResult;
79     return commandResult;
80 }
81 
DeleteLastOsAccount()82 std::string AccountCommandUtil::DeleteLastOsAccount()
83 {
84     std::vector<OsAccountInfo> osAccounts;
85     ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts);
86     GTEST_LOG_(INFO) << "AccountCommandUtil::DeleteLastOsAccount result = " << result;
87     GTEST_LOG_(INFO) << "AccountCommandUtil::DeleteLastOsAccount osAccounts size = " << osAccounts.size();
88     if (osAccounts.empty()) {
89         return STRING_EMPTY;
90     }
91 
92     std::string localAccountId = std::to_string(osAccounts.rbegin()->GetLocalId());
93 
94     std::string command = TOOL_NAME + " delete -i " + localAccountId;
95     GTEST_LOG_(INFO) << "command = " << command;
96 
97     std::string commandResult = ExecuteCommand(command);
98     GTEST_LOG_(INFO) << "commandResult = " << commandResult;
99     return commandResult;
100 }
101 
DumpLastOsAccount()102 std::string AccountCommandUtil::DumpLastOsAccount()
103 {
104     std::vector<OsAccountInfo> osAccounts;
105     ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts);
106     GTEST_LOG_(INFO) << "AccountCommandUtil::DumpLastOsAccount result = " << result;
107     GTEST_LOG_(INFO) << "AccountCommandUtil::DumpLastOsAccount osAccounts size = " << osAccounts.size();
108 
109     if (osAccounts.empty()) {
110         return STRING_EMPTY;
111     }
112 
113     std::string localAccountId = std::to_string(osAccounts.rbegin()->GetLocalId());
114 
115     std::string command = TOOL_NAME + " dump -i " + localAccountId;
116     GTEST_LOG_(INFO) << "command = " << command;
117 
118     std::string commandResult = ExecuteCommand(command);
119     GTEST_LOG_(INFO) << "AccountCommandUtil::DumpLastOsAccount commandResult " << commandResult;
120     return commandResult;
121 }
122 
SwitchToFirstOsAccount()123 std::string AccountCommandUtil::SwitchToFirstOsAccount()
124 {
125     std::vector<OsAccountInfo> osAccounts;
126     ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts);
127     GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToFirstOsAccount result = " << result;
128     GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToFirstOsAccount osAccounts size = " << osAccounts.size();
129 
130     if (osAccounts.empty()) {
131         return STRING_EMPTY;
132     }
133 
134     std::string localAccountId = std::to_string(osAccounts.begin()->GetLocalId());
135 
136     std::string command = TOOL_NAME + " switch -i " + localAccountId;
137     GTEST_LOG_(INFO) << "command = " << command;
138 
139     std::string commandResult = ExecuteCommand(command);
140     GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToFirstOsAccount commandResult = " << commandResult;
141     return commandResult;
142 }
143 
SwitchToLastOsAccount()144 std::string AccountCommandUtil::SwitchToLastOsAccount()
145 {
146     std::vector<OsAccountInfo> osAccounts;
147     ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts);
148     GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToLastOsAccount result = " << result;
149     GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToLastOsAccount osAccounts size = " << osAccounts.size();
150 
151     if (osAccounts.empty()) {
152         return STRING_EMPTY;
153     }
154 
155     std::string localAccountId = std::to_string(osAccounts.rbegin()->GetLocalId());
156 
157     std::string command = TOOL_NAME + " switch -i " + localAccountId;
158     GTEST_LOG_(INFO) << "command = " << command;
159 
160     std::string commandResult = ExecuteCommand(command);
161     GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToLastOsAccount commandResult = " << commandResult;
162     return commandResult;
163 }
164 }  // namespace AccountSA
165 }  // namespace OHOS
166