blob: 3ebb08eb791e0cf203215cb711960f447ce83673 [file] [log] [blame]
Adam Lesinski59e04c62016-02-04 15:59:23 -08001/*
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 Lesinski59e04c62016-02-04 15:59:23 -080017#include "proto/ProtoSerialize.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include "ResourceTable.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070020#include "test/Test.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080021
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022using ::google::protobuf::io::StringOutputStream;
Adam Lesinskia45893a2017-05-30 15:19:02 -070023using ::testing::NotNull;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070024
Adam Lesinski59e04c62016-02-04 15:59:23 -080025namespace aapt {
26
27TEST(TableProtoSerializer, SerializeSinglePackage) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
29 std::unique_ptr<ResourceTable> table =
30 test::ResourceTableBuilder()
31 .SetPackageId("com.app.a", 0x7f)
Adam Lesinski4488f1c2017-05-26 17:33:38 -070032 .AddFileReference("com.app.a:layout/main", ResourceId(0x7f020000), "res/layout/main.xml")
33 .AddReference("com.app.a:layout/other", ResourceId(0x7f020001), "com.app.a:layout/main")
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 .AddString("com.app.a:string/text", {}, "hi")
35 .AddValue("com.app.a:id/foo", {}, util::make_unique<Id>())
Adam Lesinski4488f1c2017-05-26 17:33:38 -070036 .SetSymbolState("com.app.a:bool/foo", {}, SymbolState::kUndefined, true /*allow_new*/)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 .Build();
Adam Lesinski59e04c62016-02-04 15:59:23 -080038
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 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 Lesinski59e04c62016-02-04 15:59:23 -080044
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 Id* id = test::GetValue<Id>(table.get(), "com.app.a:id/foo");
46 ASSERT_NE(nullptr, id);
Adam Lesinski59e04c62016-02-04 15:59:23 -080047
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 // Make a plural.
49 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinskia45893a2017-05-30 15:19:02 -070050 plural->values[Plural::One] = util::make_unique<String>(table->string_pool.MakeRef("one"));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 ASSERT_TRUE(table->AddResource(test::ParseNameOrDie("com.app.a:plurals/hey"),
52 ConfigDescription{}, {}, std::move(plural),
53 context->GetDiagnostics()));
Adam Lesinski59e04c62016-02-04 15:59:23 -080054
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 // Make a resource with different products.
56 ASSERT_TRUE(table->AddResource(
57 test::ParseNameOrDie("com.app.a:integer/one"),
58 test::ParseConfigOrDie("land"), {},
59 test::BuildPrimitive(android::Res_value::TYPE_INT_DEC, 123u),
60 context->GetDiagnostics()));
61 ASSERT_TRUE(table->AddResource(
62 test::ParseNameOrDie("com.app.a:integer/one"),
63 test::ParseConfigOrDie("land"), "tablet",
64 test::BuildPrimitive(android::Res_value::TYPE_INT_DEC, 321u),
65 context->GetDiagnostics()));
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080066
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 // Make a reference with both resource name and resource ID.
68 // The reference should point to a resource outside of this table to test that
69 // both
70 // name and id get serialized.
71 Reference expected_ref;
72 expected_ref.name = test::ParseNameOrDie("android:layout/main");
73 expected_ref.id = ResourceId(0x01020000);
74 ASSERT_TRUE(table->AddResource(test::ParseNameOrDie("com.app.a:layout/abc"),
75 ConfigDescription::DefaultConfig(), {},
76 util::make_unique<Reference>(expected_ref),
77 context->GetDiagnostics()));
Adam Lesinski64587af2016-02-18 18:33:06 -080078
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(table.get());
Adam Lesinskia45893a2017-05-30 15:19:02 -070080 ASSERT_THAT(pb_table, NotNull());
Adam Lesinski59e04c62016-02-04 15:59:23 -080081
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 std::unique_ptr<ResourceTable> new_table = DeserializeTableFromPb(
83 *pb_table, Source{"test"}, context->GetDiagnostics());
Adam Lesinskia45893a2017-05-30 15:19:02 -070084 ASSERT_THAT(new_table, NotNull());
Adam Lesinski59e04c62016-02-04 15:59:23 -080085
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 Id* new_id = test::GetValue<Id>(new_table.get(), "com.app.a:id/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -070087 ASSERT_THAT(new_id, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 EXPECT_EQ(id->IsWeak(), new_id->IsWeak());
Adam Lesinski59e04c62016-02-04 15:59:23 -080089
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 Maybe<ResourceTable::SearchResult> result =
91 new_table->FindResource(test::ParseNameOrDie("com.app.a:layout/main"));
Adam Lesinskia45893a2017-05-30 15:19:02 -070092 ASSERT_TRUE(result);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 EXPECT_EQ(SymbolState::kPublic, result.value().type->symbol_status.state);
94 EXPECT_EQ(SymbolState::kPublic, result.value().entry->symbol_status.state);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080095
Adam Lesinski4488f1c2017-05-26 17:33:38 -070096 result = new_table->FindResource(test::ParseNameOrDie("com.app.a:bool/foo"));
97 ASSERT_TRUE(result);
98 EXPECT_EQ(SymbolState::kUndefined, result.value().entry->symbol_status.state);
99 EXPECT_TRUE(result.value().entry->symbol_status.allow_new);
100
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 // Find the product-dependent values
102 BinaryPrimitive* prim = test::GetValueForConfigAndProduct<BinaryPrimitive>(
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700103 new_table.get(), "com.app.a:integer/one", test::ParseConfigOrDie("land"), "");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700104 ASSERT_THAT(prim, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 EXPECT_EQ(123u, prim->value.data);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800106
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 prim = test::GetValueForConfigAndProduct<BinaryPrimitive>(
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700108 new_table.get(), "com.app.a:integer/one", test::ParseConfigOrDie("land"), "tablet");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700109 ASSERT_THAT(prim, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 EXPECT_EQ(321u, prim->value.data);
Adam Lesinski64587af2016-02-18 18:33:06 -0800111
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700112 Reference* actual_ref = test::GetValue<Reference>(new_table.get(), "com.app.a:layout/abc");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700113 ASSERT_THAT(actual_ref, NotNull());
114 ASSERT_TRUE(actual_ref->name);
115 ASSERT_TRUE(actual_ref->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 EXPECT_EQ(expected_ref.name.value(), actual_ref->name.value());
117 EXPECT_EQ(expected_ref.id.value(), actual_ref->id.value());
Adam Lesinski59e04c62016-02-04 15:59:23 -0800118}
119
120TEST(TableProtoSerializer, SerializeFileHeader) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Adam Lesinski59e04c62016-02-04 15:59:23 -0800122
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 ResourceFile f;
124 f.config = test::ParseConfigOrDie("hdpi-v9");
125 f.name = test::ParseNameOrDie("com.app.a:layout/main");
126 f.source.path = "res/layout-hdpi-v9/main.xml";
127 f.exported_symbols.push_back(
128 SourcedResourceName{test::ParseNameOrDie("id/unchecked"), 23u});
Adam Lesinski59e04c62016-02-04 15:59:23 -0800129
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 const std::string expected_data1 = "123";
131 const std::string expected_data2 = "1234";
Adam Lesinski59e04c62016-02-04 15:59:23 -0800132
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 std::string output_str;
134 {
135 std::unique_ptr<pb::CompiledFile> pb_file1 = SerializeCompiledFileToPb(f);
Adam Lesinski59e04c62016-02-04 15:59:23 -0800136
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 f.name.entry = "__" + f.name.entry + "$0";
138 std::unique_ptr<pb::CompiledFile> pb_file2 = SerializeCompiledFileToPb(f);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700139
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 StringOutputStream out_stream(&output_str);
141 CompiledFileOutputStream out_file_stream(&out_stream);
142 out_file_stream.WriteLittleEndian32(2);
143 out_file_stream.WriteCompiledFile(pb_file1.get());
144 out_file_stream.WriteData(expected_data1.data(), expected_data1.size());
145 out_file_stream.WriteCompiledFile(pb_file2.get());
146 out_file_stream.WriteData(expected_data2.data(), expected_data2.size());
147 ASSERT_FALSE(out_file_stream.HadError());
148 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800149
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 CompiledFileInputStream in_file_stream(output_str.data(), output_str.size());
151 uint32_t num_files = 0;
152 ASSERT_TRUE(in_file_stream.ReadLittleEndian32(&num_files));
153 ASSERT_EQ(2u, num_files);
Adam Lesinski59e04c62016-02-04 15:59:23 -0800154
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 // Read the first compiled file.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700156
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 pb::CompiledFile new_pb_file;
158 ASSERT_TRUE(in_file_stream.ReadCompiledFile(&new_pb_file));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700159
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 std::unique_ptr<ResourceFile> file = DeserializeCompiledFileFromPb(
161 new_pb_file, Source("test"), context->GetDiagnostics());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700162 ASSERT_THAT(file, NotNull());
Adam Lesinski59e04c62016-02-04 15:59:23 -0800163
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 uint64_t offset, len;
165 ASSERT_TRUE(in_file_stream.ReadDataMetaData(&offset, &len));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700166
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 std::string actual_data(output_str.data() + offset, len);
168 EXPECT_EQ(expected_data1, actual_data);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700169
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 // Expect the data to be aligned.
171 EXPECT_EQ(0u, offset & 0x03);
Adam Lesinski59e04c62016-02-04 15:59:23 -0800172
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 ASSERT_EQ(1u, file->exported_symbols.size());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700174 EXPECT_EQ(test::ParseNameOrDie("id/unchecked"), file->exported_symbols[0].name);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700175
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 // Read the second compiled file.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700177
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 ASSERT_TRUE(in_file_stream.ReadCompiledFile(&new_pb_file));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700179
Adam Lesinskia45893a2017-05-30 15:19:02 -0700180 file = DeserializeCompiledFileFromPb(new_pb_file, Source("test"), context->GetDiagnostics());
181 ASSERT_THAT(file, NotNull());
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700182
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183 ASSERT_TRUE(in_file_stream.ReadDataMetaData(&offset, &len));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700184
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185 actual_data = std::string(output_str.data() + offset, len);
186 EXPECT_EQ(expected_data2, actual_data);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700187
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188 // Expect the data to be aligned.
189 EXPECT_EQ(0u, offset & 0x03);
Adam Lesinski59e04c62016-02-04 15:59:23 -0800190}
191
Adam Lesinski64587af2016-02-18 18:33:06 -0800192TEST(TableProtoSerializer, DeserializeCorruptHeaderSafely) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 ResourceFile f;
194 std::unique_ptr<pb::CompiledFile> pb_file = SerializeCompiledFileToPb(f);
Adam Lesinski64587af2016-02-18 18:33:06 -0800195
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196 const std::string expected_data = "1234";
Adam Lesinski64587af2016-02-18 18:33:06 -0800197
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700198 std::string output_str;
199 {
200 StringOutputStream out_stream(&output_str);
201 CompiledFileOutputStream out_file_stream(&out_stream);
202 out_file_stream.WriteLittleEndian32(1);
203 out_file_stream.WriteCompiledFile(pb_file.get());
204 out_file_stream.WriteData(expected_data.data(), expected_data.size());
205 ASSERT_FALSE(out_file_stream.HadError());
206 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800207
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700208 output_str[4] = 0xff;
Adam Lesinski64587af2016-02-18 18:33:06 -0800209
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210 CompiledFileInputStream in_file_stream(output_str.data(), output_str.size());
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700211
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700212 uint32_t num_files = 0;
213 EXPECT_TRUE(in_file_stream.ReadLittleEndian32(&num_files));
214 EXPECT_EQ(1u, num_files);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700215
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216 pb::CompiledFile new_pb_file;
217 EXPECT_FALSE(in_file_stream.ReadCompiledFile(&new_pb_file));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700218
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219 uint64_t offset, len;
220 EXPECT_FALSE(in_file_stream.ReadDataMetaData(&offset, &len));
Adam Lesinski64587af2016-02-18 18:33:06 -0800221}
222
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223} // namespace aapt