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
|
// 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 "base/file_util.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop.h"
#include "base/observer_list.h"
#include "base/test/test_file_util.h"
#include "chrome/browser/download/download_path_reservation_tracker.h"
#include "chrome/browser/download/download_util.h"
#include "content/public/test/mock_download_item.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::BrowserThread;
using content::DownloadId;
using content::DownloadItem;
using content::MockDownloadItem;
using testing::AnyNumber;
using testing::Return;
using testing::ReturnRef;
using testing::ReturnRefOfCopy;
namespace {
// MockDownloadItem with real observers and state.
class FakeDownloadItem : public MockDownloadItem {
public:
explicit FakeDownloadItem()
: state_(IN_PROGRESS) {
}
virtual ~FakeDownloadItem() {
FOR_EACH_OBSERVER(Observer, observers_, OnDownloadDestroyed(this));
EXPECT_EQ(0u, observers_.size());
}
virtual void AddObserver(Observer* observer) OVERRIDE {
observers_.AddObserver(observer);
}
virtual void RemoveObserver(Observer* observer) OVERRIDE {
observers_.RemoveObserver(observer);
}
virtual void UpdateObservers() OVERRIDE {
FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this));
}
virtual DownloadState GetState() const OVERRIDE {
return state_;
}
void SetState(DownloadState state) {
state_ = state;
UpdateObservers();
}
private:
DownloadState state_;
ObserverList<Observer> observers_;
};
class DownloadPathReservationTrackerTest : public testing::Test {
public:
DownloadPathReservationTrackerTest();
// testing::Test
virtual void SetUp() OVERRIDE;
virtual void TearDown() OVERRIDE;
FakeDownloadItem* CreateDownloadItem(int32 id);
base::FilePath GetPathInDownloadsDirectory(
const base::FilePath::CharType* suffix);
bool IsPathInUse(const base::FilePath& path);
void CallGetReservedPath(
DownloadItem& download_item,
const base::FilePath& target_path,
bool create_directory,
DownloadPathReservationTracker::FilenameConflictAction conflict_action,
base::FilePath* return_path,
bool* return_verified);
const base::FilePath& default_download_path() const {
return default_download_path_;
}
void set_default_download_path(const base::FilePath& path) {
default_download_path_ = path;
}
// Creates a name of form 'a'*repeat + suffix
base::FilePath GetLongNamePathInDownloadsDirectory(
size_t repeat, const base::FilePath::CharType* suffix);
protected:
base::ScopedTempDir test_download_dir_;
base::FilePath default_download_path_;
MessageLoopForUI message_loop_;
content::TestBrowserThread ui_thread_;
content::TestBrowserThread file_thread_;
private:
void TestReservedPathCallback(base::FilePath* return_path,
bool* return_verified, bool* did_run_callback,
const base::FilePath& path, bool verified);
};
DownloadPathReservationTrackerTest::DownloadPathReservationTrackerTest()
: ui_thread_(BrowserThread::UI, &message_loop_),
file_thread_(BrowserThread::FILE, &message_loop_) {
}
void DownloadPathReservationTrackerTest::SetUp() {
ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir());
set_default_download_path(test_download_dir_.path());
}
void DownloadPathReservationTrackerTest::TearDown() {
message_loop_.RunUntilIdle();
}
FakeDownloadItem* DownloadPathReservationTrackerTest::CreateDownloadItem(
int32 id) {
FakeDownloadItem* item = new ::testing::StrictMock<FakeDownloadItem>;
DownloadId download_id(reinterpret_cast<void*>(this), id);
EXPECT_CALL(*item, GetGlobalId())
.WillRepeatedly(Return(download_id));
EXPECT_CALL(*item, GetTargetFilePath())
.WillRepeatedly(ReturnRefOfCopy(base::FilePath()));
return item;
}
base::FilePath DownloadPathReservationTrackerTest::GetPathInDownloadsDirectory(
const base::FilePath::CharType* suffix) {
return default_download_path().Append(suffix).NormalizePathSeparators();
}
bool DownloadPathReservationTrackerTest::IsPathInUse(
const base::FilePath& path) {
return DownloadPathReservationTracker::IsPathInUseForTesting(path);
}
void DownloadPathReservationTrackerTest::CallGetReservedPath(
DownloadItem& download_item,
const base::FilePath& target_path,
bool create_directory,
DownloadPathReservationTracker::FilenameConflictAction conflict_action,
base::FilePath* return_path,
bool* return_verified) {
// Weak pointer factory to prevent the callback from running after this
// function has returned.
base::WeakPtrFactory<DownloadPathReservationTrackerTest> weak_ptr_factory(
this);
bool did_run_callback = false;
DownloadPathReservationTracker::GetReservedPath(
download_item,
target_path,
default_download_path(),
create_directory,
conflict_action,
base::Bind(&DownloadPathReservationTrackerTest::TestReservedPathCallback,
weak_ptr_factory.GetWeakPtr(), return_path, return_verified,
&did_run_callback));
message_loop_.RunUntilIdle();
EXPECT_TRUE(did_run_callback);
}
void DownloadPathReservationTrackerTest::TestReservedPathCallback(
base::FilePath* return_path, bool* return_verified, bool* did_run_callback,
const base::FilePath& path, bool verified) {
*did_run_callback = true;
*return_path = path;
*return_verified = verified;
}
base::FilePath
DownloadPathReservationTrackerTest::GetLongNamePathInDownloadsDirectory(
size_t repeat, const base::FilePath::CharType* suffix) {
return GetPathInDownloadsDirectory(
(base::FilePath::StringType(repeat, FILE_PATH_LITERAL('a'))
+ suffix).c_str());
}
} // namespace
// A basic reservation is acquired and committed.
TEST_F(DownloadPathReservationTrackerTest, BasicReservation) {
scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
base::FilePath path(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
ASSERT_FALSE(IsPathInUse(path));
base::FilePath reserved_path;
bool verified = false;
DownloadPathReservationTracker::FilenameConflictAction conflict_action =
DownloadPathReservationTracker::OVERWRITE;
bool create_directory = false;
CallGetReservedPath(
*item,
path,
create_directory,
conflict_action,
&reserved_path,
&verified);
EXPECT_TRUE(IsPathInUse(path));
EXPECT_TRUE(verified);
EXPECT_EQ(path.value(), reserved_path.value());
// Destroying the item should release the reservation.
item.reset();
message_loop_.RunUntilIdle();
EXPECT_FALSE(IsPathInUse(path));
}
// A download that is interrupted should lose its reservation.
TEST_F(DownloadPathReservationTrackerTest, InterruptedDownload) {
scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
base::FilePath path(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
ASSERT_FALSE(IsPathInUse(path));
base::FilePath reserved_path;
bool verified = false;
DownloadPathReservationTracker::FilenameConflictAction conflict_action =
DownloadPathReservationTracker::OVERWRITE;
bool create_directory = false;
CallGetReservedPath(
*item,
path,
create_directory,
conflict_action,
&reserved_path,
&verified);
EXPECT_TRUE(IsPathInUse(path));
EXPECT_TRUE(verified);
EXPECT_EQ(path.value(), reserved_path.value());
// Once the download is interrupted, the path should become available again.
item->SetState(DownloadItem::INTERRUPTED);
message_loop_.RunUntilIdle();
EXPECT_FALSE(IsPathInUse(path));
}
// A completed download should also lose its reservation.
TEST_F(DownloadPathReservationTrackerTest, CompleteDownload) {
scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
base::FilePath path(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
ASSERT_FALSE(IsPathInUse(path));
base::FilePath reserved_path;
bool verified = false;
DownloadPathReservationTracker::FilenameConflictAction conflict_action =
DownloadPathReservationTracker::OVERWRITE;
bool create_directory = false;
CallGetReservedPath(
*item,
path,
create_directory,
conflict_action,
&reserved_path,
&verified);
EXPECT_TRUE(IsPathInUse(path));
EXPECT_TRUE(verified);
EXPECT_EQ(path.value(), reserved_path.value());
// Once the download completes, the path should become available again. For a
// real download, at this point only the path reservation will be released.
// The path wouldn't be available since it is occupied on disk by the
// completed download.
item->SetState(DownloadItem::COMPLETE);
message_loop_.RunUntilIdle();
EXPECT_FALSE(IsPathInUse(path));
}
// If there are files on the file system, a unique reservation should uniquify
// around it.
TEST_F(DownloadPathReservationTrackerTest, ConflictingFiles) {
scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
base::FilePath path(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
base::FilePath path1(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt")));
// Create a file at |path|, and a .crdownload file at |path1|.
ASSERT_EQ(0, file_util::WriteFile(path, "", 0));
ASSERT_EQ(0, file_util::WriteFile(download_util::GetCrDownloadPath(path1),
"", 0));
ASSERT_TRUE(IsPathInUse(path));
base::FilePath reserved_path;
bool verified = false;
bool create_directory = false;
DownloadPathReservationTracker::FilenameConflictAction conflict_action =
DownloadPathReservationTracker::UNIQUIFY;
CallGetReservedPath(
*item,
path,
create_directory,
conflict_action,
&reserved_path,
&verified);
EXPECT_TRUE(IsPathInUse(path));
EXPECT_TRUE(IsPathInUse(reserved_path));
EXPECT_TRUE(verified);
// The path should be uniquified, skipping over foo.txt but not over
// "foo (1).txt.crdownload"
EXPECT_EQ(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt")).value(),
reserved_path.value());
item.reset();
message_loop_.RunUntilIdle();
EXPECT_TRUE(IsPathInUse(path));
EXPECT_FALSE(IsPathInUse(reserved_path));
}
// Multiple reservations for the same path should uniquify around each other.
TEST_F(DownloadPathReservationTrackerTest, ConflictingReservations) {
scoped_ptr<FakeDownloadItem> item1(CreateDownloadItem(1));
base::FilePath path(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
base::FilePath uniquified_path(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo (1).txt")));
ASSERT_FALSE(IsPathInUse(path));
ASSERT_FALSE(IsPathInUse(uniquified_path));
base::FilePath reserved_path1;
bool verified = false;
bool create_directory = false;
DownloadPathReservationTracker::FilenameConflictAction conflict_action =
DownloadPathReservationTracker::UNIQUIFY;
CallGetReservedPath(
*item1,
path,
create_directory,
conflict_action,
&reserved_path1,
&verified);
EXPECT_TRUE(IsPathInUse(path));
EXPECT_TRUE(verified);
{
// Requesting a reservation for the same path with uniquification results in
// a uniquified path.
scoped_ptr<FakeDownloadItem> item2(CreateDownloadItem(2));
base::FilePath reserved_path2;
CallGetReservedPath(
*item2,
path,
create_directory,
conflict_action,
&reserved_path2,
&verified);
EXPECT_TRUE(IsPathInUse(path));
EXPECT_TRUE(IsPathInUse(uniquified_path));
EXPECT_EQ(uniquified_path.value(), reserved_path2.value());
}
message_loop_.RunUntilIdle();
EXPECT_TRUE(IsPathInUse(path));
EXPECT_FALSE(IsPathInUse(uniquified_path));
{
// Since the previous download item was removed, requesting a reservation
// for the same path should result in the same uniquified path.
scoped_ptr<FakeDownloadItem> item2(CreateDownloadItem(2));
base::FilePath reserved_path2;
CallGetReservedPath(
*item2,
path,
create_directory,
conflict_action,
&reserved_path2,
&verified);
EXPECT_TRUE(IsPathInUse(path));
EXPECT_TRUE(IsPathInUse(uniquified_path));
EXPECT_EQ(uniquified_path.value(), reserved_path2.value());
}
message_loop_.RunUntilIdle();
// Now acquire an overwriting reservation. We should end up with the same
// non-uniquified path for both reservations.
scoped_ptr<FakeDownloadItem> item3(CreateDownloadItem(2));
base::FilePath reserved_path3;
conflict_action = DownloadPathReservationTracker::OVERWRITE;
CallGetReservedPath(
*item3,
path,
create_directory,
conflict_action,
&reserved_path3,
&verified);
EXPECT_TRUE(IsPathInUse(path));
EXPECT_FALSE(IsPathInUse(uniquified_path));
EXPECT_EQ(path.value(), reserved_path1.value());
EXPECT_EQ(path.value(), reserved_path3.value());
}
// If a unique path cannot be determined after trying kMaxUniqueFiles
// uniquifiers, then the callback should notified that verification failed, and
// the returned path should be set to the original requested path.
TEST_F(DownloadPathReservationTrackerTest, UnresolvedConflicts) {
base::FilePath path(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
scoped_ptr<FakeDownloadItem> items[
DownloadPathReservationTracker::kMaxUniqueFiles + 1];
DownloadPathReservationTracker::FilenameConflictAction conflict_action =
DownloadPathReservationTracker::UNIQUIFY;
bool create_directory = false;
// Create |kMaxUniqueFiles + 1| reservations for |path|. The first reservation
// will have no uniquifier. The |kMaxUniqueFiles| remaining reservations do.
for (int i = 0; i <= DownloadPathReservationTracker::kMaxUniqueFiles; i++) {
base::FilePath reserved_path;
base::FilePath expected_path;
bool verified = false;
if (i > 0) {
expected_path =
path.InsertBeforeExtensionASCII(base::StringPrintf(" (%d)", i));
} else {
expected_path = path;
}
items[i].reset(CreateDownloadItem(i));
EXPECT_FALSE(IsPathInUse(expected_path));
CallGetReservedPath(
*items[i],
path,
create_directory,
conflict_action,
&reserved_path,
&verified);
EXPECT_TRUE(IsPathInUse(expected_path));
EXPECT_EQ(expected_path.value(), reserved_path.value());
EXPECT_TRUE(verified);
}
// The next reservation for |path| will fail to be unique.
scoped_ptr<FakeDownloadItem> item(
CreateDownloadItem(DownloadPathReservationTracker::kMaxUniqueFiles + 1));
base::FilePath reserved_path;
bool verified = true;
CallGetReservedPath(
*item,
path,
create_directory,
conflict_action,
&reserved_path,
&verified);
EXPECT_FALSE(verified);
EXPECT_EQ(path.value(), reserved_path.value());
}
// If the target directory is unwriteable, then callback should be notified that
// verification failed.
TEST_F(DownloadPathReservationTrackerTest, UnwriteableDirectory) {
scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
base::FilePath path(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
base::FilePath dir(path.DirName());
ASSERT_FALSE(IsPathInUse(path));
{
// Scope for PermissionRestorer
file_util::PermissionRestorer restorer(dir);
EXPECT_TRUE(file_util::MakeFileUnwritable(dir));
base::FilePath reserved_path;
bool verified = true;
DownloadPathReservationTracker::FilenameConflictAction conflict_action =
DownloadPathReservationTracker::OVERWRITE;
bool create_directory = false;
CallGetReservedPath(
*item,
path,
create_directory,
conflict_action,
&reserved_path,
&verified);
// Verification fails.
EXPECT_FALSE(verified);
EXPECT_EQ(path.BaseName().value(), reserved_path.BaseName().value());
}
}
// If the default download directory doesn't exist, then it should be
// created. But only if we are actually going to create the download path there.
TEST_F(DownloadPathReservationTrackerTest, CreateDefaultDownloadPath) {
base::FilePath path(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo/foo.txt")));
base::FilePath dir(path.DirName());
ASSERT_FALSE(file_util::DirectoryExists(dir));
DownloadPathReservationTracker::FilenameConflictAction conflict_action =
DownloadPathReservationTracker::OVERWRITE;
bool create_directory = false;
{
scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
base::FilePath reserved_path;
bool verified = true;
CallGetReservedPath(
*item,
path,
create_directory,
conflict_action,
&reserved_path,
&verified);
// Verification fails because the directory doesn't exist.
EXPECT_FALSE(verified);
}
ASSERT_FALSE(IsPathInUse(path));
{
scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
base::FilePath reserved_path;
bool verified = true;
set_default_download_path(dir);
CallGetReservedPath(
*item,
path,
create_directory,
conflict_action,
&reserved_path,
&verified);
// Verification succeeds because the directory is created.
EXPECT_TRUE(verified);
EXPECT_TRUE(file_util::DirectoryExists(dir));
}
}
// If the target path of the download item changes, the reservation should be
// updated to match.
TEST_F(DownloadPathReservationTrackerTest, UpdatesToTargetPath) {
scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
base::FilePath path(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("foo.txt")));
ASSERT_FALSE(IsPathInUse(path));
base::FilePath reserved_path;
bool verified = false;
DownloadPathReservationTracker::FilenameConflictAction conflict_action =
DownloadPathReservationTracker::OVERWRITE;
bool create_directory = false;
CallGetReservedPath(
*item,
path,
create_directory,
conflict_action,
&reserved_path,
&verified);
EXPECT_TRUE(IsPathInUse(path));
EXPECT_TRUE(verified);
EXPECT_EQ(path.value(), reserved_path.value());
// The target path is initially empty. If an OnDownloadUpdated() is issued in
// this state, we shouldn't lose the reservation.
ASSERT_EQ(base::FilePath::StringType(), item->GetTargetFilePath().value());
item->UpdateObservers();
message_loop_.RunUntilIdle();
EXPECT_TRUE(IsPathInUse(path));
// If the target path changes, we should update the reservation to match.
base::FilePath new_target_path(
GetPathInDownloadsDirectory(FILE_PATH_LITERAL("bar.txt")));
ASSERT_FALSE(IsPathInUse(new_target_path));
EXPECT_CALL(*item, GetTargetFilePath())
.WillRepeatedly(ReturnRef(new_target_path));
item->UpdateObservers();
message_loop_.RunUntilIdle();
EXPECT_FALSE(IsPathInUse(path));
EXPECT_TRUE(IsPathInUse(new_target_path));
// Destroying the item should release the reservation.
item.reset();
message_loop_.RunUntilIdle();
EXPECT_FALSE(IsPathInUse(new_target_path));
}
// Tests for long name truncation. On other platforms automatic truncation
// is not performed (yet).
#if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS)
TEST_F(DownloadPathReservationTrackerTest, BasicTruncation) {
int real_max_length =
file_util::GetMaximumPathComponentLength(default_download_path());
ASSERT_NE(-1, real_max_length);
// TODO(kinaba): the current implementation leaves spaces for appending
// ".crdownload". So take it into account. Should be removed in the future.
const size_t max_length = real_max_length - 11;
scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
base::FilePath path(GetLongNamePathInDownloadsDirectory(
max_length, FILE_PATH_LITERAL(".txt")));
ASSERT_FALSE(IsPathInUse(path));
base::FilePath reserved_path;
bool verified = false;
DownloadPathReservationTracker::FilenameConflictAction conflict_action =
DownloadPathReservationTracker::OVERWRITE;
bool create_directory = false;
CallGetReservedPath(
*item,
path,
create_directory,
conflict_action,
&reserved_path,
&verified);
EXPECT_TRUE(IsPathInUse(reserved_path));
EXPECT_TRUE(verified);
// The file name length is truncated to max_length.
EXPECT_EQ(max_length, reserved_path.BaseName().value().size());
// But the extension is kept unchanged.
EXPECT_EQ(path.Extension(), reserved_path.Extension());
}
TEST_F(DownloadPathReservationTrackerTest, TruncationConflict) {
int real_max_length =
file_util::GetMaximumPathComponentLength(default_download_path());
ASSERT_NE(-1, real_max_length);
const size_t max_length = real_max_length - 11;
scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
base::FilePath path(GetLongNamePathInDownloadsDirectory(
max_length, FILE_PATH_LITERAL(".txt")));
base::FilePath path0(GetLongNamePathInDownloadsDirectory(
max_length - 4, FILE_PATH_LITERAL(".txt")));
base::FilePath path1(GetLongNamePathInDownloadsDirectory(
max_length - 8, FILE_PATH_LITERAL(" (1).txt")));
base::FilePath path2(GetLongNamePathInDownloadsDirectory(
max_length - 8, FILE_PATH_LITERAL(" (2).txt")));
ASSERT_FALSE(IsPathInUse(path));
// "aaa...aaaaaaa.txt" (truncated path) and
// "aaa...aaa (1).txt" (truncated and first uniquification try) exists.
// "aaa...aaa (2).txt" should be used.
ASSERT_EQ(0, file_util::WriteFile(path0, "", 0));
ASSERT_EQ(0, file_util::WriteFile(path1, "", 0));
base::FilePath reserved_path;
bool verified = false;
DownloadPathReservationTracker::FilenameConflictAction conflict_action =
DownloadPathReservationTracker::UNIQUIFY;
bool create_directory = false;
CallGetReservedPath(
*item,
path,
create_directory,
conflict_action,
&reserved_path,
&verified);
EXPECT_TRUE(IsPathInUse(reserved_path));
EXPECT_TRUE(verified);
EXPECT_EQ(path2, reserved_path);
}
TEST_F(DownloadPathReservationTrackerTest, TruncationFail) {
int real_max_length =
file_util::GetMaximumPathComponentLength(default_download_path());
ASSERT_NE(-1, real_max_length);
const size_t max_length = real_max_length - 11;
scoped_ptr<FakeDownloadItem> item(CreateDownloadItem(1));
base::FilePath path(GetPathInDownloadsDirectory(
(FILE_PATH_LITERAL("a.") +
base::FilePath::StringType(max_length, 'b')).c_str()));
ASSERT_FALSE(IsPathInUse(path));
base::FilePath reserved_path;
bool verified = false;
DownloadPathReservationTracker::FilenameConflictAction conflict_action =
DownloadPathReservationTracker::OVERWRITE;
bool create_directory = false;
CallGetReservedPath(
*item,
path,
create_directory,
conflict_action,
&reserved_path,
&verified);
// We cannot truncate a path with very long extension.
EXPECT_FALSE(verified);
}
#endif
|