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/system/local_data_pipe.h" | 5 #include "mojo/system/local_data_pipe.h" |
6 | 6 |
| 7 #include "base/basictypes.h" |
7 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
8 #include "mojo/system/data_pipe.h" | 9 #include "mojo/system/data_pipe.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
10 | 11 |
11 namespace mojo { | 12 namespace mojo { |
12 namespace system { | 13 namespace system { |
13 namespace { | 14 namespace { |
14 | 15 |
| 16 const uint32_t kSizeOfOptions = |
| 17 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)); |
| 18 |
15 // Validate options. | 19 // Validate options. |
16 TEST(LocalDataPipeTest, Creation) { | 20 TEST(LocalDataPipeTest, Creation) { |
17 // Get default options. | |
18 MojoCreateDataPipeOptions default_options = { 0 }; | |
19 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateOptions(NULL, &default_options)); | |
20 | |
21 // Create using default options. | 21 // Create using default options. |
22 { | 22 { |
| 23 // Get default options. |
| 24 MojoCreateDataPipeOptions default_options = { 0 }; |
| 25 EXPECT_EQ(MOJO_RESULT_OK, |
| 26 DataPipe::ValidateOptions(NULL, &default_options)); |
23 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(default_options)); | 27 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(default_options)); |
24 dp->ProducerClose(); | 28 dp->ProducerClose(); |
25 dp->ConsumerClose(); | 29 dp->ConsumerClose(); |
26 } | 30 } |
27 | 31 |
28 // TODO(vtl): More. | 32 // Create using non-default options. |
| 33 { |
| 34 const MojoCreateDataPipeOptions options = { |
| 35 kSizeOfOptions, // |struct_size|. |
| 36 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
| 37 1, // |element_num_bytes|. |
| 38 1000 // |capacity_num_bytes|. |
| 39 }; |
| 40 MojoCreateDataPipeOptions validated_options = { 0 }; |
| 41 EXPECT_EQ(MOJO_RESULT_OK, |
| 42 DataPipe::ValidateOptions(&options, &validated_options)); |
| 43 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
| 44 dp->ProducerClose(); |
| 45 dp->ConsumerClose(); |
| 46 } |
| 47 { |
| 48 const MojoCreateDataPipeOptions options = { |
| 49 kSizeOfOptions, // |struct_size|. |
| 50 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
| 51 4, // |element_num_bytes|. |
| 52 4000 // |capacity_num_bytes|. |
| 53 }; |
| 54 MojoCreateDataPipeOptions validated_options = { 0 }; |
| 55 EXPECT_EQ(MOJO_RESULT_OK, |
| 56 DataPipe::ValidateOptions(&options, &validated_options)); |
| 57 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
| 58 dp->ProducerClose(); |
| 59 dp->ConsumerClose(); |
| 60 } |
| 61 { |
| 62 const MojoCreateDataPipeOptions options = { |
| 63 kSizeOfOptions, // |struct_size|. |
| 64 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. |
| 65 7, // |element_num_bytes|. |
| 66 7000000 // |capacity_num_bytes|. |
| 67 }; |
| 68 MojoCreateDataPipeOptions validated_options = { 0 }; |
| 69 EXPECT_EQ(MOJO_RESULT_OK, |
| 70 DataPipe::ValidateOptions(&options, &validated_options)); |
| 71 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
| 72 dp->ProducerClose(); |
| 73 dp->ConsumerClose(); |
| 74 } |
| 75 // Default capacity. |
| 76 { |
| 77 const MojoCreateDataPipeOptions options = { |
| 78 kSizeOfOptions, // |struct_size|. |
| 79 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. |
| 80 100, // |element_num_bytes|. |
| 81 0 // |capacity_num_bytes|. |
| 82 }; |
| 83 MojoCreateDataPipeOptions validated_options = { 0 }; |
| 84 EXPECT_EQ(MOJO_RESULT_OK, |
| 85 DataPipe::ValidateOptions(&options, &validated_options)); |
| 86 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
| 87 dp->ProducerClose(); |
| 88 dp->ConsumerClose(); |
| 89 } |
| 90 } |
| 91 |
| 92 TEST(LocalDataPipeTest, SimpleReadWrite) { |
| 93 const MojoCreateDataPipeOptions options = { |
| 94 kSizeOfOptions, // |struct_size|. |
| 95 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
| 96 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
| 97 1000 * sizeof(int32_t) // |capacity_num_bytes|. |
| 98 }; |
| 99 MojoCreateDataPipeOptions validated_options = { 0 }; |
| 100 EXPECT_EQ(MOJO_RESULT_OK, |
| 101 DataPipe::ValidateOptions(&options, &validated_options)); |
| 102 |
| 103 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
| 104 |
| 105 int32_t elements[10] = { 0 }; |
| 106 uint32_t num_bytes = 0; |
| 107 |
| 108 // Try reading; nothing there yet. |
| 109 num_bytes = static_cast<uint32_t>(arraysize(elements) * sizeof(elements[0])); |
| 110 EXPECT_EQ(MOJO_RESULT_NOT_FOUND, |
| 111 dp->ConsumerReadData(elements, &num_bytes, false)); |
| 112 |
| 113 // Query; nothing there yet. |
| 114 num_bytes = 0; |
| 115 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); |
| 116 EXPECT_EQ(0u, num_bytes); |
| 117 |
| 118 // Discard; nothing there yet. |
| 119 num_bytes = static_cast<uint32_t>(5u * sizeof(elements[0])); |
| 120 EXPECT_EQ(MOJO_RESULT_NOT_FOUND, dp->ConsumerDiscardData(&num_bytes, false)); |
| 121 |
| 122 // Read with invalid |num_bytes|. |
| 123 num_bytes = sizeof(elements[0]) + 1; |
| 124 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
| 125 dp->ConsumerReadData(elements, &num_bytes, false)); |
| 126 |
| 127 // Write two elements. |
| 128 elements[0] = 123; |
| 129 elements[1] = 456; |
| 130 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); |
| 131 EXPECT_EQ(MOJO_RESULT_OK, |
| 132 dp->ProducerWriteData(elements, &num_bytes, false)); |
| 133 // It should have written everything (even without "all or none"). |
| 134 EXPECT_EQ(2u * sizeof(elements[0]), num_bytes); |
| 135 |
| 136 // Query. |
| 137 num_bytes = 0; |
| 138 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); |
| 139 EXPECT_EQ(2 * sizeof(elements[0]), num_bytes); |
| 140 |
| 141 // Read one element. |
| 142 elements[0] = -1; |
| 143 elements[1] = -1; |
| 144 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); |
| 145 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(elements, &num_bytes, false)); |
| 146 EXPECT_EQ(1u * sizeof(elements[0]), num_bytes); |
| 147 EXPECT_EQ(123, elements[0]); |
| 148 EXPECT_EQ(-1, elements[1]); |
| 149 |
| 150 // Query. |
| 151 num_bytes = 0; |
| 152 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); |
| 153 EXPECT_EQ(1 * sizeof(elements[0]), num_bytes); |
| 154 |
| 155 // Try to read two elements, with "all or none". |
| 156 elements[0] = -1; |
| 157 elements[1] = -1; |
| 158 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); |
| 159 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
| 160 dp->ConsumerReadData(elements, &num_bytes, true)); |
| 161 EXPECT_EQ(-1, elements[0]); |
| 162 EXPECT_EQ(-1, elements[1]); |
| 163 |
| 164 // Try to read two elements, without "all or none". |
| 165 elements[0] = -1; |
| 166 elements[1] = -1; |
| 167 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); |
| 168 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(elements, &num_bytes, false)); |
| 169 EXPECT_EQ(456, elements[0]); |
| 170 EXPECT_EQ(-1, elements[1]); |
| 171 |
| 172 // Query. |
| 173 num_bytes = 0; |
| 174 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); |
| 175 EXPECT_EQ(0u, num_bytes); |
| 176 |
| 177 // TODO(vtl): More: discard (with/without "all or none"). More "all or none" |
| 178 // tests. |
| 179 |
| 180 dp->ProducerClose(); |
| 181 dp->ConsumerClose(); |
29 } | 182 } |
30 | 183 |
31 // TODO(vtl): More. | 184 // TODO(vtl): More. |
32 | 185 |
33 } // namespace | 186 } // namespace |
34 } // namespace system | 187 } // namespace system |
35 } // namespace mojo | 188 } // namespace mojo |
OLD | NEW |