Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
Adam Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 19 | #include "SdkConstants.h" |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 20 | #include "androidfw/BigBuffer.h" |
Adam Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 21 | #include "format/binary/TableFlattener.h" |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 22 | #include "test/Test.h" |
Adam Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 23 | |
| 24 | using ::testing::Eq; |
| 25 | using ::testing::IsNull; |
| 26 | using ::testing::Ne; |
| 27 | using ::testing::NotNull; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 28 | |
| 29 | namespace aapt { |
| 30 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 31 | TEST(ResourceTableSymbolSourceTest, FindSymbols) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 32 | std::unique_ptr<ResourceTable> table = |
| 33 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 34 | .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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 39 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 40 | ResourceTableSymbolSource symbol_source(table.get()); |
Adam Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 41 | EXPECT_THAT(symbol_source.FindByName(test::ParseNameOrDie("android:id/foo")), NotNull()); |
| 42 | EXPECT_THAT(symbol_source.FindByName(test::ParseNameOrDie("android:id/bar")), NotNull()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 43 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 44 | std::unique_ptr<SymbolTable::Symbol> s = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 45 | symbol_source.FindByName(test::ParseNameOrDie("android:attr/foo")); |
Adam Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 46 | ASSERT_THAT(s, NotNull()); |
| 47 | EXPECT_THAT(s->attribute, NotNull()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 50 | TEST(ResourceTableSymbolSourceTest, FindPrivateAttrSymbol) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 51 | std::unique_ptr<ResourceTable> table = |
| 52 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 53 | .AddValue("android:^attr-private/foo", ResourceId(0x01010000), |
| 54 | test::AttributeBuilder().Build()) |
| 55 | .Build(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 56 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 57 | ResourceTableSymbolSource symbol_source(table.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 58 | std::unique_ptr<SymbolTable::Symbol> s = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | symbol_source.FindByName(test::ParseNameOrDie("android:attr/foo")); |
Adam Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 60 | ASSERT_THAT(s, NotNull()); |
| 61 | EXPECT_THAT(s->attribute, NotNull()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 64 | TEST(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 Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 75 | 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 Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 79 | using SymbolTableTestFixture = CommandTestFixture; |
| 80 | TEST_F(SymbolTableTestFixture, FindByNameWhenSymbolIsMangledInResTable) { |
Adam Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 81 | using namespace android; |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 82 | StdErrDiagnostics diag; |
Adam Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 83 | |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 84 | // 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 Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 92 | |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 93 | 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 Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 101 | |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 102 | // 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 Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 111 | |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 112 | 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 Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 120 | |
| 121 | // Construct the test AssetManager. |
| 122 | auto asset_manager_source = util::make_unique<AssetManagerSymbolSource>(); |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 123 | asset_manager_source->AddAssetPath(out_apk); |
Adam Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 124 | |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 125 | NameMangler name_mangler(NameManglerPolicy{"com.android.app"}); |
| 126 | SymbolTable symbol_table(&name_mangler); |
Adam Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 127 | 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 Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 137 | } // namespace aapt |