blob: 49e30ff1c31dc52bb30adfa864438cde46f9cf24 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "test/testsupport/fileutils.h"
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <stdio.h>
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <algorithm>
alessiob00b16f42017-06-01 03:29:40 -070015#include <fstream>
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000016#include <string>
17
Danil Chapovalov431abd92018-06-18 12:54:17 +020018#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/checks.h"
Patrik Höglund8434aeb2018-10-05 14:52:11 +020020#include "test/gmock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "test/gtest.h"
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000022
23#ifdef WIN32
phoglund@webrtc.org9d9ad882012-02-07 16:16:52 +000024#define chdir _chdir
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000025#endif
26
Patrik Höglund8434aeb2018-10-05 14:52:11 +020027using ::testing::EndsWith;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000028
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000029namespace webrtc {
alessiob00b16f42017-06-01 03:29:40 -070030namespace test {
31
32namespace {
33
Patrik Höglund8434aeb2018-10-05 14:52:11 +020034std::string Path(const std::string& path) {
35 std::string result = path;
36 std::replace(result.begin(), result.end(), '/', *kPathDelimiter);
37 return result;
38}
39
alessiob00b16f42017-06-01 03:29:40 -070040// Remove files and directories in a directory non-recursively and writes the
41// number of deleted items in |num_deleted_entries|.
42void CleanDir(const std::string& dir, size_t* num_deleted_entries) {
43 RTC_DCHECK(num_deleted_entries);
44 *num_deleted_entries = 0;
Danil Chapovalov431abd92018-06-18 12:54:17 +020045 absl::optional<std::vector<std::string>> dir_content = ReadDirectory(dir);
alessiob00b16f42017-06-01 03:29:40 -070046 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
60void 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.org7951e812011-10-13 12:24:41 +000067
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000068// 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.org4ed4f242011-12-05 16:31:12 +000071class FileUtilsTest : public testing::Test {
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000072 protected:
Yves Gerey665174f2018-06-19 15:03:05 +020073 FileUtilsTest() {}
ehmaldonado37535bf2016-12-05 06:42:45 -080074 ~FileUtilsTest() override {}
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000075 // Runs before the first test
76 static void SetUpTestCase() {
77 original_working_dir_ = webrtc::test::WorkingDir();
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000078 }
Yves Gerey665174f2018-06-19 15:03:05 +020079 void SetUp() override { ASSERT_EQ(chdir(original_working_dir_.c_str()), 0); }
ehmaldonado37535bf2016-12-05 06:42:45 -080080 void TearDown() override {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000081 ASSERT_EQ(chdir(original_working_dir_.c_str()), 0);
82 }
Yves Gerey665174f2018-06-19 15:03:05 +020083
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000084 private:
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000085 static std::string original_working_dir_;
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000086};
87
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000088std::string FileUtilsTest::original_working_dir_ = "";
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000089
Patrik Höglund8434aeb2018-10-05 14:52:11 +020090// 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.
92std::string ExpectedRootDirByPlatform() {
93#if defined(WEBRTC_ANDROID)
94 return Path("chromium_tests_root/");
95#elif defined(WEBRTC_IOS)
96 return Path("tmp/");
Peter Boströme2976c82016-01-04 22:44:05 +010097#else
Patrik Höglund8434aeb2018-10-05 14:52:11 +020098 return Path("out/");
Peter Boströme2976c82016-01-04 22:44:05 +010099#endif
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200100}
101
102TEST_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.org7951e812011-10-13 12:24:41 +0000107}
108
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000109// Tests with current working directory set to a directory higher up in the
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000110// directory tree than the project root dir.
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200111TEST_F(FileUtilsTest, OutputPathFromRootWorkingDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000112 ASSERT_EQ(0, chdir(kPathDelimiter));
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200113
114 std::string expected_end = ExpectedRootDirByPlatform();
115 std::string result = webrtc::test::OutputPath();
116
117 ASSERT_THAT(result, EndsWith(expected_end));
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000118}
119
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000120TEST_F(FileUtilsTest, TempFilename) {
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000121 std::string temp_filename = webrtc::test::TempFilename(
122 webrtc::test::OutputPath(), "TempFilenameTest");
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000123 ASSERT_TRUE(webrtc::test::FileExists(temp_filename))
124 << "Couldn't find file: " << temp_filename;
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000125 remove(temp_filename.c_str());
126}
127
Artem Titove62f6002018-03-19 15:40:00 +0100128TEST_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.org4ed4f242011-12-05 16:31:12 +0000141// Only tests that the code executes
kthelgason5d682ca2017-01-10 03:00:41 -0800142#if defined(WEBRTC_IOS)
143#define MAYBE_CreateDir DISABLED_CreateDir
144#else
145#define MAYBE_CreateDir CreateDir
146#endif
147TEST_F(FileUtilsTest, MAYBE_CreateDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000148 std::string directory = "fileutils-unittest-empty-dir";
149 // Make sure it's removed if a previous test has failed:
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +0000150 remove(directory.c_str());
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000151 ASSERT_TRUE(webrtc::test::CreateDir(directory));
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +0000152 remove(directory.c_str());
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000153}
154
155TEST_F(FileUtilsTest, WorkingDirReturnsValue) {
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200156 // This will obviously be different depending on where the webrtc checkout is,
157 // so just check something is returned.
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000158 std::string working_dir = webrtc::test::WorkingDir();
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000159 ASSERT_GT(working_dir.length(), 0u);
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000160}
161
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200162TEST_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.org193600b2012-10-17 04:39:44 +0000177}
178
179TEST_F(FileUtilsTest, ResourcePathFromRootWorkingDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000180 ASSERT_EQ(0, chdir(kPathDelimiter));
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200181 std::string resource = webrtc::test::ResourcePath("whatever", "ext");
kthelgason5d682ca2017-01-10 03:00:41 -0800182#if !defined(WEBRTC_IOS)
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000183 ASSERT_NE(resource.find("resources"), std::string::npos);
kthelgason5d682ca2017-01-10 03:00:41 -0800184#endif
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200185 ASSERT_GT(resource.find("whatever"), 0u);
186 ASSERT_GT(resource.find("ext"), 0u);
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000187}
188
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000189TEST_F(FileUtilsTest, GetFileSizeExistingFile) {
kjellander@webrtc.orge794c362014-09-29 11:47:28 +0000190 // 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 Gerey665174f2018-06-19 15:03:05 +0200195 ASSERT_GT(fprintf(file, "%s", "Dummy data"), 0)
196 << "Failed to write to file: " << temp_filename;
kjellander@webrtc.orge794c362014-09-29 11:47:28 +0000197 fclose(file);
198 ASSERT_GT(webrtc::test::GetFileSize(std::string(temp_filename.c_str())), 0u);
199 remove(temp_filename.c_str());
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000200}
201
202TEST_F(FileUtilsTest, GetFileSizeNonExistingFile) {
203 ASSERT_EQ(0u, webrtc::test::GetFileSize("non-existing-file.tmp"));
204}
205
alessiobe49fede2017-03-15 06:04:59 -0700206TEST_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
alessiob00b16f42017-06-01 03:29:40 -0700226TEST_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öglund8434aeb2018-10-05 14:52:11 +0200231 OutputPath() + Path("TempFileUtilsTestReadDirectory/");
alessiob00b16f42017-06-01 03:29:40 -0700232 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öglund8434aeb2018-10-05 14:52:11 +0200242 const std::string temp_subdir = temp_directory + Path("subdir/");
alessiob00b16f42017-06-01 03:29:40 -0700243 EXPECT_TRUE(CreateDir(temp_subdir));
244 EXPECT_TRUE(DirExists(temp_subdir));
245
246 // Checks.
Danil Chapovalov431abd92018-06-18 12:54:17 +0200247 absl::optional<std::vector<std::string>> dir_content =
alessiob00b16f42017-06-01 03:29:40 -0700248 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öglund8434aeb2018-10-05 14:52:11 +0200257TEST_F(FileUtilsTest, DirNameStripsFilename) {
258 EXPECT_EQ(Path("/some/path"), DirName(Path("/some/path/file.txt")));
259}
260
261TEST_F(FileUtilsTest, DirNameKeepsStrippingRightmostPathComponent) {
262 EXPECT_EQ(Path("/some"), DirName(DirName(Path("/some/path/file.txt"))));
263}
264
265TEST_F(FileUtilsTest, DirNameDoesntCareIfAPathEndsInPathSeparator) {
266 EXPECT_EQ(Path("/some"), DirName(Path("/some/path/")));
267}
268
269TEST_F(FileUtilsTest, DirNameStopsAtRoot) {
270 EXPECT_EQ(Path("/"), DirName(Path("/")));
271}
272
alessiob00b16f42017-06-01 03:29:40 -0700273} // namespace test
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000274} // namespace webrtc