• 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 
DeleteLastOsAccount()72 std::string AccountCommandUtil::DeleteLastOsAccount()
73 {
74     std::vector<OsAccountInfo> osAccounts;
75     ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts);
76     GTEST_LOG_(INFO) << "AccountCommandUtil::DeleteLastOsAccount result = " << result;
77     GTEST_LOG_(INFO) << "AccountCommandUtil::DeleteLastOsAccount osAccounts size = " << osAccounts.size();
78     if (osAccounts.empty()) {
79         return STRING_EMPTY;
80     }
81 
82     std::string localAccountId = std::to_string(osAccounts.rbegin()->GetLocalId());
83 
84     std::string command = TOOL_NAME + " delete -i " + localAccountId;
85     GTEST_LOG_(INFO) << "command = " << command;
86 
87     std::string commandResult = ExecuteCommand(command);
88     GTEST_LOG_(INFO) << "commandResult = " << commandResult;
89     return commandResult;
90 }
91 
DumpLastOsAccount()92 std::string AccountCommandUtil::DumpLastOsAccount()
93 {
94     std::vector<OsAccountInfo> osAccounts;
95     ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts);
96     GTEST_LOG_(INFO) << "AccountCommandUtil::DumpLastOsAccount result = " << result;
97     GTEST_LOG_(INFO) << "AccountCommandUtil::DumpLastOsAccount osAccounts size = " << osAccounts.size();
98 
99     if (osAccounts.empty()) {
100         return STRING_EMPTY;
101     }
102 
103     std::string localAccountId = std::to_string(osAccounts.rbegin()->GetLocalId());
104 
105     std::string command = TOOL_NAME + " dump -i " + localAccountId;
106     GTEST_LOG_(INFO) << "command = " << command;
107 
108     std::string commandResult = ExecuteCommand(command);
109     GTEST_LOG_(INFO) << "AccountCommandUtil::DumpLastOsAccount commandResult " << commandResult;
110     return commandResult;
111 }
112 
SwitchToFirstOsAccount()113 std::string AccountCommandUtil::SwitchToFirstOsAccount()
114 {
115     std::vector<OsAccountInfo> osAccounts;
116     ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts);
117     GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToFirstOsAccount result = " << result;
118     GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToFirstOsAccount osAccounts size = " << osAccounts.size();
119 
120     if (osAccounts.empty()) {
121         return STRING_EMPTY;
122     }
123 
124     std::string localAccountId = std::to_string(osAccounts.begin()->GetLocalId());
125 
126     std::string command = TOOL_NAME + " switch -i " + localAccountId;
127     GTEST_LOG_(INFO) << "command = " << command;
128 
129     std::string commandResult = ExecuteCommand(command);
130     GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToFirstOsAccount commandResult = " << commandResult;
131     return commandResult;
132 }
133 
SwitchToLastOsAccount()134 std::string AccountCommandUtil::SwitchToLastOsAccount()
135 {
136     std::vector<OsAccountInfo> osAccounts;
137     ErrCode result = OsAccountManager::QueryAllCreatedOsAccounts(osAccounts);
138     GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToLastOsAccount result = " << result;
139     GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToLastOsAccount osAccounts size = " << osAccounts.size();
140 
141     if (osAccounts.empty()) {
142         return STRING_EMPTY;
143     }
144 
145     std::string localAccountId = std::to_string(osAccounts.rbegin()->GetLocalId());
146 
147     std::string command = TOOL_NAME + " switch -i " + localAccountId;
148     GTEST_LOG_(INFO) << "command = " << command;
149 
150     std::string commandResult = ExecuteCommand(command);
151     GTEST_LOG_(INFO) << "AccountCommandUtil::SwitchToLastOsAccount commandResult = " << commandResult;
152     return commandResult;
153 }
154 }  // namespace AccountSA
155 }  // namespace OHOS
156