blob: 132436f0040974dd151bceb43105f720bb9154d5 [file] [log] [blame]
kjellander@webrtc.org7951e812011-10-13 12:24:41 +00001/*
phoglund@webrtc.org9d9ad882012-02-07 16:16:52 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
kjellander@webrtc.org7951e812011-10-13 12:24:41 +00003 *
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
pbos@webrtc.org34741c82013-05-27 08:02:22 +000011#include "webrtc/test/testsupport/fileutils.h"
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <stdio.h>
14
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000015#include <list>
16#include <string>
17
kwiberg77eab702016-09-28 17:42:01 -070018#include "webrtc/test/gtest.h"
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000019
20#ifdef WIN32
phoglund@webrtc.org9d9ad882012-02-07 16:16:52 +000021#define chdir _chdir
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000022static const char* kPathDelimiter = "\\";
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000023#else
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000024static const char* kPathDelimiter = "/";
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000025#endif
26
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000027static const std::string kResourcesDir = "resources";
28static const std::string kTestName = "fileutils_unittest";
29static const std::string kExtension = "tmp";
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 }
ehmaldonado37535bf2016-12-05 06:42:45 -080040 ~FileUtilsTest() override {}
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();
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000044 }
ehmaldonado37535bf2016-12-05 06:42:45 -080045 void SetUp() override {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000046 ASSERT_EQ(chdir(original_working_dir_.c_str()), 0);
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000047 }
ehmaldonado37535bf2016-12-05 06:42:45 -080048 void TearDown() override {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000049 ASSERT_EQ(chdir(original_working_dir_.c_str()), 0);
50 }
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000051 private:
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000052 static std::string original_working_dir_;
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000053};
54
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000055std::string FileUtilsTest::original_working_dir_ = "";
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000056
ehmaldonadod661e9c2016-11-22 10:42:59 -080057// Tests that the project output dir path is returned for the default working
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000058// directory that is automatically set when the test executable is launched.
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000059// The test is not fully testing the implementation, since we cannot be sure
60// of where the executable was launched from.
kthelgason5d682ca2017-01-10 03:00:41 -080061#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Peter Boströme2976c82016-01-04 22:44:05 +010062#define MAYBE_OutputPathFromUnchangedWorkingDir \
63 DISABLED_OutputPathFromUnchangedWorkingDir
64#else
65#define MAYBE_OutputPathFromUnchangedWorkingDir \
66 OutputPathFromUnchangedWorkingDir
67#endif
68TEST_F(FileUtilsTest, MAYBE_OutputPathFromUnchangedWorkingDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000069 std::string path = webrtc::test::OutputPath();
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000070 std::string expected_end = "out";
71 expected_end = kPathDelimiter + expected_end + kPathDelimiter;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000072 ASSERT_EQ(path.length() - expected_end.length(), path.find(expected_end));
73}
74
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000075// Tests with current working directory set to a directory higher up in the
kjellander@webrtc.org193600b2012-10-17 04:39:44 +000076// directory tree than the project root dir.
kthelgason5d682ca2017-01-10 03:00:41 -080077#if defined(WEBRTC_ANDROID) || defined(WIN32) || defined(WEBRTC_IOS)
Peter Boströme2976c82016-01-04 22:44:05 +010078#define MAYBE_OutputPathFromRootWorkingDir DISABLED_OutputPathFromRootWorkingDir
79#else
80#define MAYBE_OutputPathFromRootWorkingDir OutputPathFromRootWorkingDir
81#endif
82TEST_F(FileUtilsTest, MAYBE_OutputPathFromRootWorkingDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000083 ASSERT_EQ(0, chdir(kPathDelimiter));
84 ASSERT_EQ("./", webrtc::test::OutputPath());
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000085}
86
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000087TEST_F(FileUtilsTest, TempFilename) {
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000088 std::string temp_filename = webrtc::test::TempFilename(
89 webrtc::test::OutputPath(), "TempFilenameTest");
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000090 ASSERT_TRUE(webrtc::test::FileExists(temp_filename))
91 << "Couldn't find file: " << temp_filename;
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000092 remove(temp_filename.c_str());
93}
94
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000095// Only tests that the code executes
kthelgason5d682ca2017-01-10 03:00:41 -080096#if defined(WEBRTC_IOS)
97#define MAYBE_CreateDir DISABLED_CreateDir
98#else
99#define MAYBE_CreateDir CreateDir
100#endif
101TEST_F(FileUtilsTest, MAYBE_CreateDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000102 std::string directory = "fileutils-unittest-empty-dir";
103 // Make sure it's removed if a previous test has failed:
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +0000104 remove(directory.c_str());
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000105 ASSERT_TRUE(webrtc::test::CreateDir(directory));
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +0000106 remove(directory.c_str());
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000107}
108
109TEST_F(FileUtilsTest, WorkingDirReturnsValue) {
110 // Hard to cover all platforms. Just test that it returns something without
111 // crashing:
112 std::string working_dir = webrtc::test::WorkingDir();
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000113 ASSERT_GT(working_dir.length(), 0u);
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000114}
115
116// Due to multiple platforms, it is hard to make a complete test for
117// ResourcePath. Manual testing has been performed by removing files and
118// verified the result confirms with the specified documentation for the
119// function.
120TEST_F(FileUtilsTest, ResourcePathReturnsValue) {
121 std::string resource = webrtc::test::ResourcePath(kTestName, kExtension);
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000122 ASSERT_GT(resource.find(kTestName), 0u);
123 ASSERT_GT(resource.find(kExtension), 0u);
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000124}
125
126TEST_F(FileUtilsTest, ResourcePathFromRootWorkingDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000127 ASSERT_EQ(0, chdir(kPathDelimiter));
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000128 std::string resource = webrtc::test::ResourcePath(kTestName, kExtension);
kthelgason5d682ca2017-01-10 03:00:41 -0800129#if !defined(WEBRTC_IOS)
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000130 ASSERT_NE(resource.find("resources"), std::string::npos);
kthelgason5d682ca2017-01-10 03:00:41 -0800131#endif
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000132 ASSERT_GT(resource.find(kTestName), 0u);
133 ASSERT_GT(resource.find(kExtension), 0u);
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000134}
135
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000136TEST_F(FileUtilsTest, GetFileSizeExistingFile) {
kjellander@webrtc.orge794c362014-09-29 11:47:28 +0000137 // 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.org5b97b122011-12-08 07:42:18 +0000147}
148
149TEST_F(FileUtilsTest, GetFileSizeNonExistingFile) {
150 ASSERT_EQ(0u, webrtc::test::GetFileSize("non-existing-file.tmp"));
151}
152
alessiobe49fede2017-03-15 06:04:59 -0700153TEST_F(FileUtilsTest, DirExists) {
154 // Check that an existing directory is recognized as such.
155 ASSERT_TRUE(webrtc::test::DirExists(webrtc::test::OutputPath()))
156 << "Existing directory not found";
157
158 // Check that a non-existing directory is recognized as such.
159 std::string directory = "direxists-unittest-non_existing-dir";
160 ASSERT_FALSE(webrtc::test::DirExists(directory))
161 << "Non-existing directory found";
162
163 // Check that an existing file is not recognized as an existing directory.
164 std::string temp_filename = webrtc::test::TempFilename(
165 webrtc::test::OutputPath(), "TempFilenameTest");
166 ASSERT_TRUE(webrtc::test::FileExists(temp_filename))
167 << "Couldn't find file: " << temp_filename;
168 ASSERT_FALSE(webrtc::test::DirExists(temp_filename))
169 << "Existing file recognized as existing directory";
170 remove(temp_filename.c_str());
171}
172
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000173} // namespace webrtc