2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/test/testsupport/fileutils.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "webrtc/test/testsupport/gtest_disable.h"
23 static const char* kPathDelimiter = "\\";
25 static const char* kPathDelimiter = "/";
28 static const std::string kResourcesDir = "resources";
29 static const std::string kTestName = "fileutils_unittest";
30 static const std::string kExtension = "tmp";
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
37 class FileUtilsTest : public testing::Test {
41 virtual ~FileUtilsTest() {}
42 // Runs before the first test
43 static void SetUpTestCase() {
44 original_working_dir_ = webrtc::test::WorkingDir();
47 ASSERT_EQ(chdir(original_working_dir_.c_str()), 0);
50 ASSERT_EQ(chdir(original_working_dir_.c_str()), 0);
53 static std::string original_working_dir_;
56 std::string FileUtilsTest::original_working_dir_ = "";
58 // Tests that the project root path is returned for the default working
59 // directory that is automatically set when the test executable is launched.
60 // The test is not fully testing the implementation, since we cannot be sure
61 // of where the executable was launched from.
62 TEST_F(FileUtilsTest, ProjectRootPath) {
63 std::string project_root = webrtc::test::ProjectRootPath();
64 // Not very smart, but at least tests something.
65 ASSERT_GT(project_root.length(), 0u);
68 // Similar to the above test, but for the output dir
69 TEST_F(FileUtilsTest, DISABLED_ON_ANDROID(OutputPathFromUnchangedWorkingDir)) {
70 std::string path = webrtc::test::OutputPath();
71 std::string expected_end = "out";
72 expected_end = kPathDelimiter + expected_end + kPathDelimiter;
73 ASSERT_EQ(path.length() - expected_end.length(), path.find(expected_end));
76 // Tests with current working directory set to a directory higher up in the
77 // directory tree than the project root dir.
78 TEST_F(FileUtilsTest, DISABLED_ON_ANDROID(OutputPathFromRootWorkingDir)) {
79 ASSERT_EQ(0, chdir(kPathDelimiter));
80 ASSERT_EQ("./", webrtc::test::OutputPath());
83 TEST_F(FileUtilsTest, TempFilename) {
84 std::string temp_filename = webrtc::test::TempFilename(
85 webrtc::test::OutputPath(), "TempFilenameTest");
86 ASSERT_TRUE(webrtc::test::FileExists(temp_filename))
87 << "Couldn't find file: " << temp_filename;
88 remove(temp_filename.c_str());
91 // Only tests that the code executes
92 TEST_F(FileUtilsTest, CreateDir) {
93 std::string directory = "fileutils-unittest-empty-dir";
94 // Make sure it's removed if a previous test has failed:
95 remove(directory.c_str());
96 ASSERT_TRUE(webrtc::test::CreateDir(directory));
97 remove(directory.c_str());
100 TEST_F(FileUtilsTest, WorkingDirReturnsValue) {
101 // Hard to cover all platforms. Just test that it returns something without
103 std::string working_dir = webrtc::test::WorkingDir();
104 ASSERT_GT(working_dir.length(), 0u);
107 // Due to multiple platforms, it is hard to make a complete test for
108 // ResourcePath. Manual testing has been performed by removing files and
109 // verified the result confirms with the specified documentation for the
111 TEST_F(FileUtilsTest, ResourcePathReturnsValue) {
112 std::string resource = webrtc::test::ResourcePath(kTestName, kExtension);
113 ASSERT_GT(resource.find(kTestName), 0u);
114 ASSERT_GT(resource.find(kExtension), 0u);
117 TEST_F(FileUtilsTest, ResourcePathFromRootWorkingDir) {
118 ASSERT_EQ(0, chdir(kPathDelimiter));
119 std::string resource = webrtc::test::ResourcePath(kTestName, kExtension);
120 ASSERT_NE(resource.find("resources"), std::string::npos);
121 ASSERT_GT(resource.find(kTestName), 0u);
122 ASSERT_GT(resource.find(kExtension), 0u);
125 TEST_F(FileUtilsTest, GetFileSizeExistingFile) {
126 // Create a file with some dummy data in.
127 std::string temp_filename = webrtc::test::TempFilename(
128 webrtc::test::OutputPath(), "fileutils_unittest");
129 FILE* file = fopen(temp_filename.c_str(), "wb");
130 ASSERT_TRUE(file != NULL) << "Failed to open file: " << temp_filename;
131 ASSERT_GT(fprintf(file, "%s", "Dummy data"), 0) <<
132 "Failed to write to file: " << temp_filename;
134 ASSERT_GT(webrtc::test::GetFileSize(std::string(temp_filename.c_str())), 0u);
135 remove(temp_filename.c_str());
138 TEST_F(FileUtilsTest, GetFileSizeNonExistingFile) {
139 ASSERT_EQ(0u, webrtc::test::GetFileSize("non-existing-file.tmp"));
142 } // namespace webrtc