| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 // TODO(vardhan): Needs a lot more testing. | 5 // TODO(vardhan): Needs a lot more testing. |
| 6 | 6 |
| 7 #include "mojo/public/c/bindings/array.h" | 7 #include "mojo/public/c/bindings/array.h" |
| 8 | 8 |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 | 10 |
| 11 #include "mojo/public/c/bindings/struct.h" | 11 #include "mojo/public/c/bindings/struct.h" |
| 12 #include "mojo/public/c/bindings/tests/testing_util.h" |
| 12 #include "mojo/public/cpp/system/macros.h" | 13 #include "mojo/public/cpp/system/macros.h" |
| 13 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom-c.h" | 14 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom-c.h" |
| 14 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom-c.h" | 15 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom-c.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 // Tests MojomArray_New(). | 20 // Tests MojomArray_New(). |
| 20 TEST(ArrayTest, New) { | 21 TEST(ArrayTest, New) { |
| 21 char bytes_buffer[1000]; | 22 char bytes_buffer[1000]; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 EXPECT_EQ(8u + 6 * 8u // 6 references types | 93 EXPECT_EQ(8u + 6 * 8u // 6 references types |
| 93 + 1 * 16u // 1 union type | 94 + 1 * 16u // 1 union type |
| 94 + (8u + 16u * 2), // array of 2 unions | 95 + (8u + 16u * 2), // array of 2 unions |
| 95 mojo_test_SmallStruct_ComputeSerializedSize(small_struct)); | 96 mojo_test_SmallStruct_ComputeSerializedSize(small_struct)); |
| 96 | 97 |
| 97 // Save the underlying buffer before encoding, so we can decode+compare | 98 // Save the underlying buffer before encoding, so we can decode+compare |
| 98 // later. | 99 // later. |
| 99 char bytes_buffer_copy[sizeof(bytes_buffer)]; | 100 char bytes_buffer_copy[sizeof(bytes_buffer)]; |
| 100 memcpy(bytes_buffer_copy, bytes_buffer, sizeof(bytes_buffer)); | 101 memcpy(bytes_buffer_copy, bytes_buffer, sizeof(bytes_buffer)); |
| 101 | 102 |
| 102 struct MojomHandleBuffer handle_buf = {NULL, 0u, 0u}; | 103 mojo_test_SmallStruct_EncodePointersAndHandles(small_struct, |
| 103 mojo_test_SmallStruct_EncodePointersAndHandles( | 104 buf.num_bytes_used, NULL); |
| 104 small_struct, buf.num_bytes_used, &handle_buf); | |
| 105 EXPECT_EQ(0u, handle_buf.num_handles_used); | |
| 106 | 105 |
| 107 // The null pointers should now be 0-offsets: | 106 // The null pointers should now be 0-offsets: |
| 108 EXPECT_EQ(0u, small_struct->dummy_struct.offset); | 107 EXPECT_EQ(0u, small_struct->dummy_struct.offset); |
| 109 EXPECT_EQ(0u, small_struct->pod_union_array.offset); | 108 EXPECT_EQ(0u, small_struct->pod_union_array.offset); |
| 110 EXPECT_EQ(0u, small_struct->s_array.offset); | 109 EXPECT_EQ(0u, small_struct->s_array.offset); |
| 111 EXPECT_EQ(0u, small_struct->pod_union_map.offset); | 110 EXPECT_EQ(0u, small_struct->pod_union_map.offset); |
| 112 EXPECT_EQ(0u, small_struct->nullable_pod_union_map.offset); | 111 EXPECT_EQ(0u, small_struct->nullable_pod_union_map.offset); |
| 113 | 112 |
| 114 // Test the pod_union_array offset: | 113 // Test the pod_union_array offset: |
| 115 EXPECT_EQ( | 114 EXPECT_EQ( |
| 116 sizeof(struct mojo_test_SmallStruct) - | 115 sizeof(struct mojo_test_SmallStruct) - |
| 117 offsetof(struct mojo_test_SmallStruct, nullable_pod_union_array), | 116 offsetof(struct mojo_test_SmallStruct, nullable_pod_union_array), |
| 118 small_struct->nullable_pod_union_array.offset); | 117 small_struct->nullable_pod_union_array.offset); |
| 119 | 118 |
| 120 mojo_test_SmallStruct_DecodePointersAndHandles(small_struct, | 119 mojo_test_SmallStruct_DecodePointersAndHandles(small_struct, |
| 121 buf.num_bytes_used, NULL, 0); | 120 buf.num_bytes_used, NULL, 0); |
| 122 EXPECT_EQ(0, memcmp(buf.buf, bytes_buffer_copy, buf.num_bytes_used)); | 121 EXPECT_EQ(0, memcmp(buf.buf, bytes_buffer_copy, buf.num_bytes_used)); |
| 122 |
| 123 { |
| 124 char bytes_buffer2[sizeof(bytes_buffer)] = {0}; |
| 125 struct MojomBuffer buf2 = {bytes_buffer2, sizeof(bytes_buffer2), 0}; |
| 126 CopyAndCompare(&buf2, small_struct, buf.num_bytes_used, |
| 127 mojo_test_SmallStruct_DeepCopy, |
| 128 mojo_test_SmallStruct_EncodePointersAndHandles, |
| 129 mojo_test_SmallStruct_DecodePointersAndHandles); |
| 130 } |
| 123 } | 131 } |
| 124 | 132 |
| 125 // Tests serialized size of an array of arrays. | 133 // Tests serialized size of an array of arrays. |
| 126 TEST(ArraySerializationTest, ArrayOfArrays) { | 134 TEST(ArraySerializationTest, ArrayOfArrays) { |
| 127 char bytes_buffer[1000] = {0}; | 135 char bytes_buffer[1000] = {0}; |
| 128 MojomBuffer buf = {bytes_buffer, sizeof(bytes_buffer), 0}; | 136 MojomBuffer buf = {bytes_buffer, sizeof(bytes_buffer), 0}; |
| 129 | 137 |
| 130 struct mojo_test_ArrayOfArrays* arr = | 138 struct mojo_test_ArrayOfArrays* arr = |
| 131 static_cast<struct mojo_test_ArrayOfArrays*>( | 139 static_cast<struct mojo_test_ArrayOfArrays*>( |
| 132 MojomBuffer_Allocate(&buf, sizeof(struct mojo_test_ArrayOfArrays))); | 140 MojomBuffer_Allocate(&buf, sizeof(struct mojo_test_ArrayOfArrays))); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 159 EXPECT_EQ(24u + (8u + 2 * 8u) // a (with 2 arrays, 1 of them NULL) | 167 EXPECT_EQ(24u + (8u + 2 * 8u) // a (with 2 arrays, 1 of them NULL) |
| 160 + 0u // b (null altogether) | 168 + 0u // b (null altogether) |
| 161 + (8u + 8u), // first array<int> in a | 169 + (8u + 8u), // first array<int> in a |
| 162 mojo_test_ArrayOfArrays_ComputeSerializedSize(arr)); | 170 mojo_test_ArrayOfArrays_ComputeSerializedSize(arr)); |
| 163 | 171 |
| 164 // Save the underlying buffer before encoding, so we can decode+compare | 172 // Save the underlying buffer before encoding, so we can decode+compare |
| 165 // later. | 173 // later. |
| 166 char bytes_buffer_copy[sizeof(bytes_buffer)]; | 174 char bytes_buffer_copy[sizeof(bytes_buffer)]; |
| 167 memcpy(bytes_buffer_copy, bytes_buffer, sizeof(bytes_buffer)); | 175 memcpy(bytes_buffer_copy, bytes_buffer, sizeof(bytes_buffer)); |
| 168 | 176 |
| 169 struct MojomHandleBuffer handle_buf = {NULL, 0u, 0u}; | |
| 170 mojo_test_ArrayOfArrays_EncodePointersAndHandles(arr, buf.num_bytes_used, | 177 mojo_test_ArrayOfArrays_EncodePointersAndHandles(arr, buf.num_bytes_used, |
| 171 &handle_buf); | 178 NULL); |
| 172 EXPECT_EQ(0u, handle_buf.num_handles_used); | |
| 173 | 179 |
| 174 EXPECT_EQ(sizeof(struct mojo_test_ArrayOfArrays) - | 180 EXPECT_EQ(sizeof(struct mojo_test_ArrayOfArrays) - |
| 175 offsetof(struct mojo_test_ArrayOfArrays, a), | 181 offsetof(struct mojo_test_ArrayOfArrays, a), |
| 176 arr->a.offset); | 182 arr->a.offset); |
| 177 | 183 |
| 178 // Array of int32 should occur at the end of the array of 2 arrays -- so the | 184 // Array of int32 should occur at the end of the array of 2 arrays -- so the |
| 179 // offset is 2 pointer-sizes (1st one pointers to the array of int32, second | 185 // offset is 2 pointer-sizes (1st one pointers to the array of int32, second |
| 180 // one is NULL). | 186 // one is NULL). |
| 181 EXPECT_EQ(2 * sizeof(union MojomArrayHeaderPtr), | 187 EXPECT_EQ(2 * sizeof(union MojomArrayHeaderPtr), |
| 182 MOJOM_ARRAY_INDEX(a_array, union MojomArrayHeaderPtr, 0)->offset); | 188 MOJOM_ARRAY_INDEX(a_array, union MojomArrayHeaderPtr, 0)->offset); |
| 183 EXPECT_EQ(0u, | 189 EXPECT_EQ(0u, |
| 184 MOJOM_ARRAY_INDEX(a_array, union MojomArrayHeaderPtr, 1)->offset); | 190 MOJOM_ARRAY_INDEX(a_array, union MojomArrayHeaderPtr, 1)->offset); |
| 185 | 191 |
| 186 mojo_test_ArrayOfArrays_DecodePointersAndHandles(arr, buf.num_bytes_used, | 192 mojo_test_ArrayOfArrays_DecodePointersAndHandles(arr, buf.num_bytes_used, |
| 187 NULL, 0); | 193 NULL, 0); |
| 188 EXPECT_EQ(0, memcmp(buf.buf, bytes_buffer_copy, buf.num_bytes_used)); | 194 EXPECT_EQ(0, memcmp(buf.buf, bytes_buffer_copy, buf.num_bytes_used)); |
| 195 |
| 196 { |
| 197 char bytes_buffer2[sizeof(bytes_buffer)] = {0}; |
| 198 struct MojomBuffer buf2 = {bytes_buffer2, sizeof(bytes_buffer2), 0}; |
| 199 CopyAndCompare(&buf2, arr, buf.num_bytes_used, |
| 200 mojo_test_ArrayOfArrays_DeepCopy, |
| 201 mojo_test_ArrayOfArrays_EncodePointersAndHandles, |
| 202 mojo_test_ArrayOfArrays_DecodePointersAndHandles); |
| 203 } |
| 189 } | 204 } |
| 190 | 205 |
| 191 // Tests serialization of an array of handles. | 206 // Tests serialization of an array of handles. |
| 192 TEST(ArraySerializationTest, ArrayOfHandles) { | 207 TEST(ArraySerializationTest, ArrayOfHandles) { |
| 193 char buffer_bytes[1000] = {0}; | 208 char buffer_bytes[1000] = {0}; |
| 194 MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0}; | 209 MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0}; |
| 195 | 210 |
| 196 struct mojo_test_StructWithNullableHandles* handle_struct = | 211 struct mojo_test_StructWithNullableHandles* handle_struct = |
| 197 static_cast<struct mojo_test_StructWithNullableHandles*>( | 212 static_cast<struct mojo_test_StructWithNullableHandles*>( |
| 198 MojomBuffer_Allocate( | 213 MojomBuffer_Allocate( |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 handle_struct->array_h.offset); | 255 handle_struct->array_h.offset); |
| 241 | 256 |
| 242 mojo_test_StructWithNullableHandles_DecodePointersAndHandles( | 257 mojo_test_StructWithNullableHandles_DecodePointersAndHandles( |
| 243 handle_struct, buf.num_bytes_used, handles, MOJO_ARRAYSIZE(handles)); | 258 handle_struct, buf.num_bytes_used, handles, MOJO_ARRAYSIZE(handles)); |
| 244 EXPECT_EQ(0, memcmp(buf.buf, buffer_bytes_copy, buf.num_bytes_used)); | 259 EXPECT_EQ(0, memcmp(buf.buf, buffer_bytes_copy, buf.num_bytes_used)); |
| 245 | 260 |
| 246 // Check that the handles in the handles array are invalidated: | 261 // Check that the handles in the handles array are invalidated: |
| 247 EXPECT_EQ(MOJO_HANDLE_INVALID, handles[0]); | 262 EXPECT_EQ(MOJO_HANDLE_INVALID, handles[0]); |
| 248 EXPECT_EQ(MOJO_HANDLE_INVALID, handles[1]); | 263 EXPECT_EQ(MOJO_HANDLE_INVALID, handles[1]); |
| 249 EXPECT_EQ(MOJO_HANDLE_INVALID, handles[2]); | 264 EXPECT_EQ(MOJO_HANDLE_INVALID, handles[2]); |
| 265 |
| 266 { |
| 267 char buffer_bytes2[sizeof(buffer_bytes)] = {0}; |
| 268 struct MojomBuffer buf2 = {buffer_bytes2, sizeof(buffer_bytes2), 0}; |
| 269 auto* copied_struct = |
| 270 mojo_test_StructWithNullableHandles_DeepCopy(&buf2, handle_struct); |
| 271 |
| 272 // The copy should have moved the handles over to the new struct. |
| 273 EXPECT_EQ(MOJO_HANDLE_INVALID, handle_struct->h); |
| 274 EXPECT_EQ(MOJO_HANDLE_INVALID, |
| 275 *MOJOM_ARRAY_INDEX(handle_struct->array_h.ptr, MojoHandle, 0)); |
| 276 EXPECT_EQ(MOJO_HANDLE_INVALID, |
| 277 *MOJOM_ARRAY_INDEX(handle_struct->array_h.ptr, MojoHandle, 1)); |
| 278 EXPECT_EQ(MOJO_HANDLE_INVALID, |
| 279 *MOJOM_ARRAY_INDEX(handle_struct->array_h.ptr, MojoHandle, 2)); |
| 280 // The new struct should have the handles now: |
| 281 EXPECT_EQ(static_cast<MojoHandle>(10u), copied_struct->h); |
| 282 EXPECT_EQ(static_cast<MojoHandle>(20u), |
| 283 *MOJOM_ARRAY_INDEX(copied_struct->array_h.ptr, MojoHandle, 0)); |
| 284 EXPECT_EQ(MOJO_HANDLE_INVALID, |
| 285 *MOJOM_ARRAY_INDEX(copied_struct->array_h.ptr, MojoHandle, 1)); |
| 286 EXPECT_EQ(static_cast<MojoHandle>(30u), |
| 287 *MOJOM_ARRAY_INDEX(copied_struct->array_h.ptr, MojoHandle, 2)); |
| 288 } |
| 250 } | 289 } |
| 251 | 290 |
| 252 } // namespace | 291 } // namespace |
| OLD | NEW |