Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 "Link.h" |
| 18 | |
| 19 | #include "LoadedApk.h" |
| 20 | #include "test/Test.h" |
| 21 | |
| 22 | using testing::Eq; |
| 23 | using testing::Ne; |
| 24 | |
| 25 | namespace aapt { |
| 26 | |
| 27 | using LinkTest = CommandTestFixture; |
| 28 | |
| 29 | TEST_F(LinkTest, RemoveRawXmlStrings) { |
| 30 | StdErrDiagnostics diag; |
| 31 | const std::string compiled_files_dir = GetTestPath("compiled"); |
| 32 | ASSERT_TRUE(CompileFile(GetTestPath("res/xml/test.xml"), R"(<Item AgentCode="007"/>)", |
| 33 | compiled_files_dir, &diag)); |
| 34 | |
| 35 | const std::string out_apk = GetTestPath("out.apk"); |
| 36 | std::vector<std::string> link_args = { |
| 37 | "--manifest", GetDefaultManifest(), |
| 38 | "-o", out_apk, |
| 39 | }; |
| 40 | |
| 41 | ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag)); |
| 42 | |
| 43 | // Load the binary xml tree |
| 44 | android::ResXMLTree tree; |
| 45 | std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag); |
Winson | b7be793 | 2019-01-23 11:10:52 -0800 | [diff] [blame] | 46 | std::unique_ptr<io::IData> data = OpenFileAsData(apk.get(), "res/xml/test.xml"); |
| 47 | ASSERT_THAT(data, Ne(nullptr)); |
Winson | b7be793 | 2019-01-23 11:10:52 -0800 | [diff] [blame] | 48 | AssertLoadXml(apk.get(), data.get(), &tree); |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 49 | |
| 50 | // Check that the raw string index has not been assigned |
| 51 | EXPECT_THAT(tree.getAttributeValueStringID(0), Eq(-1)); |
| 52 | } |
| 53 | |
| 54 | TEST_F(LinkTest, KeepRawXmlStrings) { |
| 55 | StdErrDiagnostics diag; |
| 56 | const std::string compiled_files_dir = GetTestPath("compiled"); |
| 57 | ASSERT_TRUE(CompileFile(GetTestPath("res/xml/test.xml"), R"(<Item AgentCode="007"/>)", |
| 58 | compiled_files_dir, &diag)); |
| 59 | |
| 60 | const std::string out_apk = GetTestPath("out.apk"); |
| 61 | std::vector<std::string> link_args = { |
| 62 | "--manifest", GetDefaultManifest(), |
| 63 | "-o", out_apk, |
| 64 | "--keep-raw-values" |
| 65 | }; |
| 66 | |
| 67 | ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag)); |
| 68 | |
| 69 | // Load the binary xml tree |
| 70 | android::ResXMLTree tree; |
| 71 | std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag); |
Winson | b7be793 | 2019-01-23 11:10:52 -0800 | [diff] [blame] | 72 | std::unique_ptr<io::IData> data = OpenFileAsData(apk.get(), "res/xml/test.xml"); |
| 73 | ASSERT_THAT(data, Ne(nullptr)); |
Winson | b7be793 | 2019-01-23 11:10:52 -0800 | [diff] [blame] | 74 | AssertLoadXml(apk.get(), data.get(), &tree); |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 75 | |
| 76 | // Check that the raw string index has been set to the correct string pool entry |
| 77 | int32_t raw_index = tree.getAttributeValueStringID(0); |
| 78 | ASSERT_THAT(raw_index, Ne(-1)); |
| 79 | EXPECT_THAT(util::GetString(tree.getStrings(), static_cast<size_t>(raw_index)), Eq("007")); |
| 80 | } |
| 81 | |
Ryan Mitchell | 81dfca0 | 2019-06-07 10:20:27 -0700 | [diff] [blame] | 82 | TEST_F(LinkTest, NoCompressAssets) { |
| 83 | StdErrDiagnostics diag; |
| 84 | std::string content(500, 'a'); |
| 85 | WriteFile(GetTestPath("assets/testtxt"), content); |
| 86 | WriteFile(GetTestPath("assets/testtxt2"), content); |
| 87 | WriteFile(GetTestPath("assets/test.txt"), content); |
| 88 | WriteFile(GetTestPath("assets/test.hello.txt"), content); |
| 89 | WriteFile(GetTestPath("assets/test.hello.xml"), content); |
| 90 | |
| 91 | const std::string out_apk = GetTestPath("out.apk"); |
| 92 | std::vector<std::string> link_args = { |
| 93 | "--manifest", GetDefaultManifest(), |
| 94 | "-o", out_apk, |
| 95 | "-0", ".txt", |
| 96 | "-0", "txt2", |
| 97 | "-0", ".hello.txt", |
| 98 | "-0", "hello.xml", |
| 99 | "-A", GetTestPath("assets") |
| 100 | }; |
| 101 | |
| 102 | ASSERT_TRUE(Link(link_args, &diag)); |
| 103 | |
| 104 | std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag); |
| 105 | ASSERT_THAT(apk, Ne(nullptr)); |
| 106 | io::IFileCollection* zip = apk->GetFileCollection(); |
| 107 | ASSERT_THAT(zip, Ne(nullptr)); |
| 108 | |
| 109 | auto file = zip->FindFile("assets/testtxt"); |
| 110 | ASSERT_THAT(file, Ne(nullptr)); |
| 111 | EXPECT_TRUE(file->WasCompressed()); |
| 112 | |
| 113 | file = zip->FindFile("assets/testtxt2"); |
| 114 | ASSERT_THAT(file, Ne(nullptr)); |
| 115 | EXPECT_FALSE(file->WasCompressed()); |
| 116 | |
| 117 | file = zip->FindFile("assets/test.txt"); |
| 118 | ASSERT_THAT(file, Ne(nullptr)); |
| 119 | EXPECT_FALSE(file->WasCompressed()); |
| 120 | |
| 121 | file = zip->FindFile("assets/test.hello.txt"); |
| 122 | ASSERT_THAT(file, Ne(nullptr)); |
| 123 | EXPECT_FALSE(file->WasCompressed()); |
| 124 | |
| 125 | file = zip->FindFile("assets/test.hello.xml"); |
| 126 | ASSERT_THAT(file, Ne(nullptr)); |
| 127 | EXPECT_FALSE(file->WasCompressed()); |
| 128 | } |
| 129 | |
| 130 | TEST_F(LinkTest, NoCompressResources) { |
| 131 | StdErrDiagnostics diag; |
| 132 | std::string content(500, 'a'); |
| 133 | const std::string compiled_files_dir = GetTestPath("compiled"); |
| 134 | ASSERT_TRUE(CompileFile(GetTestPath("res/raw/testtxt"), content, compiled_files_dir, &diag)); |
| 135 | ASSERT_TRUE(CompileFile(GetTestPath("res/raw/test.txt"), content, compiled_files_dir, &diag)); |
| 136 | ASSERT_TRUE(CompileFile(GetTestPath("res/raw/test1.hello.txt"), content, compiled_files_dir, |
| 137 | &diag)); |
| 138 | ASSERT_TRUE(CompileFile(GetTestPath("res/raw/test2.goodbye.xml"), content, compiled_files_dir, |
| 139 | &diag)); |
| 140 | |
| 141 | const std::string out_apk = GetTestPath("out.apk"); |
| 142 | std::vector<std::string> link_args = { |
| 143 | "--manifest", GetDefaultManifest(), |
| 144 | "-o", out_apk, |
| 145 | "-0", ".txt", |
| 146 | "-0", ".hello.txt", |
| 147 | "-0", "goodbye.xml", |
| 148 | }; |
| 149 | |
| 150 | ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag)); |
| 151 | |
| 152 | std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag); |
| 153 | ASSERT_THAT(apk, Ne(nullptr)); |
| 154 | io::IFileCollection* zip = apk->GetFileCollection(); |
| 155 | ASSERT_THAT(zip, Ne(nullptr)); |
| 156 | |
| 157 | auto file = zip->FindFile("res/raw/testtxt"); |
| 158 | ASSERT_THAT(file, Ne(nullptr)); |
| 159 | EXPECT_TRUE(file->WasCompressed()); |
| 160 | |
| 161 | file = zip->FindFile("res/raw/test.txt"); |
| 162 | ASSERT_THAT(file, Ne(nullptr)); |
| 163 | EXPECT_FALSE(file->WasCompressed()); |
| 164 | |
| 165 | file = zip->FindFile("res/raw/test1.hello.hello.txt"); |
| 166 | ASSERT_THAT(file, Ne(nullptr)); |
| 167 | EXPECT_FALSE(file->WasCompressed()); |
| 168 | |
| 169 | file = zip->FindFile("res/raw/test2.goodbye.goodbye.xml"); |
| 170 | ASSERT_THAT(file, Ne(nullptr)); |
| 171 | EXPECT_FALSE(file->WasCompressed()); |
| 172 | } |
| 173 | |
Donald Chai | 121c6e8 | 2019-06-12 12:51:57 -0700 | [diff] [blame^] | 174 | TEST_F(LinkTest, OverlayStyles) { |
| 175 | StdErrDiagnostics diag; |
| 176 | const std::string compiled_files_dir = GetTestPath("compiled"); |
| 177 | const std::string override_files_dir = GetTestPath("compiled-override"); |
| 178 | ASSERT_TRUE(CompileFile(GetTestPath("res/values/values.xml"), |
| 179 | R"(<resources> |
| 180 | <style name="MyStyle"> |
| 181 | <item name="android:textColor">#123</item> |
| 182 | </style> |
| 183 | </resources>)", |
| 184 | compiled_files_dir, &diag)); |
| 185 | ASSERT_TRUE(CompileFile(GetTestPath("res/values/values-override.xml"), |
| 186 | R"(<resources> |
| 187 | <style name="MyStyle"> |
| 188 | <item name="android:background">#456</item> |
| 189 | </style> |
| 190 | </resources>)", |
| 191 | override_files_dir, &diag)); |
| 192 | |
| 193 | |
| 194 | const std::string out_apk = GetTestPath("out.apk"); |
| 195 | std::vector<std::string> link_args = { |
| 196 | "--manifest", GetDefaultManifest(kDefaultPackageName), |
| 197 | "-o", out_apk, |
| 198 | }; |
| 199 | const auto override_files = file::FindFiles(override_files_dir, &diag); |
| 200 | for (const auto &override_file : override_files.value()) { |
| 201 | link_args.push_back("-R"); |
| 202 | link_args.push_back(file::BuildPath({override_files_dir, override_file})); |
| 203 | } |
| 204 | ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag)); |
| 205 | |
| 206 | std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag); |
| 207 | const Style* actual_style = test::GetValue<Style>( |
| 208 | apk->GetResourceTable(), std::string(kDefaultPackageName) + ":style/MyStyle"); |
| 209 | ASSERT_NE(actual_style, nullptr); |
| 210 | ASSERT_EQ(actual_style->entries.size(), 2); |
| 211 | EXPECT_EQ(actual_style->entries[0].key.id, 0x01010098); // android:textColor |
| 212 | EXPECT_EQ(actual_style->entries[1].key.id, 0x010100d4); // android:background |
| 213 | } |
| 214 | |
| 215 | TEST_F(LinkTest, OverrideStylesInsteadOfOverlaying) { |
| 216 | StdErrDiagnostics diag; |
| 217 | const std::string compiled_files_dir = GetTestPath("compiled"); |
| 218 | const std::string override_files_dir = GetTestPath("compiled-override"); |
| 219 | ASSERT_TRUE(CompileFile(GetTestPath("res/values/values.xml"), |
| 220 | R"(<resources> |
| 221 | <style name="MyStyle"> |
| 222 | <item name="android:textColor">#123</item> |
| 223 | </style> |
| 224 | </resources>)", |
| 225 | compiled_files_dir, &diag)); |
| 226 | ASSERT_TRUE(CompileFile(GetTestPath("res/values/values-override.xml"), |
| 227 | R"(<resources> |
| 228 | <style name="MyStyle"> |
| 229 | <item name="android:background">#456</item> |
| 230 | </style> |
| 231 | </resources>)", |
| 232 | override_files_dir, &diag)); |
| 233 | |
| 234 | |
| 235 | const std::string out_apk = GetTestPath("out.apk"); |
| 236 | std::vector<std::string> link_args = { |
| 237 | "--manifest", GetDefaultManifest(kDefaultPackageName), |
| 238 | "--override-styles-instead-of-overlaying", |
| 239 | "-o", out_apk, |
| 240 | }; |
| 241 | const auto override_files = file::FindFiles(override_files_dir, &diag); |
| 242 | for (const auto &override_file : override_files.value()) { |
| 243 | link_args.push_back("-R"); |
| 244 | link_args.push_back(file::BuildPath({override_files_dir, override_file})); |
| 245 | } |
| 246 | ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag)); |
| 247 | |
| 248 | std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag); |
| 249 | const Style* actual_style = test::GetValue<Style>( |
| 250 | apk->GetResourceTable(), std::string(kDefaultPackageName) + ":style/MyStyle"); |
| 251 | ASSERT_NE(actual_style, nullptr); |
| 252 | ASSERT_EQ(actual_style->entries.size(), 1); |
| 253 | EXPECT_EQ(actual_style->entries[0].key.id, 0x010100d4); // android:background |
| 254 | } |
| 255 | |
| 256 | } // namespace aapt |