File: file_system_usage_cache_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (162 lines) | stat: -rw-r--r-- 6,033 bytes parent folder | download | duplicates (11)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "storage/browser/file_system/file_system_usage_cache.h"

#include <stdint.h>

#include <limits>

#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/test/task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace storage {

class FileSystemUsageCacheTest : public testing::Test,
                                 public ::testing::WithParamInterface<bool> {
 public:
  FileSystemUsageCacheTest() : usage_cache_(is_incognito()) {}

  FileSystemUsageCacheTest(const FileSystemUsageCacheTest&) = delete;
  FileSystemUsageCacheTest& operator=(const FileSystemUsageCacheTest&) = delete;

  void SetUp() override { ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); }

  bool is_incognito() { return GetParam(); }

 protected:
  base::FilePath GetUsageFilePath() {
    return data_dir_.GetPath().Append(FileSystemUsageCache::kUsageFileName);
  }

  FileSystemUsageCache* usage_cache() { return &usage_cache_; }

 private:
  base::test::SingleThreadTaskEnvironment task_environment_;
  base::ScopedTempDir data_dir_;
  FileSystemUsageCache usage_cache_;
};

INSTANTIATE_TEST_SUITE_P(All, FileSystemUsageCacheTest, testing::Bool());

TEST_P(FileSystemUsageCacheTest, CreateTest) {
  base::FilePath usage_file_path = GetUsageFilePath();
  EXPECT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 0));
}

TEST_P(FileSystemUsageCacheTest, SetSizeTest) {
  static const int64_t size = 240122;
  base::FilePath usage_file_path = GetUsageFilePath();
  int64_t usage = 0;
  ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size));
  EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage));
  EXPECT_EQ(size, usage);
}

TEST_P(FileSystemUsageCacheTest, SetLargeSizeTest) {
  static const int64_t size = std::numeric_limits<int64_t>::max();
  base::FilePath usage_file_path = GetUsageFilePath();
  int64_t usage = 0;
  ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size));
  EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage));
  EXPECT_EQ(size, usage);
}

TEST_P(FileSystemUsageCacheTest, IncAndGetSizeTest) {
  base::FilePath usage_file_path = GetUsageFilePath();
  uint32_t dirty = 0;
  int64_t usage = 0;
  ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 98214));
  ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path));
  EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path, &dirty));
  EXPECT_EQ(1u, dirty);
  EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage));
  EXPECT_EQ(98214, usage);
}

TEST_P(FileSystemUsageCacheTest, DecAndGetSizeTest) {
  static const int64_t size = 71839;
  base::FilePath usage_file_path = GetUsageFilePath();
  int64_t usage = 0;
  ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size));
  // DecrementDirty for dirty = 0 is invalid. It returns false.
  ASSERT_FALSE(usage_cache()->DecrementDirty(usage_file_path));
  EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage));
  EXPECT_EQ(size, usage);
}

TEST_P(FileSystemUsageCacheTest, IncDecAndGetSizeTest) {
  static const int64_t size = 198491;
  base::FilePath usage_file_path = GetUsageFilePath();
  int64_t usage = 0;
  ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size));
  ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path));
  ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path));
  EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage));
  EXPECT_EQ(size, usage);
}

TEST_P(FileSystemUsageCacheTest, DecIncAndGetSizeTest) {
  base::FilePath usage_file_path = GetUsageFilePath();
  uint32_t dirty = 0;
  int64_t usage = 0;
  ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 854238));
  // DecrementDirty for dirty = 0 is invalid. It returns false.
  ASSERT_FALSE(usage_cache()->DecrementDirty(usage_file_path));
  ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path));
  // It tests DecrementDirty (which returns false) has no effect, i.e
  // does not make dirty = -1 after DecrementDirty.
  EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path, &dirty));
  EXPECT_EQ(1u, dirty);
  EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage));
  EXPECT_EQ(854238, usage);
}

TEST_P(FileSystemUsageCacheTest, ManyIncsSameDecsAndGetSizeTest) {
  static const int64_t size = 82412;
  base::FilePath usage_file_path = GetUsageFilePath();
  int64_t usage = 0;
  ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size));
  for (int i = 0; i < 20; i++)
    ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path));
  for (int i = 0; i < 20; i++)
    ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path));
  EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage));
  EXPECT_EQ(size, usage);
}

TEST_P(FileSystemUsageCacheTest, ManyIncsLessDecsAndGetSizeTest) {
  uint32_t dirty = 0;
  int64_t usage = 0;
  base::FilePath usage_file_path = GetUsageFilePath();
  ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 19319));
  for (int i = 0; i < 20; i++)
    ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path));
  for (int i = 0; i < 19; i++)
    ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path));
  EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path, &dirty));
  EXPECT_EQ(1u, dirty);
  EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage));
  EXPECT_EQ(19319, usage);
}

TEST_P(FileSystemUsageCacheTest, GetSizeWithoutCacheFileTest) {
  int64_t usage = 0;
  base::FilePath usage_file_path = GetUsageFilePath();
  EXPECT_FALSE(usage_cache()->GetUsage(usage_file_path, &usage));
}

TEST_P(FileSystemUsageCacheTest, IncrementDirtyWithoutCacheFileTest) {
  base::FilePath usage_file_path = GetUsageFilePath();
  EXPECT_FALSE(usage_cache()->IncrementDirty(usage_file_path));
}

TEST_P(FileSystemUsageCacheTest, DecrementDirtyWithoutCacheFileTest) {
  base::FilePath usage_file_path = GetUsageFilePath();
  EXPECT_FALSE(usage_cache()->IncrementDirty(usage_file_path));
}

}  // namespace storage