| 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/struct.h" | 7 #include "mojo/public/c/bindings/struct.h" |
| 8 | 8 |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 | 10 |
| 11 #include "mojo/public/c/bindings/array.h" | 11 #include "mojo/public/c/bindings/array.h" |
| 12 #include "mojo/public/c/bindings/lib/util.h" | 12 #include "mojo/public/c/bindings/lib/util.h" |
| 13 #include "mojo/public/c/bindings/tests/testing_util.h" |
| 13 #include "mojo/public/cpp/system/macros.h" | 14 #include "mojo/public/cpp/system/macros.h" |
| 14 #include "mojo/public/interfaces/bindings/tests/rect.mojom-c.h" | 15 #include "mojo/public/interfaces/bindings/tests/rect.mojom-c.h" |
| 15 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom-c.h" | 16 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom-c.h" |
| 16 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom-c.h" | 17 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom-c.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 #define BYTES_LEFT_AFTER_FIELD(type, field) \ | 22 #define BYTES_LEFT_AFTER_FIELD(type, field) \ |
| 22 (sizeof(type) - offsetof(type, field)) | 23 (sizeof(type) - offsetof(type, field)) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 40 const char* chars, | 41 const char* chars, |
| 41 size_t num_chars) { | 42 size_t num_chars) { |
| 42 struct MojomArrayHeader* arr = MojomArray_New(buf, num_chars, 1); | 43 struct MojomArrayHeader* arr = MojomArray_New(buf, num_chars, 1); |
| 43 assert(NULL != arr); | 44 assert(NULL != arr); |
| 44 | 45 |
| 45 memcpy((char*)arr + sizeof(MojomStringHeader), chars, num_chars); | 46 memcpy((char*)arr + sizeof(MojomStringHeader), chars, num_chars); |
| 46 return (struct MojomStringHeader*)arr; | 47 return (struct MojomStringHeader*)arr; |
| 47 } | 48 } |
| 48 | 49 |
| 49 TEST(StructSerializedSizeTest, Basic) { | 50 TEST(StructSerializedSizeTest, Basic) { |
| 51 struct mojo_test_Rect rect = {{ |
| 52 sizeof(struct mojo_test_Rect), 0, |
| 53 }, |
| 54 0u, |
| 55 0u, |
| 56 0u, |
| 57 0u}; |
| 58 size_t size = mojo_test_Rect_ComputeSerializedSize(&rect); |
| 59 EXPECT_EQ(8U + 16U, size); |
| 60 |
| 50 char buffer_bytes[1000]; | 61 char buffer_bytes[1000]; |
| 51 struct MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0}; | 62 struct MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0}; |
| 52 struct mojo_test_Rect* rect = MakeRect(&buf); | 63 CopyAndCompare(&buf, &rect, sizeof(rect), mojo_test_Rect_DeepCopy, |
| 53 | 64 mojo_test_Rect_EncodePointersAndHandles, |
| 54 size_t size = mojo_test_Rect_ComputeSerializedSize(rect); | 65 mojo_test_Rect_DecodePointersAndHandles); |
| 55 EXPECT_EQ(8U + 16U, size); | |
| 56 } | 66 } |
| 57 | 67 |
| 58 TEST(StructSerializationTest, StructOfStructs) { | 68 TEST(StructSerializationTest, StructOfStructs) { |
| 59 char buffer_bytes[1000] = {0}; | 69 char buffer_bytes[1000] = {0}; |
| 60 struct MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0}; | 70 struct MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0}; |
| 61 | 71 |
| 62 struct mojo_test_RectPair* pair = static_cast<struct mojo_test_RectPair*>( | 72 struct mojo_test_RectPair* pair = static_cast<struct mojo_test_RectPair*>( |
| 63 MojomBuffer_Allocate(&buf, sizeof(struct mojo_test_RectPair))); | 73 MojomBuffer_Allocate(&buf, sizeof(struct mojo_test_RectPair))); |
| 64 *pair = mojo_test_RectPair{ | 74 *pair = mojo_test_RectPair{ |
| 65 {sizeof(struct mojo_test_RectPair), 0}, | 75 {sizeof(struct mojo_test_RectPair), 0}, |
| 66 {MakeRect(&buf)}, // first | 76 {MakeRect(&buf)}, // first |
| 67 {MakeRect(&buf)}, // second | 77 {MakeRect(&buf)}, // second |
| 68 }; | 78 }; |
| 69 | 79 |
| 70 EXPECT_EQ(8U + 16U + 2 * (8U + 16U), | 80 EXPECT_EQ(8U + 16U + 2 * (8U + 16U), |
| 71 mojo_test_RectPair_ComputeSerializedSize(pair)); | 81 mojo_test_RectPair_ComputeSerializedSize(pair)); |
| 72 | 82 |
| 73 // We save the underlying (unencoded) buffer. We can compare the two after | 83 // We save the underlying (unencoded) buffer. We can compare the two after |
| 74 // deserialization to make sure deserialization is correct. | 84 // deserialization to make sure deserialization is correct. |
| 75 char buffer_bytes_copy[sizeof(buffer_bytes)]; | 85 char buffer_bytes_copy[sizeof(buffer_bytes)]; |
| 76 memcpy(buffer_bytes_copy, buffer_bytes, sizeof(buffer_bytes_copy)); | 86 memcpy(buffer_bytes_copy, buffer_bytes, sizeof(buffer_bytes_copy)); |
| 77 | 87 |
| 78 struct MojomHandleBuffer handle_buf = {NULL, 0u, 0u}; | 88 mojo_test_RectPair_EncodePointersAndHandles(pair, buf.num_bytes_used, NULL); |
| 79 mojo_test_RectPair_EncodePointersAndHandles(pair, buf.num_bytes_used, | |
| 80 &handle_buf); | |
| 81 EXPECT_EQ(0u, handle_buf.num_handles_used); | |
| 82 | 89 |
| 83 EXPECT_EQ(BYTES_LEFT_AFTER_FIELD(struct mojo_test_RectPair, first), | 90 EXPECT_EQ(BYTES_LEFT_AFTER_FIELD(struct mojo_test_RectPair, first), |
| 84 pair->first.offset); | 91 pair->first.offset); |
| 85 EXPECT_EQ(BYTES_LEFT_AFTER_FIELD(struct mojo_test_RectPair, second) + | 92 EXPECT_EQ(BYTES_LEFT_AFTER_FIELD(struct mojo_test_RectPair, second) + |
| 86 sizeof(struct mojo_test_Rect), | 93 sizeof(struct mojo_test_Rect), |
| 87 pair->second.offset); | 94 pair->second.offset); |
| 88 | 95 |
| 89 mojo_test_RectPair_DecodePointersAndHandles( | 96 mojo_test_RectPair_DecodePointersAndHandles( |
| 90 reinterpret_cast<struct mojo_test_RectPair*>(buf.buf), buf.num_bytes_used, | 97 reinterpret_cast<struct mojo_test_RectPair*>(buf.buf), buf.num_bytes_used, |
| 91 NULL, 0); | 98 NULL, 0); |
| 92 EXPECT_EQ(0, memcmp(buf.buf, buffer_bytes_copy, buf.num_bytes_used)); | 99 EXPECT_EQ(0, memcmp(buf.buf, buffer_bytes_copy, buf.num_bytes_used)); |
| 100 |
| 101 { |
| 102 char buffer_bytes2[1000] = {0}; |
| 103 struct MojomBuffer buf2 = {buffer_bytes2, sizeof(buffer_bytes2), 0}; |
| 104 CopyAndCompare(&buf2, pair, buf.num_bytes_used, mojo_test_RectPair_DeepCopy, |
| 105 mojo_test_RectPair_EncodePointersAndHandles, |
| 106 mojo_test_RectPair_DecodePointersAndHandles); |
| 107 } |
| 93 } | 108 } |
| 94 | 109 |
| 95 // Tests a struct that has: | 110 // Tests a struct that has: |
| 96 // - nullable string which isn't null. | 111 // - nullable string which isn't null. |
| 97 // - nullable array of rects, which isn't null. | 112 // - nullable array of rects, which isn't null. |
| 98 TEST(StructSerializationTest, StructOfArrays) { | 113 TEST(StructSerializationTest, StructOfArrays) { |
| 99 char buffer_bytes[1000]; | 114 char buffer_bytes[1000]; |
| 100 MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0}; | 115 MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0}; |
| 101 | 116 |
| 102 const char* kRegionName = "region"; | 117 const char* kRegionName = "region"; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 8U + // name pointer | 149 8U + // name pointer |
| 135 8U + // rects pointer | 150 8U + // rects pointer |
| 136 8U + // name header | 151 8U + // name header |
| 137 8U + // name payload (rounded up) | 152 8U + // name payload (rounded up) |
| 138 8U + // rects header | 153 8U + // rects header |
| 139 4 * 8U + // rects payload (four pointers) | 154 4 * 8U + // rects payload (four pointers) |
| 140 4 * (8U + // rect header | 155 4 * (8U + // rect header |
| 141 16U), // rect payload (four ints) | 156 16U), // rect payload (four ints) |
| 142 size); | 157 size); |
| 143 | 158 |
| 144 char buffer_bytes_copy[sizeof(buffer_bytes)]; | 159 char buffer_bytes_copy[sizeof(buffer_bytes)] = {0}; |
| 145 memcpy(buffer_bytes_copy, buffer_bytes, sizeof(buffer_bytes_copy)); | 160 memcpy(buffer_bytes_copy, buffer_bytes, sizeof(buffer_bytes_copy)); |
| 146 | 161 |
| 147 struct MojomHandleBuffer handle_buf = {NULL, 0u, 0u}; | 162 mojo_test_NamedRegion_EncodePointersAndHandles(named_region, |
| 148 mojo_test_NamedRegion_EncodePointersAndHandles( | 163 buf.num_bytes_used, NULL); |
| 149 named_region, buf.num_bytes_used, &handle_buf); | |
| 150 EXPECT_EQ(0u, handle_buf.num_handles_used); | |
| 151 | 164 |
| 152 EXPECT_EQ(BYTES_LEFT_AFTER_FIELD(struct mojo_test_NamedRegion, name), | 165 EXPECT_EQ(BYTES_LEFT_AFTER_FIELD(struct mojo_test_NamedRegion, name), |
| 153 named_region->name.offset); | 166 named_region->name.offset); |
| 154 EXPECT_EQ(BYTES_LEFT_AFTER_FIELD(struct mojo_test_NamedRegion, rects) + | 167 EXPECT_EQ(BYTES_LEFT_AFTER_FIELD(struct mojo_test_NamedRegion, rects) + |
| 155 MOJOM_INTERNAL_ROUND_TO_8(sizeof(struct MojomStringHeader) + | 168 MOJOM_INTERNAL_ROUND_TO_8(sizeof(struct MojomStringHeader) + |
| 156 strlen(kRegionName)), | 169 strlen(kRegionName)), |
| 157 named_region->rects.offset); | 170 named_region->rects.offset); |
| 158 | 171 |
| 159 // Test the offsets encoded inside the rect array. | 172 // Test the offsets encoded inside the rect array. |
| 160 EXPECT_EQ(sizeof(union mojo_test_RectPtr) * 4, | 173 EXPECT_EQ(sizeof(union mojo_test_RectPtr) * 4, |
| 161 MOJOM_ARRAY_INDEX(rects, union mojo_test_RectPtr, 0)->offset); | 174 MOJOM_ARRAY_INDEX(rects, union mojo_test_RectPtr, 0)->offset); |
| 162 EXPECT_EQ(sizeof(union mojo_test_RectPtr) * 3 + sizeof(struct mojo_test_Rect), | 175 EXPECT_EQ(sizeof(union mojo_test_RectPtr) * 3 + sizeof(struct mojo_test_Rect), |
| 163 MOJOM_ARRAY_INDEX(rects, union mojo_test_RectPtr, 1)->offset); | 176 MOJOM_ARRAY_INDEX(rects, union mojo_test_RectPtr, 1)->offset); |
| 164 EXPECT_EQ( | 177 EXPECT_EQ( |
| 165 sizeof(union mojo_test_RectPtr) * 2 + sizeof(struct mojo_test_Rect) * 2, | 178 sizeof(union mojo_test_RectPtr) * 2 + sizeof(struct mojo_test_Rect) * 2, |
| 166 MOJOM_ARRAY_INDEX(rects, union mojo_test_RectPtr, 2)->offset); | 179 MOJOM_ARRAY_INDEX(rects, union mojo_test_RectPtr, 2)->offset); |
| 167 EXPECT_EQ(sizeof(union mojo_test_RectPtr) + sizeof(struct mojo_test_Rect) * 3, | 180 EXPECT_EQ(sizeof(union mojo_test_RectPtr) + sizeof(struct mojo_test_Rect) * 3, |
| 168 MOJOM_ARRAY_INDEX(rects, union mojo_test_RectPtr, 3)->offset); | 181 MOJOM_ARRAY_INDEX(rects, union mojo_test_RectPtr, 3)->offset); |
| 169 | 182 |
| 170 mojo_test_NamedRegion_DecodePointersAndHandles( | 183 mojo_test_NamedRegion_DecodePointersAndHandles( |
| 171 reinterpret_cast<struct mojo_test_NamedRegion*>(buf.buf), | 184 reinterpret_cast<struct mojo_test_NamedRegion*>(buf.buf), |
| 172 buf.num_bytes_used, NULL, 0); | 185 buf.num_bytes_used, NULL, 0); |
| 173 EXPECT_EQ(0, memcmp(buf.buf, buffer_bytes_copy, buf.num_bytes_used)); | 186 EXPECT_EQ(0, memcmp(buf.buf, buffer_bytes_copy, buf.num_bytes_used)); |
| 187 |
| 188 { |
| 189 char buffer_bytes2[sizeof(buffer_bytes)] = {0}; |
| 190 struct MojomBuffer buf2 = {buffer_bytes2, sizeof(buffer_bytes2), 0}; |
| 191 CopyAndCompare(&buf2, named_region, buf.num_bytes_used, |
| 192 mojo_test_NamedRegion_DeepCopy, |
| 193 mojo_test_NamedRegion_EncodePointersAndHandles, |
| 194 mojo_test_NamedRegion_DecodePointersAndHandles); |
| 195 } |
| 174 } | 196 } |
| 175 | 197 |
| 176 // Tests a struct that has: | 198 // Tests a struct that has: |
| 177 // - nullable string which is null. | 199 // - nullable string which is null. |
| 178 // - nullable array of rects, which is null. | 200 // - nullable array of rects, which is null. |
| 179 TEST(StructSerializationTest, StructOfNullArrays) { | 201 TEST(StructSerializationTest, StructOfNullArrays) { |
| 180 struct mojo_test_NamedRegion named_region = { | 202 struct mojo_test_NamedRegion named_region = { |
| 181 // header | 203 // header |
| 182 { | 204 { |
| 183 sizeof(struct mojo_test_NamedRegion), 0, | 205 sizeof(struct mojo_test_NamedRegion), 0, |
| 184 }, | 206 }, |
| 185 {NULL}, | 207 {NULL}, |
| 186 {NULL}, | 208 {NULL}, |
| 187 }; | 209 }; |
| 188 struct mojo_test_NamedRegion named_region_copy = named_region; | 210 struct mojo_test_NamedRegion named_region_copy = named_region; |
| 189 | 211 |
| 190 size_t size = mojo_test_NamedRegion_ComputeSerializedSize(&named_region); | 212 size_t size = mojo_test_NamedRegion_ComputeSerializedSize(&named_region); |
| 191 EXPECT_EQ(8U + // header | 213 EXPECT_EQ(8U + // header |
| 192 8U + // name pointer | 214 8U + // name pointer |
| 193 8U, // rects pointer | 215 8U, // rects pointer |
| 194 size); | 216 size); |
| 195 | 217 |
| 196 struct MojomHandleBuffer handle_buf = {NULL, 0u, 0u}; | |
| 197 mojo_test_NamedRegion_EncodePointersAndHandles( | 218 mojo_test_NamedRegion_EncodePointersAndHandles( |
| 198 &named_region, sizeof(struct mojo_test_NamedRegion), &handle_buf); | 219 &named_region, sizeof(struct mojo_test_NamedRegion), NULL); |
| 199 EXPECT_EQ(0u, handle_buf.num_handles_used); | |
| 200 EXPECT_EQ(0u, named_region.name.offset); | 220 EXPECT_EQ(0u, named_region.name.offset); |
| 201 EXPECT_EQ(0u, named_region.rects.offset); | 221 EXPECT_EQ(0u, named_region.rects.offset); |
| 202 | 222 |
| 203 mojo_test_NamedRegion_DecodePointersAndHandles( | 223 mojo_test_NamedRegion_DecodePointersAndHandles( |
| 204 &named_region, sizeof(struct mojo_test_NamedRegion), NULL, 0); | 224 &named_region, sizeof(struct mojo_test_NamedRegion), NULL, 0); |
| 205 EXPECT_EQ(0, memcmp(&named_region, &named_region_copy, | 225 EXPECT_EQ(0, memcmp(&named_region, &named_region_copy, |
| 206 sizeof(struct mojo_test_NamedRegion))); | 226 sizeof(struct mojo_test_NamedRegion))); |
| 227 |
| 228 { |
| 229 char buffer_bytes2[sizeof(named_region)] = {0}; |
| 230 struct MojomBuffer buf2 = {buffer_bytes2, sizeof(buffer_bytes2), 0}; |
| 231 CopyAndCompare(&buf2, &named_region, sizeof(named_region), |
| 232 mojo_test_NamedRegion_DeepCopy, |
| 233 mojo_test_NamedRegion_EncodePointersAndHandles, |
| 234 mojo_test_NamedRegion_DecodePointersAndHandles); |
| 235 } |
| 207 } | 236 } |
| 208 | 237 |
| 209 TEST(StructSerializationTest, StructOfUnion) { | 238 TEST(StructSerializationTest, StructOfUnion) { |
| 210 struct mojo_test_SmallStructNonNullableUnion u = { | 239 struct mojo_test_SmallStructNonNullableUnion u = { |
| 211 // header | 240 // header |
| 212 { | 241 { |
| 213 sizeof(struct mojo_test_SmallStructNonNullableUnion), | 242 sizeof(struct mojo_test_SmallStructNonNullableUnion), |
| 214 0, // version | 243 0, // version |
| 215 }, | 244 }, |
| 216 // PodUnion | 245 // PodUnion |
| (...skipping 21 matching lines...) Expand all Loading... |
| 238 EXPECT_EQ(8U + // header | 267 EXPECT_EQ(8U + // header |
| 239 16U, // union | 268 16U, // union |
| 240 mojo_test_SmallStructNonNullableUnion_ComputeSerializedSize(&u)); | 269 mojo_test_SmallStructNonNullableUnion_ComputeSerializedSize(&u)); |
| 241 EXPECT_EQ( | 270 EXPECT_EQ( |
| 242 8U + // header | 271 8U + // header |
| 243 16U, // union | 272 16U, // union |
| 244 mojo_test_SmallStructNonNullableUnion_ComputeSerializedSize(&u_null)); | 273 mojo_test_SmallStructNonNullableUnion_ComputeSerializedSize(&u_null)); |
| 245 | 274 |
| 246 // Encoding shouldn't have done anything to these structs (they have no | 275 // Encoding shouldn't have done anything to these structs (they have no |
| 247 // pointers or handles): | 276 // pointers or handles): |
| 248 struct MojomHandleBuffer handle_buf = {NULL, 0u, 0u}; | |
| 249 struct mojo_test_SmallStructNonNullableUnion u_copy = u; | 277 struct mojo_test_SmallStructNonNullableUnion u_copy = u; |
| 250 mojo_test_SmallStructNonNullableUnion_EncodePointersAndHandles( | 278 mojo_test_SmallStructNonNullableUnion_EncodePointersAndHandles( |
| 251 &u, sizeof(struct mojo_test_SmallStructNonNullableUnion), &handle_buf); | 279 &u, sizeof(struct mojo_test_SmallStructNonNullableUnion), NULL); |
| 252 EXPECT_EQ(0u, handle_buf.num_handles_used); | |
| 253 EXPECT_EQ(0, memcmp(&u, &u_copy, sizeof(u))); | 280 EXPECT_EQ(0, memcmp(&u, &u_copy, sizeof(u))); |
| 254 | 281 |
| 255 // Similarly, decoding shouldn't change the struct at all: | 282 // Similarly, decoding shouldn't change the struct at all: |
| 256 mojo_test_SmallStructNonNullableUnion_DecodePointersAndHandles( | 283 mojo_test_SmallStructNonNullableUnion_DecodePointersAndHandles( |
| 257 &u, sizeof(struct mojo_test_SmallStructNonNullableUnion), NULL, 0); | 284 &u, sizeof(struct mojo_test_SmallStructNonNullableUnion), NULL, 0); |
| 258 EXPECT_EQ(0, memcmp(&u, &u_copy, sizeof(u))); | 285 EXPECT_EQ(0, memcmp(&u, &u_copy, sizeof(u))); |
| 259 | 286 |
| 260 struct mojo_test_SmallStructNonNullableUnion u_null_copy = u_null; | 287 struct mojo_test_SmallStructNonNullableUnion u_null_copy = u_null; |
| 261 mojo_test_SmallStructNonNullableUnion_EncodePointersAndHandles( | 288 mojo_test_SmallStructNonNullableUnion_EncodePointersAndHandles( |
| 262 &u_null, sizeof(struct mojo_test_SmallStructNonNullableUnion), | 289 &u_null, sizeof(struct mojo_test_SmallStructNonNullableUnion), NULL); |
| 263 &handle_buf); | |
| 264 EXPECT_EQ(0u, handle_buf.num_handles_used); | |
| 265 EXPECT_EQ(0, memcmp(&u_null, &u_null_copy, sizeof(u_null))); | 290 EXPECT_EQ(0, memcmp(&u_null, &u_null_copy, sizeof(u_null))); |
| 266 | 291 |
| 267 mojo_test_SmallStructNonNullableUnion_DecodePointersAndHandles( | 292 mojo_test_SmallStructNonNullableUnion_DecodePointersAndHandles( |
| 268 &u_null, sizeof(struct mojo_test_SmallStructNonNullableUnion), NULL, 0); | 293 &u_null, sizeof(struct mojo_test_SmallStructNonNullableUnion), NULL, 0); |
| 269 EXPECT_EQ(0, memcmp(&u_null, &u_null_copy, sizeof(u_null))); | 294 EXPECT_EQ(0, memcmp(&u_null, &u_null_copy, sizeof(u_null))); |
| 295 |
| 296 { |
| 297 char buffer_bytes[sizeof(u)] = {0}; |
| 298 struct MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0}; |
| 299 CopyAndCompare( |
| 300 &buf, &u, sizeof(u), mojo_test_SmallStructNonNullableUnion_DeepCopy, |
| 301 mojo_test_SmallStructNonNullableUnion_EncodePointersAndHandles, |
| 302 mojo_test_SmallStructNonNullableUnion_DecodePointersAndHandles); |
| 303 } |
| 270 } | 304 } |
| 271 | 305 |
| 272 TEST(StructSerializationTest, StructWithHandle) { | 306 TEST(StructSerializationTest, StructWithHandle) { |
| 273 struct mojo_test_NullableHandleStruct handle_struct = | 307 struct mojo_test_NullableHandleStruct handle_struct = |
| 274 mojo_test_NullableHandleStruct{ | 308 mojo_test_NullableHandleStruct{ |
| 275 // header | 309 // header |
| 276 { | 310 { |
| 277 sizeof(struct mojo_test_NullableHandleStruct), 0, | 311 sizeof(struct mojo_test_NullableHandleStruct), 0, |
| 278 }, | 312 }, |
| 279 MOJO_HANDLE_INVALID, | 313 MOJO_HANDLE_INVALID, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 303 &handle_buf); | 337 &handle_buf); |
| 304 EXPECT_EQ(1u, handle_buf.num_handles_used); | 338 EXPECT_EQ(1u, handle_buf.num_handles_used); |
| 305 EXPECT_EQ(static_cast<MojoHandle>(0), handle_struct.h); | 339 EXPECT_EQ(static_cast<MojoHandle>(0), handle_struct.h); |
| 306 EXPECT_EQ(static_cast<MojoHandle>(123), handles[0]); | 340 EXPECT_EQ(static_cast<MojoHandle>(123), handles[0]); |
| 307 | 341 |
| 308 mojo_test_NullableHandleStruct_DecodePointersAndHandles( | 342 mojo_test_NullableHandleStruct_DecodePointersAndHandles( |
| 309 &handle_struct, sizeof(struct mojo_test_NullableHandleStruct), handles, | 343 &handle_struct, sizeof(struct mojo_test_NullableHandleStruct), handles, |
| 310 MOJO_ARRAYSIZE(handles)); | 344 MOJO_ARRAYSIZE(handles)); |
| 311 EXPECT_EQ(static_cast<MojoHandle>(123), handle_struct.h); | 345 EXPECT_EQ(static_cast<MojoHandle>(123), handle_struct.h); |
| 312 EXPECT_EQ(MOJO_HANDLE_INVALID, handles[0]); | 346 EXPECT_EQ(MOJO_HANDLE_INVALID, handles[0]); |
| 347 |
| 348 { |
| 349 char buffer_bytes[1000] = {0}; |
| 350 struct MojomBuffer buf = {buffer_bytes, 4, 0}; |
| 351 auto* copied_struct = |
| 352 mojo_test_NullableHandleStruct_DeepCopy(&buf, &handle_struct); |
| 353 // Not enough space: |
| 354 ASSERT_FALSE(copied_struct); |
| 355 buf.buf_size = sizeof(buffer_bytes); |
| 356 copied_struct = |
| 357 mojo_test_NullableHandleStruct_DeepCopy(&buf, &handle_struct); |
| 358 ASSERT_TRUE(copied_struct); |
| 359 // The old and the new copy should both have the handles. |
| 360 EXPECT_EQ(static_cast<MojoHandle>(123), handle_struct.h); |
| 361 EXPECT_EQ(static_cast<MojoHandle>(123), copied_struct->h); |
| 362 } |
| 313 } | 363 } |
| 314 | 364 |
| 315 } // namespace | 365 } // namespace |
| OLD | NEW |