blob: c6dc86deb200a3c3c02d28d104e265266870982a [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>
14
alessiob00b16f42017-06-01 03:29:40 -070015#include <fstream>
16#include <iostream>
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000017#include <list>
18#include <string>
19
Danil Chapovalov431abd92018-06-18 12:54:17 +020020#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/checks.h"
Patrik Höglund8434aeb2018-10-05 14:52:11 +020022#include "test/gmock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "test/gtest.h"
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000024
25#ifdef WIN32
phoglund@webrtc.org9d9ad882012-02-07 16:16:52 +000026#define chdir _chdir
Qingsi Wang2039ee72018-11-02 16:30:10 +000027static const char* kPathDelimiter = "\\";
28#else
29static const char* kPathDelimiter = "/";
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000030#endif
31
Patrik Höglund8434aeb2018-10-05 14:52:11 +020032using ::testing::EndsWith;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000033
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000034namespace webrtc {
alessiob00b16f42017-06-01 03:29:40 -070035namespace test {
36
37namespace {
38
Patrik Höglund8434aeb2018-10-05 14:52:11 +020039std::string Path(const std::string& path) {
40 std::string result = path;
41 std::replace(result.begin(), result.end(), '/', *kPathDelimiter);
42 return result;
43}
44
alessiob00b16f42017-06-01 03:29:40 -070045// Remove files and directories in a directory non-recursively and writes the
46// number of deleted items in |num_deleted_entries|.
47void CleanDir(const std::string& dir, size_t* num_deleted_entries) {
48 RTC_DCHECK(num_deleted_entries);
49 *num_deleted_entries = 0;
Danil Chapovalov431abd92018-06-18 12:54:17 +020050 absl::optional<std::vector<std::string>> dir_content = ReadDirectory(dir);
alessiob00b16f42017-06-01 03:29:40 -070051 EXPECT_TRUE(dir_content);
52 for (const auto& entry : *dir_content) {
53 if (DirExists(entry)) {
54 EXPECT_TRUE(RemoveDir(entry));
55 (*num_deleted_entries)++;
56 } else if (FileExists(entry)) {
57 EXPECT_TRUE(RemoveFile(entry));
58 (*num_deleted_entries)++;
59 } else {
60 FAIL();
61 }
62 }
63}
64
65void WriteStringInFile(const std::string& what, const std::string& file_path) {
66 std::ofstream out(file_path);
67 out << what;
68 out.close();
69}
70
71} // namespace
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000072
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000073// Test fixture to restore the working directory between each test, since some
74// of them change it with chdir during execution (not restored by the
75// gtest framework).
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000076class FileUtilsTest : public testing::Test {
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000077 protected:
Yves Gerey665174f2018-06-19 15:03:05 +020078 FileUtilsTest() {}
ehmaldonado37535bf2016-12-05 06:42:45 -080079 ~FileUtilsTest() override {}
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000080 // Runs before the first test
81 static void SetUpTestCase() {
82 original_working_dir_ = webrtc::test::WorkingDir();
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000083 }
Yves Gerey665174f2018-06-19 15:03:05 +020084 void SetUp() override { ASSERT_EQ(chdir(original_working_dir_.c_str()), 0); }
ehmaldonado37535bf2016-12-05 06:42:45 -080085 void TearDown() override {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000086 ASSERT_EQ(chdir(original_working_dir_.c_str()), 0);
87 }
Yves Gerey665174f2018-06-19 15:03:05 +020088
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000089 private:
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000090 static std::string original_working_dir_;
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000091};
92
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000093std::string FileUtilsTest::original_working_dir_ = "";
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000094
Patrik Höglund8434aeb2018-10-05 14:52:11 +020095// The location will vary depending on where the webrtc checkout is on the
96// system, but it should end as described above and be an absolute path.
97std::string ExpectedRootDirByPlatform() {
98#if defined(WEBRTC_ANDROID)
99 return Path("chromium_tests_root/");
100#elif defined(WEBRTC_IOS)
101 return Path("tmp/");
Peter Boströme2976c82016-01-04 22:44:05 +0100102#else
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200103 return Path("out/");
Peter Boströme2976c82016-01-04 22:44:05 +0100104#endif
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200105}
106
107TEST_F(FileUtilsTest, OutputPathFromUnchangedWorkingDir) {
108 std::string expected_end = ExpectedRootDirByPlatform();
109 std::string result = webrtc::test::OutputPath();
110
111 ASSERT_THAT(result, EndsWith(expected_end));
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000112}
113
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000114// Tests with current working directory set to a directory higher up in the
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000115// directory tree than the project root dir.
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200116TEST_F(FileUtilsTest, OutputPathFromRootWorkingDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000117 ASSERT_EQ(0, chdir(kPathDelimiter));
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200118
119 std::string expected_end = ExpectedRootDirByPlatform();
120 std::string result = webrtc::test::OutputPath();
121
122 ASSERT_THAT(result, EndsWith(expected_end));
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000123}
124
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000125TEST_F(FileUtilsTest, TempFilename) {
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000126 std::string temp_filename = webrtc::test::TempFilename(
127 webrtc::test::OutputPath(), "TempFilenameTest");
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000128 ASSERT_TRUE(webrtc::test::FileExists(temp_filename))
129 << "Couldn't find file: " << temp_filename;
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000130 remove(temp_filename.c_str());
131}
132
Artem Titove62f6002018-03-19 15:40:00 +0100133TEST_F(FileUtilsTest, GenerateTempFilename) {
134 std::string temp_filename = webrtc::test::GenerateTempFilename(
135 webrtc::test::OutputPath(), "TempFilenameTest");
136 ASSERT_FALSE(webrtc::test::FileExists(temp_filename))
137 << "File exists: " << temp_filename;
138 FILE* file = fopen(temp_filename.c_str(), "wb");
139 ASSERT_TRUE(file != NULL) << "Failed to open file: " << temp_filename;
140 ASSERT_GT(fprintf(file, "%s", "Dummy data"), 0)
141 << "Failed to write to file: " << temp_filename;
142 fclose(file);
143 remove(temp_filename.c_str());
144}
145
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000146// Only tests that the code executes
kthelgason5d682ca2017-01-10 03:00:41 -0800147#if defined(WEBRTC_IOS)
148#define MAYBE_CreateDir DISABLED_CreateDir
149#else
150#define MAYBE_CreateDir CreateDir
151#endif
152TEST_F(FileUtilsTest, MAYBE_CreateDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000153 std::string directory = "fileutils-unittest-empty-dir";
154 // Make sure it's removed if a previous test has failed:
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +0000155 remove(directory.c_str());
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000156 ASSERT_TRUE(webrtc::test::CreateDir(directory));
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +0000157 remove(directory.c_str());
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000158}
159
160TEST_F(FileUtilsTest, WorkingDirReturnsValue) {
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200161 // This will obviously be different depending on where the webrtc checkout is,
162 // so just check something is returned.
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000163 std::string working_dir = webrtc::test::WorkingDir();
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000164 ASSERT_GT(working_dir.length(), 0u);
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000165}
166
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200167TEST_F(FileUtilsTest, ResourcePathReturnsCorrectPath) {
168 std::string result = webrtc::test::ResourcePath(
169 Path("video_coding/frame-ethernet-ii"), "pcap");
170#if defined(WEBRTC_IOS)
171 // iOS bundles resources straight into the bundle root.
172 std::string expected_end = Path("/frame-ethernet-ii.pcap");
173#else
174 // Other platforms: it's a separate dir.
175 std::string expected_end =
176 Path("resources/video_coding/frame-ethernet-ii.pcap");
177#endif
178
179 ASSERT_THAT(result, EndsWith(expected_end));
180 ASSERT_TRUE(FileExists(result)) << "Expected " << result << " to exist; did "
181 << "ResourcePath return an incorrect path?";
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000182}
183
184TEST_F(FileUtilsTest, ResourcePathFromRootWorkingDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000185 ASSERT_EQ(0, chdir(kPathDelimiter));
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200186 std::string resource = webrtc::test::ResourcePath("whatever", "ext");
kthelgason5d682ca2017-01-10 03:00:41 -0800187#if !defined(WEBRTC_IOS)
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000188 ASSERT_NE(resource.find("resources"), std::string::npos);
kthelgason5d682ca2017-01-10 03:00:41 -0800189#endif
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200190 ASSERT_GT(resource.find("whatever"), 0u);
191 ASSERT_GT(resource.find("ext"), 0u);
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000192}
193
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000194TEST_F(FileUtilsTest, GetFileSizeExistingFile) {
kjellander@webrtc.orge794c362014-09-29 11:47:28 +0000195 // Create a file with some dummy data in.
196 std::string temp_filename = webrtc::test::TempFilename(
197 webrtc::test::OutputPath(), "fileutils_unittest");
198 FILE* file = fopen(temp_filename.c_str(), "wb");
199 ASSERT_TRUE(file != NULL) << "Failed to open file: " << temp_filename;
Yves Gerey665174f2018-06-19 15:03:05 +0200200 ASSERT_GT(fprintf(file, "%s", "Dummy data"), 0)
201 << "Failed to write to file: " << temp_filename;
kjellander@webrtc.orge794c362014-09-29 11:47:28 +0000202 fclose(file);
203 ASSERT_GT(webrtc::test::GetFileSize(std::string(temp_filename.c_str())), 0u);
204 remove(temp_filename.c_str());
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000205}
206
207TEST_F(FileUtilsTest, GetFileSizeNonExistingFile) {
208 ASSERT_EQ(0u, webrtc::test::GetFileSize("non-existing-file.tmp"));
209}
210
alessiobe49fede2017-03-15 06:04:59 -0700211TEST_F(FileUtilsTest, DirExists) {
212 // Check that an existing directory is recognized as such.
213 ASSERT_TRUE(webrtc::test::DirExists(webrtc::test::OutputPath()))
214 << "Existing directory not found";
215
216 // Check that a non-existing directory is recognized as such.
217 std::string directory = "direxists-unittest-non_existing-dir";
218 ASSERT_FALSE(webrtc::test::DirExists(directory))
219 << "Non-existing directory found";
220
221 // Check that an existing file is not recognized as an existing directory.
222 std::string temp_filename = webrtc::test::TempFilename(
223 webrtc::test::OutputPath(), "TempFilenameTest");
224 ASSERT_TRUE(webrtc::test::FileExists(temp_filename))
225 << "Couldn't find file: " << temp_filename;
226 ASSERT_FALSE(webrtc::test::DirExists(temp_filename))
227 << "Existing file recognized as existing directory";
228 remove(temp_filename.c_str());
229}
230
alessiob00b16f42017-06-01 03:29:40 -0700231TEST_F(FileUtilsTest, WriteReadDeleteFilesAndDirs) {
232 size_t num_deleted_entries;
233
234 // Create an empty temporary directory for this test.
235 const std::string temp_directory =
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200236 OutputPath() + Path("TempFileUtilsTestReadDirectory/");
alessiob00b16f42017-06-01 03:29:40 -0700237 CreateDir(temp_directory);
238 EXPECT_NO_FATAL_FAILURE(CleanDir(temp_directory, &num_deleted_entries));
239 EXPECT_TRUE(DirExists(temp_directory));
240
241 // Add a file.
242 const std::string temp_filename = temp_directory + "TempFilenameTest";
243 WriteStringInFile("test\n", temp_filename);
244 EXPECT_TRUE(FileExists(temp_filename));
245
246 // Add an empty directory.
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200247 const std::string temp_subdir = temp_directory + Path("subdir/");
alessiob00b16f42017-06-01 03:29:40 -0700248 EXPECT_TRUE(CreateDir(temp_subdir));
249 EXPECT_TRUE(DirExists(temp_subdir));
250
251 // Checks.
Danil Chapovalov431abd92018-06-18 12:54:17 +0200252 absl::optional<std::vector<std::string>> dir_content =
alessiob00b16f42017-06-01 03:29:40 -0700253 ReadDirectory(temp_directory);
254 EXPECT_TRUE(dir_content);
255 EXPECT_EQ(2u, dir_content->size());
256 EXPECT_NO_FATAL_FAILURE(CleanDir(temp_directory, &num_deleted_entries));
257 EXPECT_EQ(2u, num_deleted_entries);
258 EXPECT_TRUE(RemoveDir(temp_directory));
259 EXPECT_FALSE(DirExists(temp_directory));
260}
261
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200262TEST_F(FileUtilsTest, DirNameStripsFilename) {
263 EXPECT_EQ(Path("/some/path"), DirName(Path("/some/path/file.txt")));
264}
265
266TEST_F(FileUtilsTest, DirNameKeepsStrippingRightmostPathComponent) {
267 EXPECT_EQ(Path("/some"), DirName(DirName(Path("/some/path/file.txt"))));
268}
269
270TEST_F(FileUtilsTest, DirNameDoesntCareIfAPathEndsInPathSeparator) {
271 EXPECT_EQ(Path("/some"), DirName(Path("/some/path/")));
272}
273
274TEST_F(FileUtilsTest, DirNameStopsAtRoot) {
275 EXPECT_EQ(Path("/"), DirName(Path("/")));
276}
277
alessiob00b16f42017-06-01 03:29:40 -0700278} // namespace test
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000279} // namespace webrtc