OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // TODO(vardhan): Needs a lot more testing. | |
6 | |
7 #include <mojo/bindings/struct.h> | |
8 | |
9 #include <mojo/bindings/array.h> | |
10 #include <mojo/bindings/internal/util.h> | |
11 #include <string.h> | |
12 | |
13 #include "mojo/public/c/bindings/tests/testing_util.h" | |
14 #include "mojo/public/cpp/system/macros.h" | |
15 #include "mojo/public/interfaces/bindings/tests/rect.mojom-c.h" | |
16 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom-c.h" | |
17 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom-c.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 namespace { | |
21 | |
22 #define BYTES_LEFT_AFTER_FIELD(type, field) \ | |
23 (sizeof(type) - offsetof(type, field)) | |
24 | |
25 struct mojo_test_Rect* MakeRect(struct MojomBuffer* buf) { | |
26 struct mojo_test_Rect* r = static_cast<struct mojo_test_Rect*>( | |
27 MojomBuffer_Allocate(buf, sizeof(struct mojo_test_Rect))); | |
28 *r = mojo_test_Rect{// header | |
29 { | |
30 sizeof(struct mojo_test_Rect), 0, | |
31 }, | |
32 0u, | |
33 0u, | |
34 0u, | |
35 0u}; | |
36 return r; | |
37 } | |
38 | |
39 // TODO(vardhan): Move this into string.h/c if it proves useful again. | |
40 struct MojomStringHeader* MakeMojomStringFromCString(MojomBuffer* buf, | |
41 const char* chars, | |
42 size_t num_chars) { | |
43 struct MojomArrayHeader* arr = MojomArray_New(buf, num_chars, 1); | |
44 assert(NULL != arr); | |
45 | |
46 memcpy((char*)arr + sizeof(MojomStringHeader), chars, num_chars); | |
47 return (struct MojomStringHeader*)arr; | |
48 } | |
49 | |
50 TEST(StructSerializedSizeTest, Basic) { | |
51 struct mojo_test_Rect rect = {{ | |
52 sizeof(struct mojo_test_Rect), 0, | |
53 }, | |
54 0u, | |
55 0u, | |
56 0u, | |
57 0u}; | |
58 size_t size = mojo_test_Rect_ComputeSerializedSize(&rect); | |
59 EXPECT_EQ(8U + 16U, size); | |
60 | |
61 char buffer_bytes[1000]; | |
62 struct MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0}; | |
63 CopyAndCompare(&buf, &rect, sizeof(rect), mojo_test_Rect_DeepCopy, | |
64 mojo_test_Rect_EncodePointersAndHandles, | |
65 mojo_test_Rect_DecodePointersAndHandles); | |
66 } | |
67 | |
68 TEST(StructSerializationTest, StructOfStructs) { | |
69 char buffer_bytes[1000] = {0}; | |
70 struct MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0}; | |
71 | |
72 struct mojo_test_RectPair* pair = static_cast<struct mojo_test_RectPair*>( | |
73 MojomBuffer_Allocate(&buf, sizeof(struct mojo_test_RectPair))); | |
74 *pair = mojo_test_RectPair{ | |
75 {sizeof(struct mojo_test_RectPair), 0}, | |
76 {MakeRect(&buf)}, // first | |
77 {MakeRect(&buf)}, // second | |
78 }; | |
79 | |
80 EXPECT_EQ(8U + 16U + 2 * (8U + 16U), | |
81 mojo_test_RectPair_ComputeSerializedSize(pair)); | |
82 | |
83 // We save the underlying (unencoded) buffer. We can compare the two after | |
84 // deserialization to make sure deserialization is correct. | |
85 char buffer_bytes_copy[sizeof(buffer_bytes)]; | |
86 memcpy(buffer_bytes_copy, buffer_bytes, sizeof(buffer_bytes_copy)); | |
87 | |
88 mojo_test_RectPair_EncodePointersAndHandles(pair, buf.num_bytes_used, NULL); | |
89 | |
90 EXPECT_EQ(BYTES_LEFT_AFTER_FIELD(struct mojo_test_RectPair, first), | |
91 pair->first.offset); | |
92 EXPECT_EQ(BYTES_LEFT_AFTER_FIELD(struct mojo_test_RectPair, second) + | |
93 sizeof(struct mojo_test_Rect), | |
94 pair->second.offset); | |
95 | |
96 mojo_test_RectPair_DecodePointersAndHandles( | |
97 reinterpret_cast<struct mojo_test_RectPair*>(buf.buf), buf.num_bytes_used, | |
98 NULL, 0); | |
99 EXPECT_EQ(0, memcmp(buf.buf, buffer_bytes_copy, buf.num_bytes_used)); | |
100 | |
101 { | |
102 char buffer_bytes2[1000] = {0}; | |
103 struct MojomBuffer buf2 = {buffer_bytes2, sizeof(buffer_bytes2), 0}; | |
104 CopyAndCompare(&buf2, pair, buf.num_bytes_used, mojo_test_RectPair_DeepCopy, | |
105 mojo_test_RectPair_EncodePointersAndHandles, | |
106 mojo_test_RectPair_DecodePointersAndHandles); | |
107 } | |
108 } | |
109 | |
110 // Tests a struct that has: | |
111 // - nullable string which isn't null. | |
112 // - nullable array of rects, which isn't null. | |
113 TEST(StructSerializationTest, StructOfArrays) { | |
114 char buffer_bytes[1000]; | |
115 MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0}; | |
116 | |
117 const char* kRegionName = "region"; | |
118 | |
119 struct mojo_test_NamedRegion* named_region = | |
120 static_cast<struct mojo_test_NamedRegion*>( | |
121 MojomBuffer_Allocate(&buf, sizeof(struct mojo_test_NamedRegion))); | |
122 *named_region = mojo_test_NamedRegion{ | |
123 // header | |
124 { | |
125 sizeof(struct mojo_test_NamedRegion), 0, | |
126 }, | |
127 {NULL}, | |
128 {NULL}, | |
129 }; | |
130 named_region->name.ptr = | |
131 MakeMojomStringFromCString(&buf, kRegionName, strlen(kRegionName)); | |
132 // We save a pointer to |rects| so we can use it after it has been encoded | |
133 // within |named_region|. | |
134 auto rects = MojomArray_New(&buf, 4, sizeof(union mojo_test_RectPtr)); | |
135 named_region->rects.ptr = rects; | |
136 ASSERT_TRUE(named_region->rects.ptr != NULL); | |
137 | |
138 MOJOM_ARRAY_INDEX(named_region->rects.ptr, union mojo_test_RectPtr, 0) | |
139 ->ptr = MakeRect(&buf); | |
140 MOJOM_ARRAY_INDEX(named_region->rects.ptr, union mojo_test_RectPtr, 1) | |
141 ->ptr = MakeRect(&buf); | |
142 MOJOM_ARRAY_INDEX(named_region->rects.ptr, union mojo_test_RectPtr, 2) | |
143 ->ptr = MakeRect(&buf); | |
144 MOJOM_ARRAY_INDEX(named_region->rects.ptr, union mojo_test_RectPtr, 3) | |
145 ->ptr = MakeRect(&buf); | |
146 | |
147 size_t size = mojo_test_NamedRegion_ComputeSerializedSize(named_region); | |
148 EXPECT_EQ(8U + // header | |
149 8U + // name pointer | |
150 8U + // rects pointer | |
151 8U + // name header | |
152 8U + // name payload (rounded up) | |
153 8U + // rects header | |
154 4 * 8U + // rects payload (four pointers) | |
155 4 * (8U + // rect header | |
156 16U), // rect payload (four ints) | |
157 size); | |
158 | |
159 char buffer_bytes_copy[sizeof(buffer_bytes)] = {0}; | |
160 memcpy(buffer_bytes_copy, buffer_bytes, sizeof(buffer_bytes_copy)); | |
161 | |
162 mojo_test_NamedRegion_EncodePointersAndHandles(named_region, | |
163 buf.num_bytes_used, NULL); | |
164 | |
165 EXPECT_EQ(BYTES_LEFT_AFTER_FIELD(struct mojo_test_NamedRegion, name), | |
166 named_region->name.offset); | |
167 EXPECT_EQ(BYTES_LEFT_AFTER_FIELD(struct mojo_test_NamedRegion, rects) + | |
168 MOJOM_INTERNAL_ROUND_TO_8(sizeof(struct MojomStringHeader) + | |
169 strlen(kRegionName)), | |
170 named_region->rects.offset); | |
171 | |
172 // Test the offsets encoded inside the rect array. | |
173 EXPECT_EQ(sizeof(union mojo_test_RectPtr) * 4, | |
174 MOJOM_ARRAY_INDEX(rects, union mojo_test_RectPtr, 0)->offset); | |
175 EXPECT_EQ(sizeof(union mojo_test_RectPtr) * 3 + sizeof(struct mojo_test_Rect), | |
176 MOJOM_ARRAY_INDEX(rects, union mojo_test_RectPtr, 1)->offset); | |
177 EXPECT_EQ( | |
178 sizeof(union mojo_test_RectPtr) * 2 + sizeof(struct mojo_test_Rect) * 2, | |
179 MOJOM_ARRAY_INDEX(rects, union mojo_test_RectPtr, 2)->offset); | |
180 EXPECT_EQ(sizeof(union mojo_test_RectPtr) + sizeof(struct mojo_test_Rect) * 3, | |
181 MOJOM_ARRAY_INDEX(rects, union mojo_test_RectPtr, 3)->offset); | |
182 | |
183 mojo_test_NamedRegion_DecodePointersAndHandles( | |
184 reinterpret_cast<struct mojo_test_NamedRegion*>(buf.buf), | |
185 buf.num_bytes_used, NULL, 0); | |
186 EXPECT_EQ(0, memcmp(buf.buf, buffer_bytes_copy, buf.num_bytes_used)); | |
187 | |
188 { | |
189 char buffer_bytes2[sizeof(buffer_bytes)] = {0}; | |
190 struct MojomBuffer buf2 = {buffer_bytes2, sizeof(buffer_bytes2), 0}; | |
191 CopyAndCompare(&buf2, named_region, buf.num_bytes_used, | |
192 mojo_test_NamedRegion_DeepCopy, | |
193 mojo_test_NamedRegion_EncodePointersAndHandles, | |
194 mojo_test_NamedRegion_DecodePointersAndHandles); | |
195 } | |
196 } | |
197 | |
198 // Tests a struct that has: | |
199 // - nullable string which is null. | |
200 // - nullable array of rects, which is null. | |
201 TEST(StructSerializationTest, StructOfNullArrays) { | |
202 struct mojo_test_NamedRegion named_region = { | |
203 // header | |
204 { | |
205 sizeof(struct mojo_test_NamedRegion), 0, | |
206 }, | |
207 {NULL}, | |
208 {NULL}, | |
209 }; | |
210 struct mojo_test_NamedRegion named_region_copy = named_region; | |
211 | |
212 size_t size = mojo_test_NamedRegion_ComputeSerializedSize(&named_region); | |
213 EXPECT_EQ(8U + // header | |
214 8U + // name pointer | |
215 8U, // rects pointer | |
216 size); | |
217 | |
218 mojo_test_NamedRegion_EncodePointersAndHandles( | |
219 &named_region, sizeof(struct mojo_test_NamedRegion), NULL); | |
220 EXPECT_EQ(0u, named_region.name.offset); | |
221 EXPECT_EQ(0u, named_region.rects.offset); | |
222 | |
223 mojo_test_NamedRegion_DecodePointersAndHandles( | |
224 &named_region, sizeof(struct mojo_test_NamedRegion), NULL, 0); | |
225 EXPECT_EQ(0, memcmp(&named_region, &named_region_copy, | |
226 sizeof(struct mojo_test_NamedRegion))); | |
227 | |
228 { | |
229 char buffer_bytes2[sizeof(named_region)] = {0}; | |
230 struct MojomBuffer buf2 = {buffer_bytes2, sizeof(buffer_bytes2), 0}; | |
231 CopyAndCompare(&buf2, &named_region, sizeof(named_region), | |
232 mojo_test_NamedRegion_DeepCopy, | |
233 mojo_test_NamedRegion_EncodePointersAndHandles, | |
234 mojo_test_NamedRegion_DecodePointersAndHandles); | |
235 } | |
236 } | |
237 | |
238 TEST(StructSerializationTest, StructOfUnion) { | |
239 struct mojo_test_SmallStructNonNullableUnion u = { | |
240 // header | |
241 { | |
242 sizeof(struct mojo_test_SmallStructNonNullableUnion), | |
243 0, // version | |
244 }, | |
245 // PodUnion | |
246 { | |
247 16ul, // size | |
248 mojo_test_PodUnion_Tag_f_int8, // tag, | |
249 {0}, // data | |
250 }, | |
251 }; | |
252 | |
253 struct mojo_test_SmallStructNonNullableUnion u_null = { | |
254 // header | |
255 { | |
256 sizeof(struct mojo_test_SmallStructNonNullableUnion), | |
257 0, // version | |
258 }, | |
259 // PodUnion | |
260 { | |
261 0ul, // size | |
262 mojo_test_PodUnion_Tag__UNKNOWN__, // tag, | |
263 {0}, // data | |
264 }, | |
265 }; | |
266 | |
267 EXPECT_EQ(8U + // header | |
268 16U, // union | |
269 mojo_test_SmallStructNonNullableUnion_ComputeSerializedSize(&u)); | |
270 EXPECT_EQ( | |
271 8U + // header | |
272 16U, // union | |
273 mojo_test_SmallStructNonNullableUnion_ComputeSerializedSize(&u_null)); | |
274 | |
275 // Encoding shouldn't have done anything to these structs (they have no | |
276 // pointers or handles): | |
277 struct mojo_test_SmallStructNonNullableUnion u_copy = u; | |
278 mojo_test_SmallStructNonNullableUnion_EncodePointersAndHandles( | |
279 &u, sizeof(struct mojo_test_SmallStructNonNullableUnion), NULL); | |
280 EXPECT_EQ(0, memcmp(&u, &u_copy, sizeof(u))); | |
281 | |
282 // Similarly, decoding shouldn't change the struct at all: | |
283 mojo_test_SmallStructNonNullableUnion_DecodePointersAndHandles( | |
284 &u, sizeof(struct mojo_test_SmallStructNonNullableUnion), NULL, 0); | |
285 EXPECT_EQ(0, memcmp(&u, &u_copy, sizeof(u))); | |
286 | |
287 struct mojo_test_SmallStructNonNullableUnion u_null_copy = u_null; | |
288 mojo_test_SmallStructNonNullableUnion_EncodePointersAndHandles( | |
289 &u_null, sizeof(struct mojo_test_SmallStructNonNullableUnion), NULL); | |
290 EXPECT_EQ(0, memcmp(&u_null, &u_null_copy, sizeof(u_null))); | |
291 | |
292 mojo_test_SmallStructNonNullableUnion_DecodePointersAndHandles( | |
293 &u_null, sizeof(struct mojo_test_SmallStructNonNullableUnion), NULL, 0); | |
294 EXPECT_EQ(0, memcmp(&u_null, &u_null_copy, sizeof(u_null))); | |
295 | |
296 { | |
297 char buffer_bytes[sizeof(u)] = {0}; | |
298 struct MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0}; | |
299 CopyAndCompare( | |
300 &buf, &u, sizeof(u), mojo_test_SmallStructNonNullableUnion_DeepCopy, | |
301 mojo_test_SmallStructNonNullableUnion_EncodePointersAndHandles, | |
302 mojo_test_SmallStructNonNullableUnion_DecodePointersAndHandles); | |
303 } | |
304 } | |
305 | |
306 TEST(StructSerializationTest, StructWithHandle) { | |
307 struct mojo_test_NullableHandleStruct handle_struct = | |
308 mojo_test_NullableHandleStruct{ | |
309 // header | |
310 { | |
311 sizeof(struct mojo_test_NullableHandleStruct), 0, | |
312 }, | |
313 MOJO_HANDLE_INVALID, | |
314 13, | |
315 }; | |
316 | |
317 EXPECT_EQ( | |
318 sizeof(struct mojo_test_NullableHandleStruct), | |
319 mojo_test_NullableHandleStruct_ComputeSerializedSize(&handle_struct)); | |
320 | |
321 MojoHandle handles[1]; | |
322 struct MojomHandleBuffer handle_buf = {handles, MOJO_ARRAYSIZE(handles), 0u}; | |
323 mojo_test_NullableHandleStruct_EncodePointersAndHandles( | |
324 &handle_struct, sizeof(struct mojo_test_NullableHandleStruct), | |
325 &handle_buf); | |
326 EXPECT_EQ(0u, handle_buf.num_handles_used); | |
327 EXPECT_EQ(static_cast<MojoHandle>(-1), handle_struct.h); | |
328 | |
329 mojo_test_NullableHandleStruct_DecodePointersAndHandles( | |
330 &handle_struct, sizeof(struct mojo_test_NullableHandleStruct), handles, | |
331 MOJO_ARRAYSIZE(handles)); | |
332 EXPECT_EQ(MOJO_HANDLE_INVALID, handle_struct.h); | |
333 | |
334 handle_struct.h = 123; | |
335 mojo_test_NullableHandleStruct_EncodePointersAndHandles( | |
336 &handle_struct, sizeof(struct mojo_test_NullableHandleStruct), | |
337 &handle_buf); | |
338 EXPECT_EQ(1u, handle_buf.num_handles_used); | |
339 EXPECT_EQ(static_cast<MojoHandle>(0), handle_struct.h); | |
340 EXPECT_EQ(static_cast<MojoHandle>(123), handles[0]); | |
341 | |
342 mojo_test_NullableHandleStruct_DecodePointersAndHandles( | |
343 &handle_struct, sizeof(struct mojo_test_NullableHandleStruct), handles, | |
344 MOJO_ARRAYSIZE(handles)); | |
345 EXPECT_EQ(static_cast<MojoHandle>(123), handle_struct.h); | |
346 EXPECT_EQ(MOJO_HANDLE_INVALID, handles[0]); | |
347 | |
348 { | |
349 char buffer_bytes[1000] = {0}; | |
350 struct MojomBuffer buf = {buffer_bytes, 4, 0}; | |
351 auto* copied_struct = | |
352 mojo_test_NullableHandleStruct_DeepCopy(&buf, &handle_struct); | |
353 // Not enough space: | |
354 ASSERT_FALSE(copied_struct); | |
355 buf.buf_size = sizeof(buffer_bytes); | |
356 copied_struct = | |
357 mojo_test_NullableHandleStruct_DeepCopy(&buf, &handle_struct); | |
358 ASSERT_TRUE(copied_struct); | |
359 // The old and the new copy should both have the handles. | |
360 EXPECT_EQ(static_cast<MojoHandle>(123), handle_struct.h); | |
361 EXPECT_EQ(static_cast<MojoHandle>(123), copied_struct->h); | |
362 } | |
363 } | |
364 | |
365 } // namespace | |
OLD | NEW |