blob: e9aa03c509f5fc15bb400efe1d559beb64a872f2 [file] [log] [blame]
kjellander@webrtc.org7951e812011-10-13 12:24:41 +00001/*
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.org4d8cd9d2011-11-09 11:24:14 +000010
11#include <cstdio>
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000012#include <list>
13#include <string>
14
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000015#include "gtest/gtest.h"
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000016#include "testsupport/fileutils.h"
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000017
18#ifdef WIN32
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000019static const char* kPathDelimiter = "\\";
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000020#else
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000021static const char* kPathDelimiter = "/";
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000022#endif
23
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000024static const std::string kDummyDir = "file_utils_unittest_dummy_dir";
25static const std::string kResourcesDir = "resources";
26static const std::string kTestName = "fileutils_unittest";
27static const std::string kExtension = "tmp";
28
29typedef std::list<std::string> FileList;
30
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000031namespace webrtc {
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000032
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000033// 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.org4ed4f242011-12-05 16:31:12 +000036class FileUtilsTest : public testing::Test {
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000037 protected:
38 FileUtilsTest() {
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000039 }
40 virtual ~FileUtilsTest() {}
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000041 // 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.org4d8cd9d2011-11-09 11:24:14 +000081 void SetUp() {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000082 ASSERT_EQ(chdir(original_working_dir_.c_str()), 0);
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000083 }
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000084 void TearDown() {
85 ASSERT_EQ(chdir(original_working_dir_.c_str()), 0);
86 }
87 protected:
88 static std::string empty_dummy_dir_;
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000089 private:
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000090 static FileList files_;
91 static std::string original_working_dir_;
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000092};
93
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000094FileList FileUtilsTest::files_;
95std::string FileUtilsTest::original_working_dir_ = "";
96std::string FileUtilsTest::empty_dummy_dir_ = "";
97
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000098// 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.org7951e812011-10-13 12:24:41 +0000100// 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.org0db7dc62011-11-13 01:34:05 +0000103TEST_F(FileUtilsTest, ProjectRootPathFromUnchangedWorkingDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000104 std::string path = webrtc::test::ProjectRootPath();
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000105 std::string expected_end = "trunk";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000106 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.org0db7dc62011-11-13 01:34:05 +0000111TEST_F(FileUtilsTest, OutputPathFromUnchangedWorkingDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000112 std::string path = webrtc::test::OutputPath();
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000113 std::string expected_end = "out";
114 expected_end = kPathDelimiter + expected_end + kPathDelimiter;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000115 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.org0db7dc62011-11-13 01:34:05 +0000121TEST_F(FileUtilsTest, ProjectRootPathFromDeeperWorkingDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000122 std::string path = webrtc::test::ProjectRootPath();
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000123 std::string original_working_dir = path; // This is the correct project root
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000124 // 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.org7951e812011-10-13 12:24:41 +0000127}
128
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000129// Similar to the above test, but for the output dir
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000130TEST_F(FileUtilsTest, OutputPathFromDeeperWorkingDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000131 std::string path = webrtc::test::OutputPath();
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000132 std::string original_working_dir = path;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000133 ASSERT_EQ(0, chdir(empty_dummy_dir_.c_str()));
134 ASSERT_EQ(original_working_dir, webrtc::test::OutputPath());
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000135}
136
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000137// 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.org0db7dc62011-11-13 01:34:05 +0000140TEST_F(FileUtilsTest, ProjectRootPathFromRootWorkingDir) {
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000141 // Change current working dir to the root of the current file system
142 // (this will always be "above" our project root dir).
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000143 ASSERT_EQ(0, chdir(kPathDelimiter));
144 ASSERT_EQ(webrtc::test::kCannotFindProjectRootDir,
145 webrtc::test::ProjectRootPath());
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000146}
147
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000148// Similar to the above test, but for the output dir
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000149TEST_F(FileUtilsTest, OutputPathFromRootWorkingDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000150 ASSERT_EQ(0, chdir(kPathDelimiter));
151 ASSERT_EQ("./", webrtc::test::OutputPath());
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000152}
153
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000154// Only tests that the code executes
155TEST_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
163TEST_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.
174TEST_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.org7951e812011-10-13 12:24:41 +0000182} // namespace webrtc