kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 1 | /* |
phoglund@webrtc.org | 9d9ad88 | 2012-02-07 16:16:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 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 | |
pbos@webrtc.org | 34741c8 | 2013-05-27 08:02:22 +0000 | [diff] [blame] | 11 | #include "webrtc/test/testsupport/fileutils.h" |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <stdio.h> |
| 14 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 15 | #include <list> |
| 16 | #include <string> |
| 17 | |
pbos@webrtc.org | 34741c8 | 2013-05-27 08:02:22 +0000 | [diff] [blame] | 18 | #include "testing/gtest/include/gtest/gtest.h" |
henrike@webrtc.org | 89c6740 | 2013-08-02 16:53:47 +0000 | [diff] [blame] | 19 | #include "webrtc/test/testsupport/gtest_disable.h" |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 20 | |
| 21 | #ifdef WIN32 |
phoglund@webrtc.org | 9d9ad88 | 2012-02-07 16:16:52 +0000 | [diff] [blame] | 22 | #define chdir _chdir |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 23 | static const char* kPathDelimiter = "\\"; |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 24 | #else |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 25 | static const char* kPathDelimiter = "/"; |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 26 | #endif |
| 27 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 28 | static const std::string kResourcesDir = "resources"; |
| 29 | static const std::string kTestName = "fileutils_unittest"; |
| 30 | static const std::string kExtension = "tmp"; |
| 31 | |
| 32 | typedef std::list<std::string> FileList; |
| 33 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 34 | namespace webrtc { |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 35 | |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 36 | // Test fixture to restore the working directory between each test, since some |
| 37 | // of them change it with chdir during execution (not restored by the |
| 38 | // gtest framework). |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 39 | class FileUtilsTest : public testing::Test { |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 40 | protected: |
| 41 | FileUtilsTest() { |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 42 | } |
| 43 | virtual ~FileUtilsTest() {} |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 44 | // Runs before the first test |
| 45 | static void SetUpTestCase() { |
| 46 | original_working_dir_ = webrtc::test::WorkingDir(); |
| 47 | std::string resources_path = original_working_dir_ + kPathDelimiter + |
| 48 | kResourcesDir + kPathDelimiter; |
| 49 | webrtc::test::CreateDirectory(resources_path); |
| 50 | |
| 51 | files_.push_back(resources_path + kTestName + "." + kExtension); |
| 52 | files_.push_back(resources_path + kTestName + "_32." + kExtension); |
| 53 | files_.push_back(resources_path + kTestName + "_64." + kExtension); |
| 54 | files_.push_back(resources_path + kTestName + "_linux." + kExtension); |
| 55 | files_.push_back(resources_path + kTestName + "_mac." + kExtension); |
| 56 | files_.push_back(resources_path + kTestName + "_win." + kExtension); |
| 57 | files_.push_back(resources_path + kTestName + "_linux_32." + kExtension); |
| 58 | files_.push_back(resources_path + kTestName + "_mac_32." + kExtension); |
| 59 | files_.push_back(resources_path + kTestName + "_win_32." + kExtension); |
| 60 | files_.push_back(resources_path + kTestName + "_linux_64." + kExtension); |
| 61 | files_.push_back(resources_path + kTestName + "_mac_64." + kExtension); |
| 62 | files_.push_back(resources_path + kTestName + "_win_64." + kExtension); |
| 63 | |
| 64 | // Now that the resources dir exists, write some empty test files into it. |
| 65 | for (FileList::iterator file_it = files_.begin(); |
| 66 | file_it != files_.end(); ++file_it) { |
| 67 | FILE* file = fopen(file_it->c_str(), "wb"); |
| 68 | 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] | 69 | ASSERT_GT(fprintf(file, "%s", "Dummy data"), 0); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 70 | fclose(file); |
| 71 | } |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 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 | } |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 79 | } |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 80 | void SetUp() { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 81 | ASSERT_EQ(chdir(original_working_dir_.c_str()), 0); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 82 | } |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 83 | void TearDown() { |
| 84 | ASSERT_EQ(chdir(original_working_dir_.c_str()), 0); |
| 85 | } |
| 86 | protected: |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 87 | static FileList files_; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 88 | private: |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 89 | static std::string original_working_dir_; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 92 | FileList FileUtilsTest::files_; |
| 93 | std::string FileUtilsTest::original_working_dir_ = ""; |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 94 | |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 95 | // Tests that the project root path is returned for the default working |
| 96 | // directory that is automatically set when the test executable is launched. |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 97 | // The test is not fully testing the implementation, since we cannot be sure |
| 98 | // of where the executable was launched from. |
kjellander@webrtc.org | bf0e560 | 2012-10-19 16:07:02 +0000 | [diff] [blame] | 99 | TEST_F(FileUtilsTest, ProjectRootPath) { |
| 100 | std::string project_root = webrtc::test::ProjectRootPath(); |
| 101 | // Not very smart, but at least tests something. |
| 102 | ASSERT_GT(project_root.length(), 0u); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // Similar to the above test, but for the output dir |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 106 | TEST_F(FileUtilsTest, OutputPathFromUnchangedWorkingDir) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 107 | std::string path = webrtc::test::OutputPath(); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 108 | std::string expected_end = "out"; |
| 109 | expected_end = kPathDelimiter + expected_end + kPathDelimiter; |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 110 | ASSERT_EQ(path.length() - expected_end.length(), path.find(expected_end)); |
| 111 | } |
| 112 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 113 | // Tests with current working directory set to a directory higher up in the |
kjellander@webrtc.org | 193600b | 2012-10-17 04:39:44 +0000 | [diff] [blame] | 114 | // directory tree than the project root dir. |
henrike@webrtc.org | 89c6740 | 2013-08-02 16:53:47 +0000 | [diff] [blame] | 115 | TEST_F(FileUtilsTest, DISABLED_ON_ANDROID(OutputPathFromRootWorkingDir)) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 116 | ASSERT_EQ(0, chdir(kPathDelimiter)); |
| 117 | ASSERT_EQ("./", webrtc::test::OutputPath()); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 118 | } |
| 119 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 120 | // Only tests that the code executes |
| 121 | TEST_F(FileUtilsTest, CreateDirectory) { |
| 122 | std::string directory = "fileutils-unittest-empty-dir"; |
| 123 | // Make sure it's removed if a previous test has failed: |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 124 | remove(directory.c_str()); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 125 | ASSERT_TRUE(webrtc::test::CreateDirectory(directory)); |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 126 | remove(directory.c_str()); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | TEST_F(FileUtilsTest, WorkingDirReturnsValue) { |
| 130 | // Hard to cover all platforms. Just test that it returns something without |
| 131 | // crashing: |
| 132 | std::string working_dir = webrtc::test::WorkingDir(); |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 133 | ASSERT_GT(working_dir.length(), 0u); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // Due to multiple platforms, it is hard to make a complete test for |
| 137 | // ResourcePath. Manual testing has been performed by removing files and |
| 138 | // verified the result confirms with the specified documentation for the |
| 139 | // function. |
| 140 | TEST_F(FileUtilsTest, ResourcePathReturnsValue) { |
| 141 | std::string resource = webrtc::test::ResourcePath(kTestName, kExtension); |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 142 | ASSERT_GT(resource.find(kTestName), 0u); |
| 143 | ASSERT_GT(resource.find(kExtension), 0u); |
kjellander@webrtc.org | 193600b | 2012-10-17 04:39:44 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | TEST_F(FileUtilsTest, ResourcePathFromRootWorkingDir) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 147 | ASSERT_EQ(0, chdir(kPathDelimiter)); |
kjellander@webrtc.org | 193600b | 2012-10-17 04:39:44 +0000 | [diff] [blame] | 148 | std::string resource = webrtc::test::ResourcePath(kTestName, kExtension); |
| 149 | ASSERT_NE(resource.find("resources"), std::string::npos); |
| 150 | ASSERT_GT(resource.find(kTestName), 0u); |
| 151 | ASSERT_GT(resource.find(kExtension), 0u); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 152 | } |
| 153 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 154 | TEST_F(FileUtilsTest, GetFileSizeExistingFile) { |
| 155 | ASSERT_GT(webrtc::test::GetFileSize(files_.front()), 0u); |
| 156 | } |
| 157 | |
| 158 | TEST_F(FileUtilsTest, GetFileSizeNonExistingFile) { |
| 159 | ASSERT_EQ(0u, webrtc::test::GetFileSize("non-existing-file.tmp")); |
| 160 | } |
| 161 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 162 | } // namespace webrtc |