xref: /aosp_15_r20/external/cronet/net/disk_cache/simple/simple_file_enumerator_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/disk_cache/simple/simple_file_enumerator.h"
6 
7 #include "base/path_service.h"
8 #include "net/test/gtest_util.h"
9 #include "net/test/test_with_task_environment.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 
12 namespace disk_cache {
13 namespace {
14 
GetRoot()15 base::FilePath GetRoot() {
16   base::FilePath root;
17   base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &root);
18   return root.AppendASCII("net")
19       .AppendASCII("data")
20       .AppendASCII("cache_tests")
21       .AppendASCII("simple_file_enumerator");
22 }
23 
TEST(SimpleFileEnumeratorTest,Root)24 TEST(SimpleFileEnumeratorTest, Root) {
25   const base::FilePath kRoot = GetRoot();
26   SimpleFileEnumerator enumerator(kRoot);
27 
28   auto entry = enumerator.Next();
29   ASSERT_TRUE(entry.has_value());
30   EXPECT_EQ(entry->path, kRoot.AppendASCII("test.txt"));
31   EXPECT_EQ(entry->size, 13);
32   EXPECT_FALSE(enumerator.HasError());
33 
34   // No directories should be listed, no indirect descendants should be listed.
35   EXPECT_EQ(std::nullopt, enumerator.Next());
36   EXPECT_FALSE(enumerator.HasError());
37 
38   // We can call enumerator.Next() after the iteration is done.
39   EXPECT_EQ(std::nullopt, enumerator.Next());
40   EXPECT_FALSE(enumerator.HasError());
41 }
42 
TEST(SimpleFileEnumeratorTest,NotFound)43 TEST(SimpleFileEnumeratorTest, NotFound) {
44   const base::FilePath kRoot = GetRoot().AppendASCII("not-found");
45   SimpleFileEnumerator enumerator(kRoot);
46 
47   auto entry = enumerator.Next();
48   EXPECT_EQ(std::nullopt, enumerator.Next());
49 #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
50   EXPECT_TRUE(enumerator.HasError());
51 #endif
52 }
53 
54 }  // namespace
55 }  // namespace disk_cache
56