| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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 #include <deque> |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/memory/scoped_callback_factory.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "base/platform_file.h" |
| 11 #include "base/scoped_temp_dir.h" |
| 12 #include "base/task.h" |
| 13 #include "webkit/plugins/ppapi/mock_plugin_delegate.h" |
| 14 #include "webkit/plugins/ppapi/ppapi_unittest.h" |
| 15 #include "webkit/plugins/ppapi/quota_file_io.h" |
| 16 |
| 17 using base::MessageLoopProxy; |
| 18 using base::PlatformFile; |
| 19 using base::PlatformFileError; |
| 20 |
| 21 namespace webkit { |
| 22 namespace ppapi { |
| 23 |
| 24 namespace { |
| 25 class QuotaMockPluginDelegate : public MockPluginDelegate { |
| 26 public: |
| 27 typedef PluginDelegate::AvailableSpaceCallback Callback; |
| 28 |
| 29 QuotaMockPluginDelegate() |
| 30 : available_space_(0), |
| 31 will_update_count_(0), |
| 32 file_thread_(MessageLoopProxy::CreateForCurrentThread()), |
| 33 runnable_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 34 } |
| 35 virtual ~QuotaMockPluginDelegate() {} |
| 36 |
| 37 virtual scoped_refptr<MessageLoopProxy> GetFileThreadMessageLoopProxy() { |
| 38 return file_thread_; |
| 39 } |
| 40 |
| 41 virtual void QueryAvailableSpace( |
| 42 const GURL& origin, |
| 43 quota::StorageType type, |
| 44 Callback* callback) OVERRIDE { |
| 45 DCHECK(callback); |
| 46 MessageLoopProxy::CreateForCurrentThread()->PostTask( |
| 47 FROM_HERE, runnable_factory_.NewRunnableMethod( |
| 48 &QuotaMockPluginDelegate::RunAvailableSpaceCallback, callback)); |
| 49 } |
| 50 |
| 51 virtual void WillUpdateFile(const GURL& file_path) OVERRIDE { |
| 52 file_path_ = file_path; |
| 53 ++will_update_count_; |
| 54 } |
| 55 |
| 56 virtual void DidUpdateFile(const GURL& file_path, int64_t delta) OVERRIDE { |
| 57 ASSERT_EQ(file_path_, file_path); |
| 58 ASSERT_GT(will_update_count_, 0); |
| 59 --will_update_count_; |
| 60 available_space_ -= delta; |
| 61 } |
| 62 |
| 63 void set_available_space(int64 available) { available_space_ = available; } |
| 64 int64_t available_space() const { return available_space_; } |
| 65 |
| 66 private: |
| 67 void RunAvailableSpaceCallback(Callback* callback) { |
| 68 callback->Run(available_space_); |
| 69 delete callback; |
| 70 } |
| 71 |
| 72 int64_t available_space_; |
| 73 int will_update_count_; |
| 74 GURL file_path_; |
| 75 scoped_refptr<MessageLoopProxy> file_thread_; |
| 76 ScopedRunnableMethodFactory<QuotaMockPluginDelegate> runnable_factory_; |
| 77 }; |
| 78 } // namespace |
| 79 |
| 80 class QuotaFileIOTest : public PpapiUnittest { |
| 81 public: |
| 82 QuotaFileIOTest() |
| 83 : callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {} |
| 84 |
| 85 virtual void SetUp() OVERRIDE { |
| 86 PpapiUnittest::SetUp(); |
| 87 ASSERT_TRUE(dir_.CreateUniqueTempDir()); |
| 88 FilePath path; |
| 89 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(dir_.path(), &path)); |
| 90 int file_flags = base::PLATFORM_FILE_OPEN | |
| 91 base::PLATFORM_FILE_READ | |
| 92 base::PLATFORM_FILE_WRITE | |
| 93 base::PLATFORM_FILE_WRITE_ATTRIBUTES; |
| 94 bool created = false; |
| 95 file_ = base::kInvalidPlatformFileValue; |
| 96 PlatformFileError error = base::PLATFORM_FILE_OK; |
| 97 file_ = base::CreatePlatformFile(path, file_flags, &created, &error); |
| 98 ASSERT_EQ(base::PLATFORM_FILE_OK, error); |
| 99 ASSERT_NE(base::kInvalidPlatformFileValue, file_); |
| 100 ASSERT_FALSE(created); |
| 101 quota_file_io_.reset(new QuotaFileIO( |
| 102 instance(), file_, GURL(), PP_FILESYSTEMTYPE_LOCALTEMPORARY)); |
| 103 } |
| 104 |
| 105 virtual void TearDown() OVERRIDE { |
| 106 quota_file_io_.reset(); |
| 107 if (file_ != base::kInvalidPlatformFileValue) |
| 108 base::ClosePlatformFile(file_); |
| 109 PpapiUnittest::TearDown(); |
| 110 } |
| 111 |
| 112 protected: |
| 113 virtual MockPluginDelegate* NewPluginDelegate() OVERRIDE { |
| 114 return static_cast<MockPluginDelegate*>(new QuotaMockPluginDelegate); |
| 115 } |
| 116 |
| 117 void WriteTestBody(bool will_operation) { |
| 118 quota_plugin_delegate()->set_available_space(100); |
| 119 std::string read_buffer; |
| 120 |
| 121 // Write 8 bytes at offset 0 (-> length=8). |
| 122 std::string data("12345678"); |
| 123 Write(0, data, will_operation); |
| 124 MessageLoop::current()->RunAllPending(); |
| 125 ASSERT_EQ(1U, num_results()); |
| 126 EXPECT_EQ(static_cast<int>(data.size()), bytes_written().front()); |
| 127 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front()); |
| 128 EXPECT_EQ(100 - 8, quota_plugin_delegate()->available_space()); |
| 129 reset_results(); |
| 130 |
| 131 if (will_operation) { |
| 132 // WillWrite doesn't actually write. |
| 133 EXPECT_EQ(0, GetPlatformFileSize()); |
| 134 // Adjust the actual file size to 'fake' write to proceed testing. |
| 135 SetPlatformFileSize(8); |
| 136 } else { |
| 137 EXPECT_EQ(8, GetPlatformFileSize()); |
| 138 ReadPlatformFile(&read_buffer); |
| 139 EXPECT_EQ(data, read_buffer); |
| 140 } |
| 141 |
| 142 // Write 5 bytes at offset 5 (-> length=10). |
| 143 data = "55555"; |
| 144 Write(5, data, will_operation); |
| 145 MessageLoop::current()->RunAllPending(); |
| 146 ASSERT_EQ(1U, num_results()); |
| 147 EXPECT_EQ(static_cast<int>(data.size()), bytes_written().front()); |
| 148 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front()); |
| 149 EXPECT_EQ(100 - 10, quota_plugin_delegate()->available_space()); |
| 150 reset_results(); |
| 151 |
| 152 if (will_operation) { |
| 153 EXPECT_EQ(8, GetPlatformFileSize()); |
| 154 SetPlatformFileSize(10); |
| 155 } else { |
| 156 EXPECT_EQ(10, GetPlatformFileSize()); |
| 157 ReadPlatformFile(&read_buffer); |
| 158 EXPECT_EQ("1234555555", read_buffer); |
| 159 } |
| 160 |
| 161 // Write 7 bytes at offset 8 (-> length=15). |
| 162 data = "9012345"; |
| 163 Write(8, data, will_operation); |
| 164 MessageLoop::current()->RunAllPending(); |
| 165 ASSERT_EQ(1U, num_results()); |
| 166 EXPECT_EQ(static_cast<int>(data.size()), bytes_written().front()); |
| 167 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front()); |
| 168 EXPECT_EQ(100 - 15, quota_plugin_delegate()->available_space()); |
| 169 reset_results(); |
| 170 |
| 171 if (will_operation) { |
| 172 EXPECT_EQ(10, GetPlatformFileSize()); |
| 173 SetPlatformFileSize(15); |
| 174 } else { |
| 175 EXPECT_EQ(15, GetPlatformFileSize()); |
| 176 ReadPlatformFile(&read_buffer); |
| 177 EXPECT_EQ("123455559012345", read_buffer); |
| 178 } |
| 179 |
| 180 // Write 2 bytes at offset 2 (-> length=15). |
| 181 data = "33"; |
| 182 Write(2, data, will_operation); |
| 183 MessageLoop::current()->RunAllPending(); |
| 184 ASSERT_EQ(1U, num_results()); |
| 185 EXPECT_EQ(static_cast<int>(data.size()), bytes_written().front()); |
| 186 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front()); |
| 187 EXPECT_EQ(100 - 15, quota_plugin_delegate()->available_space()); |
| 188 reset_results(); |
| 189 |
| 190 if (will_operation) { |
| 191 EXPECT_EQ(15, GetPlatformFileSize()); |
| 192 } else { |
| 193 EXPECT_EQ(15, GetPlatformFileSize()); |
| 194 ReadPlatformFile(&read_buffer); |
| 195 EXPECT_EQ("123355559012345", read_buffer); |
| 196 } |
| 197 |
| 198 // Write 4 bytes at offset 20 (-> length=24). |
| 199 data = "XXXX"; |
| 200 Write(20, data, will_operation); |
| 201 MessageLoop::current()->RunAllPending(); |
| 202 ASSERT_EQ(1U, num_results()); |
| 203 EXPECT_EQ(static_cast<int>(data.size()), bytes_written().front()); |
| 204 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front()); |
| 205 EXPECT_EQ(100 - 24, quota_plugin_delegate()->available_space()); |
| 206 reset_results(); |
| 207 |
| 208 if (will_operation) { |
| 209 EXPECT_EQ(15, GetPlatformFileSize()); |
| 210 SetPlatformFileSize(24); |
| 211 } else { |
| 212 EXPECT_EQ(24, GetPlatformFileSize()); |
| 213 ReadPlatformFile(&read_buffer); |
| 214 EXPECT_EQ(std::string("123355559012345\0\0\0\0\0XXXX", 24), read_buffer); |
| 215 } |
| 216 |
| 217 quota_plugin_delegate()->set_available_space(5); |
| 218 |
| 219 // Quota error case. Write 7 bytes at offset 23 (-> length is unchanged) |
| 220 data = "ABCDEFG"; |
| 221 Write(23, data, will_operation); |
| 222 MessageLoop::current()->RunAllPending(); |
| 223 ASSERT_EQ(1U, num_results()); |
| 224 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, status().front()); |
| 225 EXPECT_EQ(5, quota_plugin_delegate()->available_space()); |
| 226 reset_results(); |
| 227 |
| 228 // Overlapping write. Write 6 bytes at offset 2 (-> length is unchanged) |
| 229 data = "ABCDEF"; |
| 230 Write(2, data, will_operation); |
| 231 MessageLoop::current()->RunAllPending(); |
| 232 ASSERT_EQ(1U, num_results()); |
| 233 EXPECT_EQ(static_cast<int>(data.size()), bytes_written().front()); |
| 234 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front()); |
| 235 EXPECT_EQ(5, quota_plugin_delegate()->available_space()); |
| 236 reset_results(); |
| 237 |
| 238 // Overlapping + extending the file size, but within the quota. |
| 239 // Write 6 bytes at offset 23 (-> length=29). |
| 240 Write(23, data, will_operation); |
| 241 MessageLoop::current()->RunAllPending(); |
| 242 ASSERT_EQ(1U, num_results()); |
| 243 EXPECT_EQ(static_cast<int>(data.size()), bytes_written().front()); |
| 244 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front()); |
| 245 EXPECT_EQ(0, quota_plugin_delegate()->available_space()); |
| 246 reset_results(); |
| 247 |
| 248 if (!will_operation) { |
| 249 EXPECT_EQ(29, GetPlatformFileSize()); |
| 250 ReadPlatformFile(&read_buffer); |
| 251 EXPECT_EQ(std::string("12ABCDEF9012345\0\0\0\0\0XXXABCDEF", 29), |
| 252 read_buffer); |
| 253 } |
| 254 } |
| 255 |
| 256 void SetLengthTestBody(bool will_operation) { |
| 257 quota_plugin_delegate()->set_available_space(100); |
| 258 |
| 259 SetLength(0, will_operation); |
| 260 MessageLoop::current()->RunAllPending(); |
| 261 ASSERT_EQ(1U, num_results()); |
| 262 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front()); |
| 263 EXPECT_EQ(0, GetPlatformFileSize()); |
| 264 EXPECT_EQ(100, quota_plugin_delegate()->available_space()); |
| 265 reset_results(); |
| 266 |
| 267 SetLength(8, will_operation); |
| 268 MessageLoop::current()->RunAllPending(); |
| 269 ASSERT_EQ(1U, num_results()); |
| 270 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front()); |
| 271 EXPECT_EQ(100 - 8, quota_plugin_delegate()->available_space()); |
| 272 reset_results(); |
| 273 |
| 274 if (will_operation) { |
| 275 EXPECT_EQ(0, GetPlatformFileSize()); |
| 276 SetPlatformFileSize(8); |
| 277 } else { |
| 278 EXPECT_EQ(8, GetPlatformFileSize()); |
| 279 } |
| 280 |
| 281 SetLength(16, will_operation); |
| 282 MessageLoop::current()->RunAllPending(); |
| 283 ASSERT_EQ(1U, num_results()); |
| 284 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front()); |
| 285 EXPECT_EQ(100 - 16, quota_plugin_delegate()->available_space()); |
| 286 reset_results(); |
| 287 |
| 288 if (will_operation) { |
| 289 EXPECT_EQ(8, GetPlatformFileSize()); |
| 290 SetPlatformFileSize(16); |
| 291 } else { |
| 292 EXPECT_EQ(16, GetPlatformFileSize()); |
| 293 } |
| 294 |
| 295 SetLength(4, will_operation); |
| 296 MessageLoop::current()->RunAllPending(); |
| 297 ASSERT_EQ(1U, num_results()); |
| 298 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front()); |
| 299 EXPECT_EQ(100 - 4, quota_plugin_delegate()->available_space()); |
| 300 reset_results(); |
| 301 |
| 302 if (will_operation) { |
| 303 EXPECT_EQ(16, GetPlatformFileSize()); |
| 304 SetPlatformFileSize(4); |
| 305 } else { |
| 306 EXPECT_EQ(4, GetPlatformFileSize()); |
| 307 } |
| 308 |
| 309 SetLength(0, will_operation); |
| 310 MessageLoop::current()->RunAllPending(); |
| 311 ASSERT_EQ(1U, num_results()); |
| 312 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front()); |
| 313 EXPECT_EQ(100, quota_plugin_delegate()->available_space()); |
| 314 reset_results(); |
| 315 |
| 316 if (will_operation) { |
| 317 EXPECT_EQ(4, GetPlatformFileSize()); |
| 318 SetPlatformFileSize(0); |
| 319 } else { |
| 320 EXPECT_EQ(0, GetPlatformFileSize()); |
| 321 } |
| 322 |
| 323 quota_plugin_delegate()->set_available_space(5); |
| 324 |
| 325 // Quota error case. |
| 326 SetLength(7, will_operation); |
| 327 MessageLoop::current()->RunAllPending(); |
| 328 ASSERT_EQ(1U, num_results()); |
| 329 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, status().front()); |
| 330 EXPECT_EQ(5, quota_plugin_delegate()->available_space()); |
| 331 reset_results(); |
| 332 } |
| 333 |
| 334 QuotaMockPluginDelegate* quota_plugin_delegate() { |
| 335 return static_cast<QuotaMockPluginDelegate*>(delegate()); |
| 336 } |
| 337 |
| 338 void Write(int64_t offset, const std::string& data, bool will_operation) { |
| 339 if (will_operation) { |
| 340 ASSERT_TRUE(quota_file_io_->WillWrite( |
| 341 offset, data.size(), |
| 342 callback_factory_.NewCallback( |
| 343 &QuotaFileIOTest::DidWrite))); |
| 344 } else { |
| 345 ASSERT_TRUE(quota_file_io_->Write( |
| 346 offset, data.c_str(), data.size(), |
| 347 callback_factory_.NewCallback( |
| 348 &QuotaFileIOTest::DidWrite))); |
| 349 } |
| 350 } |
| 351 |
| 352 void SetLength(int64_t length, bool will_operation) { |
| 353 if (will_operation) { |
| 354 ASSERT_TRUE(quota_file_io_->WillSetLength( |
| 355 length, |
| 356 callback_factory_.NewCallback( |
| 357 &QuotaFileIOTest::DidSetLength))); |
| 358 } else { |
| 359 ASSERT_TRUE(quota_file_io_->SetLength( |
| 360 length, |
| 361 callback_factory_.NewCallback( |
| 362 &QuotaFileIOTest::DidSetLength))); |
| 363 } |
| 364 } |
| 365 |
| 366 void DidWrite(PlatformFileError status, int bytes_written) { |
| 367 status_.push_back(status); |
| 368 bytes_written_.push_back(bytes_written); |
| 369 } |
| 370 |
| 371 void DidSetLength(PlatformFileError status) { |
| 372 status_.push_back(status); |
| 373 } |
| 374 |
| 375 size_t num_results() const { return status_.size(); } |
| 376 const std::deque<int>& bytes_written() const { return bytes_written_; } |
| 377 const std::deque<PlatformFileError>& status() const { return status_; } |
| 378 |
| 379 void reset_results() { |
| 380 bytes_written_.clear(); |
| 381 status_.clear(); |
| 382 } |
| 383 |
| 384 void pop_result() { |
| 385 bytes_written_.pop_front(); |
| 386 status_.pop_front(); |
| 387 } |
| 388 |
| 389 void ReadPlatformFile(std::string* data) { |
| 390 data->clear(); |
| 391 char buf[256]; |
| 392 int32_t read_offset = 0; |
| 393 for (;;) { |
| 394 int rv = base::ReadPlatformFile(file_, read_offset, buf, sizeof(buf)); |
| 395 ASSERT_GE(rv, 0); |
| 396 if (rv == 0) |
| 397 break; |
| 398 read_offset += rv; |
| 399 data->append(buf, rv); |
| 400 } |
| 401 } |
| 402 |
| 403 int64_t GetPlatformFileSize() { |
| 404 base::PlatformFileInfo info; |
| 405 EXPECT_TRUE(base::GetPlatformFileInfo(file_, &info)); |
| 406 return info.size; |
| 407 } |
| 408 |
| 409 void SetPlatformFileSize(int64_t length) { |
| 410 EXPECT_TRUE(base::TruncatePlatformFile(file_, length)); |
| 411 } |
| 412 |
| 413 private: |
| 414 ScopedTempDir dir_; |
| 415 PlatformFile file_; |
| 416 scoped_ptr<QuotaFileIO> quota_file_io_; |
| 417 std::deque<int> bytes_written_; |
| 418 std::deque<PlatformFileError> status_; |
| 419 base::ScopedCallbackFactory<QuotaFileIOTest> callback_factory_; |
| 420 }; |
| 421 |
| 422 TEST_F(QuotaFileIOTest, Write) { |
| 423 WriteTestBody(false); |
| 424 } |
| 425 |
| 426 TEST_F(QuotaFileIOTest, WillWrite) { |
| 427 WriteTestBody(true); |
| 428 } |
| 429 |
| 430 TEST_F(QuotaFileIOTest, SetLength) { |
| 431 SetLengthTestBody(false); |
| 432 } |
| 433 |
| 434 TEST_F(QuotaFileIOTest, WillSetLength) { |
| 435 SetLengthTestBody(true); |
| 436 } |
| 437 |
| 438 TEST_F(QuotaFileIOTest, ParallelWrites) { |
| 439 quota_plugin_delegate()->set_available_space(22); |
| 440 std::string read_buffer; |
| 441 |
| 442 const std::string data1[] = { |
| 443 std::string("12345678"), |
| 444 std::string("55555"), |
| 445 std::string("9012345"), |
| 446 }; |
| 447 Write(0, data1[0], false); |
| 448 Write(5, data1[1], false); |
| 449 Write(8, data1[2], false); |
| 450 MessageLoop::current()->RunAllPending(); |
| 451 |
| 452 ASSERT_EQ(ARRAYSIZE_UNSAFE(data1), num_results()); |
| 453 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data1); ++i) { |
| 454 EXPECT_EQ(static_cast<int>(data1[i].size()), bytes_written().front()); |
| 455 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front()); |
| 456 pop_result(); |
| 457 } |
| 458 |
| 459 EXPECT_EQ(22 - 15, quota_plugin_delegate()->available_space()); |
| 460 EXPECT_EQ(15, GetPlatformFileSize()); |
| 461 ReadPlatformFile(&read_buffer); |
| 462 EXPECT_EQ("123455559012345", read_buffer); |
| 463 |
| 464 // The second write will fail for quota error. |
| 465 const std::string data2[] = { |
| 466 std::string("33"), |
| 467 std::string("XXXX"), |
| 468 }; |
| 469 Write(2, data2[0], false); |
| 470 Write(20, data2[1], false); |
| 471 MessageLoop::current()->RunAllPending(); |
| 472 |
| 473 ASSERT_EQ(ARRAYSIZE_UNSAFE(data2), num_results()); |
| 474 EXPECT_EQ(static_cast<int>(data2[0].size()), bytes_written().front()); |
| 475 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front()); |
| 476 pop_result(); |
| 477 EXPECT_EQ(0, bytes_written().front()); |
| 478 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, status().front()); |
| 479 pop_result(); |
| 480 |
| 481 EXPECT_EQ(22 - 15, quota_plugin_delegate()->available_space()); |
| 482 EXPECT_EQ(15, GetPlatformFileSize()); |
| 483 ReadPlatformFile(&read_buffer); |
| 484 EXPECT_EQ("123355559012345", read_buffer); |
| 485 } |
| 486 |
| 487 } // namespace ppapi |
| 488 } // namespace webkit |
| OLD | NEW |