Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 17 | #include "proto/ProtoSerialize.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| 19 | #include "ResourceTable.h" |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 20 | #include "test/Test.h" |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 21 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 22 | using ::google::protobuf::io::StringOutputStream; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 23 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 24 | namespace aapt { |
| 25 | |
| 26 | TEST(TableProtoSerializer, SerializeSinglePackage) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 27 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 28 | std::unique_ptr<ResourceTable> table = |
| 29 | test::ResourceTableBuilder() |
| 30 | .SetPackageId("com.app.a", 0x7f) |
| 31 | .AddFileReference("com.app.a:layout/main", ResourceId(0x7f020000), |
| 32 | "res/layout/main.xml") |
| 33 | .AddReference("com.app.a:layout/other", ResourceId(0x7f020001), |
| 34 | "com.app.a:layout/main") |
| 35 | .AddString("com.app.a:string/text", {}, "hi") |
| 36 | .AddValue("com.app.a:id/foo", {}, util::make_unique<Id>()) |
| 37 | .Build(); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 38 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | Symbol public_symbol; |
| 40 | public_symbol.state = SymbolState::kPublic; |
| 41 | ASSERT_TRUE(table->SetSymbolState( |
| 42 | test::ParseNameOrDie("com.app.a:layout/main"), ResourceId(0x7f020000), |
| 43 | public_symbol, context->GetDiagnostics())); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 44 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 45 | Id* id = test::GetValue<Id>(table.get(), "com.app.a:id/foo"); |
| 46 | ASSERT_NE(nullptr, id); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 47 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 48 | // Make a plural. |
| 49 | std::unique_ptr<Plural> plural = util::make_unique<Plural>(); |
| 50 | plural->values[Plural::One] = |
| 51 | util::make_unique<String>(table->string_pool.MakeRef("one")); |
| 52 | ASSERT_TRUE(table->AddResource(test::ParseNameOrDie("com.app.a:plurals/hey"), |
| 53 | ConfigDescription{}, {}, std::move(plural), |
| 54 | context->GetDiagnostics())); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 55 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 56 | // Make a resource with different products. |
| 57 | ASSERT_TRUE(table->AddResource( |
| 58 | test::ParseNameOrDie("com.app.a:integer/one"), |
| 59 | test::ParseConfigOrDie("land"), {}, |
| 60 | test::BuildPrimitive(android::Res_value::TYPE_INT_DEC, 123u), |
| 61 | context->GetDiagnostics())); |
| 62 | ASSERT_TRUE(table->AddResource( |
| 63 | test::ParseNameOrDie("com.app.a:integer/one"), |
| 64 | test::ParseConfigOrDie("land"), "tablet", |
| 65 | test::BuildPrimitive(android::Res_value::TYPE_INT_DEC, 321u), |
| 66 | context->GetDiagnostics())); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 67 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 68 | // Make a reference with both resource name and resource ID. |
| 69 | // The reference should point to a resource outside of this table to test that |
| 70 | // both |
| 71 | // name and id get serialized. |
| 72 | Reference expected_ref; |
| 73 | expected_ref.name = test::ParseNameOrDie("android:layout/main"); |
| 74 | expected_ref.id = ResourceId(0x01020000); |
| 75 | ASSERT_TRUE(table->AddResource(test::ParseNameOrDie("com.app.a:layout/abc"), |
| 76 | ConfigDescription::DefaultConfig(), {}, |
| 77 | util::make_unique<Reference>(expected_ref), |
| 78 | context->GetDiagnostics())); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 79 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 80 | std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(table.get()); |
| 81 | ASSERT_NE(nullptr, pb_table); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 82 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | std::unique_ptr<ResourceTable> new_table = DeserializeTableFromPb( |
| 84 | *pb_table, Source{"test"}, context->GetDiagnostics()); |
| 85 | ASSERT_NE(nullptr, new_table); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 86 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | Id* new_id = test::GetValue<Id>(new_table.get(), "com.app.a:id/foo"); |
| 88 | ASSERT_NE(nullptr, new_id); |
| 89 | EXPECT_EQ(id->IsWeak(), new_id->IsWeak()); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 90 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 91 | Maybe<ResourceTable::SearchResult> result = |
| 92 | new_table->FindResource(test::ParseNameOrDie("com.app.a:layout/main")); |
| 93 | AAPT_ASSERT_TRUE(result); |
| 94 | EXPECT_EQ(SymbolState::kPublic, result.value().type->symbol_status.state); |
| 95 | EXPECT_EQ(SymbolState::kPublic, result.value().entry->symbol_status.state); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 96 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 97 | // Find the product-dependent values |
| 98 | BinaryPrimitive* prim = test::GetValueForConfigAndProduct<BinaryPrimitive>( |
| 99 | new_table.get(), "com.app.a:integer/one", test::ParseConfigOrDie("land"), |
| 100 | ""); |
| 101 | ASSERT_NE(nullptr, prim); |
| 102 | EXPECT_EQ(123u, prim->value.data); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 103 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 104 | prim = test::GetValueForConfigAndProduct<BinaryPrimitive>( |
| 105 | new_table.get(), "com.app.a:integer/one", test::ParseConfigOrDie("land"), |
| 106 | "tablet"); |
| 107 | ASSERT_NE(nullptr, prim); |
| 108 | EXPECT_EQ(321u, prim->value.data); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 109 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 110 | Reference* actual_ref = |
| 111 | test::GetValue<Reference>(new_table.get(), "com.app.a:layout/abc"); |
| 112 | ASSERT_NE(nullptr, actual_ref); |
| 113 | AAPT_ASSERT_TRUE(actual_ref->name); |
| 114 | AAPT_ASSERT_TRUE(actual_ref->id); |
| 115 | EXPECT_EQ(expected_ref.name.value(), actual_ref->name.value()); |
| 116 | EXPECT_EQ(expected_ref.id.value(), actual_ref->id.value()); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | TEST(TableProtoSerializer, SerializeFileHeader) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 120 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 121 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 122 | ResourceFile f; |
| 123 | f.config = test::ParseConfigOrDie("hdpi-v9"); |
| 124 | f.name = test::ParseNameOrDie("com.app.a:layout/main"); |
| 125 | f.source.path = "res/layout-hdpi-v9/main.xml"; |
| 126 | f.exported_symbols.push_back( |
| 127 | SourcedResourceName{test::ParseNameOrDie("id/unchecked"), 23u}); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 128 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 129 | const std::string expected_data1 = "123"; |
| 130 | const std::string expected_data2 = "1234"; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 131 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 132 | std::string output_str; |
| 133 | { |
| 134 | std::unique_ptr<pb::CompiledFile> pb_file1 = SerializeCompiledFileToPb(f); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 135 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 136 | f.name.entry = "__" + f.name.entry + "$0"; |
| 137 | std::unique_ptr<pb::CompiledFile> pb_file2 = SerializeCompiledFileToPb(f); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 138 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 139 | StringOutputStream out_stream(&output_str); |
| 140 | CompiledFileOutputStream out_file_stream(&out_stream); |
| 141 | out_file_stream.WriteLittleEndian32(2); |
| 142 | out_file_stream.WriteCompiledFile(pb_file1.get()); |
| 143 | out_file_stream.WriteData(expected_data1.data(), expected_data1.size()); |
| 144 | out_file_stream.WriteCompiledFile(pb_file2.get()); |
| 145 | out_file_stream.WriteData(expected_data2.data(), expected_data2.size()); |
| 146 | ASSERT_FALSE(out_file_stream.HadError()); |
| 147 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 148 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 149 | CompiledFileInputStream in_file_stream(output_str.data(), output_str.size()); |
| 150 | uint32_t num_files = 0; |
| 151 | ASSERT_TRUE(in_file_stream.ReadLittleEndian32(&num_files)); |
| 152 | ASSERT_EQ(2u, num_files); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 153 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 154 | // Read the first compiled file. |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 155 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 156 | pb::CompiledFile new_pb_file; |
| 157 | ASSERT_TRUE(in_file_stream.ReadCompiledFile(&new_pb_file)); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 158 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 159 | std::unique_ptr<ResourceFile> file = DeserializeCompiledFileFromPb( |
| 160 | new_pb_file, Source("test"), context->GetDiagnostics()); |
| 161 | ASSERT_NE(nullptr, file); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 162 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 163 | uint64_t offset, len; |
| 164 | ASSERT_TRUE(in_file_stream.ReadDataMetaData(&offset, &len)); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 165 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 166 | std::string actual_data(output_str.data() + offset, len); |
| 167 | EXPECT_EQ(expected_data1, actual_data); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 168 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 169 | // Expect the data to be aligned. |
| 170 | EXPECT_EQ(0u, offset & 0x03); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 171 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 172 | ASSERT_EQ(1u, file->exported_symbols.size()); |
| 173 | EXPECT_EQ(test::ParseNameOrDie("id/unchecked"), |
| 174 | file->exported_symbols[0].name); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 175 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 176 | // Read the second compiled file. |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 177 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 178 | ASSERT_TRUE(in_file_stream.ReadCompiledFile(&new_pb_file)); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 179 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 180 | file = DeserializeCompiledFileFromPb(new_pb_file, Source("test"), |
| 181 | context->GetDiagnostics()); |
| 182 | ASSERT_NE(nullptr, file); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 183 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 184 | ASSERT_TRUE(in_file_stream.ReadDataMetaData(&offset, &len)); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 185 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 186 | actual_data = std::string(output_str.data() + offset, len); |
| 187 | EXPECT_EQ(expected_data2, actual_data); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 188 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 189 | // Expect the data to be aligned. |
| 190 | EXPECT_EQ(0u, offset & 0x03); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 193 | TEST(TableProtoSerializer, DeserializeCorruptHeaderSafely) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 194 | ResourceFile f; |
| 195 | std::unique_ptr<pb::CompiledFile> pb_file = SerializeCompiledFileToPb(f); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 196 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 197 | const std::string expected_data = "1234"; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 198 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 199 | std::string output_str; |
| 200 | { |
| 201 | StringOutputStream out_stream(&output_str); |
| 202 | CompiledFileOutputStream out_file_stream(&out_stream); |
| 203 | out_file_stream.WriteLittleEndian32(1); |
| 204 | out_file_stream.WriteCompiledFile(pb_file.get()); |
| 205 | out_file_stream.WriteData(expected_data.data(), expected_data.size()); |
| 206 | ASSERT_FALSE(out_file_stream.HadError()); |
| 207 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 208 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 209 | output_str[4] = 0xff; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 210 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 211 | CompiledFileInputStream in_file_stream(output_str.data(), output_str.size()); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 212 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 213 | uint32_t num_files = 0; |
| 214 | EXPECT_TRUE(in_file_stream.ReadLittleEndian32(&num_files)); |
| 215 | EXPECT_EQ(1u, num_files); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 216 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 217 | pb::CompiledFile new_pb_file; |
| 218 | EXPECT_FALSE(in_file_stream.ReadCompiledFile(&new_pb_file)); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 219 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 220 | uint64_t offset, len; |
| 221 | EXPECT_FALSE(in_file_stream.ReadDataMetaData(&offset, &len)); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 224 | } // namespace aapt |