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" |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 19 | |
| 20 | #ifdef WIN32 |
phoglund@webrtc.org | 9d9ad88 | 2012-02-07 16:16:52 +0000 | [diff] [blame] | 21 | #define chdir _chdir |
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 | #else |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 24 | static const char* kPathDelimiter = "/"; |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 25 | #endif |
| 26 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 27 | static const std::string kResourcesDir = "resources"; |
| 28 | static const std::string kTestName = "fileutils_unittest"; |
| 29 | static const std::string kExtension = "tmp"; |
| 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(); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 44 | } |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 45 | void SetUp() { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 46 | ASSERT_EQ(chdir(original_working_dir_.c_str()), 0); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 47 | } |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 48 | void TearDown() { |
| 49 | ASSERT_EQ(chdir(original_working_dir_.c_str()), 0); |
| 50 | } |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 51 | private: |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 52 | static std::string original_working_dir_; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 55 | std::string FileUtilsTest::original_working_dir_ = ""; |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 56 | |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 57 | // Tests that the project root path is returned for the default working |
| 58 | // directory that is automatically set when the test executable is launched. |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 59 | // The test is not fully testing the implementation, since we cannot be sure |
| 60 | // of where the executable was launched from. |
kjellander@webrtc.org | bf0e560 | 2012-10-19 16:07:02 +0000 | [diff] [blame] | 61 | TEST_F(FileUtilsTest, ProjectRootPath) { |
| 62 | std::string project_root = webrtc::test::ProjectRootPath(); |
| 63 | // Not very smart, but at least tests something. |
| 64 | ASSERT_GT(project_root.length(), 0u); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // Similar to the above test, but for the output dir |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame^] | 68 | #if defined(WEBRTC_ANDROID) |
| 69 | #define MAYBE_OutputPathFromUnchangedWorkingDir \ |
| 70 | DISABLED_OutputPathFromUnchangedWorkingDir |
| 71 | #else |
| 72 | #define MAYBE_OutputPathFromUnchangedWorkingDir \ |
| 73 | OutputPathFromUnchangedWorkingDir |
| 74 | #endif |
| 75 | TEST_F(FileUtilsTest, MAYBE_OutputPathFromUnchangedWorkingDir) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 76 | std::string path = webrtc::test::OutputPath(); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 77 | std::string expected_end = "out"; |
| 78 | expected_end = kPathDelimiter + expected_end + kPathDelimiter; |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 79 | ASSERT_EQ(path.length() - expected_end.length(), path.find(expected_end)); |
| 80 | } |
| 81 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 82 | // 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] | 83 | // directory tree than the project root dir. |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame^] | 84 | #if defined(WEBRTC_ANDROID) |
| 85 | #define MAYBE_OutputPathFromRootWorkingDir DISABLED_OutputPathFromRootWorkingDir |
| 86 | #else |
| 87 | #define MAYBE_OutputPathFromRootWorkingDir OutputPathFromRootWorkingDir |
| 88 | #endif |
| 89 | TEST_F(FileUtilsTest, MAYBE_OutputPathFromRootWorkingDir) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 90 | ASSERT_EQ(0, chdir(kPathDelimiter)); |
| 91 | ASSERT_EQ("./", webrtc::test::OutputPath()); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 92 | } |
| 93 | |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 94 | TEST_F(FileUtilsTest, TempFilename) { |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 95 | std::string temp_filename = webrtc::test::TempFilename( |
| 96 | webrtc::test::OutputPath(), "TempFilenameTest"); |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 97 | ASSERT_TRUE(webrtc::test::FileExists(temp_filename)) |
| 98 | << "Couldn't find file: " << temp_filename; |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 99 | remove(temp_filename.c_str()); |
| 100 | } |
| 101 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 102 | // Only tests that the code executes |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 103 | TEST_F(FileUtilsTest, CreateDir) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 104 | std::string directory = "fileutils-unittest-empty-dir"; |
| 105 | // Make sure it's removed if a previous test has failed: |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 106 | remove(directory.c_str()); |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 107 | ASSERT_TRUE(webrtc::test::CreateDir(directory)); |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 108 | remove(directory.c_str()); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | TEST_F(FileUtilsTest, WorkingDirReturnsValue) { |
| 112 | // Hard to cover all platforms. Just test that it returns something without |
| 113 | // crashing: |
| 114 | std::string working_dir = webrtc::test::WorkingDir(); |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 115 | ASSERT_GT(working_dir.length(), 0u); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // Due to multiple platforms, it is hard to make a complete test for |
| 119 | // ResourcePath. Manual testing has been performed by removing files and |
| 120 | // verified the result confirms with the specified documentation for the |
| 121 | // function. |
| 122 | TEST_F(FileUtilsTest, ResourcePathReturnsValue) { |
| 123 | std::string resource = webrtc::test::ResourcePath(kTestName, kExtension); |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 124 | ASSERT_GT(resource.find(kTestName), 0u); |
| 125 | ASSERT_GT(resource.find(kExtension), 0u); |
kjellander@webrtc.org | 193600b | 2012-10-17 04:39:44 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | TEST_F(FileUtilsTest, ResourcePathFromRootWorkingDir) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 129 | ASSERT_EQ(0, chdir(kPathDelimiter)); |
kjellander@webrtc.org | 193600b | 2012-10-17 04:39:44 +0000 | [diff] [blame] | 130 | std::string resource = webrtc::test::ResourcePath(kTestName, kExtension); |
| 131 | ASSERT_NE(resource.find("resources"), std::string::npos); |
| 132 | ASSERT_GT(resource.find(kTestName), 0u); |
| 133 | ASSERT_GT(resource.find(kExtension), 0u); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 134 | } |
| 135 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 136 | TEST_F(FileUtilsTest, GetFileSizeExistingFile) { |
kjellander@webrtc.org | e794c36 | 2014-09-29 11:47:28 +0000 | [diff] [blame] | 137 | // Create a file with some dummy data in. |
| 138 | std::string temp_filename = webrtc::test::TempFilename( |
| 139 | webrtc::test::OutputPath(), "fileutils_unittest"); |
| 140 | FILE* file = fopen(temp_filename.c_str(), "wb"); |
| 141 | ASSERT_TRUE(file != NULL) << "Failed to open file: " << temp_filename; |
| 142 | ASSERT_GT(fprintf(file, "%s", "Dummy data"), 0) << |
| 143 | "Failed to write to file: " << temp_filename; |
| 144 | fclose(file); |
| 145 | ASSERT_GT(webrtc::test::GetFileSize(std::string(temp_filename.c_str())), 0u); |
| 146 | remove(temp_filename.c_str()); |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | TEST_F(FileUtilsTest, GetFileSizeNonExistingFile) { |
| 150 | ASSERT_EQ(0u, webrtc::test::GetFileSize("non-existing-file.tmp")); |
| 151 | } |
| 152 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 153 | } // namespace webrtc |