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

Side by Side Diff: content/browser/fileapi/blob_url_request_job_unittest.cc

Issue 1498003003: Remove kint64max. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: INT64_MAX Created 5 years 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
OLDNEW
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 "base/basictypes.h" 5 #include <stdint.h>
6
7 #include <limits>
8
6 #include "base/bind.h" 9 #include "base/bind.h"
7 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
8 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h" 12 #include "base/files/scoped_temp_dir.h"
10 #include "base/macros.h" 13 #include "base/macros.h"
11 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
13 #include "base/numerics/safe_conversions.h" 16 #include "base/numerics/safe_conversions.h"
14 #include "base/run_loop.h" 17 #include "base/run_loop.h"
15 #include "base/thread_task_runner_handle.h" 18 #include "base/thread_task_runner_handle.h"
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 220
218 void OnValidateFileSystem(const GURL& root, 221 void OnValidateFileSystem(const GURL& root,
219 const std::string& name, 222 const std::string& name,
220 base::File::Error result) { 223 base::File::Error result) {
221 ASSERT_EQ(base::File::FILE_OK, result); 224 ASSERT_EQ(base::File::FILE_OK, result);
222 ASSERT_TRUE(root.is_valid()); 225 ASSERT_TRUE(root.is_valid());
223 file_system_root_url_ = root; 226 file_system_root_url_ = root;
224 } 227 }
225 228
226 void TestSuccessNonrangeRequest(const std::string& expected_response, 229 void TestSuccessNonrangeRequest(const std::string& expected_response,
227 int64 expected_content_length) { 230 int64_t expected_content_length) {
228 expected_status_code_ = 200; 231 expected_status_code_ = 200;
229 expected_response_ = expected_response; 232 expected_response_ = expected_response;
230 TestRequest("GET", net::HttpRequestHeaders()); 233 TestRequest("GET", net::HttpRequestHeaders());
231 EXPECT_EQ(expected_content_length, 234 EXPECT_EQ(expected_content_length,
232 request_->response_headers()->GetContentLength()); 235 request_->response_headers()->GetContentLength());
233 } 236 }
234 237
235 void TestErrorRequest(int expected_status_code) { 238 void TestErrorRequest(int expected_status_code) {
236 expected_status_code_ = expected_status_code; 239 expected_status_code_ = expected_status_code;
237 expected_response_ = ""; 240 expected_response_ = "";
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 288
286 storage::BlobDataHandle* GetHandleFromBuilder() { 289 storage::BlobDataHandle* GetHandleFromBuilder() {
287 if (!blob_handle_) { 290 if (!blob_handle_) {
288 blob_handle_ = blob_context_.AddFinishedBlob(blob_data_.get()).Pass(); 291 blob_handle_ = blob_context_.AddFinishedBlob(blob_data_.get()).Pass();
289 } 292 }
290 return blob_handle_.get(); 293 return blob_handle_.get();
291 } 294 }
292 295
293 // This only works if all the Blob items have a definite pre-computed length. 296 // This only works if all the Blob items have a definite pre-computed length.
294 // Otherwise, this will fail a CHECK. 297 // Otherwise, this will fail a CHECK.
295 int64 GetTotalBlobLength() { 298 int64_t GetTotalBlobLength() {
296 int64 total = 0; 299 int64_t total = 0;
297 scoped_ptr<BlobDataSnapshot> data = 300 scoped_ptr<BlobDataSnapshot> data =
298 GetHandleFromBuilder()->CreateSnapshot(); 301 GetHandleFromBuilder()->CreateSnapshot();
299 const auto& items = data->items(); 302 const auto& items = data->items();
300 for (const auto& item : items) { 303 for (const auto& item : items) {
301 int64 length = base::checked_cast<int64>(item->length()); 304 int64_t length = base::checked_cast<int64_t>(item->length());
302 CHECK(length <= kint64max - total); 305 CHECK(length <= std::numeric_limits<int64_t>::max() - total);
303 total += length; 306 total += length;
304 } 307 }
305 return total; 308 return total;
306 } 309 }
307 310
308 protected: 311 protected:
309 base::ScopedTempDir temp_dir_; 312 base::ScopedTempDir temp_dir_;
310 base::FilePath temp_file1_; 313 base::FilePath temp_file1_;
311 base::FilePath temp_file2_; 314 base::FilePath temp_file2_;
312 base::Time temp_file_modification_time1_; 315 base::Time temp_file_modification_time1_;
(...skipping 22 matching lines...) Expand all
335 int expected_status_code_; 338 int expected_status_code_;
336 std::string expected_response_; 339 std::string expected_response_;
337 }; 340 };
338 341
339 TEST_F(BlobURLRequestJobTest, TestGetSimpleDataRequest) { 342 TEST_F(BlobURLRequestJobTest, TestGetSimpleDataRequest) {
340 blob_data_->AppendData(kTestData1); 343 blob_data_->AppendData(kTestData1);
341 TestSuccessNonrangeRequest(kTestData1, arraysize(kTestData1) - 1); 344 TestSuccessNonrangeRequest(kTestData1, arraysize(kTestData1) - 1);
342 } 345 }
343 346
344 TEST_F(BlobURLRequestJobTest, TestGetSimpleFileRequest) { 347 TEST_F(BlobURLRequestJobTest, TestGetSimpleFileRequest) {
345 blob_data_->AppendFile(temp_file1_, 0, kuint64max, base::Time()); 348 blob_data_->AppendFile(temp_file1_, 0, std::numeric_limits<uint64_t>::max(),
349 base::Time());
346 TestSuccessNonrangeRequest(kTestFileData1, arraysize(kTestFileData1) - 1); 350 TestSuccessNonrangeRequest(kTestFileData1, arraysize(kTestFileData1) - 1);
347 } 351 }
348 352
349 TEST_F(BlobURLRequestJobTest, TestGetLargeFileRequest) { 353 TEST_F(BlobURLRequestJobTest, TestGetLargeFileRequest) {
350 base::FilePath large_temp_file = 354 base::FilePath large_temp_file =
351 temp_dir_.path().AppendASCII("LargeBlob.dat"); 355 temp_dir_.path().AppendASCII("LargeBlob.dat");
352 std::string large_data; 356 std::string large_data;
353 large_data.reserve(kBufferSize * 5); 357 large_data.reserve(kBufferSize * 5);
354 for (int i = 0; i < kBufferSize * 5; ++i) 358 for (int i = 0; i < kBufferSize * 5; ++i)
355 large_data.append(1, static_cast<char>(i % 256)); 359 large_data.append(1, static_cast<char>(i % 256));
356 ASSERT_EQ(static_cast<int>(large_data.size()), 360 ASSERT_EQ(static_cast<int>(large_data.size()),
357 base::WriteFile(large_temp_file, large_data.data(), 361 base::WriteFile(large_temp_file, large_data.data(),
358 large_data.size())); 362 large_data.size()));
359 blob_data_->AppendFile(large_temp_file, 0, kuint64max, base::Time()); 363 blob_data_->AppendFile(large_temp_file, 0,
364 std::numeric_limits<uint64_t>::max(), base::Time());
360 TestSuccessNonrangeRequest(large_data, large_data.size()); 365 TestSuccessNonrangeRequest(large_data, large_data.size());
361 } 366 }
362 367
363 TEST_F(BlobURLRequestJobTest, TestGetNonExistentFileRequest) { 368 TEST_F(BlobURLRequestJobTest, TestGetNonExistentFileRequest) {
364 base::FilePath non_existent_file = 369 base::FilePath non_existent_file =
365 temp_file1_.InsertBeforeExtension(FILE_PATH_LITERAL("-na")); 370 temp_file1_.InsertBeforeExtension(FILE_PATH_LITERAL("-na"));
366 blob_data_->AppendFile(non_existent_file, 0, kuint64max, base::Time()); 371 blob_data_->AppendFile(non_existent_file, 0,
372 std::numeric_limits<uint64_t>::max(), base::Time());
367 TestErrorRequest(404); 373 TestErrorRequest(404);
368 } 374 }
369 375
370 TEST_F(BlobURLRequestJobTest, TestGetChangedFileRequest) { 376 TEST_F(BlobURLRequestJobTest, TestGetChangedFileRequest) {
371 base::Time old_time = 377 base::Time old_time =
372 temp_file_modification_time1_ - base::TimeDelta::FromSeconds(10); 378 temp_file_modification_time1_ - base::TimeDelta::FromSeconds(10);
373 blob_data_->AppendFile(temp_file1_, 0, 3, old_time); 379 blob_data_->AppendFile(temp_file1_, 0, 3, old_time);
374 TestErrorRequest(404); 380 TestErrorRequest(404);
375 } 381 }
376 382
377 TEST_F(BlobURLRequestJobTest, TestGetSlicedFileRequest) { 383 TEST_F(BlobURLRequestJobTest, TestGetSlicedFileRequest) {
378 blob_data_->AppendFile(temp_file1_, 2, 4, temp_file_modification_time1_); 384 blob_data_->AppendFile(temp_file1_, 2, 4, temp_file_modification_time1_);
379 std::string result(kTestFileData1 + 2, 4); 385 std::string result(kTestFileData1 + 2, 4);
380 TestSuccessNonrangeRequest(result, 4); 386 TestSuccessNonrangeRequest(result, 4);
381 } 387 }
382 388
383 TEST_F(BlobURLRequestJobTest, TestGetSimpleFileSystemFileRequest) { 389 TEST_F(BlobURLRequestJobTest, TestGetSimpleFileSystemFileRequest) {
384 SetUpFileSystem(); 390 SetUpFileSystem();
385 blob_data_->AppendFileSystemFile(temp_file_system_file1_, 0, kuint64max, 391 blob_data_->AppendFileSystemFile(temp_file_system_file1_, 0,
392 std::numeric_limits<uint64_t>::max(),
386 base::Time()); 393 base::Time());
387 TestSuccessNonrangeRequest(kTestFileSystemFileData1, 394 TestSuccessNonrangeRequest(kTestFileSystemFileData1,
388 arraysize(kTestFileSystemFileData1) - 1); 395 arraysize(kTestFileSystemFileData1) - 1);
389 } 396 }
390 397
391 TEST_F(BlobURLRequestJobTest, TestGetLargeFileSystemFileRequest) { 398 TEST_F(BlobURLRequestJobTest, TestGetLargeFileSystemFileRequest) {
392 SetUpFileSystem(); 399 SetUpFileSystem();
393 std::string large_data; 400 std::string large_data;
394 large_data.reserve(kBufferSize * 5); 401 large_data.reserve(kBufferSize * 5);
395 for (int i = 0; i < kBufferSize * 5; ++i) 402 for (int i = 0; i < kBufferSize * 5; ++i)
396 large_data.append(1, static_cast<char>(i % 256)); 403 large_data.append(1, static_cast<char>(i % 256));
397 404
398 const char kFilename[] = "LargeBlob.dat"; 405 const char kFilename[] = "LargeBlob.dat";
399 WriteFileSystemFile(kFilename, large_data.data(), large_data.size(), NULL); 406 WriteFileSystemFile(kFilename, large_data.data(), large_data.size(), NULL);
400 407
401 blob_data_->AppendFileSystemFile(GetFileSystemURL(kFilename), 0, kuint64max, 408 blob_data_->AppendFileSystemFile(GetFileSystemURL(kFilename), 0,
409 std::numeric_limits<uint64_t>::max(),
402 base::Time()); 410 base::Time());
403 TestSuccessNonrangeRequest(large_data, large_data.size()); 411 TestSuccessNonrangeRequest(large_data, large_data.size());
404 } 412 }
405 413
406 TEST_F(BlobURLRequestJobTest, TestGetNonExistentFileSystemFileRequest) { 414 TEST_F(BlobURLRequestJobTest, TestGetNonExistentFileSystemFileRequest) {
407 SetUpFileSystem(); 415 SetUpFileSystem();
408 GURL non_existent_file = GetFileSystemURL("non-existent.dat"); 416 GURL non_existent_file = GetFileSystemURL("non-existent.dat");
409 blob_data_->AppendFileSystemFile(non_existent_file, 0, kuint64max, 417 blob_data_->AppendFileSystemFile(
410 base::Time()); 418 non_existent_file, 0, std::numeric_limits<uint64_t>::max(), base::Time());
411 TestErrorRequest(404); 419 TestErrorRequest(404);
412 } 420 }
413 421
414 TEST_F(BlobURLRequestJobTest, TestGetInvalidFileSystemFileRequest) { 422 TEST_F(BlobURLRequestJobTest, TestGetInvalidFileSystemFileRequest) {
415 SetUpFileSystem(); 423 SetUpFileSystem();
416 GURL invalid_file; 424 GURL invalid_file;
417 blob_data_->AppendFileSystemFile(invalid_file, 0, kuint64max, base::Time()); 425 blob_data_->AppendFileSystemFile(
426 invalid_file, 0, std::numeric_limits<uint64_t>::max(), base::Time());
418 TestErrorRequest(500); 427 TestErrorRequest(500);
419 } 428 }
420 429
421 TEST_F(BlobURLRequestJobTest, TestGetChangedFileSystemFileRequest) { 430 TEST_F(BlobURLRequestJobTest, TestGetChangedFileSystemFileRequest) {
422 SetUpFileSystem(); 431 SetUpFileSystem();
423 base::Time old_time = 432 base::Time old_time =
424 temp_file_system_file_modification_time1_ - 433 temp_file_system_file_modification_time1_ -
425 base::TimeDelta::FromSeconds(10); 434 base::TimeDelta::FromSeconds(10);
426 blob_data_->AppendFileSystemFile(temp_file_system_file1_, 0, 3, old_time); 435 blob_data_->AppendFileSystemFile(temp_file_system_file1_, 0, 3, old_time);
427 TestErrorRequest(404); 436 TestErrorRequest(404);
(...skipping 28 matching lines...) Expand all
456 BuildComplicatedData(&result); 465 BuildComplicatedData(&result);
457 net::HttpRequestHeaders extra_headers; 466 net::HttpRequestHeaders extra_headers;
458 extra_headers.SetHeader(net::HttpRequestHeaders::kRange, 467 extra_headers.SetHeader(net::HttpRequestHeaders::kRange,
459 net::HttpByteRange::Bounded(5, 10).GetHeaderValue()); 468 net::HttpByteRange::Bounded(5, 10).GetHeaderValue());
460 expected_status_code_ = 206; 469 expected_status_code_ = 206;
461 expected_response_ = result.substr(5, 10 - 5 + 1); 470 expected_response_ = result.substr(5, 10 - 5 + 1);
462 TestRequest("GET", extra_headers); 471 TestRequest("GET", extra_headers);
463 472
464 EXPECT_EQ(6, request_->response_headers()->GetContentLength()); 473 EXPECT_EQ(6, request_->response_headers()->GetContentLength());
465 474
466 int64 first = 0, last = 0, length = 0; 475 int64_t first = 0, last = 0, length = 0;
467 EXPECT_TRUE( 476 EXPECT_TRUE(
468 request_->response_headers()->GetContentRange(&first, &last, &length)); 477 request_->response_headers()->GetContentRange(&first, &last, &length));
469 EXPECT_EQ(5, first); 478 EXPECT_EQ(5, first);
470 EXPECT_EQ(10, last); 479 EXPECT_EQ(10, last);
471 EXPECT_EQ(GetTotalBlobLength(), length); 480 EXPECT_EQ(GetTotalBlobLength(), length);
472 } 481 }
473 482
474 TEST_F(BlobURLRequestJobTest, TestGetRangeRequest2) { 483 TEST_F(BlobURLRequestJobTest, TestGetRangeRequest2) {
475 SetUpFileSystem(); 484 SetUpFileSystem();
476 std::string result; 485 std::string result;
477 BuildComplicatedData(&result); 486 BuildComplicatedData(&result);
478 net::HttpRequestHeaders extra_headers; 487 net::HttpRequestHeaders extra_headers;
479 extra_headers.SetHeader(net::HttpRequestHeaders::kRange, 488 extra_headers.SetHeader(net::HttpRequestHeaders::kRange,
480 net::HttpByteRange::Suffix(10).GetHeaderValue()); 489 net::HttpByteRange::Suffix(10).GetHeaderValue());
481 expected_status_code_ = 206; 490 expected_status_code_ = 206;
482 expected_response_ = result.substr(result.length() - 10); 491 expected_response_ = result.substr(result.length() - 10);
483 TestRequest("GET", extra_headers); 492 TestRequest("GET", extra_headers);
484 493
485 EXPECT_EQ(10, request_->response_headers()->GetContentLength()); 494 EXPECT_EQ(10, request_->response_headers()->GetContentLength());
486 495
487 int64 total = GetTotalBlobLength(); 496 int64_t total = GetTotalBlobLength();
488 int64 first = 0, last = 0, length = 0; 497 int64_t first = 0, last = 0, length = 0;
489 EXPECT_TRUE( 498 EXPECT_TRUE(
490 request_->response_headers()->GetContentRange(&first, &last, &length)); 499 request_->response_headers()->GetContentRange(&first, &last, &length));
491 EXPECT_EQ(total - 10, first); 500 EXPECT_EQ(total - 10, first);
492 EXPECT_EQ(total - 1, last); 501 EXPECT_EQ(total - 1, last);
493 EXPECT_EQ(total, length); 502 EXPECT_EQ(total, length);
494 } 503 }
495 504
496 TEST_F(BlobURLRequestJobTest, TestGetRangeRequest3) { 505 TEST_F(BlobURLRequestJobTest, TestGetRangeRequest3) {
497 SetUpFileSystem(); 506 SetUpFileSystem();
498 std::string result; 507 std::string result;
499 BuildComplicatedData(&result); 508 BuildComplicatedData(&result);
500 net::HttpRequestHeaders extra_headers; 509 net::HttpRequestHeaders extra_headers;
501 extra_headers.SetHeader(net::HttpRequestHeaders::kRange, 510 extra_headers.SetHeader(net::HttpRequestHeaders::kRange,
502 net::HttpByteRange::Bounded(0, 2).GetHeaderValue()); 511 net::HttpByteRange::Bounded(0, 2).GetHeaderValue());
503 expected_status_code_ = 206; 512 expected_status_code_ = 206;
504 expected_response_ = result.substr(0, 3); 513 expected_response_ = result.substr(0, 3);
505 TestRequest("GET", extra_headers); 514 TestRequest("GET", extra_headers);
506 515
507 EXPECT_EQ(3, request_->response_headers()->GetContentLength()); 516 EXPECT_EQ(3, request_->response_headers()->GetContentLength());
508 517
509 int64 first = 0, last = 0, length = 0; 518 int64_t first = 0, last = 0, length = 0;
510 EXPECT_TRUE( 519 EXPECT_TRUE(
511 request_->response_headers()->GetContentRange(&first, &last, &length)); 520 request_->response_headers()->GetContentRange(&first, &last, &length));
512 EXPECT_EQ(0, first); 521 EXPECT_EQ(0, first);
513 EXPECT_EQ(2, last); 522 EXPECT_EQ(2, last);
514 EXPECT_EQ(GetTotalBlobLength(), length); 523 EXPECT_EQ(GetTotalBlobLength(), length);
515 } 524 }
516 525
517 TEST_F(BlobURLRequestJobTest, TestExtraHeaders) { 526 TEST_F(BlobURLRequestJobTest, TestExtraHeaders) {
518 blob_data_->set_content_type(kTestContentType); 527 blob_data_->set_content_type(kTestContentType);
519 blob_data_->set_content_disposition(kTestContentDisposition); 528 blob_data_->set_content_disposition(kTestContentDisposition);
520 blob_data_->AppendData(kTestData1); 529 blob_data_->AppendData(kTestData1);
521 expected_status_code_ = 200; 530 expected_status_code_ = 200;
522 expected_response_ = kTestData1; 531 expected_response_ = kTestData1;
523 TestRequest("GET", net::HttpRequestHeaders()); 532 TestRequest("GET", net::HttpRequestHeaders());
524 533
525 std::string content_type; 534 std::string content_type;
526 EXPECT_TRUE(request_->response_headers()->GetMimeType(&content_type)); 535 EXPECT_TRUE(request_->response_headers()->GetMimeType(&content_type));
527 EXPECT_EQ(kTestContentType, content_type); 536 EXPECT_EQ(kTestContentType, content_type);
528 void* iter = NULL; 537 void* iter = NULL;
529 std::string content_disposition; 538 std::string content_disposition;
530 EXPECT_TRUE(request_->response_headers()->EnumerateHeader( 539 EXPECT_TRUE(request_->response_headers()->EnumerateHeader(
531 &iter, "Content-Disposition", &content_disposition)); 540 &iter, "Content-Disposition", &content_disposition));
532 EXPECT_EQ(kTestContentDisposition, content_disposition); 541 EXPECT_EQ(kTestContentDisposition, content_disposition);
533 } 542 }
534 543
535 } // namespace content 544 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698