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 "Convert.h" |
| 18 | |
| 19 | #include "LoadedApk.h" |
| 20 | #include "test/Test.h" |
Winson | f54c9a1 | 2019-01-23 12:39:40 -0800 | [diff] [blame^] | 21 | #include "ziparchive/zip_archive.h" |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 22 | |
| 23 | using testing::Eq; |
| 24 | using testing::Ne; |
| 25 | |
| 26 | namespace aapt { |
| 27 | |
| 28 | using ConvertTest = CommandTestFixture; |
| 29 | |
| 30 | TEST_F(ConvertTest, RemoveRawXmlStrings) { |
| 31 | StdErrDiagnostics diag; |
| 32 | const std::string compiled_files_dir = GetTestPath("compiled"); |
| 33 | ASSERT_TRUE(CompileFile(GetTestPath("res/xml/test.xml"), R"(<Item AgentCode="007"/>)", |
| 34 | compiled_files_dir, &diag)); |
| 35 | |
| 36 | const std::string out_apk = GetTestPath("out.apk"); |
| 37 | std::vector<std::string> link_args = { |
| 38 | "--manifest", GetDefaultManifest(), |
| 39 | "-o", out_apk, |
| 40 | "--keep-raw-values", |
| 41 | "--proto-format" |
| 42 | }; |
| 43 | |
| 44 | ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag)); |
| 45 | |
| 46 | const std::string out_convert_apk = GetTestPath("out_convert.apk"); |
| 47 | std::vector<android::StringPiece> convert_args = { |
| 48 | "-o", out_convert_apk, |
| 49 | "--output-format", "binary", |
| 50 | out_apk, |
| 51 | }; |
| 52 | ASSERT_THAT(ConvertCommand().Execute(convert_args, &std::cerr), Eq(0)); |
| 53 | |
| 54 | // Load the binary xml tree |
| 55 | android::ResXMLTree tree; |
| 56 | std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_convert_apk, &diag); |
| 57 | AssertLoadXml(apk.get(), "res/xml/test.xml", &tree); |
| 58 | |
| 59 | // Check that the raw string index has not been assigned |
| 60 | EXPECT_THAT(tree.getAttributeValueStringID(0), Eq(-1)); |
| 61 | } |
| 62 | |
| 63 | TEST_F(ConvertTest, KeepRawXmlStrings) { |
| 64 | StdErrDiagnostics diag; |
| 65 | const std::string compiled_files_dir = GetTestPath("compiled"); |
| 66 | ASSERT_TRUE(CompileFile(GetTestPath("res/xml/test.xml"), R"(<Item AgentCode="007"/>)", |
| 67 | compiled_files_dir, &diag)); |
| 68 | |
| 69 | const std::string out_apk = GetTestPath("out.apk"); |
| 70 | std::vector<std::string> link_args = { |
| 71 | "--manifest", GetDefaultManifest(), |
| 72 | "-o", out_apk, |
| 73 | "--keep-raw-values", |
| 74 | "--proto-format" |
| 75 | }; |
| 76 | |
| 77 | ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag)); |
| 78 | |
| 79 | const std::string out_convert_apk = GetTestPath("out_convert.apk"); |
| 80 | std::vector<android::StringPiece> convert_args = { |
| 81 | "-o", out_convert_apk, |
| 82 | "--output-format", "binary", |
| 83 | "--keep-raw-values", |
| 84 | out_apk, |
| 85 | }; |
| 86 | ASSERT_THAT(ConvertCommand().Execute(convert_args, &std::cerr), Eq(0)); |
| 87 | |
| 88 | // Load the binary xml tree |
| 89 | android::ResXMLTree tree; |
| 90 | std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_convert_apk, &diag); |
| 91 | AssertLoadXml(apk.get(), "res/xml/test.xml", &tree); |
| 92 | |
| 93 | // Check that the raw string index has been set to the correct string pool entry |
| 94 | int32_t raw_index = tree.getAttributeValueStringID(0); |
| 95 | ASSERT_THAT(raw_index, Ne(-1)); |
| 96 | EXPECT_THAT(util::GetString(tree.getStrings(), static_cast<size_t>(raw_index)), Eq("007")); |
| 97 | } |
| 98 | |
Winson | f54c9a1 | 2019-01-23 12:39:40 -0800 | [diff] [blame^] | 99 | TEST_F(ConvertTest, DuplicateEntriesWrittenOnce) { |
| 100 | StdErrDiagnostics diag; |
| 101 | const std::string apk_path = |
| 102 | file::BuildPath({android::base::GetExecutableDirectory(), |
| 103 | "integration-tests", "ConvertTest", "duplicate_entries.apk"}); |
| 104 | |
| 105 | const std::string out_convert_apk = GetTestPath("out_convert.apk"); |
| 106 | std::vector<android::StringPiece> convert_args = { |
| 107 | "-o", out_convert_apk, |
| 108 | "--output-format", "proto", |
| 109 | apk_path |
| 110 | }; |
| 111 | ASSERT_THAT(ConvertCommand().Execute(convert_args, &std::cerr), Eq(0)); |
| 112 | |
| 113 | ZipArchiveHandle handle; |
| 114 | ASSERT_THAT(OpenArchive(out_convert_apk.c_str(), &handle), Eq(0)); |
| 115 | |
| 116 | void* cookie = nullptr; |
| 117 | |
| 118 | ZipString prefix("res/theme/10"); |
| 119 | int32_t result = StartIteration(handle, &cookie, &prefix, nullptr); |
| 120 | |
| 121 | // If this is -5, that means we've found a duplicate entry and this test has failed |
| 122 | EXPECT_THAT(result, Eq(0)); |
| 123 | |
| 124 | // But if read succeeds, verify only one res/theme/10 entry |
| 125 | int count = 0; |
| 126 | |
| 127 | // Can't pass nullptrs into Next() |
| 128 | ZipString zip_name; |
| 129 | ZipEntry zip_data; |
| 130 | |
| 131 | while ((result = Next(cookie, &zip_data, &zip_name)) == 0) { |
| 132 | count++; |
| 133 | } |
| 134 | |
| 135 | EndIteration(cookie); |
| 136 | |
| 137 | EXPECT_THAT(count, Eq(1)); |
| 138 | } |
| 139 | |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 140 | } // namespace aapt |