1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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 */
10
11 #include "test/testsupport/file_utils.h"
12
13 #include <stdio.h>
14
15 #include <algorithm>
16 #include <fstream>
17 #include <string>
18
19 #include "absl/strings/string_view.h"
20 #include "absl/types/optional.h"
21 #include "rtc_base/checks.h"
22 #include "test/gmock.h"
23 #include "test/gtest.h"
24
25 #ifdef WIN32
26 #define chdir _chdir
27 #endif
28
29 using ::testing::EndsWith;
30
31 namespace webrtc {
32 namespace test {
33
34 namespace {
35
Path(absl::string_view path)36 std::string Path(absl::string_view path) {
37 std::string result(path);
38 std::replace(result.begin(), result.end(), '/', kPathDelimiter[0]);
39 return result;
40 }
41
42 // Remove files and directories in a directory non-recursively and writes the
43 // number of deleted items in `num_deleted_entries`.
CleanDir(absl::string_view dir,size_t * num_deleted_entries)44 void CleanDir(absl::string_view dir, size_t* num_deleted_entries) {
45 RTC_DCHECK(num_deleted_entries);
46 *num_deleted_entries = 0;
47 absl::optional<std::vector<std::string>> dir_content = ReadDirectory(dir);
48 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
WriteStringInFile(absl::string_view what,absl::string_view file_path)62 void WriteStringInFile(absl::string_view what, absl::string_view file_path) {
63 std::ofstream out(std::string{file_path});
64 out << what;
65 out.close();
66 }
67
68 } // namespace
69
70 // 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).
73 class FileUtilsTest : public ::testing::Test {
74 protected:
FileUtilsTest()75 FileUtilsTest() {}
~FileUtilsTest()76 ~FileUtilsTest() override {}
77 // Runs before the first test
SetUpTestSuite()78 static void SetUpTestSuite() {
79 original_working_dir_ = webrtc::test::WorkingDir();
80 }
SetUp()81 void SetUp() override { ASSERT_EQ(chdir(original_working_dir_.c_str()), 0); }
TearDown()82 void TearDown() override {
83 ASSERT_EQ(chdir(original_working_dir_.c_str()), 0);
84 }
85
86 private:
87 static std::string original_working_dir_;
88 };
89
90 std::string FileUtilsTest::original_working_dir_ = "";
91
92 // 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.
ExpectedRootDirByPlatform()94 std::string ExpectedRootDirByPlatform() {
95 #if defined(WEBRTC_ANDROID)
96 return Path("chromium_tests_root/");
97 #elif defined(WEBRTC_IOS)
98 return Path("tmp/");
99 #else
100 return Path("out/");
101 #endif
102 }
103
TEST_F(FileUtilsTest,OutputPathFromUnchangedWorkingDir)104 TEST_F(FileUtilsTest, OutputPathFromUnchangedWorkingDir) {
105 std::string expected_end = ExpectedRootDirByPlatform();
106 std::string result = webrtc::test::OutputPath();
107
108 ASSERT_THAT(result, EndsWith(expected_end));
109 }
110
111 // Tests with current working directory set to a directory higher up in the
112 // directory tree than the project root dir.
TEST_F(FileUtilsTest,OutputPathFromRootWorkingDir)113 TEST_F(FileUtilsTest, OutputPathFromRootWorkingDir) {
114 ASSERT_EQ(0, chdir(kPathDelimiter.data()));
115
116 std::string expected_end = ExpectedRootDirByPlatform();
117 std::string result = webrtc::test::OutputPath();
118
119 ASSERT_THAT(result, EndsWith(expected_end));
120 }
121
TEST_F(FileUtilsTest,TempFilename)122 TEST_F(FileUtilsTest, TempFilename) {
123 std::string temp_filename = webrtc::test::TempFilename(
124 webrtc::test::OutputPath(), "TempFilenameTest");
125 ASSERT_TRUE(webrtc::test::FileExists(temp_filename))
126 << "Couldn't find file: " << temp_filename;
127 remove(temp_filename.c_str());
128 }
129
TEST_F(FileUtilsTest,GenerateTempFilename)130 TEST_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
143 // Only tests that the code executes
144 #if defined(WEBRTC_IOS)
145 #define MAYBE_CreateDir DISABLED_CreateDir
146 #else
147 #define MAYBE_CreateDir CreateDir
148 #endif
TEST_F(FileUtilsTest,MAYBE_CreateDir)149 TEST_F(FileUtilsTest, MAYBE_CreateDir) {
150 std::string directory = "fileutils-unittest-empty-dir";
151 // Make sure it's removed if a previous test has failed:
152 remove(directory.c_str());
153 ASSERT_TRUE(webrtc::test::CreateDir(directory));
154 remove(directory.c_str());
155 }
156
TEST_F(FileUtilsTest,WorkingDirReturnsValue)157 TEST_F(FileUtilsTest, WorkingDirReturnsValue) {
158 // This will obviously be different depending on where the webrtc checkout is,
159 // so just check something is returned.
160 std::string working_dir = webrtc::test::WorkingDir();
161 ASSERT_GT(working_dir.length(), 0u);
162 }
163
TEST_F(FileUtilsTest,ResourcePathReturnsCorrectPath)164 TEST_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
178 << " to exist; did "
179 "ResourcePath return an incorrect path?";
180 }
181
TEST_F(FileUtilsTest,ResourcePathFromRootWorkingDir)182 TEST_F(FileUtilsTest, ResourcePathFromRootWorkingDir) {
183 ASSERT_EQ(0, chdir(kPathDelimiter.data()));
184 std::string resource = webrtc::test::ResourcePath("whatever", "ext");
185 #if !defined(WEBRTC_IOS)
186 ASSERT_NE(resource.find("resources"), std::string::npos);
187 #endif
188 ASSERT_GT(resource.find("whatever"), 0u);
189 ASSERT_GT(resource.find("ext"), 0u);
190 }
191
TEST_F(FileUtilsTest,GetFileSizeExistingFile)192 TEST_F(FileUtilsTest, GetFileSizeExistingFile) {
193 // Create a file with some dummy data in.
194 std::string temp_filename = webrtc::test::TempFilename(
195 webrtc::test::OutputPath(), "fileutils_unittest");
196 FILE* file = fopen(temp_filename.c_str(), "wb");
197 ASSERT_TRUE(file != NULL) << "Failed to open file: " << temp_filename;
198 ASSERT_GT(fprintf(file, "%s", "Dummy data"), 0)
199 << "Failed to write to file: " << temp_filename;
200 fclose(file);
201 ASSERT_GT(webrtc::test::GetFileSize(temp_filename), 0u);
202 remove(temp_filename.c_str());
203 }
204
TEST_F(FileUtilsTest,GetFileSizeNonExistingFile)205 TEST_F(FileUtilsTest, GetFileSizeNonExistingFile) {
206 ASSERT_EQ(0u, webrtc::test::GetFileSize("non-existing-file.tmp"));
207 }
208
TEST_F(FileUtilsTest,DirExists)209 TEST_F(FileUtilsTest, DirExists) {
210 // Check that an existing directory is recognized as such.
211 ASSERT_TRUE(webrtc::test::DirExists(webrtc::test::OutputPath()))
212 << "Existing directory not found";
213
214 // Check that a non-existing directory is recognized as such.
215 std::string directory = "direxists-unittest-non_existing-dir";
216 ASSERT_FALSE(webrtc::test::DirExists(directory))
217 << "Non-existing directory found";
218
219 // Check that an existing file is not recognized as an existing directory.
220 std::string temp_filename = webrtc::test::TempFilename(
221 webrtc::test::OutputPath(), "TempFilenameTest");
222 ASSERT_TRUE(webrtc::test::FileExists(temp_filename))
223 << "Couldn't find file: " << temp_filename;
224 ASSERT_FALSE(webrtc::test::DirExists(temp_filename))
225 << "Existing file recognized as existing directory";
226 remove(temp_filename.c_str());
227 }
228
TEST_F(FileUtilsTest,WriteReadDeleteFilesAndDirs)229 TEST_F(FileUtilsTest, WriteReadDeleteFilesAndDirs) {
230 size_t num_deleted_entries;
231
232 // Create an empty temporary directory for this test.
233 const std::string temp_directory =
234 OutputPath() + Path("TempFileUtilsTestReadDirectory/");
235 CreateDir(temp_directory);
236 EXPECT_NO_FATAL_FAILURE(CleanDir(temp_directory, &num_deleted_entries));
237 EXPECT_TRUE(DirExists(temp_directory));
238
239 // Add a file.
240 const std::string temp_filename = temp_directory + "TempFilenameTest";
241 WriteStringInFile("test\n", temp_filename);
242 EXPECT_TRUE(FileExists(temp_filename));
243
244 // Add an empty directory.
245 const std::string temp_subdir = temp_directory + Path("subdir/");
246 EXPECT_TRUE(CreateDir(temp_subdir));
247 EXPECT_TRUE(DirExists(temp_subdir));
248
249 // Checks.
250 absl::optional<std::vector<std::string>> dir_content =
251 ReadDirectory(temp_directory);
252 EXPECT_TRUE(dir_content);
253 EXPECT_EQ(2u, dir_content->size());
254 EXPECT_NO_FATAL_FAILURE(CleanDir(temp_directory, &num_deleted_entries));
255 EXPECT_EQ(2u, num_deleted_entries);
256 EXPECT_TRUE(RemoveDir(temp_directory));
257 EXPECT_FALSE(DirExists(temp_directory));
258 }
259
TEST_F(FileUtilsTest,DirNameStripsFilename)260 TEST_F(FileUtilsTest, DirNameStripsFilename) {
261 EXPECT_EQ(Path("/some/path"), DirName(Path("/some/path/file.txt")));
262 }
263
TEST_F(FileUtilsTest,DirNameKeepsStrippingRightmostPathComponent)264 TEST_F(FileUtilsTest, DirNameKeepsStrippingRightmostPathComponent) {
265 EXPECT_EQ(Path("/some"), DirName(DirName(Path("/some/path/file.txt"))));
266 }
267
TEST_F(FileUtilsTest,DirNameDoesntCareIfAPathEndsInPathSeparator)268 TEST_F(FileUtilsTest, DirNameDoesntCareIfAPathEndsInPathSeparator) {
269 EXPECT_EQ(Path("/some"), DirName(Path("/some/path/")));
270 }
271
TEST_F(FileUtilsTest,DirNameStopsAtRoot)272 TEST_F(FileUtilsTest, DirNameStopsAtRoot) {
273 EXPECT_EQ(Path("/"), DirName(Path("/")));
274 }
275
276 } // namespace test
277 } // namespace webrtc
278