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