Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 19 | #include "android-base/file.h" |
| 20 | #include "android-base/unique_fd.h" |
| 21 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 22 | #include "TestHelpers.h" |
| 23 | #include "data/basic/R.h" |
| 24 | |
| 25 | using com::android::basic::R; |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | TEST(ApkAssetsTest, LoadApk) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 30 | std::unique_ptr<const ApkAssets> loaded_apk = |
| 31 | ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk"); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 32 | ASSERT_NE(nullptr, loaded_apk); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 33 | EXPECT_NE(nullptr, loaded_apk->GetLoadedArsc()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 34 | |
| 35 | std::unique_ptr<Asset> asset = loaded_apk->Open("res/layout/main.xml"); |
| 36 | ASSERT_NE(nullptr, asset); |
| 37 | } |
| 38 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 39 | TEST(ApkAssetsTest, LoadApkAsSharedLibrary) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 40 | std::unique_ptr<const ApkAssets> loaded_apk = |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 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 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 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 | |
| 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 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 94 | } // namespace android |