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

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

Issue 923033003: Implement unions as members of structs. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 10 months 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/lib/bounds_checker.h" 5 #include "mojo/public/cpp/bindings/lib/bounds_checker.h"
6 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" 6 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
7 #include "mojo/public/cpp/bindings/string.h" 7 #include "mojo/public/cpp/bindings/string.h"
8 #include "mojo/public/cpp/environment/environment.h" 8 #include "mojo/public/cpp/environment/environment.h"
9 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom.h" 9 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 } 101 }
102 102
103 TEST(UnionTest, SerializationPod) { 103 TEST(UnionTest, SerializationPod) {
104 PodUnionPtr pod1(PodUnion::New()); 104 PodUnionPtr pod1(PodUnion::New());
105 pod1->set_f_int8(10); 105 pod1->set_f_int8(10);
106 106
107 size_t size = GetSerializedSize_(pod1); 107 size_t size = GetSerializedSize_(pod1);
108 EXPECT_EQ(16U, size); 108 EXPECT_EQ(16U, size);
109 109
110 mojo::internal::FixedBuffer buf(size); 110 mojo::internal::FixedBuffer buf(size);
111 internal::PodUnion_Data* data; 111 internal::PodUnion_Data* data = nullptr;
112 Serialize_(pod1.Pass(), &buf, &data); 112 Serialize_(pod1.Pass(), &buf, &data);
113 113
114 PodUnionPtr pod2; 114 PodUnionPtr pod2;
115 Deserialize_(data, &pod2); 115 Deserialize_(data, &pod2);
116 116
117 EXPECT_EQ(10, pod2->get_f_int8()); 117 EXPECT_EQ(10, pod2->get_f_int8());
118 EXPECT_TRUE(pod2->is_f_int8()); 118 EXPECT_TRUE(pod2->is_f_int8());
119 EXPECT_EQ(pod2->which(), PodUnion::Tag::F_INT8); 119 EXPECT_EQ(pod2->which(), PodUnion::Tag::F_INT8);
120 } 120 }
121 121
122 TEST(UnionTest, ValidationJustWorksPod) { 122 TEST(UnionTest, ValidationJustWorksPod) {
123 PodUnionPtr pod(PodUnion::New()); 123 PodUnionPtr pod(PodUnion::New());
124 pod->set_f_int8(10); 124 pod->set_f_int8(10);
125 125
126 size_t size = GetSerializedSize_(pod); 126 size_t size = GetSerializedSize_(pod);
127 EXPECT_EQ(16U, size); 127 EXPECT_EQ(16U, size);
128 128
129 mojo::internal::FixedBuffer buf(size); 129 mojo::internal::FixedBuffer buf(size);
130 internal::PodUnion_Data* data; 130 internal::PodUnion_Data* data = nullptr;
131 Serialize_(pod.Pass(), &buf, &data); 131 Serialize_(pod.Pass(), &buf, &data);
132 void* raw_buf = buf.Leak(); 132 void* raw_buf = buf.Leak();
133 mojo::internal::BoundsChecker bounds_checker(data, size, 0); 133 mojo::internal::BoundsChecker bounds_checker(data, size, 0);
134 EXPECT_TRUE(internal::PodUnion_Data::Validate(raw_buf, &bounds_checker)); 134 EXPECT_TRUE(internal::PodUnion_Data::Validate(raw_buf, &bounds_checker));
135 free(raw_buf); 135 free(raw_buf);
136 } 136 }
137 137
138 TEST(UnionTest, SerializeNotNull) {
139 PodUnionPtr pod(PodUnion::New());
140 pod->set_f_int8(0);
141 size_t size = GetSerializedSize_(pod);
142 mojo::internal::FixedBuffer buf(size);
143 internal::PodUnion_Data* data = nullptr;
144 Serialize_(pod.Pass(), &buf, &data);
145 EXPECT_FALSE(data->is_null());
146 }
147
138 TEST(UnionTest, NullValidation) { 148 TEST(UnionTest, NullValidation) {
139 void* buf = nullptr; 149 void* buf = nullptr;
140 mojo::internal::BoundsChecker bounds_checker(buf, 0, 0); 150 mojo::internal::BoundsChecker bounds_checker(buf, 0, 0);
141 EXPECT_TRUE(internal::PodUnion_Data::Validate(buf, &bounds_checker)); 151 EXPECT_TRUE(internal::PodUnion_Data::Validate(buf, &bounds_checker));
142 } 152 }
143 153
144 TEST(UnionTest, OutOfAlignmentValidation) { 154 TEST(UnionTest, OutOfAlignmentValidation) {
145 Environment environment; 155 Environment environment;
146 size_t size = sizeof(internal::PodUnion_Data); 156 size_t size = sizeof(internal::PodUnion_Data);
147 // Get an aligned object and shift the alignment. 157 // Get an aligned object and shift the alignment.
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 } 223 }
214 224
215 TEST(UnionTest, StringSerialization) { 225 TEST(UnionTest, StringSerialization) {
216 ObjectUnionPtr pod1(ObjectUnion::New()); 226 ObjectUnionPtr pod1(ObjectUnion::New());
217 227
218 String hello("hello world"); 228 String hello("hello world");
219 pod1->set_f_string(hello); 229 pod1->set_f_string(hello);
220 230
221 size_t size = GetSerializedSize_(pod1); 231 size_t size = GetSerializedSize_(pod1);
222 mojo::internal::FixedBuffer buf(size); 232 mojo::internal::FixedBuffer buf(size);
223 internal::ObjectUnion_Data* data; 233 internal::ObjectUnion_Data* data = nullptr;
224 Serialize_(pod1.Pass(), &buf, &data); 234 Serialize_(pod1.Pass(), &buf, &data);
225 235
226 ObjectUnionPtr pod2; 236 ObjectUnionPtr pod2;
227 Deserialize_(data, &pod2); 237 Deserialize_(data, &pod2);
228 EXPECT_EQ(hello, pod2->get_f_string()); 238 EXPECT_EQ(hello, pod2->get_f_string());
229 EXPECT_TRUE(pod2->is_f_string()); 239 EXPECT_TRUE(pod2->is_f_string());
230 EXPECT_EQ(pod2->which(), ObjectUnion::Tag::F_STRING); 240 EXPECT_EQ(pod2->which(), ObjectUnion::Tag::F_STRING);
231 } 241 }
232 242
233 TEST(UnionTest, StringValidationNull) { 243 TEST(UnionTest, StringValidationNull) {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 char* ptr = reinterpret_cast<char*>(&data->data.f_f_string); 277 char* ptr = reinterpret_cast<char*>(&data->data.f_f_string);
268 mojo::internal::ArrayHeader* array_header = 278 mojo::internal::ArrayHeader* array_header =
269 reinterpret_cast<mojo::internal::ArrayHeader*>(ptr + *ptr); 279 reinterpret_cast<mojo::internal::ArrayHeader*>(ptr + *ptr);
270 array_header->num_bytes = 20; // This should go out of bounds. 280 array_header->num_bytes = 20; // This should go out of bounds.
271 array_header->num_elements = 20; 281 array_header->num_elements = 20;
272 mojo::internal::BoundsChecker bounds_checker(data, 32, 0); 282 mojo::internal::BoundsChecker bounds_checker(data, 32, 0);
273 void* raw_buf = buf.Leak(); 283 void* raw_buf = buf.Leak();
274 EXPECT_FALSE(internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker)); 284 EXPECT_FALSE(internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker));
275 free(raw_buf); 285 free(raw_buf);
276 } 286 }
287
288 TEST(UnionTest, PodUnionInStruct) {
289 SmallStructPtr small_struct(SmallStruct::New());
yzshen1 2015/02/17 19:34:35 Why do we need this test and ObjectUnionInStruct?
azani 2015/02/18 00:27:57 Artefacts of TDD. Gone now.
290 small_struct->pod_union = PodUnion::New();
291 small_struct->pod_union->set_f_int8(10);
292 EXPECT_EQ(10, small_struct->pod_union->get_f_int8());
293 }
294
295 TEST(UnionTest, PodUnionInStructClone) {
296 SmallStructPtr small_struct(SmallStruct::New());
297 small_struct->pod_union = PodUnion::New();
298 small_struct->pod_union->set_f_int8(10);
299
300 SmallStructPtr clone = small_struct.Clone();
301 EXPECT_EQ(10, clone->pod_union->get_f_int8());
302 }
303
304 TEST(UnionTest, ObjectUnionInStruct) {
305 SmallObjStructPtr small_struct(SmallObjStruct::New());
306 String hello("hello world");
307 small_struct->obj_union = ObjectUnion::New();
308 small_struct->obj_union->set_f_string(hello);
309 EXPECT_EQ(hello, small_struct->obj_union->get_f_string());
310 }
311
312 TEST(UnionTest, PodUnionInStructSerialization) {
313 SmallStructPtr small_struct(SmallStruct::New());
314 small_struct->pod_union = PodUnion::New();
315 small_struct->pod_union->set_f_int32(10);
316
317 size_t size = GetSerializedSize_(small_struct);
318
319 mojo::internal::FixedBuffer buf(size);
320 internal::SmallStruct_Data* data = nullptr;
321 Serialize_(small_struct.Pass(), &buf, &data);
322
323 SmallStructPtr deserialized;
324 Deserialize_(data, &deserialized);
325
326 EXPECT_EQ(10, deserialized->pod_union->get_f_int32());
327 }
328
329 TEST(UnionTest, ObjectUnionInStructSerialization) {
330 SmallObjStructPtr obj_struct(SmallObjStruct::New());
331 obj_struct->obj_union = ObjectUnion::New();
332 String hello("hello world");
333 obj_struct->obj_union->set_f_string(hello);
334
335 size_t size = GetSerializedSize_(obj_struct);
336
337 mojo::internal::FixedBuffer buf(size);
338 internal::SmallObjStruct_Data* data = nullptr;
339 Serialize_(obj_struct.Pass(), &buf, &data);
340
341 SmallObjStructPtr deserialized;
342 Deserialize_(data, &deserialized);
343
344 EXPECT_EQ(hello, deserialized->obj_union->get_f_string());
345 }
346
347 TEST(UnionTest, ValidationJustWorksForUnionsInStructs) {
yzshen1 2015/02/17 19:34:35 Nit: maybe UnionsInStructsValidation?
azani 2015/02/18 00:27:57 Done.
348 SmallStructPtr small_struct(SmallStruct::New());
349 small_struct->pod_union = PodUnion::New();
350 small_struct->pod_union->set_f_int32(10);
351
352 size_t size = GetSerializedSize_(small_struct);
353
354 mojo::internal::FixedBuffer buf(size);
355 internal::SmallStruct_Data* data = nullptr;
356 Serialize_(small_struct.Pass(), &buf, &data);
357
358 void* raw_buf = buf.Leak();
359 mojo::internal::BoundsChecker bounds_checker(data, size, 0);
360 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker));
361 free(raw_buf);
362 }
363
364 TEST(UnionTest, PodUnionInStructValidationFails) {
yzshen1 2015/02/17 19:34:35 Maybe use the same style across multiple test name
azani 2015/02/18 00:27:57 Done.
365 Environment environment;
366 SmallStructPtr small_struct(SmallStruct::New());
367 small_struct->pod_union = PodUnion::New();
368 small_struct->pod_union->set_f_int32(10);
369
370 size_t size = GetSerializedSize_(small_struct);
371
372 mojo::internal::FixedBuffer buf(size);
373 internal::SmallStruct_Data* data = nullptr;
374 Serialize_(small_struct.Pass(), &buf, &data);
375 data->pod_union.tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(100);
376
377 void* raw_buf = buf.Leak();
378 mojo::internal::BoundsChecker bounds_checker(data, size, 0);
379 EXPECT_FALSE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker));
380 free(raw_buf);
381 }
382
383 TEST(UnionTest, ValidationNullUnionFailure) {
384 Environment environment;
385 SmallStructPtr small_struct(SmallStruct::New());
386
387 size_t size = GetSerializedSize_(small_struct);
388
389 mojo::internal::FixedBuffer buf(size);
390 internal::SmallStruct_Data* data = nullptr;
391 Serialize_(small_struct.Pass(), &buf, &data);
392
393 void* raw_buf = buf.Leak();
394 mojo::internal::BoundsChecker bounds_checker(data, size, 0);
395 EXPECT_FALSE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker));
396 free(raw_buf);
397 }
398
399 TEST(UnionTest, ValidationNullbleUnion) {
400 Environment environment;
401 SmallStructNullableUnionPtr small_struct(SmallStructNullableUnion::New());
402
403 size_t size = GetSerializedSize_(small_struct);
404
405 mojo::internal::FixedBuffer buf(size);
406 internal::SmallStructNullableUnion_Data* data = nullptr;
407 Serialize_(small_struct.Pass(), &buf, &data);
408
409 void* raw_buf = buf.Leak();
410 mojo::internal::BoundsChecker bounds_checker(data, size, 0);
411 EXPECT_TRUE(internal::SmallStructNullableUnion_Data::Validate(
412 raw_buf, &bounds_checker));
413 free(raw_buf);
414 }
277 } // namespace test 415 } // namespace test
278 } // namespace mojo 416 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698