OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/edk/system/data_pipe.h" | 5 #include "mojo/edk/system/data_pipe.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <limits> | 10 #include <limits> |
11 | 11 |
12 #include "mojo/edk/system/configuration.h" | 12 #include "mojo/edk/system/constants.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace mojo { | 15 namespace mojo { |
16 namespace system { | 16 namespace system { |
17 namespace { | 17 namespace { |
18 | 18 |
19 const uint32_t kSizeOfCreateOptions = | 19 const uint32_t kSizeOfCreateOptions = |
20 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)); | 20 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)); |
21 | 21 |
22 // Does a cursory sanity check of |validated_options|. Calls | 22 // Does a cursory sanity check of |validated_options|. Calls |
23 // |ValidateCreateOptions()| on already-validated options. The validated options | 23 // |ValidateCreateOptions()| on already-validated options. The validated options |
24 // should be valid, and the revalidated copy should be the same. | 24 // should be valid, and the revalidated copy should be the same. |
25 void RevalidateCreateOptions( | 25 void RevalidateCreateOptions( |
26 const MojoCreateDataPipeOptions& validated_options) { | 26 const MojoCreateDataPipeOptions& validated_options) { |
27 EXPECT_EQ(kSizeOfCreateOptions, validated_options.struct_size); | 27 EXPECT_EQ(kSizeOfCreateOptions, validated_options.struct_size); |
28 // Nothing to check for flags. | 28 // Nothing to check for flags. |
29 EXPECT_GT(validated_options.element_num_bytes, 0u); | 29 EXPECT_GT(validated_options.element_num_bytes, 0u); |
30 EXPECT_GT(validated_options.capacity_num_bytes, 0u); | 30 EXPECT_GT(validated_options.capacity_num_bytes, 0u); |
31 EXPECT_EQ(0u, validated_options.capacity_num_bytes % | 31 EXPECT_EQ(0u, |
32 validated_options.element_num_bytes); | 32 validated_options.capacity_num_bytes % |
| 33 validated_options.element_num_bytes); |
33 | 34 |
34 MojoCreateDataPipeOptions revalidated_options = {}; | 35 MojoCreateDataPipeOptions revalidated_options = {}; |
35 EXPECT_EQ(MOJO_RESULT_OK, | 36 EXPECT_EQ(MOJO_RESULT_OK, |
36 DataPipe::ValidateCreateOptions(MakeUserPointer(&validated_options), | 37 DataPipe::ValidateCreateOptions(MakeUserPointer(&validated_options), |
37 &revalidated_options)); | 38 &revalidated_options)); |
38 EXPECT_EQ(validated_options.struct_size, revalidated_options.struct_size); | 39 EXPECT_EQ(validated_options.struct_size, revalidated_options.struct_size); |
39 EXPECT_EQ(validated_options.element_num_bytes, | 40 EXPECT_EQ(validated_options.element_num_bytes, |
40 revalidated_options.element_num_bytes); | 41 revalidated_options.element_num_bytes); |
41 EXPECT_EQ(validated_options.capacity_num_bytes, | 42 EXPECT_EQ(validated_options.capacity_num_bytes, |
42 revalidated_options.capacity_num_bytes); | 43 revalidated_options.capacity_num_bytes); |
43 EXPECT_EQ(validated_options.flags, revalidated_options.flags); | 44 EXPECT_EQ(validated_options.flags, revalidated_options.flags); |
44 } | 45 } |
45 | 46 |
46 // Checks that a default-computed capacity is correct. (Does not duplicate the | 47 // Checks that a default-computed capacity is correct. (Does not duplicate the |
47 // checks done by |RevalidateCreateOptions()|.) | 48 // checks done by |RevalidateCreateOptions()|.) |
48 void CheckDefaultCapacity(const MojoCreateDataPipeOptions& validated_options) { | 49 void CheckDefaultCapacity(const MojoCreateDataPipeOptions& validated_options) { |
49 EXPECT_LE(validated_options.capacity_num_bytes, | 50 EXPECT_LE(validated_options.capacity_num_bytes, |
50 GetConfiguration().default_data_pipe_capacity_bytes); | 51 kDefaultDataPipeCapacityBytes); |
51 EXPECT_GT(validated_options.capacity_num_bytes + | 52 EXPECT_GT(validated_options.capacity_num_bytes + |
52 validated_options.element_num_bytes, | 53 validated_options.element_num_bytes, |
53 GetConfiguration().default_data_pipe_capacity_bytes); | 54 kDefaultDataPipeCapacityBytes); |
54 } | 55 } |
55 | 56 |
56 // Tests valid inputs to |ValidateCreateOptions()|. | 57 // Tests valid inputs to |ValidateCreateOptions()|. |
57 TEST(DataPipeTest, ValidateCreateOptionsValid) { | 58 TEST(DataPipeTest, ValidateCreateOptionsValid) { |
58 // Default options. | 59 // Default options. |
59 { | 60 { |
60 MojoCreateDataPipeOptions validated_options = {}; | 61 MojoCreateDataPipeOptions validated_options = {}; |
61 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 62 EXPECT_EQ( |
62 NullUserPointer(), &validated_options)); | 63 MOJO_RESULT_OK, |
| 64 DataPipe::ValidateCreateOptions(NullUserPointer(), &validated_options)); |
63 RevalidateCreateOptions(validated_options); | 65 RevalidateCreateOptions(validated_options); |
64 CheckDefaultCapacity(validated_options); | 66 CheckDefaultCapacity(validated_options); |
65 } | 67 } |
66 | 68 |
67 // Size member, but nothing beyond. | 69 // Size member, but nothing beyond. |
68 { | 70 { |
69 MojoCreateDataPipeOptions options = { | 71 MojoCreateDataPipeOptions options = { |
70 offsetof(MojoCreateDataPipeOptions, flags) // |struct_size|. | 72 offsetof(MojoCreateDataPipeOptions, flags) // |struct_size|. |
71 }; | 73 }; |
72 MojoCreateDataPipeOptions validated_options = {}; | 74 MojoCreateDataPipeOptions validated_options = {}; |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 MojoCreateDataPipeOptions unused; | 368 MojoCreateDataPipeOptions unused; |
367 EXPECT_EQ( | 369 EXPECT_EQ( |
368 MOJO_RESULT_RESOURCE_EXHAUSTED, | 370 MOJO_RESULT_RESOURCE_EXHAUSTED, |
369 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), &unused)); | 371 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), &unused)); |
370 } | 372 } |
371 } | 373 } |
372 | 374 |
373 } // namespace | 375 } // namespace |
374 } // namespace system | 376 } // namespace system |
375 } // namespace mojo | 377 } // namespace mojo |
OLD | NEW |