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" |
| 21 | |
| 22 | using testing::Eq; |
| 23 | using testing::Ne; |
| 24 | |
| 25 | namespace aapt { |
| 26 | |
| 27 | using ConvertTest = CommandTestFixture; |
| 28 | |
| 29 | TEST_F(ConvertTest, 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 | "--keep-raw-values", |
| 40 | "--proto-format" |
| 41 | }; |
| 42 | |
| 43 | ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag)); |
| 44 | |
| 45 | const std::string out_convert_apk = GetTestPath("out_convert.apk"); |
| 46 | std::vector<android::StringPiece> convert_args = { |
| 47 | "-o", out_convert_apk, |
| 48 | "--output-format", "binary", |
| 49 | out_apk, |
| 50 | }; |
| 51 | ASSERT_THAT(ConvertCommand().Execute(convert_args, &std::cerr), Eq(0)); |
| 52 | |
| 53 | // Load the binary xml tree |
| 54 | android::ResXMLTree tree; |
| 55 | std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_convert_apk, &diag); |
Winson | b7be793 | 2019-01-23 11:10:52 -0800 | [diff] [blame^] | 56 | |
| 57 | std::unique_ptr<io::IData> data = OpenFileAsData(apk.get(), "res/xml/test.xml"); |
| 58 | ASSERT_THAT(data, Ne(nullptr)); |
| 59 | |
| 60 | AssertLoadXml(apk.get(), data.get(), &tree); |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 61 | |
| 62 | // Check that the raw string index has not been assigned |
| 63 | EXPECT_THAT(tree.getAttributeValueStringID(0), Eq(-1)); |
| 64 | } |
| 65 | |
| 66 | TEST_F(ConvertTest, KeepRawXmlStrings) { |
| 67 | StdErrDiagnostics diag; |
| 68 | const std::string compiled_files_dir = GetTestPath("compiled"); |
| 69 | ASSERT_TRUE(CompileFile(GetTestPath("res/xml/test.xml"), R"(<Item AgentCode="007"/>)", |
| 70 | compiled_files_dir, &diag)); |
| 71 | |
| 72 | const std::string out_apk = GetTestPath("out.apk"); |
| 73 | std::vector<std::string> link_args = { |
| 74 | "--manifest", GetDefaultManifest(), |
| 75 | "-o", out_apk, |
| 76 | "--keep-raw-values", |
| 77 | "--proto-format" |
| 78 | }; |
| 79 | |
| 80 | ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag)); |
| 81 | |
| 82 | const std::string out_convert_apk = GetTestPath("out_convert.apk"); |
| 83 | std::vector<android::StringPiece> convert_args = { |
| 84 | "-o", out_convert_apk, |
| 85 | "--output-format", "binary", |
| 86 | "--keep-raw-values", |
| 87 | out_apk, |
| 88 | }; |
| 89 | ASSERT_THAT(ConvertCommand().Execute(convert_args, &std::cerr), Eq(0)); |
| 90 | |
| 91 | // Load the binary xml tree |
| 92 | android::ResXMLTree tree; |
| 93 | std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_convert_apk, &diag); |
Winson | b7be793 | 2019-01-23 11:10:52 -0800 | [diff] [blame^] | 94 | |
| 95 | std::unique_ptr<io::IData> data = OpenFileAsData(apk.get(), "res/xml/test.xml"); |
| 96 | ASSERT_THAT(data, Ne(nullptr)); |
| 97 | |
| 98 | AssertLoadXml(apk.get(), data.get(), &tree); |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 99 | |
| 100 | // Check that the raw string index has been set to the correct string pool entry |
| 101 | int32_t raw_index = tree.getAttributeValueStringID(0); |
| 102 | ASSERT_THAT(raw_index, Ne(-1)); |
| 103 | EXPECT_THAT(util::GetString(tree.getStrings(), static_cast<size_t>(raw_index)), Eq("007")); |
| 104 | } |
| 105 | |
| 106 | } // namespace aapt |