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

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

Issue 1509703002: Mojo C++ bindings: Fix bug: array<>, map<> should only initialize elements if they're not null when… (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/public/cpp/bindings/array.h" 5 #include "mojo/public/cpp/bindings/array.h"
6 #include "mojo/public/cpp/bindings/lib/array_serialization.h" 6 #include "mojo/public/cpp/bindings/lib/array_serialization.h"
7 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" 7 #include "mojo/public/cpp/bindings/lib/bindings_internal.h"
8 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" 8 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
9 #include "mojo/public/cpp/bindings/lib/map_serialization.h" 9 #include "mojo/public/cpp/bindings/lib/map_serialization.h"
10 #include "mojo/public/cpp/bindings/lib/validate_params.h" 10 #include "mojo/public/cpp/bindings/lib/validate_params.h"
11 #include "mojo/public/cpp/bindings/map.h" 11 #include "mojo/public/cpp/bindings/map.h"
12 #include "mojo/public/cpp/bindings/string.h" 12 #include "mojo/public/cpp/bindings/string.h"
13 #include "mojo/public/cpp/bindings/tests/container_test_util.h" 13 #include "mojo/public/cpp/bindings/tests/container_test_util.h"
14 #include "mojo/public/cpp/environment/environment.h" 14 #include "mojo/public/cpp/environment/environment.h"
15 #include "mojo/public/interfaces/bindings/tests/rect.mojom.h"
15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
16 17
17 namespace mojo { 18 namespace mojo {
18 namespace test { 19 namespace test {
19 20
20 namespace { 21 namespace {
21 22
22 using mojo::internal::Array_Data; 23 using mojo::internal::Array_Data;
23 using mojo::internal::ArrayValidateParams; 24 using mojo::internal::ArrayValidateParams;
24 using mojo::internal::FixedBufferForTesting; 25 using mojo::internal::FixedBufferForTesting;
25 using mojo::internal::Map_Data; 26 using mojo::internal::Map_Data;
26 using mojo::internal::String_Data; 27 using mojo::internal::String_Data;
28 using mojo::internal::ValidationError;
27 29
28 struct StringIntData { 30 struct StringIntData {
29 const char* string_data; 31 const char* string_data;
30 int int_data; 32 int int_data;
31 } kStringIntData[] = { 33 } kStringIntData[] = {
32 {"one", 1}, 34 {"one", 1},
33 {"two", 2}, 35 {"two", 2},
34 {"three", 3}, 36 {"three", 3},
35 {"four", 4}, 37 {"four", 4},
36 }; 38 };
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 TEST_F(MapTest, ArrayOfMap) { 251 TEST_F(MapTest, ArrayOfMap) {
250 { 252 {
251 auto array = Array<Map<int32_t, int8_t>>::New(1); 253 auto array = Array<Map<int32_t, int8_t>>::New(1);
252 array[0].insert(1, 42); 254 array[0].insert(1, 42);
253 255
254 size_t size = GetSerializedSize_(array); 256 size_t size = GetSerializedSize_(array);
255 FixedBufferForTesting buf(size); 257 FixedBufferForTesting buf(size);
256 Array_Data<Map_Data<int32_t, int8_t>*>* data = nullptr; 258 Array_Data<Map_Data<int32_t, int8_t>*>* data = nullptr;
257 ArrayValidateParams validate_params( 259 ArrayValidateParams validate_params(
258 0, false, new ArrayValidateParams(0, false, nullptr)); 260 0, false, new ArrayValidateParams(0, false, nullptr));
259 EXPECT_EQ(internal::ValidationError::NONE, 261 EXPECT_EQ(ValidationError::NONE,
260 SerializeArray_(&array, &buf, &data, &validate_params)); 262 SerializeArray_(&array, &buf, &data, &validate_params));
261 263
262 Array<Map<int32_t, int8_t>> deserialized_array; 264 Array<Map<int32_t, int8_t>> deserialized_array;
263 Deserialize_(data, &deserialized_array); 265 Deserialize_(data, &deserialized_array);
264 266
265 ASSERT_EQ(1u, deserialized_array.size()); 267 ASSERT_EQ(1u, deserialized_array.size());
266 ASSERT_EQ(1u, deserialized_array[0].size()); 268 ASSERT_EQ(1u, deserialized_array[0].size());
267 ASSERT_EQ(42, deserialized_array[0].at(1)); 269 ASSERT_EQ(42, deserialized_array[0].at(1));
268 } 270 }
269 271
270 { 272 {
271 auto array = Array<Map<String, Array<bool>>>::New(1); 273 auto array = Array<Map<String, Array<bool>>>::New(1);
272 auto map_value = Array<bool>::New(2); 274 auto map_value = Array<bool>::New(2);
273 map_value[0] = false; 275 map_value[0] = false;
274 map_value[1] = true; 276 map_value[1] = true;
275 array[0].insert("hello world", map_value.Pass()); 277 array[0].insert("hello world", map_value.Pass());
276 278
277 size_t size = GetSerializedSize_(array); 279 size_t size = GetSerializedSize_(array);
278 FixedBufferForTesting buf(size); 280 FixedBufferForTesting buf(size);
279 Array_Data<Map_Data<String_Data*, Array_Data<bool>*>*>* data = nullptr; 281 Array_Data<Map_Data<String_Data*, Array_Data<bool>*>*>* data = nullptr;
280 ArrayValidateParams validate_params( 282 ArrayValidateParams validate_params(
281 0, false, new ArrayValidateParams( 283 0, false, new ArrayValidateParams(
282 0, false, new ArrayValidateParams(0, false, nullptr))); 284 0, false, new ArrayValidateParams(0, false, nullptr)));
283 EXPECT_EQ(internal::ValidationError::NONE, 285 EXPECT_EQ(ValidationError::NONE,
284 SerializeArray_(&array, &buf, &data, &validate_params)); 286 SerializeArray_(&array, &buf, &data, &validate_params));
285 287
286 Array<Map<String, Array<bool>>> deserialized_array; 288 Array<Map<String, Array<bool>>> deserialized_array;
287 Deserialize_(data, &deserialized_array); 289 Deserialize_(data, &deserialized_array);
288 290
289 ASSERT_EQ(1u, deserialized_array.size()); 291 ASSERT_EQ(1u, deserialized_array.size());
290 ASSERT_EQ(1u, deserialized_array[0].size()); 292 ASSERT_EQ(1u, deserialized_array[0].size());
291 ASSERT_FALSE(deserialized_array[0].at("hello world")[0]); 293 ASSERT_FALSE(deserialized_array[0].at("hello world")[0]);
292 ASSERT_TRUE(deserialized_array[0].at("hello world")[1]); 294 ASSERT_TRUE(deserialized_array[0].at("hello world")[1]);
293 } 295 }
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 ASSERT_NE(test_map2.find(iter.GetKey()), test_map2.end()); 372 ASSERT_NE(test_map2.find(iter.GetKey()), test_map2.end());
371 EXPECT_EQ(test_map.at(iter.GetKey()), test_map.at(iter.GetKey())); 373 EXPECT_EQ(test_map.at(iter.GetKey()), test_map.at(iter.GetKey()));
372 } 374 }
373 375
374 for (auto iter = test_map2.cbegin(); iter != test_map2.cend(); ++iter) { 376 for (auto iter = test_map2.cbegin(); iter != test_map2.cend(); ++iter) {
375 ASSERT_NE(test_map.find(iter.GetKey()), test_map.end()); 377 ASSERT_NE(test_map.find(iter.GetKey()), test_map.end());
376 EXPECT_EQ(test_map2.at(iter.GetKey()), test_map2.at(iter.GetKey())); 378 EXPECT_EQ(test_map2.at(iter.GetKey()), test_map2.at(iter.GetKey()));
377 } 379 }
378 } 380 }
379 381
382 // Test serialization/deserialization of a map with null elements.
383 TEST_F(MapTest, Serialization_MapOfNullableStructs) {
384 ArrayValidateParams validate_nullable(2, true, nullptr);
385 ArrayValidateParams validate_non_nullable(2, false, nullptr);
386
387 Map<uint32_t, RectPtr> map;
388 map[0] = RectPtr();
389 map[1] = Rect::New();
390 map[1]->x = 1;
391 map[1]->y = 2;
392 map[1]->width = 3;
393 map[1]->height = 4;
394 EXPECT_TRUE(map[0].is_null());
395 EXPECT_TRUE(!map[1].is_null());
396
397 size_t size = GetSerializedSize_(map);
398 EXPECT_EQ(8u + // map header
399 (8u + 8u) + // pointers to keys and values array
400 (8u + 2 * 4u) + // keys array data
401 (8u + // values array data
402 (8u) + // 1 null value
403 (8u + 8U + 4 * 4U)), // 1 Rect value
404 size);
405
406 // 1. Should not be able to serialize null elements.
407 {
408 FixedBufferForTesting buf(size);
409 Map_Data<int32_t, Rect::Data_*>* data = nullptr;
410 EXPECT_EQ(ValidationError::UNEXPECTED_NULL_POINTER,
411 SerializeMap_(&map, &buf, &data, &validate_non_nullable));
412 }
413
414 // 2. Successfully serialize null elements.
415 FixedBufferForTesting buf(size);
416 Map_Data<int32_t, Rect::Data_*>* data = nullptr;
417 EXPECT_EQ(ValidationError::NONE,
418 SerializeMap_(&map, &buf, &data, &validate_nullable));
419 EXPECT_NE(nullptr, data);
420
421 // 3. Deserialize deserialize null elements.
422 Map<uint32_t, RectPtr> map2;
423 EXPECT_EQ(0u, map2.size());
424 EXPECT_TRUE(map2.is_null());
425 Deserialize_(data, &map2);
426 EXPECT_EQ(2u, map2.size());
427 EXPECT_FALSE(map2.is_null());
428 EXPECT_TRUE(map2[0].is_null());
429 EXPECT_FALSE(map2[1].is_null());
430 EXPECT_EQ(1, map2[1]->x);
431 EXPECT_EQ(2, map2[1]->y);
432 EXPECT_EQ(3, map2[1]->width);
433 EXPECT_EQ(4, map2[1]->height);
434 }
435
380 } // namespace 436 } // namespace
381 } // namespace test 437 } // namespace test
382 } // namespace mojo 438 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698