OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/test/testsupport/fileutils.h" | 11 #include "webrtc/test/testsupport/fileutils.h" |
12 | 12 |
13 #include <stdio.h> | 13 #include <stdio.h> |
14 | 14 |
| 15 #include <fstream> |
| 16 #include <iostream> |
15 #include <list> | 17 #include <list> |
16 #include <string> | 18 #include <string> |
17 | 19 |
| 20 #include "webrtc/base/checks.h" |
| 21 #include "webrtc/base/optional.h" |
| 22 #include "webrtc/base/pathutils.h" |
18 #include "webrtc/test/gtest.h" | 23 #include "webrtc/test/gtest.h" |
19 | 24 |
20 #ifdef WIN32 | 25 #ifdef WIN32 |
21 #define chdir _chdir | 26 #define chdir _chdir |
22 static const char* kPathDelimiter = "\\"; | 27 static const char* kPathDelimiter = "\\"; |
23 #else | 28 #else |
24 static const char* kPathDelimiter = "/"; | 29 static const char* kPathDelimiter = "/"; |
25 #endif | 30 #endif |
26 | 31 |
27 static const std::string kResourcesDir = "resources"; | 32 static const char kTestName[] = "fileutils_unittest"; |
28 static const std::string kTestName = "fileutils_unittest"; | 33 static const char kExtension[] = "tmp"; |
29 static const std::string kExtension = "tmp"; | |
30 | 34 |
31 namespace webrtc { | 35 namespace webrtc { |
| 36 namespace test { |
| 37 |
| 38 namespace { |
| 39 |
| 40 // Remove files and directories in a directory non-recursively and writes the |
| 41 // number of deleted items in |num_deleted_entries|. |
| 42 void CleanDir(const std::string& dir, size_t* num_deleted_entries) { |
| 43 RTC_DCHECK(num_deleted_entries); |
| 44 *num_deleted_entries = 0; |
| 45 rtc::Optional<std::vector<std::string>> dir_content = ReadDirectory(dir); |
| 46 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 |
| 60 void 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 |
32 | 67 |
33 // Test fixture to restore the working directory between each test, since some | 68 // Test fixture to restore the working directory between each test, since some |
34 // of them change it with chdir during execution (not restored by the | 69 // of them change it with chdir during execution (not restored by the |
35 // gtest framework). | 70 // gtest framework). |
36 class FileUtilsTest : public testing::Test { | 71 class FileUtilsTest : public testing::Test { |
37 protected: | 72 protected: |
38 FileUtilsTest() { | 73 FileUtilsTest() { |
39 } | 74 } |
40 ~FileUtilsTest() override {} | 75 ~FileUtilsTest() override {} |
41 // Runs before the first test | 76 // Runs before the first test |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 // Check that an existing file is not recognized as an existing directory. | 198 // Check that an existing file is not recognized as an existing directory. |
164 std::string temp_filename = webrtc::test::TempFilename( | 199 std::string temp_filename = webrtc::test::TempFilename( |
165 webrtc::test::OutputPath(), "TempFilenameTest"); | 200 webrtc::test::OutputPath(), "TempFilenameTest"); |
166 ASSERT_TRUE(webrtc::test::FileExists(temp_filename)) | 201 ASSERT_TRUE(webrtc::test::FileExists(temp_filename)) |
167 << "Couldn't find file: " << temp_filename; | 202 << "Couldn't find file: " << temp_filename; |
168 ASSERT_FALSE(webrtc::test::DirExists(temp_filename)) | 203 ASSERT_FALSE(webrtc::test::DirExists(temp_filename)) |
169 << "Existing file recognized as existing directory"; | 204 << "Existing file recognized as existing directory"; |
170 remove(temp_filename.c_str()); | 205 remove(temp_filename.c_str()); |
171 } | 206 } |
172 | 207 |
| 208 TEST_F(FileUtilsTest, WriteReadDeleteFilesAndDirs) { |
| 209 size_t num_deleted_entries; |
| 210 |
| 211 // Create an empty temporary directory for this test. |
| 212 const std::string temp_directory = |
| 213 OutputPath() + "TempFileUtilsTestReadDirectory" + kPathDelimiter; |
| 214 CreateDir(temp_directory); |
| 215 CleanDir(temp_directory, &num_deleted_entries); |
| 216 EXPECT_TRUE(DirExists(temp_directory)); |
| 217 |
| 218 // Add a file. |
| 219 const std::string temp_filename = temp_directory + "TempFilenameTest"; |
| 220 WriteStringInFile("test\n", temp_filename); |
| 221 EXPECT_TRUE(FileExists(temp_filename)); |
| 222 |
| 223 // Add an empty directory. |
| 224 const std::string temp_subdir = temp_directory + "subdir" + kPathDelimiter; |
| 225 EXPECT_TRUE(CreateDir(temp_subdir)); |
| 226 EXPECT_TRUE(DirExists(temp_subdir)); |
| 227 |
| 228 // Checks. |
| 229 rtc::Optional<std::vector<std::string>> dir_content = |
| 230 ReadDirectory(temp_directory); |
| 231 EXPECT_TRUE(dir_content); |
| 232 EXPECT_EQ(2u, dir_content->size()); |
| 233 CleanDir(temp_directory, &num_deleted_entries); |
| 234 EXPECT_EQ(2u, num_deleted_entries); |
| 235 EXPECT_TRUE(RemoveDir(temp_directory)); |
| 236 EXPECT_FALSE(DirExists(temp_directory)); |
| 237 |
| 238 EXPECT_NO_FATAL_FAILURE(); |
| 239 } |
| 240 |
| 241 } // namespace test |
173 } // namespace webrtc | 242 } // namespace webrtc |
OLD | NEW |