kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 1 | /* |
phoglund@webrtc.org | 9d9ad88 | 2012-02-07 16:16:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 11 | #include "test/testsupport/file_utils.h" |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <stdio.h> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <algorithm> |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 15 | #include <fstream> |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 16 | #include <string> |
| 17 | |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 18 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/checks.h" |
Patrik Höglund | 8434aeb | 2018-10-05 14:52:11 +0200 | [diff] [blame] | 20 | #include "test/gmock.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "test/gtest.h" |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 22 | |
| 23 | #ifdef WIN32 |
phoglund@webrtc.org | 9d9ad88 | 2012-02-07 16:16:52 +0000 | [diff] [blame] | 24 | #define chdir _chdir |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 25 | #endif |
| 26 | |
Patrik Höglund | 8434aeb | 2018-10-05 14:52:11 +0200 | [diff] [blame] | 27 | using ::testing::EndsWith; |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 28 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 29 | namespace webrtc { |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 30 | namespace test { |
| 31 | |
| 32 | namespace { |
| 33 | |
Patrik Höglund | 8434aeb | 2018-10-05 14:52:11 +0200 | [diff] [blame] | 34 | std::string Path(const std::string& path) { |
| 35 | std::string result = path; |
| 36 | std::replace(result.begin(), result.end(), '/', *kPathDelimiter); |
| 37 | return result; |
| 38 | } |
| 39 | |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 40 | // Remove files and directories in a directory non-recursively and writes the |
| 41 | // number of deleted items in |num_deleted_entries|. |
| 42 | void CleanDir(const std::string& dir, size_t* num_deleted_entries) { |
| 43 | RTC_DCHECK(num_deleted_entries); |
| 44 | *num_deleted_entries = 0; |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 45 | absl::optional<std::vector<std::string>> dir_content = ReadDirectory(dir); |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 46 | EXPECT_TRUE(dir_content); |
| 47 | for (const auto& entry : *dir_content) { |
| 48 | if (DirExists(entry)) { |
| 49 | EXPECT_TRUE(RemoveDir(entry)); |
| 50 | (*num_deleted_entries)++; |
| 51 | } else if (FileExists(entry)) { |
| 52 | EXPECT_TRUE(RemoveFile(entry)); |
| 53 | (*num_deleted_entries)++; |
| 54 | } else { |
| 55 | FAIL(); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void WriteStringInFile(const std::string& what, const std::string& file_path) { |
| 61 | std::ofstream out(file_path); |
| 62 | out << what; |
| 63 | out.close(); |
| 64 | } |
| 65 | |
| 66 | } // namespace |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 67 | |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 68 | // Test fixture to restore the working directory between each test, since some |
| 69 | // of them change it with chdir during execution (not restored by the |
| 70 | // gtest framework). |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 71 | class FileUtilsTest : public testing::Test { |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 72 | protected: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 73 | FileUtilsTest() {} |
ehmaldonado | 37535bf | 2016-12-05 06:42:45 -0800 | [diff] [blame] | 74 | ~FileUtilsTest() override {} |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 75 | // Runs before the first test |
| 76 | static void SetUpTestCase() { |
| 77 | original_working_dir_ = webrtc::test::WorkingDir(); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 78 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 79 | void SetUp() override { ASSERT_EQ(chdir(original_working_dir_.c_str()), 0); } |
ehmaldonado | 37535bf | 2016-12-05 06:42:45 -0800 | [diff] [blame] | 80 | void TearDown() override { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 81 | ASSERT_EQ(chdir(original_working_dir_.c_str()), 0); |
| 82 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 83 | |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 84 | private: |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 85 | static std::string original_working_dir_; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 88 | std::string FileUtilsTest::original_working_dir_ = ""; |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 89 | |
Patrik Höglund | 8434aeb | 2018-10-05 14:52:11 +0200 | [diff] [blame] | 90 | // The location will vary depending on where the webrtc checkout is on the |
| 91 | // system, but it should end as described above and be an absolute path. |
| 92 | std::string ExpectedRootDirByPlatform() { |
| 93 | #if defined(WEBRTC_ANDROID) |
| 94 | return Path("chromium_tests_root/"); |
| 95 | #elif defined(WEBRTC_IOS) |
| 96 | return Path("tmp/"); |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 97 | #else |
Patrik Höglund | 8434aeb | 2018-10-05 14:52:11 +0200 | [diff] [blame] | 98 | return Path("out/"); |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 99 | #endif |
Patrik Höglund | 8434aeb | 2018-10-05 14:52:11 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | TEST_F(FileUtilsTest, OutputPathFromUnchangedWorkingDir) { |
| 103 | std::string expected_end = ExpectedRootDirByPlatform(); |
| 104 | std::string result = webrtc::test::OutputPath(); |
| 105 | |
| 106 | ASSERT_THAT(result, EndsWith(expected_end)); |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 107 | } |
| 108 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 109 | // Tests with current working directory set to a directory higher up in the |
kjellander@webrtc.org | 193600b | 2012-10-17 04:39:44 +0000 | [diff] [blame] | 110 | // directory tree than the project root dir. |
Patrik Höglund | 8434aeb | 2018-10-05 14:52:11 +0200 | [diff] [blame] | 111 | TEST_F(FileUtilsTest, OutputPathFromRootWorkingDir) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 112 | ASSERT_EQ(0, chdir(kPathDelimiter)); |
Patrik Höglund | 8434aeb | 2018-10-05 14:52:11 +0200 | [diff] [blame] | 113 | |
| 114 | std::string expected_end = ExpectedRootDirByPlatform(); |
| 115 | std::string result = webrtc::test::OutputPath(); |
| 116 | |
| 117 | ASSERT_THAT(result, EndsWith(expected_end)); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 118 | } |
| 119 | |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 120 | TEST_F(FileUtilsTest, TempFilename) { |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 121 | std::string temp_filename = webrtc::test::TempFilename( |
| 122 | webrtc::test::OutputPath(), "TempFilenameTest"); |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 123 | ASSERT_TRUE(webrtc::test::FileExists(temp_filename)) |
| 124 | << "Couldn't find file: " << temp_filename; |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 125 | remove(temp_filename.c_str()); |
| 126 | } |
| 127 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 128 | TEST_F(FileUtilsTest, GenerateTempFilename) { |
| 129 | std::string temp_filename = webrtc::test::GenerateTempFilename( |
| 130 | webrtc::test::OutputPath(), "TempFilenameTest"); |
| 131 | ASSERT_FALSE(webrtc::test::FileExists(temp_filename)) |
| 132 | << "File exists: " << temp_filename; |
| 133 | FILE* file = fopen(temp_filename.c_str(), "wb"); |
| 134 | ASSERT_TRUE(file != NULL) << "Failed to open file: " << temp_filename; |
| 135 | ASSERT_GT(fprintf(file, "%s", "Dummy data"), 0) |
| 136 | << "Failed to write to file: " << temp_filename; |
| 137 | fclose(file); |
| 138 | remove(temp_filename.c_str()); |
| 139 | } |
| 140 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 141 | // Only tests that the code executes |
kthelgason | 5d682ca | 2017-01-10 03:00:41 -0800 | [diff] [blame] | 142 | #if defined(WEBRTC_IOS) |
| 143 | #define MAYBE_CreateDir DISABLED_CreateDir |
| 144 | #else |
| 145 | #define MAYBE_CreateDir CreateDir |
| 146 | #endif |
| 147 | TEST_F(FileUtilsTest, MAYBE_CreateDir) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 148 | std::string directory = "fileutils-unittest-empty-dir"; |
| 149 | // Make sure it's removed if a previous test has failed: |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 150 | remove(directory.c_str()); |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 151 | ASSERT_TRUE(webrtc::test::CreateDir(directory)); |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 152 | remove(directory.c_str()); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | TEST_F(FileUtilsTest, WorkingDirReturnsValue) { |
Patrik Höglund | 8434aeb | 2018-10-05 14:52:11 +0200 | [diff] [blame] | 156 | // This will obviously be different depending on where the webrtc checkout is, |
| 157 | // so just check something is returned. |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 158 | std::string working_dir = webrtc::test::WorkingDir(); |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 159 | ASSERT_GT(working_dir.length(), 0u); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Patrik Höglund | 8434aeb | 2018-10-05 14:52:11 +0200 | [diff] [blame] | 162 | TEST_F(FileUtilsTest, ResourcePathReturnsCorrectPath) { |
| 163 | std::string result = webrtc::test::ResourcePath( |
| 164 | Path("video_coding/frame-ethernet-ii"), "pcap"); |
| 165 | #if defined(WEBRTC_IOS) |
| 166 | // iOS bundles resources straight into the bundle root. |
| 167 | std::string expected_end = Path("/frame-ethernet-ii.pcap"); |
| 168 | #else |
| 169 | // Other platforms: it's a separate dir. |
| 170 | std::string expected_end = |
| 171 | Path("resources/video_coding/frame-ethernet-ii.pcap"); |
| 172 | #endif |
| 173 | |
| 174 | ASSERT_THAT(result, EndsWith(expected_end)); |
| 175 | ASSERT_TRUE(FileExists(result)) << "Expected " << result << " to exist; did " |
| 176 | << "ResourcePath return an incorrect path?"; |
kjellander@webrtc.org | 193600b | 2012-10-17 04:39:44 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | TEST_F(FileUtilsTest, ResourcePathFromRootWorkingDir) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 180 | ASSERT_EQ(0, chdir(kPathDelimiter)); |
Patrik Höglund | 8434aeb | 2018-10-05 14:52:11 +0200 | [diff] [blame] | 181 | std::string resource = webrtc::test::ResourcePath("whatever", "ext"); |
kthelgason | 5d682ca | 2017-01-10 03:00:41 -0800 | [diff] [blame] | 182 | #if !defined(WEBRTC_IOS) |
kjellander@webrtc.org | 193600b | 2012-10-17 04:39:44 +0000 | [diff] [blame] | 183 | ASSERT_NE(resource.find("resources"), std::string::npos); |
kthelgason | 5d682ca | 2017-01-10 03:00:41 -0800 | [diff] [blame] | 184 | #endif |
Patrik Höglund | 8434aeb | 2018-10-05 14:52:11 +0200 | [diff] [blame] | 185 | ASSERT_GT(resource.find("whatever"), 0u); |
| 186 | ASSERT_GT(resource.find("ext"), 0u); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 187 | } |
| 188 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 189 | TEST_F(FileUtilsTest, GetFileSizeExistingFile) { |
kjellander@webrtc.org | e794c36 | 2014-09-29 11:47:28 +0000 | [diff] [blame] | 190 | // Create a file with some dummy data in. |
| 191 | std::string temp_filename = webrtc::test::TempFilename( |
| 192 | webrtc::test::OutputPath(), "fileutils_unittest"); |
| 193 | FILE* file = fopen(temp_filename.c_str(), "wb"); |
| 194 | ASSERT_TRUE(file != NULL) << "Failed to open file: " << temp_filename; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 195 | ASSERT_GT(fprintf(file, "%s", "Dummy data"), 0) |
| 196 | << "Failed to write to file: " << temp_filename; |
kjellander@webrtc.org | e794c36 | 2014-09-29 11:47:28 +0000 | [diff] [blame] | 197 | fclose(file); |
| 198 | ASSERT_GT(webrtc::test::GetFileSize(std::string(temp_filename.c_str())), 0u); |
| 199 | remove(temp_filename.c_str()); |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | TEST_F(FileUtilsTest, GetFileSizeNonExistingFile) { |
| 203 | ASSERT_EQ(0u, webrtc::test::GetFileSize("non-existing-file.tmp")); |
| 204 | } |
| 205 | |
alessiob | e49fede | 2017-03-15 06:04:59 -0700 | [diff] [blame] | 206 | TEST_F(FileUtilsTest, DirExists) { |
| 207 | // Check that an existing directory is recognized as such. |
| 208 | ASSERT_TRUE(webrtc::test::DirExists(webrtc::test::OutputPath())) |
| 209 | << "Existing directory not found"; |
| 210 | |
| 211 | // Check that a non-existing directory is recognized as such. |
| 212 | std::string directory = "direxists-unittest-non_existing-dir"; |
| 213 | ASSERT_FALSE(webrtc::test::DirExists(directory)) |
| 214 | << "Non-existing directory found"; |
| 215 | |
| 216 | // Check that an existing file is not recognized as an existing directory. |
| 217 | std::string temp_filename = webrtc::test::TempFilename( |
| 218 | webrtc::test::OutputPath(), "TempFilenameTest"); |
| 219 | ASSERT_TRUE(webrtc::test::FileExists(temp_filename)) |
| 220 | << "Couldn't find file: " << temp_filename; |
| 221 | ASSERT_FALSE(webrtc::test::DirExists(temp_filename)) |
| 222 | << "Existing file recognized as existing directory"; |
| 223 | remove(temp_filename.c_str()); |
| 224 | } |
| 225 | |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 226 | TEST_F(FileUtilsTest, WriteReadDeleteFilesAndDirs) { |
| 227 | size_t num_deleted_entries; |
| 228 | |
| 229 | // Create an empty temporary directory for this test. |
| 230 | const std::string temp_directory = |
Patrik Höglund | 8434aeb | 2018-10-05 14:52:11 +0200 | [diff] [blame] | 231 | OutputPath() + Path("TempFileUtilsTestReadDirectory/"); |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 232 | CreateDir(temp_directory); |
| 233 | EXPECT_NO_FATAL_FAILURE(CleanDir(temp_directory, &num_deleted_entries)); |
| 234 | EXPECT_TRUE(DirExists(temp_directory)); |
| 235 | |
| 236 | // Add a file. |
| 237 | const std::string temp_filename = temp_directory + "TempFilenameTest"; |
| 238 | WriteStringInFile("test\n", temp_filename); |
| 239 | EXPECT_TRUE(FileExists(temp_filename)); |
| 240 | |
| 241 | // Add an empty directory. |
Patrik Höglund | 8434aeb | 2018-10-05 14:52:11 +0200 | [diff] [blame] | 242 | const std::string temp_subdir = temp_directory + Path("subdir/"); |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 243 | EXPECT_TRUE(CreateDir(temp_subdir)); |
| 244 | EXPECT_TRUE(DirExists(temp_subdir)); |
| 245 | |
| 246 | // Checks. |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 247 | absl::optional<std::vector<std::string>> dir_content = |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 248 | ReadDirectory(temp_directory); |
| 249 | EXPECT_TRUE(dir_content); |
| 250 | EXPECT_EQ(2u, dir_content->size()); |
| 251 | EXPECT_NO_FATAL_FAILURE(CleanDir(temp_directory, &num_deleted_entries)); |
| 252 | EXPECT_EQ(2u, num_deleted_entries); |
| 253 | EXPECT_TRUE(RemoveDir(temp_directory)); |
| 254 | EXPECT_FALSE(DirExists(temp_directory)); |
| 255 | } |
| 256 | |
Patrik Höglund | 8434aeb | 2018-10-05 14:52:11 +0200 | [diff] [blame] | 257 | TEST_F(FileUtilsTest, DirNameStripsFilename) { |
| 258 | EXPECT_EQ(Path("/some/path"), DirName(Path("/some/path/file.txt"))); |
| 259 | } |
| 260 | |
| 261 | TEST_F(FileUtilsTest, DirNameKeepsStrippingRightmostPathComponent) { |
| 262 | EXPECT_EQ(Path("/some"), DirName(DirName(Path("/some/path/file.txt")))); |
| 263 | } |
| 264 | |
| 265 | TEST_F(FileUtilsTest, DirNameDoesntCareIfAPathEndsInPathSeparator) { |
| 266 | EXPECT_EQ(Path("/some"), DirName(Path("/some/path/"))); |
| 267 | } |
| 268 | |
| 269 | TEST_F(FileUtilsTest, DirNameStopsAtRoot) { |
| 270 | EXPECT_EQ(Path("/"), DirName(Path("/"))); |
| 271 | } |
| 272 | |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 273 | } // namespace test |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 274 | } // namespace webrtc |