Chromium Code Reviews| 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 "mojo/public/cpp/bindings/lib/array_internal.h" | |
| 6 #include "mojo/public/cpp/bindings/lib/array_serialization.h" | |
| 5 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" | 7 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" |
| 6 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" | 8 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" |
| 7 #include "mojo/public/cpp/bindings/string.h" | 9 #include "mojo/public/cpp/bindings/string.h" |
| 8 #include "mojo/public/cpp/environment/environment.h" | 10 #include "mojo/public/cpp/environment/environment.h" |
| 9 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom.h" | 11 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 13 |
| 12 namespace mojo { | 14 namespace mojo { |
| 13 namespace test { | 15 namespace test { |
| 14 | 16 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 101 } | 103 } |
| 102 | 104 |
| 103 TEST(UnionTest, SerializationPod) { | 105 TEST(UnionTest, SerializationPod) { |
| 104 PodUnionPtr pod1(PodUnion::New()); | 106 PodUnionPtr pod1(PodUnion::New()); |
| 105 pod1->set_f_int8(10); | 107 pod1->set_f_int8(10); |
| 106 | 108 |
| 107 size_t size = GetSerializedSize_(pod1); | 109 size_t size = GetSerializedSize_(pod1); |
| 108 EXPECT_EQ(16U, size); | 110 EXPECT_EQ(16U, size); |
| 109 | 111 |
| 110 mojo::internal::FixedBuffer buf(size); | 112 mojo::internal::FixedBuffer buf(size); |
| 111 internal::PodUnion_Data* data; | 113 internal::PodUnion_Data* data = nullptr; |
| 112 Serialize_(pod1.Pass(), &buf, &data); | 114 Serialize_(pod1.Pass(), &buf, &data); |
| 113 | 115 |
| 114 PodUnionPtr pod2; | 116 PodUnionPtr pod2; |
| 115 Deserialize_(data, &pod2); | 117 Deserialize_(data, &pod2); |
| 116 | 118 |
| 117 EXPECT_EQ(10, pod2->get_f_int8()); | 119 EXPECT_EQ(10, pod2->get_f_int8()); |
| 118 EXPECT_TRUE(pod2->is_f_int8()); | 120 EXPECT_TRUE(pod2->is_f_int8()); |
| 119 EXPECT_EQ(pod2->which(), PodUnion::Tag::F_INT8); | 121 EXPECT_EQ(pod2->which(), PodUnion::Tag::F_INT8); |
| 120 } | 122 } |
| 121 | 123 |
| 122 TEST(UnionTest, ValidationJustWorksPod) { | 124 TEST(UnionTest, PodValidation) { |
| 123 PodUnionPtr pod(PodUnion::New()); | 125 PodUnionPtr pod(PodUnion::New()); |
| 124 pod->set_f_int8(10); | 126 pod->set_f_int8(10); |
| 125 | 127 |
| 126 size_t size = GetSerializedSize_(pod); | 128 size_t size = GetSerializedSize_(pod); |
| 127 EXPECT_EQ(16U, size); | 129 EXPECT_EQ(16U, size); |
| 128 | 130 |
| 129 mojo::internal::FixedBuffer buf(size); | 131 mojo::internal::FixedBuffer buf(size); |
| 130 internal::PodUnion_Data* data; | 132 internal::PodUnion_Data* data = nullptr; |
| 131 Serialize_(pod.Pass(), &buf, &data); | 133 Serialize_(pod.Pass(), &buf, &data); |
| 132 void* raw_buf = buf.Leak(); | 134 void* raw_buf = buf.Leak(); |
| 133 mojo::internal::BoundsChecker bounds_checker(data, size, 0); | 135 mojo::internal::BoundsChecker bounds_checker(data, size, 0); |
| 134 EXPECT_TRUE(internal::PodUnion_Data::Validate(raw_buf, &bounds_checker)); | 136 EXPECT_TRUE( |
| 137 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false)); | |
| 135 free(raw_buf); | 138 free(raw_buf); |
| 136 } | 139 } |
| 137 | 140 |
| 141 TEST(UnionTest, SerializeNotNull) { | |
| 142 PodUnionPtr pod(PodUnion::New()); | |
| 143 pod->set_f_int8(0); | |
| 144 size_t size = GetSerializedSize_(pod); | |
| 145 mojo::internal::FixedBuffer buf(size); | |
| 146 internal::PodUnion_Data* data = nullptr; | |
| 147 Serialize_(pod.Pass(), &buf, &data); | |
| 148 EXPECT_FALSE(data->is_null()); | |
| 149 } | |
| 150 | |
| 151 TEST(UnionTest, SerializeIsNull) { | |
| 152 PodUnionPtr pod; | |
| 153 size_t size = GetSerializedSize_(pod); | |
| 154 EXPECT_EQ(16U, size); | |
| 155 mojo::internal::FixedBuffer buf(size); | |
| 156 internal::PodUnion_Data* data = nullptr; | |
| 157 Serialize_(pod.Pass(), &buf, &data); | |
| 158 EXPECT_TRUE(data->is_null()); | |
| 159 | |
| 160 PodUnionPtr pod2; | |
| 161 Deserialize_(data, &pod2); | |
| 162 EXPECT_TRUE(pod2.is_null()); | |
| 163 } | |
| 164 | |
| 138 TEST(UnionTest, NullValidation) { | 165 TEST(UnionTest, NullValidation) { |
| 139 void* buf = nullptr; | 166 void* buf = nullptr; |
| 140 mojo::internal::BoundsChecker bounds_checker(buf, 0, 0); | 167 mojo::internal::BoundsChecker bounds_checker(buf, 0, 0); |
| 141 EXPECT_TRUE(internal::PodUnion_Data::Validate(buf, &bounds_checker)); | 168 EXPECT_TRUE(internal::PodUnion_Data::Validate(buf, &bounds_checker, false)); |
| 142 } | 169 } |
| 143 | 170 |
| 144 TEST(UnionTest, OutOfAlignmentValidation) { | 171 TEST(UnionTest, OutOfAlignmentValidation) { |
| 145 Environment environment; | 172 Environment environment; |
| 146 size_t size = sizeof(internal::PodUnion_Data); | 173 size_t size = sizeof(internal::PodUnion_Data); |
| 147 // Get an aligned object and shift the alignment. | 174 // Get an aligned object and shift the alignment. |
| 148 mojo::internal::FixedBuffer aligned_buf(size + 1); | 175 mojo::internal::FixedBuffer aligned_buf(size + 1); |
| 149 void* raw_buf = aligned_buf.Leak(); | 176 void* raw_buf = aligned_buf.Leak(); |
| 150 char* buf = reinterpret_cast<char*>(raw_buf) + 1; | 177 char* buf = reinterpret_cast<char*>(raw_buf) + 1; |
| 151 | 178 |
| 152 internal::PodUnion_Data* data = | 179 internal::PodUnion_Data* data = |
| 153 reinterpret_cast<internal::PodUnion_Data*>(buf); | 180 reinterpret_cast<internal::PodUnion_Data*>(buf); |
| 154 mojo::internal::BoundsChecker bounds_checker(data, size, 0); | 181 mojo::internal::BoundsChecker bounds_checker(data, size, 0); |
| 155 EXPECT_FALSE(internal::PodUnion_Data::Validate(buf, &bounds_checker)); | 182 EXPECT_FALSE(internal::PodUnion_Data::Validate(buf, &bounds_checker, false)); |
| 156 free(raw_buf); | 183 free(raw_buf); |
| 157 } | 184 } |
| 158 | 185 |
| 159 TEST(UnionTest, OOBValidation) { | 186 TEST(UnionTest, OOBValidation) { |
| 160 Environment environment; | 187 Environment environment; |
| 161 size_t size = sizeof(internal::PodUnion_Data) - 1; | 188 size_t size = sizeof(internal::PodUnion_Data) - 1; |
| 162 mojo::internal::FixedBuffer buf(size); | 189 mojo::internal::FixedBuffer buf(size); |
| 163 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf); | 190 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf); |
| 164 mojo::internal::BoundsChecker bounds_checker(data, size, 0); | 191 mojo::internal::BoundsChecker bounds_checker(data, size, 0); |
| 165 void* raw_buf = buf.Leak(); | 192 void* raw_buf = buf.Leak(); |
| 166 EXPECT_FALSE(internal::PodUnion_Data::Validate(raw_buf, &bounds_checker)); | 193 EXPECT_FALSE( |
| 194 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false)); | |
| 167 free(raw_buf); | 195 free(raw_buf); |
| 168 } | 196 } |
| 169 | 197 |
| 170 TEST(UnionTest, UnknownTagValidation) { | 198 TEST(UnionTest, UnknownTagValidation) { |
| 171 Environment environment; | 199 Environment environment; |
| 172 size_t size = sizeof(internal::PodUnion_Data); | 200 size_t size = sizeof(internal::PodUnion_Data); |
| 173 mojo::internal::FixedBuffer buf(size); | 201 mojo::internal::FixedBuffer buf(size); |
| 174 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf); | 202 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf); |
| 175 data->tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(0xFFFFFF); | 203 data->tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(0xFFFFFF); |
| 176 mojo::internal::BoundsChecker bounds_checker(data, size, 0); | 204 mojo::internal::BoundsChecker bounds_checker(data, size, 0); |
| 177 void* raw_buf = buf.Leak(); | 205 void* raw_buf = buf.Leak(); |
| 178 EXPECT_FALSE(internal::PodUnion_Data::Validate(raw_buf, &bounds_checker)); | 206 EXPECT_FALSE( |
| 207 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false)); | |
| 179 free(raw_buf); | 208 free(raw_buf); |
| 180 } | 209 } |
| 181 | 210 |
| 182 TEST(UnionTest, StringGetterSetter) { | 211 TEST(UnionTest, StringGetterSetter) { |
| 183 ObjectUnionPtr pod(ObjectUnion::New()); | 212 ObjectUnionPtr pod(ObjectUnion::New()); |
| 184 | 213 |
| 185 String hello("hello world"); | 214 String hello("hello world"); |
| 186 pod->set_f_string(hello); | 215 pod->set_f_string(hello); |
| 187 EXPECT_EQ(hello, pod->get_f_string()); | 216 EXPECT_EQ(hello, pod->get_f_string()); |
| 188 EXPECT_TRUE(pod->is_f_string()); | 217 EXPECT_TRUE(pod->is_f_string()); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 213 } | 242 } |
| 214 | 243 |
| 215 TEST(UnionTest, StringSerialization) { | 244 TEST(UnionTest, StringSerialization) { |
| 216 ObjectUnionPtr pod1(ObjectUnion::New()); | 245 ObjectUnionPtr pod1(ObjectUnion::New()); |
| 217 | 246 |
| 218 String hello("hello world"); | 247 String hello("hello world"); |
| 219 pod1->set_f_string(hello); | 248 pod1->set_f_string(hello); |
| 220 | 249 |
| 221 size_t size = GetSerializedSize_(pod1); | 250 size_t size = GetSerializedSize_(pod1); |
| 222 mojo::internal::FixedBuffer buf(size); | 251 mojo::internal::FixedBuffer buf(size); |
| 223 internal::ObjectUnion_Data* data; | 252 internal::ObjectUnion_Data* data = nullptr; |
| 224 Serialize_(pod1.Pass(), &buf, &data); | 253 Serialize_(pod1.Pass(), &buf, &data); |
| 225 | 254 |
| 226 ObjectUnionPtr pod2; | 255 ObjectUnionPtr pod2; |
| 227 Deserialize_(data, &pod2); | 256 Deserialize_(data, &pod2); |
| 228 EXPECT_EQ(hello, pod2->get_f_string()); | 257 EXPECT_EQ(hello, pod2->get_f_string()); |
| 229 EXPECT_TRUE(pod2->is_f_string()); | 258 EXPECT_TRUE(pod2->is_f_string()); |
| 230 EXPECT_EQ(pod2->which(), ObjectUnion::Tag::F_STRING); | 259 EXPECT_EQ(pod2->which(), ObjectUnion::Tag::F_STRING); |
| 231 } | 260 } |
| 232 | 261 |
| 233 TEST(UnionTest, StringValidationNull) { | 262 TEST(UnionTest, NullStringValidation) { |
| 234 Environment environment; | 263 Environment environment; |
| 235 size_t size = sizeof(internal::ObjectUnion_Data); | 264 size_t size = sizeof(internal::ObjectUnion_Data); |
| 236 mojo::internal::FixedBuffer buf(size); | 265 mojo::internal::FixedBuffer buf(size); |
| 237 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); | 266 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); |
| 238 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; | 267 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; |
| 239 data->data.unknown = 0x0; | 268 data->data.unknown = 0x0; |
| 240 mojo::internal::BoundsChecker bounds_checker(data, size, 0); | 269 mojo::internal::BoundsChecker bounds_checker(data, size, 0); |
| 241 void* raw_buf = buf.Leak(); | 270 void* raw_buf = buf.Leak(); |
| 242 EXPECT_FALSE(internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker)); | 271 EXPECT_FALSE( |
| 272 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); | |
| 243 free(raw_buf); | 273 free(raw_buf); |
| 244 } | 274 } |
| 245 | 275 |
| 246 TEST(UnionTest, StringValidationPointerOverflow) { | 276 TEST(UnionTest, StringPointerOverflowValidation) { |
| 247 Environment environment; | 277 Environment environment; |
| 248 size_t size = sizeof(internal::ObjectUnion_Data); | 278 size_t size = sizeof(internal::ObjectUnion_Data); |
| 249 mojo::internal::FixedBuffer buf(size); | 279 mojo::internal::FixedBuffer buf(size); |
| 250 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); | 280 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); |
| 251 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; | 281 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; |
| 252 data->data.unknown = 0xFFFFFFFFFFFFFFFF; | 282 data->data.unknown = 0xFFFFFFFFFFFFFFFF; |
| 253 mojo::internal::BoundsChecker bounds_checker(data, size, 0); | 283 mojo::internal::BoundsChecker bounds_checker(data, size, 0); |
| 254 void* raw_buf = buf.Leak(); | 284 void* raw_buf = buf.Leak(); |
| 255 EXPECT_FALSE(internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker)); | 285 EXPECT_FALSE( |
| 286 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); | |
| 256 free(raw_buf); | 287 free(raw_buf); |
| 257 } | 288 } |
| 258 | 289 |
| 259 TEST(UnionTest, StringValidationValidateString) { | 290 TEST(UnionTest, StringValidateStringValidation) { |
|
yzshen1
2015/02/25 21:07:00
nit: I cannot parse this test name...
azani
2015/03/03 00:44:15
Done.
| |
| 260 Environment environment; | 291 Environment environment; |
| 261 size_t size = 32; | 292 size_t size = 32; |
| 262 mojo::internal::FixedBuffer buf(size); | 293 mojo::internal::FixedBuffer buf(size); |
| 263 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); | 294 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); |
| 264 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; | 295 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; |
| 265 | 296 |
| 266 data->data.f_f_string = 8; | 297 data->data.f_f_string = 8; |
| 267 char* ptr = reinterpret_cast<char*>(&data->data.f_f_string); | 298 char* ptr = reinterpret_cast<char*>(&data->data.f_f_string); |
| 268 mojo::internal::ArrayHeader* array_header = | 299 mojo::internal::ArrayHeader* array_header = |
| 269 reinterpret_cast<mojo::internal::ArrayHeader*>(ptr + *ptr); | 300 reinterpret_cast<mojo::internal::ArrayHeader*>(ptr + *ptr); |
| 270 array_header->num_bytes = 20; // This should go out of bounds. | 301 array_header->num_bytes = 20; // This should go out of bounds. |
| 271 array_header->num_elements = 20; | 302 array_header->num_elements = 20; |
| 272 mojo::internal::BoundsChecker bounds_checker(data, 32, 0); | 303 mojo::internal::BoundsChecker bounds_checker(data, 32, 0); |
| 273 void* raw_buf = buf.Leak(); | 304 void* raw_buf = buf.Leak(); |
| 274 EXPECT_FALSE(internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker)); | 305 EXPECT_FALSE( |
| 275 free(raw_buf); | 306 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
| 276 } | 307 free(raw_buf); |
| 308 } | |
| 309 | |
| 310 TEST(UnionTest, PodUnionInStructClone) { | |
| 311 SmallStructPtr small_struct(SmallStruct::New()); | |
| 312 small_struct->pod_union = PodUnion::New(); | |
| 313 small_struct->pod_union->set_f_int8(10); | |
| 314 | |
| 315 SmallStructPtr clone = small_struct.Clone(); | |
| 316 EXPECT_EQ(10, clone->pod_union->get_f_int8()); | |
| 317 } | |
| 318 | |
| 319 TEST(UnionTest, PodUnionInStructSerialization) { | |
| 320 SmallStructPtr small_struct(SmallStruct::New()); | |
| 321 small_struct->pod_union = PodUnion::New(); | |
| 322 small_struct->pod_union->set_f_int32(10); | |
| 323 | |
| 324 size_t size = GetSerializedSize_(small_struct); | |
| 325 | |
| 326 mojo::internal::FixedBuffer buf(size); | |
| 327 internal::SmallStruct_Data* data = nullptr; | |
| 328 Serialize_(small_struct.Pass(), &buf, &data); | |
| 329 | |
| 330 SmallStructPtr deserialized; | |
| 331 Deserialize_(data, &deserialized); | |
| 332 | |
| 333 EXPECT_EQ(10, deserialized->pod_union->get_f_int32()); | |
| 334 } | |
| 335 | |
| 336 TEST(UnionTest, ObjectUnionInStructSerialization) { | |
| 337 SmallObjStructPtr obj_struct(SmallObjStruct::New()); | |
| 338 obj_struct->obj_union = ObjectUnion::New(); | |
| 339 String hello("hello world"); | |
| 340 obj_struct->obj_union->set_f_string(hello); | |
| 341 | |
| 342 size_t size = GetSerializedSize_(obj_struct); | |
| 343 | |
| 344 mojo::internal::FixedBuffer buf(size); | |
| 345 internal::SmallObjStruct_Data* data = nullptr; | |
| 346 Serialize_(obj_struct.Pass(), &buf, &data); | |
| 347 | |
| 348 SmallObjStructPtr deserialized; | |
| 349 Deserialize_(data, &deserialized); | |
| 350 | |
| 351 EXPECT_EQ(hello, deserialized->obj_union->get_f_string()); | |
| 352 } | |
| 353 | |
| 354 TEST(UnionTest, UnionsInStructsValidation) { | |
| 355 SmallStructPtr small_struct(SmallStruct::New()); | |
| 356 small_struct->pod_union = PodUnion::New(); | |
| 357 small_struct->pod_union->set_f_int32(10); | |
| 358 | |
| 359 size_t size = GetSerializedSize_(small_struct); | |
| 360 | |
| 361 mojo::internal::FixedBuffer buf(size); | |
| 362 internal::SmallStruct_Data* data = nullptr; | |
| 363 Serialize_(small_struct.Pass(), &buf, &data); | |
| 364 | |
| 365 void* raw_buf = buf.Leak(); | |
| 366 mojo::internal::BoundsChecker bounds_checker(data, size, 0); | |
| 367 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); | |
| 368 free(raw_buf); | |
| 369 } | |
| 370 | |
| 371 TEST(UnionTest, PodUnionInStructValidationFailure) { | |
| 372 Environment environment; | |
| 373 SmallStructPtr small_struct(SmallStruct::New()); | |
| 374 small_struct->pod_union = PodUnion::New(); | |
| 375 small_struct->pod_union->set_f_int32(10); | |
| 376 | |
| 377 size_t size = GetSerializedSize_(small_struct); | |
| 378 | |
| 379 mojo::internal::FixedBuffer buf(size); | |
| 380 internal::SmallStruct_Data* data = nullptr; | |
| 381 Serialize_(small_struct.Pass(), &buf, &data); | |
| 382 data->pod_union.tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(100); | |
| 383 | |
| 384 void* raw_buf = buf.Leak(); | |
| 385 mojo::internal::BoundsChecker bounds_checker(data, size, 0); | |
| 386 EXPECT_FALSE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); | |
| 387 free(raw_buf); | |
| 388 } | |
| 389 | |
| 390 TEST(UnionTest, NullUnionValidationFailure) { | |
| 391 Environment environment; | |
| 392 SmallStructPtr small_struct(SmallStruct::New()); | |
| 393 | |
| 394 size_t size = GetSerializedSize_(small_struct); | |
| 395 | |
| 396 mojo::internal::FixedBuffer buf(size); | |
| 397 internal::SmallStruct_Data* data = nullptr; | |
| 398 Serialize_(small_struct.Pass(), &buf, &data); | |
| 399 | |
| 400 void* raw_buf = buf.Leak(); | |
| 401 mojo::internal::BoundsChecker bounds_checker(data, size, 0); | |
| 402 EXPECT_FALSE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); | |
| 403 free(raw_buf); | |
| 404 } | |
| 405 | |
| 406 TEST(UnionTest, NullableUnionValidation) { | |
| 407 Environment environment; | |
| 408 SmallStructNullableUnionPtr small_struct(SmallStructNullableUnion::New()); | |
| 409 | |
| 410 size_t size = GetSerializedSize_(small_struct); | |
| 411 | |
| 412 mojo::internal::FixedBuffer buf(size); | |
| 413 internal::SmallStructNullableUnion_Data* data = nullptr; | |
| 414 Serialize_(small_struct.Pass(), &buf, &data); | |
| 415 | |
| 416 void* raw_buf = buf.Leak(); | |
| 417 mojo::internal::BoundsChecker bounds_checker(data, size, 0); | |
| 418 EXPECT_TRUE(internal::SmallStructNullableUnion_Data::Validate( | |
| 419 raw_buf, &bounds_checker)); | |
| 420 free(raw_buf); | |
| 421 } | |
| 422 | |
| 423 TEST(UnionTest, PodUnionInArray) { | |
| 424 SmallStructPtr small_struct(SmallStruct::New()); | |
| 425 small_struct->pod_union_array = Array<PodUnionPtr>(2); | |
| 426 small_struct->pod_union_array[0] = PodUnion::New(); | |
| 427 small_struct->pod_union_array[1] = PodUnion::New(); | |
| 428 | |
| 429 small_struct->pod_union_array[0]->set_f_int8(10); | |
| 430 small_struct->pod_union_array[1]->set_f_int16(12); | |
| 431 | |
| 432 EXPECT_EQ(10, small_struct->pod_union_array[0]->get_f_int8()); | |
| 433 EXPECT_EQ(12, small_struct->pod_union_array[1]->get_f_int16()); | |
| 434 } | |
| 435 | |
| 436 TEST(UnionTest, PodUnionInArraySerialization) { | |
| 437 Environment environment; | |
| 438 | |
| 439 Array<PodUnionPtr> array(2); | |
| 440 array[0] = PodUnion::New(); | |
| 441 array[1] = PodUnion::New(); | |
| 442 | |
| 443 array[0]->set_f_int8(10); | |
| 444 array[1]->set_f_int16(12); | |
| 445 EXPECT_EQ(2U, array.size()); | |
| 446 | |
| 447 size_t size = GetSerializedSize_(array); | |
| 448 EXPECT_EQ(40U, size); | |
| 449 | |
| 450 mojo::internal::FixedBuffer buf(size); | |
| 451 mojo::internal::Array_Data<internal::PodUnion_Data>* data; | |
| 452 SerializeArray_<mojo::internal::ArrayValidateParams< | |
| 453 0, false, mojo::internal::NoValidateParams>>(array.Pass(), &buf, &data); | |
| 454 | |
| 455 Array<PodUnionPtr> array2; | |
| 456 Deserialize_(data, &array2); | |
| 457 | |
| 458 EXPECT_EQ(2U, array2.size()); | |
| 459 | |
| 460 EXPECT_EQ(10, array2[0]->get_f_int8()); | |
| 461 EXPECT_EQ(12, array2[1]->get_f_int16()); | |
| 462 } | |
| 463 | |
| 464 TEST(UnionTest, PodUnionInArraySerializationWithNull) { | |
| 465 Environment environment; | |
| 466 | |
| 467 Array<PodUnionPtr> array(2); | |
| 468 array[0] = PodUnion::New(); | |
| 469 | |
| 470 array[0]->set_f_int8(10); | |
| 471 EXPECT_EQ(2U, array.size()); | |
| 472 | |
| 473 size_t size = GetSerializedSize_(array); | |
| 474 EXPECT_EQ(40U, size); | |
| 475 | |
| 476 mojo::internal::FixedBuffer buf(size); | |
| 477 mojo::internal::Array_Data<internal::PodUnion_Data>* data; | |
| 478 SerializeArray_<mojo::internal::ArrayValidateParams< | |
| 479 0, false, mojo::internal::NoValidateParams>>(array.Pass(), &buf, &data); | |
|
yzshen1
2015/02/25 21:07:00
false -> true, meaning that elements are nullable.
azani
2015/03/03 00:44:15
Done.
| |
| 480 | |
| 481 Array<PodUnionPtr> array2; | |
| 482 Deserialize_(data, &array2); | |
| 483 | |
| 484 EXPECT_EQ(2U, array2.size()); | |
| 485 | |
| 486 EXPECT_EQ(10, array2[0]->get_f_int8()); | |
| 487 EXPECT_TRUE(array2[1].is_null()); | |
| 488 } | |
| 489 | |
| 490 /* | |
|
yzshen1
2015/02/25 21:07:00
Please remove any code if it is not needed. (Or ma
| |
| 491 TEST(UnionTest, PodUnionInArrayInStruct) { | |
| 492 Environment environment; | |
| 493 SmallStructPtr small_struct(SmallStruct::New()); | |
| 494 | |
| 495 small_struct->pod_union_array = Array<PodUnionPtr>(2); | |
| 496 small_struct->pod_union_array[0] = PodUnion::New(); | |
| 497 small_struct->pod_union_array[1] = PodUnion::New(); | |
| 498 | |
| 499 small_struct->pod_union_array[0]->set_f_int8(10); | |
| 500 small_struct->pod_union_array[1]->set_f_int16(12); | |
| 501 | |
| 502 size_t size = GetSerializedSize_(small_struct); | |
| 503 EXPECT_EQ(88U, size); | |
| 504 | |
| 505 mojo::internal::FixedBuffer buf(size); | |
| 506 internal::SmallStruct_Data* data = nullptr; | |
| 507 Serialize_(small_struct.Pass(), &buf, &data); | |
| 508 | |
| 509 SmallStructPtr deserialized; | |
| 510 Deserialize_(data, &deserialized); | |
| 511 | |
| 512 EXPECT_EQ(10, deserialized->pod_union_array[0]->get_f_int8()); | |
| 513 EXPECT_EQ(12, deserialized->pod_union_array[1]->get_f_int16()); | |
| 514 } | |
| 515 */ | |
| 516 | |
| 277 } // namespace test | 517 } // namespace test |
| 278 } // namespace mojo | 518 } // namespace mojo |
| OLD | NEW |