blob: e6ce6d37b8798bd24547f418ae4301752ad59e6b [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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -070022using ::google::protobuf::io::StringOutputStream;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070023
Adam Lesinski59e04c62016-02-04 15:59:23 -080024namespace aapt {
25
26TEST(TableProtoSerializer, SerializeSinglePackage) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -070027 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
28 std::unique_ptr<ResourceTable> table =
29 test::ResourceTableBuilder()
30 .SetPackageId("com.app.a", 0x7f)
Adam Lesinski4488f1c2017-05-26 17:33:38 -070031 .AddFileReference("com.app.a:layout/main", ResourceId(0x7f020000), "res/layout/main.xml")
32 .AddReference("com.app.a:layout/other", ResourceId(0x7f020001), "com.app.a:layout/main")
Adam Lesinskice5e56e22016-10-21 17:56:45 -070033 .AddString("com.app.a:string/text", {}, "hi")
34 .AddValue("com.app.a:id/foo", {}, util::make_unique<Id>())
Adam Lesinski4488f1c2017-05-26 17:33:38 -070035 .SetSymbolState("com.app.a:bool/foo", {}, SymbolState::kUndefined, true /*allow_new*/)
Adam Lesinskice5e56e22016-10-21 17:56:45 -070036 .Build();
Adam Lesinski59e04c62016-02-04 15:59:23 -080037
Adam Lesinskice5e56e22016-10-21 17:56:45 -070038 Symbol public_symbol;
39 public_symbol.state = SymbolState::kPublic;
40 ASSERT_TRUE(table->SetSymbolState(
41 test::ParseNameOrDie("com.app.a:layout/main"), ResourceId(0x7f020000),
42 public_symbol, context->GetDiagnostics()));
Adam Lesinski59e04c62016-02-04 15:59:23 -080043
Adam Lesinskice5e56e22016-10-21 17:56:45 -070044 Id* id = test::GetValue<Id>(table.get(), "com.app.a:id/foo");
45 ASSERT_NE(nullptr, id);
Adam Lesinski59e04c62016-02-04 15:59:23 -080046
Adam Lesinskice5e56e22016-10-21 17:56:45 -070047 // Make a plural.
48 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
49 plural->values[Plural::One] =
50 util::make_unique<String>(table->string_pool.MakeRef("one"));
51 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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -070079 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(table.get());
80 ASSERT_NE(nullptr, pb_table);
Adam Lesinski59e04c62016-02-04 15:59:23 -080081
Adam Lesinskice5e56e22016-10-21 17:56:45 -070082 std::unique_ptr<ResourceTable> new_table = DeserializeTableFromPb(
83 *pb_table, Source{"test"}, context->GetDiagnostics());
84 ASSERT_NE(nullptr, new_table);
Adam Lesinski59e04c62016-02-04 15:59:23 -080085
Adam Lesinskice5e56e22016-10-21 17:56:45 -070086 Id* new_id = test::GetValue<Id>(new_table.get(), "com.app.a:id/foo");
87 ASSERT_NE(nullptr, new_id);
88 EXPECT_EQ(id->IsWeak(), new_id->IsWeak());
Adam Lesinski59e04c62016-02-04 15:59:23 -080089
Adam Lesinskice5e56e22016-10-21 17:56:45 -070090 Maybe<ResourceTable::SearchResult> result =
91 new_table->FindResource(test::ParseNameOrDie("com.app.a:layout/main"));
92 AAPT_ASSERT_TRUE(result);
93 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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -0700104 ASSERT_NE(nullptr, prim);
105 EXPECT_EQ(123u, prim->value.data);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800106
Adam Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -0700109 ASSERT_NE(nullptr, prim);
110 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 Lesinskice5e56e22016-10-21 17:56:45 -0700113 ASSERT_NE(nullptr, actual_ref);
114 AAPT_ASSERT_TRUE(actual_ref->name);
115 AAPT_ASSERT_TRUE(actual_ref->id);
116 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 Lesinskice5e56e22016-10-21 17:56:45 -0700121 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Adam Lesinski59e04c62016-02-04 15:59:23 -0800122
Adam Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -0700155 // Read the first compiled file.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700156
Adam Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -0700160 std::unique_ptr<ResourceFile> file = DeserializeCompiledFileFromPb(
161 new_pb_file, Source("test"), context->GetDiagnostics());
162 ASSERT_NE(nullptr, file);
Adam Lesinski59e04c62016-02-04 15:59:23 -0800163
Adam Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -0700173 ASSERT_EQ(1u, file->exported_symbols.size());
174 EXPECT_EQ(test::ParseNameOrDie("id/unchecked"),
175 file->exported_symbols[0].name);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700176
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700177 // Read the second compiled file.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700178
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700179 ASSERT_TRUE(in_file_stream.ReadCompiledFile(&new_pb_file));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700180
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700181 file = DeserializeCompiledFileFromPb(new_pb_file, Source("test"),
182 context->GetDiagnostics());
183 ASSERT_NE(nullptr, file);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700184
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700185 ASSERT_TRUE(in_file_stream.ReadDataMetaData(&offset, &len));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700186
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700187 actual_data = std::string(output_str.data() + offset, len);
188 EXPECT_EQ(expected_data2, actual_data);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700189
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700190 // Expect the data to be aligned.
191 EXPECT_EQ(0u, offset & 0x03);
Adam Lesinski59e04c62016-02-04 15:59:23 -0800192}
193
Adam Lesinski64587af2016-02-18 18:33:06 -0800194TEST(TableProtoSerializer, DeserializeCorruptHeaderSafely) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700195 ResourceFile f;
196 std::unique_ptr<pb::CompiledFile> pb_file = SerializeCompiledFileToPb(f);
Adam Lesinski64587af2016-02-18 18:33:06 -0800197
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700198 const std::string expected_data = "1234";
Adam Lesinski64587af2016-02-18 18:33:06 -0800199
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700200 std::string output_str;
201 {
202 StringOutputStream out_stream(&output_str);
203 CompiledFileOutputStream out_file_stream(&out_stream);
204 out_file_stream.WriteLittleEndian32(1);
205 out_file_stream.WriteCompiledFile(pb_file.get());
206 out_file_stream.WriteData(expected_data.data(), expected_data.size());
207 ASSERT_FALSE(out_file_stream.HadError());
208 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800209
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700210 output_str[4] = 0xff;
Adam Lesinski64587af2016-02-18 18:33:06 -0800211
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700212 CompiledFileInputStream in_file_stream(output_str.data(), output_str.size());
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700213
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700214 uint32_t num_files = 0;
215 EXPECT_TRUE(in_file_stream.ReadLittleEndian32(&num_files));
216 EXPECT_EQ(1u, num_files);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700217
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700218 pb::CompiledFile new_pb_file;
219 EXPECT_FALSE(in_file_stream.ReadCompiledFile(&new_pb_file));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700220
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700221 uint64_t offset, len;
222 EXPECT_FALSE(in_file_stream.ReadDataMetaData(&offset, &len));
Adam Lesinski64587af2016-02-18 18:33:06 -0800223}
224
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700225} // namespace aapt