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

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

Issue 1387993002: mojo::Serialize*_() calls now propogate/return validation errors. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Fixed MOJO_DCHECK issues, removed SerializationWarningObserver & addressing trung's CL comments Created 5 years, 2 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 <string.h> 5 #include <string.h>
6 6
7 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" 7 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
8 #include "mojo/public/cpp/bindings/lib/validation_errors.h"
8 #include "mojo/public/cpp/environment/environment.h" 9 #include "mojo/public/cpp/environment/environment.h"
9 #include "mojo/public/cpp/system/message_pipe.h" 10 #include "mojo/public/cpp/system/message_pipe.h"
10 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" 11 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h"
11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
12 13
13 namespace mojo { 14 namespace mojo {
14 namespace test { 15 namespace test {
15 namespace { 16 namespace {
16 17
17 RectPtr MakeRect(int32_t factor = 1) { 18 RectPtr MakeRect(int32_t factor = 1) {
(...skipping 29 matching lines...) Expand all
47 } 48 }
48 49
49 template <typename U, typename T> 50 template <typename U, typename T>
50 U SerializeAndDeserialize(T input) { 51 U SerializeAndDeserialize(T input) {
51 typedef typename mojo::internal::WrapperTraits<T>::DataType InputDataType; 52 typedef typename mojo::internal::WrapperTraits<T>::DataType InputDataType;
52 typedef typename mojo::internal::WrapperTraits<U>::DataType OutputDataType; 53 typedef typename mojo::internal::WrapperTraits<U>::DataType OutputDataType;
53 54
54 size_t size = GetSerializedSize_(*input); 55 size_t size = GetSerializedSize_(*input);
55 mojo::internal::FixedBufferForTesting buf(size + 32); 56 mojo::internal::FixedBufferForTesting buf(size + 32);
56 InputDataType data; 57 InputDataType data;
57 Serialize_(input.get(), &buf, &data); 58 EXPECT_EQ(mojo::internal::VALIDATION_ERROR_NONE,
59 Serialize_(input.get(), &buf, &data));
58 60
59 std::vector<Handle> handles; 61 std::vector<Handle> handles;
60 data->EncodePointersAndHandles(&handles); 62 data->EncodePointersAndHandles(&handles);
61 63
62 // Set the subsequent area to a special value, so that we can find out if we 64 // Set the subsequent area to a special value, so that we can find out if we
63 // mistakenly access the area. 65 // mistakenly access the area.
64 void* subsequent_area = buf.Allocate(32); 66 void* subsequent_area = buf.Allocate(32);
65 memset(subsequent_area, 0xAA, 32); 67 memset(subsequent_area, 0xAA, 32);
66 68
67 OutputDataType output_data = reinterpret_cast<OutputDataType>(data); 69 OutputDataType output_data = reinterpret_cast<OutputDataType>(data);
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 139
138 // Serialization test of a struct with no pointer or handle members. 140 // Serialization test of a struct with no pointer or handle members.
139 TEST_F(StructTest, Serialization_Basic) { 141 TEST_F(StructTest, Serialization_Basic) {
140 RectPtr rect(MakeRect()); 142 RectPtr rect(MakeRect());
141 143
142 size_t size = GetSerializedSize_(*rect); 144 size_t size = GetSerializedSize_(*rect);
143 EXPECT_EQ(8U + 16U, size); 145 EXPECT_EQ(8U + 16U, size);
144 146
145 mojo::internal::FixedBufferForTesting buf(size); 147 mojo::internal::FixedBufferForTesting buf(size);
146 internal::Rect_Data* data; 148 internal::Rect_Data* data;
147 Serialize_(rect.get(), &buf, &data); 149 EXPECT_EQ(mojo::internal::VALIDATION_ERROR_NONE,
150 Serialize_(rect.get(), &buf, &data));
148 151
149 RectPtr rect2(Rect::New()); 152 RectPtr rect2(Rect::New());
150 Deserialize_(data, rect2.get()); 153 Deserialize_(data, rect2.get());
151 154
152 CheckRect(*rect2); 155 CheckRect(*rect2);
153 } 156 }
154 157
155 // Construction of a struct with struct pointers from null. 158 // Construction of a struct with struct pointers from null.
156 TEST_F(StructTest, Construction_StructPointers) { 159 TEST_F(StructTest, Construction_StructPointers) {
157 RectPairPtr pair; 160 RectPairPtr pair;
(...skipping 12 matching lines...) Expand all
170 TEST_F(StructTest, Serialization_StructPointers) { 173 TEST_F(StructTest, Serialization_StructPointers) {
171 RectPairPtr pair(RectPair::New()); 174 RectPairPtr pair(RectPair::New());
172 pair->first = MakeRect(); 175 pair->first = MakeRect();
173 pair->second = MakeRect(); 176 pair->second = MakeRect();
174 177
175 size_t size = GetSerializedSize_(*pair); 178 size_t size = GetSerializedSize_(*pair);
176 EXPECT_EQ(8U + 16U + 2 * (8U + 16U), size); 179 EXPECT_EQ(8U + 16U + 2 * (8U + 16U), size);
177 180
178 mojo::internal::FixedBufferForTesting buf(size); 181 mojo::internal::FixedBufferForTesting buf(size);
179 internal::RectPair_Data* data; 182 internal::RectPair_Data* data;
180 Serialize_(pair.get(), &buf, &data); 183 EXPECT_EQ(mojo::internal::VALIDATION_ERROR_NONE,
184 Serialize_(pair.get(), &buf, &data));
181 185
182 RectPairPtr pair2(RectPair::New()); 186 RectPairPtr pair2(RectPair::New());
183 Deserialize_(data, pair2.get()); 187 Deserialize_(data, pair2.get());
184 188
185 CheckRect(*pair2->first); 189 CheckRect(*pair2->first);
186 CheckRect(*pair2->second); 190 CheckRect(*pair2->second);
187 } 191 }
188 192
189 // Serialization test of a struct with an array member. 193 // Serialization test of a struct with an array member.
190 TEST_F(StructTest, Serialization_ArrayPointers) { 194 TEST_F(StructTest, Serialization_ArrayPointers) {
(...skipping 10 matching lines...) Expand all
201 8U + // name header 205 8U + // name header
202 8U + // name payload (rounded up) 206 8U + // name payload (rounded up)
203 8U + // rects header 207 8U + // rects header
204 4 * 8U + // rects payload (four pointers) 208 4 * 8U + // rects payload (four pointers)
205 4 * (8U + // rect header 209 4 * (8U + // rect header
206 16U), // rect payload (four ints) 210 16U), // rect payload (four ints)
207 size); 211 size);
208 212
209 mojo::internal::FixedBufferForTesting buf(size); 213 mojo::internal::FixedBufferForTesting buf(size);
210 internal::NamedRegion_Data* data; 214 internal::NamedRegion_Data* data;
211 Serialize_(region.get(), &buf, &data); 215 EXPECT_EQ(mojo::internal::VALIDATION_ERROR_NONE,
216 Serialize_(region.get(), &buf, &data));
212 217
213 NamedRegionPtr region2(NamedRegion::New()); 218 NamedRegionPtr region2(NamedRegion::New());
214 Deserialize_(data, region2.get()); 219 Deserialize_(data, region2.get());
215 220
216 EXPECT_EQ(String("region"), region2->name); 221 EXPECT_EQ(String("region"), region2->name);
217 222
218 EXPECT_EQ(4U, region2->rects.size()); 223 EXPECT_EQ(4U, region2->rects.size());
219 for (size_t i = 0; i < region2->rects.size(); ++i) 224 for (size_t i = 0; i < region2->rects.size(); ++i)
220 CheckRect(*region2->rects[i], static_cast<int32_t>(i) + 1); 225 CheckRect(*region2->rects[i], static_cast<int32_t>(i) + 1);
221 } 226 }
222 227
223 // Serialization test of a struct with null array pointers. 228 // Serialization test of a struct with null array pointers.
224 TEST_F(StructTest, Serialization_NullArrayPointers) { 229 TEST_F(StructTest, Serialization_NullArrayPointers) {
225 NamedRegionPtr region(NamedRegion::New()); 230 NamedRegionPtr region(NamedRegion::New());
226 EXPECT_TRUE(region->name.is_null()); 231 EXPECT_TRUE(region->name.is_null());
227 EXPECT_TRUE(region->rects.is_null()); 232 EXPECT_TRUE(region->rects.is_null());
228 233
229 size_t size = GetSerializedSize_(*region); 234 size_t size = GetSerializedSize_(*region);
230 EXPECT_EQ(8U + // header 235 EXPECT_EQ(8U + // header
231 8U + // name pointer 236 8U + // name pointer
232 8U, // rects pointer 237 8U, // rects pointer
233 size); 238 size);
234 239
235 mojo::internal::FixedBufferForTesting buf(size); 240 mojo::internal::FixedBufferForTesting buf(size);
236 internal::NamedRegion_Data* data; 241 internal::NamedRegion_Data* data;
237 Serialize_(region.get(), &buf, &data); 242 EXPECT_EQ(mojo::internal::VALIDATION_ERROR_NONE,
243 Serialize_(region.get(), &buf, &data));
238 244
239 NamedRegionPtr region2(NamedRegion::New()); 245 NamedRegionPtr region2(NamedRegion::New());
240 Deserialize_(data, region2.get()); 246 Deserialize_(data, region2.get());
241 247
242 EXPECT_TRUE(region2->name.is_null()); 248 EXPECT_TRUE(region2->name.is_null());
243 EXPECT_TRUE(region2->rects.is_null()); 249 EXPECT_TRUE(region2->rects.is_null());
244 } 250 }
245 251
246 // Tests deserializing structs as a newer version. 252 // Tests deserializing structs as a newer version.
247 TEST_F(StructTest, Versioning_OldToNew) { 253 TEST_F(StructTest, Versioning_OldToNew) {
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 expected_output->f_int32 = 123; 420 expected_output->f_int32 = 123;
415 421
416 MultiVersionStructV0Ptr output = 422 MultiVersionStructV0Ptr output =
417 SerializeAndDeserialize<MultiVersionStructV0Ptr>(input.Pass()); 423 SerializeAndDeserialize<MultiVersionStructV0Ptr>(input.Pass());
418 EXPECT_TRUE(output); 424 EXPECT_TRUE(output);
419 EXPECT_TRUE(output->Equals(*expected_output)); 425 EXPECT_TRUE(output->Equals(*expected_output));
420 } 426 }
421 } 427 }
422 } // namespace test 428 } // namespace test
423 } // namespace mojo 429 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/tests/serialization_warning_unittest.cc ('k') | mojo/public/cpp/bindings/tests/union_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698