1 /* 2 * Copyright (C) 2017 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 "format/Container.h" 18 19 #include "google/protobuf/io/zero_copy_stream_impl_lite.h" 20 21 #include "io/StringStream.h" 22 #include "test/Test.h" 23 24 using ::google::protobuf::io::StringOutputStream; 25 using ::testing::Eq; 26 using ::testing::IsEmpty; 27 using ::testing::IsNull; 28 using ::testing::NotNull; 29 using ::testing::StrEq; 30 31 namespace aapt { 32 33 TEST(ContainerTest, SerializeCompiledFile) { 34 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); 35 36 const std::string expected_data = "123"; 37 38 std::string output_str; 39 { 40 StringOutputStream out_stream(&output_str); 41 ContainerWriter writer(&out_stream, 2u); 42 ASSERT_FALSE(writer.HadError()); 43 44 pb::internal::CompiledFile pb_compiled_file; 45 pb_compiled_file.set_resource_name("android:layout/main.xml"); 46 pb_compiled_file.set_type(pb::FileReference::PROTO_XML); 47 pb_compiled_file.set_source_path("res/layout/main.xml"); 48 io::StringInputStream data(expected_data); 49 ASSERT_TRUE(writer.AddResFileEntry(pb_compiled_file, &data)); 50 51 pb::ResourceTable pb_table; 52 pb::Package* pb_pkg = pb_table.add_package(); 53 pb_pkg->set_package_name("android"); 54 pb_pkg->mutable_package_id()->set_id(0x01u); 55 ASSERT_TRUE(writer.AddResTableEntry(pb_table)); 56 57 ASSERT_FALSE(writer.HadError()); 58 } 59 60 io::StringInputStream input(output_str); 61 ContainerReader reader(&input); 62 ASSERT_FALSE(reader.HadError()); 63 64 ContainerReaderEntry* entry = reader.Next(); 65 ASSERT_THAT(entry, NotNull()); 66 ASSERT_THAT(entry->Type(), Eq(ContainerEntryType::kResFile)); 67 68 pb::internal::CompiledFile pb_new_file; 69 off64_t offset; 70 size_t len; 71 ASSERT_TRUE(entry->GetResFileOffsets(&pb_new_file, &offset, &len)) << entry->GetError(); 72 EXPECT_THAT(offset & 0x03, Eq(0u)); 73 EXPECT_THAT(output_str.substr(static_cast<size_t>(offset), len), StrEq(expected_data)); 74 75 entry = reader.Next(); 76 ASSERT_THAT(entry, NotNull()); 77 ASSERT_THAT(entry->Type(), Eq(ContainerEntryType::kResTable)); 78 79 pb::ResourceTable pb_new_table; 80 ASSERT_TRUE(entry->GetResTable(&pb_new_table)); 81 ASSERT_THAT(pb_new_table.package_size(), Eq(1)); 82 EXPECT_THAT(pb_new_table.package(0).package_name(), StrEq("android")); 83 EXPECT_THAT(pb_new_table.package(0).package_id().id(), Eq(0x01u)); 84 85 EXPECT_THAT(reader.Next(), IsNull()); 86 EXPECT_FALSE(reader.HadError()); 87 EXPECT_THAT(reader.GetError(), IsEmpty()); 88 } 89 90 } // namespace aapt 91