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
|
// 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 "chrome/browser/google_apis/drive_uploader.h"
#include <algorithm>
#include <cstdlib>
#include <string>
#include "base/bind.h"
#include "base/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "base/values.h"
#include "chrome/browser/google_apis/dummy_drive_service.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace google_apis {
namespace {
const char kTestDummyId[] = "file:dummy_id";
const char kTestDocumentTitle[] = "Hello world";
const char kTestDrivePath[] = "drive/dummy.txt";
const char kTestInitialUploadURL[] =
"http://test/feeds/upload/create-session/default/private/full";
const char kTestMimeType[] = "text/plain";
const char kTestUploadURL[] = "http://test/upload_location";
const int64 kUploadChunkSize = 512 * 1024;
const char kTestETag[] = "test_etag";
// Creates a |size| byte file and returns its |path|. The file is filled with
// random bytes so that the test assertions can identify correct
// portion of the file is being sent.
bool CreateFileOfSpecifiedSize(const FilePath& temp_dir,
size_t size,
FilePath* path,
std::string* data) {
data->resize(size);
for (size_t i = 0; i < size; ++i)
(*data)[i] = static_cast<char>(rand() % 256); // NOLINT
if (!file_util::CreateTemporaryFileInDir(temp_dir, path))
return false;
return file_util::WriteFile(*path, data->c_str(), static_cast<int>(size)) ==
static_cast<int>(size);
}
// 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.
explicit MockDriveServiceWithUploadExpectation(
const std::string& expected_upload_content)
: expected_upload_content_(expected_upload_content),
received_bytes_(0),
resume_upload_call_count_(0) {}
int64 received_bytes() const { return received_bytes_; }
int64 resume_upload_call_count() const { return resume_upload_call_count_; }
private:
// DriveServiceInterface overrides.
// Handles a request for obtaining an upload location URL.
virtual void InitiateUpload(const InitiateUploadParams& params,
const InitiateUploadCallback& callback) OVERRIDE {
const int64 expected_size = expected_upload_content_.size();
// Verify that the expected parameters are passed.
if (params.upload_mode == UPLOAD_NEW_FILE)
EXPECT_EQ(kTestDocumentTitle, params.title);
else
EXPECT_EQ("", params.title);
EXPECT_EQ(kTestMimeType, params.content_type);
EXPECT_EQ(expected_size, params.content_length);
EXPECT_EQ(GURL(kTestInitialUploadURL), params.upload_location);
if (!params.etag.empty() && params.etag != kTestETag) {
MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback, HTTP_PRECONDITION, GURL()));
return;
}
// Calls back the upload URL for subsequent ResumeUpload operations.
// InitiateUpload is an asynchronous function, so don't callback directly.
MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadURL)));
}
// Handles a request for uploading a chunk of bytes.
virtual void ResumeUpload(const ResumeUploadParams& params,
const ResumeUploadCallback& callback) OVERRIDE {
const int64 expected_size = expected_upload_content_.size();
// The upload range should start from the current first unreceived byte.
EXPECT_EQ(received_bytes_, params.start_position);
// The upload data must be split into 512KB chunks.
const int64 expected_chunk_end =
std::min(received_bytes_ + kUploadChunkSize, expected_size);
EXPECT_EQ(expected_chunk_end, params.end_position);
const int64 expected_chunk_size = expected_chunk_end - received_bytes_;
const std::string expected_chunk_data(
expected_upload_content_.substr(received_bytes_,
expected_chunk_size));
std::string uploading_data(params.buf->data(),
params.buf->data() + expected_chunk_size);
EXPECT_EQ(expected_chunk_data, uploading_data);
// The upload URL returned by InitiateUpload() must be used.
EXPECT_EQ(GURL(kTestUploadURL), params.upload_location);
// Other parameters should be the exact values passed to DriveUploader.
EXPECT_EQ(expected_size, params.content_length);
EXPECT_EQ(kTestMimeType, params.content_type);
// Update the internal status of the current upload session.
resume_upload_call_count_++;
received_bytes_ = params.end_position;
// Callback with response.
ResumeUploadResponse response;
scoped_ptr<ResourceEntry> entry;
if (received_bytes_ == params.content_length) {
response = ResumeUploadResponse(
params.upload_mode == UPLOAD_NEW_FILE ? HTTP_CREATED : HTTP_SUCCESS,
-1, -1);
base::DictionaryValue dict;
dict.Set("id.$t", new base::StringValue(kTestDummyId));
entry = ResourceEntry::CreateFrom(dict);
} else {
response = ResumeUploadResponse(HTTP_RESUME_INCOMPLETE, 0,
params.end_position);
}
// ResumeUpload is an asynchronous function, so don't callback directly.
MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback, response, base::Passed(&entry)));
}
std::string expected_upload_content_;
int64 received_bytes_;
int64 resume_upload_call_count_;
};
// Mock DriveService that returns a failure at InitiateUpload().
class MockDriveServiceNoConnectionAtInitiate : public DummyDriveService {
// Returns error.
virtual void InitiateUpload(const InitiateUploadParams& params,
const InitiateUploadCallback& callback) OVERRIDE {
MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback, GDATA_NO_CONNECTION, GURL()));
}
// Should not be used.
virtual void ResumeUpload(const ResumeUploadParams& params,
const ResumeUploadCallback& callback) OVERRIDE {
NOTREACHED();
}
};
// Mock DriveService that returns a failure at ResumeUpload().
class MockDriveServiceNoConnectionAtResume : public DummyDriveService {
// Succeeds and returns an upload location URL.
virtual void InitiateUpload(const InitiateUploadParams& params,
const InitiateUploadCallback& callback) OVERRIDE {
MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback, HTTP_SUCCESS, GURL(kTestInitialUploadURL)));
}
// Returns error.
virtual void ResumeUpload(const ResumeUploadParams& params,
const ResumeUploadCallback& callback) OVERRIDE {
MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback,
ResumeUploadResponse(GDATA_NO_CONNECTION, -1, -1),
base::Passed(scoped_ptr<ResourceEntry>())));
}
};
class DriveUploaderTest : public testing::Test {
public:
DriveUploaderTest()
: ui_thread_(content::BrowserThread::UI, &message_loop_) {
}
virtual void SetUp() OVERRIDE {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
}
virtual void TearDown() OVERRIDE {
ASSERT_TRUE(temp_dir_.Delete());
}
protected:
MessageLoopForUI message_loop_;
content::TestBrowserThread ui_thread_;
base::ScopedTempDir temp_dir_;
};
// Struct for holding the results copied from UploadCompletionCallback.
struct UploadCompletionCallbackResult {
UploadCompletionCallbackResult() : error(DRIVE_UPLOAD_ERROR_ABORT) {}
DriveUploadError error;
FilePath drive_path;
FilePath file_path;
scoped_ptr<ResourceEntry> resource_entry;
};
// Copies the result from UploadCompletionCallback and quit the message loop.
void CopyResultsFromUploadCompletionCallbackAndQuit(
UploadCompletionCallbackResult* out,
DriveUploadError error,
const FilePath& drive_path,
const FilePath& file_path,
scoped_ptr<ResourceEntry> resource_entry) {
out->error = error;
out->drive_path = drive_path;
out->file_path = file_path;
out->resource_entry = resource_entry.Pass();
MessageLoop::current()->Quit();
}
} // namespace
TEST_F(DriveUploaderTest, UploadExisting0KB) {
FilePath local_path;
std::string data;
ASSERT_TRUE(CreateFileOfSpecifiedSize(temp_dir_.path(), 0,
&local_path, &data));
UploadCompletionCallbackResult out;
MockDriveServiceWithUploadExpectation mock_service(data);
DriveUploader uploader(&mock_service);
uploader.UploadExistingFile(
GURL(kTestInitialUploadURL),
FilePath::FromUTF8Unsafe(kTestDrivePath),
local_path,
kTestMimeType,
"", // etag
base::Bind(&CopyResultsFromUploadCompletionCallbackAndQuit, &out));
message_loop_.Run();
EXPECT_EQ(1, mock_service.resume_upload_call_count());
EXPECT_EQ(0, mock_service.received_bytes());
EXPECT_EQ(DRIVE_UPLOAD_OK, out.error);
EXPECT_EQ(FilePath::FromUTF8Unsafe(kTestDrivePath), out.drive_path);
EXPECT_EQ(local_path, out.file_path);
ASSERT_TRUE(out.resource_entry);
EXPECT_EQ(kTestDummyId, out.resource_entry->id());
}
TEST_F(DriveUploaderTest, UploadExisting512KB) {
FilePath local_path;
std::string data;
ASSERT_TRUE(CreateFileOfSpecifiedSize(temp_dir_.path(), 512 * 1024,
&local_path, &data));
UploadCompletionCallbackResult out;
MockDriveServiceWithUploadExpectation mock_service(data);
DriveUploader uploader(&mock_service);
uploader.UploadExistingFile(
GURL(kTestInitialUploadURL),
FilePath::FromUTF8Unsafe(kTestDrivePath),
local_path,
kTestMimeType,
"", // etag
base::Bind(&CopyResultsFromUploadCompletionCallbackAndQuit, &out));
message_loop_.Run();
// 512KB upload should not be split into multiple chunks.
EXPECT_EQ(1, mock_service.resume_upload_call_count());
EXPECT_EQ(512 * 1024, mock_service.received_bytes());
EXPECT_EQ(DRIVE_UPLOAD_OK, out.error);
EXPECT_EQ(FilePath::FromUTF8Unsafe(kTestDrivePath), out.drive_path);
EXPECT_EQ(local_path, out.file_path);
ASSERT_TRUE(out.resource_entry);
EXPECT_EQ(kTestDummyId, out.resource_entry->id());
}
TEST_F(DriveUploaderTest, UploadExisting1234KB) {
FilePath local_path;
std::string data;
ASSERT_TRUE(CreateFileOfSpecifiedSize(temp_dir_.path(), 1234 * 1024,
&local_path, &data));
UploadCompletionCallbackResult out;
MockDriveServiceWithUploadExpectation mock_service(data);
DriveUploader uploader(&mock_service);
uploader.UploadExistingFile(
GURL(kTestInitialUploadURL),
FilePath::FromUTF8Unsafe(kTestDrivePath),
local_path,
kTestMimeType,
"", // etag
base::Bind(&CopyResultsFromUploadCompletionCallbackAndQuit, &out));
message_loop_.Run();
// The file should be split into 3 chunks (1234 = 512 + 512 + 210).
EXPECT_EQ(3, mock_service.resume_upload_call_count());
EXPECT_EQ(1234 * 1024, mock_service.received_bytes());
EXPECT_EQ(DRIVE_UPLOAD_OK, out.error);
EXPECT_EQ(FilePath::FromUTF8Unsafe(kTestDrivePath), out.drive_path);
EXPECT_EQ(local_path, out.file_path);
ASSERT_TRUE(out.resource_entry);
EXPECT_EQ(kTestDummyId, out.resource_entry->id());
}
TEST_F(DriveUploaderTest, UploadNew1234KB) {
FilePath local_path;
std::string data;
ASSERT_TRUE(CreateFileOfSpecifiedSize(temp_dir_.path(), 1234 * 1024,
&local_path, &data));
UploadCompletionCallbackResult out;
MockDriveServiceWithUploadExpectation mock_service(data);
DriveUploader uploader(&mock_service);
uploader.UploadNewFile(
GURL(kTestInitialUploadURL),
FilePath::FromUTF8Unsafe(kTestDrivePath),
local_path,
kTestDocumentTitle,
kTestMimeType,
base::Bind(&CopyResultsFromUploadCompletionCallbackAndQuit, &out));
message_loop_.Run();
// The file should be split into 3 chunks (1234 = 512 + 512 + 210).
EXPECT_EQ(3, mock_service.resume_upload_call_count());
EXPECT_EQ(1234 * 1024, mock_service.received_bytes());
EXPECT_EQ(DRIVE_UPLOAD_OK, out.error);
EXPECT_EQ(FilePath::FromUTF8Unsafe(kTestDrivePath), out.drive_path);
EXPECT_EQ(local_path, out.file_path);
ASSERT_TRUE(out.resource_entry);
EXPECT_EQ(kTestDummyId, out.resource_entry->id());
}
TEST_F(DriveUploaderTest, InitiateUploadFail) {
FilePath local_path;
std::string data;
ASSERT_TRUE(CreateFileOfSpecifiedSize(temp_dir_.path(), 512 * 1024,
&local_path, &data));
UploadCompletionCallbackResult out;
MockDriveServiceNoConnectionAtInitiate mock_service;
DriveUploader uploader(&mock_service);
uploader.UploadExistingFile(
GURL(kTestInitialUploadURL),
FilePath::FromUTF8Unsafe(kTestDrivePath),
local_path,
kTestMimeType,
"", // etag
base::Bind(&CopyResultsFromUploadCompletionCallbackAndQuit, &out));
message_loop_.Run();
EXPECT_EQ(DRIVE_UPLOAD_ERROR_ABORT, out.error);
}
TEST_F(DriveUploaderTest, InitiateUploadNoConflict) {
FilePath local_path;
std::string data;
ASSERT_TRUE(CreateFileOfSpecifiedSize(temp_dir_.path(), 512 * 1024,
&local_path, &data));
UploadCompletionCallbackResult out;
MockDriveServiceWithUploadExpectation mock_service(data);
DriveUploader uploader(&mock_service);
uploader.UploadExistingFile(
GURL(kTestInitialUploadURL),
FilePath::FromUTF8Unsafe(kTestDrivePath),
local_path,
kTestMimeType,
kTestETag,
base::Bind(&CopyResultsFromUploadCompletionCallbackAndQuit, &out));
message_loop_.Run();
EXPECT_EQ(DRIVE_UPLOAD_OK, out.error);
}
TEST_F(DriveUploaderTest, InitiateUploadConflict) {
FilePath local_path;
std::string data;
ASSERT_TRUE(CreateFileOfSpecifiedSize(temp_dir_.path(), 512 * 1024,
&local_path, &data));
const std::string kDestinationETag("destination_etag");
UploadCompletionCallbackResult out;
MockDriveServiceWithUploadExpectation mock_service(data);
DriveUploader uploader(&mock_service);
uploader.UploadExistingFile(
GURL(kTestInitialUploadURL),
FilePath::FromUTF8Unsafe(kTestDrivePath),
local_path,
kTestMimeType,
kDestinationETag,
base::Bind(&CopyResultsFromUploadCompletionCallbackAndQuit, &out));
message_loop_.Run();
EXPECT_EQ(DRIVE_UPLOAD_ERROR_CONFLICT, out.error);
}
TEST_F(DriveUploaderTest, ResumeUploadFail) {
FilePath local_path;
std::string data;
ASSERT_TRUE(CreateFileOfSpecifiedSize(temp_dir_.path(), 512 * 1024,
&local_path, &data));
UploadCompletionCallbackResult out;
MockDriveServiceNoConnectionAtResume mock_service;
DriveUploader uploader(&mock_service);
uploader.UploadExistingFile(
GURL(kTestInitialUploadURL),
FilePath::FromUTF8Unsafe(kTestDrivePath),
local_path,
kTestMimeType,
"", // etag
base::Bind(&CopyResultsFromUploadCompletionCallbackAndQuit, &out));
message_loop_.Run();
EXPECT_EQ(DRIVE_UPLOAD_ERROR_ABORT, out.error);
}
TEST_F(DriveUploaderTest, NonExistingSourceFile) {
UploadCompletionCallbackResult out;
DriveUploader uploader(NULL); // NULL, the service won't be used.
uploader.UploadExistingFile(
GURL(kTestInitialUploadURL),
FilePath::FromUTF8Unsafe(kTestDrivePath),
temp_dir_.path().AppendASCII("_this_path_should_not_exist_"),
kTestMimeType,
"", // etag
base::Bind(&CopyResultsFromUploadCompletionCallbackAndQuit, &out));
message_loop_.Run();
// Should return failure without doing any attempt to connect to the server.
EXPECT_EQ(DRIVE_UPLOAD_ERROR_NOT_FOUND, out.error);
}
} // namespace google_apis
|