blob: c62bb7a3abac92a58b464e7e1ca77ad9482fb901 [file] [log] [blame]
kjellander@webrtc.org7951e812011-10-13 12:24:41 +00001/*
phoglund@webrtc.org9d9ad882012-02-07 16:16:52 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
kjellander@webrtc.org7951e812011-10-13 12:24:41 +00003 *
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.org4d8cd9d2011-11-09 11:24:14 +000010
Steve Anton10542f22019-01-11 09:11:00 -080011#include "test/testsupport/file_utils.h"
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <stdio.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Yves Gerey3e707812018-11-28 16:47:49 +010015#include <algorithm>
alessiob00b16f42017-06-01 03:29:40 -070016#include <fstream>
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000017#include <string>
18
Danil Chapovalov431abd92018-06-18 12:54:17 +020019#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/checks.h"
Patrik Höglund8434aeb2018-10-05 14:52:11 +020021#include "test/gmock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "test/gtest.h"
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000023
24#ifdef WIN32
phoglund@webrtc.org9d9ad882012-02-07 16:16:52 +000025#define chdir _chdir
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000026#endif
27
Patrik Höglund8434aeb2018-10-05 14:52:11 +020028using ::testing::EndsWith;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000029
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000030namespace webrtc {
alessiob00b16f42017-06-01 03:29:40 -070031namespace test {
32
33namespace {
34
Patrik Höglund8434aeb2018-10-05 14:52:11 +020035std::string Path(const std::string& path) {
36 std::string result = path;
37 std::replace(result.begin(), result.end(), '/', *kPathDelimiter);
38 return result;
39}
40
alessiob00b16f42017-06-01 03:29:40 -070041// Remove files and directories in a directory non-recursively and writes the
42// number of deleted items in |num_deleted_entries|.
43void CleanDir(const std::string& dir, size_t* num_deleted_entries) {
44 RTC_DCHECK(num_deleted_entries);
45 *num_deleted_entries = 0;
Danil Chapovalov431abd92018-06-18 12:54:17 +020046 absl::optional<std::vector<std::string>> dir_content = ReadDirectory(dir);
alessiob00b16f42017-06-01 03:29:40 -070047 EXPECT_TRUE(dir_content);
48 for (const auto& entry : *dir_content) {
49 if (DirExists(entry)) {
50 EXPECT_TRUE(RemoveDir(entry));
51 (*num_deleted_entries)++;
52 } else if (FileExists(entry)) {
53 EXPECT_TRUE(RemoveFile(entry));
54 (*num_deleted_entries)++;
55 } else {
56 FAIL();
57 }
58 }
59}
60
61void WriteStringInFile(const std::string& what, const std::string& file_path) {
62 std::ofstream out(file_path);
63 out << what;
64 out.close();
65}
66
67} // namespace
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000068
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000069// Test fixture to restore the working directory between each test, since some
70// of them change it with chdir during execution (not restored by the
71// gtest framework).
Mirko Bonadei6a489f22019-04-09 15:11:12 +020072class FileUtilsTest : public ::testing::Test {
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000073 protected:
Yves Gerey665174f2018-06-19 15:03:05 +020074 FileUtilsTest() {}
ehmaldonado37535bf2016-12-05 06:42:45 -080075 ~FileUtilsTest() override {}
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000076 // Runs before the first test
Mirko Bonadei71061bc2019-06-04 09:01:51 +020077 static void SetUpTestSuite() {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000078 original_working_dir_ = webrtc::test::WorkingDir();
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000079 }
Yves Gerey665174f2018-06-19 15:03:05 +020080 void SetUp() override { ASSERT_EQ(chdir(original_working_dir_.c_str()), 0); }
ehmaldonado37535bf2016-12-05 06:42:45 -080081 void TearDown() override {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000082 ASSERT_EQ(chdir(original_working_dir_.c_str()), 0);
83 }
Yves Gerey665174f2018-06-19 15:03:05 +020084
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000085 private:
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000086 static std::string original_working_dir_;
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000087};
88
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000089std::string FileUtilsTest::original_working_dir_ = "";
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000090
Patrik Höglund8434aeb2018-10-05 14:52:11 +020091// The location will vary depending on where the webrtc checkout is on the
92// system, but it should end as described above and be an absolute path.
93std::string ExpectedRootDirByPlatform() {
94#if defined(WEBRTC_ANDROID)
95 return Path("chromium_tests_root/");
96#elif defined(WEBRTC_IOS)
97 return Path("tmp/");
Peter Boströme2976c82016-01-04 22:44:05 +010098#else
Patrik Höglund8434aeb2018-10-05 14:52:11 +020099 return Path("out/");
Peter Boströme2976c82016-01-04 22:44:05 +0100100#endif
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200101}
102
103TEST_F(FileUtilsTest, OutputPathFromUnchangedWorkingDir) {
104 std::string expected_end = ExpectedRootDirByPlatform();
105 std::string result = webrtc::test::OutputPath();
106
107 ASSERT_THAT(result, EndsWith(expected_end));
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000108}
109
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000110// Tests with current working directory set to a directory higher up in the
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000111// directory tree than the project root dir.
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200112TEST_F(FileUtilsTest, OutputPathFromRootWorkingDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000113 ASSERT_EQ(0, chdir(kPathDelimiter));
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200114
115 std::string expected_end = ExpectedRootDirByPlatform();
116 std::string result = webrtc::test::OutputPath();
117
118 ASSERT_THAT(result, EndsWith(expected_end));
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000119}
120
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000121TEST_F(FileUtilsTest, TempFilename) {
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000122 std::string temp_filename = webrtc::test::TempFilename(
123 webrtc::test::OutputPath(), "TempFilenameTest");
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000124 ASSERT_TRUE(webrtc::test::FileExists(temp_filename))
125 << "Couldn't find file: " << temp_filename;
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000126 remove(temp_filename.c_str());
127}
128
Artem Titove62f6002018-03-19 15:40:00 +0100129TEST_F(FileUtilsTest, GenerateTempFilename) {
130 std::string temp_filename = webrtc::test::GenerateTempFilename(
131 webrtc::test::OutputPath(), "TempFilenameTest");
132 ASSERT_FALSE(webrtc::test::FileExists(temp_filename))
133 << "File exists: " << temp_filename;
134 FILE* file = fopen(temp_filename.c_str(), "wb");
135 ASSERT_TRUE(file != NULL) << "Failed to open file: " << temp_filename;
136 ASSERT_GT(fprintf(file, "%s", "Dummy data"), 0)
137 << "Failed to write to file: " << temp_filename;
138 fclose(file);
139 remove(temp_filename.c_str());
140}
141
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000142// Only tests that the code executes
kthelgason5d682ca2017-01-10 03:00:41 -0800143#if defined(WEBRTC_IOS)
144#define MAYBE_CreateDir DISABLED_CreateDir
145#else
146#define MAYBE_CreateDir CreateDir
147#endif
148TEST_F(FileUtilsTest, MAYBE_CreateDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000149 std::string directory = "fileutils-unittest-empty-dir";
150 // Make sure it's removed if a previous test has failed:
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +0000151 remove(directory.c_str());
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000152 ASSERT_TRUE(webrtc::test::CreateDir(directory));
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +0000153 remove(directory.c_str());
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000154}
155
156TEST_F(FileUtilsTest, WorkingDirReturnsValue) {
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200157 // This will obviously be different depending on where the webrtc checkout is,
158 // so just check something is returned.
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000159 std::string working_dir = webrtc::test::WorkingDir();
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000160 ASSERT_GT(working_dir.length(), 0u);
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000161}
162
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200163TEST_F(FileUtilsTest, ResourcePathReturnsCorrectPath) {
164 std::string result = webrtc::test::ResourcePath(
165 Path("video_coding/frame-ethernet-ii"), "pcap");
166#if defined(WEBRTC_IOS)
167 // iOS bundles resources straight into the bundle root.
168 std::string expected_end = Path("/frame-ethernet-ii.pcap");
169#else
170 // Other platforms: it's a separate dir.
171 std::string expected_end =
172 Path("resources/video_coding/frame-ethernet-ii.pcap");
173#endif
174
175 ASSERT_THAT(result, EndsWith(expected_end));
176 ASSERT_TRUE(FileExists(result)) << "Expected " << result << " to exist; did "
177 << "ResourcePath return an incorrect path?";
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000178}
179
180TEST_F(FileUtilsTest, ResourcePathFromRootWorkingDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000181 ASSERT_EQ(0, chdir(kPathDelimiter));
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200182 std::string resource = webrtc::test::ResourcePath("whatever", "ext");
kthelgason5d682ca2017-01-10 03:00:41 -0800183#if !defined(WEBRTC_IOS)
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000184 ASSERT_NE(resource.find("resources"), std::string::npos);
kthelgason5d682ca2017-01-10 03:00:41 -0800185#endif
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200186 ASSERT_GT(resource.find("whatever"), 0u);
187 ASSERT_GT(resource.find("ext"), 0u);
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000188}
189
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000190TEST_F(FileUtilsTest, GetFileSizeExistingFile) {
kjellander@webrtc.orge794c362014-09-29 11:47:28 +0000191 // Create a file with some dummy data in.
192 std::string temp_filename = webrtc::test::TempFilename(
193 webrtc::test::OutputPath(), "fileutils_unittest");
194 FILE* file = fopen(temp_filename.c_str(), "wb");
195 ASSERT_TRUE(file != NULL) << "Failed to open file: " << temp_filename;
Yves Gerey665174f2018-06-19 15:03:05 +0200196 ASSERT_GT(fprintf(file, "%s", "Dummy data"), 0)
197 << "Failed to write to file: " << temp_filename;
kjellander@webrtc.orge794c362014-09-29 11:47:28 +0000198 fclose(file);
199 ASSERT_GT(webrtc::test::GetFileSize(std::string(temp_filename.c_str())), 0u);
200 remove(temp_filename.c_str());
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000201}
202
203TEST_F(FileUtilsTest, GetFileSizeNonExistingFile) {
204 ASSERT_EQ(0u, webrtc::test::GetFileSize("non-existing-file.tmp"));
205}
206
alessiobe49fede2017-03-15 06:04:59 -0700207TEST_F(FileUtilsTest, DirExists) {
208 // Check that an existing directory is recognized as such.
209 ASSERT_TRUE(webrtc::test::DirExists(webrtc::test::OutputPath()))
210 << "Existing directory not found";
211
212 // Check that a non-existing directory is recognized as such.
213 std::string directory = "direxists-unittest-non_existing-dir";
214 ASSERT_FALSE(webrtc::test::DirExists(directory))
215 << "Non-existing directory found";
216
217 // Check that an existing file is not recognized as an existing directory.
218 std::string temp_filename = webrtc::test::TempFilename(
219 webrtc::test::OutputPath(), "TempFilenameTest");
220 ASSERT_TRUE(webrtc::test::FileExists(temp_filename))
221 << "Couldn't find file: " << temp_filename;
222 ASSERT_FALSE(webrtc::test::DirExists(temp_filename))
223 << "Existing file recognized as existing directory";
224 remove(temp_filename.c_str());
225}
226
alessiob00b16f42017-06-01 03:29:40 -0700227TEST_F(FileUtilsTest, WriteReadDeleteFilesAndDirs) {
228 size_t num_deleted_entries;
229
230 // Create an empty temporary directory for this test.
231 const std::string temp_directory =
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200232 OutputPath() + Path("TempFileUtilsTestReadDirectory/");
alessiob00b16f42017-06-01 03:29:40 -0700233 CreateDir(temp_directory);
234 EXPECT_NO_FATAL_FAILURE(CleanDir(temp_directory, &num_deleted_entries));
235 EXPECT_TRUE(DirExists(temp_directory));
236
237 // Add a file.
238 const std::string temp_filename = temp_directory + "TempFilenameTest";
239 WriteStringInFile("test\n", temp_filename);
240 EXPECT_TRUE(FileExists(temp_filename));
241
242 // Add an empty directory.
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200243 const std::string temp_subdir = temp_directory + Path("subdir/");
alessiob00b16f42017-06-01 03:29:40 -0700244 EXPECT_TRUE(CreateDir(temp_subdir));
245 EXPECT_TRUE(DirExists(temp_subdir));
246
247 // Checks.
Danil Chapovalov431abd92018-06-18 12:54:17 +0200248 absl::optional<std::vector<std::string>> dir_content =
alessiob00b16f42017-06-01 03:29:40 -0700249 ReadDirectory(temp_directory);
250 EXPECT_TRUE(dir_content);
251 EXPECT_EQ(2u, dir_content->size());
252 EXPECT_NO_FATAL_FAILURE(CleanDir(temp_directory, &num_deleted_entries));
253 EXPECT_EQ(2u, num_deleted_entries);
254 EXPECT_TRUE(RemoveDir(temp_directory));
255 EXPECT_FALSE(DirExists(temp_directory));
256}
257
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200258TEST_F(FileUtilsTest, DirNameStripsFilename) {
259 EXPECT_EQ(Path("/some/path"), DirName(Path("/some/path/file.txt")));
260}
261
262TEST_F(FileUtilsTest, DirNameKeepsStrippingRightmostPathComponent) {
263 EXPECT_EQ(Path("/some"), DirName(DirName(Path("/some/path/file.txt"))));
264}
265
266TEST_F(FileUtilsTest, DirNameDoesntCareIfAPathEndsInPathSeparator) {
267 EXPECT_EQ(Path("/some"), DirName(Path("/some/path/")));
268}
269
270TEST_F(FileUtilsTest, DirNameStopsAtRoot) {
271 EXPECT_EQ(Path("/"), DirName(Path("/")));
272}
273
alessiob00b16f42017-06-01 03:29:40 -0700274} // namespace test
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000275} // namespace webrtc