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 #include <fstream>
17 #include <gtest/gtest.h>
18 #include <map>
19 #include <sstream>
20 #include <string>
21 
22 #include "appexecfwk_errors.h"
23 #include "bundle_info.h"
24 #include "bundle_mgr_service.h"
25 #include "bundle_mgr_proxy.h"
26 #include "bundle_pack_info.h"
27 #include "inner_bundle_info.h"
28 
29 #include "dispatch_info.h"
30 #include "install_result.h"
31 #include "json_util.h"
32 #include "nlohmann/json.hpp"
33 #include "parcel_macro.h"
34 #include "target_ability_info.h"
35 
36 using namespace testing::ext;
37 using namespace std::chrono_literals;
38 using namespace OHOS::AppExecFwk;
39 
40 namespace OHOS {
41 namespace {
42 const std::string VERSION = "1.0.0.1";
43 const std::string TRANSACT_ID = "transact_id";
44 const std::string RESULT_MSG = "result_msg";
45 const int32_t DOWNLOAD_SIZE = 10;
46 const int32_t TOTAL_SIZE = 20;
47 const int32_t REASON_FLAG = 2;
48 const int32_t VERSIONID = 1;
49 const std::string EXT_MAP_KEY = "EXT_MAP_KEY";
50 const std::string EXT_MAP_VALUE = "EXT_MAP_VALUE";
51 const std::string BUNDLE_NAME = "com.example.freeInstall";
52 const std::string MODULE_NAME = "entry";
53 const std::string ABILITY_NAME = "MainAbility";
54 }  // namespace
55 
56 class BmsBundleFreeInstallBaseTest : public testing::Test {
57 public:
58     BmsBundleFreeInstallBaseTest();
59     ~BmsBundleFreeInstallBaseTest();
60     static void SetUpTestCase();
61     static void TearDownTestCase();
62     void SetUp();
63     void TearDown();
64 private:
65     static std::shared_ptr<BundleMgrService> bundleMgrService_;
66 };
67 
68 std::shared_ptr<BundleMgrService> BmsBundleFreeInstallBaseTest::bundleMgrService_ =
69     DelayedSingleton<BundleMgrService>::GetInstance();
70 
BmsBundleFreeInstallBaseTest()71 BmsBundleFreeInstallBaseTest::BmsBundleFreeInstallBaseTest()
72 {}
73 
~BmsBundleFreeInstallBaseTest()74 BmsBundleFreeInstallBaseTest::~BmsBundleFreeInstallBaseTest()
75 {}
76 
SetUpTestCase()77 void BmsBundleFreeInstallBaseTest::SetUpTestCase()
78 {}
79 
TearDownTestCase()80 void BmsBundleFreeInstallBaseTest::TearDownTestCase()
81 {
82     bundleMgrService_->OnStop();
83 }
84 
SetUp()85 void BmsBundleFreeInstallBaseTest::SetUp()
86 {}
87 
TearDown()88 void BmsBundleFreeInstallBaseTest::TearDown()
89 {}
90 
91 /**
92  * @tc.number: BmsBundleFreeInstallBaseTest_0001
93  * Function: DispatcherInfoJson
94  * @tc.name: test DispatcherInfoJson
95  * @tc.desc: DispatcherInfoJson
96  */
97 HWTEST_F(BmsBundleFreeInstallBaseTest, BmsBundleFreeInstallBaseTest_0001, Function | SmallTest | Level0)
98 {
99     DispatcherInfo info;
100     info.version = VERSION;
101     nlohmann::json jsonObj;
102     DispatcherInfo result;
103     from_json(jsonObj, result);
104     EXPECT_NE(info.version, result.version);
105     to_json(jsonObj, info);
106     from_json(jsonObj, result);
107     EXPECT_EQ(info.version, result.version);
108 }
109 
110 /**
111  * @tc.number: BmsBundleFreeInstallBaseTest_0002
112  * Function: DispatcherInfoMarshall
113  * @tc.name: test DispatcherInfoMarshall
114  * @tc.desc: DispatcherInfoMarshall
115  */
116 HWTEST_F(BmsBundleFreeInstallBaseTest, BmsBundleFreeInstallBaseTest_0002, Function | SmallTest | Level0)
117 {
118     DispatcherInfo info;
119     info.version = VERSION;
120     Parcel parcel;
121     auto result = DispatcherInfo::Unmarshalling(parcel);
122     EXPECT_NE(result->version, VERSION);
123     auto ret = info.Marshalling(parcel);
124     EXPECT_TRUE(ret);
125     result = DispatcherInfo::Unmarshalling(parcel);
126     EXPECT_EQ(result->version, VERSION);
127 }
128 
129 /**
130  * @tc.number: BmsBundleFreeInstallBaseTest_0003
131  * Function: ResultJson
132  * @tc.name: test ResultJson
133  * @tc.desc: ResultJson
134  */
135 HWTEST_F(BmsBundleFreeInstallBaseTest, BmsBundleFreeInstallBaseTest_0003, Function | SmallTest | Level0)
136 {
137     Result installResult;
138     installResult.transactId = TRANSACT_ID;
139     installResult.resultMsg = RESULT_MSG;
140     installResult.retCode = VERSIONID;
141     nlohmann::json jsonObj;
142     Result result;
143     from_json(jsonObj, result);
144     EXPECT_NE(result.transactId, TRANSACT_ID);
145     to_json(jsonObj, installResult);
146     from_json(jsonObj, result);
147     EXPECT_EQ(result.transactId, TRANSACT_ID);
148 }
149 
150 /**
151  * @tc.number: BmsBundleFreeInstallBaseTest_0004
152  * Function: InstallResultJson
153  * @tc.name: test InstallResultJson
154  * @tc.desc: InstallResultJson
155  */
156 HWTEST_F(BmsBundleFreeInstallBaseTest, BmsBundleFreeInstallBaseTest_0004, Function | SmallTest | Level0)
157 {
158     InstallResult installResult;
159     installResult.version = VERSION;
160     nlohmann::json jsonObj;
161     InstallResult result;
162     from_json(jsonObj, result);
163     EXPECT_NE(result.version, VERSION);
164     to_json(jsonObj, installResult);
165     from_json(jsonObj, result);
166     EXPECT_EQ(result.version, VERSION);
167 }
168 
169 /**
170  * @tc.number: BmsBundleFreeInstallBaseTest_0005
171  * Function: ResultMarshalling
172  * @tc.name: test ResultMarshalling
173  * @tc.desc: ResultMarshalling
174  */
175 HWTEST_F(BmsBundleFreeInstallBaseTest, BmsBundleFreeInstallBaseTest_0005, Function | SmallTest | Level0)
176 {
177     Result installResult;
178     installResult.transactId = TRANSACT_ID;
179     installResult.resultMsg = RESULT_MSG;
180     installResult.retCode = 1;
181     Parcel parcel;
182     auto unmarshalledResult = Result::Unmarshalling(parcel);
183     EXPECT_NE(unmarshalledResult->transactId, TRANSACT_ID);
184     auto ret = installResult.Marshalling(parcel);
185     EXPECT_TRUE(ret);
186     unmarshalledResult = Result::Unmarshalling(parcel);
187     EXPECT_EQ(unmarshalledResult->transactId, TRANSACT_ID);
188     ret = unmarshalledResult->ReadFromParcel(parcel);
189     EXPECT_TRUE(ret);
190 }
191 
192 /**
193  * @tc.number: BmsBundleFreeInstallBaseTest_0006
194  * Function: ProgressMarshalling
195  * @tc.name: test ProgressMarshalling
196  * @tc.desc: ProgressMarshalling
197  */
198 HWTEST_F(BmsBundleFreeInstallBaseTest, BmsBundleFreeInstallBaseTest_0006, Function | SmallTest | Level0)
199 {
200     Progress progress;
201     progress.downloadSize = DOWNLOAD_SIZE;
202     progress.totalSize = TOTAL_SIZE;
203     Parcel parcel;
204     auto result = Progress::Unmarshalling(parcel);
205     EXPECT_NE(result->downloadSize, DOWNLOAD_SIZE);
206     auto ret = progress.Marshalling(parcel);
207     EXPECT_TRUE(ret);
208     result = Progress::Unmarshalling(parcel);
209     EXPECT_EQ(result->downloadSize, DOWNLOAD_SIZE);
210     EXPECT_EQ(result->totalSize, TOTAL_SIZE);
211     ret = result->ReadFromParcel(parcel);
212     EXPECT_TRUE(ret);
213 }
214 
215 /**
216  * @tc.number: BmsBundleFreeInstallBaseTest_0007
217  * Function: InstallResultMarshalling
218  * @tc.name: test InstallResultMarshalling
219  * @tc.desc: InstallResultMarshalling
220  */
221 HWTEST_F(BmsBundleFreeInstallBaseTest, BmsBundleFreeInstallBaseTest_0007, Function | SmallTest | Level0)
222 {
223     Result installResult;
224     installResult.transactId = TRANSACT_ID;
225     installResult.resultMsg = RESULT_MSG;
226     installResult.retCode = VERSIONID;
227     Parcel parcel;
228     auto ret = installResult.Marshalling(parcel);
229     EXPECT_TRUE(ret);
230     auto unmarshalledResult = Result::Unmarshalling(parcel);
231     EXPECT_EQ(unmarshalledResult->transactId, TRANSACT_ID);
232     ret = unmarshalledResult->ReadFromParcel(parcel);
233     EXPECT_TRUE(ret);
234 }
235 
236 /**
237  * @tc.number: BmsBundleFreeInstallBaseTest_0008
238  * Function: TargetInfo
239  * @tc.name: test TargetInfo
240  * @tc.desc: TargetInfo
241  */
242 HWTEST_F(BmsBundleFreeInstallBaseTest, BmsBundleFreeInstallBaseTest_0008, Function | SmallTest | Level0)
243 {
244     TargetInfo targetInfo;
245     targetInfo.bundleName = Constants::BUNDLE_NAME;
246 
247     Parcel parcel;
248     auto unmarshalledResult = TargetInfo::Unmarshalling(parcel);
249     auto ret = targetInfo.Marshalling(parcel);
250     EXPECT_TRUE(ret);
251     unmarshalledResult = TargetInfo::Unmarshalling(parcel);
252     EXPECT_EQ(unmarshalledResult->bundleName, Constants::BUNDLE_NAME);
253 }
254 
255 /**
256  * @tc.number: BmsBundleFreeInstallBaseTest_0009
257  * Function: TargetInfo
258  * @tc.name: test TargetInfo
259  * @tc.desc: TargetInfo
260  */
261 HWTEST_F(BmsBundleFreeInstallBaseTest, BmsBundleFreeInstallBaseTest_0009, Function | SmallTest | Level0)
262 {
263     TargetInfo targetInfo;
264     targetInfo.bundleName = Constants::BUNDLE_NAME;
265     targetInfo.moduleName = Constants::MODULE_NAME;
266     targetInfo.abilityName = Constants::ABILITY_NAME;
267     targetInfo.flags = REASON_FLAG;
268     targetInfo.reasonFlag = REASON_FLAG;
269 
270     nlohmann::json jsonObject;
271     TargetInfo result;
272     from_json(jsonObject, result);
273     EXPECT_NE(result.bundleName, Constants::BUNDLE_NAME);
274     to_json(jsonObject, targetInfo);
275     from_json(jsonObject, result);
276     EXPECT_EQ(result.bundleName, Constants::BUNDLE_NAME);
277     EXPECT_EQ(result.moduleName, Constants::MODULE_NAME);
278     EXPECT_EQ(result.abilityName, Constants::ABILITY_NAME);
279     EXPECT_EQ(result.flags, REASON_FLAG);
280     EXPECT_EQ(result.reasonFlag, REASON_FLAG);
281 }
282 
283 /**
284  * @tc.number: BmsBundleFreeInstallBaseTest_0010
285  * Function: TargetExtSetting
286  * @tc.name: test TargetExtSetting
287  * @tc.desc: TargetExtSetting
288  */
289 HWTEST_F(BmsBundleFreeInstallBaseTest, BmsBundleFreeInstallBaseTest_0010, Function | SmallTest | Level0)
290 {
291     TargetExtSetting targetExtSetting;
292     std::map<std::string, std::string> value;
293     value.emplace("1", "2");
294     targetExtSetting.extValues = value;
295 
296     Parcel parcel;
297     auto unmarshalledResult = TargetExtSetting::Unmarshalling(parcel);
298     bool ret = targetExtSetting.Marshalling(parcel);
299     EXPECT_TRUE(ret);
300     unmarshalledResult = TargetExtSetting::Unmarshalling(parcel);
301     EXPECT_EQ(unmarshalledResult->extValues.size(), 1);
302 }
303 
304 /**
305  * @tc.number: BmsBundleFreeInstallBaseTest_0010
306  * Function: TargetExtSetting
307  * @tc.name: test TargetExtSetting
308  * @tc.desc: TargetExtSetting
309  */
310 HWTEST_F(BmsBundleFreeInstallBaseTest, BmsBundleFreeInstallBaseTest_0011, Function | SmallTest | Level0)
311 {
312     TargetExtSetting targetExtSetting;
313     std::map<std::string, std::string> value;
314     value.emplace("1", "2");
315     targetExtSetting.extValues = value;
316     nlohmann::json jsonObject;
317     TargetExtSetting result;
318     from_json(jsonObject, result);
319     EXPECT_NE(result.extValues, value);
320     to_json(jsonObject, targetExtSetting);
321     from_json(jsonObject, result);
322     EXPECT_EQ(result.extValues, value);
323 }
324 
325 /**
326  * @tc.number: BmsBundleFreeInstallBaseTest_0012
327  * Function: TargetExtSetting
328  * @tc.name: test TargetExtSetting
329  * @tc.desc: TargetExtSetting
330  */
331 HWTEST_F(BmsBundleFreeInstallBaseTest, BmsBundleFreeInstallBaseTest_0012, Function | SmallTest | Level0)
332 {
333     TargetAbilityInfo targetAbilityInfo;
334     targetAbilityInfo.version = "version";
335 
336     Parcel parcel;
337     auto unmarshalledResult = TargetAbilityInfo::Unmarshalling(parcel);
338     bool ret = targetAbilityInfo.Marshalling(parcel);
339     EXPECT_TRUE(ret);
340     unmarshalledResult = TargetAbilityInfo::Unmarshalling(parcel);
341     EXPECT_EQ(unmarshalledResult->version, "version");
342 }
343 
344 /**
345  * @tc.number: BmsBundleFreeInstallBaseTest_0013
346  * Function: TargetExtSetting
347  * @tc.name: test TargetExtSetting
348  * @tc.desc: TargetExtSetting
349  */
350 HWTEST_F(BmsBundleFreeInstallBaseTest, BmsBundleFreeInstallBaseTest_0013, Function | SmallTest | Level0)
351 {
352     TargetAbilityInfo targetAbilityInfo;
353     targetAbilityInfo.version = "version";
354     nlohmann::json jsonObject;
355     TargetAbilityInfo result;
356     from_json(jsonObject, result);
357     EXPECT_NE(result.version, "version");
358     to_json(jsonObject, targetAbilityInfo);
359     from_json(jsonObject, result);
360     EXPECT_EQ(result.version, "version");
361 }
362 }