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 | 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" |
11 #include "mojo/message_pump/message_pump_mojo.h" | 11 #include "mojo/message_pump/message_pump_mojo.h" |
12 #include "mojo/public/cpp/bindings/array.h" | 12 #include "mojo/public/cpp/bindings/array.h" |
13 #include "mojo/public/cpp/bindings/binding.h" | 13 #include "mojo/public/cpp/bindings/binding.h" |
14 #include "mojo/public/cpp/bindings/lib/array_internal.h" | 14 #include "mojo/public/cpp/bindings/lib/array_internal.h" |
15 #include "mojo/public/cpp/bindings/lib/array_serialization.h" | 15 #include "mojo/public/cpp/bindings/lib/array_serialization.h" |
16 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" | 16 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" |
17 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" | 17 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 size_t size = GetSerializedSize_(pod1, false); | 124 size_t size = GetSerializedSize_(pod1, false); |
125 EXPECT_EQ(16U, size); | 125 EXPECT_EQ(16U, size); |
126 | 126 |
127 mojo::internal::FixedBufferForTesting buf(size); | 127 mojo::internal::FixedBufferForTesting buf(size); |
128 internal::PodUnion_Data* data = nullptr; | 128 internal::PodUnion_Data* data = nullptr; |
129 SerializeUnion_(pod1.Pass(), &buf, &data, false); | 129 SerializeUnion_(std::move(pod1), &buf, &data, false); |
130 | 130 |
131 PodUnionPtr pod2; | 131 PodUnionPtr pod2; |
132 Deserialize_(data, &pod2, nullptr); | 132 Deserialize_(data, &pod2, nullptr); |
133 | 133 |
134 EXPECT_EQ(10, pod2->get_f_int8()); | 134 EXPECT_EQ(10, pod2->get_f_int8()); |
135 EXPECT_TRUE(pod2->is_f_int8()); | 135 EXPECT_TRUE(pod2->is_f_int8()); |
136 EXPECT_EQ(pod2->which(), PodUnion::Tag::F_INT8); | 136 EXPECT_EQ(pod2->which(), PodUnion::Tag::F_INT8); |
137 } | 137 } |
138 | 138 |
139 TEST(UnionTest, EnumSerialization) { | 139 TEST(UnionTest, EnumSerialization) { |
140 PodUnionPtr pod1(PodUnion::New()); | 140 PodUnionPtr pod1(PodUnion::New()); |
141 pod1->set_f_enum(AN_ENUM_SECOND); | 141 pod1->set_f_enum(AN_ENUM_SECOND); |
142 | 142 |
143 size_t size = GetSerializedSize_(pod1, false); | 143 size_t size = GetSerializedSize_(pod1, false); |
144 EXPECT_EQ(16U, size); | 144 EXPECT_EQ(16U, size); |
145 | 145 |
146 mojo::internal::FixedBufferForTesting buf(size); | 146 mojo::internal::FixedBufferForTesting buf(size); |
147 internal::PodUnion_Data* data = nullptr; | 147 internal::PodUnion_Data* data = nullptr; |
148 SerializeUnion_(pod1.Pass(), &buf, &data, false); | 148 SerializeUnion_(std::move(pod1), &buf, &data, false); |
149 | 149 |
150 PodUnionPtr pod2; | 150 PodUnionPtr pod2; |
151 Deserialize_(data, &pod2, nullptr); | 151 Deserialize_(data, &pod2, nullptr); |
152 | 152 |
153 EXPECT_EQ(AN_ENUM_SECOND, pod2->get_f_enum()); | 153 EXPECT_EQ(AN_ENUM_SECOND, pod2->get_f_enum()); |
154 EXPECT_TRUE(pod2->is_f_enum()); | 154 EXPECT_TRUE(pod2->is_f_enum()); |
155 EXPECT_EQ(pod2->which(), PodUnion::Tag::F_ENUM); | 155 EXPECT_EQ(pod2->which(), PodUnion::Tag::F_ENUM); |
156 } | 156 } |
157 | 157 |
158 TEST(UnionTest, PodValidation) { | 158 TEST(UnionTest, PodValidation) { |
159 PodUnionPtr pod(PodUnion::New()); | 159 PodUnionPtr pod(PodUnion::New()); |
160 pod->set_f_int8(10); | 160 pod->set_f_int8(10); |
161 | 161 |
162 size_t size = GetSerializedSize_(pod, false); | 162 size_t size = GetSerializedSize_(pod, false); |
163 EXPECT_EQ(16U, size); | 163 EXPECT_EQ(16U, size); |
164 | 164 |
165 mojo::internal::FixedBufferForTesting buf(size); | 165 mojo::internal::FixedBufferForTesting buf(size); |
166 internal::PodUnion_Data* data = nullptr; | 166 internal::PodUnion_Data* data = nullptr; |
167 SerializeUnion_(pod.Pass(), &buf, &data, false); | 167 SerializeUnion_(std::move(pod), &buf, &data, false); |
168 std::vector<Handle> handles; | 168 std::vector<Handle> handles; |
169 data->EncodePointersAndHandles(&handles); | 169 data->EncodePointersAndHandles(&handles); |
170 EXPECT_TRUE(handles.empty()); | 170 EXPECT_TRUE(handles.empty()); |
171 | 171 |
172 void* raw_buf = buf.Leak(); | 172 void* raw_buf = buf.Leak(); |
173 mojo::internal::BoundsChecker bounds_checker(data, | 173 mojo::internal::BoundsChecker bounds_checker(data, |
174 static_cast<uint32_t>(size), 0); | 174 static_cast<uint32_t>(size), 0); |
175 EXPECT_TRUE( | 175 EXPECT_TRUE( |
176 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 176 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
177 free(raw_buf); | 177 free(raw_buf); |
178 } | 178 } |
179 | 179 |
180 TEST(UnionTest, SerializeNotNull) { | 180 TEST(UnionTest, SerializeNotNull) { |
181 PodUnionPtr pod(PodUnion::New()); | 181 PodUnionPtr pod(PodUnion::New()); |
182 pod->set_f_int8(0); | 182 pod->set_f_int8(0); |
183 size_t size = GetSerializedSize_(pod, false); | 183 size_t size = GetSerializedSize_(pod, false); |
184 mojo::internal::FixedBufferForTesting buf(size); | 184 mojo::internal::FixedBufferForTesting buf(size); |
185 internal::PodUnion_Data* data = nullptr; | 185 internal::PodUnion_Data* data = nullptr; |
186 SerializeUnion_(pod.Pass(), &buf, &data, false); | 186 SerializeUnion_(std::move(pod), &buf, &data, false); |
187 EXPECT_FALSE(data->is_null()); | 187 EXPECT_FALSE(data->is_null()); |
188 } | 188 } |
189 | 189 |
190 TEST(UnionTest, SerializeIsNullInlined) { | 190 TEST(UnionTest, SerializeIsNullInlined) { |
191 PodUnionPtr pod; | 191 PodUnionPtr pod; |
192 size_t size = GetSerializedSize_(pod, false); | 192 size_t size = GetSerializedSize_(pod, false); |
193 EXPECT_EQ(16U, size); | 193 EXPECT_EQ(16U, size); |
194 mojo::internal::FixedBufferForTesting buf(size); | 194 mojo::internal::FixedBufferForTesting buf(size); |
195 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf); | 195 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf); |
196 | 196 |
197 // Check that dirty output buffers are handled correctly by serialization. | 197 // Check that dirty output buffers are handled correctly by serialization. |
198 data->size = 16U; | 198 data->size = 16U; |
199 data->tag = PodUnion::Tag::F_UINT16; | 199 data->tag = PodUnion::Tag::F_UINT16; |
200 data->data.f_f_int16 = 20; | 200 data->data.f_f_int16 = 20; |
201 | 201 |
202 SerializeUnion_(pod.Pass(), &buf, &data, true); | 202 SerializeUnion_(std::move(pod), &buf, &data, true); |
203 EXPECT_TRUE(data->is_null()); | 203 EXPECT_TRUE(data->is_null()); |
204 | 204 |
205 PodUnionPtr pod2; | 205 PodUnionPtr pod2; |
206 Deserialize_(data, &pod2, nullptr); | 206 Deserialize_(data, &pod2, nullptr); |
207 EXPECT_TRUE(pod2.is_null()); | 207 EXPECT_TRUE(pod2.is_null()); |
208 } | 208 } |
209 | 209 |
210 TEST(UnionTest, SerializeIsNullNotInlined) { | 210 TEST(UnionTest, SerializeIsNullNotInlined) { |
211 PodUnionPtr pod; | 211 PodUnionPtr pod; |
212 size_t size = GetSerializedSize_(pod, false); | 212 size_t size = GetSerializedSize_(pod, false); |
213 EXPECT_EQ(16U, size); | 213 EXPECT_EQ(16U, size); |
214 mojo::internal::FixedBufferForTesting buf(size); | 214 mojo::internal::FixedBufferForTesting buf(size); |
215 internal::PodUnion_Data* data = nullptr; | 215 internal::PodUnion_Data* data = nullptr; |
216 SerializeUnion_(pod.Pass(), &buf, &data, false); | 216 SerializeUnion_(std::move(pod), &buf, &data, false); |
217 EXPECT_EQ(nullptr, data); | 217 EXPECT_EQ(nullptr, data); |
218 } | 218 } |
219 | 219 |
220 TEST(UnionTest, NullValidation) { | 220 TEST(UnionTest, NullValidation) { |
221 void* buf = nullptr; | 221 void* buf = nullptr; |
222 mojo::internal::BoundsChecker bounds_checker(buf, 0, 0); | 222 mojo::internal::BoundsChecker bounds_checker(buf, 0, 0); |
223 EXPECT_TRUE(internal::PodUnion_Data::Validate(buf, &bounds_checker, false)); | 223 EXPECT_TRUE(internal::PodUnion_Data::Validate(buf, &bounds_checker, false)); |
224 } | 224 } |
225 | 225 |
226 TEST(UnionTest, OutOfAlignmentValidation) { | 226 TEST(UnionTest, OutOfAlignmentValidation) { |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 | 298 |
299 TEST(UnionTest, StringSerialization) { | 299 TEST(UnionTest, StringSerialization) { |
300 ObjectUnionPtr pod1(ObjectUnion::New()); | 300 ObjectUnionPtr pod1(ObjectUnion::New()); |
301 | 301 |
302 String hello("hello world"); | 302 String hello("hello world"); |
303 pod1->set_f_string(hello); | 303 pod1->set_f_string(hello); |
304 | 304 |
305 size_t size = GetSerializedSize_(pod1, false); | 305 size_t size = GetSerializedSize_(pod1, false); |
306 mojo::internal::FixedBufferForTesting buf(size); | 306 mojo::internal::FixedBufferForTesting buf(size); |
307 internal::ObjectUnion_Data* data = nullptr; | 307 internal::ObjectUnion_Data* data = nullptr; |
308 SerializeUnion_(pod1.Pass(), &buf, &data, false); | 308 SerializeUnion_(std::move(pod1), &buf, &data, false); |
309 | 309 |
310 std::vector<Handle> handles; | 310 std::vector<Handle> handles; |
311 data->EncodePointersAndHandles(&handles); | 311 data->EncodePointersAndHandles(&handles); |
312 data->DecodePointersAndHandles(&handles); | 312 data->DecodePointersAndHandles(&handles); |
313 | 313 |
314 ObjectUnionPtr pod2; | 314 ObjectUnionPtr pod2; |
315 Deserialize_(data, &pod2, nullptr); | 315 Deserialize_(data, &pod2, nullptr); |
316 EXPECT_EQ(hello, pod2->get_f_string()); | 316 EXPECT_EQ(hello, pod2->get_f_string()); |
317 EXPECT_TRUE(pod2->is_f_string()); | 317 EXPECT_TRUE(pod2->is_f_string()); |
318 EXPECT_EQ(pod2->which(), ObjectUnion::Tag::F_STRING); | 318 EXPECT_EQ(pod2->which(), ObjectUnion::Tag::F_STRING); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 array[0]->set_f_int8(10); | 388 array[0]->set_f_int8(10); |
389 array[1]->set_f_int16(12); | 389 array[1]->set_f_int16(12); |
390 EXPECT_EQ(2U, array.size()); | 390 EXPECT_EQ(2U, array.size()); |
391 | 391 |
392 size_t size = GetSerializedSize_(array); | 392 size_t size = GetSerializedSize_(array); |
393 EXPECT_EQ(40U, size); | 393 EXPECT_EQ(40U, size); |
394 | 394 |
395 mojo::internal::FixedBufferForTesting buf(size); | 395 mojo::internal::FixedBufferForTesting buf(size); |
396 mojo::internal::Array_Data<internal::PodUnion_Data>* data; | 396 mojo::internal::Array_Data<internal::PodUnion_Data>* data; |
397 mojo::internal::ArrayValidateParams validate_params(0, false, nullptr); | 397 mojo::internal::ArrayValidateParams validate_params(0, false, nullptr); |
398 SerializeArray_(array.Pass(), &buf, &data, &validate_params); | 398 SerializeArray_(std::move(array), &buf, &data, &validate_params); |
399 | 399 |
400 Array<PodUnionPtr> array2; | 400 Array<PodUnionPtr> array2; |
401 Deserialize_(data, &array2, nullptr); | 401 Deserialize_(data, &array2, nullptr); |
402 | 402 |
403 EXPECT_EQ(2U, array2.size()); | 403 EXPECT_EQ(2U, array2.size()); |
404 | 404 |
405 EXPECT_EQ(10, array2[0]->get_f_int8()); | 405 EXPECT_EQ(10, array2[0]->get_f_int8()); |
406 EXPECT_EQ(12, array2[1]->get_f_int16()); | 406 EXPECT_EQ(12, array2[1]->get_f_int16()); |
407 } | 407 } |
408 | 408 |
409 TEST(UnionTest, PodUnionInArraySerializationWithNull) { | 409 TEST(UnionTest, PodUnionInArraySerializationWithNull) { |
410 Array<PodUnionPtr> array(2); | 410 Array<PodUnionPtr> array(2); |
411 array[0] = PodUnion::New(); | 411 array[0] = PodUnion::New(); |
412 | 412 |
413 array[0]->set_f_int8(10); | 413 array[0]->set_f_int8(10); |
414 EXPECT_EQ(2U, array.size()); | 414 EXPECT_EQ(2U, array.size()); |
415 | 415 |
416 size_t size = GetSerializedSize_(array); | 416 size_t size = GetSerializedSize_(array); |
417 EXPECT_EQ(40U, size); | 417 EXPECT_EQ(40U, size); |
418 | 418 |
419 mojo::internal::FixedBufferForTesting buf(size); | 419 mojo::internal::FixedBufferForTesting buf(size); |
420 mojo::internal::Array_Data<internal::PodUnion_Data>* data; | 420 mojo::internal::Array_Data<internal::PodUnion_Data>* data; |
421 mojo::internal::ArrayValidateParams validate_params(0, true, nullptr); | 421 mojo::internal::ArrayValidateParams validate_params(0, true, nullptr); |
422 SerializeArray_(array.Pass(), &buf, &data, &validate_params); | 422 SerializeArray_(std::move(array), &buf, &data, &validate_params); |
423 | 423 |
424 Array<PodUnionPtr> array2; | 424 Array<PodUnionPtr> array2; |
425 Deserialize_(data, &array2, nullptr); | 425 Deserialize_(data, &array2, nullptr); |
426 | 426 |
427 EXPECT_EQ(2U, array2.size()); | 427 EXPECT_EQ(2U, array2.size()); |
428 | 428 |
429 EXPECT_EQ(10, array2[0]->get_f_int8()); | 429 EXPECT_EQ(10, array2[0]->get_f_int8()); |
430 EXPECT_TRUE(array2[1].is_null()); | 430 EXPECT_TRUE(array2[1].is_null()); |
431 } | 431 } |
432 | 432 |
(...skipping 11 matching lines...) Expand all Loading... |
444 // Serialization test of a struct with a union of plain old data. | 444 // Serialization test of a struct with a union of plain old data. |
445 TEST(UnionTest, Serialization_UnionOfPods) { | 445 TEST(UnionTest, Serialization_UnionOfPods) { |
446 SmallStructPtr small_struct(SmallStruct::New()); | 446 SmallStructPtr small_struct(SmallStruct::New()); |
447 small_struct->pod_union = PodUnion::New(); | 447 small_struct->pod_union = PodUnion::New(); |
448 small_struct->pod_union->set_f_int32(10); | 448 small_struct->pod_union->set_f_int32(10); |
449 | 449 |
450 size_t size = GetSerializedSize_(small_struct); | 450 size_t size = GetSerializedSize_(small_struct); |
451 | 451 |
452 mojo::internal::FixedBufferForTesting buf(size); | 452 mojo::internal::FixedBufferForTesting buf(size); |
453 internal::SmallStruct_Data* data = nullptr; | 453 internal::SmallStruct_Data* data = nullptr; |
454 Serialize_(small_struct.Pass(), &buf, &data); | 454 Serialize_(std::move(small_struct), &buf, &data); |
455 | 455 |
456 SmallStructPtr deserialized; | 456 SmallStructPtr deserialized; |
457 Deserialize_(data, &deserialized, nullptr); | 457 Deserialize_(data, &deserialized, nullptr); |
458 | 458 |
459 EXPECT_EQ(10, deserialized->pod_union->get_f_int32()); | 459 EXPECT_EQ(10, deserialized->pod_union->get_f_int32()); |
460 } | 460 } |
461 | 461 |
462 // Serialization test of a struct with a union of structs. | 462 // Serialization test of a struct with a union of structs. |
463 TEST(UnionTest, Serialization_UnionOfObjects) { | 463 TEST(UnionTest, Serialization_UnionOfObjects) { |
464 SmallObjStructPtr obj_struct(SmallObjStruct::New()); | 464 SmallObjStructPtr obj_struct(SmallObjStruct::New()); |
465 obj_struct->obj_union = ObjectUnion::New(); | 465 obj_struct->obj_union = ObjectUnion::New(); |
466 String hello("hello world"); | 466 String hello("hello world"); |
467 obj_struct->obj_union->set_f_string(hello); | 467 obj_struct->obj_union->set_f_string(hello); |
468 | 468 |
469 size_t size = GetSerializedSize_(obj_struct); | 469 size_t size = GetSerializedSize_(obj_struct); |
470 | 470 |
471 mojo::internal::FixedBufferForTesting buf(size); | 471 mojo::internal::FixedBufferForTesting buf(size); |
472 internal::SmallObjStruct_Data* data = nullptr; | 472 internal::SmallObjStruct_Data* data = nullptr; |
473 Serialize_(obj_struct.Pass(), &buf, &data); | 473 Serialize_(std::move(obj_struct), &buf, &data); |
474 | 474 |
475 std::vector<Handle> handles; | 475 std::vector<Handle> handles; |
476 data->EncodePointersAndHandles(&handles); | 476 data->EncodePointersAndHandles(&handles); |
477 data->DecodePointersAndHandles(&handles); | 477 data->DecodePointersAndHandles(&handles); |
478 | 478 |
479 SmallObjStructPtr deserialized; | 479 SmallObjStructPtr deserialized; |
480 Deserialize_(data, &deserialized, nullptr); | 480 Deserialize_(data, &deserialized, nullptr); |
481 | 481 |
482 EXPECT_EQ(hello, deserialized->obj_union->get_f_string()); | 482 EXPECT_EQ(hello, deserialized->obj_union->get_f_string()); |
483 } | 483 } |
484 | 484 |
485 // Validation test of a struct with a union. | 485 // Validation test of a struct with a union. |
486 TEST(UnionTest, Validation_UnionsInStruct) { | 486 TEST(UnionTest, Validation_UnionsInStruct) { |
487 SmallStructPtr small_struct(SmallStruct::New()); | 487 SmallStructPtr small_struct(SmallStruct::New()); |
488 small_struct->pod_union = PodUnion::New(); | 488 small_struct->pod_union = PodUnion::New(); |
489 small_struct->pod_union->set_f_int32(10); | 489 small_struct->pod_union->set_f_int32(10); |
490 | 490 |
491 size_t size = GetSerializedSize_(small_struct); | 491 size_t size = GetSerializedSize_(small_struct); |
492 | 492 |
493 mojo::internal::FixedBufferForTesting buf(size); | 493 mojo::internal::FixedBufferForTesting buf(size); |
494 internal::SmallStruct_Data* data = nullptr; | 494 internal::SmallStruct_Data* data = nullptr; |
495 Serialize_(small_struct.Pass(), &buf, &data); | 495 Serialize_(std::move(small_struct), &buf, &data); |
496 | 496 |
497 std::vector<Handle> handles; | 497 std::vector<Handle> handles; |
498 data->EncodePointersAndHandles(&handles); | 498 data->EncodePointersAndHandles(&handles); |
499 EXPECT_TRUE(handles.empty()); | 499 EXPECT_TRUE(handles.empty()); |
500 | 500 |
501 void* raw_buf = buf.Leak(); | 501 void* raw_buf = buf.Leak(); |
502 mojo::internal::BoundsChecker bounds_checker(data, | 502 mojo::internal::BoundsChecker bounds_checker(data, |
503 static_cast<uint32_t>(size), 0); | 503 static_cast<uint32_t>(size), 0); |
504 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); | 504 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); |
505 free(raw_buf); | 505 free(raw_buf); |
506 } | 506 } |
507 | 507 |
508 // Validation test of a struct union fails due to unknown union tag. | 508 // Validation test of a struct union fails due to unknown union tag. |
509 TEST(UnionTest, Validation_PodUnionInStruct_Failure) { | 509 TEST(UnionTest, Validation_PodUnionInStruct_Failure) { |
510 SmallStructPtr small_struct(SmallStruct::New()); | 510 SmallStructPtr small_struct(SmallStruct::New()); |
511 small_struct->pod_union = PodUnion::New(); | 511 small_struct->pod_union = PodUnion::New(); |
512 small_struct->pod_union->set_f_int32(10); | 512 small_struct->pod_union->set_f_int32(10); |
513 | 513 |
514 size_t size = GetSerializedSize_(small_struct); | 514 size_t size = GetSerializedSize_(small_struct); |
515 | 515 |
516 mojo::internal::FixedBufferForTesting buf(size); | 516 mojo::internal::FixedBufferForTesting buf(size); |
517 internal::SmallStruct_Data* data = nullptr; | 517 internal::SmallStruct_Data* data = nullptr; |
518 Serialize_(small_struct.Pass(), &buf, &data); | 518 Serialize_(std::move(small_struct), &buf, &data); |
519 data->pod_union.tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(100); | 519 data->pod_union.tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(100); |
520 | 520 |
521 std::vector<Handle> handles; | 521 std::vector<Handle> handles; |
522 data->EncodePointersAndHandles(&handles); | 522 data->EncodePointersAndHandles(&handles); |
523 EXPECT_TRUE(handles.empty()); | 523 EXPECT_TRUE(handles.empty()); |
524 | 524 |
525 void* raw_buf = buf.Leak(); | 525 void* raw_buf = buf.Leak(); |
526 mojo::internal::BoundsChecker bounds_checker(data, | 526 mojo::internal::BoundsChecker bounds_checker(data, |
527 static_cast<uint32_t>(size), 0); | 527 static_cast<uint32_t>(size), 0); |
528 EXPECT_FALSE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); | 528 EXPECT_FALSE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); |
(...skipping 20 matching lines...) Expand all Loading... |
549 } | 549 } |
550 | 550 |
551 // Validation passes with nullable null union. | 551 // Validation passes with nullable null union. |
552 TEST(UnionTest, Validation_NullableUnion) { | 552 TEST(UnionTest, Validation_NullableUnion) { |
553 SmallStructPtr small_struct(SmallStruct::New()); | 553 SmallStructPtr small_struct(SmallStruct::New()); |
554 | 554 |
555 size_t size = GetSerializedSize_(small_struct); | 555 size_t size = GetSerializedSize_(small_struct); |
556 | 556 |
557 mojo::internal::FixedBufferForTesting buf(size); | 557 mojo::internal::FixedBufferForTesting buf(size); |
558 internal::SmallStruct_Data* data = nullptr; | 558 internal::SmallStruct_Data* data = nullptr; |
559 Serialize_(small_struct.Pass(), &buf, &data); | 559 Serialize_(std::move(small_struct), &buf, &data); |
560 | 560 |
561 std::vector<Handle> handles; | 561 std::vector<Handle> handles; |
562 data->EncodePointersAndHandles(&handles); | 562 data->EncodePointersAndHandles(&handles); |
563 EXPECT_TRUE(handles.empty()); | 563 EXPECT_TRUE(handles.empty()); |
564 | 564 |
565 void* raw_buf = buf.Leak(); | 565 void* raw_buf = buf.Leak(); |
566 mojo::internal::BoundsChecker bounds_checker(data, | 566 mojo::internal::BoundsChecker bounds_checker(data, |
567 static_cast<uint32_t>(size), 0); | 567 static_cast<uint32_t>(size), 0); |
568 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); | 568 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); |
569 free(raw_buf); | 569 free(raw_buf); |
(...skipping 22 matching lines...) Expand all Loading... |
592 map["one"]->set_f_int8(8); | 592 map["one"]->set_f_int8(8); |
593 map["two"]->set_f_int16(16); | 593 map["two"]->set_f_int16(16); |
594 | 594 |
595 size_t size = GetSerializedSize_(map); | 595 size_t size = GetSerializedSize_(map); |
596 EXPECT_EQ(120U, size); | 596 EXPECT_EQ(120U, size); |
597 | 597 |
598 mojo::internal::FixedBufferForTesting buf(size); | 598 mojo::internal::FixedBufferForTesting buf(size); |
599 mojo::internal::Map_Data<mojo::internal::String_Data*, | 599 mojo::internal::Map_Data<mojo::internal::String_Data*, |
600 internal::PodUnion_Data>* data; | 600 internal::PodUnion_Data>* data; |
601 mojo::internal::ArrayValidateParams validate_params(0, false, nullptr); | 601 mojo::internal::ArrayValidateParams validate_params(0, false, nullptr); |
602 SerializeMap_(map.Pass(), &buf, &data, &validate_params); | 602 SerializeMap_(std::move(map), &buf, &data, &validate_params); |
603 | 603 |
604 Map<String, PodUnionPtr> map2; | 604 Map<String, PodUnionPtr> map2; |
605 Deserialize_(data, &map2, nullptr); | 605 Deserialize_(data, &map2, nullptr); |
606 | 606 |
607 EXPECT_EQ(8, map2["one"]->get_f_int8()); | 607 EXPECT_EQ(8, map2["one"]->get_f_int8()); |
608 EXPECT_EQ(16, map2["two"]->get_f_int16()); | 608 EXPECT_EQ(16, map2["two"]->get_f_int16()); |
609 } | 609 } |
610 | 610 |
611 TEST(UnionTest, PodUnionInMapSerializationWithNull) { | 611 TEST(UnionTest, PodUnionInMapSerializationWithNull) { |
612 Map<String, PodUnionPtr> map; | 612 Map<String, PodUnionPtr> map; |
613 map.insert("one", PodUnion::New()); | 613 map.insert("one", PodUnion::New()); |
614 map.insert("two", nullptr); | 614 map.insert("two", nullptr); |
615 | 615 |
616 map["one"]->set_f_int8(8); | 616 map["one"]->set_f_int8(8); |
617 | 617 |
618 size_t size = GetSerializedSize_(map); | 618 size_t size = GetSerializedSize_(map); |
619 EXPECT_EQ(120U, size); | 619 EXPECT_EQ(120U, size); |
620 | 620 |
621 mojo::internal::FixedBufferForTesting buf(size); | 621 mojo::internal::FixedBufferForTesting buf(size); |
622 mojo::internal::Map_Data<mojo::internal::String_Data*, | 622 mojo::internal::Map_Data<mojo::internal::String_Data*, |
623 internal::PodUnion_Data>* data; | 623 internal::PodUnion_Data>* data; |
624 mojo::internal::ArrayValidateParams validate_params(0, true, nullptr); | 624 mojo::internal::ArrayValidateParams validate_params(0, true, nullptr); |
625 SerializeMap_(map.Pass(), &buf, &data, &validate_params); | 625 SerializeMap_(std::move(map), &buf, &data, &validate_params); |
626 | 626 |
627 Map<String, PodUnionPtr> map2; | 627 Map<String, PodUnionPtr> map2; |
628 Deserialize_(data, &map2, nullptr); | 628 Deserialize_(data, &map2, nullptr); |
629 | 629 |
630 EXPECT_EQ(8, map2["one"]->get_f_int8()); | 630 EXPECT_EQ(8, map2["one"]->get_f_int8()); |
631 EXPECT_TRUE(map2["two"].is_null()); | 631 EXPECT_TRUE(map2["two"].is_null()); |
632 } | 632 } |
633 | 633 |
634 TEST(UnionTest, StructInUnionGetterSetterPasser) { | 634 TEST(UnionTest, StructInUnionGetterSetterPasser) { |
635 DummyStructPtr dummy(DummyStruct::New()); | 635 DummyStructPtr dummy(DummyStruct::New()); |
636 dummy->f_int8 = 8; | 636 dummy->f_int8 = 8; |
637 | 637 |
638 ObjectUnionPtr obj(ObjectUnion::New()); | 638 ObjectUnionPtr obj(ObjectUnion::New()); |
639 obj->set_f_dummy(dummy.Pass()); | 639 obj->set_f_dummy(std::move(dummy)); |
640 | 640 |
641 EXPECT_EQ(8, obj->get_f_dummy()->f_int8); | 641 EXPECT_EQ(8, obj->get_f_dummy()->f_int8); |
642 } | 642 } |
643 | 643 |
644 TEST(UnionTest, StructInUnionSerialization) { | 644 TEST(UnionTest, StructInUnionSerialization) { |
645 DummyStructPtr dummy(DummyStruct::New()); | 645 DummyStructPtr dummy(DummyStruct::New()); |
646 dummy->f_int8 = 8; | 646 dummy->f_int8 = 8; |
647 | 647 |
648 ObjectUnionPtr obj(ObjectUnion::New()); | 648 ObjectUnionPtr obj(ObjectUnion::New()); |
649 obj->set_f_dummy(dummy.Pass()); | 649 obj->set_f_dummy(std::move(dummy)); |
650 | 650 |
651 size_t size = GetSerializedSize_(obj, false); | 651 size_t size = GetSerializedSize_(obj, false); |
652 EXPECT_EQ(32U, size); | 652 EXPECT_EQ(32U, size); |
653 | 653 |
654 mojo::internal::FixedBufferForTesting buf(size); | 654 mojo::internal::FixedBufferForTesting buf(size); |
655 internal::ObjectUnion_Data* data = nullptr; | 655 internal::ObjectUnion_Data* data = nullptr; |
656 SerializeUnion_(obj.Pass(), &buf, &data, false); | 656 SerializeUnion_(std::move(obj), &buf, &data, false); |
657 | 657 |
658 std::vector<Handle> handles; | 658 std::vector<Handle> handles; |
659 data->EncodePointersAndHandles(&handles); | 659 data->EncodePointersAndHandles(&handles); |
660 data->DecodePointersAndHandles(&handles); | 660 data->DecodePointersAndHandles(&handles); |
661 | 661 |
662 ObjectUnionPtr obj2; | 662 ObjectUnionPtr obj2; |
663 Deserialize_(data, &obj2, nullptr); | 663 Deserialize_(data, &obj2, nullptr); |
664 EXPECT_EQ(8, obj2->get_f_dummy()->f_int8); | 664 EXPECT_EQ(8, obj2->get_f_dummy()->f_int8); |
665 } | 665 } |
666 | 666 |
667 TEST(UnionTest, StructInUnionValidation) { | 667 TEST(UnionTest, StructInUnionValidation) { |
668 DummyStructPtr dummy(DummyStruct::New()); | 668 DummyStructPtr dummy(DummyStruct::New()); |
669 dummy->f_int8 = 8; | 669 dummy->f_int8 = 8; |
670 | 670 |
671 ObjectUnionPtr obj(ObjectUnion::New()); | 671 ObjectUnionPtr obj(ObjectUnion::New()); |
672 obj->set_f_dummy(dummy.Pass()); | 672 obj->set_f_dummy(std::move(dummy)); |
673 | 673 |
674 size_t size = GetSerializedSize_(obj, false); | 674 size_t size = GetSerializedSize_(obj, false); |
675 | 675 |
676 mojo::internal::FixedBufferForTesting buf(size); | 676 mojo::internal::FixedBufferForTesting buf(size); |
677 internal::ObjectUnion_Data* data = nullptr; | 677 internal::ObjectUnion_Data* data = nullptr; |
678 SerializeUnion_(obj.Pass(), &buf, &data, false); | 678 SerializeUnion_(std::move(obj), &buf, &data, false); |
679 | 679 |
680 std::vector<Handle> handles; | 680 std::vector<Handle> handles; |
681 data->EncodePointersAndHandles(&handles); | 681 data->EncodePointersAndHandles(&handles); |
682 EXPECT_TRUE(handles.empty()); | 682 EXPECT_TRUE(handles.empty()); |
683 | 683 |
684 void* raw_buf = buf.Leak(); | 684 void* raw_buf = buf.Leak(); |
685 mojo::internal::BoundsChecker bounds_checker(data, | 685 mojo::internal::BoundsChecker bounds_checker(data, |
686 static_cast<uint32_t>(size), 0); | 686 static_cast<uint32_t>(size), 0); |
687 EXPECT_TRUE( | 687 EXPECT_TRUE( |
688 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 688 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
689 free(raw_buf); | 689 free(raw_buf); |
690 } | 690 } |
691 | 691 |
692 TEST(UnionTest, StructInUnionValidationNonNullable) { | 692 TEST(UnionTest, StructInUnionValidationNonNullable) { |
693 DummyStructPtr dummy(nullptr); | 693 DummyStructPtr dummy(nullptr); |
694 | 694 |
695 ObjectUnionPtr obj(ObjectUnion::New()); | 695 ObjectUnionPtr obj(ObjectUnion::New()); |
696 obj->set_f_dummy(dummy.Pass()); | 696 obj->set_f_dummy(std::move(dummy)); |
697 | 697 |
698 size_t size = GetSerializedSize_(obj, false); | 698 size_t size = GetSerializedSize_(obj, false); |
699 | 699 |
700 mojo::internal::FixedBufferForTesting buf(size); | 700 mojo::internal::FixedBufferForTesting buf(size); |
701 internal::ObjectUnion_Data* data = nullptr; | 701 internal::ObjectUnion_Data* data = nullptr; |
702 SerializeUnion_(obj.Pass(), &buf, &data, false); | 702 SerializeUnion_(std::move(obj), &buf, &data, false); |
703 | 703 |
704 std::vector<Handle> handles; | 704 std::vector<Handle> handles; |
705 data->EncodePointersAndHandles(&handles); | 705 data->EncodePointersAndHandles(&handles); |
706 EXPECT_TRUE(handles.empty()); | 706 EXPECT_TRUE(handles.empty()); |
707 | 707 |
708 void* raw_buf = buf.Leak(); | 708 void* raw_buf = buf.Leak(); |
709 mojo::internal::BoundsChecker bounds_checker(data, | 709 mojo::internal::BoundsChecker bounds_checker(data, |
710 static_cast<uint32_t>(size), 0); | 710 static_cast<uint32_t>(size), 0); |
711 EXPECT_FALSE( | 711 EXPECT_FALSE( |
712 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 712 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
713 free(raw_buf); | 713 free(raw_buf); |
714 } | 714 } |
715 | 715 |
716 TEST(UnionTest, StructInUnionValidationNullable) { | 716 TEST(UnionTest, StructInUnionValidationNullable) { |
717 DummyStructPtr dummy(nullptr); | 717 DummyStructPtr dummy(nullptr); |
718 | 718 |
719 ObjectUnionPtr obj(ObjectUnion::New()); | 719 ObjectUnionPtr obj(ObjectUnion::New()); |
720 obj->set_f_nullable(dummy.Pass()); | 720 obj->set_f_nullable(std::move(dummy)); |
721 | 721 |
722 size_t size = GetSerializedSize_(obj, false); | 722 size_t size = GetSerializedSize_(obj, false); |
723 | 723 |
724 mojo::internal::FixedBufferForTesting buf(size); | 724 mojo::internal::FixedBufferForTesting buf(size); |
725 internal::ObjectUnion_Data* data = nullptr; | 725 internal::ObjectUnion_Data* data = nullptr; |
726 SerializeUnion_(obj.Pass(), &buf, &data, false); | 726 SerializeUnion_(std::move(obj), &buf, &data, false); |
727 | 727 |
728 std::vector<Handle> handles; | 728 std::vector<Handle> handles; |
729 data->EncodePointersAndHandles(&handles); | 729 data->EncodePointersAndHandles(&handles); |
730 EXPECT_TRUE(handles.empty()); | 730 EXPECT_TRUE(handles.empty()); |
731 | 731 |
732 void* raw_buf = buf.Leak(); | 732 void* raw_buf = buf.Leak(); |
733 mojo::internal::BoundsChecker bounds_checker(data, | 733 mojo::internal::BoundsChecker bounds_checker(data, |
734 static_cast<uint32_t>(size), 0); | 734 static_cast<uint32_t>(size), 0); |
735 EXPECT_TRUE( | 735 EXPECT_TRUE( |
736 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 736 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
737 free(raw_buf); | 737 free(raw_buf); |
738 } | 738 } |
739 | 739 |
740 TEST(UnionTest, ArrayInUnionGetterSetter) { | 740 TEST(UnionTest, ArrayInUnionGetterSetter) { |
741 Array<int8_t> array(2); | 741 Array<int8_t> array(2); |
742 array[0] = 8; | 742 array[0] = 8; |
743 array[1] = 9; | 743 array[1] = 9; |
744 | 744 |
745 ObjectUnionPtr obj(ObjectUnion::New()); | 745 ObjectUnionPtr obj(ObjectUnion::New()); |
746 obj->set_f_array_int8(array.Pass()); | 746 obj->set_f_array_int8(std::move(array)); |
747 | 747 |
748 EXPECT_EQ(8, obj->get_f_array_int8()[0]); | 748 EXPECT_EQ(8, obj->get_f_array_int8()[0]); |
749 EXPECT_EQ(9, obj->get_f_array_int8()[1]); | 749 EXPECT_EQ(9, obj->get_f_array_int8()[1]); |
750 } | 750 } |
751 | 751 |
752 TEST(UnionTest, ArrayInUnionSerialization) { | 752 TEST(UnionTest, ArrayInUnionSerialization) { |
753 Array<int8_t> array(2); | 753 Array<int8_t> array(2); |
754 array[0] = 8; | 754 array[0] = 8; |
755 array[1] = 9; | 755 array[1] = 9; |
756 | 756 |
757 ObjectUnionPtr obj(ObjectUnion::New()); | 757 ObjectUnionPtr obj(ObjectUnion::New()); |
758 obj->set_f_array_int8(array.Pass()); | 758 obj->set_f_array_int8(std::move(array)); |
759 | 759 |
760 size_t size = GetSerializedSize_(obj, false); | 760 size_t size = GetSerializedSize_(obj, false); |
761 EXPECT_EQ(32U, size); | 761 EXPECT_EQ(32U, size); |
762 | 762 |
763 mojo::internal::FixedBufferForTesting buf(size); | 763 mojo::internal::FixedBufferForTesting buf(size); |
764 internal::ObjectUnion_Data* data = nullptr; | 764 internal::ObjectUnion_Data* data = nullptr; |
765 SerializeUnion_(obj.Pass(), &buf, &data, false); | 765 SerializeUnion_(std::move(obj), &buf, &data, false); |
766 | 766 |
767 std::vector<Handle> handles; | 767 std::vector<Handle> handles; |
768 data->EncodePointersAndHandles(&handles); | 768 data->EncodePointersAndHandles(&handles); |
769 data->DecodePointersAndHandles(&handles); | 769 data->DecodePointersAndHandles(&handles); |
770 | 770 |
771 ObjectUnionPtr obj2; | 771 ObjectUnionPtr obj2; |
772 Deserialize_(data, &obj2, nullptr); | 772 Deserialize_(data, &obj2, nullptr); |
773 | 773 |
774 EXPECT_EQ(8, obj2->get_f_array_int8()[0]); | 774 EXPECT_EQ(8, obj2->get_f_array_int8()[0]); |
775 EXPECT_EQ(9, obj2->get_f_array_int8()[1]); | 775 EXPECT_EQ(9, obj2->get_f_array_int8()[1]); |
776 } | 776 } |
777 | 777 |
778 TEST(UnionTest, ArrayInUnionValidation) { | 778 TEST(UnionTest, ArrayInUnionValidation) { |
779 Array<int8_t> array(2); | 779 Array<int8_t> array(2); |
780 array[0] = 8; | 780 array[0] = 8; |
781 array[1] = 9; | 781 array[1] = 9; |
782 | 782 |
783 ObjectUnionPtr obj(ObjectUnion::New()); | 783 ObjectUnionPtr obj(ObjectUnion::New()); |
784 obj->set_f_array_int8(array.Pass()); | 784 obj->set_f_array_int8(std::move(array)); |
785 | 785 |
786 size_t size = GetSerializedSize_(obj, false); | 786 size_t size = GetSerializedSize_(obj, false); |
787 mojo::internal::FixedBufferForTesting buf(size); | 787 mojo::internal::FixedBufferForTesting buf(size); |
788 internal::ObjectUnion_Data* data = nullptr; | 788 internal::ObjectUnion_Data* data = nullptr; |
789 SerializeUnion_(obj.Pass(), &buf, &data, false); | 789 SerializeUnion_(std::move(obj), &buf, &data, false); |
790 | 790 |
791 std::vector<Handle> handles; | 791 std::vector<Handle> handles; |
792 data->EncodePointersAndHandles(&handles); | 792 data->EncodePointersAndHandles(&handles); |
793 | 793 |
794 void* raw_buf = buf.Leak(); | 794 void* raw_buf = buf.Leak(); |
795 mojo::internal::BoundsChecker bounds_checker(data, | 795 mojo::internal::BoundsChecker bounds_checker(data, |
796 static_cast<uint32_t>(size), 0); | 796 static_cast<uint32_t>(size), 0); |
797 | 797 |
798 EXPECT_TRUE( | 798 EXPECT_TRUE( |
799 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 799 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
800 free(raw_buf); | 800 free(raw_buf); |
801 } | 801 } |
802 | 802 |
803 TEST(UnionTest, MapInUnionGetterSetter) { | 803 TEST(UnionTest, MapInUnionGetterSetter) { |
804 Map<String, int8_t> map; | 804 Map<String, int8_t> map; |
805 map.insert("one", 1); | 805 map.insert("one", 1); |
806 map.insert("two", 2); | 806 map.insert("two", 2); |
807 | 807 |
808 ObjectUnionPtr obj(ObjectUnion::New()); | 808 ObjectUnionPtr obj(ObjectUnion::New()); |
809 obj->set_f_map_int8(map.Pass()); | 809 obj->set_f_map_int8(std::move(map)); |
810 | 810 |
811 EXPECT_EQ(1, obj->get_f_map_int8()["one"]); | 811 EXPECT_EQ(1, obj->get_f_map_int8()["one"]); |
812 EXPECT_EQ(2, obj->get_f_map_int8()["two"]); | 812 EXPECT_EQ(2, obj->get_f_map_int8()["two"]); |
813 } | 813 } |
814 | 814 |
815 TEST(UnionTest, MapInUnionSerialization) { | 815 TEST(UnionTest, MapInUnionSerialization) { |
816 Map<String, int8_t> map; | 816 Map<String, int8_t> map; |
817 map.insert("one", 1); | 817 map.insert("one", 1); |
818 map.insert("two", 2); | 818 map.insert("two", 2); |
819 | 819 |
820 ObjectUnionPtr obj(ObjectUnion::New()); | 820 ObjectUnionPtr obj(ObjectUnion::New()); |
821 obj->set_f_map_int8(map.Pass()); | 821 obj->set_f_map_int8(std::move(map)); |
822 | 822 |
823 size_t size = GetSerializedSize_(obj, false); | 823 size_t size = GetSerializedSize_(obj, false); |
824 EXPECT_EQ(112U, size); | 824 EXPECT_EQ(112U, size); |
825 | 825 |
826 mojo::internal::FixedBufferForTesting buf(size); | 826 mojo::internal::FixedBufferForTesting buf(size); |
827 internal::ObjectUnion_Data* data = nullptr; | 827 internal::ObjectUnion_Data* data = nullptr; |
828 SerializeUnion_(obj.Pass(), &buf, &data, false); | 828 SerializeUnion_(std::move(obj), &buf, &data, false); |
829 | 829 |
830 std::vector<Handle> handles; | 830 std::vector<Handle> handles; |
831 data->EncodePointersAndHandles(&handles); | 831 data->EncodePointersAndHandles(&handles); |
832 data->DecodePointersAndHandles(&handles); | 832 data->DecodePointersAndHandles(&handles); |
833 | 833 |
834 ObjectUnionPtr obj2; | 834 ObjectUnionPtr obj2; |
835 Deserialize_(data, &obj2, nullptr); | 835 Deserialize_(data, &obj2, nullptr); |
836 | 836 |
837 EXPECT_EQ(1, obj2->get_f_map_int8()["one"]); | 837 EXPECT_EQ(1, obj2->get_f_map_int8()["one"]); |
838 EXPECT_EQ(2, obj2->get_f_map_int8()["two"]); | 838 EXPECT_EQ(2, obj2->get_f_map_int8()["two"]); |
839 } | 839 } |
840 | 840 |
841 TEST(UnionTest, MapInUnionValidation) { | 841 TEST(UnionTest, MapInUnionValidation) { |
842 Map<String, int8_t> map; | 842 Map<String, int8_t> map; |
843 map.insert("one", 1); | 843 map.insert("one", 1); |
844 map.insert("two", 2); | 844 map.insert("two", 2); |
845 | 845 |
846 ObjectUnionPtr obj(ObjectUnion::New()); | 846 ObjectUnionPtr obj(ObjectUnion::New()); |
847 obj->set_f_map_int8(map.Pass()); | 847 obj->set_f_map_int8(std::move(map)); |
848 | 848 |
849 size_t size = GetSerializedSize_(obj, false); | 849 size_t size = GetSerializedSize_(obj, false); |
850 EXPECT_EQ(112U, size); | 850 EXPECT_EQ(112U, size); |
851 | 851 |
852 mojo::internal::FixedBufferForTesting buf(size); | 852 mojo::internal::FixedBufferForTesting buf(size); |
853 internal::ObjectUnion_Data* data = nullptr; | 853 internal::ObjectUnion_Data* data = nullptr; |
854 SerializeUnion_(obj.Pass(), &buf, &data, false); | 854 SerializeUnion_(std::move(obj), &buf, &data, false); |
855 | 855 |
856 std::vector<Handle> handles; | 856 std::vector<Handle> handles; |
857 data->EncodePointersAndHandles(&handles); | 857 data->EncodePointersAndHandles(&handles); |
858 EXPECT_TRUE(handles.empty()); | 858 EXPECT_TRUE(handles.empty()); |
859 | 859 |
860 void* raw_buf = buf.Leak(); | 860 void* raw_buf = buf.Leak(); |
861 mojo::internal::BoundsChecker bounds_checker(data, | 861 mojo::internal::BoundsChecker bounds_checker(data, |
862 static_cast<uint32_t>(size), 0); | 862 static_cast<uint32_t>(size), 0); |
863 | 863 |
864 EXPECT_TRUE( | 864 EXPECT_TRUE( |
865 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 865 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
866 free(raw_buf); | 866 free(raw_buf); |
867 } | 867 } |
868 | 868 |
869 TEST(UnionTest, UnionInUnionGetterSetter) { | 869 TEST(UnionTest, UnionInUnionGetterSetter) { |
870 PodUnionPtr pod(PodUnion::New()); | 870 PodUnionPtr pod(PodUnion::New()); |
871 pod->set_f_int8(10); | 871 pod->set_f_int8(10); |
872 | 872 |
873 ObjectUnionPtr obj(ObjectUnion::New()); | 873 ObjectUnionPtr obj(ObjectUnion::New()); |
874 obj->set_f_pod_union(pod.Pass()); | 874 obj->set_f_pod_union(std::move(pod)); |
875 | 875 |
876 EXPECT_EQ(10, obj->get_f_pod_union()->get_f_int8()); | 876 EXPECT_EQ(10, obj->get_f_pod_union()->get_f_int8()); |
877 } | 877 } |
878 | 878 |
879 TEST(UnionTest, UnionInUnionSerialization) { | 879 TEST(UnionTest, UnionInUnionSerialization) { |
880 PodUnionPtr pod(PodUnion::New()); | 880 PodUnionPtr pod(PodUnion::New()); |
881 pod->set_f_int8(10); | 881 pod->set_f_int8(10); |
882 | 882 |
883 ObjectUnionPtr obj(ObjectUnion::New()); | 883 ObjectUnionPtr obj(ObjectUnion::New()); |
884 obj->set_f_pod_union(pod.Pass()); | 884 obj->set_f_pod_union(std::move(pod)); |
885 | 885 |
886 size_t size = GetSerializedSize_(obj, false); | 886 size_t size = GetSerializedSize_(obj, false); |
887 EXPECT_EQ(32U, size); | 887 EXPECT_EQ(32U, size); |
888 | 888 |
889 mojo::internal::FixedBufferForTesting buf(size); | 889 mojo::internal::FixedBufferForTesting buf(size); |
890 internal::ObjectUnion_Data* data = nullptr; | 890 internal::ObjectUnion_Data* data = nullptr; |
891 SerializeUnion_(obj.Pass(), &buf, &data, false); | 891 SerializeUnion_(std::move(obj), &buf, &data, false); |
892 | 892 |
893 std::vector<Handle> handles; | 893 std::vector<Handle> handles; |
894 data->EncodePointersAndHandles(&handles); | 894 data->EncodePointersAndHandles(&handles); |
895 data->DecodePointersAndHandles(&handles); | 895 data->DecodePointersAndHandles(&handles); |
896 | 896 |
897 ObjectUnionPtr obj2; | 897 ObjectUnionPtr obj2; |
898 Deserialize_(data, &obj2, nullptr); | 898 Deserialize_(data, &obj2, nullptr); |
899 EXPECT_EQ(10, obj2->get_f_pod_union()->get_f_int8()); | 899 EXPECT_EQ(10, obj2->get_f_pod_union()->get_f_int8()); |
900 } | 900 } |
901 | 901 |
902 TEST(UnionTest, UnionInUnionValidation) { | 902 TEST(UnionTest, UnionInUnionValidation) { |
903 PodUnionPtr pod(PodUnion::New()); | 903 PodUnionPtr pod(PodUnion::New()); |
904 pod->set_f_int8(10); | 904 pod->set_f_int8(10); |
905 | 905 |
906 ObjectUnionPtr obj(ObjectUnion::New()); | 906 ObjectUnionPtr obj(ObjectUnion::New()); |
907 obj->set_f_pod_union(pod.Pass()); | 907 obj->set_f_pod_union(std::move(pod)); |
908 | 908 |
909 size_t size = GetSerializedSize_(obj, false); | 909 size_t size = GetSerializedSize_(obj, false); |
910 EXPECT_EQ(32U, size); | 910 EXPECT_EQ(32U, size); |
911 | 911 |
912 mojo::internal::FixedBufferForTesting buf(size); | 912 mojo::internal::FixedBufferForTesting buf(size); |
913 internal::ObjectUnion_Data* data = nullptr; | 913 internal::ObjectUnion_Data* data = nullptr; |
914 SerializeUnion_(obj.Pass(), &buf, &data, false); | 914 SerializeUnion_(std::move(obj), &buf, &data, false); |
915 | 915 |
916 std::vector<Handle> handles; | 916 std::vector<Handle> handles; |
917 data->EncodePointersAndHandles(&handles); | 917 data->EncodePointersAndHandles(&handles); |
918 EXPECT_TRUE(handles.empty()); | 918 EXPECT_TRUE(handles.empty()); |
919 | 919 |
920 void* raw_buf = buf.Leak(); | 920 void* raw_buf = buf.Leak(); |
921 mojo::internal::BoundsChecker bounds_checker(data, | 921 mojo::internal::BoundsChecker bounds_checker(data, |
922 static_cast<uint32_t>(size), 0); | 922 static_cast<uint32_t>(size), 0); |
923 EXPECT_TRUE( | 923 EXPECT_TRUE( |
924 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 924 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
925 free(raw_buf); | 925 free(raw_buf); |
926 } | 926 } |
927 | 927 |
928 TEST(UnionTest, UnionInUnionValidationNonNullable) { | 928 TEST(UnionTest, UnionInUnionValidationNonNullable) { |
929 PodUnionPtr pod(nullptr); | 929 PodUnionPtr pod(nullptr); |
930 | 930 |
931 ObjectUnionPtr obj(ObjectUnion::New()); | 931 ObjectUnionPtr obj(ObjectUnion::New()); |
932 obj->set_f_pod_union(pod.Pass()); | 932 obj->set_f_pod_union(std::move(pod)); |
933 | 933 |
934 size_t size = GetSerializedSize_(obj, false); | 934 size_t size = GetSerializedSize_(obj, false); |
935 | 935 |
936 mojo::internal::FixedBufferForTesting buf(size); | 936 mojo::internal::FixedBufferForTesting buf(size); |
937 internal::ObjectUnion_Data* data = nullptr; | 937 internal::ObjectUnion_Data* data = nullptr; |
938 SerializeUnion_(obj.Pass(), &buf, &data, false); | 938 SerializeUnion_(std::move(obj), &buf, &data, false); |
939 std::vector<Handle> handles; | 939 std::vector<Handle> handles; |
940 data->EncodePointersAndHandles(&handles); | 940 data->EncodePointersAndHandles(&handles); |
941 | 941 |
942 void* raw_buf = buf.Leak(); | 942 void* raw_buf = buf.Leak(); |
943 mojo::internal::BoundsChecker bounds_checker(data, | 943 mojo::internal::BoundsChecker bounds_checker(data, |
944 static_cast<uint32_t>(size), 0); | 944 static_cast<uint32_t>(size), 0); |
945 EXPECT_FALSE( | 945 EXPECT_FALSE( |
946 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 946 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
947 free(raw_buf); | 947 free(raw_buf); |
948 } | 948 } |
949 | 949 |
950 TEST(UnionTest, HandleInUnionGetterSetter) { | 950 TEST(UnionTest, HandleInUnionGetterSetter) { |
951 ScopedMessagePipeHandle pipe0; | 951 ScopedMessagePipeHandle pipe0; |
952 ScopedMessagePipeHandle pipe1; | 952 ScopedMessagePipeHandle pipe1; |
953 | 953 |
954 CreateMessagePipe(nullptr, &pipe0, &pipe1); | 954 CreateMessagePipe(nullptr, &pipe0, &pipe1); |
955 | 955 |
956 HandleUnionPtr handle(HandleUnion::New()); | 956 HandleUnionPtr handle(HandleUnion::New()); |
957 handle->set_f_message_pipe(pipe1.Pass()); | 957 handle->set_f_message_pipe(std::move(pipe1)); |
958 | 958 |
959 std::string golden("hello world"); | 959 std::string golden("hello world"); |
960 WriteTextMessage(pipe0.get(), golden); | 960 WriteTextMessage(pipe0.get(), golden); |
961 | 961 |
962 std::string actual; | 962 std::string actual; |
963 ReadTextMessage(handle->get_f_message_pipe().get(), &actual); | 963 ReadTextMessage(handle->get_f_message_pipe().get(), &actual); |
964 | 964 |
965 EXPECT_EQ(golden, actual); | 965 EXPECT_EQ(golden, actual); |
966 } | 966 } |
967 | 967 |
968 TEST(UnionTest, HandleInUnionSerialization) { | 968 TEST(UnionTest, HandleInUnionSerialization) { |
969 ScopedMessagePipeHandle pipe0; | 969 ScopedMessagePipeHandle pipe0; |
970 ScopedMessagePipeHandle pipe1; | 970 ScopedMessagePipeHandle pipe1; |
971 | 971 |
972 CreateMessagePipe(nullptr, &pipe0, &pipe1); | 972 CreateMessagePipe(nullptr, &pipe0, &pipe1); |
973 | 973 |
974 HandleUnionPtr handle(HandleUnion::New()); | 974 HandleUnionPtr handle(HandleUnion::New()); |
975 handle->set_f_message_pipe(pipe1.Pass()); | 975 handle->set_f_message_pipe(std::move(pipe1)); |
976 | 976 |
977 size_t size = GetSerializedSize_(handle, false); | 977 size_t size = GetSerializedSize_(handle, false); |
978 EXPECT_EQ(16U, size); | 978 EXPECT_EQ(16U, size); |
979 | 979 |
980 mojo::internal::FixedBufferForTesting buf(size); | 980 mojo::internal::FixedBufferForTesting buf(size); |
981 internal::HandleUnion_Data* data = nullptr; | 981 internal::HandleUnion_Data* data = nullptr; |
982 SerializeUnion_(handle.Pass(), &buf, &data, false); | 982 SerializeUnion_(std::move(handle), &buf, &data, false); |
983 | 983 |
984 std::vector<Handle> handles; | 984 std::vector<Handle> handles; |
985 data->EncodePointersAndHandles(&handles); | 985 data->EncodePointersAndHandles(&handles); |
986 EXPECT_EQ(1U, handles.size()); | 986 EXPECT_EQ(1U, handles.size()); |
987 data->DecodePointersAndHandles(&handles); | 987 data->DecodePointersAndHandles(&handles); |
988 | 988 |
989 HandleUnionPtr handle2(HandleUnion::New()); | 989 HandleUnionPtr handle2(HandleUnion::New()); |
990 Deserialize_(data, &handle2, nullptr); | 990 Deserialize_(data, &handle2, nullptr); |
991 | 991 |
992 std::string golden("hello world"); | 992 std::string golden("hello world"); |
993 WriteTextMessage(pipe0.get(), golden); | 993 WriteTextMessage(pipe0.get(), golden); |
994 | 994 |
995 std::string actual; | 995 std::string actual; |
996 ReadTextMessage(handle2->get_f_message_pipe().get(), &actual); | 996 ReadTextMessage(handle2->get_f_message_pipe().get(), &actual); |
997 | 997 |
998 EXPECT_EQ(golden, actual); | 998 EXPECT_EQ(golden, actual); |
999 } | 999 } |
1000 | 1000 |
1001 TEST(UnionTest, HandleInUnionValidation) { | 1001 TEST(UnionTest, HandleInUnionValidation) { |
1002 ScopedMessagePipeHandle pipe0; | 1002 ScopedMessagePipeHandle pipe0; |
1003 ScopedMessagePipeHandle pipe1; | 1003 ScopedMessagePipeHandle pipe1; |
1004 | 1004 |
1005 CreateMessagePipe(nullptr, &pipe0, &pipe1); | 1005 CreateMessagePipe(nullptr, &pipe0, &pipe1); |
1006 | 1006 |
1007 HandleUnionPtr handle(HandleUnion::New()); | 1007 HandleUnionPtr handle(HandleUnion::New()); |
1008 handle->set_f_message_pipe(pipe1.Pass()); | 1008 handle->set_f_message_pipe(std::move(pipe1)); |
1009 | 1009 |
1010 size_t size = GetSerializedSize_(handle, false); | 1010 size_t size = GetSerializedSize_(handle, false); |
1011 EXPECT_EQ(16U, size); | 1011 EXPECT_EQ(16U, size); |
1012 | 1012 |
1013 mojo::internal::FixedBufferForTesting buf(size); | 1013 mojo::internal::FixedBufferForTesting buf(size); |
1014 internal::HandleUnion_Data* data = nullptr; | 1014 internal::HandleUnion_Data* data = nullptr; |
1015 SerializeUnion_(handle.Pass(), &buf, &data, false); | 1015 SerializeUnion_(std::move(handle), &buf, &data, false); |
1016 | 1016 |
1017 std::vector<Handle> handles; | 1017 std::vector<Handle> handles; |
1018 data->EncodePointersAndHandles(&handles); | 1018 data->EncodePointersAndHandles(&handles); |
1019 | 1019 |
1020 void* raw_buf = buf.Leak(); | 1020 void* raw_buf = buf.Leak(); |
1021 mojo::internal::BoundsChecker bounds_checker(data, | 1021 mojo::internal::BoundsChecker bounds_checker(data, |
1022 static_cast<uint32_t>(size), 1); | 1022 static_cast<uint32_t>(size), 1); |
1023 EXPECT_TRUE( | 1023 EXPECT_TRUE( |
1024 internal::HandleUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 1024 internal::HandleUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
1025 free(raw_buf); | 1025 free(raw_buf); |
1026 } | 1026 } |
1027 | 1027 |
1028 TEST(UnionTest, HandleInUnionValidationNull) { | 1028 TEST(UnionTest, HandleInUnionValidationNull) { |
1029 ScopedMessagePipeHandle pipe; | 1029 ScopedMessagePipeHandle pipe; |
1030 HandleUnionPtr handle(HandleUnion::New()); | 1030 HandleUnionPtr handle(HandleUnion::New()); |
1031 handle->set_f_message_pipe(pipe.Pass()); | 1031 handle->set_f_message_pipe(std::move(pipe)); |
1032 | 1032 |
1033 size_t size = GetSerializedSize_(handle, false); | 1033 size_t size = GetSerializedSize_(handle, false); |
1034 EXPECT_EQ(16U, size); | 1034 EXPECT_EQ(16U, size); |
1035 | 1035 |
1036 mojo::internal::FixedBufferForTesting buf(size); | 1036 mojo::internal::FixedBufferForTesting buf(size); |
1037 internal::HandleUnion_Data* data = nullptr; | 1037 internal::HandleUnion_Data* data = nullptr; |
1038 SerializeUnion_(handle.Pass(), &buf, &data, false); | 1038 SerializeUnion_(std::move(handle), &buf, &data, false); |
1039 | 1039 |
1040 std::vector<Handle> handles; | 1040 std::vector<Handle> handles; |
1041 data->EncodePointersAndHandles(&handles); | 1041 data->EncodePointersAndHandles(&handles); |
1042 | 1042 |
1043 void* raw_buf = buf.Leak(); | 1043 void* raw_buf = buf.Leak(); |
1044 mojo::internal::BoundsChecker bounds_checker(data, | 1044 mojo::internal::BoundsChecker bounds_checker(data, |
1045 static_cast<uint32_t>(size), 1); | 1045 static_cast<uint32_t>(size), 1); |
1046 EXPECT_FALSE( | 1046 EXPECT_FALSE( |
1047 internal::HandleUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 1047 internal::HandleUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
1048 free(raw_buf); | 1048 free(raw_buf); |
(...skipping 14 matching lines...) Expand all Loading... |
1063 int64_t int_value_; | 1063 int64_t int_value_; |
1064 }; | 1064 }; |
1065 | 1065 |
1066 TEST(UnionTest, InterfaceInUnion) { | 1066 TEST(UnionTest, InterfaceInUnion) { |
1067 base::MessageLoop run_loop(common::MessagePumpMojo::Create()); | 1067 base::MessageLoop run_loop(common::MessagePumpMojo::Create()); |
1068 SmallCacheImpl impl; | 1068 SmallCacheImpl impl; |
1069 SmallCachePtr ptr; | 1069 SmallCachePtr ptr; |
1070 Binding<SmallCache> bindings(&impl, GetProxy(&ptr)); | 1070 Binding<SmallCache> bindings(&impl, GetProxy(&ptr)); |
1071 | 1071 |
1072 HandleUnionPtr handle(HandleUnion::New()); | 1072 HandleUnionPtr handle(HandleUnion::New()); |
1073 handle->set_f_small_cache(ptr.Pass()); | 1073 handle->set_f_small_cache(std::move(ptr)); |
1074 | 1074 |
1075 handle->get_f_small_cache()->SetIntValue(10); | 1075 handle->get_f_small_cache()->SetIntValue(10); |
1076 run_loop.RunUntilIdle(); | 1076 run_loop.RunUntilIdle(); |
1077 EXPECT_EQ(10, impl.int_value()); | 1077 EXPECT_EQ(10, impl.int_value()); |
1078 } | 1078 } |
1079 | 1079 |
1080 TEST(UnionTest, InterfaceInUnionSerialization) { | 1080 TEST(UnionTest, InterfaceInUnionSerialization) { |
1081 base::MessageLoop run_loop(common::MessagePumpMojo::Create()); | 1081 base::MessageLoop run_loop(common::MessagePumpMojo::Create()); |
1082 SmallCacheImpl impl; | 1082 SmallCacheImpl impl; |
1083 SmallCachePtr ptr; | 1083 SmallCachePtr ptr; |
1084 Binding<SmallCache> bindings(&impl, GetProxy(&ptr)); | 1084 Binding<SmallCache> bindings(&impl, GetProxy(&ptr)); |
1085 | 1085 |
1086 HandleUnionPtr handle(HandleUnion::New()); | 1086 HandleUnionPtr handle(HandleUnion::New()); |
1087 handle->set_f_small_cache(ptr.Pass()); | 1087 handle->set_f_small_cache(std::move(ptr)); |
1088 size_t size = GetSerializedSize_(handle, false); | 1088 size_t size = GetSerializedSize_(handle, false); |
1089 EXPECT_EQ(16U, size); | 1089 EXPECT_EQ(16U, size); |
1090 | 1090 |
1091 mojo::internal::FixedBufferForTesting buf(size); | 1091 mojo::internal::FixedBufferForTesting buf(size); |
1092 internal::HandleUnion_Data* data = nullptr; | 1092 internal::HandleUnion_Data* data = nullptr; |
1093 SerializeUnion_(handle.Pass(), &buf, &data, false); | 1093 SerializeUnion_(std::move(handle), &buf, &data, false); |
1094 | 1094 |
1095 std::vector<Handle> handles; | 1095 std::vector<Handle> handles; |
1096 data->EncodePointersAndHandles(&handles); | 1096 data->EncodePointersAndHandles(&handles); |
1097 EXPECT_EQ(1U, handles.size()); | 1097 EXPECT_EQ(1U, handles.size()); |
1098 data->DecodePointersAndHandles(&handles); | 1098 data->DecodePointersAndHandles(&handles); |
1099 | 1099 |
1100 HandleUnionPtr handle2(HandleUnion::New()); | 1100 HandleUnionPtr handle2(HandleUnion::New()); |
1101 Deserialize_(data, &handle2, nullptr); | 1101 Deserialize_(data, &handle2, nullptr); |
1102 | 1102 |
1103 handle2->get_f_small_cache()->SetIntValue(10); | 1103 handle2->get_f_small_cache()->SetIntValue(10); |
1104 run_loop.RunUntilIdle(); | 1104 run_loop.RunUntilIdle(); |
1105 EXPECT_EQ(10, impl.int_value()); | 1105 EXPECT_EQ(10, impl.int_value()); |
1106 } | 1106 } |
1107 | 1107 |
1108 class UnionInterfaceImpl : public UnionInterface { | 1108 class UnionInterfaceImpl : public UnionInterface { |
1109 public: | 1109 public: |
1110 UnionInterfaceImpl() {} | 1110 UnionInterfaceImpl() {} |
1111 ~UnionInterfaceImpl() override {} | 1111 ~UnionInterfaceImpl() override {} |
1112 | 1112 |
1113 private: | 1113 private: |
1114 void Echo(PodUnionPtr in, const EchoCallback& callback) override { | 1114 void Echo(PodUnionPtr in, const EchoCallback& callback) override { |
1115 callback.Run(in.Pass()); | 1115 callback.Run(std::move(in)); |
1116 } | 1116 } |
1117 }; | 1117 }; |
1118 | 1118 |
1119 TEST(UnionTest, UnionInInterface) { | 1119 TEST(UnionTest, UnionInInterface) { |
1120 base::MessageLoop run_loop(common::MessagePumpMojo::Create()); | 1120 base::MessageLoop run_loop(common::MessagePumpMojo::Create()); |
1121 UnionInterfaceImpl impl; | 1121 UnionInterfaceImpl impl; |
1122 UnionInterfacePtr ptr; | 1122 UnionInterfacePtr ptr; |
1123 Binding<UnionInterface> bindings(&impl, GetProxy(&ptr)); | 1123 Binding<UnionInterface> bindings(&impl, GetProxy(&ptr)); |
1124 | 1124 |
1125 PodUnionPtr pod(PodUnion::New()); | 1125 PodUnionPtr pod(PodUnion::New()); |
1126 pod->set_f_int16(16); | 1126 pod->set_f_int16(16); |
1127 | 1127 |
1128 ptr->Echo(pod.Pass(), | 1128 ptr->Echo(std::move(pod), |
1129 [](PodUnionPtr out) { EXPECT_EQ(16, out->get_f_int16()); }); | 1129 [](PodUnionPtr out) { EXPECT_EQ(16, out->get_f_int16()); }); |
1130 run_loop.RunUntilIdle(); | 1130 run_loop.RunUntilIdle(); |
1131 } | 1131 } |
1132 | 1132 |
1133 } // namespace test | 1133 } // namespace test |
1134 } // namespace mojo | 1134 } // namespace mojo |
OLD | NEW |