kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 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 | */ |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 10 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame^] | 11 | #include "testsupport/fileutils.h" |
| 12 | |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 13 | #include <cstdio> |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 14 | #include <list> |
| 15 | #include <string> |
| 16 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 17 | #include "gtest/gtest.h" |
| 18 | |
| 19 | #ifdef WIN32 |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 20 | static const char* kPathDelimiter = "\\"; |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 21 | #else |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 22 | static const char* kPathDelimiter = "/"; |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 23 | #endif |
| 24 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 25 | static const std::string kDummyDir = "file_utils_unittest_dummy_dir"; |
| 26 | static const std::string kResourcesDir = "resources"; |
| 27 | static const std::string kTestName = "fileutils_unittest"; |
| 28 | static const std::string kExtension = "tmp"; |
| 29 | |
| 30 | typedef std::list<std::string> FileList; |
| 31 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 32 | namespace webrtc { |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 33 | |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 34 | // Test fixture to restore the working directory between each test, since some |
| 35 | // of them change it with chdir during execution (not restored by the |
| 36 | // gtest framework). |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 37 | class FileUtilsTest : public testing::Test { |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 38 | protected: |
| 39 | FileUtilsTest() { |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 40 | } |
| 41 | virtual ~FileUtilsTest() {} |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 42 | // Runs before the first test |
| 43 | static void SetUpTestCase() { |
| 44 | original_working_dir_ = webrtc::test::WorkingDir(); |
| 45 | std::string resources_path = original_working_dir_ + kPathDelimiter + |
| 46 | kResourcesDir + kPathDelimiter; |
| 47 | webrtc::test::CreateDirectory(resources_path); |
| 48 | |
| 49 | files_.push_back(resources_path + kTestName + "." + kExtension); |
| 50 | files_.push_back(resources_path + kTestName + "_32." + kExtension); |
| 51 | files_.push_back(resources_path + kTestName + "_64." + kExtension); |
| 52 | files_.push_back(resources_path + kTestName + "_linux." + kExtension); |
| 53 | files_.push_back(resources_path + kTestName + "_mac." + kExtension); |
| 54 | files_.push_back(resources_path + kTestName + "_win." + kExtension); |
| 55 | files_.push_back(resources_path + kTestName + "_linux_32." + kExtension); |
| 56 | files_.push_back(resources_path + kTestName + "_mac_32." + kExtension); |
| 57 | files_.push_back(resources_path + kTestName + "_win_32." + kExtension); |
| 58 | files_.push_back(resources_path + kTestName + "_linux_64." + kExtension); |
| 59 | files_.push_back(resources_path + kTestName + "_mac_64." + kExtension); |
| 60 | files_.push_back(resources_path + kTestName + "_win_64." + kExtension); |
| 61 | |
| 62 | // Now that the resources dir exists, write some empty test files into it. |
| 63 | for (FileList::iterator file_it = files_.begin(); |
| 64 | file_it != files_.end(); ++file_it) { |
| 65 | FILE* file = fopen(file_it->c_str(), "wb"); |
| 66 | ASSERT_TRUE(file != NULL) << "Failed to write file: " << file_it->c_str(); |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame^] | 67 | ASSERT_GT(fprintf(file, "%s", "Dummy data"), 0); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 68 | fclose(file); |
| 69 | } |
| 70 | // Create a dummy subdir that can be chdir'ed into for testing purposes. |
| 71 | empty_dummy_dir_ = original_working_dir_ + kPathDelimiter + kDummyDir; |
| 72 | webrtc::test::CreateDirectory(empty_dummy_dir_); |
| 73 | } |
| 74 | static void TearDownTestCase() { |
| 75 | // Clean up all resource files written |
| 76 | for (FileList::iterator file_it = files_.begin(); |
| 77 | file_it != files_.end(); ++file_it) { |
| 78 | remove(file_it->c_str()); |
| 79 | } |
| 80 | std::remove(empty_dummy_dir_.c_str()); |
| 81 | } |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 82 | void SetUp() { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 83 | ASSERT_EQ(chdir(original_working_dir_.c_str()), 0); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 84 | } |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 85 | void TearDown() { |
| 86 | ASSERT_EQ(chdir(original_working_dir_.c_str()), 0); |
| 87 | } |
| 88 | protected: |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame^] | 89 | static FileList files_; |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 90 | static std::string empty_dummy_dir_; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 91 | private: |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 92 | static std::string original_working_dir_; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 95 | FileList FileUtilsTest::files_; |
| 96 | std::string FileUtilsTest::original_working_dir_ = ""; |
| 97 | std::string FileUtilsTest::empty_dummy_dir_ = ""; |
| 98 | |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 99 | // Tests that the project root path is returned for the default working |
| 100 | // directory that is automatically set when the test executable is launched. |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 101 | // The test is not fully testing the implementation, since we cannot be sure |
| 102 | // of where the executable was launched from. |
| 103 | // The test will fail if the top level directory is not named "trunk". |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 104 | TEST_F(FileUtilsTest, ProjectRootPathFromUnchangedWorkingDir) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 105 | std::string path = webrtc::test::ProjectRootPath(); |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 106 | std::string expected_end = "trunk"; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 107 | expected_end = kPathDelimiter + expected_end + kPathDelimiter; |
| 108 | ASSERT_EQ(path.length() - expected_end.length(), path.find(expected_end)); |
| 109 | } |
| 110 | |
| 111 | // Similar to the above test, but for the output dir |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 112 | TEST_F(FileUtilsTest, OutputPathFromUnchangedWorkingDir) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 113 | std::string path = webrtc::test::OutputPath(); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 114 | std::string expected_end = "out"; |
| 115 | expected_end = kPathDelimiter + expected_end + kPathDelimiter; |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 116 | ASSERT_EQ(path.length() - expected_end.length(), path.find(expected_end)); |
| 117 | } |
| 118 | |
| 119 | // Tests setting the current working directory to a directory three levels |
| 120 | // deeper from the current one. Then testing that the project path returned |
| 121 | // is still the same, when the function under test is called again. |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 122 | TEST_F(FileUtilsTest, ProjectRootPathFromDeeperWorkingDir) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 123 | std::string path = webrtc::test::ProjectRootPath(); |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 124 | std::string original_working_dir = path; // This is the correct project root |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 125 | // Change to a subdirectory path. |
| 126 | ASSERT_EQ(0, chdir(empty_dummy_dir_.c_str())); |
| 127 | ASSERT_EQ(original_working_dir, webrtc::test::ProjectRootPath()); |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 128 | } |
| 129 | |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 130 | // Similar to the above test, but for the output dir |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 131 | TEST_F(FileUtilsTest, OutputPathFromDeeperWorkingDir) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 132 | std::string path = webrtc::test::OutputPath(); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 133 | std::string original_working_dir = path; |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 134 | ASSERT_EQ(0, chdir(empty_dummy_dir_.c_str())); |
| 135 | ASSERT_EQ(original_working_dir, webrtc::test::OutputPath()); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 136 | } |
| 137 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 138 | // Tests with current working directory set to a directory higher up in the |
| 139 | // directory tree than the project root dir. This case shall return a specified |
| 140 | // error string as a directory (which will be an invalid path). |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 141 | TEST_F(FileUtilsTest, ProjectRootPathFromRootWorkingDir) { |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 142 | // Change current working dir to the root of the current file system |
| 143 | // (this will always be "above" our project root dir). |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 144 | ASSERT_EQ(0, chdir(kPathDelimiter)); |
| 145 | ASSERT_EQ(webrtc::test::kCannotFindProjectRootDir, |
| 146 | webrtc::test::ProjectRootPath()); |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 147 | } |
| 148 | |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 149 | // Similar to the above test, but for the output dir |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 150 | TEST_F(FileUtilsTest, OutputPathFromRootWorkingDir) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 151 | ASSERT_EQ(0, chdir(kPathDelimiter)); |
| 152 | ASSERT_EQ("./", webrtc::test::OutputPath()); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 153 | } |
| 154 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 155 | // Only tests that the code executes |
| 156 | TEST_F(FileUtilsTest, CreateDirectory) { |
| 157 | std::string directory = "fileutils-unittest-empty-dir"; |
| 158 | // Make sure it's removed if a previous test has failed: |
| 159 | std::remove(directory.c_str()); |
| 160 | ASSERT_TRUE(webrtc::test::CreateDirectory(directory)); |
| 161 | std::remove(directory.c_str()); |
| 162 | } |
| 163 | |
| 164 | TEST_F(FileUtilsTest, WorkingDirReturnsValue) { |
| 165 | // Hard to cover all platforms. Just test that it returns something without |
| 166 | // crashing: |
| 167 | std::string working_dir = webrtc::test::WorkingDir(); |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame^] | 168 | ASSERT_GT(working_dir.length(), 0u); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | // Due to multiple platforms, it is hard to make a complete test for |
| 172 | // ResourcePath. Manual testing has been performed by removing files and |
| 173 | // verified the result confirms with the specified documentation for the |
| 174 | // function. |
| 175 | TEST_F(FileUtilsTest, ResourcePathReturnsValue) { |
| 176 | std::string resource = webrtc::test::ResourcePath(kTestName, kExtension); |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame^] | 177 | ASSERT_GT(resource.find(kTestName), 0u); |
| 178 | ASSERT_GT(resource.find(kExtension), 0u); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 179 | ASSERT_EQ(0, chdir(kPathDelimiter)); |
| 180 | ASSERT_EQ("./", webrtc::test::OutputPath()); |
| 181 | } |
| 182 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame^] | 183 | TEST_F(FileUtilsTest, GetFileSizeExistingFile) { |
| 184 | ASSERT_GT(webrtc::test::GetFileSize(files_.front()), 0u); |
| 185 | } |
| 186 | |
| 187 | TEST_F(FileUtilsTest, GetFileSizeNonExistingFile) { |
| 188 | ASSERT_EQ(0u, webrtc::test::GetFileSize("non-existing-file.tmp")); |
| 189 | } |
| 190 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 191 | } // namespace webrtc |