1 /*
2 * Copyright (c) 2023 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 "tool_system_test.h"
17
18 #include <gtest/gtest.h>
19 #include <thread>
20
21 using namespace OHOS::AppExecFwk;
22
23 namespace {
24 const std::string STRING_INSTALL_BUNDLE_OK = "install bundle successfully.";
25 const std::string STRING_UNINSTALL_BUNDLE_OK = "uninstall bundle successfully.";
26 } // namespace
27
ExecuteCommand(const std::string & command)28 std::string ToolSystemTest::ExecuteCommand(const std::string& command)
29 {
30 std::string result = "";
31 FILE* file = popen(command.c_str(), "r");
32
33 // wait for services
34 std::this_thread::sleep_for(std::chrono::seconds(TIME_DELAY_FOR_SERVICES));
35
36 if (file != nullptr) {
37 char commandResult[1024] = { 0 };
38 while ((fgets(commandResult, sizeof(commandResult), file)) != nullptr) {
39 result.append(commandResult);
40 }
41 pclose(file);
42 file = nullptr;
43 }
44
45 return result;
46 }
47
InstallBundle(const std::string & bundlePath,const bool checkResult)48 void ToolSystemTest::InstallBundle(const std::string& bundlePath, const bool checkResult)
49 {
50 // install a bundle
51 std::string command = "bm install -p " + bundlePath;
52 std::string commandResult = ExecuteCommand(command);
53
54 if (checkResult) {
55 EXPECT_PRED2(ToolSystemTest::IsSubSequence, commandResult, STRING_INSTALL_BUNDLE_OK + "\n");
56 }
57 }
58
UninstallBundle(const std::string & bundleName,const bool checkResult)59 void ToolSystemTest::UninstallBundle(const std::string& bundleName, const bool checkResult)
60 {
61 // uninstall a bundle
62 std::string command = "bm uninstall -n " + bundleName;
63 std::string commandResult = ExecuteCommand(command);
64
65 if (checkResult) {
66 EXPECT_PRED2(ToolSystemTest::IsSubSequence, commandResult, STRING_UNINSTALL_BUNDLE_OK + "\n");
67 }
68 }
69
IsSubSequence(const std::string & str,const std::string & subStr)70 bool ToolSystemTest::IsSubSequence(const std::string& str, const std::string& subStr)
71 {
72 return str.find(subStr) != std::string::npos;
73 }
74