1 /*
2  * Copyright (C) 2016 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 "androidfw/ApkAssets.h"
18 
19 #include "android-base/file.h"
20 #include "android-base/test_utils.h"
21 #include "android-base/unique_fd.h"
22 #include "androidfw/Util.h"
23 
24 #include "TestHelpers.h"
25 #include "data/basic/R.h"
26 
27 using ::android::base::unique_fd;
28 using ::com::android::basic::R;
29 using ::testing::Eq;
30 using ::testing::Ge;
31 using ::testing::NotNull;
32 using ::testing::SizeIs;
33 using ::testing::StrEq;
34 
35 namespace android {
36 
TEST(ApkAssetsTest,LoadApk)37 TEST(ApkAssetsTest, LoadApk) {
38   auto loaded_apk = ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk");
39   ASSERT_THAT(loaded_apk, NotNull());
40 
41   const LoadedArsc* loaded_arsc = loaded_apk->GetLoadedArsc();
42   ASSERT_THAT(loaded_arsc, NotNull());
43   ASSERT_THAT(loaded_arsc->GetPackageById(0x7fu), NotNull());
44   ASSERT_THAT(loaded_apk->GetAssetsProvider()->Open("res/layout/main.xml"), NotNull());
45 }
46 
TEST(ApkAssetsTest,LoadApkFromFd)47 TEST(ApkAssetsTest, LoadApkFromFd) {
48   const std::string path = GetTestDataPath() + "/basic/basic.apk";
49   unique_fd fd(::open(path.c_str(), O_RDONLY | O_BINARY));
50   ASSERT_THAT(fd.get(), Ge(0));
51 
52   auto loaded_apk = ApkAssets::LoadFromFd(std::move(fd), path);
53   ASSERT_THAT(loaded_apk, NotNull());
54 
55   const LoadedArsc* loaded_arsc = loaded_apk->GetLoadedArsc();
56   ASSERT_THAT(loaded_arsc, NotNull());
57   ASSERT_THAT(loaded_arsc->GetPackageById(0x7fu), NotNull());
58   ASSERT_THAT(loaded_apk->GetAssetsProvider()->Open("res/layout/main.xml"), NotNull());
59 }
60 
TEST(ApkAssetsTest,LoadApkAsSharedLibrary)61 TEST(ApkAssetsTest, LoadApkAsSharedLibrary) {
62   auto loaded_apk = ApkAssets::Load(GetTestDataPath() + "/appaslib/appaslib.apk");
63   ASSERT_THAT(loaded_apk, NotNull());
64 
65   const LoadedArsc* loaded_arsc = loaded_apk->GetLoadedArsc();
66   ASSERT_THAT(loaded_arsc, NotNull());
67   ASSERT_THAT(loaded_arsc->GetPackages(), SizeIs(1u));
68   EXPECT_FALSE(loaded_arsc->GetPackages()[0]->IsDynamic());
69 
70   loaded_apk = ApkAssets::Load(GetTestDataPath() + "/appaslib/appaslib.apk", PROPERTY_DYNAMIC);
71   ASSERT_THAT(loaded_apk, NotNull());
72 
73   loaded_arsc = loaded_apk->GetLoadedArsc();
74   ASSERT_THAT(loaded_arsc, NotNull());
75   ASSERT_THAT(loaded_arsc->GetPackages(), SizeIs(1u));
76   EXPECT_TRUE(loaded_arsc->GetPackages()[0]->IsDynamic());
77 }
78 
TEST(ApkAssetsTest,CreateAndDestroyAssetKeepsApkAssetsOpen)79 TEST(ApkAssetsTest, CreateAndDestroyAssetKeepsApkAssetsOpen) {
80   auto loaded_apk = ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk");
81   ASSERT_THAT(loaded_apk, NotNull());
82 
83   { ASSERT_THAT(loaded_apk->GetAssetsProvider()->Open("res/layout/main.xml",
84                                                       Asset::ACCESS_BUFFER), NotNull()); }
85 
86   { ASSERT_THAT(loaded_apk->GetAssetsProvider()->Open("res/layout/main.xml",
87                                                       Asset::ACCESS_BUFFER), NotNull()); }
88 }
89 
TEST(ApkAssetsTest,OpenUncompressedAssetFd)90 TEST(ApkAssetsTest, OpenUncompressedAssetFd) {
91   auto loaded_apk = ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk");
92   ASSERT_THAT(loaded_apk, NotNull());
93 
94   auto asset = loaded_apk->GetAssetsProvider()->Open("assets/uncompressed.txt",
95                                                      Asset::ACCESS_UNKNOWN);
96   ASSERT_THAT(asset, NotNull());
97 
98   off64_t start, length;
99   unique_fd fd(asset->openFileDescriptor(&start, &length));
100   ASSERT_THAT(fd.get(), Ge(0));
101 
102   lseek64(fd.get(), start, SEEK_SET);
103 
104   std::string buffer;
105   buffer.resize(length);
106   ASSERT_TRUE(base::ReadFully(fd.get(), &*buffer.begin(), length));
107 
108   EXPECT_THAT(buffer, StrEq("This should be uncompressed.\n\n"));
109 }
110 
111 }  // namespace android
112