blob: e576709a776dfdbb4e578b8841cf92597b69cc11 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
2 * Copyright (C) 2015 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 "process/SymbolTable.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
Adam Lesinskidc785052017-12-07 15:58:46 -080019#include "SdkConstants.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000020#include "androidfw/BigBuffer.h"
Adam Lesinskidc785052017-12-07 15:58:46 -080021#include "format/binary/TableFlattener.h"
Adam Lesinski64587af2016-02-18 18:33:06 -080022#include "test/Test.h"
Adam Lesinskidc785052017-12-07 15:58:46 -080023
24using ::testing::Eq;
25using ::testing::IsNull;
26using ::testing::Ne;
27using ::testing::NotNull;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028
29namespace aapt {
30
Adam Lesinski64587af2016-02-18 18:33:06 -080031TEST(ResourceTableSymbolSourceTest, FindSymbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 std::unique_ptr<ResourceTable> table =
33 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 .AddSimple("android:id/foo", ResourceId(0x01020000))
35 .AddSimple("android:id/bar")
36 .AddValue("android:attr/foo", ResourceId(0x01010000),
37 test::AttributeBuilder().Build())
38 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 ResourceTableSymbolSource symbol_source(table.get());
Adam Lesinskidc785052017-12-07 15:58:46 -080041 EXPECT_THAT(symbol_source.FindByName(test::ParseNameOrDie("android:id/foo")), NotNull());
42 EXPECT_THAT(symbol_source.FindByName(test::ParseNameOrDie("android:id/bar")), NotNull());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070043
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 std::unique_ptr<SymbolTable::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 symbol_source.FindByName(test::ParseNameOrDie("android:attr/foo"));
Adam Lesinskidc785052017-12-07 15:58:46 -080046 ASSERT_THAT(s, NotNull());
47 EXPECT_THAT(s->attribute, NotNull());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070048}
49
Adam Lesinski64587af2016-02-18 18:33:06 -080050TEST(ResourceTableSymbolSourceTest, FindPrivateAttrSymbol) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 std::unique_ptr<ResourceTable> table =
52 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 .AddValue("android:^attr-private/foo", ResourceId(0x01010000),
54 test::AttributeBuilder().Build())
55 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 ResourceTableSymbolSource symbol_source(table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 std::unique_ptr<SymbolTable::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 symbol_source.FindByName(test::ParseNameOrDie("android:attr/foo"));
Adam Lesinskidc785052017-12-07 15:58:46 -080060 ASSERT_THAT(s, NotNull());
61 EXPECT_THAT(s->attribute, NotNull());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070062}
63
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080064TEST(SymbolTableTest, FindByName) {
65 std::unique_ptr<ResourceTable> table =
66 test::ResourceTableBuilder()
67 .AddSimple("com.android.app:id/foo")
68 .AddSimple("com.android.app:id/" + NameMangler::MangleEntry("com.android.lib", "foo"))
69 .Build();
70
71 NameMangler mangler(NameManglerPolicy{"com.android.app", {"com.android.lib"}});
72 SymbolTable symbol_table(&mangler);
73 symbol_table.AppendSource(util::make_unique<ResourceTableSymbolSource>(table.get()));
74
Adam Lesinskidc785052017-12-07 15:58:46 -080075 EXPECT_THAT(symbol_table.FindByName(test::ParseNameOrDie("id/foo")), NotNull());
76 EXPECT_THAT(symbol_table.FindByName(test::ParseNameOrDie("com.android.lib:id/foo")), NotNull());
77}
78
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080079using SymbolTableTestFixture = CommandTestFixture;
80TEST_F(SymbolTableTestFixture, FindByNameWhenSymbolIsMangledInResTable) {
Adam Lesinskidc785052017-12-07 15:58:46 -080081 using namespace android;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080082 StdErrDiagnostics diag;
Adam Lesinskidc785052017-12-07 15:58:46 -080083
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080084 // Create a static library.
85 const std::string static_lib_compiled_files_dir = GetTestPath("static-lib-compiled");
86 ASSERT_TRUE(CompileFile(GetTestPath("res/values/values.xml"),
87 R"(<?xml version="1.0" encoding="utf-8"?>
88 <resources>
89 <item type="id" name="foo"/>
90 </resources>)",
91 static_lib_compiled_files_dir, &diag));
Adam Lesinskidc785052017-12-07 15:58:46 -080092
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080093 const std::string static_lib_apk = GetTestPath("static_lib.apk");
94 std::vector<std::string> link_args = {
95 "--manifest", GetDefaultManifest("com.android.lib"),
96 "--min-sdk-version", "22",
97 "--static-lib",
98 "-o", static_lib_apk,
99 };
100 ASSERT_TRUE(Link(link_args, static_lib_compiled_files_dir, &diag));
Adam Lesinskidc785052017-12-07 15:58:46 -0800101
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800102 // Merge the static library into the main application package. The static library resources will
103 // be mangled with the library package name.
104 const std::string app_compiled_files_dir = GetTestPath("app-compiled");
105 ASSERT_TRUE(CompileFile(GetTestPath("res/values/values.xml"),
106 R"(<?xml version="1.0" encoding="utf-8"?>
107 <resources>
108 <item type="id" name="bar"/>
109 </resources>)",
110 app_compiled_files_dir, &diag));
Adam Lesinskidc785052017-12-07 15:58:46 -0800111
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800112 const std::string out_apk = GetTestPath("out.apk");
113 link_args = {
114 "--manifest", GetDefaultManifest("com.android.app"),
115 "--min-sdk-version", "22",
116 "-o", out_apk,
117 static_lib_apk
118 };
119 ASSERT_TRUE(Link(link_args, app_compiled_files_dir, &diag));
Adam Lesinskidc785052017-12-07 15:58:46 -0800120
121 // Construct the test AssetManager.
122 auto asset_manager_source = util::make_unique<AssetManagerSymbolSource>();
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800123 asset_manager_source->AddAssetPath(out_apk);
Adam Lesinskidc785052017-12-07 15:58:46 -0800124
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800125 NameMangler name_mangler(NameManglerPolicy{"com.android.app"});
126 SymbolTable symbol_table(&name_mangler);
Adam Lesinskidc785052017-12-07 15:58:46 -0800127 symbol_table.AppendSource(std::move(asset_manager_source));
128
129 EXPECT_THAT(symbol_table.FindByName(test::ParseNameOrDie("com.android.lib:id/foo")), NotNull());
130 EXPECT_THAT(symbol_table.FindByName(test::ParseNameOrDie("com.android.app:id/bar")), NotNull());
131
132 EXPECT_THAT(symbol_table.FindByName(test::ParseNameOrDie("com.android.app:id/foo")), IsNull());
133 EXPECT_THAT(symbol_table.FindByName(test::ParseNameOrDie("com.android.lib:id/bar")), IsNull());
134 EXPECT_THAT(symbol_table.FindByName(test::ParseNameOrDie("com.android.other:id/foo")), IsNull());
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800135}
136
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137} // namespace aapt