blob: d39f46800418da7c376768fad123ee71969604b6 [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
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000027#endif
28
Patrik Höglund8434aeb2018-10-05 14:52:11 +020029using ::testing::EndsWith;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000030
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000031namespace webrtc {
alessiob00b16f42017-06-01 03:29:40 -070032namespace test {
33
34namespace {
35
Patrik Höglund8434aeb2018-10-05 14:52:11 +020036std::string Path(const std::string& path) {
37 std::string result = path;
38 std::replace(result.begin(), result.end(), '/', *kPathDelimiter);
39 return result;
40}
41
alessiob00b16f42017-06-01 03:29:40 -070042// Remove files and directories in a directory non-recursively and writes the
43// number of deleted items in |num_deleted_entries|.
44void CleanDir(const std::string& dir, size_t* num_deleted_entries) {
45 RTC_DCHECK(num_deleted_entries);
46 *num_deleted_entries = 0;
Danil Chapovalov431abd92018-06-18 12:54:17 +020047 absl::optional<std::vector<std::string>> dir_content = ReadDirectory(dir);
alessiob00b16f42017-06-01 03:29:40 -070048 EXPECT_TRUE(dir_content);
49 for (const auto& entry : *dir_content) {
50 if (DirExists(entry)) {
51 EXPECT_TRUE(RemoveDir(entry));
52 (*num_deleted_entries)++;
53 } else if (FileExists(entry)) {
54 EXPECT_TRUE(RemoveFile(entry));
55 (*num_deleted_entries)++;
56 } else {
57 FAIL();
58 }
59 }
60}
61
62void WriteStringInFile(const std::string& what, const std::string& file_path) {
63 std::ofstream out(file_path);
64 out << what;
65 out.close();
66}
67
68} // namespace
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000069
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000070// Test fixture to restore the working directory between each test, since some
71// of them change it with chdir during execution (not restored by the
72// gtest framework).
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000073class FileUtilsTest : public testing::Test {
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000074 protected:
Yves Gerey665174f2018-06-19 15:03:05 +020075 FileUtilsTest() {}
ehmaldonado37535bf2016-12-05 06:42:45 -080076 ~FileUtilsTest() override {}
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000077 // Runs before the first test
78 static void SetUpTestCase() {
79 original_working_dir_ = webrtc::test::WorkingDir();
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000080 }
Yves Gerey665174f2018-06-19 15:03:05 +020081 void SetUp() override { ASSERT_EQ(chdir(original_working_dir_.c_str()), 0); }
ehmaldonado37535bf2016-12-05 06:42:45 -080082 void TearDown() override {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000083 ASSERT_EQ(chdir(original_working_dir_.c_str()), 0);
84 }
Yves Gerey665174f2018-06-19 15:03:05 +020085
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000086 private:
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000087 static std::string original_working_dir_;
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000088};
89
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000090std::string FileUtilsTest::original_working_dir_ = "";
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000091
Patrik Höglund8434aeb2018-10-05 14:52:11 +020092// The location will vary depending on where the webrtc checkout is on the
93// system, but it should end as described above and be an absolute path.
94std::string ExpectedRootDirByPlatform() {
95#if defined(WEBRTC_ANDROID)
96 return Path("chromium_tests_root/");
97#elif defined(WEBRTC_IOS)
98 return Path("tmp/");
Peter Boströme2976c82016-01-04 22:44:05 +010099#else
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200100 return Path("out/");
Peter Boströme2976c82016-01-04 22:44:05 +0100101#endif
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200102}
103
104TEST_F(FileUtilsTest, OutputPathFromUnchangedWorkingDir) {
105 std::string expected_end = ExpectedRootDirByPlatform();
106 std::string result = webrtc::test::OutputPath();
107
108 ASSERT_THAT(result, EndsWith(expected_end));
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000109}
110
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000111// Tests with current working directory set to a directory higher up in the
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000112// directory tree than the project root dir.
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200113TEST_F(FileUtilsTest, OutputPathFromRootWorkingDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000114 ASSERT_EQ(0, chdir(kPathDelimiter));
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200115
116 std::string expected_end = ExpectedRootDirByPlatform();
117 std::string result = webrtc::test::OutputPath();
118
119 ASSERT_THAT(result, EndsWith(expected_end));
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000120}
121
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000122TEST_F(FileUtilsTest, TempFilename) {
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000123 std::string temp_filename = webrtc::test::TempFilename(
124 webrtc::test::OutputPath(), "TempFilenameTest");
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000125 ASSERT_TRUE(webrtc::test::FileExists(temp_filename))
126 << "Couldn't find file: " << temp_filename;
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000127 remove(temp_filename.c_str());
128}
129
Artem Titove62f6002018-03-19 15:40:00 +0100130TEST_F(FileUtilsTest, GenerateTempFilename) {
131 std::string temp_filename = webrtc::test::GenerateTempFilename(
132 webrtc::test::OutputPath(), "TempFilenameTest");
133 ASSERT_FALSE(webrtc::test::FileExists(temp_filename))
134 << "File exists: " << temp_filename;
135 FILE* file = fopen(temp_filename.c_str(), "wb");
136 ASSERT_TRUE(file != NULL) << "Failed to open file: " << temp_filename;
137 ASSERT_GT(fprintf(file, "%s", "Dummy data"), 0)
138 << "Failed to write to file: " << temp_filename;
139 fclose(file);
140 remove(temp_filename.c_str());
141}
142
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000143// Only tests that the code executes
kthelgason5d682ca2017-01-10 03:00:41 -0800144#if defined(WEBRTC_IOS)
145#define MAYBE_CreateDir DISABLED_CreateDir
146#else
147#define MAYBE_CreateDir CreateDir
148#endif
149TEST_F(FileUtilsTest, MAYBE_CreateDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000150 std::string directory = "fileutils-unittest-empty-dir";
151 // Make sure it's removed if a previous test has failed:
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +0000152 remove(directory.c_str());
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000153 ASSERT_TRUE(webrtc::test::CreateDir(directory));
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +0000154 remove(directory.c_str());
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000155}
156
157TEST_F(FileUtilsTest, WorkingDirReturnsValue) {
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200158 // This will obviously be different depending on where the webrtc checkout is,
159 // so just check something is returned.
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000160 std::string working_dir = webrtc::test::WorkingDir();
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000161 ASSERT_GT(working_dir.length(), 0u);
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000162}
163
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200164TEST_F(FileUtilsTest, ResourcePathReturnsCorrectPath) {
165 std::string result = webrtc::test::ResourcePath(
166 Path("video_coding/frame-ethernet-ii"), "pcap");
167#if defined(WEBRTC_IOS)
168 // iOS bundles resources straight into the bundle root.
169 std::string expected_end = Path("/frame-ethernet-ii.pcap");
170#else
171 // Other platforms: it's a separate dir.
172 std::string expected_end =
173 Path("resources/video_coding/frame-ethernet-ii.pcap");
174#endif
175
176 ASSERT_THAT(result, EndsWith(expected_end));
177 ASSERT_TRUE(FileExists(result)) << "Expected " << result << " to exist; did "
178 << "ResourcePath return an incorrect path?";
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000179}
180
181TEST_F(FileUtilsTest, ResourcePathFromRootWorkingDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000182 ASSERT_EQ(0, chdir(kPathDelimiter));
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200183 std::string resource = webrtc::test::ResourcePath("whatever", "ext");
kthelgason5d682ca2017-01-10 03:00:41 -0800184#if !defined(WEBRTC_IOS)
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000185 ASSERT_NE(resource.find("resources"), std::string::npos);
kthelgason5d682ca2017-01-10 03:00:41 -0800186#endif
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200187 ASSERT_GT(resource.find("whatever"), 0u);
188 ASSERT_GT(resource.find("ext"), 0u);
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000189}
190
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000191TEST_F(FileUtilsTest, GetFileSizeExistingFile) {
kjellander@webrtc.orge794c362014-09-29 11:47:28 +0000192 // Create a file with some dummy data in.
193 std::string temp_filename = webrtc::test::TempFilename(
194 webrtc::test::OutputPath(), "fileutils_unittest");
195 FILE* file = fopen(temp_filename.c_str(), "wb");
196 ASSERT_TRUE(file != NULL) << "Failed to open file: " << temp_filename;
Yves Gerey665174f2018-06-19 15:03:05 +0200197 ASSERT_GT(fprintf(file, "%s", "Dummy data"), 0)
198 << "Failed to write to file: " << temp_filename;
kjellander@webrtc.orge794c362014-09-29 11:47:28 +0000199 fclose(file);
200 ASSERT_GT(webrtc::test::GetFileSize(std::string(temp_filename.c_str())), 0u);
201 remove(temp_filename.c_str());
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000202}
203
204TEST_F(FileUtilsTest, GetFileSizeNonExistingFile) {
205 ASSERT_EQ(0u, webrtc::test::GetFileSize("non-existing-file.tmp"));
206}
207
alessiobe49fede2017-03-15 06:04:59 -0700208TEST_F(FileUtilsTest, DirExists) {
209 // Check that an existing directory is recognized as such.
210 ASSERT_TRUE(webrtc::test::DirExists(webrtc::test::OutputPath()))
211 << "Existing directory not found";
212
213 // Check that a non-existing directory is recognized as such.
214 std::string directory = "direxists-unittest-non_existing-dir";
215 ASSERT_FALSE(webrtc::test::DirExists(directory))
216 << "Non-existing directory found";
217
218 // Check that an existing file is not recognized as an existing directory.
219 std::string temp_filename = webrtc::test::TempFilename(
220 webrtc::test::OutputPath(), "TempFilenameTest");
221 ASSERT_TRUE(webrtc::test::FileExists(temp_filename))
222 << "Couldn't find file: " << temp_filename;
223 ASSERT_FALSE(webrtc::test::DirExists(temp_filename))
224 << "Existing file recognized as existing directory";
225 remove(temp_filename.c_str());
226}
227
alessiob00b16f42017-06-01 03:29:40 -0700228TEST_F(FileUtilsTest, WriteReadDeleteFilesAndDirs) {
229 size_t num_deleted_entries;
230
231 // Create an empty temporary directory for this test.
232 const std::string temp_directory =
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200233 OutputPath() + Path("TempFileUtilsTestReadDirectory/");
alessiob00b16f42017-06-01 03:29:40 -0700234 CreateDir(temp_directory);
235 EXPECT_NO_FATAL_FAILURE(CleanDir(temp_directory, &num_deleted_entries));
236 EXPECT_TRUE(DirExists(temp_directory));
237
238 // Add a file.
239 const std::string temp_filename = temp_directory + "TempFilenameTest";
240 WriteStringInFile("test\n", temp_filename);
241 EXPECT_TRUE(FileExists(temp_filename));
242
243 // Add an empty directory.
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200244 const std::string temp_subdir = temp_directory + Path("subdir/");
alessiob00b16f42017-06-01 03:29:40 -0700245 EXPECT_TRUE(CreateDir(temp_subdir));
246 EXPECT_TRUE(DirExists(temp_subdir));
247
248 // Checks.
Danil Chapovalov431abd92018-06-18 12:54:17 +0200249 absl::optional<std::vector<std::string>> dir_content =
alessiob00b16f42017-06-01 03:29:40 -0700250 ReadDirectory(temp_directory);
251 EXPECT_TRUE(dir_content);
252 EXPECT_EQ(2u, dir_content->size());
253 EXPECT_NO_FATAL_FAILURE(CleanDir(temp_directory, &num_deleted_entries));
254 EXPECT_EQ(2u, num_deleted_entries);
255 EXPECT_TRUE(RemoveDir(temp_directory));
256 EXPECT_FALSE(DirExists(temp_directory));
257}
258
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200259TEST_F(FileUtilsTest, DirNameStripsFilename) {
260 EXPECT_EQ(Path("/some/path"), DirName(Path("/some/path/file.txt")));
261}
262
263TEST_F(FileUtilsTest, DirNameKeepsStrippingRightmostPathComponent) {
264 EXPECT_EQ(Path("/some"), DirName(DirName(Path("/some/path/file.txt"))));
265}
266
267TEST_F(FileUtilsTest, DirNameDoesntCareIfAPathEndsInPathSeparator) {
268 EXPECT_EQ(Path("/some"), DirName(Path("/some/path/")));
269}
270
271TEST_F(FileUtilsTest, DirNameStopsAtRoot) {
272 EXPECT_EQ(Path("/"), DirName(Path("/")));
273}
274
alessiob00b16f42017-06-01 03:29:40 -0700275} // namespace test
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000276} // namespace webrtc