1/*
2 * Copyright (c) 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#ifndef FOUNDATION_BUNDLEMANAGER_BUNDLE_FRAMEWORK_BUNDLE_TOOL_INCLUDE_BUNDLE_TEST_TOOL_H
17#define FOUNDATION_BUNDLEMANAGER_BUNDLE_FRAMEWORK_BUNDLE_TOOL_INCLUDE_BUNDLE_TEST_TOOL_H
18
19#include "shell_command.h"
20#include "bundle_event_callback_host.h"
21#include "bundle_mgr_interface.h"
22#include "bundle_installer_interface.h"
23#ifdef DISTRIBUTED_BUNDLE_FRAMEWORK
24#include "distributed_bms_interface.h"
25#endif
26
27namespace OHOS {
28namespace AppExecFwk {
29class BundleEventCallbackImpl : public BundleEventCallbackHost {
30public:
31    BundleEventCallbackImpl();
32    virtual ~BundleEventCallbackImpl() override;
33    virtual void OnReceiveEvent(const EventFwk::CommonEventData eventData) override;
34
35private:
36    DISALLOW_COPY_AND_MOVE(BundleEventCallbackImpl);
37};
38
39class BundleTestTool : public ShellCommand {
40public:
41    BundleTestTool(int argc, char *argv[]);
42    ~BundleTestTool();
43
44private:
45    ErrCode CreateCommandMap() override;
46    ErrCode CreateMessageMap() override;
47    ErrCode Init() override;
48    void CreateQuickFixMsgMap(std::unordered_map<int32_t, std::string> &quickFixMsgMap);
49    std::string GetResMsg(int32_t code);
50    std::string GetResMsg(int32_t code, const std::shared_ptr<QuickFixResult> &quickFixRes);
51
52    ErrCode RunAsHelpCommand();
53    ErrCode RunAsCheckCommand();
54    ErrCode CheckOperation(int userId, std::string deviceId, std::string bundleName,
55        std::string moduleName, std::string abilityName);
56    ErrCode RunAsSetRemovableCommand();
57    ErrCode RunAsGetRemovableCommand();
58    ErrCode RunAsInstallSandboxCommand();
59    ErrCode RunAsUninstallSandboxCommand();
60    ErrCode RunAsDumpSandboxCommand();
61    ErrCode RunAsGetStringCommand();
62    ErrCode RunAsGetIconCommand();
63    ErrCode RunAsAddInstallRuleCommand();
64    ErrCode RunAsGetInstallRuleCommand();
65    ErrCode RunAsDeleteInstallRuleCommand();
66    ErrCode RunAsCleanInstallRuleCommand();
67    ErrCode RunAsAddAppRunningRuleCommand();
68    ErrCode RunAsDeleteAppRunningRuleCommand();
69    ErrCode RunAsCleanAppRunningRuleCommand();
70    ErrCode RunAsGetAppRunningControlRuleCommand();
71    ErrCode RunAsGetAppRunningControlRuleResultCommand();
72    ErrCode RunAsDeployQuickFix();
73    ErrCode RunAsSwitchQuickFix();
74    ErrCode RunAsDeleteQuickFix();
75    ErrCode RunAsSetDebugMode();
76    ErrCode RunAsGetBundleStats();
77    ErrCode RunAsGetAppProvisionInfo();
78    ErrCode RunAsGetContinueBundleName();
79    ErrCode RunAsGetDistributedBundleName();
80    ErrCode HandleBundleEventCallback();
81    ErrCode ResetAOTCompileStatus();
82    ErrCode SendCommonEvent();
83    ErrCode RunAsGetProxyDataCommand();
84    ErrCode RunAsGetAllProxyDataCommand();
85    ErrCode RunAsSetExtNameOrMIMEToAppCommand();
86    ErrCode RunAsDelExtNameOrMIMEToAppCommand();
87    ErrCode RunAsQueryDataGroupInfos();
88    ErrCode RunAsGetGroupDir();
89    ErrCode RunAsGetJsonProfile();
90    ErrCode RunAsGetUninstalledBundleInfo();
91    ErrCode RunAsGetOdid();
92    ErrCode RunGetUidByBundleName();
93    ErrCode CheckImplicitQueryWantOption(int option, std::string &value);
94    ErrCode ImplicitQuerySkillUriInfo(const std::string &bundleName,
95        const std::string &action, const std::string &entity, const std::string &uri,
96        const std::string &type, std::string &msg);
97    ErrCode RunAsImplicitQuerySkillUriInfo();
98    ErrCode RunAsQueryAbilityInfoByContinueType();
99    ErrCode RunAsCleanBundleCacheFilesAutomaticCommand();
100    ErrCode RunAsUpdateAppEncryptedStatus();
101    ErrCode RunAsGetDirByBundleNameAndAppIndex();
102    ErrCode RunAsIsBundleInstalled();
103
104    std::condition_variable cv_;
105    std::mutex mutex_;
106    bool dataReady_ {false};
107
108    sptr<IBundleMgr> bundleMgrProxy_;
109    sptr<IBundleInstaller> bundleInstallerProxy_;
110#ifdef DISTRIBUTED_BUNDLE_FRAMEWORK
111    sptr<IDistributedBms> distributedBmsProxy_;
112#endif
113
114    bool CheckRemovableErrorOption(int option, int counter, const std::string &commandName);
115    bool CheckRemovableCorrectOption(int option, const std::string &commandName, int &isRemovable, std::string &name);
116    bool SetIsRemovableOperation(const std::string &bundleName, const std::string &moduleName, int isRemovable) const;
117    bool GetIsRemovableOperation(
118        const std::string &bundleName, const std::string &moduleName, std::string &result) const;
119    bool CheckSandboxErrorOption(int option, int counter, const std::string &commandName);
120    bool CheckGetStringCorrectOption(int option, const std::string &commandName, int &temp, std::string &name);
121    bool CheckGetIconCorrectOption(int option, const std::string &commandName, int &temp, std::string &name);
122    ErrCode CheckAddInstallRuleCorrectOption(int option, const std::string &commandName,
123        std::vector<std::string> &appIds, int &controlRuleType, int &userId, int &euid);
124    ErrCode CheckGetInstallRuleCorrectOption(int option, const std::string &commandName, int &controlRuleType,
125        int &userId, int &euid);
126    ErrCode CheckDeleteInstallRuleCorrectOption(int option, const std::string &commandName,
127        int &controlRuleType, std::vector<std::string> &appIds, int &userId, int &euid);
128    ErrCode CheckCleanInstallRuleCorrectOption(int option, const std::string &commandName,
129        int &controlRuleType, int &userId, int &euid);
130    ErrCode CheckAppRunningRuleCorrectOption(int option, const std::string &commandName,
131        std::vector<AppRunningControlRule> &controlRule, int &userId, int &euid);
132    ErrCode CheckCleanAppRunningRuleCorrectOption(int option, const std::string &commandName, int &userId, int &euid);
133    ErrCode CheckGetAppRunningRuleCorrectOption(int option, const std::string &commandName,
134        int32_t &userId, int &euid);
135    ErrCode CheckGetAppRunningRuleResultCorrectOption(int option, const std::string &commandName,
136        std::string &bundleName, int32_t &userId, int &euid);
137    bool CheckSandboxCorrectOption(int option, const std::string &commandName, int &data, std::string &bundleName);
138    bool CheckGetProxyDataCorrectOption(int option, const std::string &commandName, int &temp, std::string &name);
139    bool CheckGetAllProxyDataCorrectOption(int option, const std::string &commandName, int &temp, std::string &name);
140    bool CheckExtOrMimeCorrectOption(int option, const std::string &commandName, int &temp, std::string &name);
141    ErrCode InstallSandboxOperation(
142        const std::string &bundleName, const int32_t userId, const int32_t dlpType, int32_t &appIndex) const;
143    ErrCode UninstallSandboxOperation(
144        const std::string &bundleName, const int32_t appIndex, const int32_t userId) const;
145    ErrCode DumpSandboxBundleInfo(const std::string &bundleName, const int32_t appIndex, const int32_t userId,
146        std::string &dumpResults);
147    ErrCode StringToInt(std::string option, const std::string &commandName, int &temp, bool &result);
148    ErrCode StringToUnsignedLongLong(std::string optarg, const std::string &commandName,
149        uint64_t &temp, bool &result);
150    ErrCode DeployQuickFix(const std::vector<std::string> &quickFixPaths,
151        std::shared_ptr<QuickFixResult> &quickFixRes, bool isDebug);
152    ErrCode SwitchQuickFix(const std::string &bundleName, int32_t enable,
153        std::shared_ptr<QuickFixResult> &quickFixRes);
154    ErrCode DeleteQuickFix(const std::string &bundleName, std::shared_ptr<QuickFixResult> &quickFixRes);
155    ErrCode GetQuickFixPath(int32_t index, std::vector<std::string> &quickFixPaths) const;
156    ErrCode SetDebugMode(int32_t debugMode);
157    bool GetBundleStats(const std::string &bundleName, int32_t userId, std::string &msg, int32_t appIndex);
158    ErrCode GetAppProvisionInfo(const std::string &bundleName, int32_t userId, std::string &msg);
159    ErrCode GetDistributedBundleName(const std::string &networkId, int32_t accessTokenId, std::string &msg);
160    ErrCode BundleNameAndUserIdCommonFunc(std::string &bundleName, int32_t &userId, int32_t &appIndex);
161    ErrCode CheckGetDistributedBundleNameCorrectOption(int32_t option, const std::string &commandName,
162        std::string &networkId, int32_t &accessTokenId);
163    bool QueryDataGroupInfos(const std::string &bundleName, int32_t userId, std::string& msg);
164    bool ParseEventCallbackOptions(bool &onlyUnregister, int32_t &uid);
165    bool ParseResetAOTCompileStatusOptions(std::string &bundleName, std::string &moduleName,
166        int32_t &triggerMode, int32_t &uid);
167    void Sleep(int32_t seconds);
168    bool HandleUnknownOption(const std::string &commandName, bool &ret);
169    ErrCode CallRegisterBundleEventCallback(sptr<BundleEventCallbackImpl> bundleEventCallback);
170    ErrCode CallUnRegisterBundleEventCallback(sptr<BundleEventCallbackImpl> bundleEventCallback);
171    ErrCode CheckGetGroupIdCorrectOption(int32_t option, std::string &dataGroupId);
172    bool GetGroupDir(const std::string &dataGroupId, std::string& msg);
173    ErrCode CheckGetBundleNameOption(int32_t option, std::string &bundleName);
174    ErrCode CheckCleanBundleCacheFilesAutomaticOption(int option, const std::string &commandName,
175        uint64_t &cacheSize);
176    ErrCode GetContinueBundleName(const std::string &bundleName, int32_t userId, std::string& msg);
177};
178}  // namespace AppExecFwk
179}  // namespace OHOS
180
181#endif  // FOUNDATION_BUNDLEMANAGER_BUNDLE_FRAMEWORK_BUNDLE_TOOL_INCLUDE_BUNDLE_TEST_TOOL_H