Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(807)

Side by Side Diff: mojo/edk/system/shared_buffer_dispatcher_unittest.cc

Issue 1776033002: Implement MojoGetBufferInformation(), part 1. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: doh Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/edk/system/shared_buffer_dispatcher.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/shared_buffer_dispatcher.h" 5 #include "mojo/edk/system/shared_buffer_dispatcher.h"
6 6
7 #include <limits> 7 #include <limits>
8 #include <memory> 8 #include <memory>
9 9
10 #include "mojo/edk/embedder/simple_platform_support.h" 10 #include "mojo/edk/embedder/simple_platform_support.h"
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 ~0u // |flags|. 112 ~0u // |flags|.
113 }; 113 };
114 MojoCreateSharedBufferOptions unused; 114 MojoCreateSharedBufferOptions unused;
115 EXPECT_EQ(MOJO_RESULT_UNIMPLEMENTED, 115 EXPECT_EQ(MOJO_RESULT_UNIMPLEMENTED,
116 SharedBufferDispatcher::ValidateCreateOptions( 116 SharedBufferDispatcher::ValidateCreateOptions(
117 MakeUserPointer(&options), &unused)); 117 MakeUserPointer(&options), &unused));
118 } 118 }
119 } 119 }
120 120
121 TEST_F(SharedBufferDispatcherTest, CreateAndMapBuffer) { 121 TEST_F(SharedBufferDispatcherTest, CreateAndMapBuffer) {
122 const uint64_t kSize = 100u;
123
122 MojoResult result = MOJO_RESULT_INTERNAL; 124 MojoResult result = MOJO_RESULT_INTERNAL;
123 auto dispatcher = SharedBufferDispatcher::Create( 125 auto dispatcher = SharedBufferDispatcher::Create(
124 platform_support(), SharedBufferDispatcher::kDefaultCreateOptions, 100, 126 platform_support(), SharedBufferDispatcher::kDefaultCreateOptions, kSize,
125 &result); 127 &result);
126 EXPECT_EQ(MOJO_RESULT_OK, result); 128 EXPECT_EQ(MOJO_RESULT_OK, result);
127 129
128 ASSERT_TRUE(dispatcher); 130 ASSERT_TRUE(dispatcher);
129 EXPECT_EQ(Dispatcher::Type::SHARED_BUFFER, dispatcher->GetType()); 131 EXPECT_EQ(Dispatcher::Type::SHARED_BUFFER, dispatcher->GetType());
130 132
133 // Get information about the buffer (in particular the size) and check it.
134 MojoBufferInformation info = {};
135 EXPECT_EQ(MOJO_RESULT_OK,
136 dispatcher->GetBufferInformation(
137 MakeUserPointer(&info), static_cast<uint32_t>(sizeof(info))));
138 EXPECT_EQ(sizeof(MojoBufferInformation), info.struct_size);
139 EXPECT_EQ(MOJO_BUFFER_INFORMATION_FLAG_NONE, info.flags);
140 EXPECT_EQ(kSize, info.num_bytes);
141
142 // Also check that some invalid calls to |GetBufferInformation()| fail in the
143 // expected way.
144 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
145 dispatcher->GetBufferInformation(MakeUserPointer(&info), 0u));
146 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
147 dispatcher->GetBufferInformation(MakeUserPointer(&info), 15u));
148
149 // It's also valid to call it with a larger-than-required |info_num_bytes|.
150 // (Note: The pointer must be aligned, so we use an array of two
151 // |MojoBufferInformation|s.)
152 MojoBufferInformation infos[2] = {};
153 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->GetBufferInformation(
154 MakeUserPointer(&infos[0]),
155 static_cast<uint32_t>(sizeof(infos[0]) + 1)));
156 EXPECT_EQ(kSize, infos[0].num_bytes);
157
131 // Make a couple of mappings. 158 // Make a couple of mappings.
132 std::unique_ptr<PlatformSharedBufferMapping> mapping1; 159 std::unique_ptr<PlatformSharedBufferMapping> mapping1;
133 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->MapBuffer( 160 EXPECT_EQ(
134 0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping1)); 161 MOJO_RESULT_OK,
162 dispatcher->MapBuffer(0u, kSize, MOJO_MAP_BUFFER_FLAG_NONE, &mapping1));
135 ASSERT_TRUE(mapping1); 163 ASSERT_TRUE(mapping1);
136 ASSERT_TRUE(mapping1->GetBase()); 164 ASSERT_TRUE(mapping1->GetBase());
137 EXPECT_EQ(100u, mapping1->GetLength()); 165 EXPECT_EQ(kSize, mapping1->GetLength());
138 // Write something. 166 // Write something.
139 static_cast<char*>(mapping1->GetBase())[50] = 'x'; 167 static_cast<char*>(mapping1->GetBase())[50] = 'x';
140 168
141 std::unique_ptr<PlatformSharedBufferMapping> mapping2; 169 std::unique_ptr<PlatformSharedBufferMapping> mapping2;
142 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->MapBuffer( 170 EXPECT_EQ(MOJO_RESULT_OK,
143 50, 50, MOJO_MAP_BUFFER_FLAG_NONE, &mapping2)); 171 dispatcher->MapBuffer(kSize / 2, kSize / 2,
172 MOJO_MAP_BUFFER_FLAG_NONE, &mapping2));
144 ASSERT_TRUE(mapping2); 173 ASSERT_TRUE(mapping2);
145 ASSERT_TRUE(mapping2->GetBase()); 174 ASSERT_TRUE(mapping2->GetBase());
146 EXPECT_EQ(50u, mapping2->GetLength()); 175 EXPECT_EQ(kSize / 2, mapping2->GetLength());
147 EXPECT_EQ('x', static_cast<char*>(mapping2->GetBase())[0]); 176 EXPECT_EQ('x', static_cast<char*>(mapping2->GetBase())[0]);
148 177
149 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); 178 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close());
150 179
151 // Check that we can still read/write to mappings after the dispatcher has 180 // Check that we can still read/write to mappings after the dispatcher has
152 // gone away. 181 // gone away.
153 static_cast<char*>(mapping2->GetBase())[1] = 'y'; 182 static_cast<char*>(mapping2->GetBase())[1] = 'y';
154 EXPECT_EQ('y', static_cast<char*>(mapping1->GetBase())[51]); 183 EXPECT_EQ('y', static_cast<char*>(mapping1->GetBase())[kSize / 2 + 1]);
155 } 184 }
156 185
157 TEST_F(SharedBufferDispatcherTest, DuplicateBufferHandle) { 186 TEST_F(SharedBufferDispatcherTest, DuplicateBufferHandle) {
158 MojoResult result = MOJO_RESULT_INTERNAL; 187 MojoResult result = MOJO_RESULT_INTERNAL;
159 auto dispatcher1 = SharedBufferDispatcher::Create( 188 auto dispatcher1 = SharedBufferDispatcher::Create(
160 platform_support(), SharedBufferDispatcher::kDefaultCreateOptions, 100, 189 platform_support(), SharedBufferDispatcher::kDefaultCreateOptions, 100,
161 &result); 190 &result);
162 EXPECT_EQ(MOJO_RESULT_OK, result); 191 EXPECT_EQ(MOJO_RESULT_OK, result);
163 192
164 // Map and write something. 193 // Map and write something.
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 306 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
278 dispatcher->MapBuffer(0, 0, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); 307 dispatcher->MapBuffer(0, 0, MOJO_MAP_BUFFER_FLAG_NONE, &mapping));
279 EXPECT_FALSE(mapping); 308 EXPECT_FALSE(mapping);
280 309
281 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); 310 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close());
282 } 311 }
283 312
284 } // namespace 313 } // namespace
285 } // namespace system 314 } // namespace system
286 } // namespace mojo 315 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/system/shared_buffer_dispatcher.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698