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