-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata_cache_test.cc
195 lines (154 loc) · 6.67 KB
/
metadata_cache_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
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2018 The ChromiumOS Authors
// 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/metadata_cache.h"
namespace smbprovider {
namespace {
bool AreEntriesEqual(const DirectoryEntry& lhs, const DirectoryEntry& rhs) {
return lhs.name == rhs.name && lhs.full_path == rhs.full_path &&
lhs.size == rhs.size &&
lhs.last_modified_time == rhs.last_modified_time &&
lhs.is_directory == rhs.is_directory;
}
} // namespace
class MetadataCacheTest : public testing::Test {
public:
MetadataCacheTest() {
cache_lifetime_ = base::Microseconds(kMetadataCacheLifetimeMicroseconds);
tick_clock_ = std::make_unique<base::SimpleTestTickClock>();
cache_ = std::make_unique<MetadataCache>(tick_clock_.get(), cache_lifetime_,
MetadataCache::Mode::kStandard);
}
MetadataCacheTest(const MetadataCacheTest&) = delete;
MetadataCacheTest& operator=(const MetadataCacheTest&) = delete;
~MetadataCacheTest() override = default;
protected:
base::TimeDelta cache_lifetime_;
std::unique_ptr<MetadataCache> cache_;
std::unique_ptr<base::SimpleTestTickClock> tick_clock_;
};
TEST_F(MetadataCacheTest, FindOnEmptyCache) {
DirectoryEntry found_entry;
EXPECT_FALSE(cache_->FindEntry("smb://server/share/not/found", &found_entry));
}
TEST_F(MetadataCacheTest, AddAndFindEntry) {
const std::string name = "file";
const std::string full_path = "smb://server/share/dir/" + name;
DirectoryEntry found_entry;
EXPECT_FALSE(cache_->FindEntry(full_path, &found_entry));
const int64_t expected_size = 1234;
const int64_t expected_date = 9999999;
const DirectoryEntry expected_entry(false /* is_directory */, name, full_path,
expected_size, expected_date);
cache_->AddEntry(expected_entry);
EXPECT_FALSE(cache_->IsEmpty());
EXPECT_TRUE(cache_->FindEntry(full_path, &found_entry));
EXPECT_TRUE(AreEntriesEqual(expected_entry, found_entry));
// Verify it can be found again.
DirectoryEntry found_entry2;
EXPECT_TRUE(cache_->FindEntry(full_path, &found_entry2));
EXPECT_TRUE(AreEntriesEqual(expected_entry, found_entry2));
}
TEST_F(MetadataCacheTest, AddAndFindEntryOnExpirationBoundary) {
const std::string name = "file";
const std::string full_path = "smb://server/share/dir/" + name;
DirectoryEntry found_entry;
EXPECT_FALSE(cache_->FindEntry(full_path, &found_entry));
const int64_t expected_size = 1234;
const int64_t expected_date = 9999999;
const DirectoryEntry expected_entry(false /* is_directory */, name, full_path,
expected_size, expected_date);
cache_->AddEntry(expected_entry);
EXPECT_FALSE(cache_->IsEmpty());
EXPECT_TRUE(cache_->FindEntry(full_path, &found_entry));
EXPECT_TRUE(AreEntriesEqual(expected_entry, found_entry));
// Advance the clock to the last tick where it is still valid.
tick_clock_->Advance(base::Microseconds(kMetadataCacheLifetimeMicroseconds));
// Verify it can be found again.
DirectoryEntry found_entry2;
EXPECT_TRUE(cache_->FindEntry(full_path, &found_entry2));
EXPECT_TRUE(AreEntriesEqual(expected_entry, found_entry2));
EXPECT_FALSE(cache_->IsEmpty());
// Advance one more tick to expire it.
tick_clock_->Advance(base::Microseconds(1));
// Verify it is not found any more and removed from cache.
DirectoryEntry found_entry3;
EXPECT_FALSE(cache_->FindEntry(full_path, &found_entry3));
EXPECT_TRUE(cache_->IsEmpty());
}
TEST_F(MetadataCacheTest, PurgeExpiresEntries) {
const std::string name = "file";
const std::string full_path = "smb://server/share/dir/" + name;
DirectoryEntry found_entry;
EXPECT_FALSE(cache_->FindEntry(full_path, &found_entry));
const int64_t expected_size = 1234;
const int64_t expected_date = 9999999;
const DirectoryEntry expected_entry(false /* is_directory */, name, full_path,
expected_size, expected_date);
cache_->AddEntry(expected_entry);
EXPECT_FALSE(cache_->IsEmpty());
EXPECT_TRUE(cache_->FindEntry(full_path, &found_entry));
EXPECT_TRUE(AreEntriesEqual(expected_entry, found_entry));
// Advance the clock to the last tick where it is still valid.
tick_clock_->Advance(base::Microseconds(kMetadataCacheLifetimeMicroseconds));
// Purge the cache but it shouldn't expire anything
cache_->PurgeExpiredEntries();
EXPECT_FALSE(cache_->IsEmpty());
// Verify it can be found again.
DirectoryEntry found_entry2;
EXPECT_TRUE(cache_->FindEntry(full_path, &found_entry2));
EXPECT_TRUE(AreEntriesEqual(expected_entry, found_entry2));
EXPECT_FALSE(cache_->IsEmpty());
// Advance one more tick to expire it.
tick_clock_->Advance(base::Microseconds(1));
// Purging should now remove the entry.
cache_->PurgeExpiredEntries();
EXPECT_TRUE(cache_->IsEmpty());
}
TEST_F(MetadataCacheTest, IsEmptyOnNewCache) {
EXPECT_TRUE(cache_->IsEmpty());
}
TEST_F(MetadataCacheTest, ClearOnEmptyCache) {
EXPECT_TRUE(cache_->IsEmpty());
cache_->ClearAll();
EXPECT_TRUE(cache_->IsEmpty());
}
TEST_F(MetadataCacheTest, ClearRemovesItems) {
EXPECT_TRUE(cache_->IsEmpty());
const std::string name = "path";
const std::string full_path = "smb://server/share/some/" + name;
cache_->AddEntry(DirectoryEntry(false /* is_directory */, name, full_path,
1234 /* size */, 999999 /* date */));
EXPECT_FALSE(cache_->IsEmpty());
cache_->ClearAll();
EXPECT_TRUE(cache_->IsEmpty());
}
TEST_F(MetadataCacheTest, RemoveItemExplicitly) {
EXPECT_TRUE(cache_->IsEmpty());
const std::string name = "path";
const std::string full_path = "smb://server/share/some/" + name;
cache_->AddEntry(DirectoryEntry(false /* is_directory */, name, full_path,
1234 /* size */, 999999 /* date */));
EXPECT_FALSE(cache_->IsEmpty());
EXPECT_TRUE(cache_->RemoveEntry(full_path));
EXPECT_TRUE(cache_->IsEmpty());
}
TEST_F(MetadataCacheTest, RemoveItemThatDoesntExist) {
EXPECT_TRUE(cache_->IsEmpty());
const std::string name = "path";
const std::string full_path = "smb://server/share/some/" + name;
cache_->AddEntry(DirectoryEntry(false /* is_directory */, name, full_path,
1234 /* size */, 999999 /* date */));
EXPECT_FALSE(cache_->IsEmpty());
// Removal of an entry that doesn't exist returns false and
// doesn't change the cache.
EXPECT_FALSE(cache_->RemoveEntry(full_path + "2"));
EXPECT_FALSE(cache_->IsEmpty());
DirectoryEntry entry;
EXPECT_TRUE(cache_->FindEntry(full_path, &entry));
}
} // namespace smbprovider