1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/drive/drive_uploader.h"
#include <algorithm>
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/thread_task_runner_handle.h"
#include "base/values.h"
#include "components/drive/service/dummy_drive_service.h"
#include "google_apis/drive/drive_api_parser.h"
#include "google_apis/drive/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
using google_apis::CancelCallback;
using google_apis::FileResource;
using google_apis::DriveApiErrorCode;
using google_apis::DRIVE_NO_CONNECTION;
using google_apis::DRIVE_OTHER_ERROR;
using google_apis::HTTP_CONFLICT;
using google_apis::HTTP_CREATED;
using google_apis::HTTP_NOT_FOUND;
using google_apis::HTTP_PRECONDITION;
using google_apis::HTTP_RESUME_INCOMPLETE;
using google_apis::HTTP_SUCCESS;
using google_apis::InitiateUploadCallback;
using google_apis::ProgressCallback;
using google_apis::UploadRangeResponse;
using google_apis::drive::UploadRangeCallback;
namespace test_util = google_apis::test_util;
namespace drive {
namespace {
const char kTestDummyMd5[] = "dummy_md5";
const char kTestDocumentTitle[] = "Hello world";
const char kTestInitiateUploadParentResourceId[] = "parent_resource_id";
const char kTestInitiateUploadResourceId[] = "resource_id";
const char kTestMimeType[] = "text/plain";
const char kTestUploadNewFileURL[] = "http://test/upload_location/new_file";
const char kTestUploadExistingFileURL[] =
"http://test/upload_location/existing_file";
const int64 kUploadChunkSize = 1024 * 1024 * 1024;
const char kTestETag[] = "test_etag";
CancelCallback SendMultipartUploadResult(
DriveApiErrorCode response_code,
int64 content_length,
const google_apis::FileResourceCallback& callback,
const google_apis::ProgressCallback& progress_callback) {
// Callback progress
if (!progress_callback.is_null()) {
// For the testing purpose, it always notifies the progress at the end of
// whole file uploading.
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::Bind(progress_callback, content_length, content_length));
}
// MultipartUploadXXXFile is an asynchronous function, so don't callback
// directly.
scoped_ptr<FileResource> entry;
entry.reset(new FileResource);
entry->set_md5_checksum(kTestDummyMd5);
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(callback, response_code, base::Passed(&entry)));
return CancelCallback();
}
// Mock DriveService that verifies if the uploaded content matches the preset
// expectation.
class MockDriveServiceWithUploadExpectation : public DummyDriveService {
public:
// Sets up an expected upload content. InitiateUpload and ResumeUpload will
// verify that the specified data is correctly uploaded.
MockDriveServiceWithUploadExpectation(
const base::FilePath& expected_upload_file,
int64 expected_content_length)
: expected_upload_file_(expected_upload_file),
expected_content_length_(expected_content_length),
received_bytes_(0),
resume_upload_call_count_(0),
multipart_upload_call_count_(0) {}
int64 received_bytes() const { return received_bytes_; }
void set_received_bytes(int64 received_bytes) {
received_bytes_ = received_bytes;
}
int64 resume_upload_call_count() const { return resume_upload_call_count_; }
int64 multipart_upload_call_count() const {
return multipart_upload_call_count_;
}
private:
// DriveServiceInterface overrides.
// Handles a request for obtaining an upload location URL.
CancelCallback InitiateUploadNewFile(
const std::string& content_type,
int64 content_length,
const std::string& parent_resource_id,
const std::string& title,
const UploadNewFileOptions& options,
const InitiateUploadCallback& callback) override {
EXPECT_EQ(kTestDocumentTitle, title);
EXPECT_EQ(kTestMimeType, content_type);
EXPECT_EQ(expected_content_length_, content_length);
EXPECT_EQ(kTestInitiateUploadParentResourceId, parent_resource_id);
// Calls back the upload URL for subsequent ResumeUpload requests.
// InitiateUpload is an asynchronous function, so don't callback directly.
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadNewFileURL)));
return CancelCallback();
}
CancelCallback InitiateUploadExistingFile(
const std::string& content_type,
int64 content_length,
const std::string& resource_id,
const UploadExistingFileOptions& options,
const InitiateUploadCallback& callback) override {
EXPECT_EQ(kTestMimeType, content_type);
EXPECT_EQ(expected_content_length_, content_length);
EXPECT_EQ(kTestInitiateUploadResourceId, resource_id);
if (!options.etag.empty() && options.etag != kTestETag) {
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
base::Bind(callback, HTTP_PRECONDITION, GURL()));
return CancelCallback();
}
// Calls back the upload URL for subsequent ResumeUpload requests.
// InitiateUpload is an asynchronous function, so don't callback directly.
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadExistingFileURL)));
return CancelCallback();
}
// Handles a request for uploading a chunk of bytes.
CancelCallback ResumeUpload(
const GURL& upload_location,
int64 start_position,
int64 end_position,
int64 content_length,
const std::string& content_type,
const base::FilePath& local_file_path,
const UploadRangeCallback& callback,
const ProgressCallback& progress_callback) override {
// The upload range should start from the current first unreceived byte.
EXPECT_EQ(received_bytes_, start_position);
EXPECT_EQ(expected_upload_file_, local_file_path);
// The upload data must be split into 512KB chunks.
const int64 expected_chunk_end =
std::min(received_bytes_ + kUploadChunkSize, expected_content_length_);
EXPECT_EQ(expected_chunk_end, end_position);
// The upload URL returned by InitiateUpload() must be used.
EXPECT_TRUE(GURL(kTestUploadNewFileURL) == upload_location ||
GURL(kTestUploadExistingFileURL) == upload_location);
// Other parameters should be the exact values passed to DriveUploader.
EXPECT_EQ(expected_content_length_, content_length);
EXPECT_EQ(kTestMimeType, content_type);
// Update the internal status of the current upload session.
resume_upload_call_count_++;
received_bytes_ = end_position;
// Callback progress
if (!progress_callback.is_null()) {
// For the testing purpose, it always notifies the progress at the end of
// each chunk uploading.
int64 chunk_size = end_position - start_position;
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
base::Bind(progress_callback, chunk_size, chunk_size));
}
SendUploadRangeResponse(upload_location, callback);
return CancelCallback();
}
// Handles a request to fetch the current upload status.
CancelCallback GetUploadStatus(const GURL& upload_location,
int64 content_length,
const UploadRangeCallback& callback) override {
EXPECT_EQ(expected_content_length_, content_length);
// The upload URL returned by InitiateUpload() must be used.
EXPECT_TRUE(GURL(kTestUploadNewFileURL) == upload_location ||
GURL(kTestUploadExistingFileURL) == upload_location);
SendUploadRangeResponse(upload_location, callback);
return CancelCallback();
}
// Runs |callback| with the current upload status.
void SendUploadRangeResponse(const GURL& upload_location,
const UploadRangeCallback& callback) {
// Callback with response.
UploadRangeResponse response;
scoped_ptr<FileResource> entry;
if (received_bytes_ == expected_content_length_) {
DriveApiErrorCode response_code =
upload_location == GURL(kTestUploadNewFileURL) ?
HTTP_CREATED : HTTP_SUCCESS;
response = UploadRangeResponse(response_code, -1, -1);
entry.reset(new FileResource);
entry->set_md5_checksum(kTestDummyMd5);
} else {
response = UploadRangeResponse(
HTTP_RESUME_INCOMPLETE, 0, received_bytes_);
}
// ResumeUpload is an asynchronous function, so don't callback directly.
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
base::Bind(callback, response, base::Passed(&entry)));
}
CancelCallback MultipartUploadNewFile(
const std::string& content_type,
int64 content_length,
const std::string& parent_resource_id,
const std::string& title,
const base::FilePath& local_file_path,
const UploadNewFileOptions& options,
const google_apis::FileResourceCallback& callback,
const google_apis::ProgressCallback& progress_callback) override {
EXPECT_EQ(kTestMimeType, content_type);
EXPECT_EQ(expected_content_length_, content_length);
EXPECT_EQ(kTestInitiateUploadParentResourceId, parent_resource_id);
EXPECT_EQ(kTestDocumentTitle, title);
EXPECT_EQ(expected_upload_file_, local_file_path);
received_bytes_ = content_length;
multipart_upload_call_count_++;
return SendMultipartUploadResult(HTTP_CREATED, content_length, callback,
progress_callback);
}
CancelCallback MultipartUploadExistingFile(
const std::string& content_type,
int64 content_length,
const std::string& resource_id,
const base::FilePath& local_file_path,
const UploadExistingFileOptions& options,
const google_apis::FileResourceCallback& callback,
const google_apis::ProgressCallback& progress_callback) override {
EXPECT_EQ(kTestMimeType, content_type);
EXPECT_EQ(expected_content_length_, content_length);
EXPECT_EQ(kTestInitiateUploadResourceId, resource_id);
EXPECT_EQ(expected_upload_file_, local_file_path);
if (!options.etag.empty() && options.etag != kTestETag) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::Bind(callback, HTTP_PRECONDITION,
base::Passed(make_scoped_ptr<FileResource>(NULL))));
return CancelCallback();
}
received_bytes_ = content_length;
multipart_upload_call_count_++;
return SendMultipartUploadResult(HTTP_SUCCESS, content_length, callback,
progress_callback);
}
const base::FilePath expected_upload_file_;
const int64 expected_content_length_;
int64 received_bytes_;
int64 resume_upload_call_count_;
int64 multipart_upload_call_count_;
};
// Mock DriveService that returns a failure at InitiateUpload().
class MockDriveServiceNoConnectionAtInitiate : public DummyDriveService {
// Returns error.
CancelCallback InitiateUploadNewFile(
const std::string& content_type,
int64 content_length,
const std::string& parent_resource_id,
const std::string& title,
const UploadNewFileOptions& options,
const InitiateUploadCallback& callback) override {
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
base::Bind(callback, DRIVE_NO_CONNECTION, GURL()));
return CancelCallback();
}
CancelCallback InitiateUploadExistingFile(
const std::string& content_type,
int64 content_length,
const std::string& resource_id,
const UploadExistingFileOptions& options,
const InitiateUploadCallback& callback) override {
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
base::Bind(callback, DRIVE_NO_CONNECTION, GURL()));
return CancelCallback();
}
// Should not be used.
CancelCallback ResumeUpload(
const GURL& upload_url,
int64 start_position,
int64 end_position,
int64 content_length,
const std::string& content_type,
const base::FilePath& local_file_path,
const UploadRangeCallback& callback,
const ProgressCallback& progress_callback) override {
NOTREACHED();
return CancelCallback();
}
CancelCallback MultipartUploadNewFile(
const std::string& content_type,
int64 content_length,
const std::string& parent_resource_id,
const std::string& title,
const base::FilePath& local_file_path,
const UploadNewFileOptions& options,
const google_apis::FileResourceCallback& callback,
const google_apis::ProgressCallback& progress_callback) override {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::Bind(callback, DRIVE_NO_CONNECTION,
base::Passed(make_scoped_ptr<FileResource>(NULL))));
return CancelCallback();
}
CancelCallback MultipartUploadExistingFile(
const std::string& content_type,
int64 content_length,
const std::string& resource_id,
const base::FilePath& local_file_path,
const UploadExistingFileOptions& options,
const google_apis::FileResourceCallback& callback,
const google_apis::ProgressCallback& progress_callback) override {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::Bind(callback, DRIVE_NO_CONNECTION,
base::Passed(make_scoped_ptr<FileResource>(NULL))));
return CancelCallback();
}
};
// Mock DriveService that returns a failure at ResumeUpload().
class MockDriveServiceNoConnectionAtResume : public DummyDriveService {
// Succeeds and returns an upload location URL.
CancelCallback InitiateUploadNewFile(
const std::string& content_type,
int64 content_length,
const std::string& parent_resource_id,
const std::string& title,
const UploadNewFileOptions& options,
const InitiateUploadCallback& callback) override {
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadNewFileURL)));
return CancelCallback();
}
CancelCallback InitiateUploadExistingFile(
const std::string& content_type,
int64 content_length,
const std::string& resource_id,
const UploadExistingFileOptions& options,
const InitiateUploadCallback& callback) override {
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadExistingFileURL)));
return CancelCallback();
}
// Returns error.
CancelCallback ResumeUpload(
const GURL& upload_url,
int64 start_position,
int64 end_position,
int64 content_length,
const std::string& content_type,
const base::FilePath& local_file_path,
const UploadRangeCallback& callback,
const ProgressCallback& progress_callback) override {
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
base::Bind(callback,
UploadRangeResponse(DRIVE_NO_CONNECTION, -1, -1),
base::Passed(scoped_ptr<FileResource>())));
return CancelCallback();
}
};
// Mock DriveService that returns a failure at GetUploadStatus().
class MockDriveServiceNoConnectionAtGetUploadStatus : public DummyDriveService {
// Returns error.
CancelCallback GetUploadStatus(const GURL& upload_url,
int64 content_length,
const UploadRangeCallback& callback) override {
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
base::Bind(callback,
UploadRangeResponse(DRIVE_NO_CONNECTION, -1, -1),
base::Passed(scoped_ptr<FileResource>())));
return CancelCallback();
}
};
class DriveUploaderTest : public testing::Test {
public:
void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }
protected:
base::MessageLoop message_loop_;
base::ScopedTempDir temp_dir_;
};
} // namespace
TEST_F(DriveUploaderTest, UploadExisting0KB) {
base::FilePath local_path;
std::string data;
ASSERT_TRUE(test_util::CreateFileOfSpecifiedSize(
temp_dir_.path(), 0, &local_path, &data));
DriveApiErrorCode error = DRIVE_OTHER_ERROR;
GURL upload_location;
scoped_ptr<FileResource> entry;
MockDriveServiceWithUploadExpectation mock_service(local_path, data.size());
DriveUploader uploader(&mock_service,
base::ThreadTaskRunnerHandle::Get().get());
std::vector<test_util::ProgressInfo> upload_progress_values;
uploader.UploadExistingFile(
kTestInitiateUploadResourceId, local_path, kTestMimeType,
UploadExistingFileOptions(),
test_util::CreateCopyResultCallback(&error, &upload_location, &entry),
base::Bind(&test_util::AppendProgressCallbackResult,
&upload_progress_values));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, mock_service.resume_upload_call_count());
EXPECT_EQ(1, mock_service.multipart_upload_call_count());
EXPECT_EQ(0, mock_service.received_bytes());
EXPECT_EQ(HTTP_SUCCESS, error);
EXPECT_TRUE(upload_location.is_empty());
ASSERT_TRUE(entry);
EXPECT_EQ(kTestDummyMd5, entry->md5_checksum());
ASSERT_EQ(1U, upload_progress_values.size());
EXPECT_EQ(test_util::ProgressInfo(0, 0), upload_progress_values[0]);
}
TEST_F(DriveUploaderTest, UploadExisting512KB) {
base::FilePath local_path;
std::string data;
ASSERT_TRUE(test_util::CreateFileOfSpecifiedSize(
temp_dir_.path(), 512 * 1024, &local_path, &data));
DriveApiErrorCode error = DRIVE_OTHER_ERROR;
GURL upload_location;
scoped_ptr<FileResource> entry;
MockDriveServiceWithUploadExpectation mock_service(local_path, data.size());
DriveUploader uploader(&mock_service,
base::ThreadTaskRunnerHandle::Get().get());
std::vector<test_util::ProgressInfo> upload_progress_values;
uploader.UploadExistingFile(
kTestInitiateUploadResourceId, local_path, kTestMimeType,
UploadExistingFileOptions(),
test_util::CreateCopyResultCallback(&error, &upload_location, &entry),
base::Bind(&test_util::AppendProgressCallbackResult,
&upload_progress_values));
base::RunLoop().RunUntilIdle();
// 512KB upload should be uploaded as multipart body.
EXPECT_EQ(0, mock_service.resume_upload_call_count());
EXPECT_EQ(1, mock_service.multipart_upload_call_count());
EXPECT_EQ(512 * 1024, mock_service.received_bytes());
EXPECT_EQ(HTTP_SUCCESS, error);
EXPECT_TRUE(upload_location.is_empty());
ASSERT_TRUE(entry);
EXPECT_EQ(kTestDummyMd5, entry->md5_checksum());
ASSERT_EQ(1U, upload_progress_values.size());
EXPECT_EQ(test_util::ProgressInfo(512 * 1024, 512 * 1024),
upload_progress_values[0]);
}
TEST_F(DriveUploaderTest, UploadExisting2MB) {
base::FilePath local_path;
std::string data;
ASSERT_TRUE(test_util::CreateFileOfSpecifiedSize(
temp_dir_.path(), 2 * 1024 * 1024, &local_path, &data));
DriveApiErrorCode error = DRIVE_OTHER_ERROR;
GURL upload_location;
scoped_ptr<FileResource> entry;
MockDriveServiceWithUploadExpectation mock_service(local_path, data.size());
DriveUploader uploader(&mock_service,
base::ThreadTaskRunnerHandle::Get().get());
std::vector<test_util::ProgressInfo> upload_progress_values;
uploader.UploadExistingFile(
kTestInitiateUploadResourceId, local_path, kTestMimeType,
UploadExistingFileOptions(),
test_util::CreateCopyResultCallback(&error, &upload_location, &entry),
base::Bind(&test_util::AppendProgressCallbackResult,
&upload_progress_values));
base::RunLoop().RunUntilIdle();
// 2MB upload should not be split into multiple chunks.
EXPECT_EQ(1, mock_service.resume_upload_call_count());
EXPECT_EQ(0, mock_service.multipart_upload_call_count());
EXPECT_EQ(2 * 1024 * 1024, mock_service.received_bytes());
EXPECT_EQ(HTTP_SUCCESS, error);
EXPECT_TRUE(upload_location.is_empty());
ASSERT_TRUE(entry);
EXPECT_EQ(kTestDummyMd5, entry->md5_checksum());
ASSERT_EQ(1U, upload_progress_values.size());
EXPECT_EQ(test_util::ProgressInfo(2 * 1024 * 1024, 2 * 1024 * 1024),
upload_progress_values[0]);
}
TEST_F(DriveUploaderTest, InitiateUploadFail) {
base::FilePath local_path;
std::string data;
ASSERT_TRUE(test_util::CreateFileOfSpecifiedSize(
temp_dir_.path(), 2 * 1024 * 1024, &local_path, &data));
DriveApiErrorCode error = HTTP_SUCCESS;
GURL upload_location;
scoped_ptr<FileResource> entry;
MockDriveServiceNoConnectionAtInitiate mock_service;
DriveUploader uploader(&mock_service,
base::ThreadTaskRunnerHandle::Get().get());
uploader.UploadExistingFile(
kTestInitiateUploadResourceId, local_path, kTestMimeType,
UploadExistingFileOptions(),
test_util::CreateCopyResultCallback(&error, &upload_location, &entry),
google_apis::ProgressCallback());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(DRIVE_NO_CONNECTION, error);
EXPECT_TRUE(upload_location.is_empty());
EXPECT_FALSE(entry);
}
TEST_F(DriveUploaderTest, MultipartUploadFail) {
base::FilePath local_path;
std::string data;
ASSERT_TRUE(test_util::CreateFileOfSpecifiedSize(temp_dir_.path(), 512 * 1024,
&local_path, &data));
DriveApiErrorCode error = HTTP_SUCCESS;
GURL upload_location;
scoped_ptr<FileResource> entry;
MockDriveServiceNoConnectionAtInitiate mock_service;
DriveUploader uploader(&mock_service,
base::ThreadTaskRunnerHandle::Get().get());
uploader.UploadExistingFile(
kTestInitiateUploadResourceId, local_path, kTestMimeType,
UploadExistingFileOptions(),
test_util::CreateCopyResultCallback(&error, &upload_location, &entry),
google_apis::ProgressCallback());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(DRIVE_NO_CONNECTION, error);
EXPECT_TRUE(upload_location.is_empty());
EXPECT_FALSE(entry);
}
TEST_F(DriveUploaderTest, InitiateUploadNoConflict) {
base::FilePath local_path;
std::string data;
ASSERT_TRUE(test_util::CreateFileOfSpecifiedSize(
temp_dir_.path(), 512 * 1024, &local_path, &data));
DriveApiErrorCode error = DRIVE_OTHER_ERROR;
GURL upload_location;
scoped_ptr<FileResource> entry;
MockDriveServiceWithUploadExpectation mock_service(local_path, data.size());
DriveUploader uploader(&mock_service,
base::ThreadTaskRunnerHandle::Get().get());
UploadExistingFileOptions options;
options.etag = kTestETag;
uploader.UploadExistingFile(kTestInitiateUploadResourceId,
local_path,
kTestMimeType,
options,
test_util::CreateCopyResultCallback(
&error, &upload_location, &entry),
google_apis::ProgressCallback());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(HTTP_SUCCESS, error);
EXPECT_TRUE(upload_location.is_empty());
}
TEST_F(DriveUploaderTest, MultipartUploadConflict) {
base::FilePath local_path;
std::string data;
ASSERT_TRUE(test_util::CreateFileOfSpecifiedSize(
temp_dir_.path(), 512 * 1024, &local_path, &data));
const std::string kDestinationETag("destination_etag");
DriveApiErrorCode error = DRIVE_OTHER_ERROR;
GURL upload_location;
scoped_ptr<FileResource> entry;
MockDriveServiceWithUploadExpectation mock_service(local_path, data.size());
DriveUploader uploader(&mock_service,
base::ThreadTaskRunnerHandle::Get().get());
UploadExistingFileOptions options;
options.etag = kDestinationETag;
uploader.UploadExistingFile(kTestInitiateUploadResourceId,
local_path,
kTestMimeType,
options,
test_util::CreateCopyResultCallback(
&error, &upload_location, &entry),
google_apis::ProgressCallback());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(HTTP_CONFLICT, error);
EXPECT_TRUE(upload_location.is_empty());
}
TEST_F(DriveUploaderTest, InitiateUploadConflict) {
base::FilePath local_path;
std::string data;
ASSERT_TRUE(test_util::CreateFileOfSpecifiedSize(
temp_dir_.path(), 2 * 1024 * 1024, &local_path, &data));
const std::string kDestinationETag("destination_etag");
DriveApiErrorCode error = DRIVE_OTHER_ERROR;
GURL upload_location;
scoped_ptr<FileResource> entry;
MockDriveServiceWithUploadExpectation mock_service(local_path, data.size());
DriveUploader uploader(&mock_service,
base::ThreadTaskRunnerHandle::Get().get());
UploadExistingFileOptions options;
options.etag = kDestinationETag;
uploader.UploadExistingFile(
kTestInitiateUploadResourceId, local_path, kTestMimeType, options,
test_util::CreateCopyResultCallback(&error, &upload_location, &entry),
google_apis::ProgressCallback());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(HTTP_CONFLICT, error);
EXPECT_TRUE(upload_location.is_empty());
}
TEST_F(DriveUploaderTest, ResumeUploadFail) {
base::FilePath local_path;
std::string data;
ASSERT_TRUE(test_util::CreateFileOfSpecifiedSize(
temp_dir_.path(), 2 * 1024 * 1024, &local_path, &data));
DriveApiErrorCode error = HTTP_SUCCESS;
GURL upload_location;
scoped_ptr<FileResource> entry;
MockDriveServiceNoConnectionAtResume mock_service;
DriveUploader uploader(&mock_service,
base::ThreadTaskRunnerHandle::Get().get());
uploader.UploadExistingFile(
kTestInitiateUploadResourceId, local_path, kTestMimeType,
UploadExistingFileOptions(),
test_util::CreateCopyResultCallback(&error, &upload_location, &entry),
google_apis::ProgressCallback());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(DRIVE_NO_CONNECTION, error);
EXPECT_EQ(GURL(kTestUploadExistingFileURL), upload_location);
}
TEST_F(DriveUploaderTest, GetUploadStatusFail) {
base::FilePath local_path;
std::string data;
ASSERT_TRUE(test_util::CreateFileOfSpecifiedSize(
temp_dir_.path(), 2 * 1024 * 1024, &local_path, &data));
DriveApiErrorCode error = HTTP_SUCCESS;
GURL upload_location;
scoped_ptr<FileResource> entry;
MockDriveServiceNoConnectionAtGetUploadStatus mock_service;
DriveUploader uploader(&mock_service,
base::ThreadTaskRunnerHandle::Get().get());
uploader.ResumeUploadFile(GURL(kTestUploadExistingFileURL),
local_path,
kTestMimeType,
test_util::CreateCopyResultCallback(
&error, &upload_location, &entry),
google_apis::ProgressCallback());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(DRIVE_NO_CONNECTION, error);
EXPECT_TRUE(upload_location.is_empty());
}
TEST_F(DriveUploaderTest, NonExistingSourceFile) {
DriveApiErrorCode error = DRIVE_OTHER_ERROR;
GURL upload_location;
scoped_ptr<FileResource> entry;
DriveUploader uploader(NULL, // NULL, the service won't be used.
base::ThreadTaskRunnerHandle::Get().get());
uploader.UploadExistingFile(
kTestInitiateUploadResourceId,
temp_dir_.path().AppendASCII("_this_path_should_not_exist_"),
kTestMimeType, UploadExistingFileOptions(),
test_util::CreateCopyResultCallback(&error, &upload_location, &entry),
google_apis::ProgressCallback());
base::RunLoop().RunUntilIdle();
// Should return failure without doing any attempt to connect to the server.
EXPECT_EQ(HTTP_NOT_FOUND, error);
EXPECT_TRUE(upload_location.is_empty());
}
TEST_F(DriveUploaderTest, ResumeUpload) {
base::FilePath local_path;
std::string data;
ASSERT_TRUE(test_util::CreateFileOfSpecifiedSize(
temp_dir_.path(), 1024 * 1024, &local_path, &data));
DriveApiErrorCode error = DRIVE_OTHER_ERROR;
GURL upload_location;
scoped_ptr<FileResource> entry;
MockDriveServiceWithUploadExpectation mock_service(local_path, data.size());
DriveUploader uploader(&mock_service,
base::ThreadTaskRunnerHandle::Get().get());
// Emulate the situation that the only first part is successfully uploaded,
// but not the latter half.
mock_service.set_received_bytes(512 * 1024);
std::vector<test_util::ProgressInfo> upload_progress_values;
uploader.ResumeUploadFile(
GURL(kTestUploadExistingFileURL),
local_path,
kTestMimeType,
test_util::CreateCopyResultCallback(
&error, &upload_location, &entry),
base::Bind(&test_util::AppendProgressCallbackResult,
&upload_progress_values));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, mock_service.resume_upload_call_count());
EXPECT_EQ(1024 * 1024, mock_service.received_bytes());
EXPECT_EQ(HTTP_SUCCESS, error);
EXPECT_TRUE(upload_location.is_empty());
ASSERT_TRUE(entry);
EXPECT_EQ(kTestDummyMd5, entry->md5_checksum());
ASSERT_EQ(1U, upload_progress_values.size());
EXPECT_EQ(test_util::ProgressInfo(1024 * 1024, 1024 * 1024),
upload_progress_values[0]);
}
class MockDriveServiceForBatchProcessing : public DummyDriveService {
public:
struct UploadFileInfo {
enum { NEW_FILE, EXISTING_FILE } type;
std::string content_type;
uint64 content_length;
std::string parent_resource_id;
std::string resource_id;
std::string title;
base::FilePath local_file_path;
google_apis::FileResourceCallback callback;
google_apis::ProgressCallback progress_callback;
};
class BatchRequestConfigurator : public BatchRequestConfiguratorInterface {
public:
explicit BatchRequestConfigurator(
MockDriveServiceForBatchProcessing* service)
: service(service) {}
CancelCallback MultipartUploadNewFile(
const std::string& content_type,
int64 content_length,
const std::string& parent_resource_id,
const std::string& title,
const base::FilePath& local_file_path,
const UploadNewFileOptions& options,
const google_apis::FileResourceCallback& callback,
const google_apis::ProgressCallback& progress_callback) override {
UploadFileInfo info;
info.type = UploadFileInfo::NEW_FILE;
info.content_type = content_type;
info.content_length = content_length;
info.parent_resource_id = parent_resource_id;
info.title = title;
info.local_file_path = local_file_path;
info.callback = callback;
info.progress_callback = progress_callback;
service->files.push_back(info);
return CancelCallback();
}
CancelCallback MultipartUploadExistingFile(
const std::string& content_type,
int64 content_length,
const std::string& resource_id,
const base::FilePath& local_file_path,
const UploadExistingFileOptions& options,
const google_apis::FileResourceCallback& callback,
const google_apis::ProgressCallback& progress_callback) override {
UploadFileInfo info;
info.type = UploadFileInfo::EXISTING_FILE;
info.content_type = content_type;
info.content_length = content_length;
info.resource_id = resource_id;
info.local_file_path = local_file_path;
info.callback = callback;
info.progress_callback = progress_callback;
service->files.push_back(info);
return CancelCallback();
}
void Commit() override {
ASSERT_FALSE(service->committed);
service->committed = true;
for (const auto& file : service->files) {
SendMultipartUploadResult(HTTP_SUCCESS, file.content_length,
file.callback, file.progress_callback);
}
}
private:
MockDriveServiceForBatchProcessing* service;
};
public:
scoped_ptr<BatchRequestConfiguratorInterface> StartBatchRequest() override {
committed = false;
return scoped_ptr<BatchRequestConfiguratorInterface>(
new BatchRequestConfigurator(this));
}
std::vector<UploadFileInfo> files;
bool committed;
};
TEST_F(DriveUploaderTest, BatchProcessing) {
// Preapre test file.
const size_t kTestFileSize = 1024 * 512;
base::FilePath local_path;
std::string data;
ASSERT_TRUE(test_util::CreateFileOfSpecifiedSize(
temp_dir_.path(), kTestFileSize, &local_path, &data));
// Prepare test target.
MockDriveServiceForBatchProcessing service;
DriveUploader uploader(&service, base::ThreadTaskRunnerHandle::Get().get());
struct {
DriveApiErrorCode error;
GURL resume_url;
scoped_ptr<FileResource> file;
UploadCompletionCallback callback() {
return test_util::CreateCopyResultCallback(&error, &resume_url, &file);
}
} results[2];
uploader.StartBatchProcessing();
uploader.UploadNewFile("parent_resource_id", local_path, "title",
kTestMimeType, UploadNewFileOptions(),
results[0].callback(),
google_apis::ProgressCallback());
uploader.UploadExistingFile(
"resource_id", local_path, kTestMimeType, UploadExistingFileOptions(),
results[1].callback(), google_apis::ProgressCallback());
uploader.StopBatchProcessing();
base::RunLoop().RunUntilIdle();
ASSERT_EQ(2u, service.files.size());
EXPECT_TRUE(service.committed);
EXPECT_EQ(MockDriveServiceForBatchProcessing::UploadFileInfo::NEW_FILE,
service.files[0].type);
EXPECT_EQ(kTestMimeType, service.files[0].content_type);
EXPECT_EQ(kTestFileSize, service.files[0].content_length);
EXPECT_EQ("parent_resource_id", service.files[0].parent_resource_id);
EXPECT_EQ("", service.files[0].resource_id);
EXPECT_EQ("title", service.files[0].title);
EXPECT_EQ(local_path.value(), service.files[0].local_file_path.value());
EXPECT_EQ(MockDriveServiceForBatchProcessing::UploadFileInfo::EXISTING_FILE,
service.files[1].type);
EXPECT_EQ(kTestMimeType, service.files[1].content_type);
EXPECT_EQ(kTestFileSize, service.files[1].content_length);
EXPECT_EQ("", service.files[1].parent_resource_id);
EXPECT_EQ("resource_id", service.files[1].resource_id);
EXPECT_EQ("", service.files[1].title);
EXPECT_EQ(local_path.value(), service.files[1].local_file_path.value());
EXPECT_EQ(HTTP_SUCCESS, results[0].error);
EXPECT_TRUE(results[0].resume_url.is_empty());
EXPECT_TRUE(results[0].file);
EXPECT_EQ(HTTP_SUCCESS, results[1].error);
EXPECT_TRUE(results[1].resume_url.is_empty());
EXPECT_TRUE(results[1].file);
}
TEST_F(DriveUploaderTest, BatchProcessingWithError) {
// Prepare test target.
MockDriveServiceForBatchProcessing service;
DriveUploader uploader(&service, base::ThreadTaskRunnerHandle::Get().get());
struct {
DriveApiErrorCode error;
GURL resume_url;
scoped_ptr<FileResource> file;
UploadCompletionCallback callback() {
return test_util::CreateCopyResultCallback(&error, &resume_url, &file);
}
} results[2];
uploader.StartBatchProcessing();
uploader.UploadNewFile("parent_resource_id",
base::FilePath(FILE_PATH_LITERAL("/path/non_exists")),
"title", kTestMimeType, UploadNewFileOptions(),
results[0].callback(),
google_apis::ProgressCallback());
uploader.UploadExistingFile(
"resource_id", base::FilePath(FILE_PATH_LITERAL("/path/non_exists")),
kTestMimeType, UploadExistingFileOptions(), results[1].callback(),
google_apis::ProgressCallback());
uploader.StopBatchProcessing();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0u, service.files.size());
EXPECT_TRUE(service.committed);
EXPECT_EQ(HTTP_NOT_FOUND, results[0].error);
EXPECT_TRUE(results[0].resume_url.is_empty());
EXPECT_FALSE(results[0].file);
EXPECT_EQ(HTTP_NOT_FOUND, results[1].error);
EXPECT_TRUE(results[1].resume_url.is_empty());
EXPECT_FALSE(results[1].file);
}
} // namespace drive
|