Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: mojo/public/cpp/bindings/tests/struct_unittest.cc

Issue 923033003: Implement unions as members of structs. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <string.h> 5 #include <string.h>
6 6
7 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" 7 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
8 #include "mojo/public/cpp/environment/environment.h" 8 #include "mojo/public/cpp/environment/environment.h"
9 #include "mojo/public/cpp/system/message_pipe.h" 9 #include "mojo/public/cpp/system/message_pipe.h"
10 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" 10 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h"
11 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom.h"
11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
12 13
13 namespace mojo { 14 namespace mojo {
14 namespace test { 15 namespace test {
15 namespace { 16 namespace {
16 17
17 RectPtr MakeRect(int32_t factor = 1) { 18 RectPtr MakeRect(int32_t factor = 1) {
18 RectPtr rect(Rect::New()); 19 RectPtr rect(Rect::New());
19 rect->x = 1 * factor; 20 rect->x = 1 * factor;
20 rect->y = 2 * factor; 21 rect->y = 2 * factor;
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 MultiVersionStructV0Ptr expected_output(MultiVersionStructV0::New()); 413 MultiVersionStructV0Ptr expected_output(MultiVersionStructV0::New());
413 expected_output->f_int32 = 123; 414 expected_output->f_int32 = 123;
414 415
415 MultiVersionStructV0Ptr output = 416 MultiVersionStructV0Ptr output =
416 SerializeAndDeserialize<MultiVersionStructV0Ptr>(input.Pass()); 417 SerializeAndDeserialize<MultiVersionStructV0Ptr>(input.Pass());
417 EXPECT_TRUE(output); 418 EXPECT_TRUE(output);
418 EXPECT_TRUE(output->Equals(*expected_output)); 419 EXPECT_TRUE(output->Equals(*expected_output));
419 } 420 }
420 } 421 }
421 422
423 TEST_F(StructTest, PodUnionInStructClone) {
424 SmallStructPtr small_struct(SmallStruct::New());
425 small_struct->pod_union = PodUnion::New();
426 small_struct->pod_union->set_f_int8(10);
427
428 SmallStructPtr clone = small_struct.Clone();
429 EXPECT_EQ(10, clone->pod_union->get_f_int8());
430 }
431
432 TEST_F(StructTest, PodUnionInStructSerialization) {
yzshen1 2015/03/26 07:30:14 nit: the naming is not very consistent within this
azani 2015/03/26 22:27:39 Done.
433 SmallStructPtr small_struct(SmallStruct::New());
434 small_struct->pod_union = PodUnion::New();
435 small_struct->pod_union->set_f_int32(10);
436
437 size_t size = GetSerializedSize_(small_struct);
438
439 mojo::internal::FixedBuffer buf(size);
440 internal::SmallStruct_Data* data = nullptr;
441 Serialize_(small_struct.Pass(), &buf, &data);
442
443 SmallStructPtr deserialized;
444 Deserialize_(data, &deserialized);
445
446 EXPECT_EQ(10, deserialized->pod_union->get_f_int32());
447 }
448
449 TEST_F(StructTest, ObjectUnionInStructSerialization) {
450 SmallObjStructPtr obj_struct(SmallObjStruct::New());
451 obj_struct->obj_union = ObjectUnion::New();
452 String hello("hello world");
453 obj_struct->obj_union->set_f_string(hello);
454
455 size_t size = GetSerializedSize_(obj_struct);
456
457 mojo::internal::FixedBuffer buf(size);
458 internal::SmallObjStruct_Data* data = nullptr;
459 Serialize_(obj_struct.Pass(), &buf, &data);
460
461 SmallObjStructPtr deserialized;
462 Deserialize_(data, &deserialized);
463
464 EXPECT_EQ(hello, deserialized->obj_union->get_f_string());
465 }
466
467 TEST_F(StructTest, UnionsInStructsValidation) {
468 SmallStructPtr small_struct(SmallStruct::New());
469 small_struct->pod_union = PodUnion::New();
470 small_struct->pod_union->set_f_int32(10);
471
472 size_t size = GetSerializedSize_(small_struct);
473
474 mojo::internal::FixedBuffer buf(size);
475 internal::SmallStruct_Data* data = nullptr;
476 Serialize_(small_struct.Pass(), &buf, &data);
477
478 void* raw_buf = buf.Leak();
479 mojo::internal::BoundsChecker bounds_checker(data, size, 0);
480 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker));
481 free(raw_buf);
482 }
483
484 TEST_F(StructTest, PodUnionInStructValidationFailure) {
485 SmallStructPtr small_struct(SmallStruct::New());
486 small_struct->pod_union = PodUnion::New();
487 small_struct->pod_union->set_f_int32(10);
488
489 size_t size = GetSerializedSize_(small_struct);
490
491 mojo::internal::FixedBuffer buf(size);
492 internal::SmallStruct_Data* data = nullptr;
493 Serialize_(small_struct.Pass(), &buf, &data);
494 data->pod_union.tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(100);
495
496 void* raw_buf = buf.Leak();
497 mojo::internal::BoundsChecker bounds_checker(data, size, 0);
498 EXPECT_FALSE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker));
499 free(raw_buf);
500 }
501
502 TEST_F(StructTest, NullUnionValidationFailure) {
503 SmallStructNonNullableUnionPtr small_struct(
504 SmallStructNonNullableUnion::New());
505
506 size_t size = GetSerializedSize_(small_struct);
507
508 mojo::internal::FixedBuffer buf(size);
509 internal::SmallStructNonNullableUnion_Data* data =
510 internal::SmallStructNonNullableUnion_Data::New(&buf);
511
512 void* raw_buf = buf.Leak();
513 mojo::internal::BoundsChecker bounds_checker(data, size, 0);
514 EXPECT_FALSE(internal::SmallStructNonNullableUnion_Data::Validate(
515 raw_buf, &bounds_checker));
516 free(raw_buf);
517 }
518
519 TEST_F(StructTest, NullableUnionValidation) {
520 SmallStructPtr small_struct(SmallStruct::New());
521
522 size_t size = GetSerializedSize_(small_struct);
523
524 mojo::internal::FixedBuffer buf(size);
525 internal::SmallStruct_Data* data = nullptr;
526 Serialize_(small_struct.Pass(), &buf, &data);
527
528 void* raw_buf = buf.Leak();
529 mojo::internal::BoundsChecker bounds_checker(data, size, 0);
530 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker));
531 free(raw_buf);
532 }
533
422 } // namespace test 534 } // namespace test
423 } // namespace mojo 535 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698