-
Notifications
You must be signed in to change notification settings - Fork 1
/
read_dir_progress_test.cc
183 lines (138 loc) · 5.7 KB
/
read_dir_progress_test.cc
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
// Copyright 2018 The Chromium OS 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 <memory>
#include <base/test/simple_test_tick_clock.h>
#include <gtest/gtest.h>
#include "smbprovider/constants.h"
#include "smbprovider/fake_samba_interface.h"
#include "smbprovider/iterator/caching_iterator.h"
#include "smbprovider/metadata_cache.h"
#include "smbprovider/read_dir_progress.h"
#include "smbprovider/smbprovider_test_helper.h"
namespace smbprovider {
class ReadDirProgressTest : public testing::Test {
public:
ReadDirProgressTest()
: fake_samba_(std::make_unique<FakeSambaInterface>()),
fake_tick_clock_(std::make_unique<base::SimpleTestTickClock>()) {
cache_ = std::make_unique<MetadataCache>(
fake_tick_clock_.get(),
base::TimeDelta::FromMicroseconds(kMetadataCacheLifetimeMicroseconds),
MetadataCache::Mode::kDisabled);
}
ReadDirProgressTest(const ReadDirProgressTest&) = delete;
ReadDirProgressTest& operator=(const ReadDirProgressTest&) = delete;
~ReadDirProgressTest() override = default;
protected:
void PrepareFileSystem() {
fake_samba_->AddDirectory(GetDefaultServer());
fake_samba_->AddDirectory(GetDefaultMountRoot());
}
std::unique_ptr<FakeSambaInterface> fake_samba_;
std::unique_ptr<base::TickClock> fake_tick_clock_;
std::unique_ptr<MetadataCache> cache_;
};
TEST_F(ReadDirProgressTest, StartSucceedsOnEmptyDir) {
const std::string empty_dir = GetDefaultFullPath("dir");
PrepareFileSystem();
fake_samba_->AddDirectory(empty_dir);
ReadDirProgress read_dir_progress(fake_samba_.get());
int32_t error;
DirectoryEntryListProto entries;
bool should_continue_read_dir =
read_dir_progress.StartReadDir(empty_dir, cache_.get(), &error, &entries);
EXPECT_FALSE(should_continue_read_dir);
EXPECT_EQ(0, error);
EXPECT_EQ(0, entries.entries_size());
}
TEST_F(ReadDirProgressTest, StartSucceedsOnNonEmptyDir) {
const std::string dir_path = GetDefaultFullPath("dir");
const std::string file_name = "file1.txt";
const std::string file_path = dir_path + "/" + file_name;
PrepareFileSystem();
fake_samba_->AddDirectory(dir_path);
fake_samba_->AddFile(file_path);
ReadDirProgress read_dir_progress(fake_samba_.get());
int32_t error;
DirectoryEntryListProto entries;
bool should_continue_read_dir =
read_dir_progress.StartReadDir(dir_path, cache_.get(), &error, &entries);
EXPECT_FALSE(should_continue_read_dir);
EXPECT_EQ(0, error);
EXPECT_EQ(1, entries.entries_size());
EXPECT_EQ(file_name, entries.entries(0).name());
}
TEST_F(ReadDirProgressTest, StartFailsWhenDirectoryDoesNotExist) {
const std::string dir_path = GetDefaultFullPath("dir");
ReadDirProgress read_dir_progress(fake_samba_.get());
int32_t error;
DirectoryEntryListProto entries;
bool should_continue_read_dir =
read_dir_progress.StartReadDir(dir_path, cache_.get(), &error, &entries);
EXPECT_FALSE(should_continue_read_dir);
EXPECT_EQ(ENOENT, error);
}
TEST_F(ReadDirProgressTest, StartAndContinue) {
const std::string dir_path = GetDefaultFullPath("dir");
const std::string file_name_1 = "file1.txt";
const std::string file_path_1 = dir_path + "/" + file_name_1;
const std::string file_name_2 = "file2.txt";
const std::string file_path_2 = dir_path + "/" + file_name_2;
PrepareFileSystem();
fake_samba_->AddDirectory(dir_path);
fake_samba_->AddFile(file_path_1);
fake_samba_->AddFile(file_path_2);
ReadDirProgress read_dir_progress(fake_samba_.get(),
1 /* initial_batch_size*/);
int32_t error;
DirectoryEntryListProto entries;
bool should_continue_read_dir =
read_dir_progress.StartReadDir(dir_path, cache_.get(), &error, &entries);
EXPECT_TRUE(should_continue_read_dir);
EXPECT_EQ(0, error);
EXPECT_EQ(1, entries.entries_size());
EXPECT_EQ(file_name_1, entries.entries(0).name());
should_continue_read_dir =
read_dir_progress.ContinueReadDir(&error, &entries);
EXPECT_FALSE(should_continue_read_dir);
EXPECT_EQ(0, error);
EXPECT_EQ(1, entries.entries_size());
EXPECT_EQ(file_name_2, entries.entries(0).name());
}
TEST_F(ReadDirProgressTest, BatchSizeDoubles) {
const std::string dir_path = GetDefaultFullPath("dir");
const std::string file_name_1 = "file1.txt";
const std::string file_path_1 = dir_path + "/" + file_name_1;
const std::string file_name_2 = "file2.txt";
const std::string file_path_2 = dir_path + "/" + file_name_2;
const std::string file_name_3 = "file3.txt";
const std::string file_path_3 = dir_path + "/" + file_name_3;
const std::string file_name_4 = "file4.txt";
const std::string file_path_4 = dir_path + "/" + file_name_4;
PrepareFileSystem();
fake_samba_->AddDirectory(dir_path);
fake_samba_->AddFile(file_path_1);
fake_samba_->AddFile(file_path_2);
fake_samba_->AddFile(file_path_3);
fake_samba_->AddFile(file_path_4);
ReadDirProgress read_dir_progress(fake_samba_.get(),
1 /* initial_batch_size*/);
int32_t error;
DirectoryEntryListProto entries;
bool should_continue_read_dir =
read_dir_progress.StartReadDir(dir_path, cache_.get(), &error, &entries);
EXPECT_TRUE(should_continue_read_dir);
EXPECT_EQ(1, entries.entries_size());
should_continue_read_dir =
read_dir_progress.ContinueReadDir(&error, &entries);
EXPECT_TRUE(should_continue_read_dir);
// Batch size doubles to two.
EXPECT_EQ(2, entries.entries_size());
should_continue_read_dir =
read_dir_progress.ContinueReadDir(&error, &entries);
EXPECT_FALSE(should_continue_read_dir);
// Last remaining entry should be returned.
EXPECT_EQ(1, entries.entries_size());
}
} // namespace smbprovider