blob: c85b0b98ca5e6b1c67605a9de8521cc8cd5e57bb [file] [log] [blame]
Adam Lesinski7ad11102016-10-28 16:39:15 -07001/*
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 Lesinskid1ecd7a2017-01-23 12:58:11 -080019#include "android-base/file.h"
20#include "android-base/unique_fd.h"
21
Adam Lesinski7ad11102016-10-28 16:39:15 -070022#include "TestHelpers.h"
23#include "data/basic/R.h"
24
25using com::android::basic::R;
26
27namespace android {
28
29TEST(ApkAssetsTest, LoadApk) {
Adam Lesinski0c405242017-01-13 20:47:26 -080030 std::unique_ptr<const ApkAssets> loaded_apk =
31 ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk");
Adam Lesinski7ad11102016-10-28 16:39:15 -070032 ASSERT_NE(nullptr, loaded_apk);
Adam Lesinskida431a22016-12-29 16:08:16 -050033 EXPECT_NE(nullptr, loaded_apk->GetLoadedArsc());
Adam Lesinski7ad11102016-10-28 16:39:15 -070034
35 std::unique_ptr<Asset> asset = loaded_apk->Open("res/layout/main.xml");
36 ASSERT_NE(nullptr, asset);
37}
38
Adam Lesinskida431a22016-12-29 16:08:16 -050039TEST(ApkAssetsTest, LoadApkAsSharedLibrary) {
Adam Lesinski0c405242017-01-13 20:47:26 -080040 std::unique_ptr<const ApkAssets> loaded_apk =
Adam Lesinskida431a22016-12-29 16:08:16 -050041 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 Lesinskid1ecd7a2017-01-23 12:58:11 -080057TEST(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
73TEST(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 Lesinski7ad11102016-10-28 16:39:15 -070094} // namespace android