aboutsummaryrefslogtreecommitdiff
path: root/brillo/files/file_util_test.h
blob: 182cdf4e283d7c2e0b04bdc93277c82bf3138441 (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
// Copyright 2019 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.

// Filesystem-related utility functions.

#ifndef LIBBRILLO_BRILLO_FILES_FILE_UTIL_TEST_H_
#define LIBBRILLO_BRILLO_FILES_FILE_UTIL_TEST_H_

#include <string>
#include <vector>

#include <base/files/scoped_temp_dir.h>
#include <brillo/files/safe_fd.h>
#include <gtest/gtest.h>

namespace brillo {

// Convert the SafeFD::Error enum class to a string for readability of
// test results.
std::string to_string(brillo::SafeFD::Error err);

// Helper to enable gtest to print SafeFD::Error results in a way that is easier
// to read.
std::ostream& operator<<(std::ostream& os, const brillo::SafeFD::Error err);

// Gets a short random string that can be used as part of a file name.
std::string GetRandomSuffix();

class FileTest : public testing::Test {
 public:
  static constexpr char kFileName[] = "test.temp";
  static constexpr char kSubdirName[] = "test_dir";
  static constexpr char kSymbolicFileName[] = "sym_test.temp";
  static constexpr char kSymbolicDirName[] = "sym_dir";

  static void SetUpTestCase();

  FileTest();

 protected:
  std::vector<char> temp_dir_path_;
  base::FilePath file_path_;
  base::FilePath sub_dir_path_;
  base::FilePath symlink_file_path_;
  base::FilePath symlink_dir_path_;
  base::ScopedTempDir temp_dir_;
  SafeFD root_;

  bool SetupSubdir() WARN_UNUSED_RESULT;

  bool SetupSymlinks() WARN_UNUSED_RESULT;

  // Writes |contents| to |file_path_|. Pulled into a separate function just
  // to improve readability of tests.
  bool WriteFile(const std::string& contents) WARN_UNUSED_RESULT;

  // Verifies that the file at |file_path_| exists and contains |contents|.
  void ExpectFileContains(const std::string& contents);

  // Verifies that the file at |file_path_| has |permissions|.
  void ExpectPermissions(base::FilePath path, int permissions);

  // Creates a file with a random name in the temporary directory.
  base::FilePath GetTempName() WARN_UNUSED_RESULT;
};

}  // namespace brillo

#endif  // LIBBRILLO_BRILLO_FILES_FILE_UTIL_TEST_H_