1 // Copyright 2019 The Chromium OS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Filesystem-related utility functions. 6 7 #ifndef LIBBRILLO_BRILLO_FILES_FILE_UTIL_TEST_H_ 8 #define LIBBRILLO_BRILLO_FILES_FILE_UTIL_TEST_H_ 9 10 #include <string> 11 #include <vector> 12 13 #include <base/files/scoped_temp_dir.h> 14 #include <brillo/files/safe_fd.h> 15 #include <gtest/gtest.h> 16 17 namespace brillo { 18 19 // Convert the SafeFD::Error enum class to a string for readability of 20 // test results. 21 std::string to_string(brillo::SafeFD::Error err); 22 23 // Helper to enable gtest to print SafeFD::Error results in a way that is easier 24 // to read. 25 std::ostream& operator<<(std::ostream& os, const brillo::SafeFD::Error err); 26 27 // Gets a short random string that can be used as part of a file name. 28 std::string GetRandomSuffix(); 29 30 class FileTest : public testing::Test { 31 public: 32 static constexpr char kFileName[] = "test.temp"; 33 static constexpr char kSubdirName[] = "test_dir"; 34 static constexpr char kSymbolicFileName[] = "sym_test.temp"; 35 static constexpr char kSymbolicDirName[] = "sym_dir"; 36 37 static void SetUpTestCase(); 38 39 FileTest(); 40 41 protected: 42 std::vector<char> temp_dir_path_; 43 base::FilePath file_path_; 44 base::FilePath sub_dir_path_; 45 base::FilePath symlink_file_path_; 46 base::FilePath symlink_dir_path_; 47 base::ScopedTempDir temp_dir_; 48 SafeFD root_; 49 50 bool SetupSubdir() WARN_UNUSED_RESULT; 51 52 bool SetupSymlinks() WARN_UNUSED_RESULT; 53 54 // Writes |contents| to |file_path_|. Pulled into a separate function just 55 // to improve readability of tests. 56 bool WriteFile(const std::string& contents) WARN_UNUSED_RESULT; 57 58 // Verifies that the file at |file_path_| exists and contains |contents|. 59 void ExpectFileContains(const std::string& contents); 60 61 // Verifies that the file at |file_path_| has |permissions|. 62 void ExpectPermissions(base::FilePath path, int permissions); 63 64 // Creates a file with a random name in the temporary directory. 65 base::FilePath GetTempName() WARN_UNUSED_RESULT; 66 }; 67 68 } // namespace brillo 69 70 #endif // LIBBRILLO_BRILLO_FILES_FILE_UTIL_TEST_H_ 71