OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 #include <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 EXPECT_EQ(10, pod_clone->get_f_int8()); | 115 EXPECT_EQ(10, pod_clone->get_f_int8()); |
116 EXPECT_TRUE(pod_clone->is_f_int8()); | 116 EXPECT_TRUE(pod_clone->is_f_int8()); |
117 EXPECT_EQ(pod_clone->which(), PodUnion::Tag::F_INT8); | 117 EXPECT_EQ(pod_clone->which(), PodUnion::Tag::F_INT8); |
118 } | 118 } |
119 | 119 |
120 TEST(UnionTest, PodSerialization) { | 120 TEST(UnionTest, PodSerialization) { |
121 PodUnionPtr pod1(PodUnion::New()); | 121 PodUnionPtr pod1(PodUnion::New()); |
122 pod1->set_f_int8(10); | 122 pod1->set_f_int8(10); |
123 | 123 |
124 mojo::internal::SerializationContext context; | 124 mojo::internal::SerializationContext context; |
125 size_t size = GetSerializedSize_(pod1, false, &context); | 125 size_t size = |
| 126 mojo::internal::PrepareToSerialize<PodUnionPtr>(pod1, false, &context); |
126 EXPECT_EQ(16U, size); | 127 EXPECT_EQ(16U, size); |
127 | 128 |
128 mojo::internal::FixedBufferForTesting buf(size); | 129 mojo::internal::FixedBufferForTesting buf(size); |
129 internal::PodUnion_Data* data = nullptr; | 130 internal::PodUnion_Data* data = nullptr; |
130 SerializeUnion_(std::move(pod1), &buf, &data, false, &context); | 131 mojo::internal::Serialize<PodUnionPtr>(pod1, &buf, &data, false, &context); |
131 | 132 |
132 PodUnionPtr pod2; | 133 PodUnionPtr pod2; |
133 Deserialize_(data, &pod2, &context); | 134 mojo::internal::Deserialize<PodUnionPtr>(data, &pod2, &context); |
134 | 135 |
135 EXPECT_EQ(10, pod2->get_f_int8()); | 136 EXPECT_EQ(10, pod2->get_f_int8()); |
136 EXPECT_TRUE(pod2->is_f_int8()); | 137 EXPECT_TRUE(pod2->is_f_int8()); |
137 EXPECT_EQ(pod2->which(), PodUnion::Tag::F_INT8); | 138 EXPECT_EQ(pod2->which(), PodUnion::Tag::F_INT8); |
138 } | 139 } |
139 | 140 |
140 TEST(UnionTest, EnumSerialization) { | 141 TEST(UnionTest, EnumSerialization) { |
141 PodUnionPtr pod1(PodUnion::New()); | 142 PodUnionPtr pod1(PodUnion::New()); |
142 pod1->set_f_enum(AnEnum::SECOND); | 143 pod1->set_f_enum(AnEnum::SECOND); |
143 | 144 |
144 size_t size = GetSerializedSize_(pod1, false, nullptr); | 145 size_t size = |
| 146 mojo::internal::PrepareToSerialize<PodUnionPtr>(pod1, false, nullptr); |
145 EXPECT_EQ(16U, size); | 147 EXPECT_EQ(16U, size); |
146 | 148 |
147 mojo::internal::FixedBufferForTesting buf(size); | 149 mojo::internal::FixedBufferForTesting buf(size); |
148 internal::PodUnion_Data* data = nullptr; | 150 internal::PodUnion_Data* data = nullptr; |
149 SerializeUnion_(std::move(pod1), &buf, &data, false, nullptr); | 151 mojo::internal::Serialize<PodUnionPtr>(pod1, &buf, &data, false, nullptr); |
150 | 152 |
151 PodUnionPtr pod2; | 153 PodUnionPtr pod2; |
152 Deserialize_(data, &pod2, nullptr); | 154 mojo::internal::Deserialize<PodUnionPtr>(data, &pod2, nullptr); |
153 | 155 |
154 EXPECT_EQ(AnEnum::SECOND, pod2->get_f_enum()); | 156 EXPECT_EQ(AnEnum::SECOND, pod2->get_f_enum()); |
155 EXPECT_TRUE(pod2->is_f_enum()); | 157 EXPECT_TRUE(pod2->is_f_enum()); |
156 EXPECT_EQ(pod2->which(), PodUnion::Tag::F_ENUM); | 158 EXPECT_EQ(pod2->which(), PodUnion::Tag::F_ENUM); |
157 } | 159 } |
158 | 160 |
159 TEST(UnionTest, PodValidation) { | 161 TEST(UnionTest, PodValidation) { |
160 PodUnionPtr pod(PodUnion::New()); | 162 PodUnionPtr pod(PodUnion::New()); |
161 pod->set_f_int8(10); | 163 pod->set_f_int8(10); |
162 | 164 |
163 size_t size = GetSerializedSize_(pod, false, nullptr); | 165 size_t size = |
| 166 mojo::internal::PrepareToSerialize<PodUnionPtr>(pod, false, nullptr); |
164 EXPECT_EQ(16U, size); | 167 EXPECT_EQ(16U, size); |
165 | 168 |
166 mojo::internal::FixedBufferForTesting buf(size); | 169 mojo::internal::FixedBufferForTesting buf(size); |
167 internal::PodUnion_Data* data = nullptr; | 170 internal::PodUnion_Data* data = nullptr; |
168 SerializeUnion_(std::move(pod), &buf, &data, false, nullptr); | 171 mojo::internal::Serialize<PodUnionPtr>(pod, &buf, &data, false, nullptr); |
169 data->EncodePointers(); | 172 data->EncodePointers(); |
170 | 173 |
171 void* raw_buf = buf.Leak(); | 174 void* raw_buf = buf.Leak(); |
172 mojo::internal::BoundsChecker bounds_checker(data, | 175 mojo::internal::BoundsChecker bounds_checker(data, |
173 static_cast<uint32_t>(size), 0); | 176 static_cast<uint32_t>(size), 0); |
174 EXPECT_TRUE( | 177 EXPECT_TRUE( |
175 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 178 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
176 free(raw_buf); | 179 free(raw_buf); |
177 } | 180 } |
178 | 181 |
179 TEST(UnionTest, SerializeNotNull) { | 182 TEST(UnionTest, SerializeNotNull) { |
180 PodUnionPtr pod(PodUnion::New()); | 183 PodUnionPtr pod(PodUnion::New()); |
181 pod->set_f_int8(0); | 184 pod->set_f_int8(0); |
182 size_t size = GetSerializedSize_(pod, false, nullptr); | 185 size_t size = |
| 186 mojo::internal::PrepareToSerialize<PodUnionPtr>(pod, false, nullptr); |
183 mojo::internal::FixedBufferForTesting buf(size); | 187 mojo::internal::FixedBufferForTesting buf(size); |
184 internal::PodUnion_Data* data = nullptr; | 188 internal::PodUnion_Data* data = nullptr; |
185 SerializeUnion_(std::move(pod), &buf, &data, false, nullptr); | 189 mojo::internal::Serialize<PodUnionPtr>(pod, &buf, &data, false, nullptr); |
186 EXPECT_FALSE(data->is_null()); | 190 EXPECT_FALSE(data->is_null()); |
187 } | 191 } |
188 | 192 |
189 TEST(UnionTest, SerializeIsNullInlined) { | 193 TEST(UnionTest, SerializeIsNullInlined) { |
190 PodUnionPtr pod; | 194 PodUnionPtr pod; |
191 size_t size = GetSerializedSize_(pod, false, nullptr); | 195 size_t size = |
| 196 mojo::internal::PrepareToSerialize<PodUnionPtr>(pod, false, nullptr); |
192 EXPECT_EQ(16U, size); | 197 EXPECT_EQ(16U, size); |
193 mojo::internal::FixedBufferForTesting buf(size); | 198 mojo::internal::FixedBufferForTesting buf(size); |
194 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf); | 199 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf); |
195 | 200 |
196 // Check that dirty output buffers are handled correctly by serialization. | 201 // Check that dirty output buffers are handled correctly by serialization. |
197 data->size = 16U; | 202 data->size = 16U; |
198 data->tag = PodUnion::Tag::F_UINT16; | 203 data->tag = PodUnion::Tag::F_UINT16; |
199 data->data.f_f_int16 = 20; | 204 data->data.f_f_int16 = 20; |
200 | 205 |
201 SerializeUnion_(std::move(pod), &buf, &data, true, nullptr); | 206 mojo::internal::Serialize<PodUnionPtr>(pod, &buf, &data, true, nullptr); |
202 EXPECT_TRUE(data->is_null()); | 207 EXPECT_TRUE(data->is_null()); |
203 | 208 |
204 PodUnionPtr pod2; | 209 PodUnionPtr pod2; |
205 Deserialize_(data, &pod2, nullptr); | 210 mojo::internal::Deserialize<PodUnionPtr>(data, &pod2, nullptr); |
206 EXPECT_TRUE(pod2.is_null()); | 211 EXPECT_TRUE(pod2.is_null()); |
207 } | 212 } |
208 | 213 |
209 TEST(UnionTest, SerializeIsNullNotInlined) { | 214 TEST(UnionTest, SerializeIsNullNotInlined) { |
210 PodUnionPtr pod; | 215 PodUnionPtr pod; |
211 size_t size = GetSerializedSize_(pod, false, nullptr); | 216 size_t size = |
| 217 mojo::internal::PrepareToSerialize<PodUnionPtr>(pod, false, nullptr); |
212 EXPECT_EQ(16U, size); | 218 EXPECT_EQ(16U, size); |
213 mojo::internal::FixedBufferForTesting buf(size); | 219 mojo::internal::FixedBufferForTesting buf(size); |
214 internal::PodUnion_Data* data = nullptr; | 220 internal::PodUnion_Data* data = nullptr; |
215 SerializeUnion_(std::move(pod), &buf, &data, false, nullptr); | 221 mojo::internal::Serialize<PodUnionPtr>(pod, &buf, &data, false, nullptr); |
216 EXPECT_EQ(nullptr, data); | 222 EXPECT_EQ(nullptr, data); |
217 } | 223 } |
218 | 224 |
219 TEST(UnionTest, NullValidation) { | 225 TEST(UnionTest, NullValidation) { |
220 void* buf = nullptr; | 226 void* buf = nullptr; |
221 mojo::internal::BoundsChecker bounds_checker(buf, 0, 0); | 227 mojo::internal::BoundsChecker bounds_checker(buf, 0, 0); |
222 EXPECT_TRUE(internal::PodUnion_Data::Validate(buf, &bounds_checker, false)); | 228 EXPECT_TRUE(internal::PodUnion_Data::Validate(buf, &bounds_checker, false)); |
223 } | 229 } |
224 | 230 |
225 TEST(UnionTest, OutOfAlignmentValidation) { | 231 TEST(UnionTest, OutOfAlignmentValidation) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 void* raw_buf = buf.Leak(); | 265 void* raw_buf = buf.Leak(); |
260 EXPECT_FALSE( | 266 EXPECT_FALSE( |
261 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 267 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
262 free(raw_buf); | 268 free(raw_buf); |
263 } | 269 } |
264 | 270 |
265 TEST(UnionTest, UnknownEnumValueValidation) { | 271 TEST(UnionTest, UnknownEnumValueValidation) { |
266 PodUnionPtr pod(PodUnion::New()); | 272 PodUnionPtr pod(PodUnion::New()); |
267 pod->set_f_enum(static_cast<AnEnum>(0xFFFF)); | 273 pod->set_f_enum(static_cast<AnEnum>(0xFFFF)); |
268 | 274 |
269 size_t size = GetSerializedSize_(pod, false, nullptr); | 275 size_t size = |
| 276 mojo::internal::PrepareToSerialize<PodUnionPtr>(pod, false, nullptr); |
270 EXPECT_EQ(16U, size); | 277 EXPECT_EQ(16U, size); |
271 | 278 |
272 mojo::internal::FixedBufferForTesting buf(size); | 279 mojo::internal::FixedBufferForTesting buf(size); |
273 internal::PodUnion_Data* data = nullptr; | 280 internal::PodUnion_Data* data = nullptr; |
274 SerializeUnion_(std::move(pod), &buf, &data, false, nullptr); | 281 mojo::internal::Serialize<PodUnionPtr>(pod, &buf, &data, false, nullptr); |
275 | 282 |
276 void* raw_buf = buf.Leak(); | 283 void* raw_buf = buf.Leak(); |
277 mojo::internal::BoundsChecker bounds_checker(data, | 284 mojo::internal::BoundsChecker bounds_checker(data, |
278 static_cast<uint32_t>(size), 0); | 285 static_cast<uint32_t>(size), 0); |
279 EXPECT_FALSE( | 286 EXPECT_FALSE( |
280 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 287 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
281 free(raw_buf); | 288 free(raw_buf); |
282 } | 289 } |
283 | 290 |
284 TEST(UnionTest, UnknownExtensibleEnumValueValidation) { | 291 TEST(UnionTest, UnknownExtensibleEnumValueValidation) { |
285 PodUnionPtr pod(PodUnion::New()); | 292 PodUnionPtr pod(PodUnion::New()); |
286 pod->set_f_extensible_enum(static_cast<AnExtensibleEnum>(0xFFFF)); | 293 pod->set_f_extensible_enum(static_cast<AnExtensibleEnum>(0xFFFF)); |
287 | 294 |
288 size_t size = GetSerializedSize_(pod, false, nullptr); | 295 size_t size = |
| 296 mojo::internal::PrepareToSerialize<PodUnionPtr>(pod, false, nullptr); |
289 EXPECT_EQ(16U, size); | 297 EXPECT_EQ(16U, size); |
290 | 298 |
291 mojo::internal::FixedBufferForTesting buf(size); | 299 mojo::internal::FixedBufferForTesting buf(size); |
292 internal::PodUnion_Data* data = nullptr; | 300 internal::PodUnion_Data* data = nullptr; |
293 SerializeUnion_(std::move(pod), &buf, &data, false, nullptr); | 301 mojo::internal::Serialize<PodUnionPtr>(pod, &buf, &data, false, nullptr); |
294 | 302 |
295 void* raw_buf = buf.Leak(); | 303 void* raw_buf = buf.Leak(); |
296 mojo::internal::BoundsChecker bounds_checker(data, | 304 mojo::internal::BoundsChecker bounds_checker(data, |
297 static_cast<uint32_t>(size), 0); | 305 static_cast<uint32_t>(size), 0); |
298 EXPECT_TRUE( | 306 EXPECT_TRUE( |
299 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 307 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
300 free(raw_buf); | 308 free(raw_buf); |
301 } | 309 } |
302 | 310 |
303 TEST(UnionTest, StringGetterSetter) { | 311 TEST(UnionTest, StringGetterSetter) { |
(...skipping 28 matching lines...) Expand all Loading... |
332 EXPECT_TRUE(pod_clone->is_f_string()); | 340 EXPECT_TRUE(pod_clone->is_f_string()); |
333 EXPECT_EQ(pod_clone->which(), ObjectUnion::Tag::F_STRING); | 341 EXPECT_EQ(pod_clone->which(), ObjectUnion::Tag::F_STRING); |
334 } | 342 } |
335 | 343 |
336 TEST(UnionTest, StringSerialization) { | 344 TEST(UnionTest, StringSerialization) { |
337 ObjectUnionPtr pod1(ObjectUnion::New()); | 345 ObjectUnionPtr pod1(ObjectUnion::New()); |
338 | 346 |
339 String hello("hello world"); | 347 String hello("hello world"); |
340 pod1->set_f_string(hello); | 348 pod1->set_f_string(hello); |
341 | 349 |
342 size_t size = GetSerializedSize_(pod1, false, nullptr); | 350 size_t size = |
| 351 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(pod1, false, nullptr); |
343 mojo::internal::FixedBufferForTesting buf(size); | 352 mojo::internal::FixedBufferForTesting buf(size); |
344 internal::ObjectUnion_Data* data = nullptr; | 353 internal::ObjectUnion_Data* data = nullptr; |
345 SerializeUnion_(std::move(pod1), &buf, &data, false, nullptr); | 354 mojo::internal::Serialize<ObjectUnionPtr>(pod1, &buf, &data, false, nullptr); |
346 | 355 |
347 data->EncodePointers(); | 356 data->EncodePointers(); |
348 data->DecodePointers(); | 357 data->DecodePointers(); |
349 | 358 |
350 ObjectUnionPtr pod2; | 359 ObjectUnionPtr pod2; |
351 Deserialize_(data, &pod2, nullptr); | 360 mojo::internal::Deserialize<ObjectUnionPtr>(data, &pod2, nullptr); |
352 EXPECT_EQ(hello, pod2->get_f_string()); | 361 EXPECT_EQ(hello, pod2->get_f_string()); |
353 EXPECT_TRUE(pod2->is_f_string()); | 362 EXPECT_TRUE(pod2->is_f_string()); |
354 EXPECT_EQ(pod2->which(), ObjectUnion::Tag::F_STRING); | 363 EXPECT_EQ(pod2->which(), ObjectUnion::Tag::F_STRING); |
355 } | 364 } |
356 | 365 |
357 TEST(UnionTest, NullStringValidation) { | 366 TEST(UnionTest, NullStringValidation) { |
358 size_t size = sizeof(internal::ObjectUnion_Data); | 367 size_t size = sizeof(internal::ObjectUnion_Data); |
359 mojo::internal::FixedBufferForTesting buf(size); | 368 mojo::internal::FixedBufferForTesting buf(size); |
360 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); | 369 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); |
361 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; | 370 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 EXPECT_EQ(10, clone->pod_union->get_f_int8()); | 537 EXPECT_EQ(10, clone->pod_union->get_f_int8()); |
529 } | 538 } |
530 | 539 |
531 // Serialization test of a struct with a union of plain old data. | 540 // Serialization test of a struct with a union of plain old data. |
532 TEST(UnionTest, Serialization_UnionOfPods) { | 541 TEST(UnionTest, Serialization_UnionOfPods) { |
533 SmallStructPtr small_struct(SmallStruct::New()); | 542 SmallStructPtr small_struct(SmallStruct::New()); |
534 small_struct->pod_union = PodUnion::New(); | 543 small_struct->pod_union = PodUnion::New(); |
535 small_struct->pod_union->set_f_int32(10); | 544 small_struct->pod_union->set_f_int32(10); |
536 | 545 |
537 mojo::internal::SerializationContext context; | 546 mojo::internal::SerializationContext context; |
538 size_t size = GetSerializedSize_(small_struct, &context); | 547 size_t size = mojo::internal::PrepareToSerialize<SmallStructPtr>(small_struct, |
| 548 &context); |
539 | 549 |
540 mojo::internal::FixedBufferForTesting buf(size); | 550 mojo::internal::FixedBufferForTesting buf(size); |
541 internal::SmallStruct_Data* data = nullptr; | 551 internal::SmallStruct_Data* data = nullptr; |
542 Serialize_(std::move(small_struct), &buf, &data, &context); | 552 mojo::internal::Serialize<SmallStructPtr>(small_struct, &buf, &data, |
| 553 &context); |
543 | 554 |
544 SmallStructPtr deserialized; | 555 SmallStructPtr deserialized; |
545 Deserialize_(data, &deserialized, &context); | 556 mojo::internal::Deserialize<SmallStructPtr>(data, &deserialized, &context); |
546 | 557 |
547 EXPECT_EQ(10, deserialized->pod_union->get_f_int32()); | 558 EXPECT_EQ(10, deserialized->pod_union->get_f_int32()); |
548 } | 559 } |
549 | 560 |
550 // Serialization test of a struct with a union of structs. | 561 // Serialization test of a struct with a union of structs. |
551 TEST(UnionTest, Serialization_UnionOfObjects) { | 562 TEST(UnionTest, Serialization_UnionOfObjects) { |
552 SmallObjStructPtr obj_struct(SmallObjStruct::New()); | 563 SmallObjStructPtr obj_struct(SmallObjStruct::New()); |
553 obj_struct->obj_union = ObjectUnion::New(); | 564 obj_struct->obj_union = ObjectUnion::New(); |
554 String hello("hello world"); | 565 String hello("hello world"); |
555 obj_struct->obj_union->set_f_string(hello); | 566 obj_struct->obj_union->set_f_string(hello); |
556 | 567 |
557 size_t size = GetSerializedSize_(obj_struct, nullptr); | 568 size_t size = mojo::internal::PrepareToSerialize<SmallObjStructPtr>( |
| 569 obj_struct, nullptr); |
558 | 570 |
559 mojo::internal::FixedBufferForTesting buf(size); | 571 mojo::internal::FixedBufferForTesting buf(size); |
560 internal::SmallObjStruct_Data* data = nullptr; | 572 internal::SmallObjStruct_Data* data = nullptr; |
561 Serialize_(std::move(obj_struct), &buf, &data, nullptr); | 573 mojo::internal::Serialize<SmallObjStructPtr>(obj_struct, &buf, &data, |
| 574 nullptr); |
562 | 575 |
563 data->EncodePointers(); | 576 data->EncodePointers(); |
564 data->DecodePointers(); | 577 data->DecodePointers(); |
565 | 578 |
566 SmallObjStructPtr deserialized; | 579 SmallObjStructPtr deserialized; |
567 Deserialize_(data, &deserialized, nullptr); | 580 mojo::internal::Deserialize<SmallObjStructPtr>(data, &deserialized, nullptr); |
568 | 581 |
569 EXPECT_EQ(hello, deserialized->obj_union->get_f_string()); | 582 EXPECT_EQ(hello, deserialized->obj_union->get_f_string()); |
570 } | 583 } |
571 | 584 |
572 // Validation test of a struct with a union. | 585 // Validation test of a struct with a union. |
573 TEST(UnionTest, Validation_UnionsInStruct) { | 586 TEST(UnionTest, Validation_UnionsInStruct) { |
574 SmallStructPtr small_struct(SmallStruct::New()); | 587 SmallStructPtr small_struct(SmallStruct::New()); |
575 small_struct->pod_union = PodUnion::New(); | 588 small_struct->pod_union = PodUnion::New(); |
576 small_struct->pod_union->set_f_int32(10); | 589 small_struct->pod_union->set_f_int32(10); |
577 | 590 |
578 mojo::internal::SerializationContext context; | 591 mojo::internal::SerializationContext context; |
579 size_t size = GetSerializedSize_(small_struct, &context); | 592 size_t size = mojo::internal::PrepareToSerialize<SmallStructPtr>(small_struct, |
| 593 &context); |
580 | 594 |
581 mojo::internal::FixedBufferForTesting buf(size); | 595 mojo::internal::FixedBufferForTesting buf(size); |
582 internal::SmallStruct_Data* data = nullptr; | 596 internal::SmallStruct_Data* data = nullptr; |
583 Serialize_(std::move(small_struct), &buf, &data, &context); | 597 mojo::internal::Serialize<SmallStructPtr>(small_struct, &buf, &data, |
| 598 &context); |
584 | 599 |
585 data->EncodePointers(); | 600 data->EncodePointers(); |
586 | 601 |
587 void* raw_buf = buf.Leak(); | 602 void* raw_buf = buf.Leak(); |
588 mojo::internal::BoundsChecker bounds_checker(data, | 603 mojo::internal::BoundsChecker bounds_checker(data, |
589 static_cast<uint32_t>(size), 0); | 604 static_cast<uint32_t>(size), 0); |
590 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); | 605 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); |
591 free(raw_buf); | 606 free(raw_buf); |
592 } | 607 } |
593 | 608 |
594 // Validation test of a struct union fails due to unknown union tag. | 609 // Validation test of a struct union fails due to unknown union tag. |
595 TEST(UnionTest, Validation_PodUnionInStruct_Failure) { | 610 TEST(UnionTest, Validation_PodUnionInStruct_Failure) { |
596 SmallStructPtr small_struct(SmallStruct::New()); | 611 SmallStructPtr small_struct(SmallStruct::New()); |
597 small_struct->pod_union = PodUnion::New(); | 612 small_struct->pod_union = PodUnion::New(); |
598 small_struct->pod_union->set_f_int32(10); | 613 small_struct->pod_union->set_f_int32(10); |
599 | 614 |
600 mojo::internal::SerializationContext context; | 615 mojo::internal::SerializationContext context; |
601 size_t size = GetSerializedSize_(small_struct, &context); | 616 size_t size = mojo::internal::PrepareToSerialize<SmallStructPtr>(small_struct, |
| 617 &context); |
602 | 618 |
603 mojo::internal::FixedBufferForTesting buf(size); | 619 mojo::internal::FixedBufferForTesting buf(size); |
604 internal::SmallStruct_Data* data = nullptr; | 620 internal::SmallStruct_Data* data = nullptr; |
605 Serialize_(std::move(small_struct), &buf, &data, &context); | 621 mojo::internal::Serialize<SmallStructPtr>(small_struct, &buf, &data, |
| 622 &context); |
606 data->pod_union.tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(100); | 623 data->pod_union.tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(100); |
607 | 624 |
608 data->EncodePointers(); | 625 data->EncodePointers(); |
609 | 626 |
610 void* raw_buf = buf.Leak(); | 627 void* raw_buf = buf.Leak(); |
611 mojo::internal::BoundsChecker bounds_checker(data, | 628 mojo::internal::BoundsChecker bounds_checker(data, |
612 static_cast<uint32_t>(size), 0); | 629 static_cast<uint32_t>(size), 0); |
613 EXPECT_FALSE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); | 630 EXPECT_FALSE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); |
614 free(raw_buf); | 631 free(raw_buf); |
615 } | 632 } |
616 | 633 |
617 // Validation fails due to non-nullable null union in struct. | 634 // Validation fails due to non-nullable null union in struct. |
618 TEST(UnionTest, Validation_NullUnion_Failure) { | 635 TEST(UnionTest, Validation_NullUnion_Failure) { |
619 SmallStructNonNullableUnionPtr small_struct( | 636 SmallStructNonNullableUnionPtr small_struct( |
620 SmallStructNonNullableUnion::New()); | 637 SmallStructNonNullableUnion::New()); |
621 | 638 |
622 size_t size = GetSerializedSize_(small_struct, nullptr); | 639 size_t size = |
| 640 mojo::internal::PrepareToSerialize<SmallStructNonNullableUnionPtr>( |
| 641 small_struct, nullptr); |
623 | 642 |
624 mojo::internal::FixedBufferForTesting buf(size); | 643 mojo::internal::FixedBufferForTesting buf(size); |
625 internal::SmallStructNonNullableUnion_Data* data = | 644 internal::SmallStructNonNullableUnion_Data* data = |
626 internal::SmallStructNonNullableUnion_Data::New(&buf); | 645 internal::SmallStructNonNullableUnion_Data::New(&buf); |
627 | 646 |
628 void* raw_buf = buf.Leak(); | 647 void* raw_buf = buf.Leak(); |
629 mojo::internal::BoundsChecker bounds_checker(data, | 648 mojo::internal::BoundsChecker bounds_checker(data, |
630 static_cast<uint32_t>(size), 0); | 649 static_cast<uint32_t>(size), 0); |
631 EXPECT_FALSE(internal::SmallStructNonNullableUnion_Data::Validate( | 650 EXPECT_FALSE(internal::SmallStructNonNullableUnion_Data::Validate( |
632 raw_buf, &bounds_checker)); | 651 raw_buf, &bounds_checker)); |
633 free(raw_buf); | 652 free(raw_buf); |
634 } | 653 } |
635 | 654 |
636 // Validation passes with nullable null union. | 655 // Validation passes with nullable null union. |
637 TEST(UnionTest, Validation_NullableUnion) { | 656 TEST(UnionTest, Validation_NullableUnion) { |
638 SmallStructPtr small_struct(SmallStruct::New()); | 657 SmallStructPtr small_struct(SmallStruct::New()); |
639 | 658 |
640 mojo::internal::SerializationContext context; | 659 mojo::internal::SerializationContext context; |
641 size_t size = GetSerializedSize_(small_struct, &context); | 660 size_t size = mojo::internal::PrepareToSerialize<SmallStructPtr>(small_struct, |
| 661 &context); |
642 | 662 |
643 mojo::internal::FixedBufferForTesting buf(size); | 663 mojo::internal::FixedBufferForTesting buf(size); |
644 internal::SmallStruct_Data* data = nullptr; | 664 internal::SmallStruct_Data* data = nullptr; |
645 Serialize_(std::move(small_struct), &buf, &data, &context); | 665 mojo::internal::Serialize<SmallStructPtr>(small_struct, &buf, &data, |
| 666 &context); |
646 | 667 |
647 data->EncodePointers(); | 668 data->EncodePointers(); |
648 | 669 |
649 void* raw_buf = buf.Leak(); | 670 void* raw_buf = buf.Leak(); |
650 mojo::internal::BoundsChecker bounds_checker(data, | 671 mojo::internal::BoundsChecker bounds_checker(data, |
651 static_cast<uint32_t>(size), 0); | 672 static_cast<uint32_t>(size), 0); |
652 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); | 673 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); |
653 free(raw_buf); | 674 free(raw_buf); |
654 } | 675 } |
655 | 676 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
731 EXPECT_EQ(8, obj->get_f_dummy()->f_int8); | 752 EXPECT_EQ(8, obj->get_f_dummy()->f_int8); |
732 } | 753 } |
733 | 754 |
734 TEST(UnionTest, StructInUnionSerialization) { | 755 TEST(UnionTest, StructInUnionSerialization) { |
735 DummyStructPtr dummy(DummyStruct::New()); | 756 DummyStructPtr dummy(DummyStruct::New()); |
736 dummy->f_int8 = 8; | 757 dummy->f_int8 = 8; |
737 | 758 |
738 ObjectUnionPtr obj(ObjectUnion::New()); | 759 ObjectUnionPtr obj(ObjectUnion::New()); |
739 obj->set_f_dummy(std::move(dummy)); | 760 obj->set_f_dummy(std::move(dummy)); |
740 | 761 |
741 size_t size = GetSerializedSize_(obj, false, nullptr); | 762 size_t size = |
| 763 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, nullptr); |
742 EXPECT_EQ(32U, size); | 764 EXPECT_EQ(32U, size); |
743 | 765 |
744 mojo::internal::FixedBufferForTesting buf(size); | 766 mojo::internal::FixedBufferForTesting buf(size); |
745 internal::ObjectUnion_Data* data = nullptr; | 767 internal::ObjectUnion_Data* data = nullptr; |
746 SerializeUnion_(std::move(obj), &buf, &data, false, nullptr); | 768 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, nullptr); |
747 | 769 |
748 data->EncodePointers(); | 770 data->EncodePointers(); |
749 data->DecodePointers(); | 771 data->DecodePointers(); |
750 | 772 |
751 ObjectUnionPtr obj2; | 773 ObjectUnionPtr obj2; |
752 Deserialize_(data, &obj2, nullptr); | 774 mojo::internal::Deserialize<ObjectUnionPtr>(data, &obj2, nullptr); |
753 EXPECT_EQ(8, obj2->get_f_dummy()->f_int8); | 775 EXPECT_EQ(8, obj2->get_f_dummy()->f_int8); |
754 } | 776 } |
755 | 777 |
756 TEST(UnionTest, StructInUnionValidation) { | 778 TEST(UnionTest, StructInUnionValidation) { |
757 DummyStructPtr dummy(DummyStruct::New()); | 779 DummyStructPtr dummy(DummyStruct::New()); |
758 dummy->f_int8 = 8; | 780 dummy->f_int8 = 8; |
759 | 781 |
760 ObjectUnionPtr obj(ObjectUnion::New()); | 782 ObjectUnionPtr obj(ObjectUnion::New()); |
761 obj->set_f_dummy(std::move(dummy)); | 783 obj->set_f_dummy(std::move(dummy)); |
762 | 784 |
763 size_t size = GetSerializedSize_(obj, false, nullptr); | 785 size_t size = |
| 786 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, nullptr); |
764 | 787 |
765 mojo::internal::FixedBufferForTesting buf(size); | 788 mojo::internal::FixedBufferForTesting buf(size); |
766 internal::ObjectUnion_Data* data = nullptr; | 789 internal::ObjectUnion_Data* data = nullptr; |
767 SerializeUnion_(std::move(obj), &buf, &data, false, nullptr); | 790 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, nullptr); |
768 | 791 |
769 data->EncodePointers(); | 792 data->EncodePointers(); |
770 | 793 |
771 void* raw_buf = buf.Leak(); | 794 void* raw_buf = buf.Leak(); |
772 mojo::internal::BoundsChecker bounds_checker(data, | 795 mojo::internal::BoundsChecker bounds_checker(data, |
773 static_cast<uint32_t>(size), 0); | 796 static_cast<uint32_t>(size), 0); |
774 EXPECT_TRUE( | 797 EXPECT_TRUE( |
775 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 798 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
776 free(raw_buf); | 799 free(raw_buf); |
777 } | 800 } |
778 | 801 |
779 TEST(UnionTest, StructInUnionValidationNonNullable) { | 802 TEST(UnionTest, StructInUnionValidationNonNullable) { |
780 DummyStructPtr dummy(nullptr); | 803 DummyStructPtr dummy(nullptr); |
781 | 804 |
782 ObjectUnionPtr obj(ObjectUnion::New()); | 805 ObjectUnionPtr obj(ObjectUnion::New()); |
783 obj->set_f_dummy(std::move(dummy)); | 806 obj->set_f_dummy(std::move(dummy)); |
784 | 807 |
785 size_t size = GetSerializedSize_(obj, false, nullptr); | 808 size_t size = |
| 809 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, nullptr); |
786 | 810 |
787 mojo::internal::FixedBufferForTesting buf(size); | 811 mojo::internal::FixedBufferForTesting buf(size); |
788 internal::ObjectUnion_Data* data = nullptr; | 812 internal::ObjectUnion_Data* data = nullptr; |
789 SerializeUnion_(std::move(obj), &buf, &data, false, nullptr); | 813 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, nullptr); |
790 | 814 |
791 data->EncodePointers(); | 815 data->EncodePointers(); |
792 | 816 |
793 void* raw_buf = buf.Leak(); | 817 void* raw_buf = buf.Leak(); |
794 mojo::internal::BoundsChecker bounds_checker(data, | 818 mojo::internal::BoundsChecker bounds_checker(data, |
795 static_cast<uint32_t>(size), 0); | 819 static_cast<uint32_t>(size), 0); |
796 EXPECT_FALSE( | 820 EXPECT_FALSE( |
797 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 821 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
798 free(raw_buf); | 822 free(raw_buf); |
799 } | 823 } |
800 | 824 |
801 TEST(UnionTest, StructInUnionValidationNullable) { | 825 TEST(UnionTest, StructInUnionValidationNullable) { |
802 DummyStructPtr dummy(nullptr); | 826 DummyStructPtr dummy(nullptr); |
803 | 827 |
804 ObjectUnionPtr obj(ObjectUnion::New()); | 828 ObjectUnionPtr obj(ObjectUnion::New()); |
805 obj->set_f_nullable(std::move(dummy)); | 829 obj->set_f_nullable(std::move(dummy)); |
806 | 830 |
807 size_t size = GetSerializedSize_(obj, false, nullptr); | 831 size_t size = |
| 832 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, nullptr); |
808 | 833 |
809 mojo::internal::FixedBufferForTesting buf(size); | 834 mojo::internal::FixedBufferForTesting buf(size); |
810 internal::ObjectUnion_Data* data = nullptr; | 835 internal::ObjectUnion_Data* data = nullptr; |
811 SerializeUnion_(std::move(obj), &buf, &data, false, nullptr); | 836 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, nullptr); |
812 | 837 |
813 data->EncodePointers(); | 838 data->EncodePointers(); |
814 | 839 |
815 void* raw_buf = buf.Leak(); | 840 void* raw_buf = buf.Leak(); |
816 mojo::internal::BoundsChecker bounds_checker(data, | 841 mojo::internal::BoundsChecker bounds_checker(data, |
817 static_cast<uint32_t>(size), 0); | 842 static_cast<uint32_t>(size), 0); |
818 EXPECT_TRUE( | 843 EXPECT_TRUE( |
819 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 844 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
820 free(raw_buf); | 845 free(raw_buf); |
821 } | 846 } |
(...skipping 11 matching lines...) Expand all Loading... |
833 } | 858 } |
834 | 859 |
835 TEST(UnionTest, ArrayInUnionSerialization) { | 860 TEST(UnionTest, ArrayInUnionSerialization) { |
836 Array<int8_t> array(2); | 861 Array<int8_t> array(2); |
837 array[0] = 8; | 862 array[0] = 8; |
838 array[1] = 9; | 863 array[1] = 9; |
839 | 864 |
840 ObjectUnionPtr obj(ObjectUnion::New()); | 865 ObjectUnionPtr obj(ObjectUnion::New()); |
841 obj->set_f_array_int8(std::move(array)); | 866 obj->set_f_array_int8(std::move(array)); |
842 | 867 |
843 size_t size = GetSerializedSize_(obj, false, nullptr); | 868 size_t size = |
| 869 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, nullptr); |
844 EXPECT_EQ(32U, size); | 870 EXPECT_EQ(32U, size); |
845 | 871 |
846 mojo::internal::FixedBufferForTesting buf(size); | 872 mojo::internal::FixedBufferForTesting buf(size); |
847 internal::ObjectUnion_Data* data = nullptr; | 873 internal::ObjectUnion_Data* data = nullptr; |
848 SerializeUnion_(std::move(obj), &buf, &data, false, nullptr); | 874 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, nullptr); |
849 | 875 |
850 data->EncodePointers(); | 876 data->EncodePointers(); |
851 data->DecodePointers(); | 877 data->DecodePointers(); |
852 | 878 |
853 ObjectUnionPtr obj2; | 879 ObjectUnionPtr obj2; |
854 Deserialize_(data, &obj2, nullptr); | 880 mojo::internal::Deserialize<ObjectUnionPtr>(data, &obj2, nullptr); |
855 | 881 |
856 EXPECT_EQ(8, obj2->get_f_array_int8()[0]); | 882 EXPECT_EQ(8, obj2->get_f_array_int8()[0]); |
857 EXPECT_EQ(9, obj2->get_f_array_int8()[1]); | 883 EXPECT_EQ(9, obj2->get_f_array_int8()[1]); |
858 } | 884 } |
859 | 885 |
860 TEST(UnionTest, ArrayInUnionValidation) { | 886 TEST(UnionTest, ArrayInUnionValidation) { |
861 Array<int8_t> array(2); | 887 Array<int8_t> array(2); |
862 array[0] = 8; | 888 array[0] = 8; |
863 array[1] = 9; | 889 array[1] = 9; |
864 | 890 |
865 ObjectUnionPtr obj(ObjectUnion::New()); | 891 ObjectUnionPtr obj(ObjectUnion::New()); |
866 obj->set_f_array_int8(std::move(array)); | 892 obj->set_f_array_int8(std::move(array)); |
867 | 893 |
868 size_t size = GetSerializedSize_(obj, false, nullptr); | 894 size_t size = |
| 895 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, nullptr); |
869 mojo::internal::FixedBufferForTesting buf(size); | 896 mojo::internal::FixedBufferForTesting buf(size); |
870 internal::ObjectUnion_Data* data = nullptr; | 897 internal::ObjectUnion_Data* data = nullptr; |
871 SerializeUnion_(std::move(obj), &buf, &data, false, nullptr); | 898 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, nullptr); |
872 | 899 |
873 data->EncodePointers(); | 900 data->EncodePointers(); |
874 | 901 |
875 void* raw_buf = buf.Leak(); | 902 void* raw_buf = buf.Leak(); |
876 mojo::internal::BoundsChecker bounds_checker(data, | 903 mojo::internal::BoundsChecker bounds_checker(data, |
877 static_cast<uint32_t>(size), 0); | 904 static_cast<uint32_t>(size), 0); |
878 | 905 |
879 EXPECT_TRUE( | 906 EXPECT_TRUE( |
880 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 907 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
881 free(raw_buf); | 908 free(raw_buf); |
(...skipping 13 matching lines...) Expand all Loading... |
895 | 922 |
896 TEST(UnionTest, MapInUnionSerialization) { | 923 TEST(UnionTest, MapInUnionSerialization) { |
897 Map<String, int8_t> map; | 924 Map<String, int8_t> map; |
898 map.insert("one", 1); | 925 map.insert("one", 1); |
899 map.insert("two", 2); | 926 map.insert("two", 2); |
900 | 927 |
901 ObjectUnionPtr obj(ObjectUnion::New()); | 928 ObjectUnionPtr obj(ObjectUnion::New()); |
902 obj->set_f_map_int8(std::move(map)); | 929 obj->set_f_map_int8(std::move(map)); |
903 | 930 |
904 mojo::internal::SerializationContext context; | 931 mojo::internal::SerializationContext context; |
905 size_t size = GetSerializedSize_(obj, false, &context); | 932 size_t size = |
| 933 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, &context); |
906 EXPECT_EQ(112U, size); | 934 EXPECT_EQ(112U, size); |
907 | 935 |
908 mojo::internal::FixedBufferForTesting buf(size); | 936 mojo::internal::FixedBufferForTesting buf(size); |
909 internal::ObjectUnion_Data* data = nullptr; | 937 internal::ObjectUnion_Data* data = nullptr; |
910 SerializeUnion_(std::move(obj), &buf, &data, false, &context); | 938 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, &context); |
911 | 939 |
912 data->EncodePointers(); | 940 data->EncodePointers(); |
913 data->DecodePointers(); | 941 data->DecodePointers(); |
914 | 942 |
915 ObjectUnionPtr obj2; | 943 ObjectUnionPtr obj2; |
916 Deserialize_(data, &obj2, &context); | 944 mojo::internal::Deserialize<ObjectUnionPtr>(data, &obj2, &context); |
917 | 945 |
918 EXPECT_EQ(1, obj2->get_f_map_int8()["one"]); | 946 EXPECT_EQ(1, obj2->get_f_map_int8()["one"]); |
919 EXPECT_EQ(2, obj2->get_f_map_int8()["two"]); | 947 EXPECT_EQ(2, obj2->get_f_map_int8()["two"]); |
920 } | 948 } |
921 | 949 |
922 TEST(UnionTest, MapInUnionValidation) { | 950 TEST(UnionTest, MapInUnionValidation) { |
923 Map<String, int8_t> map; | 951 Map<String, int8_t> map; |
924 map.insert("one", 1); | 952 map.insert("one", 1); |
925 map.insert("two", 2); | 953 map.insert("two", 2); |
926 | 954 |
927 ObjectUnionPtr obj(ObjectUnion::New()); | 955 ObjectUnionPtr obj(ObjectUnion::New()); |
928 obj->set_f_map_int8(std::move(map)); | 956 obj->set_f_map_int8(std::move(map)); |
929 | 957 |
930 mojo::internal::SerializationContext context; | 958 mojo::internal::SerializationContext context; |
931 size_t size = GetSerializedSize_(obj, false, &context); | 959 size_t size = |
| 960 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, &context); |
932 EXPECT_EQ(112U, size); | 961 EXPECT_EQ(112U, size); |
933 | 962 |
934 mojo::internal::FixedBufferForTesting buf(size); | 963 mojo::internal::FixedBufferForTesting buf(size); |
935 internal::ObjectUnion_Data* data = nullptr; | 964 internal::ObjectUnion_Data* data = nullptr; |
936 SerializeUnion_(std::move(obj), &buf, &data, false, &context); | 965 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, &context); |
937 | 966 |
938 data->EncodePointers(); | 967 data->EncodePointers(); |
939 | 968 |
940 void* raw_buf = buf.Leak(); | 969 void* raw_buf = buf.Leak(); |
941 mojo::internal::BoundsChecker bounds_checker(data, | 970 mojo::internal::BoundsChecker bounds_checker(data, |
942 static_cast<uint32_t>(size), 0); | 971 static_cast<uint32_t>(size), 0); |
943 | 972 |
944 EXPECT_TRUE( | 973 EXPECT_TRUE( |
945 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 974 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
946 free(raw_buf); | 975 free(raw_buf); |
947 } | 976 } |
948 | 977 |
949 TEST(UnionTest, UnionInUnionGetterSetter) { | 978 TEST(UnionTest, UnionInUnionGetterSetter) { |
950 PodUnionPtr pod(PodUnion::New()); | 979 PodUnionPtr pod(PodUnion::New()); |
951 pod->set_f_int8(10); | 980 pod->set_f_int8(10); |
952 | 981 |
953 ObjectUnionPtr obj(ObjectUnion::New()); | 982 ObjectUnionPtr obj(ObjectUnion::New()); |
954 obj->set_f_pod_union(std::move(pod)); | 983 obj->set_f_pod_union(std::move(pod)); |
955 | 984 |
956 EXPECT_EQ(10, obj->get_f_pod_union()->get_f_int8()); | 985 EXPECT_EQ(10, obj->get_f_pod_union()->get_f_int8()); |
957 } | 986 } |
958 | 987 |
959 TEST(UnionTest, UnionInUnionSerialization) { | 988 TEST(UnionTest, UnionInUnionSerialization) { |
960 PodUnionPtr pod(PodUnion::New()); | 989 PodUnionPtr pod(PodUnion::New()); |
961 pod->set_f_int8(10); | 990 pod->set_f_int8(10); |
962 | 991 |
963 ObjectUnionPtr obj(ObjectUnion::New()); | 992 ObjectUnionPtr obj(ObjectUnion::New()); |
964 obj->set_f_pod_union(std::move(pod)); | 993 obj->set_f_pod_union(std::move(pod)); |
965 | 994 |
966 size_t size = GetSerializedSize_(obj, false, nullptr); | 995 size_t size = |
| 996 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, nullptr); |
967 EXPECT_EQ(32U, size); | 997 EXPECT_EQ(32U, size); |
968 | 998 |
969 mojo::internal::FixedBufferForTesting buf(size); | 999 mojo::internal::FixedBufferForTesting buf(size); |
970 internal::ObjectUnion_Data* data = nullptr; | 1000 internal::ObjectUnion_Data* data = nullptr; |
971 SerializeUnion_(std::move(obj), &buf, &data, false, nullptr); | 1001 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, nullptr); |
972 | 1002 |
973 data->EncodePointers(); | 1003 data->EncodePointers(); |
974 data->DecodePointers(); | 1004 data->DecodePointers(); |
975 | 1005 |
976 ObjectUnionPtr obj2; | 1006 ObjectUnionPtr obj2; |
977 Deserialize_(data, &obj2, nullptr); | 1007 mojo::internal::Deserialize<ObjectUnionPtr>(data, &obj2, nullptr); |
978 EXPECT_EQ(10, obj2->get_f_pod_union()->get_f_int8()); | 1008 EXPECT_EQ(10, obj2->get_f_pod_union()->get_f_int8()); |
979 } | 1009 } |
980 | 1010 |
981 TEST(UnionTest, UnionInUnionValidation) { | 1011 TEST(UnionTest, UnionInUnionValidation) { |
982 PodUnionPtr pod(PodUnion::New()); | 1012 PodUnionPtr pod(PodUnion::New()); |
983 pod->set_f_int8(10); | 1013 pod->set_f_int8(10); |
984 | 1014 |
985 ObjectUnionPtr obj(ObjectUnion::New()); | 1015 ObjectUnionPtr obj(ObjectUnion::New()); |
986 obj->set_f_pod_union(std::move(pod)); | 1016 obj->set_f_pod_union(std::move(pod)); |
987 | 1017 |
988 size_t size = GetSerializedSize_(obj, false, nullptr); | 1018 size_t size = |
| 1019 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, nullptr); |
989 EXPECT_EQ(32U, size); | 1020 EXPECT_EQ(32U, size); |
990 | 1021 |
991 mojo::internal::FixedBufferForTesting buf(size); | 1022 mojo::internal::FixedBufferForTesting buf(size); |
992 internal::ObjectUnion_Data* data = nullptr; | 1023 internal::ObjectUnion_Data* data = nullptr; |
993 SerializeUnion_(std::move(obj), &buf, &data, false, nullptr); | 1024 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, nullptr); |
994 | 1025 |
995 data->EncodePointers(); | 1026 data->EncodePointers(); |
996 | 1027 |
997 void* raw_buf = buf.Leak(); | 1028 void* raw_buf = buf.Leak(); |
998 mojo::internal::BoundsChecker bounds_checker(data, | 1029 mojo::internal::BoundsChecker bounds_checker(data, |
999 static_cast<uint32_t>(size), 0); | 1030 static_cast<uint32_t>(size), 0); |
1000 EXPECT_TRUE( | 1031 EXPECT_TRUE( |
1001 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 1032 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
1002 free(raw_buf); | 1033 free(raw_buf); |
1003 } | 1034 } |
1004 | 1035 |
1005 TEST(UnionTest, UnionInUnionValidationNonNullable) { | 1036 TEST(UnionTest, UnionInUnionValidationNonNullable) { |
1006 PodUnionPtr pod(nullptr); | 1037 PodUnionPtr pod(nullptr); |
1007 | 1038 |
1008 ObjectUnionPtr obj(ObjectUnion::New()); | 1039 ObjectUnionPtr obj(ObjectUnion::New()); |
1009 obj->set_f_pod_union(std::move(pod)); | 1040 obj->set_f_pod_union(std::move(pod)); |
1010 | 1041 |
1011 size_t size = GetSerializedSize_(obj, false, nullptr); | 1042 size_t size = |
| 1043 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, nullptr); |
1012 | 1044 |
1013 mojo::internal::FixedBufferForTesting buf(size); | 1045 mojo::internal::FixedBufferForTesting buf(size); |
1014 internal::ObjectUnion_Data* data = nullptr; | 1046 internal::ObjectUnion_Data* data = nullptr; |
1015 SerializeUnion_(std::move(obj), &buf, &data, false, nullptr); | 1047 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, nullptr); |
1016 data->EncodePointers(); | 1048 data->EncodePointers(); |
1017 | 1049 |
1018 void* raw_buf = buf.Leak(); | 1050 void* raw_buf = buf.Leak(); |
1019 mojo::internal::BoundsChecker bounds_checker(data, | 1051 mojo::internal::BoundsChecker bounds_checker(data, |
1020 static_cast<uint32_t>(size), 0); | 1052 static_cast<uint32_t>(size), 0); |
1021 EXPECT_FALSE( | 1053 EXPECT_FALSE( |
1022 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 1054 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
1023 free(raw_buf); | 1055 free(raw_buf); |
1024 } | 1056 } |
1025 | 1057 |
(...skipping 18 matching lines...) Expand all Loading... |
1044 TEST(UnionTest, HandleInUnionSerialization) { | 1076 TEST(UnionTest, HandleInUnionSerialization) { |
1045 ScopedMessagePipeHandle pipe0; | 1077 ScopedMessagePipeHandle pipe0; |
1046 ScopedMessagePipeHandle pipe1; | 1078 ScopedMessagePipeHandle pipe1; |
1047 | 1079 |
1048 CreateMessagePipe(nullptr, &pipe0, &pipe1); | 1080 CreateMessagePipe(nullptr, &pipe0, &pipe1); |
1049 | 1081 |
1050 HandleUnionPtr handle(HandleUnion::New()); | 1082 HandleUnionPtr handle(HandleUnion::New()); |
1051 handle->set_f_message_pipe(std::move(pipe1)); | 1083 handle->set_f_message_pipe(std::move(pipe1)); |
1052 | 1084 |
1053 mojo::internal::SerializationContext context; | 1085 mojo::internal::SerializationContext context; |
1054 size_t size = GetSerializedSize_(handle, false, &context); | 1086 size_t size = mojo::internal::PrepareToSerialize<HandleUnionPtr>( |
| 1087 handle, false, &context); |
1055 EXPECT_EQ(16U, size); | 1088 EXPECT_EQ(16U, size); |
1056 | 1089 |
1057 mojo::internal::FixedBufferForTesting buf(size); | 1090 mojo::internal::FixedBufferForTesting buf(size); |
1058 internal::HandleUnion_Data* data = nullptr; | 1091 internal::HandleUnion_Data* data = nullptr; |
1059 SerializeUnion_(std::move(handle), &buf, &data, false, &context); | 1092 mojo::internal::Serialize<HandleUnionPtr>(handle, &buf, &data, false, |
| 1093 &context); |
1060 EXPECT_EQ(1U, context.handles.size()); | 1094 EXPECT_EQ(1U, context.handles.size()); |
1061 | 1095 |
1062 HandleUnionPtr handle2(HandleUnion::New()); | 1096 HandleUnionPtr handle2(HandleUnion::New()); |
1063 Deserialize_(data, &handle2, &context); | 1097 mojo::internal::Deserialize<HandleUnionPtr>(data, &handle2, &context); |
1064 | 1098 |
1065 std::string golden("hello world"); | 1099 std::string golden("hello world"); |
1066 WriteTextMessage(pipe0.get(), golden); | 1100 WriteTextMessage(pipe0.get(), golden); |
1067 | 1101 |
1068 std::string actual; | 1102 std::string actual; |
1069 ReadTextMessage(handle2->get_f_message_pipe().get(), &actual); | 1103 ReadTextMessage(handle2->get_f_message_pipe().get(), &actual); |
1070 | 1104 |
1071 EXPECT_EQ(golden, actual); | 1105 EXPECT_EQ(golden, actual); |
1072 } | 1106 } |
1073 | 1107 |
1074 TEST(UnionTest, HandleInUnionValidation) { | 1108 TEST(UnionTest, HandleInUnionValidation) { |
1075 ScopedMessagePipeHandle pipe0; | 1109 ScopedMessagePipeHandle pipe0; |
1076 ScopedMessagePipeHandle pipe1; | 1110 ScopedMessagePipeHandle pipe1; |
1077 | 1111 |
1078 CreateMessagePipe(nullptr, &pipe0, &pipe1); | 1112 CreateMessagePipe(nullptr, &pipe0, &pipe1); |
1079 | 1113 |
1080 HandleUnionPtr handle(HandleUnion::New()); | 1114 HandleUnionPtr handle(HandleUnion::New()); |
1081 handle->set_f_message_pipe(std::move(pipe1)); | 1115 handle->set_f_message_pipe(std::move(pipe1)); |
1082 | 1116 |
1083 mojo::internal::SerializationContext context; | 1117 mojo::internal::SerializationContext context; |
1084 size_t size = GetSerializedSize_(handle, false, &context); | 1118 size_t size = mojo::internal::PrepareToSerialize<HandleUnionPtr>( |
| 1119 handle, false, &context); |
1085 EXPECT_EQ(16U, size); | 1120 EXPECT_EQ(16U, size); |
1086 | 1121 |
1087 mojo::internal::FixedBufferForTesting buf(size); | 1122 mojo::internal::FixedBufferForTesting buf(size); |
1088 internal::HandleUnion_Data* data = nullptr; | 1123 internal::HandleUnion_Data* data = nullptr; |
1089 SerializeUnion_(std::move(handle), &buf, &data, false, &context); | 1124 mojo::internal::Serialize<HandleUnionPtr>(handle, &buf, &data, false, |
| 1125 &context); |
1090 | 1126 |
1091 void* raw_buf = buf.Leak(); | 1127 void* raw_buf = buf.Leak(); |
1092 mojo::internal::BoundsChecker bounds_checker(data, | 1128 mojo::internal::BoundsChecker bounds_checker(data, |
1093 static_cast<uint32_t>(size), 1); | 1129 static_cast<uint32_t>(size), 1); |
1094 EXPECT_TRUE( | 1130 EXPECT_TRUE( |
1095 internal::HandleUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 1131 internal::HandleUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
1096 free(raw_buf); | 1132 free(raw_buf); |
1097 } | 1133 } |
1098 | 1134 |
1099 TEST(UnionTest, HandleInUnionValidationNull) { | 1135 TEST(UnionTest, HandleInUnionValidationNull) { |
1100 ScopedMessagePipeHandle pipe; | 1136 ScopedMessagePipeHandle pipe; |
1101 HandleUnionPtr handle(HandleUnion::New()); | 1137 HandleUnionPtr handle(HandleUnion::New()); |
1102 handle->set_f_message_pipe(std::move(pipe)); | 1138 handle->set_f_message_pipe(std::move(pipe)); |
1103 | 1139 |
1104 mojo::internal::SerializationContext context; | 1140 mojo::internal::SerializationContext context; |
1105 size_t size = GetSerializedSize_(handle, false, &context); | 1141 size_t size = mojo::internal::PrepareToSerialize<HandleUnionPtr>( |
| 1142 handle, false, &context); |
1106 EXPECT_EQ(16U, size); | 1143 EXPECT_EQ(16U, size); |
1107 | 1144 |
1108 mojo::internal::FixedBufferForTesting buf(size); | 1145 mojo::internal::FixedBufferForTesting buf(size); |
1109 internal::HandleUnion_Data* data = nullptr; | 1146 internal::HandleUnion_Data* data = nullptr; |
1110 SerializeUnion_(std::move(handle), &buf, &data, false, &context); | 1147 mojo::internal::Serialize<HandleUnionPtr>(handle, &buf, &data, false, |
| 1148 &context); |
1111 | 1149 |
1112 void* raw_buf = buf.Leak(); | 1150 void* raw_buf = buf.Leak(); |
1113 mojo::internal::BoundsChecker bounds_checker(data, | 1151 mojo::internal::BoundsChecker bounds_checker(data, |
1114 static_cast<uint32_t>(size), 1); | 1152 static_cast<uint32_t>(size), 1); |
1115 EXPECT_FALSE( | 1153 EXPECT_FALSE( |
1116 internal::HandleUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 1154 internal::HandleUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
1117 free(raw_buf); | 1155 free(raw_buf); |
1118 } | 1156 } |
1119 | 1157 |
1120 class SmallCacheImpl : public SmallCache { | 1158 class SmallCacheImpl : public SmallCache { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1155 TEST(UnionTest, InterfaceInUnionSerialization) { | 1193 TEST(UnionTest, InterfaceInUnionSerialization) { |
1156 base::MessageLoop message_loop; | 1194 base::MessageLoop message_loop; |
1157 base::RunLoop run_loop; | 1195 base::RunLoop run_loop; |
1158 SmallCacheImpl impl(run_loop.QuitClosure()); | 1196 SmallCacheImpl impl(run_loop.QuitClosure()); |
1159 SmallCachePtr ptr; | 1197 SmallCachePtr ptr; |
1160 Binding<SmallCache> bindings(&impl, GetProxy(&ptr)); | 1198 Binding<SmallCache> bindings(&impl, GetProxy(&ptr)); |
1161 | 1199 |
1162 mojo::internal::SerializationContext context; | 1200 mojo::internal::SerializationContext context; |
1163 HandleUnionPtr handle(HandleUnion::New()); | 1201 HandleUnionPtr handle(HandleUnion::New()); |
1164 handle->set_f_small_cache(std::move(ptr)); | 1202 handle->set_f_small_cache(std::move(ptr)); |
1165 size_t size = GetSerializedSize_(handle, false, &context); | 1203 size_t size = mojo::internal::PrepareToSerialize<HandleUnionPtr>( |
| 1204 handle, false, &context); |
1166 EXPECT_EQ(16U, size); | 1205 EXPECT_EQ(16U, size); |
1167 | 1206 |
1168 mojo::internal::FixedBufferForTesting buf(size); | 1207 mojo::internal::FixedBufferForTesting buf(size); |
1169 internal::HandleUnion_Data* data = nullptr; | 1208 internal::HandleUnion_Data* data = nullptr; |
1170 SerializeUnion_(std::move(handle), &buf, &data, false, &context); | 1209 mojo::internal::Serialize<HandleUnionPtr>(handle, &buf, &data, false, |
| 1210 &context); |
1171 EXPECT_EQ(1U, context.handles.size()); | 1211 EXPECT_EQ(1U, context.handles.size()); |
1172 | 1212 |
1173 HandleUnionPtr handle2(HandleUnion::New()); | 1213 HandleUnionPtr handle2(HandleUnion::New()); |
1174 Deserialize_(data, &handle2, &context); | 1214 mojo::internal::Deserialize<HandleUnionPtr>(data, &handle2, &context); |
1175 | 1215 |
1176 handle2->get_f_small_cache()->SetIntValue(10); | 1216 handle2->get_f_small_cache()->SetIntValue(10); |
1177 run_loop.Run(); | 1217 run_loop.Run(); |
1178 EXPECT_EQ(10, impl.int_value()); | 1218 EXPECT_EQ(10, impl.int_value()); |
1179 } | 1219 } |
1180 | 1220 |
1181 class UnionInterfaceImpl : public UnionInterface { | 1221 class UnionInterfaceImpl : public UnionInterface { |
1182 public: | 1222 public: |
1183 UnionInterfaceImpl() {} | 1223 UnionInterfaceImpl() {} |
1184 ~UnionInterfaceImpl() override {} | 1224 ~UnionInterfaceImpl() override {} |
(...skipping 13 matching lines...) Expand all Loading... |
1198 PodUnionPtr pod(PodUnion::New()); | 1238 PodUnionPtr pod(PodUnion::New()); |
1199 pod->set_f_int16(16); | 1239 pod->set_f_int16(16); |
1200 | 1240 |
1201 ptr->Echo(std::move(pod), | 1241 ptr->Echo(std::move(pod), |
1202 [](PodUnionPtr out) { EXPECT_EQ(16, out->get_f_int16()); }); | 1242 [](PodUnionPtr out) { EXPECT_EQ(16, out->get_f_int16()); }); |
1203 run_loop.RunUntilIdle(); | 1243 run_loop.RunUntilIdle(); |
1204 } | 1244 } |
1205 | 1245 |
1206 } // namespace test | 1246 } // namespace test |
1207 } // namespace mojo | 1247 } // namespace mojo |
OLD | NEW |