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

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, 9 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 SerializeUnion_(pod1.Pass(), &buf, &data, false);
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, PodValidation) {
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 SerializeUnion_(pod.Pass(), &buf, &data, false);
132 void* raw_buf = buf.Leak(); 132 void* raw_buf = buf.Leak();
133 mojo::internal::BoundsChecker bounds_checker(data, 133 mojo::internal::BoundsChecker bounds_checker(data,
134 static_cast<uint32_t>(size), 0); 134 static_cast<uint32_t>(size), 0);
135 EXPECT_TRUE(internal::PodUnion_Data::Validate(raw_buf, &bounds_checker)); 135 EXPECT_TRUE(
136 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false));
136 free(raw_buf); 137 free(raw_buf);
137 } 138 }
138 139
140 TEST(UnionTest, SerializeNotNull) {
141 PodUnionPtr pod(PodUnion::New());
142 pod->set_f_int8(0);
143 size_t size = GetSerializedSize_(pod);
144 mojo::internal::FixedBuffer buf(size);
145 internal::PodUnion_Data* data = nullptr;
146 SerializeUnion_(pod.Pass(), &buf, &data, false);
147 EXPECT_FALSE(data->is_null());
148 }
149
150 TEST(UnionTest, SerializeIsNullInlined) {
151 PodUnionPtr pod;
152 size_t size = GetSerializedSize_(pod);
153 EXPECT_EQ(16U, size);
154 mojo::internal::FixedBuffer buf(size);
155 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf);
156
157 // Check that dirty output buffers are handled correctly by serialization.
158 data->size = 16U;
159 data->tag = PodUnion::Tag::F_UINT16;
160 data->data.f_f_int16 = 20;
161
162 SerializeUnion_(pod.Pass(), &buf, &data, true);
163 EXPECT_TRUE(data->is_null());
164
165 PodUnionPtr pod2;
166 Deserialize_(data, &pod2);
167 EXPECT_TRUE(pod2.is_null());
168 }
169
170 TEST(UnionTest, SerializeIsNullNotInlined) {
171 PodUnionPtr pod;
172 size_t size = GetSerializedSize_(pod);
173 EXPECT_EQ(16U, size);
174 mojo::internal::FixedBuffer buf(size);
175 internal::PodUnion_Data* data = nullptr;
176 SerializeUnion_(pod.Pass(), &buf, &data, false);
177 EXPECT_EQ(nullptr, data);
178 }
179
139 TEST(UnionTest, NullValidation) { 180 TEST(UnionTest, NullValidation) {
140 void* buf = nullptr; 181 void* buf = nullptr;
141 mojo::internal::BoundsChecker bounds_checker(buf, 0, 0); 182 mojo::internal::BoundsChecker bounds_checker(buf, 0, 0);
142 EXPECT_TRUE(internal::PodUnion_Data::Validate(buf, &bounds_checker)); 183 EXPECT_TRUE(internal::PodUnion_Data::Validate(buf, &bounds_checker, false));
143 } 184 }
144 185
145 TEST(UnionTest, OutOfAlignmentValidation) { 186 TEST(UnionTest, OutOfAlignmentValidation) {
146 Environment environment; 187 Environment environment;
147 size_t size = sizeof(internal::PodUnion_Data); 188 size_t size = sizeof(internal::PodUnion_Data);
148 // Get an aligned object and shift the alignment. 189 // Get an aligned object and shift the alignment.
149 mojo::internal::FixedBuffer aligned_buf(size + 1); 190 mojo::internal::FixedBuffer aligned_buf(size + 1);
150 void* raw_buf = aligned_buf.Leak(); 191 void* raw_buf = aligned_buf.Leak();
151 char* buf = reinterpret_cast<char*>(raw_buf) + 1; 192 char* buf = reinterpret_cast<char*>(raw_buf) + 1;
152 193
153 internal::PodUnion_Data* data = 194 internal::PodUnion_Data* data =
154 reinterpret_cast<internal::PodUnion_Data*>(buf); 195 reinterpret_cast<internal::PodUnion_Data*>(buf);
155 mojo::internal::BoundsChecker bounds_checker(data, 196 mojo::internal::BoundsChecker bounds_checker(data,
156 static_cast<uint32_t>(size), 0); 197 static_cast<uint32_t>(size), 0);
157 EXPECT_FALSE(internal::PodUnion_Data::Validate(buf, &bounds_checker)); 198 EXPECT_FALSE(internal::PodUnion_Data::Validate(buf, &bounds_checker, false));
158 free(raw_buf); 199 free(raw_buf);
159 } 200 }
160 201
161 TEST(UnionTest, OOBValidation) { 202 TEST(UnionTest, OOBValidation) {
162 Environment environment; 203 Environment environment;
163 size_t size = sizeof(internal::PodUnion_Data) - 1; 204 size_t size = sizeof(internal::PodUnion_Data) - 1;
164 mojo::internal::FixedBuffer buf(size); 205 mojo::internal::FixedBuffer buf(size);
165 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf); 206 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf);
166 mojo::internal::BoundsChecker bounds_checker(data, 207 mojo::internal::BoundsChecker bounds_checker(data,
167 static_cast<uint32_t>(size), 0); 208 static_cast<uint32_t>(size), 0);
168 void* raw_buf = buf.Leak(); 209 void* raw_buf = buf.Leak();
169 EXPECT_FALSE(internal::PodUnion_Data::Validate(raw_buf, &bounds_checker)); 210 EXPECT_FALSE(
211 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false));
170 free(raw_buf); 212 free(raw_buf);
171 } 213 }
172 214
173 TEST(UnionTest, UnknownTagValidation) { 215 TEST(UnionTest, UnknownTagValidation) {
174 Environment environment; 216 Environment environment;
175 size_t size = sizeof(internal::PodUnion_Data); 217 size_t size = sizeof(internal::PodUnion_Data);
176 mojo::internal::FixedBuffer buf(size); 218 mojo::internal::FixedBuffer buf(size);
177 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf); 219 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf);
178 data->tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(0xFFFFFF); 220 data->tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(0xFFFFFF);
179 mojo::internal::BoundsChecker bounds_checker(data, 221 mojo::internal::BoundsChecker bounds_checker(data,
180 static_cast<uint32_t>(size), 0); 222 static_cast<uint32_t>(size), 0);
181 void* raw_buf = buf.Leak(); 223 void* raw_buf = buf.Leak();
182 EXPECT_FALSE(internal::PodUnion_Data::Validate(raw_buf, &bounds_checker)); 224 EXPECT_FALSE(
225 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false));
183 free(raw_buf); 226 free(raw_buf);
184 } 227 }
185 228
186 TEST(UnionTest, StringGetterSetter) { 229 TEST(UnionTest, StringGetterSetter) {
187 ObjectUnionPtr pod(ObjectUnion::New()); 230 ObjectUnionPtr pod(ObjectUnion::New());
188 231
189 String hello("hello world"); 232 String hello("hello world");
190 pod->set_f_string(hello); 233 pod->set_f_string(hello);
191 EXPECT_EQ(hello, pod->get_f_string()); 234 EXPECT_EQ(hello, pod->get_f_string());
192 EXPECT_TRUE(pod->is_f_string()); 235 EXPECT_TRUE(pod->is_f_string());
(...skipping 24 matching lines...) Expand all
217 } 260 }
218 261
219 TEST(UnionTest, StringSerialization) { 262 TEST(UnionTest, StringSerialization) {
220 ObjectUnionPtr pod1(ObjectUnion::New()); 263 ObjectUnionPtr pod1(ObjectUnion::New());
221 264
222 String hello("hello world"); 265 String hello("hello world");
223 pod1->set_f_string(hello); 266 pod1->set_f_string(hello);
224 267
225 size_t size = GetSerializedSize_(pod1); 268 size_t size = GetSerializedSize_(pod1);
226 mojo::internal::FixedBuffer buf(size); 269 mojo::internal::FixedBuffer buf(size);
227 internal::ObjectUnion_Data* data; 270 internal::ObjectUnion_Data* data = nullptr;
228 Serialize_(pod1.Pass(), &buf, &data); 271 SerializeUnion_(pod1.Pass(), &buf, &data, false);
229 272
230 ObjectUnionPtr pod2; 273 ObjectUnionPtr pod2;
231 Deserialize_(data, &pod2); 274 Deserialize_(data, &pod2);
232 EXPECT_EQ(hello, pod2->get_f_string()); 275 EXPECT_EQ(hello, pod2->get_f_string());
233 EXPECT_TRUE(pod2->is_f_string()); 276 EXPECT_TRUE(pod2->is_f_string());
234 EXPECT_EQ(pod2->which(), ObjectUnion::Tag::F_STRING); 277 EXPECT_EQ(pod2->which(), ObjectUnion::Tag::F_STRING);
235 } 278 }
236 279
237 TEST(UnionTest, StringValidationNull) { 280 TEST(UnionTest, NullStringValidation) {
238 Environment environment; 281 Environment environment;
239 size_t size = sizeof(internal::ObjectUnion_Data); 282 size_t size = sizeof(internal::ObjectUnion_Data);
240 mojo::internal::FixedBuffer buf(size); 283 mojo::internal::FixedBuffer buf(size);
241 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); 284 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf);
242 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; 285 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING;
243 data->data.unknown = 0x0; 286 data->data.unknown = 0x0;
244 mojo::internal::BoundsChecker bounds_checker(data, 287 mojo::internal::BoundsChecker bounds_checker(data,
245 static_cast<uint32_t>(size), 0); 288 static_cast<uint32_t>(size), 0);
246 void* raw_buf = buf.Leak(); 289 void* raw_buf = buf.Leak();
247 EXPECT_FALSE(internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker)); 290 EXPECT_FALSE(
291 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false));
248 free(raw_buf); 292 free(raw_buf);
249 } 293 }
250 294
251 TEST(UnionTest, StringValidationPointerOverflow) { 295 TEST(UnionTest, StringPointerOverflowValidation) {
252 Environment environment; 296 Environment environment;
253 size_t size = sizeof(internal::ObjectUnion_Data); 297 size_t size = sizeof(internal::ObjectUnion_Data);
254 mojo::internal::FixedBuffer buf(size); 298 mojo::internal::FixedBuffer buf(size);
255 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); 299 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf);
256 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; 300 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING;
257 data->data.unknown = 0xFFFFFFFFFFFFFFFF; 301 data->data.unknown = 0xFFFFFFFFFFFFFFFF;
258 mojo::internal::BoundsChecker bounds_checker(data, 302 mojo::internal::BoundsChecker bounds_checker(data,
259 static_cast<uint32_t>(size), 0); 303 static_cast<uint32_t>(size), 0);
260 void* raw_buf = buf.Leak(); 304 void* raw_buf = buf.Leak();
261 EXPECT_FALSE(internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker)); 305 EXPECT_FALSE(
306 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false));
262 free(raw_buf); 307 free(raw_buf);
263 } 308 }
264 309
265 TEST(UnionTest, StringValidationValidateString) { 310 TEST(UnionTest, StringValidateOOB) {
266 Environment environment; 311 Environment environment;
267 size_t size = 32; 312 size_t size = 32;
268 mojo::internal::FixedBuffer buf(size); 313 mojo::internal::FixedBuffer buf(size);
269 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); 314 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf);
270 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; 315 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING;
271 316
272 data->data.f_f_string = 8; 317 data->data.f_f_string = 8;
273 char* ptr = reinterpret_cast<char*>(&data->data.f_f_string); 318 char* ptr = reinterpret_cast<char*>(&data->data.f_f_string);
274 mojo::internal::ArrayHeader* array_header = 319 mojo::internal::ArrayHeader* array_header =
275 reinterpret_cast<mojo::internal::ArrayHeader*>(ptr + *ptr); 320 reinterpret_cast<mojo::internal::ArrayHeader*>(ptr + *ptr);
276 array_header->num_bytes = 20; // This should go out of bounds. 321 array_header->num_bytes = 20; // This should go out of bounds.
277 array_header->num_elements = 20; 322 array_header->num_elements = 20;
278 mojo::internal::BoundsChecker bounds_checker(data, 32, 0); 323 mojo::internal::BoundsChecker bounds_checker(data, 32, 0);
279 void* raw_buf = buf.Leak(); 324 void* raw_buf = buf.Leak();
280 EXPECT_FALSE(internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker)); 325 EXPECT_FALSE(
326 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false));
281 free(raw_buf); 327 free(raw_buf);
282 } 328 }
283 } // namespace test 329 } // namespace test
284 } // namespace mojo 330 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698