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/types/optional.h"
20 #include "rtc_base/checks.h"
21 #include "test/gmock.h"
22 #include "test/gtest.h"
23
24 #ifdef WIN32
25 #define chdir _chdir
26 #endif
27
28 using ::testing::EndsWith;
29
30 namespace webrtc {
31 namespace test {
32
33 namespace {
34
Path(const std::string & path)35 std::string Path(const std::string& path) {
36 std::string result = path;
37 std::replace(result.begin(), result.end(), '/', *kPathDelimiter);
38 return result;
39 }
40
41 // Remove files and directories in a directory non-recursively and writes the
42 // number of deleted items in |num_deleted_entries|.
CleanDir(const std::string & dir,size_t * num_deleted_entries)43 void CleanDir(const std::string& dir, size_t* num_deleted_entries) {
44 RTC_DCHECK(num_deleted_entries);
45 *num_deleted_entries = 0;
46 absl::optional<std::vector<std::string>> dir_content = ReadDirectory(dir);
47 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
WriteStringInFile(const std::string & what,const std::string & file_path)61 void 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
68
69 // 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).
72 class FileUtilsTest : public ::testing::Test {
73 protected:
FileUtilsTest()74 FileUtilsTest() {}
~FileUtilsTest()75 ~FileUtilsTest() override {}
76 // Runs before the first test
SetUpTestSuite()77 static void SetUpTestSuite() {
78 original_working_dir_ = webrtc::test::WorkingDir();
79 }
SetUp()80 void SetUp() override { ASSERT_EQ(chdir(original_working_dir_.c_str()), 0); }
TearDown()81 void TearDown() override {
82 ASSERT_EQ(chdir(original_working_dir_.c_str()), 0);
83 }
84
85 private:
86 static std::string original_working_dir_;
87 };
88
89 std::string FileUtilsTest::original_working_dir_ = "";
90
91 // 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.
ExpectedRootDirByPlatform()93 std::string ExpectedRootDirByPlatform() {
94 #if defined(WEBRTC_ANDROID)
95 return Path("chromium_tests_root/");
96 #elif defined(WEBRTC_IOS)
97 return Path("tmp/");
98 #else
99 return Path("out/");
100 #endif
101 }
102
TEST_F(FileUtilsTest,OutputPathFromUnchangedWorkingDir)103 TEST_F(FileUtilsTest, OutputPathFromUnchangedWorkingDir) {
104 std::string expected_end = ExpectedRootDirByPlatform();
105 std::string result = webrtc::test::OutputPath();
106
107 ASSERT_THAT(result, EndsWith(expected_end));
108 }
109
110 // Tests with current working directory set to a directory higher up in the
111 // directory tree than the project root dir.
TEST_F(FileUtilsTest,OutputPathFromRootWorkingDir)112 TEST_F(FileUtilsTest, OutputPathFromRootWorkingDir) {
113 ASSERT_EQ(0, chdir(kPathDelimiter));
114
115 std::string expected_end = ExpectedRootDirByPlatform();
116 std::string result = webrtc::test::OutputPath();
117
118 ASSERT_THAT(result, EndsWith(expected_end));
119 }
120
TEST_F(FileUtilsTest,TempFilename)121 TEST_F(FileUtilsTest, TempFilename) {
122 std::string temp_filename = webrtc::test::TempFilename(
123 webrtc::test::OutputPath(), "TempFilenameTest");
124 ASSERT_TRUE(webrtc::test::FileExists(temp_filename))
125 << "Couldn't find file: " << temp_filename;
126 remove(temp_filename.c_str());
127 }
128
TEST_F(FileUtilsTest,GenerateTempFilename)129 TEST_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
142 // Only tests that the code executes
143 #if defined(WEBRTC_IOS)
144 #define MAYBE_CreateDir DISABLED_CreateDir
145 #else
146 #define MAYBE_CreateDir CreateDir
147 #endif
TEST_F(FileUtilsTest,MAYBE_CreateDir)148 TEST_F(FileUtilsTest, MAYBE_CreateDir) {
149 std::string directory = "fileutils-unittest-empty-dir";
150 // Make sure it's removed if a previous test has failed:
151 remove(directory.c_str());
152 ASSERT_TRUE(webrtc::test::CreateDir(directory));
153 remove(directory.c_str());
154 }
155
TEST_F(FileUtilsTest,WorkingDirReturnsValue)156 TEST_F(FileUtilsTest, WorkingDirReturnsValue) {
157 // This will obviously be different depending on where the webrtc checkout is,
158 // so just check something is returned.
159 std::string working_dir = webrtc::test::WorkingDir();
160 ASSERT_GT(working_dir.length(), 0u);
161 }
162
TEST_F(FileUtilsTest,ResourcePathReturnsCorrectPath)163 TEST_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
177 << " to exist; did "
178 "ResourcePath return an incorrect path?";
179 }
180
TEST_F(FileUtilsTest,ResourcePathFromRootWorkingDir)181 TEST_F(FileUtilsTest, ResourcePathFromRootWorkingDir) {
182 ASSERT_EQ(0, chdir(kPathDelimiter));
183 std::string resource = webrtc::test::ResourcePath("whatever", "ext");
184 #if !defined(WEBRTC_IOS)
185 ASSERT_NE(resource.find("resources"), std::string::npos);
186 #endif
187 ASSERT_GT(resource.find("whatever"), 0u);
188 ASSERT_GT(resource.find("ext"), 0u);
189 }
190
TEST_F(FileUtilsTest,GetFileSizeExistingFile)191 TEST_F(FileUtilsTest, GetFileSizeExistingFile) {
192 // 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;
197 ASSERT_GT(fprintf(file, "%s", "Dummy data"), 0)
198 << "Failed to write to file: " << temp_filename;
199 fclose(file);
200 ASSERT_GT(webrtc::test::GetFileSize(std::string(temp_filename.c_str())), 0u);
201 remove(temp_filename.c_str());
202 }
203
TEST_F(FileUtilsTest,GetFileSizeNonExistingFile)204 TEST_F(FileUtilsTest, GetFileSizeNonExistingFile) {
205 ASSERT_EQ(0u, webrtc::test::GetFileSize("non-existing-file.tmp"));
206 }
207
TEST_F(FileUtilsTest,DirExists)208 TEST_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
TEST_F(FileUtilsTest,WriteReadDeleteFilesAndDirs)228 TEST_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 =
233 OutputPath() + Path("TempFileUtilsTestReadDirectory/");
234 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.
244 const std::string temp_subdir = temp_directory + Path("subdir/");
245 EXPECT_TRUE(CreateDir(temp_subdir));
246 EXPECT_TRUE(DirExists(temp_subdir));
247
248 // Checks.
249 absl::optional<std::vector<std::string>> dir_content =
250 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
TEST_F(FileUtilsTest,DirNameStripsFilename)259 TEST_F(FileUtilsTest, DirNameStripsFilename) {
260 EXPECT_EQ(Path("/some/path"), DirName(Path("/some/path/file.txt")));
261 }
262
TEST_F(FileUtilsTest,DirNameKeepsStrippingRightmostPathComponent)263 TEST_F(FileUtilsTest, DirNameKeepsStrippingRightmostPathComponent) {
264 EXPECT_EQ(Path("/some"), DirName(DirName(Path("/some/path/file.txt"))));
265 }
266
TEST_F(FileUtilsTest,DirNameDoesntCareIfAPathEndsInPathSeparator)267 TEST_F(FileUtilsTest, DirNameDoesntCareIfAPathEndsInPathSeparator) {
268 EXPECT_EQ(Path("/some"), DirName(Path("/some/path/")));
269 }
270
TEST_F(FileUtilsTest,DirNameStopsAtRoot)271 TEST_F(FileUtilsTest, DirNameStopsAtRoot) {
272 EXPECT_EQ(Path("/"), DirName(Path("/")));
273 }
274
275 } // namespace test
276 } // namespace webrtc
277