| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 // TODO(vardhan): Needs a lot more testing. | 5 // TODO(vardhan): Needs a lot more testing. |
| 6 | 6 |
| 7 #include "mojo/public/c/bindings/union.h" | 7 #include "mojo/public/c/bindings/union.h" |
| 8 | 8 |
| 9 #include "mojo/public/c/bindings/array.h" | 9 #include "mojo/public/c/bindings/array.h" |
| 10 #include "mojo/public/c/bindings/tests/testing_util.h" |
| 10 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom-c.h" | 11 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom-c.h" |
| 11 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom-c.h" | 12 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom-c.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 struct mojo_test_StructOfUnionOfReferences* MakeStructOfUnionReference( | 17 struct mojo_test_StructOfUnionOfReferences* MakeStructOfUnionReference( |
| 17 struct MojomBuffer* buf) { | 18 struct MojomBuffer* buf) { |
| 18 auto* struct_with_union = | 19 auto* struct_with_union = |
| 19 static_cast<struct mojo_test_StructOfUnionOfReferences*>( | 20 static_cast<struct mojo_test_StructOfUnionOfReferences*>( |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 EXPECT_EQ(8u + 1 * 16u // 1 union type (set to PodUnion) | 61 EXPECT_EQ(8u + 1 * 16u // 1 union type (set to PodUnion) |
| 61 + 16u, // PodUnion is out of line. | 62 + 16u, // PodUnion is out of line. |
| 62 mojo_test_StructOfUnionOfReferences_ComputeSerializedSize( | 63 mojo_test_StructOfUnionOfReferences_ComputeSerializedSize( |
| 63 struct_with_union)); | 64 struct_with_union)); |
| 64 | 65 |
| 65 // We save the underlying (unencoded) buffer. We can compare the two after | 66 // We save the underlying (unencoded) buffer. We can compare the two after |
| 66 // deserialization to make sure deserialization is correct. | 67 // deserialization to make sure deserialization is correct. |
| 67 char buffer_bytes_copy[sizeof(buffer_bytes)]; | 68 char buffer_bytes_copy[sizeof(buffer_bytes)]; |
| 68 memcpy(buffer_bytes_copy, buffer_bytes, sizeof(buffer_bytes_copy)); | 69 memcpy(buffer_bytes_copy, buffer_bytes, sizeof(buffer_bytes_copy)); |
| 69 | 70 |
| 70 struct MojomHandleBuffer handle_buf = {NULL, 0u, 0u}; | |
| 71 mojo_test_StructOfUnionOfReferences_EncodePointersAndHandles( | 71 mojo_test_StructOfUnionOfReferences_EncodePointersAndHandles( |
| 72 struct_with_union, buf.num_bytes_used, &handle_buf); | 72 struct_with_union, buf.num_bytes_used, NULL); |
| 73 EXPECT_EQ(0u, handle_buf.num_handles_used); | |
| 74 | 73 |
| 75 EXPECT_EQ(sizeof(struct mojo_test_StructOfUnionOfReferences) - | 74 EXPECT_EQ(sizeof(struct mojo_test_StructOfUnionOfReferences) - |
| 76 offsetof(struct mojo_test_StructOfUnionOfReferences, u.data), | 75 offsetof(struct mojo_test_StructOfUnionOfReferences, u.data), |
| 77 struct_with_union->u.data.f_pod_union.offset); | 76 struct_with_union->u.data.f_pod_union.offset); |
| 78 | 77 |
| 79 mojo_test_StructOfUnionOfReferences_DecodePointersAndHandles( | 78 mojo_test_StructOfUnionOfReferences_DecodePointersAndHandles( |
| 80 struct_with_union, buf.num_bytes_used, NULL, 0); | 79 struct_with_union, buf.num_bytes_used, NULL, 0); |
| 81 EXPECT_EQ(0, memcmp(buf.buf, buffer_bytes_copy, buf.num_bytes_used)); | 80 EXPECT_EQ(0, memcmp(buf.buf, buffer_bytes_copy, buf.num_bytes_used)); |
| 81 |
| 82 { |
| 83 char buffer_bytes2[sizeof(buffer_bytes)] = {0}; |
| 84 struct MojomBuffer buf2 = {buffer_bytes2, sizeof(buffer_bytes2), 0}; |
| 85 CopyAndCompare( |
| 86 &buf2, struct_with_union, buf.num_bytes_used, |
| 87 mojo_test_StructOfUnionOfReferences_DeepCopy, |
| 88 mojo_test_StructOfUnionOfReferences_EncodePointersAndHandles, |
| 89 mojo_test_StructOfUnionOfReferences_DecodePointersAndHandles); |
| 90 } |
| 82 } | 91 } |
| 83 | 92 |
| 84 // Test when a union points to a struct. | 93 // Test when a union points to a struct. |
| 85 TEST(UnionSerializationTest, UnionOfStruct) { | 94 TEST(UnionSerializationTest, UnionOfStruct) { |
| 86 char buffer_bytes[1000]; | 95 char buffer_bytes[1000]; |
| 87 MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0}; | 96 MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0}; |
| 88 | 97 |
| 89 struct mojo_test_StructOfUnionOfReferences* struct_with_union = | 98 struct mojo_test_StructOfUnionOfReferences* struct_with_union = |
| 90 MakeStructOfUnionReference(&buf); | 99 MakeStructOfUnionReference(&buf); |
| 91 | 100 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 108 EXPECT_EQ(8u + 1 * 16u // 1 union type (set to DummyStruct) | 117 EXPECT_EQ(8u + 1 * 16u // 1 union type (set to DummyStruct) |
| 109 + 16u, // DummyStruct is out of line. | 118 + 16u, // DummyStruct is out of line. |
| 110 mojo_test_StructOfUnionOfReferences_ComputeSerializedSize( | 119 mojo_test_StructOfUnionOfReferences_ComputeSerializedSize( |
| 111 struct_with_union)); | 120 struct_with_union)); |
| 112 | 121 |
| 113 // We save the underlying (unencoded) buffer. We can compare the two after | 122 // We save the underlying (unencoded) buffer. We can compare the two after |
| 114 // deserialization to make sure deserialization is correct. | 123 // deserialization to make sure deserialization is correct. |
| 115 char buffer_bytes_copy[sizeof(buffer_bytes)]; | 124 char buffer_bytes_copy[sizeof(buffer_bytes)]; |
| 116 memcpy(buffer_bytes_copy, buffer_bytes, sizeof(buffer_bytes_copy)); | 125 memcpy(buffer_bytes_copy, buffer_bytes, sizeof(buffer_bytes_copy)); |
| 117 | 126 |
| 118 struct MojomHandleBuffer handle_buf = {NULL, 0u, 0u}; | |
| 119 mojo_test_StructOfUnionOfReferences_EncodePointersAndHandles( | 127 mojo_test_StructOfUnionOfReferences_EncodePointersAndHandles( |
| 120 struct_with_union, buf.num_bytes_used, &handle_buf); | 128 struct_with_union, buf.num_bytes_used, NULL); |
| 121 EXPECT_EQ(0u, handle_buf.num_handles_used); | |
| 122 | 129 |
| 123 EXPECT_EQ(sizeof(struct mojo_test_StructOfUnionOfReferences) - | 130 EXPECT_EQ(sizeof(struct mojo_test_StructOfUnionOfReferences) - |
| 124 offsetof(struct mojo_test_StructOfUnionOfReferences, u.data), | 131 offsetof(struct mojo_test_StructOfUnionOfReferences, u.data), |
| 125 struct_with_union->u.data.f_dummy_struct.offset); | 132 struct_with_union->u.data.f_dummy_struct.offset); |
| 126 | 133 |
| 127 mojo_test_StructOfUnionOfReferences_DecodePointersAndHandles( | 134 mojo_test_StructOfUnionOfReferences_DecodePointersAndHandles( |
| 128 struct_with_union, buf.num_bytes_used, NULL, 0); | 135 struct_with_union, buf.num_bytes_used, NULL, 0); |
| 129 EXPECT_EQ(0, memcmp(buf.buf, buffer_bytes_copy, buf.num_bytes_used)); | 136 EXPECT_EQ(0, memcmp(buf.buf, buffer_bytes_copy, buf.num_bytes_used)); |
| 137 |
| 138 { |
| 139 char buffer_bytes2[sizeof(buffer_bytes)] = {0}; |
| 140 struct MojomBuffer buf2 = {buffer_bytes2, sizeof(buffer_bytes2), 0}; |
| 141 CopyAndCompare( |
| 142 &buf2, struct_with_union, buf.num_bytes_used, |
| 143 mojo_test_StructOfUnionOfReferences_DeepCopy, |
| 144 mojo_test_StructOfUnionOfReferences_EncodePointersAndHandles, |
| 145 mojo_test_StructOfUnionOfReferences_DecodePointersAndHandles); |
| 146 } |
| 130 } | 147 } |
| 131 | 148 |
| 132 // Test when a union points to an array of int32 | 149 // Test when a union points to an array of int32 |
| 133 TEST(UnionSerializationTest, UnionOfArray) { | 150 TEST(UnionSerializationTest, UnionOfArray) { |
| 134 char buffer_bytes[1000]; | 151 char buffer_bytes[1000]; |
| 135 MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0}; | 152 MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0}; |
| 136 struct mojo_test_StructOfUnionOfReferences* struct_with_union = | 153 struct mojo_test_StructOfUnionOfReferences* struct_with_union = |
| 137 MakeStructOfUnionReference(&buf); | 154 MakeStructOfUnionReference(&buf); |
| 138 | 155 |
| 139 struct MojomArrayHeader* array_of_int32 = | 156 struct MojomArrayHeader* array_of_int32 = |
| (...skipping 23 matching lines...) Expand all Loading... |
| 163 struct_with_union, buf.num_bytes_used, &handle_buf); | 180 struct_with_union, buf.num_bytes_used, &handle_buf); |
| 164 EXPECT_EQ(0u, handle_buf.num_handles_used); | 181 EXPECT_EQ(0u, handle_buf.num_handles_used); |
| 165 | 182 |
| 166 EXPECT_EQ(sizeof(struct mojo_test_StructOfUnionOfReferences) - | 183 EXPECT_EQ(sizeof(struct mojo_test_StructOfUnionOfReferences) - |
| 167 offsetof(struct mojo_test_StructOfUnionOfReferences, u.data), | 184 offsetof(struct mojo_test_StructOfUnionOfReferences, u.data), |
| 168 struct_with_union->u.data.f_int_array.offset); | 185 struct_with_union->u.data.f_int_array.offset); |
| 169 | 186 |
| 170 mojo_test_StructOfUnionOfReferences_DecodePointersAndHandles( | 187 mojo_test_StructOfUnionOfReferences_DecodePointersAndHandles( |
| 171 struct_with_union, buf.num_bytes_used, NULL, 0); | 188 struct_with_union, buf.num_bytes_used, NULL, 0); |
| 172 EXPECT_EQ(0, memcmp(buf.buf, buffer_bytes_copy, buf.num_bytes_used)); | 189 EXPECT_EQ(0, memcmp(buf.buf, buffer_bytes_copy, buf.num_bytes_used)); |
| 190 |
| 191 { |
| 192 char buffer_bytes2[sizeof(buffer_bytes)] = {0}; |
| 193 struct MojomBuffer buf2 = {buffer_bytes2, sizeof(buffer_bytes2), 0}; |
| 194 CopyAndCompare( |
| 195 &buf2, struct_with_union, buf.num_bytes_used, |
| 196 mojo_test_StructOfUnionOfReferences_DeepCopy, |
| 197 mojo_test_StructOfUnionOfReferences_EncodePointersAndHandles, |
| 198 mojo_test_StructOfUnionOfReferences_DecodePointersAndHandles); |
| 199 } |
| 173 } | 200 } |
| 174 | 201 |
| 175 } // namespace | 202 } // namespace |
| OLD | NEW |