1545fdf9bSopenharmony_ci/*
2545fdf9bSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3545fdf9bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4545fdf9bSopenharmony_ci * you may not use this file except in compliance with the License.
5545fdf9bSopenharmony_ci * You may obtain a copy of the License at
6545fdf9bSopenharmony_ci *
7545fdf9bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8545fdf9bSopenharmony_ci *
9545fdf9bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10545fdf9bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11545fdf9bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12545fdf9bSopenharmony_ci * See the License for the specific language governing permissions and
13545fdf9bSopenharmony_ci * limitations under the License.
14545fdf9bSopenharmony_ci */
15545fdf9bSopenharmony_ci
16545fdf9bSopenharmony_ci#include "tool_system_test.h"
17545fdf9bSopenharmony_ci
18545fdf9bSopenharmony_ci#include <gtest/gtest.h>
19545fdf9bSopenharmony_ci#include <thread>
20545fdf9bSopenharmony_ci
21545fdf9bSopenharmony_ciusing namespace OHOS::AppExecFwk;
22545fdf9bSopenharmony_ci
23545fdf9bSopenharmony_cinamespace {
24545fdf9bSopenharmony_ciconst std::string STRING_INSTALL_BUNDLE_OK = "install bundle successfully.";
25545fdf9bSopenharmony_ciconst std::string STRING_UNINSTALL_BUNDLE_OK = "uninstall bundle successfully.";
26545fdf9bSopenharmony_ci}  // namespace
27545fdf9bSopenharmony_ci
28545fdf9bSopenharmony_cistd::string ToolSystemTest::ExecuteCommand(const std::string& command)
29545fdf9bSopenharmony_ci{
30545fdf9bSopenharmony_ci    std::string result = "";
31545fdf9bSopenharmony_ci    FILE* file = popen(command.c_str(), "r");
32545fdf9bSopenharmony_ci
33545fdf9bSopenharmony_ci    // wait for services
34545fdf9bSopenharmony_ci    std::this_thread::sleep_for(std::chrono::seconds(TIME_DELAY_FOR_SERVICES));
35545fdf9bSopenharmony_ci
36545fdf9bSopenharmony_ci    if (file != nullptr) {
37545fdf9bSopenharmony_ci        char commandResult[1024] = { 0 };
38545fdf9bSopenharmony_ci        while ((fgets(commandResult, sizeof(commandResult), file)) != nullptr) {
39545fdf9bSopenharmony_ci            result.append(commandResult);
40545fdf9bSopenharmony_ci        }
41545fdf9bSopenharmony_ci        pclose(file);
42545fdf9bSopenharmony_ci        file = nullptr;
43545fdf9bSopenharmony_ci    }
44545fdf9bSopenharmony_ci
45545fdf9bSopenharmony_ci    return result;
46545fdf9bSopenharmony_ci}
47545fdf9bSopenharmony_ci
48545fdf9bSopenharmony_civoid ToolSystemTest::InstallBundle(const std::string& bundlePath, const bool checkResult)
49545fdf9bSopenharmony_ci{
50545fdf9bSopenharmony_ci    // install a bundle
51545fdf9bSopenharmony_ci    std::string command = "bm install -p " + bundlePath;
52545fdf9bSopenharmony_ci    std::string commandResult = ExecuteCommand(command);
53545fdf9bSopenharmony_ci
54545fdf9bSopenharmony_ci    if (checkResult) {
55545fdf9bSopenharmony_ci        EXPECT_PRED2(ToolSystemTest::IsSubSequence, commandResult, STRING_INSTALL_BUNDLE_OK + "\n");
56545fdf9bSopenharmony_ci    }
57545fdf9bSopenharmony_ci}
58545fdf9bSopenharmony_ci
59545fdf9bSopenharmony_civoid ToolSystemTest::UninstallBundle(const std::string& bundleName, const bool checkResult)
60545fdf9bSopenharmony_ci{
61545fdf9bSopenharmony_ci    // uninstall a bundle
62545fdf9bSopenharmony_ci    std::string command = "bm uninstall -n " + bundleName;
63545fdf9bSopenharmony_ci    std::string commandResult = ExecuteCommand(command);
64545fdf9bSopenharmony_ci
65545fdf9bSopenharmony_ci    if (checkResult) {
66545fdf9bSopenharmony_ci        EXPECT_PRED2(ToolSystemTest::IsSubSequence, commandResult, STRING_UNINSTALL_BUNDLE_OK + "\n");
67545fdf9bSopenharmony_ci    }
68545fdf9bSopenharmony_ci}
69545fdf9bSopenharmony_ci
70545fdf9bSopenharmony_cibool ToolSystemTest::IsSubSequence(const std::string& str, const std::string& subStr)
71545fdf9bSopenharmony_ci{
72545fdf9bSopenharmony_ci    return str.find(subStr) != std::string::npos;
73545fdf9bSopenharmony_ci}
74