• 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 <gtest/gtest.h>
17 #include "gmock/gmock.h"
18 #include "bundle_manager_mock.h"
19 
20 namespace OHOS {
21 namespace AppExecFwk {
22 namespace {
23 constexpr int32_t ERR_FAIL_NAMEFORUID = 777;
24 }
QueryExtensionAbilityInfos(const Want & want,const ExtensionAbilityType & extensionType,const int32_t & flag,const int32_t & userId,std::vector<ExtensionAbilityInfo> & extensionInfos)25 bool BundleMgrProxy::QueryExtensionAbilityInfos(const Want &want, const ExtensionAbilityType &extensionType,
26     const int32_t &flag, const int32_t &userId, std::vector<ExtensionAbilityInfo> &extensionInfos)
27 {
28     GTEST_LOG_(INFO) << "mock BundleMgrProxy QueryExtensionAbilityInfos in";
29     if (userId == 1) {
30         GTEST_LOG_(INFO) << "mock BundleMgrProxy QueryExtensionAbilityInfos userId==1 return";
31         return false;
32     }
33     ExtensionAbilityInfo extensionAbilityInfo;
34     AppExecFwk::ElementName admin = want.GetElement();
35     if (admin.GetBundleName() == "com.edm.test.demo22" ||
36         admin.GetBundleName() == "com.edm.test.demo33") {
37     } else {
38         extensionAbilityInfo.bundleName = admin.GetBundleName();
39         extensionAbilityInfo.name = admin.GetAbilityName();
40     }
41     extensionInfos.emplace_back(extensionAbilityInfo);
42     GTEST_LOG_(INFO) << "mock BundleMgrProxy QueryExtensionAbilityInfos bundleName=" << admin.GetBundleName() <<
43         " abilityName=" << admin.GetAbilityName();
44     return true;
45 }
46 
GetNameForUid(const int uid,std::string & name)47 ErrCode BundleMgrProxy::GetNameForUid(const int uid, std::string &name)
48 {
49     GTEST_LOG_(INFO) << "mock BundleMgrProxy GetNameForUid in bundleNameMock_=" << bundleNameMock_;
50     if (bundleNameMock_ == "com.edm.test.demo.ipc.suc") {
51         name = bundleNameMock_;
52     } else if (bundleNameMock_ == "com.edm.test.demo.ipc.fail") {
53         return ERR_FAIL_NAMEFORUID;
54     }
55     return ERR_OK;
56 }
57 
GetBundleInfo(const std::string & bundleName,const BundleFlag flag,BundleInfo & bundleInfo,int32_t userId)58 bool BundleMgrProxy::GetBundleInfo(const std::string &bundleName, const BundleFlag flag,
59     BundleInfo &bundleInfo, int32_t userId)
60 {
61     GTEST_LOG_(INFO) << "mock BundleMgrProxy GetBundleInfo in";
62     if (userId == 0) {
63         GTEST_LOG_(INFO) << "mock BundleMgrProxy GetBundleInfo userId==0 return";
64         return false;
65     }
66     if (bundleName == "com.edm.test.demo") {
67         bundleInfo.reqPermissions.emplace_back("ohos.permission.aaa");
68         bundleInfo.reqPermissions.emplace_back("ohos.permission.bbb");
69         GTEST_LOG_(INFO) << "mock BundleMgrProxy com.edm.test.demo in";
70     } else if (bundleName == "com.edm.test.demo.ipc.suc") {
71         bundleInfo.reqPermissions.emplace_back("ohos.permission.aaa");
72         bundleInfo.reqPermissions.emplace_back("ohos.permission.bbb");
73         GTEST_LOG_(INFO) << "mock BundleMgrProxy com.edm.test.demo.ipc.suc in";
74         bundleNameMock_ = bundleName;
75     } else if (bundleName == "com.edm.test.demo.ipc.fail") {
76         bundleInfo.reqPermissions.emplace_back("ohos.permission.aaa");
77         bundleInfo.reqPermissions.emplace_back("ohos.permission.bbb");
78         GTEST_LOG_(INFO) << "mock BundleMgrProxy com.edm.test.demo.ipc.fail in";
79         bundleNameMock_ = bundleName;
80     } else if (bundleName == "com.edm.test.permission.empty") {
81     } else if (bundleName == "com.edm.test.demo11") {
82         bundleInfo.reqPermissions.emplace_back("ohos.permission.EDM_TEST_PERMISSION");
83         bundleInfo.reqPermissions.emplace_back("ohos.permission.EDM_TEST_ENT_PERMISSION");
84         GTEST_LOG_(INFO) << "mock BundleMgrProxy com.edm.test.demo11 in";
85     } else if ((bundleName == "com.edm.test.demo22")) {
86         bundleInfo.reqPermissions.emplace_back("ohos.permission.aaa");
87         bundleInfo.reqPermissions.emplace_back("ohos.permission.bbb");
88         GTEST_LOG_(INFO) << "mock BundleMgrProxy com.edm.test.demo22 in";
89     } else if ((bundleName == "com.edm.test.demo33")) {
90         bundleInfo.reqPermissions.emplace_back("ohos.permission.EDM_TEST_PERMISSION");
91         bundleInfo.reqPermissions.emplace_back("ohos.permission.EDM_TEST_ENT_PERMISSION");
92         GTEST_LOG_(INFO) << "mock BundleMgrProxy com.edm.test.demo33 in";
93     }
94     return true;
95 }
96 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)97 int BundleMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
98 {
99     GTEST_LOG_(INFO) << "mock BundleMgrStub OnRemoteRequest in";
100     return 0;
101 }
102 
BundleMgrService()103 BundleMgrService::BundleMgrService()
104 {
105     GTEST_LOG_(INFO) << "mock BundleMgrService constructor";
106 }
107 
QueryExtensionAbilityInfos(const Want & want,const ExtensionAbilityType & extensionType,const int32_t & flag,const int32_t & userId,std::vector<ExtensionAbilityInfo> & extensionInfos)108 bool BundleMgrService::QueryExtensionAbilityInfos(const Want &want, const ExtensionAbilityType &extensionType,
109     const int32_t &flag, const int32_t &userId, std::vector<ExtensionAbilityInfo> &extensionInfos)
110 {
111     GTEST_LOG_(INFO) << "mock BundleMgrService QueryExtensionAbilityInfos in";
112     if (userId == 1) {
113         GTEST_LOG_(INFO) << "mock BundleMgrService QueryExtensionAbilityInfos userId==1 return";
114         return false;
115     }
116     ExtensionAbilityInfo extensionAbilityInfo;
117     AppExecFwk::ElementName admin = want.GetElement();
118     if (admin.GetBundleName() == "com.edm.test.demo22" ||
119         admin.GetBundleName() == "com.edm.test.demo33") {
120     } else {
121         extensionAbilityInfo.bundleName = admin.GetBundleName();
122         extensionAbilityInfo.name = admin.GetAbilityName();
123     }
124     extensionInfos.emplace_back(extensionAbilityInfo);
125     GTEST_LOG_(INFO) << "mock BundleMgrService QueryExtensionAbilityInfos bundleName=" << admin.GetBundleName() <<
126         " abilityName=" << admin.GetAbilityName();
127     return true;
128 }
129 
GetNameForUid(const int uid,std::string & name)130 ErrCode BundleMgrService::GetNameForUid(const int uid, std::string &name)
131 {
132     GTEST_LOG_(INFO) << "mock BundleMgrService GetNameForUid in bundleNameMock_=" << bundleNameMock_;
133     if (bundleNameMock_ == "com.edm.test.demo.ipc.suc") {
134         name = bundleNameMock_;
135     } else if (bundleNameMock_ == "com.edm.test.demo.ipc.fail") {
136         return ERR_FAIL_NAMEFORUID;
137     }
138     return ERR_OK;
139 }
140 
GetBundleInfo(const std::string & bundleName,const BundleFlag flag,BundleInfo & bundleInfo,int32_t userId)141 bool BundleMgrService::GetBundleInfo(const std::string &bundleName, const BundleFlag flag,
142     BundleInfo &bundleInfo, int32_t userId)
143 {
144     GTEST_LOG_(INFO) << "mock BundleMgrService GetBundleInfo in";
145     if (userId == 0) {
146         GTEST_LOG_(INFO) << "mock BundleMgrService GetBundleInfo userId==0 return";
147         return false;
148     }
149     if (bundleName == "com.edm.test.demo") {
150         bundleInfo.reqPermissions.emplace_back("ohos.permission.aaa");
151         bundleInfo.reqPermissions.emplace_back("ohos.permission.bbb");
152         GTEST_LOG_(INFO) << "mock BundleMgrService com.edm.test.demo in";
153     } else if (bundleName == "com.edm.test.demo.ipc.suc") {
154         bundleInfo.reqPermissions.emplace_back("ohos.permission.aaa");
155         bundleInfo.reqPermissions.emplace_back("ohos.permission.bbb");
156         GTEST_LOG_(INFO) << "mock BundleMgrService com.edm.test.demo.ipc.suc in";
157         bundleNameMock_ = bundleName;
158     } else if (bundleName == "com.edm.test.demo.ipc.fail") {
159         bundleInfo.reqPermissions.emplace_back("ohos.permission.aaa");
160         bundleInfo.reqPermissions.emplace_back("ohos.permission.bbb");
161         GTEST_LOG_(INFO) << "mock BundleMgrService com.edm.test.demo.ipc.fail in";
162         bundleNameMock_ = bundleName;
163     } else if (bundleName == "com.edm.test.permission.empty") {
164     } else if (bundleName == "com.edm.test.demo11") {
165         bundleInfo.reqPermissions.emplace_back("ohos.permission.EDM_TEST_PERMISSION");
166         bundleInfo.reqPermissions.emplace_back("ohos.permission.EDM_TEST_ENT_PERMISSION");
167         GTEST_LOG_(INFO) << "mock BundleMgrService com.edm.test.demo11 in";
168     } else if ((bundleName == "com.edm.test.demo22")) {
169         bundleInfo.reqPermissions.emplace_back("ohos.permission.aaa");
170         bundleInfo.reqPermissions.emplace_back("ohos.permission.bbb");
171         GTEST_LOG_(INFO) << "mock BundleMgrService com.edm.test.demo22 in";
172     } else if ((bundleName == "com.edm.test.demo33")) {
173         bundleInfo.reqPermissions.emplace_back("ohos.permission.EDM_TEST_PERMISSION");
174         bundleInfo.reqPermissions.emplace_back("ohos.permission.EDM_TEST_ENT_PERMISSION");
175         GTEST_LOG_(INFO) << "mock BundleMgrService com.edm.test.demo33 in";
176     }
177     return true;
178 }
179 }  // namespace AppExecFwk
180 }  // namespace OHOS
181