3 * Copyright 2004--2011, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "talk/base/fileutils.h"
29 #include "talk/base/gunit.h"
30 #include "talk/base/pathutils.h"
31 #include "talk/base/stream.h"
35 // Make sure we can get a temp folder for the later tests.
36 TEST(FilesystemTest, GetTemporaryFolder) {
38 EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL));
41 // Test creating a temp file, reading it back in, and deleting it.
42 TEST(FilesystemTest, TestOpenFile) {
44 EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL));
45 path.SetPathname(Filesystem::TempFilename(path, "ut"));
51 fs = Filesystem::OpenFile(path, "wb");
52 ASSERT_TRUE(fs != NULL);
53 EXPECT_EQ(SR_SUCCESS, fs->Write("test", 4, &bytes, NULL));
57 EXPECT_TRUE(Filesystem::IsFile(path));
59 fs = Filesystem::OpenFile(path, "rb");
60 ASSERT_TRUE(fs != NULL);
61 EXPECT_EQ(SR_SUCCESS, fs->Read(buf, sizeof(buf), &bytes, NULL));
65 EXPECT_TRUE(Filesystem::DeleteFile(path));
66 EXPECT_FALSE(Filesystem::IsFile(path));
69 // Test opening a non-existent file.
70 TEST(FilesystemTest, TestOpenBadFile) {
72 EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL));
73 path.SetFilename("not an actual file");
75 EXPECT_FALSE(Filesystem::IsFile(path));
77 FileStream* fs = Filesystem::OpenFile(path, "rb");
78 EXPECT_FALSE(fs != NULL);
81 // Test that CreatePrivateFile fails for existing files and succeeds for
83 TEST(FilesystemTest, TestCreatePrivateFile) {
85 EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL));
86 path.SetFilename("private_file_test");
88 // First call should succeed because the file doesn't exist yet.
89 EXPECT_TRUE(Filesystem::CreatePrivateFile(path));
90 // Next call should fail, because now it exists.
91 EXPECT_FALSE(Filesystem::CreatePrivateFile(path));
93 // Verify that we have permission to open the file for reading and writing.
94 scoped_ptr<FileStream> fs(Filesystem::OpenFile(path, "wb"));
95 EXPECT_TRUE(fs.get() != NULL);
96 // Have to close the file on Windows before it will let us delete it.
99 // Verify that we have permission to delete the file.
100 EXPECT_TRUE(Filesystem::DeleteFile(path));
103 // Test checking for free disk space.
104 TEST(FilesystemTest, TestGetDiskFreeSpace) {
105 // Note that we should avoid picking any file/folder which could be located
106 // at the remotely mounted drive/device.
108 ASSERT_TRUE(Filesystem::GetAppDataFolder(&path, true));
111 EXPECT_TRUE(Filesystem::IsFolder(path));
112 EXPECT_FALSE(Filesystem::IsFile(path));
113 EXPECT_TRUE(Filesystem::GetDiskFreeSpace(path, &free1));
117 path.AppendFolder("this_folder_doesnt_exist");
118 EXPECT_FALSE(Filesystem::IsFolder(path));
119 EXPECT_TRUE(Filesystem::IsAbsent(path));
120 EXPECT_TRUE(Filesystem::GetDiskFreeSpace(path, &free2));
121 // These should be the same disk, and disk free space should not have changed
122 // by more than 1% between the two calls.
123 EXPECT_LT(static_cast<int64>(free1 * .9), free2);
124 EXPECT_LT(free2, static_cast<int64>(free1 * 1.1));
128 EXPECT_TRUE(path.empty());
129 EXPECT_TRUE(Filesystem::GetDiskFreeSpace(path, &free3));
130 // Current working directory may not be where exe is.
131 // EXPECT_LT(static_cast<int64>(free1 * .9), free3);
132 // EXPECT_LT(free3, static_cast<int64>(free1 * 1.1));
136 // Tests that GetCurrentDirectory() returns something.
137 TEST(FilesystemTest, TestGetCurrentDirectory) {
138 EXPECT_FALSE(Filesystem::GetCurrentDirectory().empty());
141 // Tests that GetAppPathname returns something.
142 TEST(FilesystemTest, TestGetAppPathname) {
144 EXPECT_TRUE(Filesystem::GetAppPathname(&path));
145 EXPECT_FALSE(path.empty());
148 } // namespace talk_base