Home | History | Annotate | Download | only in cmd
      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);
     46 
     47   std::unique_ptr<io::IData> data = OpenFileAsData(apk.get(), "res/xml/test.xml");
     48   ASSERT_THAT(data, Ne(nullptr));
     49 
     50   AssertLoadXml(apk.get(), data.get(), &tree);
     51 
     52   // Check that the raw string index has not been assigned
     53   EXPECT_THAT(tree.getAttributeValueStringID(0), Eq(-1));
     54 }
     55 
     56 TEST_F(LinkTest, KeepRawXmlStrings) {
     57   StdErrDiagnostics diag;
     58   const std::string compiled_files_dir = GetTestPath("compiled");
     59   ASSERT_TRUE(CompileFile(GetTestPath("res/xml/test.xml"), R"(<Item AgentCode="007"/>)",
     60                           compiled_files_dir, &diag));
     61 
     62   const std::string out_apk = GetTestPath("out.apk");
     63   std::vector<std::string> link_args = {
     64       "--manifest", GetDefaultManifest(),
     65       "-o", out_apk,
     66       "--keep-raw-values"
     67   };
     68 
     69   ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag));
     70 
     71   // Load the binary xml tree
     72   android::ResXMLTree tree;
     73   std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag);
     74 
     75   std::unique_ptr<io::IData> data = OpenFileAsData(apk.get(), "res/xml/test.xml");
     76   ASSERT_THAT(data, Ne(nullptr));
     77 
     78   AssertLoadXml(apk.get(), data.get(), &tree);
     79 
     80   // Check that the raw string index has been set to the correct string pool entry
     81   int32_t raw_index = tree.getAttributeValueStringID(0);
     82   ASSERT_THAT(raw_index, Ne(-1));
     83   EXPECT_THAT(util::GetString(tree.getStrings(), static_cast<size_t>(raw_index)), Eq("007"));
     84 }
     85 
     86 }  // namespace aapt