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

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

Issue 1800753005: C++ bindings: A struct's Deserialize() now does validation before deserializing. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/validation_errors.h" 7 #include "mojo/public/cpp/bindings/lib/validation_errors.h"
8 #include "mojo/public/cpp/system/message_pipe.h" 8 #include "mojo/public/cpp/system/message_pipe.h"
9 #include "mojo/public/interfaces/bindings/tests/rect.mojom.h" 9 #include "mojo/public/interfaces/bindings/tests/rect.mojom.h"
10 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" 10 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h"
(...skipping 19 matching lines...) Expand all
30 // serialization. 30 // serialization.
31 bytes[num_bytes] = 170; 31 bytes[num_bytes] = 170;
32 val->Serialize(bytes.data(), num_bytes); 32 val->Serialize(bytes.data(), num_bytes);
33 EXPECT_EQ(170u, bytes[num_bytes]); 33 EXPECT_EQ(170u, bytes[num_bytes]);
34 34
35 mojo::internal::BoundsChecker bounds_checker(bytes.data(), num_bytes, 0); 35 mojo::internal::BoundsChecker bounds_checker(bytes.data(), num_bytes, 0);
36 auto actual_validation_error = 36 auto actual_validation_error =
37 Type::Data_::Validate(bytes.data(), &bounds_checker, nullptr); 37 Type::Data_::Validate(bytes.data(), &bounds_checker, nullptr);
38 EXPECT_EQ(expected_validation_error, actual_validation_error); 38 EXPECT_EQ(expected_validation_error, actual_validation_error);
39 39
40 Type out_val;
41 auto deserialize_ret = out_val.Deserialize(bytes.data(), bytes.size());
40 if (actual_validation_error == mojo::internal::ValidationError::NONE) { 42 if (actual_validation_error == mojo::internal::ValidationError::NONE) {
41 Type out_val;
42 out_val.Deserialize(bytes.data());
43 EXPECT_TRUE(val->Equals(out_val)); 43 EXPECT_TRUE(val->Equals(out_val));
44 } 44 }
45 EXPECT_EQ(actual_validation_error == mojo::internal::ValidationError::NONE,
46 deserialize_ret);
45 } 47 }
46 48
47 private: 49 private:
48 Environment env_; 50 Environment env_;
49 }; 51 };
50 52
51 TEST_F(StructSerializationAPITest, GetSerializedSize) { 53 TEST_F(StructSerializationAPITest, GetSerializedSize) {
52 Rect rect; 54 Rect rect;
53 // 8 byte struct header 55 // 8 byte struct header
54 // + 16 bytes worth of fields 56 // + 16 bytes worth of fields
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 137
136 // We should be able to Serialize/Deserialize a struct that has a nullable 138 // We should be able to Serialize/Deserialize a struct that has a nullable
137 // handle which is null. 139 // handle which is null.
138 TEST_F(StructSerializationAPITest, NullableHandleSerialization) { 140 TEST_F(StructSerializationAPITest, NullableHandleSerialization) {
139 NullableHandleStruct handle_struct; 141 NullableHandleStruct handle_struct;
140 handle_struct.data = 16; 142 handle_struct.data = 16;
141 SerializeAndDeserialize(&handle_struct, 143 SerializeAndDeserialize(&handle_struct,
142 mojo::internal::ValidationError::NONE); 144 mojo::internal::ValidationError::NONE);
143 } 145 }
144 146
147 TEST_F(StructSerializationAPITest, DeserializationFailure) {
148 void* buf[100];
149 EmptyStruct es;
150
151 // Bounds checker should fail this, since buf_size is too small.
152 EXPECT_FALSE(es.Deserialize(buf, 1));
153
154 es.Serialize(buf, sizeof(buf));
155 EXPECT_TRUE(es.Deserialize(buf, sizeof(buf)));
156
157 // Invalid struct header: this should happen inside
158 // EmptyStruct::Data_::Validate()).
159 es.Serialize(buf, sizeof(buf));
160 EmptyStruct::Data_* es_data = reinterpret_cast<EmptyStruct::Data_*>(buf);
161 es_data->header_.num_bytes = 0;
162 EXPECT_FALSE(es.Deserialize(buf, sizeof(buf)));
163 }
164
145 } // namespace 165 } // namespace
146 } // namespace test 166 } // namespace test
147 } // namespace mojo 167 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698