| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 #include <utility> | 7 #include <utility> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 EXPECT_EQ(10, pod_clone->get_f_int8()); | 116 EXPECT_EQ(10, pod_clone->get_f_int8()); |
| 117 EXPECT_TRUE(pod_clone->is_f_int8()); | 117 EXPECT_TRUE(pod_clone->is_f_int8()); |
| 118 EXPECT_EQ(pod_clone->which(), PodUnion::Tag::F_INT8); | 118 EXPECT_EQ(pod_clone->which(), PodUnion::Tag::F_INT8); |
| 119 } | 119 } |
| 120 | 120 |
| 121 TEST(UnionTest, PodSerialization) { | 121 TEST(UnionTest, PodSerialization) { |
| 122 PodUnionPtr pod1(PodUnion::New()); | 122 PodUnionPtr pod1(PodUnion::New()); |
| 123 pod1->set_f_int8(10); | 123 pod1->set_f_int8(10); |
| 124 | 124 |
| 125 mojo::internal::SerializationContext context; | 125 mojo::internal::SerializationContext context; |
| 126 size_t size = | 126 size_t size = mojo::internal::PrepareToSerialize<PodUnionDataView>( |
| 127 mojo::internal::PrepareToSerialize<PodUnionPtr>(pod1, false, &context); | 127 pod1, false, &context); |
| 128 EXPECT_EQ(16U, size); | 128 EXPECT_EQ(16U, size); |
| 129 | 129 |
| 130 mojo::internal::FixedBufferForTesting buf(size); | 130 mojo::internal::FixedBufferForTesting buf(size); |
| 131 internal::PodUnion_Data* data = nullptr; | 131 internal::PodUnion_Data* data = nullptr; |
| 132 mojo::internal::Serialize<PodUnionPtr>(pod1, &buf, &data, false, &context); | 132 mojo::internal::Serialize<PodUnionDataView>(pod1, &buf, &data, false, |
| 133 &context); |
| 133 | 134 |
| 134 PodUnionPtr pod2; | 135 PodUnionPtr pod2; |
| 135 mojo::internal::Deserialize<PodUnionPtr>(data, &pod2, &context); | 136 mojo::internal::Deserialize<PodUnionDataView>(data, &pod2, &context); |
| 136 | 137 |
| 137 EXPECT_EQ(10, pod2->get_f_int8()); | 138 EXPECT_EQ(10, pod2->get_f_int8()); |
| 138 EXPECT_TRUE(pod2->is_f_int8()); | 139 EXPECT_TRUE(pod2->is_f_int8()); |
| 139 EXPECT_EQ(pod2->which(), PodUnion::Tag::F_INT8); | 140 EXPECT_EQ(pod2->which(), PodUnion::Tag::F_INT8); |
| 140 } | 141 } |
| 141 | 142 |
| 142 TEST(UnionTest, EnumSerialization) { | 143 TEST(UnionTest, EnumSerialization) { |
| 143 PodUnionPtr pod1(PodUnion::New()); | 144 PodUnionPtr pod1(PodUnion::New()); |
| 144 pod1->set_f_enum(AnEnum::SECOND); | 145 pod1->set_f_enum(AnEnum::SECOND); |
| 145 | 146 |
| 146 size_t size = | 147 size_t size = mojo::internal::PrepareToSerialize<PodUnionDataView>( |
| 147 mojo::internal::PrepareToSerialize<PodUnionPtr>(pod1, false, nullptr); | 148 pod1, false, nullptr); |
| 148 EXPECT_EQ(16U, size); | 149 EXPECT_EQ(16U, size); |
| 149 | 150 |
| 150 mojo::internal::FixedBufferForTesting buf(size); | 151 mojo::internal::FixedBufferForTesting buf(size); |
| 151 internal::PodUnion_Data* data = nullptr; | 152 internal::PodUnion_Data* data = nullptr; |
| 152 mojo::internal::Serialize<PodUnionPtr>(pod1, &buf, &data, false, nullptr); | 153 mojo::internal::Serialize<PodUnionDataView>(pod1, &buf, &data, false, |
| 154 nullptr); |
| 153 | 155 |
| 154 PodUnionPtr pod2; | 156 PodUnionPtr pod2; |
| 155 mojo::internal::Deserialize<PodUnionPtr>(data, &pod2, nullptr); | 157 mojo::internal::Deserialize<PodUnionDataView>(data, &pod2, nullptr); |
| 156 | 158 |
| 157 EXPECT_EQ(AnEnum::SECOND, pod2->get_f_enum()); | 159 EXPECT_EQ(AnEnum::SECOND, pod2->get_f_enum()); |
| 158 EXPECT_TRUE(pod2->is_f_enum()); | 160 EXPECT_TRUE(pod2->is_f_enum()); |
| 159 EXPECT_EQ(pod2->which(), PodUnion::Tag::F_ENUM); | 161 EXPECT_EQ(pod2->which(), PodUnion::Tag::F_ENUM); |
| 160 } | 162 } |
| 161 | 163 |
| 162 TEST(UnionTest, PodValidation) { | 164 TEST(UnionTest, PodValidation) { |
| 163 PodUnionPtr pod(PodUnion::New()); | 165 PodUnionPtr pod(PodUnion::New()); |
| 164 pod->set_f_int8(10); | 166 pod->set_f_int8(10); |
| 165 | 167 |
| 166 size_t size = | 168 size_t size = |
| 167 mojo::internal::PrepareToSerialize<PodUnionPtr>(pod, false, nullptr); | 169 mojo::internal::PrepareToSerialize<PodUnionDataView>(pod, false, nullptr); |
| 168 EXPECT_EQ(16U, size); | 170 EXPECT_EQ(16U, size); |
| 169 | 171 |
| 170 mojo::internal::FixedBufferForTesting buf(size); | 172 mojo::internal::FixedBufferForTesting buf(size); |
| 171 internal::PodUnion_Data* data = nullptr; | 173 internal::PodUnion_Data* data = nullptr; |
| 172 mojo::internal::Serialize<PodUnionPtr>(pod, &buf, &data, false, nullptr); | 174 mojo::internal::Serialize<PodUnionDataView>(pod, &buf, &data, false, nullptr); |
| 173 | 175 |
| 174 void* raw_buf = buf.Leak(); | 176 void* raw_buf = buf.Leak(); |
| 175 mojo::internal::ValidationContext validation_context( | 177 mojo::internal::ValidationContext validation_context( |
| 176 data, static_cast<uint32_t>(size), 0); | 178 data, static_cast<uint32_t>(size), 0); |
| 177 EXPECT_TRUE( | 179 EXPECT_TRUE( |
| 178 internal::PodUnion_Data::Validate(raw_buf, &validation_context, false)); | 180 internal::PodUnion_Data::Validate(raw_buf, &validation_context, false)); |
| 179 free(raw_buf); | 181 free(raw_buf); |
| 180 } | 182 } |
| 181 | 183 |
| 182 TEST(UnionTest, SerializeNotNull) { | 184 TEST(UnionTest, SerializeNotNull) { |
| 183 PodUnionPtr pod(PodUnion::New()); | 185 PodUnionPtr pod(PodUnion::New()); |
| 184 pod->set_f_int8(0); | 186 pod->set_f_int8(0); |
| 185 size_t size = | 187 size_t size = |
| 186 mojo::internal::PrepareToSerialize<PodUnionPtr>(pod, false, nullptr); | 188 mojo::internal::PrepareToSerialize<PodUnionDataView>(pod, false, nullptr); |
| 187 mojo::internal::FixedBufferForTesting buf(size); | 189 mojo::internal::FixedBufferForTesting buf(size); |
| 188 internal::PodUnion_Data* data = nullptr; | 190 internal::PodUnion_Data* data = nullptr; |
| 189 mojo::internal::Serialize<PodUnionPtr>(pod, &buf, &data, false, nullptr); | 191 mojo::internal::Serialize<PodUnionDataView>(pod, &buf, &data, false, nullptr); |
| 190 EXPECT_FALSE(data->is_null()); | 192 EXPECT_FALSE(data->is_null()); |
| 191 } | 193 } |
| 192 | 194 |
| 193 TEST(UnionTest, SerializeIsNullInlined) { | 195 TEST(UnionTest, SerializeIsNullInlined) { |
| 194 PodUnionPtr pod; | 196 PodUnionPtr pod; |
| 195 size_t size = | 197 size_t size = |
| 196 mojo::internal::PrepareToSerialize<PodUnionPtr>(pod, false, nullptr); | 198 mojo::internal::PrepareToSerialize<PodUnionDataView>(pod, false, nullptr); |
| 197 EXPECT_EQ(16U, size); | 199 EXPECT_EQ(16U, size); |
| 198 mojo::internal::FixedBufferForTesting buf(size); | 200 mojo::internal::FixedBufferForTesting buf(size); |
| 199 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf); | 201 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf); |
| 200 | 202 |
| 201 // Check that dirty output buffers are handled correctly by serialization. | 203 // Check that dirty output buffers are handled correctly by serialization. |
| 202 data->size = 16U; | 204 data->size = 16U; |
| 203 data->tag = PodUnion::Tag::F_UINT16; | 205 data->tag = PodUnion::Tag::F_UINT16; |
| 204 data->data.f_f_int16 = 20; | 206 data->data.f_f_int16 = 20; |
| 205 | 207 |
| 206 mojo::internal::Serialize<PodUnionPtr>(pod, &buf, &data, true, nullptr); | 208 mojo::internal::Serialize<PodUnionDataView>(pod, &buf, &data, true, nullptr); |
| 207 EXPECT_TRUE(data->is_null()); | 209 EXPECT_TRUE(data->is_null()); |
| 208 | 210 |
| 209 PodUnionPtr pod2; | 211 PodUnionPtr pod2; |
| 210 mojo::internal::Deserialize<PodUnionPtr>(data, &pod2, nullptr); | 212 mojo::internal::Deserialize<PodUnionDataView>(data, &pod2, nullptr); |
| 211 EXPECT_TRUE(pod2.is_null()); | 213 EXPECT_TRUE(pod2.is_null()); |
| 212 } | 214 } |
| 213 | 215 |
| 214 TEST(UnionTest, SerializeIsNullNotInlined) { | 216 TEST(UnionTest, SerializeIsNullNotInlined) { |
| 215 PodUnionPtr pod; | 217 PodUnionPtr pod; |
| 216 size_t size = | 218 size_t size = |
| 217 mojo::internal::PrepareToSerialize<PodUnionPtr>(pod, false, nullptr); | 219 mojo::internal::PrepareToSerialize<PodUnionDataView>(pod, false, nullptr); |
| 218 EXPECT_EQ(16U, size); | 220 EXPECT_EQ(16U, size); |
| 219 mojo::internal::FixedBufferForTesting buf(size); | 221 mojo::internal::FixedBufferForTesting buf(size); |
| 220 internal::PodUnion_Data* data = nullptr; | 222 internal::PodUnion_Data* data = nullptr; |
| 221 mojo::internal::Serialize<PodUnionPtr>(pod, &buf, &data, false, nullptr); | 223 mojo::internal::Serialize<PodUnionDataView>(pod, &buf, &data, false, nullptr); |
| 222 EXPECT_EQ(nullptr, data); | 224 EXPECT_EQ(nullptr, data); |
| 223 } | 225 } |
| 224 | 226 |
| 225 TEST(UnionTest, NullValidation) { | 227 TEST(UnionTest, NullValidation) { |
| 226 void* buf = nullptr; | 228 void* buf = nullptr; |
| 227 mojo::internal::ValidationContext validation_context(buf, 0, 0); | 229 mojo::internal::ValidationContext validation_context(buf, 0, 0); |
| 228 EXPECT_TRUE(internal::PodUnion_Data::Validate( | 230 EXPECT_TRUE(internal::PodUnion_Data::Validate( |
| 229 buf, &validation_context, false)); | 231 buf, &validation_context, false)); |
| 230 } | 232 } |
| 231 | 233 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 EXPECT_FALSE( | 270 EXPECT_FALSE( |
| 269 internal::PodUnion_Data::Validate(raw_buf, &validation_context, false)); | 271 internal::PodUnion_Data::Validate(raw_buf, &validation_context, false)); |
| 270 free(raw_buf); | 272 free(raw_buf); |
| 271 } | 273 } |
| 272 | 274 |
| 273 TEST(UnionTest, UnknownEnumValueValidation) { | 275 TEST(UnionTest, UnknownEnumValueValidation) { |
| 274 PodUnionPtr pod(PodUnion::New()); | 276 PodUnionPtr pod(PodUnion::New()); |
| 275 pod->set_f_enum(static_cast<AnEnum>(0xFFFF)); | 277 pod->set_f_enum(static_cast<AnEnum>(0xFFFF)); |
| 276 | 278 |
| 277 size_t size = | 279 size_t size = |
| 278 mojo::internal::PrepareToSerialize<PodUnionPtr>(pod, false, nullptr); | 280 mojo::internal::PrepareToSerialize<PodUnionDataView>(pod, false, nullptr); |
| 279 EXPECT_EQ(16U, size); | 281 EXPECT_EQ(16U, size); |
| 280 | 282 |
| 281 mojo::internal::FixedBufferForTesting buf(size); | 283 mojo::internal::FixedBufferForTesting buf(size); |
| 282 internal::PodUnion_Data* data = nullptr; | 284 internal::PodUnion_Data* data = nullptr; |
| 283 mojo::internal::Serialize<PodUnionPtr>(pod, &buf, &data, false, nullptr); | 285 mojo::internal::Serialize<PodUnionDataView>(pod, &buf, &data, false, nullptr); |
| 284 | 286 |
| 285 void* raw_buf = buf.Leak(); | 287 void* raw_buf = buf.Leak(); |
| 286 mojo::internal::ValidationContext validation_context( | 288 mojo::internal::ValidationContext validation_context( |
| 287 data, static_cast<uint32_t>(size), 0); | 289 data, static_cast<uint32_t>(size), 0); |
| 288 EXPECT_FALSE( | 290 EXPECT_FALSE( |
| 289 internal::PodUnion_Data::Validate(raw_buf, &validation_context, false)); | 291 internal::PodUnion_Data::Validate(raw_buf, &validation_context, false)); |
| 290 free(raw_buf); | 292 free(raw_buf); |
| 291 } | 293 } |
| 292 | 294 |
| 293 TEST(UnionTest, UnknownExtensibleEnumValueValidation) { | 295 TEST(UnionTest, UnknownExtensibleEnumValueValidation) { |
| 294 PodUnionPtr pod(PodUnion::New()); | 296 PodUnionPtr pod(PodUnion::New()); |
| 295 pod->set_f_extensible_enum(static_cast<AnExtensibleEnum>(0xFFFF)); | 297 pod->set_f_extensible_enum(static_cast<AnExtensibleEnum>(0xFFFF)); |
| 296 | 298 |
| 297 size_t size = | 299 size_t size = |
| 298 mojo::internal::PrepareToSerialize<PodUnionPtr>(pod, false, nullptr); | 300 mojo::internal::PrepareToSerialize<PodUnionDataView>(pod, false, nullptr); |
| 299 EXPECT_EQ(16U, size); | 301 EXPECT_EQ(16U, size); |
| 300 | 302 |
| 301 mojo::internal::FixedBufferForTesting buf(size); | 303 mojo::internal::FixedBufferForTesting buf(size); |
| 302 internal::PodUnion_Data* data = nullptr; | 304 internal::PodUnion_Data* data = nullptr; |
| 303 mojo::internal::Serialize<PodUnionPtr>(pod, &buf, &data, false, nullptr); | 305 mojo::internal::Serialize<PodUnionDataView>(pod, &buf, &data, false, nullptr); |
| 304 | 306 |
| 305 void* raw_buf = buf.Leak(); | 307 void* raw_buf = buf.Leak(); |
| 306 mojo::internal::ValidationContext validation_context( | 308 mojo::internal::ValidationContext validation_context( |
| 307 data, static_cast<uint32_t>(size), 0); | 309 data, static_cast<uint32_t>(size), 0); |
| 308 EXPECT_TRUE( | 310 EXPECT_TRUE( |
| 309 internal::PodUnion_Data::Validate(raw_buf, &validation_context, false)); | 311 internal::PodUnion_Data::Validate(raw_buf, &validation_context, false)); |
| 310 free(raw_buf); | 312 free(raw_buf); |
| 311 } | 313 } |
| 312 | 314 |
| 313 TEST(UnionTest, StringGetterSetter) { | 315 TEST(UnionTest, StringGetterSetter) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 342 EXPECT_TRUE(pod_clone->is_f_string()); | 344 EXPECT_TRUE(pod_clone->is_f_string()); |
| 343 EXPECT_EQ(pod_clone->which(), ObjectUnion::Tag::F_STRING); | 345 EXPECT_EQ(pod_clone->which(), ObjectUnion::Tag::F_STRING); |
| 344 } | 346 } |
| 345 | 347 |
| 346 TEST(UnionTest, StringSerialization) { | 348 TEST(UnionTest, StringSerialization) { |
| 347 ObjectUnionPtr pod1(ObjectUnion::New()); | 349 ObjectUnionPtr pod1(ObjectUnion::New()); |
| 348 | 350 |
| 349 String hello("hello world"); | 351 String hello("hello world"); |
| 350 pod1->set_f_string(hello); | 352 pod1->set_f_string(hello); |
| 351 | 353 |
| 352 size_t size = | 354 size_t size = mojo::internal::PrepareToSerialize<ObjectUnionDataView>( |
| 353 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(pod1, false, nullptr); | 355 pod1, false, nullptr); |
| 354 mojo::internal::FixedBufferForTesting buf(size); | 356 mojo::internal::FixedBufferForTesting buf(size); |
| 355 internal::ObjectUnion_Data* data = nullptr; | 357 internal::ObjectUnion_Data* data = nullptr; |
| 356 mojo::internal::Serialize<ObjectUnionPtr>(pod1, &buf, &data, false, nullptr); | 358 mojo::internal::Serialize<ObjectUnionDataView>(pod1, &buf, &data, false, |
| 359 nullptr); |
| 357 | 360 |
| 358 ObjectUnionPtr pod2; | 361 ObjectUnionPtr pod2; |
| 359 mojo::internal::Deserialize<ObjectUnionPtr>(data, &pod2, nullptr); | 362 mojo::internal::Deserialize<ObjectUnionDataView>(data, &pod2, nullptr); |
| 360 EXPECT_EQ(hello, pod2->get_f_string()); | 363 EXPECT_EQ(hello, pod2->get_f_string()); |
| 361 EXPECT_TRUE(pod2->is_f_string()); | 364 EXPECT_TRUE(pod2->is_f_string()); |
| 362 EXPECT_EQ(pod2->which(), ObjectUnion::Tag::F_STRING); | 365 EXPECT_EQ(pod2->which(), ObjectUnion::Tag::F_STRING); |
| 363 } | 366 } |
| 364 | 367 |
| 365 TEST(UnionTest, NullStringValidation) { | 368 TEST(UnionTest, NullStringValidation) { |
| 366 size_t size = sizeof(internal::ObjectUnion_Data); | 369 size_t size = sizeof(internal::ObjectUnion_Data); |
| 367 mojo::internal::FixedBufferForTesting buf(size); | 370 mojo::internal::FixedBufferForTesting buf(size); |
| 368 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); | 371 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); |
| 369 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; | 372 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 TEST(UnionTest, PodUnionInArraySerialization) { | 430 TEST(UnionTest, PodUnionInArraySerialization) { |
| 428 Array<PodUnionPtr> array(2); | 431 Array<PodUnionPtr> array(2); |
| 429 array[0] = PodUnion::New(); | 432 array[0] = PodUnion::New(); |
| 430 array[1] = PodUnion::New(); | 433 array[1] = PodUnion::New(); |
| 431 | 434 |
| 432 array[0]->set_f_int8(10); | 435 array[0]->set_f_int8(10); |
| 433 array[1]->set_f_int16(12); | 436 array[1]->set_f_int16(12); |
| 434 EXPECT_EQ(2U, array.size()); | 437 EXPECT_EQ(2U, array.size()); |
| 435 | 438 |
| 436 size_t size = | 439 size_t size = |
| 437 mojo::internal::PrepareToSerialize<Array<PodUnionPtr>>(array, nullptr); | 440 mojo::internal::PrepareToSerialize<ArrayDataView<PodUnionDataView>>( |
| 441 array, nullptr); |
| 438 EXPECT_EQ(40U, size); | 442 EXPECT_EQ(40U, size); |
| 439 | 443 |
| 440 mojo::internal::FixedBufferForTesting buf(size); | 444 mojo::internal::FixedBufferForTesting buf(size); |
| 441 mojo::internal::Array_Data<internal::PodUnion_Data>* data; | 445 mojo::internal::Array_Data<internal::PodUnion_Data>* data; |
| 442 mojo::internal::ContainerValidateParams validate_params(0, false, nullptr); | 446 mojo::internal::ContainerValidateParams validate_params(0, false, nullptr); |
| 443 mojo::internal::Serialize<Array<PodUnionPtr>>(array, &buf, &data, | 447 mojo::internal::Serialize<ArrayDataView<PodUnionDataView>>( |
| 444 &validate_params, nullptr); | 448 array, &buf, &data, &validate_params, nullptr); |
| 445 | 449 |
| 446 Array<PodUnionPtr> array2; | 450 Array<PodUnionPtr> array2; |
| 447 mojo::internal::Deserialize<Array<PodUnionPtr>>(data, &array2, nullptr); | 451 mojo::internal::Deserialize<ArrayDataView<PodUnionDataView>>(data, &array2, |
| 452 nullptr); |
| 448 | 453 |
| 449 EXPECT_EQ(2U, array2.size()); | 454 EXPECT_EQ(2U, array2.size()); |
| 450 | 455 |
| 451 EXPECT_EQ(10, array2[0]->get_f_int8()); | 456 EXPECT_EQ(10, array2[0]->get_f_int8()); |
| 452 EXPECT_EQ(12, array2[1]->get_f_int16()); | 457 EXPECT_EQ(12, array2[1]->get_f_int16()); |
| 453 } | 458 } |
| 454 | 459 |
| 455 TEST(UnionTest, PodUnionInArraySerializationWithNull) { | 460 TEST(UnionTest, PodUnionInArraySerializationWithNull) { |
| 456 Array<PodUnionPtr> array(2); | 461 Array<PodUnionPtr> array(2); |
| 457 array[0] = PodUnion::New(); | 462 array[0] = PodUnion::New(); |
| 458 | 463 |
| 459 array[0]->set_f_int8(10); | 464 array[0]->set_f_int8(10); |
| 460 EXPECT_EQ(2U, array.size()); | 465 EXPECT_EQ(2U, array.size()); |
| 461 | 466 |
| 462 size_t size = | 467 size_t size = |
| 463 mojo::internal::PrepareToSerialize<Array<PodUnionPtr>>(array, nullptr); | 468 mojo::internal::PrepareToSerialize<ArrayDataView<PodUnionDataView>>( |
| 469 array, nullptr); |
| 464 EXPECT_EQ(40U, size); | 470 EXPECT_EQ(40U, size); |
| 465 | 471 |
| 466 mojo::internal::FixedBufferForTesting buf(size); | 472 mojo::internal::FixedBufferForTesting buf(size); |
| 467 mojo::internal::Array_Data<internal::PodUnion_Data>* data; | 473 mojo::internal::Array_Data<internal::PodUnion_Data>* data; |
| 468 mojo::internal::ContainerValidateParams validate_params(0, true, nullptr); | 474 mojo::internal::ContainerValidateParams validate_params(0, true, nullptr); |
| 469 mojo::internal::Serialize<Array<PodUnionPtr>>(array, &buf, &data, | 475 mojo::internal::Serialize<ArrayDataView<PodUnionDataView>>( |
| 470 &validate_params, nullptr); | 476 array, &buf, &data, &validate_params, nullptr); |
| 471 | 477 |
| 472 Array<PodUnionPtr> array2; | 478 Array<PodUnionPtr> array2; |
| 473 mojo::internal::Deserialize<Array<PodUnionPtr>>(data, &array2, nullptr); | 479 mojo::internal::Deserialize<ArrayDataView<PodUnionDataView>>(data, &array2, |
| 480 nullptr); |
| 474 | 481 |
| 475 EXPECT_EQ(2U, array2.size()); | 482 EXPECT_EQ(2U, array2.size()); |
| 476 | 483 |
| 477 EXPECT_EQ(10, array2[0]->get_f_int8()); | 484 EXPECT_EQ(10, array2[0]->get_f_int8()); |
| 478 EXPECT_TRUE(array2[1].is_null()); | 485 EXPECT_TRUE(array2[1].is_null()); |
| 479 } | 486 } |
| 480 | 487 |
| 481 TEST(UnionTest, ObjectUnionInArraySerialization) { | 488 TEST(UnionTest, ObjectUnionInArraySerialization) { |
| 482 Array<ObjectUnionPtr> array(2); | 489 Array<ObjectUnionPtr> array(2); |
| 483 array[0] = ObjectUnion::New(); | 490 array[0] = ObjectUnion::New(); |
| 484 array[1] = ObjectUnion::New(); | 491 array[1] = ObjectUnion::New(); |
| 485 | 492 |
| 486 array[0]->set_f_string("hello"); | 493 array[0]->set_f_string("hello"); |
| 487 array[1]->set_f_string("world"); | 494 array[1]->set_f_string("world"); |
| 488 EXPECT_EQ(2U, array.size()); | 495 EXPECT_EQ(2U, array.size()); |
| 489 | 496 |
| 490 size_t size = | 497 size_t size = |
| 491 mojo::internal::PrepareToSerialize<Array<ObjectUnionPtr>>(array, nullptr); | 498 mojo::internal::PrepareToSerialize<ArrayDataView<ObjectUnionDataView>>( |
| 499 array, nullptr); |
| 492 EXPECT_EQ(72U, size); | 500 EXPECT_EQ(72U, size); |
| 493 | 501 |
| 494 mojo::internal::FixedBufferForTesting buf(size); | 502 mojo::internal::FixedBufferForTesting buf(size); |
| 495 | 503 |
| 496 mojo::internal::Array_Data<internal::ObjectUnion_Data>* data; | 504 mojo::internal::Array_Data<internal::ObjectUnion_Data>* data; |
| 497 mojo::internal::ContainerValidateParams validate_params(0, false, nullptr); | 505 mojo::internal::ContainerValidateParams validate_params(0, false, nullptr); |
| 498 mojo::internal::Serialize<Array<ObjectUnionPtr>>(array, &buf, &data, | 506 mojo::internal::Serialize<ArrayDataView<ObjectUnionDataView>>( |
| 499 &validate_params, nullptr); | 507 array, &buf, &data, &validate_params, nullptr); |
| 500 | 508 |
| 501 std::vector<char> new_buf; | 509 std::vector<char> new_buf; |
| 502 new_buf.resize(size); | 510 new_buf.resize(size); |
| 503 | 511 |
| 504 void* raw_buf = buf.Leak(); | 512 void* raw_buf = buf.Leak(); |
| 505 memcpy(new_buf.data(), raw_buf, size); | 513 memcpy(new_buf.data(), raw_buf, size); |
| 506 free(raw_buf); | 514 free(raw_buf); |
| 507 | 515 |
| 508 data = | 516 data = |
| 509 reinterpret_cast<mojo::internal::Array_Data<internal::ObjectUnion_Data>*>( | 517 reinterpret_cast<mojo::internal::Array_Data<internal::ObjectUnion_Data>*>( |
| 510 new_buf.data()); | 518 new_buf.data()); |
| 511 mojo::internal::ValidationContext validation_context( | 519 mojo::internal::ValidationContext validation_context( |
| 512 data, static_cast<uint32_t>(size), 0); | 520 data, static_cast<uint32_t>(size), 0); |
| 513 ASSERT_TRUE(mojo::internal::Array_Data<internal::ObjectUnion_Data>::Validate( | 521 ASSERT_TRUE(mojo::internal::Array_Data<internal::ObjectUnion_Data>::Validate( |
| 514 data, &validation_context, &validate_params)); | 522 data, &validation_context, &validate_params)); |
| 515 | 523 |
| 516 Array<ObjectUnionPtr> array2; | 524 Array<ObjectUnionPtr> array2; |
| 517 mojo::internal::Deserialize<Array<ObjectUnionPtr>>(data, &array2, nullptr); | 525 mojo::internal::Deserialize<ArrayDataView<ObjectUnionDataView>>(data, &array2, |
| 526 nullptr); |
| 518 | 527 |
| 519 EXPECT_EQ(2U, array2.size()); | 528 EXPECT_EQ(2U, array2.size()); |
| 520 | 529 |
| 521 EXPECT_EQ(String("hello"), array2[0]->get_f_string()); | 530 EXPECT_EQ(String("hello"), array2[0]->get_f_string()); |
| 522 EXPECT_EQ(String("world"), array2[1]->get_f_string()); | 531 EXPECT_EQ(String("world"), array2[1]->get_f_string()); |
| 523 } | 532 } |
| 524 | 533 |
| 525 // TODO(azani): Move back in struct_unittest.cc when possible. | 534 // TODO(azani): Move back in struct_unittest.cc when possible. |
| 526 // Struct tests | 535 // Struct tests |
| 527 TEST(UnionTest, Clone_Union) { | 536 TEST(UnionTest, Clone_Union) { |
| 528 SmallStructPtr small_struct(SmallStruct::New()); | 537 SmallStructPtr small_struct(SmallStruct::New()); |
| 529 small_struct->pod_union = PodUnion::New(); | 538 small_struct->pod_union = PodUnion::New(); |
| 530 small_struct->pod_union->set_f_int8(10); | 539 small_struct->pod_union->set_f_int8(10); |
| 531 | 540 |
| 532 SmallStructPtr clone = small_struct.Clone(); | 541 SmallStructPtr clone = small_struct.Clone(); |
| 533 EXPECT_EQ(10, clone->pod_union->get_f_int8()); | 542 EXPECT_EQ(10, clone->pod_union->get_f_int8()); |
| 534 } | 543 } |
| 535 | 544 |
| 536 // Serialization test of a struct with a union of plain old data. | 545 // Serialization test of a struct with a union of plain old data. |
| 537 TEST(UnionTest, Serialization_UnionOfPods) { | 546 TEST(UnionTest, Serialization_UnionOfPods) { |
| 538 SmallStructPtr small_struct(SmallStruct::New()); | 547 SmallStructPtr small_struct(SmallStruct::New()); |
| 539 small_struct->pod_union = PodUnion::New(); | 548 small_struct->pod_union = PodUnion::New(); |
| 540 small_struct->pod_union->set_f_int32(10); | 549 small_struct->pod_union->set_f_int32(10); |
| 541 | 550 |
| 542 mojo::internal::SerializationContext context; | 551 mojo::internal::SerializationContext context; |
| 543 size_t size = mojo::internal::PrepareToSerialize<SmallStructPtr>(small_struct, | 552 size_t size = mojo::internal::PrepareToSerialize<SmallStructDataView>( |
| 544 &context); | 553 small_struct, &context); |
| 545 | 554 |
| 546 mojo::internal::FixedBufferForTesting buf(size); | 555 mojo::internal::FixedBufferForTesting buf(size); |
| 547 internal::SmallStruct_Data* data = nullptr; | 556 internal::SmallStruct_Data* data = nullptr; |
| 548 mojo::internal::Serialize<SmallStructPtr>(small_struct, &buf, &data, | 557 mojo::internal::Serialize<SmallStructDataView>(small_struct, &buf, &data, |
| 549 &context); | 558 &context); |
| 550 | 559 |
| 551 SmallStructPtr deserialized; | 560 SmallStructPtr deserialized; |
| 552 mojo::internal::Deserialize<SmallStructPtr>(data, &deserialized, &context); | 561 mojo::internal::Deserialize<SmallStructDataView>(data, &deserialized, |
| 562 &context); |
| 553 | 563 |
| 554 EXPECT_EQ(10, deserialized->pod_union->get_f_int32()); | 564 EXPECT_EQ(10, deserialized->pod_union->get_f_int32()); |
| 555 } | 565 } |
| 556 | 566 |
| 557 // Serialization test of a struct with a union of structs. | 567 // Serialization test of a struct with a union of structs. |
| 558 TEST(UnionTest, Serialization_UnionOfObjects) { | 568 TEST(UnionTest, Serialization_UnionOfObjects) { |
| 559 SmallObjStructPtr obj_struct(SmallObjStruct::New()); | 569 SmallObjStructPtr obj_struct(SmallObjStruct::New()); |
| 560 obj_struct->obj_union = ObjectUnion::New(); | 570 obj_struct->obj_union = ObjectUnion::New(); |
| 561 String hello("hello world"); | 571 String hello("hello world"); |
| 562 obj_struct->obj_union->set_f_string(hello); | 572 obj_struct->obj_union->set_f_string(hello); |
| 563 | 573 |
| 564 size_t size = mojo::internal::PrepareToSerialize<SmallObjStructPtr>( | 574 size_t size = mojo::internal::PrepareToSerialize<SmallObjStructDataView>( |
| 565 obj_struct, nullptr); | 575 obj_struct, nullptr); |
| 566 | 576 |
| 567 mojo::internal::FixedBufferForTesting buf(size); | 577 mojo::internal::FixedBufferForTesting buf(size); |
| 568 internal::SmallObjStruct_Data* data = nullptr; | 578 internal::SmallObjStruct_Data* data = nullptr; |
| 569 mojo::internal::Serialize<SmallObjStructPtr>(obj_struct, &buf, &data, | 579 mojo::internal::Serialize<SmallObjStructDataView>(obj_struct, &buf, &data, |
| 570 nullptr); | 580 nullptr); |
| 571 | 581 |
| 572 SmallObjStructPtr deserialized; | 582 SmallObjStructPtr deserialized; |
| 573 mojo::internal::Deserialize<SmallObjStructPtr>(data, &deserialized, nullptr); | 583 mojo::internal::Deserialize<SmallObjStructDataView>(data, &deserialized, |
| 584 nullptr); |
| 574 | 585 |
| 575 EXPECT_EQ(hello, deserialized->obj_union->get_f_string()); | 586 EXPECT_EQ(hello, deserialized->obj_union->get_f_string()); |
| 576 } | 587 } |
| 577 | 588 |
| 578 // Validation test of a struct with a union. | 589 // Validation test of a struct with a union. |
| 579 TEST(UnionTest, Validation_UnionsInStruct) { | 590 TEST(UnionTest, Validation_UnionsInStruct) { |
| 580 SmallStructPtr small_struct(SmallStruct::New()); | 591 SmallStructPtr small_struct(SmallStruct::New()); |
| 581 small_struct->pod_union = PodUnion::New(); | 592 small_struct->pod_union = PodUnion::New(); |
| 582 small_struct->pod_union->set_f_int32(10); | 593 small_struct->pod_union->set_f_int32(10); |
| 583 | 594 |
| 584 mojo::internal::SerializationContext context; | 595 mojo::internal::SerializationContext context; |
| 585 size_t size = mojo::internal::PrepareToSerialize<SmallStructPtr>(small_struct, | 596 size_t size = mojo::internal::PrepareToSerialize<SmallStructDataView>( |
| 586 &context); | 597 small_struct, &context); |
| 587 | 598 |
| 588 mojo::internal::FixedBufferForTesting buf(size); | 599 mojo::internal::FixedBufferForTesting buf(size); |
| 589 internal::SmallStruct_Data* data = nullptr; | 600 internal::SmallStruct_Data* data = nullptr; |
| 590 mojo::internal::Serialize<SmallStructPtr>(small_struct, &buf, &data, | 601 mojo::internal::Serialize<SmallStructDataView>(small_struct, &buf, &data, |
| 591 &context); | 602 &context); |
| 592 | |
| 593 | 603 |
| 594 void* raw_buf = buf.Leak(); | 604 void* raw_buf = buf.Leak(); |
| 595 mojo::internal::ValidationContext validation_context( | 605 mojo::internal::ValidationContext validation_context( |
| 596 data, static_cast<uint32_t>(size), 0); | 606 data, static_cast<uint32_t>(size), 0); |
| 597 EXPECT_TRUE(internal::SmallStruct_Data::Validate( | 607 EXPECT_TRUE(internal::SmallStruct_Data::Validate( |
| 598 raw_buf, &validation_context)); | 608 raw_buf, &validation_context)); |
| 599 free(raw_buf); | 609 free(raw_buf); |
| 600 } | 610 } |
| 601 | 611 |
| 602 // Validation test of a struct union fails due to unknown union tag. | 612 // Validation test of a struct union fails due to unknown union tag. |
| 603 TEST(UnionTest, Validation_PodUnionInStruct_Failure) { | 613 TEST(UnionTest, Validation_PodUnionInStruct_Failure) { |
| 604 SmallStructPtr small_struct(SmallStruct::New()); | 614 SmallStructPtr small_struct(SmallStruct::New()); |
| 605 small_struct->pod_union = PodUnion::New(); | 615 small_struct->pod_union = PodUnion::New(); |
| 606 small_struct->pod_union->set_f_int32(10); | 616 small_struct->pod_union->set_f_int32(10); |
| 607 | 617 |
| 608 mojo::internal::SerializationContext context; | 618 mojo::internal::SerializationContext context; |
| 609 size_t size = mojo::internal::PrepareToSerialize<SmallStructPtr>(small_struct, | 619 size_t size = mojo::internal::PrepareToSerialize<SmallStructDataView>( |
| 610 &context); | 620 small_struct, &context); |
| 611 | 621 |
| 612 mojo::internal::FixedBufferForTesting buf(size); | 622 mojo::internal::FixedBufferForTesting buf(size); |
| 613 internal::SmallStruct_Data* data = nullptr; | 623 internal::SmallStruct_Data* data = nullptr; |
| 614 mojo::internal::Serialize<SmallStructPtr>(small_struct, &buf, &data, | 624 mojo::internal::Serialize<SmallStructDataView>(small_struct, &buf, &data, |
| 615 &context); | 625 &context); |
| 616 data->pod_union.tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(100); | 626 data->pod_union.tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(100); |
| 617 | 627 |
| 618 void* raw_buf = buf.Leak(); | 628 void* raw_buf = buf.Leak(); |
| 619 mojo::internal::ValidationContext validation_context( | 629 mojo::internal::ValidationContext validation_context( |
| 620 data, static_cast<uint32_t>(size), 0); | 630 data, static_cast<uint32_t>(size), 0); |
| 621 EXPECT_FALSE(internal::SmallStruct_Data::Validate( | 631 EXPECT_FALSE(internal::SmallStruct_Data::Validate( |
| 622 raw_buf, &validation_context)); | 632 raw_buf, &validation_context)); |
| 623 free(raw_buf); | 633 free(raw_buf); |
| 624 } | 634 } |
| 625 | 635 |
| 626 // Validation fails due to non-nullable null union in struct. | 636 // Validation fails due to non-nullable null union in struct. |
| 627 TEST(UnionTest, Validation_NullUnion_Failure) { | 637 TEST(UnionTest, Validation_NullUnion_Failure) { |
| 628 SmallStructNonNullableUnionPtr small_struct( | 638 SmallStructNonNullableUnionPtr small_struct( |
| 629 SmallStructNonNullableUnion::New()); | 639 SmallStructNonNullableUnion::New()); |
| 630 | 640 |
| 631 size_t size = | 641 size_t size = |
| 632 mojo::internal::PrepareToSerialize<SmallStructNonNullableUnionPtr>( | 642 mojo::internal::PrepareToSerialize<SmallStructNonNullableUnionDataView>( |
| 633 small_struct, nullptr); | 643 small_struct, nullptr); |
| 634 | 644 |
| 635 mojo::internal::FixedBufferForTesting buf(size); | 645 mojo::internal::FixedBufferForTesting buf(size); |
| 636 internal::SmallStructNonNullableUnion_Data* data = | 646 internal::SmallStructNonNullableUnion_Data* data = |
| 637 internal::SmallStructNonNullableUnion_Data::New(&buf); | 647 internal::SmallStructNonNullableUnion_Data::New(&buf); |
| 638 | 648 |
| 639 void* raw_buf = buf.Leak(); | 649 void* raw_buf = buf.Leak(); |
| 640 mojo::internal::ValidationContext validation_context( | 650 mojo::internal::ValidationContext validation_context( |
| 641 data, static_cast<uint32_t>(size), 0); | 651 data, static_cast<uint32_t>(size), 0); |
| 642 EXPECT_FALSE(internal::SmallStructNonNullableUnion_Data::Validate( | 652 EXPECT_FALSE(internal::SmallStructNonNullableUnion_Data::Validate( |
| 643 raw_buf, &validation_context)); | 653 raw_buf, &validation_context)); |
| 644 free(raw_buf); | 654 free(raw_buf); |
| 645 } | 655 } |
| 646 | 656 |
| 647 // Validation passes with nullable null union. | 657 // Validation passes with nullable null union. |
| 648 TEST(UnionTest, Validation_NullableUnion) { | 658 TEST(UnionTest, Validation_NullableUnion) { |
| 649 SmallStructPtr small_struct(SmallStruct::New()); | 659 SmallStructPtr small_struct(SmallStruct::New()); |
| 650 | 660 |
| 651 mojo::internal::SerializationContext context; | 661 mojo::internal::SerializationContext context; |
| 652 size_t size = mojo::internal::PrepareToSerialize<SmallStructPtr>(small_struct, | 662 size_t size = mojo::internal::PrepareToSerialize<SmallStructDataView>( |
| 653 &context); | 663 small_struct, &context); |
| 654 | 664 |
| 655 mojo::internal::FixedBufferForTesting buf(size); | 665 mojo::internal::FixedBufferForTesting buf(size); |
| 656 internal::SmallStruct_Data* data = nullptr; | 666 internal::SmallStruct_Data* data = nullptr; |
| 657 mojo::internal::Serialize<SmallStructPtr>(small_struct, &buf, &data, | 667 mojo::internal::Serialize<SmallStructDataView>(small_struct, &buf, &data, |
| 658 &context); | 668 &context); |
| 659 | 669 |
| 660 void* raw_buf = buf.Leak(); | 670 void* raw_buf = buf.Leak(); |
| 661 mojo::internal::ValidationContext validation_context( | 671 mojo::internal::ValidationContext validation_context( |
| 662 data, static_cast<uint32_t>(size), 0); | 672 data, static_cast<uint32_t>(size), 0); |
| 663 EXPECT_TRUE(internal::SmallStruct_Data::Validate( | 673 EXPECT_TRUE(internal::SmallStruct_Data::Validate( |
| 664 raw_buf, &validation_context)); | 674 raw_buf, &validation_context)); |
| 665 free(raw_buf); | 675 free(raw_buf); |
| 666 } | 676 } |
| 667 | 677 |
| 668 // TODO(azani): Move back in map_unittest.cc when possible. | 678 // TODO(azani): Move back in map_unittest.cc when possible. |
| 669 // Map Tests | 679 // Map Tests |
| 670 TEST(UnionTest, PodUnionInMap) { | 680 TEST(UnionTest, PodUnionInMap) { |
| 671 SmallStructPtr small_struct(SmallStruct::New()); | 681 SmallStructPtr small_struct(SmallStruct::New()); |
| 672 small_struct->pod_union_map.emplace(); | 682 small_struct->pod_union_map.emplace(); |
| 673 small_struct->pod_union_map.value()["one"] = PodUnion::New(); | 683 small_struct->pod_union_map.value()["one"] = PodUnion::New(); |
| 674 small_struct->pod_union_map.value()["two"] = PodUnion::New(); | 684 small_struct->pod_union_map.value()["two"] = PodUnion::New(); |
| 675 | 685 |
| 676 small_struct->pod_union_map.value()["one"]->set_f_int8(8); | 686 small_struct->pod_union_map.value()["one"]->set_f_int8(8); |
| 677 small_struct->pod_union_map.value()["two"]->set_f_int16(16); | 687 small_struct->pod_union_map.value()["two"]->set_f_int16(16); |
| 678 | 688 |
| 679 EXPECT_EQ(8, small_struct->pod_union_map.value()["one"]->get_f_int8()); | 689 EXPECT_EQ(8, small_struct->pod_union_map.value()["one"]->get_f_int8()); |
| 680 EXPECT_EQ(16, small_struct->pod_union_map.value()["two"]->get_f_int16()); | 690 EXPECT_EQ(16, small_struct->pod_union_map.value()["two"]->get_f_int16()); |
| 681 } | 691 } |
| 682 | 692 |
| 683 TEST(UnionTest, PodUnionInMapSerialization) { | 693 TEST(UnionTest, PodUnionInMapSerialization) { |
| 694 using MojomType = MapDataView<StringDataView, PodUnionDataView>; |
| 695 |
| 684 Map<String, PodUnionPtr> map; | 696 Map<String, PodUnionPtr> map; |
| 685 map.insert("one", PodUnion::New()); | 697 map.insert("one", PodUnion::New()); |
| 686 map.insert("two", PodUnion::New()); | 698 map.insert("two", PodUnion::New()); |
| 687 | 699 |
| 688 map["one"]->set_f_int8(8); | 700 map["one"]->set_f_int8(8); |
| 689 map["two"]->set_f_int16(16); | 701 map["two"]->set_f_int16(16); |
| 690 | 702 |
| 691 mojo::internal::SerializationContext context; | 703 mojo::internal::SerializationContext context; |
| 692 size_t size = mojo::internal::PrepareToSerialize<Map<String, PodUnionPtr>>( | 704 size_t size = mojo::internal::PrepareToSerialize<MojomType>(map, &context); |
| 693 map, &context); | |
| 694 EXPECT_EQ(120U, size); | 705 EXPECT_EQ(120U, size); |
| 695 | 706 |
| 696 mojo::internal::FixedBufferForTesting buf(size); | 707 mojo::internal::FixedBufferForTesting buf(size); |
| 697 typename mojo::internal::MojomTypeTraits<Map<String, PodUnionPtr>>::Data* | 708 |
| 698 data; | 709 typename mojo::internal::MojomTypeTraits<MojomType>::Data* data; |
| 699 mojo::internal::ContainerValidateParams validate_params( | 710 mojo::internal::ContainerValidateParams validate_params( |
| 700 new mojo::internal::ContainerValidateParams(0, false, nullptr), | 711 new mojo::internal::ContainerValidateParams(0, false, nullptr), |
| 701 new mojo::internal::ContainerValidateParams(0, false, nullptr)); | 712 new mojo::internal::ContainerValidateParams(0, false, nullptr)); |
| 702 mojo::internal::Serialize<Map<String, PodUnionPtr>>( | 713 mojo::internal::Serialize<MojomType>(map, &buf, &data, &validate_params, |
| 703 map, &buf, &data, &validate_params, &context); | 714 &context); |
| 704 | 715 |
| 705 Map<String, PodUnionPtr> map2; | 716 Map<String, PodUnionPtr> map2; |
| 706 mojo::internal::Deserialize<Map<String, PodUnionPtr>>(data, &map2, &context); | 717 mojo::internal::Deserialize<MojomType>(data, &map2, &context); |
| 707 | 718 |
| 708 EXPECT_EQ(8, map2["one"]->get_f_int8()); | 719 EXPECT_EQ(8, map2["one"]->get_f_int8()); |
| 709 EXPECT_EQ(16, map2["two"]->get_f_int16()); | 720 EXPECT_EQ(16, map2["two"]->get_f_int16()); |
| 710 } | 721 } |
| 711 | 722 |
| 712 TEST(UnionTest, PodUnionInMapSerializationWithNull) { | 723 TEST(UnionTest, PodUnionInMapSerializationWithNull) { |
| 724 using MojomType = MapDataView<StringDataView, PodUnionDataView>; |
| 725 |
| 713 Map<String, PodUnionPtr> map; | 726 Map<String, PodUnionPtr> map; |
| 714 map.insert("one", PodUnion::New()); | 727 map.insert("one", PodUnion::New()); |
| 715 map.insert("two", nullptr); | 728 map.insert("two", nullptr); |
| 716 | 729 |
| 717 map["one"]->set_f_int8(8); | 730 map["one"]->set_f_int8(8); |
| 718 | 731 |
| 719 mojo::internal::SerializationContext context; | 732 mojo::internal::SerializationContext context; |
| 720 size_t size = mojo::internal::PrepareToSerialize<Map<String, PodUnionPtr>>( | 733 size_t size = mojo::internal::PrepareToSerialize<MojomType>(map, &context); |
| 721 map, &context); | |
| 722 EXPECT_EQ(120U, size); | 734 EXPECT_EQ(120U, size); |
| 723 | 735 |
| 724 mojo::internal::FixedBufferForTesting buf(size); | 736 mojo::internal::FixedBufferForTesting buf(size); |
| 725 typename mojo::internal::MojomTypeTraits<Map<String, PodUnionPtr>>::Data* | 737 typename mojo::internal::MojomTypeTraits<MojomType>::Data* data; |
| 726 data; | |
| 727 mojo::internal::ContainerValidateParams validate_params( | 738 mojo::internal::ContainerValidateParams validate_params( |
| 728 new mojo::internal::ContainerValidateParams(0, false, nullptr), | 739 new mojo::internal::ContainerValidateParams(0, false, nullptr), |
| 729 new mojo::internal::ContainerValidateParams(0, true, nullptr)); | 740 new mojo::internal::ContainerValidateParams(0, true, nullptr)); |
| 730 mojo::internal::Serialize<Map<String, PodUnionPtr>>( | 741 mojo::internal::Serialize<MojomType>(map, &buf, &data, &validate_params, |
| 731 map, &buf, &data, &validate_params, &context); | 742 &context); |
| 732 | 743 |
| 733 Map<String, PodUnionPtr> map2; | 744 Map<String, PodUnionPtr> map2; |
| 734 mojo::internal::Deserialize<Map<String, PodUnionPtr>>(data, &map2, &context); | 745 mojo::internal::Deserialize<MojomType>(data, &map2, &context); |
| 735 | 746 |
| 736 EXPECT_EQ(8, map2["one"]->get_f_int8()); | 747 EXPECT_EQ(8, map2["one"]->get_f_int8()); |
| 737 EXPECT_TRUE(map2["two"].is_null()); | 748 EXPECT_TRUE(map2["two"].is_null()); |
| 738 } | 749 } |
| 739 | 750 |
| 740 TEST(UnionTest, StructInUnionGetterSetterPasser) { | 751 TEST(UnionTest, StructInUnionGetterSetterPasser) { |
| 741 DummyStructPtr dummy(DummyStruct::New()); | 752 DummyStructPtr dummy(DummyStruct::New()); |
| 742 dummy->f_int8 = 8; | 753 dummy->f_int8 = 8; |
| 743 | 754 |
| 744 ObjectUnionPtr obj(ObjectUnion::New()); | 755 ObjectUnionPtr obj(ObjectUnion::New()); |
| 745 obj->set_f_dummy(std::move(dummy)); | 756 obj->set_f_dummy(std::move(dummy)); |
| 746 | 757 |
| 747 EXPECT_EQ(8, obj->get_f_dummy()->f_int8); | 758 EXPECT_EQ(8, obj->get_f_dummy()->f_int8); |
| 748 } | 759 } |
| 749 | 760 |
| 750 TEST(UnionTest, StructInUnionSerialization) { | 761 TEST(UnionTest, StructInUnionSerialization) { |
| 751 DummyStructPtr dummy(DummyStruct::New()); | 762 DummyStructPtr dummy(DummyStruct::New()); |
| 752 dummy->f_int8 = 8; | 763 dummy->f_int8 = 8; |
| 753 | 764 |
| 754 ObjectUnionPtr obj(ObjectUnion::New()); | 765 ObjectUnionPtr obj(ObjectUnion::New()); |
| 755 obj->set_f_dummy(std::move(dummy)); | 766 obj->set_f_dummy(std::move(dummy)); |
| 756 | 767 |
| 757 size_t size = | 768 size_t size = mojo::internal::PrepareToSerialize<ObjectUnionDataView>( |
| 758 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, nullptr); | 769 obj, false, nullptr); |
| 759 EXPECT_EQ(32U, size); | 770 EXPECT_EQ(32U, size); |
| 760 | 771 |
| 761 mojo::internal::FixedBufferForTesting buf(size); | 772 mojo::internal::FixedBufferForTesting buf(size); |
| 762 internal::ObjectUnion_Data* data = nullptr; | 773 internal::ObjectUnion_Data* data = nullptr; |
| 763 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, nullptr); | 774 mojo::internal::Serialize<ObjectUnionDataView>(obj, &buf, &data, false, |
| 775 nullptr); |
| 764 | 776 |
| 765 ObjectUnionPtr obj2; | 777 ObjectUnionPtr obj2; |
| 766 mojo::internal::Deserialize<ObjectUnionPtr>(data, &obj2, nullptr); | 778 mojo::internal::Deserialize<ObjectUnionDataView>(data, &obj2, nullptr); |
| 767 EXPECT_EQ(8, obj2->get_f_dummy()->f_int8); | 779 EXPECT_EQ(8, obj2->get_f_dummy()->f_int8); |
| 768 } | 780 } |
| 769 | 781 |
| 770 TEST(UnionTest, StructInUnionValidation) { | 782 TEST(UnionTest, StructInUnionValidation) { |
| 771 DummyStructPtr dummy(DummyStruct::New()); | 783 DummyStructPtr dummy(DummyStruct::New()); |
| 772 dummy->f_int8 = 8; | 784 dummy->f_int8 = 8; |
| 773 | 785 |
| 774 ObjectUnionPtr obj(ObjectUnion::New()); | 786 ObjectUnionPtr obj(ObjectUnion::New()); |
| 775 obj->set_f_dummy(std::move(dummy)); | 787 obj->set_f_dummy(std::move(dummy)); |
| 776 | 788 |
| 777 size_t size = | 789 size_t size = mojo::internal::PrepareToSerialize<ObjectUnionDataView>( |
| 778 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, nullptr); | 790 obj, false, nullptr); |
| 779 | 791 |
| 780 mojo::internal::FixedBufferForTesting buf(size); | 792 mojo::internal::FixedBufferForTesting buf(size); |
| 781 internal::ObjectUnion_Data* data = nullptr; | 793 internal::ObjectUnion_Data* data = nullptr; |
| 782 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, nullptr); | 794 mojo::internal::Serialize<ObjectUnionDataView>(obj, &buf, &data, false, |
| 795 nullptr); |
| 783 | 796 |
| 784 void* raw_buf = buf.Leak(); | 797 void* raw_buf = buf.Leak(); |
| 785 mojo::internal::ValidationContext validation_context( | 798 mojo::internal::ValidationContext validation_context( |
| 786 data, static_cast<uint32_t>(size), 0); | 799 data, static_cast<uint32_t>(size), 0); |
| 787 EXPECT_TRUE(internal::ObjectUnion_Data::Validate( | 800 EXPECT_TRUE(internal::ObjectUnion_Data::Validate( |
| 788 raw_buf, &validation_context, false)); | 801 raw_buf, &validation_context, false)); |
| 789 free(raw_buf); | 802 free(raw_buf); |
| 790 } | 803 } |
| 791 | 804 |
| 792 TEST(UnionTest, StructInUnionValidationNonNullable) { | 805 TEST(UnionTest, StructInUnionValidationNonNullable) { |
| 793 mojo::internal::SerializationWarningObserverForTesting suppress_warning; | 806 mojo::internal::SerializationWarningObserverForTesting suppress_warning; |
| 794 | 807 |
| 795 DummyStructPtr dummy(nullptr); | 808 DummyStructPtr dummy(nullptr); |
| 796 | 809 |
| 797 ObjectUnionPtr obj(ObjectUnion::New()); | 810 ObjectUnionPtr obj(ObjectUnion::New()); |
| 798 obj->set_f_dummy(std::move(dummy)); | 811 obj->set_f_dummy(std::move(dummy)); |
| 799 | 812 |
| 800 size_t size = | 813 size_t size = mojo::internal::PrepareToSerialize<ObjectUnionDataView>( |
| 801 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, nullptr); | 814 obj, false, nullptr); |
| 802 | 815 |
| 803 mojo::internal::FixedBufferForTesting buf(size); | 816 mojo::internal::FixedBufferForTesting buf(size); |
| 804 internal::ObjectUnion_Data* data = nullptr; | 817 internal::ObjectUnion_Data* data = nullptr; |
| 805 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, nullptr); | 818 mojo::internal::Serialize<ObjectUnionDataView>(obj, &buf, &data, false, |
| 819 nullptr); |
| 806 | 820 |
| 807 void* raw_buf = buf.Leak(); | 821 void* raw_buf = buf.Leak(); |
| 808 mojo::internal::ValidationContext validation_context( | 822 mojo::internal::ValidationContext validation_context( |
| 809 data, static_cast<uint32_t>(size), 0); | 823 data, static_cast<uint32_t>(size), 0); |
| 810 EXPECT_FALSE(internal::ObjectUnion_Data::Validate( | 824 EXPECT_FALSE(internal::ObjectUnion_Data::Validate( |
| 811 raw_buf, &validation_context, false)); | 825 raw_buf, &validation_context, false)); |
| 812 free(raw_buf); | 826 free(raw_buf); |
| 813 } | 827 } |
| 814 | 828 |
| 815 TEST(UnionTest, StructInUnionValidationNullable) { | 829 TEST(UnionTest, StructInUnionValidationNullable) { |
| 816 DummyStructPtr dummy(nullptr); | 830 DummyStructPtr dummy(nullptr); |
| 817 | 831 |
| 818 ObjectUnionPtr obj(ObjectUnion::New()); | 832 ObjectUnionPtr obj(ObjectUnion::New()); |
| 819 obj->set_f_nullable(std::move(dummy)); | 833 obj->set_f_nullable(std::move(dummy)); |
| 820 | 834 |
| 821 size_t size = | 835 size_t size = mojo::internal::PrepareToSerialize<ObjectUnionDataView>( |
| 822 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, nullptr); | 836 obj, false, nullptr); |
| 823 | 837 |
| 824 mojo::internal::FixedBufferForTesting buf(size); | 838 mojo::internal::FixedBufferForTesting buf(size); |
| 825 internal::ObjectUnion_Data* data = nullptr; | 839 internal::ObjectUnion_Data* data = nullptr; |
| 826 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, nullptr); | 840 mojo::internal::Serialize<ObjectUnionDataView>(obj, &buf, &data, false, |
| 841 nullptr); |
| 827 | 842 |
| 828 void* raw_buf = buf.Leak(); | 843 void* raw_buf = buf.Leak(); |
| 829 mojo::internal::ValidationContext validation_context( | 844 mojo::internal::ValidationContext validation_context( |
| 830 data, static_cast<uint32_t>(size), 0); | 845 data, static_cast<uint32_t>(size), 0); |
| 831 EXPECT_TRUE(internal::ObjectUnion_Data::Validate( | 846 EXPECT_TRUE(internal::ObjectUnion_Data::Validate( |
| 832 raw_buf, &validation_context, false)); | 847 raw_buf, &validation_context, false)); |
| 833 free(raw_buf); | 848 free(raw_buf); |
| 834 } | 849 } |
| 835 | 850 |
| 836 TEST(UnionTest, ArrayInUnionGetterSetter) { | 851 TEST(UnionTest, ArrayInUnionGetterSetter) { |
| 837 Array<int8_t> array(2); | 852 Array<int8_t> array(2); |
| 838 array[0] = 8; | 853 array[0] = 8; |
| 839 array[1] = 9; | 854 array[1] = 9; |
| 840 | 855 |
| 841 ObjectUnionPtr obj(ObjectUnion::New()); | 856 ObjectUnionPtr obj(ObjectUnion::New()); |
| 842 obj->set_f_array_int8(std::move(array)); | 857 obj->set_f_array_int8(std::move(array)); |
| 843 | 858 |
| 844 EXPECT_EQ(8, obj->get_f_array_int8()[0]); | 859 EXPECT_EQ(8, obj->get_f_array_int8()[0]); |
| 845 EXPECT_EQ(9, obj->get_f_array_int8()[1]); | 860 EXPECT_EQ(9, obj->get_f_array_int8()[1]); |
| 846 } | 861 } |
| 847 | 862 |
| 848 TEST(UnionTest, ArrayInUnionSerialization) { | 863 TEST(UnionTest, ArrayInUnionSerialization) { |
| 849 Array<int8_t> array(2); | 864 Array<int8_t> array(2); |
| 850 array[0] = 8; | 865 array[0] = 8; |
| 851 array[1] = 9; | 866 array[1] = 9; |
| 852 | 867 |
| 853 ObjectUnionPtr obj(ObjectUnion::New()); | 868 ObjectUnionPtr obj(ObjectUnion::New()); |
| 854 obj->set_f_array_int8(std::move(array)); | 869 obj->set_f_array_int8(std::move(array)); |
| 855 | 870 |
| 856 size_t size = | 871 size_t size = mojo::internal::PrepareToSerialize<ObjectUnionDataView>( |
| 857 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, nullptr); | 872 obj, false, nullptr); |
| 858 EXPECT_EQ(32U, size); | 873 EXPECT_EQ(32U, size); |
| 859 | 874 |
| 860 mojo::internal::FixedBufferForTesting buf(size); | 875 mojo::internal::FixedBufferForTesting buf(size); |
| 861 internal::ObjectUnion_Data* data = nullptr; | 876 internal::ObjectUnion_Data* data = nullptr; |
| 862 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, nullptr); | 877 mojo::internal::Serialize<ObjectUnionDataView>(obj, &buf, &data, false, |
| 878 nullptr); |
| 863 | 879 |
| 864 ObjectUnionPtr obj2; | 880 ObjectUnionPtr obj2; |
| 865 mojo::internal::Deserialize<ObjectUnionPtr>(data, &obj2, nullptr); | 881 mojo::internal::Deserialize<ObjectUnionDataView>(data, &obj2, nullptr); |
| 866 | 882 |
| 867 EXPECT_EQ(8, obj2->get_f_array_int8()[0]); | 883 EXPECT_EQ(8, obj2->get_f_array_int8()[0]); |
| 868 EXPECT_EQ(9, obj2->get_f_array_int8()[1]); | 884 EXPECT_EQ(9, obj2->get_f_array_int8()[1]); |
| 869 } | 885 } |
| 870 | 886 |
| 871 TEST(UnionTest, ArrayInUnionValidation) { | 887 TEST(UnionTest, ArrayInUnionValidation) { |
| 872 Array<int8_t> array(2); | 888 Array<int8_t> array(2); |
| 873 array[0] = 8; | 889 array[0] = 8; |
| 874 array[1] = 9; | 890 array[1] = 9; |
| 875 | 891 |
| 876 ObjectUnionPtr obj(ObjectUnion::New()); | 892 ObjectUnionPtr obj(ObjectUnion::New()); |
| 877 obj->set_f_array_int8(std::move(array)); | 893 obj->set_f_array_int8(std::move(array)); |
| 878 | 894 |
| 879 size_t size = | 895 size_t size = mojo::internal::PrepareToSerialize<ObjectUnionDataView>( |
| 880 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, nullptr); | 896 obj, false, nullptr); |
| 881 mojo::internal::FixedBufferForTesting buf(size); | 897 mojo::internal::FixedBufferForTesting buf(size); |
| 882 internal::ObjectUnion_Data* data = nullptr; | 898 internal::ObjectUnion_Data* data = nullptr; |
| 883 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, nullptr); | 899 mojo::internal::Serialize<ObjectUnionDataView>(obj, &buf, &data, false, |
| 900 nullptr); |
| 884 | 901 |
| 885 void* raw_buf = buf.Leak(); | 902 void* raw_buf = buf.Leak(); |
| 886 mojo::internal::ValidationContext validation_context( | 903 mojo::internal::ValidationContext validation_context( |
| 887 data, static_cast<uint32_t>(size), 0); | 904 data, static_cast<uint32_t>(size), 0); |
| 888 | 905 |
| 889 EXPECT_TRUE(internal::ObjectUnion_Data::Validate( | 906 EXPECT_TRUE(internal::ObjectUnion_Data::Validate( |
| 890 raw_buf, &validation_context, false)); | 907 raw_buf, &validation_context, false)); |
| 891 free(raw_buf); | 908 free(raw_buf); |
| 892 } | 909 } |
| 893 | 910 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 905 | 922 |
| 906 TEST(UnionTest, MapInUnionSerialization) { | 923 TEST(UnionTest, MapInUnionSerialization) { |
| 907 std::unordered_map<std::string, int8_t> map; | 924 std::unordered_map<std::string, int8_t> map; |
| 908 map.insert({"one", 1}); | 925 map.insert({"one", 1}); |
| 909 map.insert({"two", 2}); | 926 map.insert({"two", 2}); |
| 910 | 927 |
| 911 ObjectUnionPtr obj(ObjectUnion::New()); | 928 ObjectUnionPtr obj(ObjectUnion::New()); |
| 912 obj->set_f_map_int8(std::move(map)); | 929 obj->set_f_map_int8(std::move(map)); |
| 913 | 930 |
| 914 mojo::internal::SerializationContext context; | 931 mojo::internal::SerializationContext context; |
| 915 size_t size = | 932 size_t size = mojo::internal::PrepareToSerialize<ObjectUnionDataView>( |
| 916 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, &context); | 933 obj, false, &context); |
| 917 EXPECT_EQ(112U, size); | 934 EXPECT_EQ(112U, size); |
| 918 | 935 |
| 919 mojo::internal::FixedBufferForTesting buf(size); | 936 mojo::internal::FixedBufferForTesting buf(size); |
| 920 internal::ObjectUnion_Data* data = nullptr; | 937 internal::ObjectUnion_Data* data = nullptr; |
| 921 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, &context); | 938 mojo::internal::Serialize<ObjectUnionDataView>(obj, &buf, &data, false, |
| 939 &context); |
| 922 | 940 |
| 923 ObjectUnionPtr obj2; | 941 ObjectUnionPtr obj2; |
| 924 mojo::internal::Deserialize<ObjectUnionPtr>(data, &obj2, &context); | 942 mojo::internal::Deserialize<ObjectUnionDataView>(data, &obj2, &context); |
| 925 | 943 |
| 926 EXPECT_EQ(1, obj2->get_f_map_int8()["one"]); | 944 EXPECT_EQ(1, obj2->get_f_map_int8()["one"]); |
| 927 EXPECT_EQ(2, obj2->get_f_map_int8()["two"]); | 945 EXPECT_EQ(2, obj2->get_f_map_int8()["two"]); |
| 928 } | 946 } |
| 929 | 947 |
| 930 TEST(UnionTest, MapInUnionValidation) { | 948 TEST(UnionTest, MapInUnionValidation) { |
| 931 std::unordered_map<std::string, int8_t> map; | 949 std::unordered_map<std::string, int8_t> map; |
| 932 map.insert({"one", 1}); | 950 map.insert({"one", 1}); |
| 933 map.insert({"two", 2}); | 951 map.insert({"two", 2}); |
| 934 | 952 |
| 935 ObjectUnionPtr obj(ObjectUnion::New()); | 953 ObjectUnionPtr obj(ObjectUnion::New()); |
| 936 obj->set_f_map_int8(std::move(map)); | 954 obj->set_f_map_int8(std::move(map)); |
| 937 | 955 |
| 938 mojo::internal::SerializationContext context; | 956 mojo::internal::SerializationContext context; |
| 939 size_t size = | 957 size_t size = mojo::internal::PrepareToSerialize<ObjectUnionDataView>( |
| 940 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, &context); | 958 obj, false, &context); |
| 941 EXPECT_EQ(112U, size); | 959 EXPECT_EQ(112U, size); |
| 942 | 960 |
| 943 mojo::internal::FixedBufferForTesting buf(size); | 961 mojo::internal::FixedBufferForTesting buf(size); |
| 944 internal::ObjectUnion_Data* data = nullptr; | 962 internal::ObjectUnion_Data* data = nullptr; |
| 945 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, &context); | 963 mojo::internal::Serialize<ObjectUnionDataView>(obj, &buf, &data, false, |
| 964 &context); |
| 946 | 965 |
| 947 void* raw_buf = buf.Leak(); | 966 void* raw_buf = buf.Leak(); |
| 948 mojo::internal::ValidationContext validation_context( | 967 mojo::internal::ValidationContext validation_context( |
| 949 data, static_cast<uint32_t>(size), 0); | 968 data, static_cast<uint32_t>(size), 0); |
| 950 | 969 |
| 951 EXPECT_TRUE(internal::ObjectUnion_Data::Validate( | 970 EXPECT_TRUE(internal::ObjectUnion_Data::Validate( |
| 952 raw_buf, &validation_context, false)); | 971 raw_buf, &validation_context, false)); |
| 953 free(raw_buf); | 972 free(raw_buf); |
| 954 } | 973 } |
| 955 | 974 |
| 956 TEST(UnionTest, UnionInUnionGetterSetter) { | 975 TEST(UnionTest, UnionInUnionGetterSetter) { |
| 957 PodUnionPtr pod(PodUnion::New()); | 976 PodUnionPtr pod(PodUnion::New()); |
| 958 pod->set_f_int8(10); | 977 pod->set_f_int8(10); |
| 959 | 978 |
| 960 ObjectUnionPtr obj(ObjectUnion::New()); | 979 ObjectUnionPtr obj(ObjectUnion::New()); |
| 961 obj->set_f_pod_union(std::move(pod)); | 980 obj->set_f_pod_union(std::move(pod)); |
| 962 | 981 |
| 963 EXPECT_EQ(10, obj->get_f_pod_union()->get_f_int8()); | 982 EXPECT_EQ(10, obj->get_f_pod_union()->get_f_int8()); |
| 964 } | 983 } |
| 965 | 984 |
| 966 TEST(UnionTest, UnionInUnionSerialization) { | 985 TEST(UnionTest, UnionInUnionSerialization) { |
| 967 PodUnionPtr pod(PodUnion::New()); | 986 PodUnionPtr pod(PodUnion::New()); |
| 968 pod->set_f_int8(10); | 987 pod->set_f_int8(10); |
| 969 | 988 |
| 970 ObjectUnionPtr obj(ObjectUnion::New()); | 989 ObjectUnionPtr obj(ObjectUnion::New()); |
| 971 obj->set_f_pod_union(std::move(pod)); | 990 obj->set_f_pod_union(std::move(pod)); |
| 972 | 991 |
| 973 size_t size = | 992 size_t size = mojo::internal::PrepareToSerialize<ObjectUnionDataView>( |
| 974 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, nullptr); | 993 obj, false, nullptr); |
| 975 EXPECT_EQ(32U, size); | 994 EXPECT_EQ(32U, size); |
| 976 | 995 |
| 977 mojo::internal::FixedBufferForTesting buf(size); | 996 mojo::internal::FixedBufferForTesting buf(size); |
| 978 internal::ObjectUnion_Data* data = nullptr; | 997 internal::ObjectUnion_Data* data = nullptr; |
| 979 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, nullptr); | 998 mojo::internal::Serialize<ObjectUnionDataView>(obj, &buf, &data, false, |
| 999 nullptr); |
| 980 | 1000 |
| 981 ObjectUnionPtr obj2; | 1001 ObjectUnionPtr obj2; |
| 982 mojo::internal::Deserialize<ObjectUnionPtr>(data, &obj2, nullptr); | 1002 mojo::internal::Deserialize<ObjectUnionDataView>(data, &obj2, nullptr); |
| 983 EXPECT_EQ(10, obj2->get_f_pod_union()->get_f_int8()); | 1003 EXPECT_EQ(10, obj2->get_f_pod_union()->get_f_int8()); |
| 984 } | 1004 } |
| 985 | 1005 |
| 986 TEST(UnionTest, UnionInUnionValidation) { | 1006 TEST(UnionTest, UnionInUnionValidation) { |
| 987 PodUnionPtr pod(PodUnion::New()); | 1007 PodUnionPtr pod(PodUnion::New()); |
| 988 pod->set_f_int8(10); | 1008 pod->set_f_int8(10); |
| 989 | 1009 |
| 990 ObjectUnionPtr obj(ObjectUnion::New()); | 1010 ObjectUnionPtr obj(ObjectUnion::New()); |
| 991 obj->set_f_pod_union(std::move(pod)); | 1011 obj->set_f_pod_union(std::move(pod)); |
| 992 | 1012 |
| 993 size_t size = | 1013 size_t size = mojo::internal::PrepareToSerialize<ObjectUnionDataView>( |
| 994 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, nullptr); | 1014 obj, false, nullptr); |
| 995 EXPECT_EQ(32U, size); | 1015 EXPECT_EQ(32U, size); |
| 996 | 1016 |
| 997 mojo::internal::FixedBufferForTesting buf(size); | 1017 mojo::internal::FixedBufferForTesting buf(size); |
| 998 internal::ObjectUnion_Data* data = nullptr; | 1018 internal::ObjectUnion_Data* data = nullptr; |
| 999 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, nullptr); | 1019 mojo::internal::Serialize<ObjectUnionDataView>(obj, &buf, &data, false, |
| 1020 nullptr); |
| 1000 | 1021 |
| 1001 void* raw_buf = buf.Leak(); | 1022 void* raw_buf = buf.Leak(); |
| 1002 mojo::internal::ValidationContext validation_context( | 1023 mojo::internal::ValidationContext validation_context( |
| 1003 data, static_cast<uint32_t>(size), 0); | 1024 data, static_cast<uint32_t>(size), 0); |
| 1004 EXPECT_TRUE(internal::ObjectUnion_Data::Validate( | 1025 EXPECT_TRUE(internal::ObjectUnion_Data::Validate( |
| 1005 raw_buf, &validation_context, false)); | 1026 raw_buf, &validation_context, false)); |
| 1006 free(raw_buf); | 1027 free(raw_buf); |
| 1007 } | 1028 } |
| 1008 | 1029 |
| 1009 TEST(UnionTest, UnionInUnionValidationNonNullable) { | 1030 TEST(UnionTest, UnionInUnionValidationNonNullable) { |
| 1010 mojo::internal::SerializationWarningObserverForTesting suppress_warning; | 1031 mojo::internal::SerializationWarningObserverForTesting suppress_warning; |
| 1011 | 1032 |
| 1012 PodUnionPtr pod(nullptr); | 1033 PodUnionPtr pod(nullptr); |
| 1013 | 1034 |
| 1014 ObjectUnionPtr obj(ObjectUnion::New()); | 1035 ObjectUnionPtr obj(ObjectUnion::New()); |
| 1015 obj->set_f_pod_union(std::move(pod)); | 1036 obj->set_f_pod_union(std::move(pod)); |
| 1016 | 1037 |
| 1017 size_t size = | 1038 size_t size = mojo::internal::PrepareToSerialize<ObjectUnionDataView>( |
| 1018 mojo::internal::PrepareToSerialize<ObjectUnionPtr>(obj, false, nullptr); | 1039 obj, false, nullptr); |
| 1019 | 1040 |
| 1020 mojo::internal::FixedBufferForTesting buf(size); | 1041 mojo::internal::FixedBufferForTesting buf(size); |
| 1021 internal::ObjectUnion_Data* data = nullptr; | 1042 internal::ObjectUnion_Data* data = nullptr; |
| 1022 mojo::internal::Serialize<ObjectUnionPtr>(obj, &buf, &data, false, nullptr); | 1043 mojo::internal::Serialize<ObjectUnionDataView>(obj, &buf, &data, false, |
| 1044 nullptr); |
| 1023 | 1045 |
| 1024 void* raw_buf = buf.Leak(); | 1046 void* raw_buf = buf.Leak(); |
| 1025 mojo::internal::ValidationContext validation_context( | 1047 mojo::internal::ValidationContext validation_context( |
| 1026 data, static_cast<uint32_t>(size), 0); | 1048 data, static_cast<uint32_t>(size), 0); |
| 1027 EXPECT_FALSE(internal::ObjectUnion_Data::Validate( | 1049 EXPECT_FALSE(internal::ObjectUnion_Data::Validate( |
| 1028 raw_buf, &validation_context, false)); | 1050 raw_buf, &validation_context, false)); |
| 1029 free(raw_buf); | 1051 free(raw_buf); |
| 1030 } | 1052 } |
| 1031 | 1053 |
| 1032 TEST(UnionTest, HandleInUnionGetterSetter) { | 1054 TEST(UnionTest, HandleInUnionGetterSetter) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1050 TEST(UnionTest, HandleInUnionSerialization) { | 1072 TEST(UnionTest, HandleInUnionSerialization) { |
| 1051 ScopedMessagePipeHandle pipe0; | 1073 ScopedMessagePipeHandle pipe0; |
| 1052 ScopedMessagePipeHandle pipe1; | 1074 ScopedMessagePipeHandle pipe1; |
| 1053 | 1075 |
| 1054 CreateMessagePipe(nullptr, &pipe0, &pipe1); | 1076 CreateMessagePipe(nullptr, &pipe0, &pipe1); |
| 1055 | 1077 |
| 1056 HandleUnionPtr handle(HandleUnion::New()); | 1078 HandleUnionPtr handle(HandleUnion::New()); |
| 1057 handle->set_f_message_pipe(std::move(pipe1)); | 1079 handle->set_f_message_pipe(std::move(pipe1)); |
| 1058 | 1080 |
| 1059 mojo::internal::SerializationContext context; | 1081 mojo::internal::SerializationContext context; |
| 1060 size_t size = mojo::internal::PrepareToSerialize<HandleUnionPtr>( | 1082 size_t size = mojo::internal::PrepareToSerialize<HandleUnionDataView>( |
| 1061 handle, false, &context); | 1083 handle, false, &context); |
| 1062 EXPECT_EQ(16U, size); | 1084 EXPECT_EQ(16U, size); |
| 1063 | 1085 |
| 1064 mojo::internal::FixedBufferForTesting buf(size); | 1086 mojo::internal::FixedBufferForTesting buf(size); |
| 1065 internal::HandleUnion_Data* data = nullptr; | 1087 internal::HandleUnion_Data* data = nullptr; |
| 1066 mojo::internal::Serialize<HandleUnionPtr>(handle, &buf, &data, false, | 1088 mojo::internal::Serialize<HandleUnionDataView>(handle, &buf, &data, false, |
| 1067 &context); | 1089 &context); |
| 1068 EXPECT_EQ(1U, context.handles.size()); | 1090 EXPECT_EQ(1U, context.handles.size()); |
| 1069 | 1091 |
| 1070 HandleUnionPtr handle2(HandleUnion::New()); | 1092 HandleUnionPtr handle2(HandleUnion::New()); |
| 1071 mojo::internal::Deserialize<HandleUnionPtr>(data, &handle2, &context); | 1093 mojo::internal::Deserialize<HandleUnionDataView>(data, &handle2, &context); |
| 1072 | 1094 |
| 1073 std::string golden("hello world"); | 1095 std::string golden("hello world"); |
| 1074 WriteTextMessage(pipe0.get(), golden); | 1096 WriteTextMessage(pipe0.get(), golden); |
| 1075 | 1097 |
| 1076 std::string actual; | 1098 std::string actual; |
| 1077 ReadTextMessage(handle2->get_f_message_pipe().get(), &actual); | 1099 ReadTextMessage(handle2->get_f_message_pipe().get(), &actual); |
| 1078 | 1100 |
| 1079 EXPECT_EQ(golden, actual); | 1101 EXPECT_EQ(golden, actual); |
| 1080 } | 1102 } |
| 1081 | 1103 |
| 1082 TEST(UnionTest, HandleInUnionValidation) { | 1104 TEST(UnionTest, HandleInUnionValidation) { |
| 1083 ScopedMessagePipeHandle pipe0; | 1105 ScopedMessagePipeHandle pipe0; |
| 1084 ScopedMessagePipeHandle pipe1; | 1106 ScopedMessagePipeHandle pipe1; |
| 1085 | 1107 |
| 1086 CreateMessagePipe(nullptr, &pipe0, &pipe1); | 1108 CreateMessagePipe(nullptr, &pipe0, &pipe1); |
| 1087 | 1109 |
| 1088 HandleUnionPtr handle(HandleUnion::New()); | 1110 HandleUnionPtr handle(HandleUnion::New()); |
| 1089 handle->set_f_message_pipe(std::move(pipe1)); | 1111 handle->set_f_message_pipe(std::move(pipe1)); |
| 1090 | 1112 |
| 1091 mojo::internal::SerializationContext context; | 1113 mojo::internal::SerializationContext context; |
| 1092 size_t size = mojo::internal::PrepareToSerialize<HandleUnionPtr>( | 1114 size_t size = mojo::internal::PrepareToSerialize<HandleUnionDataView>( |
| 1093 handle, false, &context); | 1115 handle, false, &context); |
| 1094 EXPECT_EQ(16U, size); | 1116 EXPECT_EQ(16U, size); |
| 1095 | 1117 |
| 1096 mojo::internal::FixedBufferForTesting buf(size); | 1118 mojo::internal::FixedBufferForTesting buf(size); |
| 1097 internal::HandleUnion_Data* data = nullptr; | 1119 internal::HandleUnion_Data* data = nullptr; |
| 1098 mojo::internal::Serialize<HandleUnionPtr>(handle, &buf, &data, false, | 1120 mojo::internal::Serialize<HandleUnionDataView>(handle, &buf, &data, false, |
| 1099 &context); | 1121 &context); |
| 1100 | 1122 |
| 1101 void* raw_buf = buf.Leak(); | 1123 void* raw_buf = buf.Leak(); |
| 1102 mojo::internal::ValidationContext validation_context( | 1124 mojo::internal::ValidationContext validation_context( |
| 1103 data, static_cast<uint32_t>(size), 1); | 1125 data, static_cast<uint32_t>(size), 1); |
| 1104 EXPECT_TRUE(internal::HandleUnion_Data::Validate( | 1126 EXPECT_TRUE(internal::HandleUnion_Data::Validate( |
| 1105 raw_buf, &validation_context, false)); | 1127 raw_buf, &validation_context, false)); |
| 1106 free(raw_buf); | 1128 free(raw_buf); |
| 1107 } | 1129 } |
| 1108 | 1130 |
| 1109 TEST(UnionTest, HandleInUnionValidationNull) { | 1131 TEST(UnionTest, HandleInUnionValidationNull) { |
| 1110 mojo::internal::SerializationWarningObserverForTesting suppress_warning; | 1132 mojo::internal::SerializationWarningObserverForTesting suppress_warning; |
| 1111 | 1133 |
| 1112 ScopedMessagePipeHandle pipe; | 1134 ScopedMessagePipeHandle pipe; |
| 1113 HandleUnionPtr handle(HandleUnion::New()); | 1135 HandleUnionPtr handle(HandleUnion::New()); |
| 1114 handle->set_f_message_pipe(std::move(pipe)); | 1136 handle->set_f_message_pipe(std::move(pipe)); |
| 1115 | 1137 |
| 1116 mojo::internal::SerializationContext context; | 1138 mojo::internal::SerializationContext context; |
| 1117 size_t size = mojo::internal::PrepareToSerialize<HandleUnionPtr>( | 1139 size_t size = mojo::internal::PrepareToSerialize<HandleUnionDataView>( |
| 1118 handle, false, &context); | 1140 handle, false, &context); |
| 1119 EXPECT_EQ(16U, size); | 1141 EXPECT_EQ(16U, size); |
| 1120 | 1142 |
| 1121 mojo::internal::FixedBufferForTesting buf(size); | 1143 mojo::internal::FixedBufferForTesting buf(size); |
| 1122 internal::HandleUnion_Data* data = nullptr; | 1144 internal::HandleUnion_Data* data = nullptr; |
| 1123 mojo::internal::Serialize<HandleUnionPtr>(handle, &buf, &data, false, | 1145 mojo::internal::Serialize<HandleUnionDataView>(handle, &buf, &data, false, |
| 1124 &context); | 1146 &context); |
| 1125 | 1147 |
| 1126 void* raw_buf = buf.Leak(); | 1148 void* raw_buf = buf.Leak(); |
| 1127 mojo::internal::ValidationContext validation_context( | 1149 mojo::internal::ValidationContext validation_context( |
| 1128 data, static_cast<uint32_t>(size), 1); | 1150 data, static_cast<uint32_t>(size), 1); |
| 1129 EXPECT_FALSE(internal::HandleUnion_Data::Validate( | 1151 EXPECT_FALSE(internal::HandleUnion_Data::Validate( |
| 1130 raw_buf, &validation_context, false)); | 1152 raw_buf, &validation_context, false)); |
| 1131 free(raw_buf); | 1153 free(raw_buf); |
| 1132 } | 1154 } |
| 1133 | 1155 |
| 1134 class SmallCacheImpl : public SmallCache { | 1156 class SmallCacheImpl : public SmallCache { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1169 TEST(UnionTest, InterfaceInUnionSerialization) { | 1191 TEST(UnionTest, InterfaceInUnionSerialization) { |
| 1170 base::MessageLoop message_loop; | 1192 base::MessageLoop message_loop; |
| 1171 base::RunLoop run_loop; | 1193 base::RunLoop run_loop; |
| 1172 SmallCacheImpl impl(run_loop.QuitClosure()); | 1194 SmallCacheImpl impl(run_loop.QuitClosure()); |
| 1173 SmallCachePtr ptr; | 1195 SmallCachePtr ptr; |
| 1174 Binding<SmallCache> bindings(&impl, GetProxy(&ptr)); | 1196 Binding<SmallCache> bindings(&impl, GetProxy(&ptr)); |
| 1175 | 1197 |
| 1176 mojo::internal::SerializationContext context; | 1198 mojo::internal::SerializationContext context; |
| 1177 HandleUnionPtr handle(HandleUnion::New()); | 1199 HandleUnionPtr handle(HandleUnion::New()); |
| 1178 handle->set_f_small_cache(std::move(ptr)); | 1200 handle->set_f_small_cache(std::move(ptr)); |
| 1179 size_t size = mojo::internal::PrepareToSerialize<HandleUnionPtr>( | 1201 size_t size = mojo::internal::PrepareToSerialize<HandleUnionDataView>( |
| 1180 handle, false, &context); | 1202 handle, false, &context); |
| 1181 EXPECT_EQ(16U, size); | 1203 EXPECT_EQ(16U, size); |
| 1182 | 1204 |
| 1183 mojo::internal::FixedBufferForTesting buf(size); | 1205 mojo::internal::FixedBufferForTesting buf(size); |
| 1184 internal::HandleUnion_Data* data = nullptr; | 1206 internal::HandleUnion_Data* data = nullptr; |
| 1185 mojo::internal::Serialize<HandleUnionPtr>(handle, &buf, &data, false, | 1207 mojo::internal::Serialize<HandleUnionDataView>(handle, &buf, &data, false, |
| 1186 &context); | 1208 &context); |
| 1187 EXPECT_EQ(1U, context.handles.size()); | 1209 EXPECT_EQ(1U, context.handles.size()); |
| 1188 | 1210 |
| 1189 HandleUnionPtr handle2(HandleUnion::New()); | 1211 HandleUnionPtr handle2(HandleUnion::New()); |
| 1190 mojo::internal::Deserialize<HandleUnionPtr>(data, &handle2, &context); | 1212 mojo::internal::Deserialize<HandleUnionDataView>(data, &handle2, &context); |
| 1191 | 1213 |
| 1192 handle2->get_f_small_cache()->SetIntValue(10); | 1214 handle2->get_f_small_cache()->SetIntValue(10); |
| 1193 run_loop.Run(); | 1215 run_loop.Run(); |
| 1194 EXPECT_EQ(10, impl.int_value()); | 1216 EXPECT_EQ(10, impl.int_value()); |
| 1195 } | 1217 } |
| 1196 | 1218 |
| 1197 class UnionInterfaceImpl : public UnionInterface { | 1219 class UnionInterfaceImpl : public UnionInterface { |
| 1198 public: | 1220 public: |
| 1199 UnionInterfaceImpl() {} | 1221 UnionInterfaceImpl() {} |
| 1200 ~UnionInterfaceImpl() override {} | 1222 ~UnionInterfaceImpl() override {} |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1217 | 1239 |
| 1218 PodUnionPtr pod(PodUnion::New()); | 1240 PodUnionPtr pod(PodUnion::New()); |
| 1219 pod->set_f_int16(16); | 1241 pod->set_f_int16(16); |
| 1220 | 1242 |
| 1221 ptr->Echo(std::move(pod), base::Bind(&ExpectInt16, 16)); | 1243 ptr->Echo(std::move(pod), base::Bind(&ExpectInt16, 16)); |
| 1222 base::RunLoop().RunUntilIdle(); | 1244 base::RunLoop().RunUntilIdle(); |
| 1223 } | 1245 } |
| 1224 | 1246 |
| 1225 } // namespace test | 1247 } // namespace test |
| 1226 } // namespace mojo | 1248 } // namespace mojo |
| OLD | NEW |