summaryrefslogtreecommitdiff
path: root/chromium/net/third_party/quiche/src/quiche/common/platform/api/quiche_file_utils_test.cc
blob: 68387a421cc4ce176b75310b81a4272169660e23 (plain)
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
#include "quiche/common/platform/api/quiche_file_utils.h"

#include <vector>

#include "absl/algorithm/container.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/strip.h"
#include "absl/types/optional.h"
#include "quiche/common/platform/api/quiche_test.h"

namespace quiche {
namespace test {
namespace {

using testing::UnorderedElementsAre;
using testing::UnorderedElementsAreArray;

TEST(QuicheFileUtilsTest, ReadFileContents) {
  std::string path = absl::StrCat(QuicheGetCommonSourcePath(),
                                  "/platform/api/testdir/testfile");
  absl::optional<std::string> contents = ReadFileContents(path);
  ASSERT_TRUE(contents.has_value());
  EXPECT_EQ(*contents, "This is a test file.");
}

TEST(QuicheFileUtilsTest, ReadFileContentsFileNotFound) {
  std::string path =
      absl::StrCat(QuicheGetCommonSourcePath(),
                   "/platform/api/testdir/file-that-does-not-exist");
  absl::optional<std::string> contents = ReadFileContents(path);
  EXPECT_FALSE(contents.has_value());
}

TEST(QuicheFileUtilsTest, EnumerateDirectory) {
  std::string path =
      absl::StrCat(QuicheGetCommonSourcePath(), "/platform/api/testdir");
  std::vector<std::string> dirs;
  std::vector<std::string> files;
  bool success = EnumerateDirectory(path, dirs, files);
  EXPECT_TRUE(success);
  EXPECT_THAT(files, UnorderedElementsAre("testfile", "README.md"));
  EXPECT_THAT(dirs, UnorderedElementsAre("a"));
}

TEST(QuicheFileUtilsTest, EnumerateDirectoryNoSuchDirectory) {
  std::string path = absl::StrCat(QuicheGetCommonSourcePath(),
                                  "/platform/api/testdir/no-such-directory");
  std::vector<std::string> dirs;
  std::vector<std::string> files;
  bool success = EnumerateDirectory(path, dirs, files);
  EXPECT_FALSE(success);
}

TEST(QuicheFileUtilsTest, EnumerateDirectoryNotADirectory) {
  std::string path = absl::StrCat(QuicheGetCommonSourcePath(),
                                  "/platform/api/testdir/testfile");
  std::vector<std::string> dirs;
  std::vector<std::string> files;
  bool success = EnumerateDirectory(path, dirs, files);
  EXPECT_FALSE(success);
}

TEST(QuicheFileUtilsTest, EnumerateDirectoryRecursively) {
  std::vector<std::string> expected_paths = {"a/b/c/d/e", "a/subdir/testfile",
                                             "a/z", "testfile", "README.md"};

  std::string root_path =
      absl::StrCat(QuicheGetCommonSourcePath(), "/platform/api/testdir");
  for (std::string& path : expected_paths) {
    // For Windows, use Windows path separators.
    if (JoinPath("a", "b") == "a\\b") {
      absl::c_replace(path, '/', '\\');
    }

    path = JoinPath(root_path, path);
  }

  std::vector<std::string> files;
  bool success = EnumerateDirectoryRecursively(root_path, files);
  EXPECT_TRUE(success);
  EXPECT_THAT(files, UnorderedElementsAreArray(expected_paths));
}

}  // namespace
}  // namespace test
}  // namespace quiche