• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/unique_fd.h"
21 
22 #include "TestHelpers.h"
23 #include "data/basic/R.h"
24 
25 using com::android::basic::R;
26 
27 namespace android {
28 
TEST(ApkAssetsTest,LoadApk)29 TEST(ApkAssetsTest, LoadApk) {
30   std::unique_ptr<const ApkAssets> loaded_apk =
31       ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk");
32   ASSERT_NE(nullptr, loaded_apk);
33   EXPECT_NE(nullptr, loaded_apk->GetLoadedArsc());
34 
35   std::unique_ptr<Asset> asset = loaded_apk->Open("res/layout/main.xml");
36   ASSERT_NE(nullptr, asset);
37 }
38 
TEST(ApkAssetsTest,LoadApkAsSharedLibrary)39 TEST(ApkAssetsTest, LoadApkAsSharedLibrary) {
40   std::unique_ptr<const ApkAssets> loaded_apk =
41       ApkAssets::Load(GetTestDataPath() + "/appaslib/appaslib.apk");
42   ASSERT_NE(nullptr, loaded_apk);
43   const LoadedArsc* loaded_arsc = loaded_apk->GetLoadedArsc();
44   ASSERT_NE(nullptr, loaded_arsc);
45   ASSERT_EQ(1u, loaded_arsc->GetPackages().size());
46   EXPECT_FALSE(loaded_arsc->GetPackages()[0]->IsDynamic());
47 
48   loaded_apk = ApkAssets::LoadAsSharedLibrary(GetTestDataPath() + "/appaslib/appaslib.apk");
49   ASSERT_NE(nullptr, loaded_apk);
50 
51   loaded_arsc = loaded_apk->GetLoadedArsc();
52   ASSERT_NE(nullptr, loaded_arsc);
53   ASSERT_EQ(1u, loaded_arsc->GetPackages().size());
54   EXPECT_TRUE(loaded_arsc->GetPackages()[0]->IsDynamic());
55 }
56 
TEST(ApkAssetsTest,CreateAndDestroyAssetKeepsApkAssetsOpen)57 TEST(ApkAssetsTest, CreateAndDestroyAssetKeepsApkAssetsOpen) {
58   std::unique_ptr<const ApkAssets> loaded_apk =
59       ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk");
60   ASSERT_NE(nullptr, loaded_apk);
61 
62   {
63     std::unique_ptr<Asset> assets = loaded_apk->Open("res/layout/main.xml", Asset::ACCESS_BUFFER);
64     ASSERT_NE(nullptr, assets);
65   }
66 
67   {
68     std::unique_ptr<Asset> assets = loaded_apk->Open("res/layout/main.xml", Asset::ACCESS_BUFFER);
69     ASSERT_NE(nullptr, assets);
70   }
71 }
72 
TEST(ApkAssetsTest,OpenUncompressedAssetFd)73 TEST(ApkAssetsTest, OpenUncompressedAssetFd) {
74   std::unique_ptr<const ApkAssets> loaded_apk =
75       ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk");
76   ASSERT_NE(nullptr, loaded_apk);
77 
78   auto asset = loaded_apk->Open("assets/uncompressed.txt", Asset::ACCESS_UNKNOWN);
79   ASSERT_NE(nullptr, asset);
80 
81   off64_t start, length;
82   base::unique_fd fd(asset->openFileDescriptor(&start, &length));
83   EXPECT_GE(fd.get(), 0);
84 
85   lseek64(fd.get(), start, SEEK_SET);
86 
87   std::string buffer;
88   buffer.resize(length);
89   ASSERT_TRUE(base::ReadFully(fd.get(), &*buffer.begin(), length));
90 
91   EXPECT_EQ("This should be uncompressed.\n\n", buffer);
92 }
93 
94 }  // namespace android
95