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 #ifndef MOJO_PUBLIC_C_TESTS_BINDINGS_TESTING_UTIL_H_ | |
6 #define MOJO_PUBLIC_C_TESTS_BINDINGS_TESTING_UTIL_H_ | |
7 | |
8 #include <mojo/bindings/buffer.h> | |
9 #include <mojo/system/handle.h> | |
10 #include <stddef.h> | |
11 #include <stdint.h> | |
12 #include <string.h> | |
13 | |
14 #include "gtest/gtest.h" | |
15 | |
16 // This will copy the supplied |in_struct| and compare it against the new | |
17 // copy, expecting them to be the same. It compares the encoded version to be | |
18 // sure that they are the same, since a unencoded version will have pointers | |
19 // pointing to separate objects in the two copies, whereas encoded versions will | |
20 // only have relative offsets for pointers. The new copy will remain encoded, | |
21 // and the original will be decoded. | |
22 // This function won't work for structs with handles, and will crash. | |
23 template <typename T> | |
24 void CopyAndCompare( | |
25 MojomBuffer* buf, | |
26 T* in_struct, | |
27 size_t in_struct_size, | |
28 T* (*copy_fn)(struct MojomBuffer* in_buffer, T* in_struct), | |
29 void (*encode_fn)(T* inout_struct, | |
30 uint32_t size, | |
31 struct MojomHandleBuffer* inout_handle_buffer), | |
32 void (*decode_fn)(T* inout_struct, | |
33 uint32_t size, | |
34 MojoHandle* handles, | |
35 uint32_t num_handles)) { | |
36 T* out_struct = copy_fn(buf, in_struct); | |
37 ASSERT_TRUE(out_struct); | |
38 EXPECT_EQ(in_struct_size, buf->num_bytes_used); | |
39 | |
40 encode_fn(in_struct, in_struct_size, NULL); | |
41 encode_fn(out_struct, buf->num_bytes_used, NULL); | |
42 EXPECT_EQ(0, memcmp(in_struct, out_struct, buf->num_bytes_used)); | |
43 | |
44 decode_fn(in_struct, in_struct_size, NULL, 0); | |
45 } | |
46 | |
47 #endif // MOJO_PUBLIC_C_TESTS_BINDINGS_TESTING_UTIL_H_ | |
OLD | NEW |