1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "deploy_patch_generator.h"
18 #include "apk_archive.h"
19 #include "patch_utils.h"
20 
21 #include <android-base/file.h>
22 #include <gtest/gtest.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <string>
26 
27 #include "sysdeps.h"
28 
29 using namespace com::android::fastdeploy;
30 
31 static std::string GetTestFile(const std::string& name) {
32     return "fastdeploy/testdata/" + name;
33 }
34 
35 struct TestPatchGenerator : DeployPatchGenerator {
36     using DeployPatchGenerator::BuildIdenticalEntries;
37     using DeployPatchGenerator::DeployPatchGenerator;
38 };
39 
40 TEST(DeployPatchGeneratorTest, IdenticalFileEntries) {
41     std::string apkPath = GetTestFile("rotating_cube-release.apk");
42     APKMetaData metadataA = PatchUtils::GetHostAPKMetaData(apkPath.c_str());
43     TestPatchGenerator generator(false);
44     std::vector<DeployPatchGenerator::SimpleEntry> entries;
45     generator.BuildIdenticalEntries(entries, metadataA, metadataA);
46     // Expect the entry count to match the number of entries in the metadata.
47     const uint32_t identicalCount = entries.size();
48     const uint32_t entriesCount = metadataA.entries_size();
49     EXPECT_EQ(identicalCount, entriesCount);
50 }
51 
52 TEST(DeployPatchGeneratorTest, NoDeviceMetadata) {
53     std::string apkPath = GetTestFile("rotating_cube-release.apk");
54     // Get size of our test apk.
55     long apkSize = 0;
56     {
57         unique_fd apkFile(adb_open(apkPath.c_str(), O_RDWR));
58         apkSize = adb_lseek(apkFile, 0L, SEEK_END);
59     }
60 
61     // Create a patch that is 100% different.
62     TemporaryFile output;
63     DeployPatchGenerator generator(true);
64     generator.CreatePatch(apkPath.c_str(), {}, output.fd);
65 
66     // Expect a patch file that has a size at least the size of our initial APK.
67     long patchSize = adb_lseek(output.fd, 0L, SEEK_END);
68     EXPECT_GT(patchSize, apkSize);
69 }
70 
71 TEST(DeployPatchGeneratorTest, ZeroSizePatch) {
72     std::string apkPath = GetTestFile("rotating_cube-release.apk");
73     ApkArchive archive(apkPath);
74     auto dump = archive.ExtractMetadata();
75     EXPECT_NE(dump.cd().size(), 0u);
76 
77     APKMetaData metadata = PatchUtils::GetDeviceAPKMetaData(dump);
78 
79     // Create a patch that is 100% the same.
80     TemporaryFile output;
81     output.DoNotRemove();
82     DeployPatchGenerator generator(true);
83     generator.CreatePatch(apkPath.c_str(), metadata, output.fd);
84 
85     // Expect a patch file that is smaller than 0.5K.
86     int64_t patchSize = adb_lseek(output.fd, 0L, SEEK_END);
87     EXPECT_LE(patchSize, 512);
88 }
89