henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004--2011, Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 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. |
| 15 | * |
| 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. |
| 26 | */ |
| 27 | |
| 28 | #include "talk/base/fileutils.h" |
| 29 | #include "talk/base/gunit.h" |
| 30 | #include "talk/base/pathutils.h" |
| 31 | #include "talk/base/stream.h" |
| 32 | |
| 33 | namespace talk_base { |
| 34 | |
| 35 | // Make sure we can get a temp folder for the later tests. |
| 36 | TEST(FilesystemTest, GetTemporaryFolder) { |
| 37 | Pathname path; |
| 38 | EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL)); |
| 39 | } |
| 40 | |
| 41 | // Test creating a temp file, reading it back in, and deleting it. |
| 42 | TEST(FilesystemTest, TestOpenFile) { |
| 43 | Pathname path; |
| 44 | EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL)); |
| 45 | path.SetPathname(Filesystem::TempFilename(path, "ut")); |
| 46 | |
| 47 | FileStream* fs; |
| 48 | char buf[256]; |
| 49 | size_t bytes; |
| 50 | |
| 51 | fs = Filesystem::OpenFile(path, "wb"); |
| 52 | ASSERT_TRUE(fs != NULL); |
| 53 | EXPECT_EQ(SR_SUCCESS, fs->Write("test", 4, &bytes, NULL)); |
| 54 | EXPECT_EQ(4U, bytes); |
| 55 | delete fs; |
| 56 | |
| 57 | EXPECT_TRUE(Filesystem::IsFile(path)); |
| 58 | |
| 59 | fs = Filesystem::OpenFile(path, "rb"); |
| 60 | ASSERT_TRUE(fs != NULL); |
| 61 | EXPECT_EQ(SR_SUCCESS, fs->Read(buf, sizeof(buf), &bytes, NULL)); |
| 62 | EXPECT_EQ(4U, bytes); |
| 63 | delete fs; |
| 64 | |
| 65 | EXPECT_TRUE(Filesystem::DeleteFile(path)); |
| 66 | EXPECT_FALSE(Filesystem::IsFile(path)); |
| 67 | } |
| 68 | |
| 69 | // Test opening a non-existent file. |
| 70 | TEST(FilesystemTest, TestOpenBadFile) { |
| 71 | Pathname path; |
| 72 | EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL)); |
| 73 | path.SetFilename("not an actual file"); |
| 74 | |
| 75 | EXPECT_FALSE(Filesystem::IsFile(path)); |
| 76 | |
| 77 | FileStream* fs = Filesystem::OpenFile(path, "rb"); |
| 78 | EXPECT_FALSE(fs != NULL); |
| 79 | } |
| 80 | |
| 81 | // Test that CreatePrivateFile fails for existing files and succeeds for |
| 82 | // non-existent ones. |
| 83 | TEST(FilesystemTest, TestCreatePrivateFile) { |
| 84 | Pathname path; |
| 85 | EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL)); |
| 86 | path.SetFilename("private_file_test"); |
| 87 | |
| 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)); |
| 92 | |
| 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. |
| 97 | fs.reset(); |
| 98 | |
| 99 | // Verify that we have permission to delete the file. |
| 100 | EXPECT_TRUE(Filesystem::DeleteFile(path)); |
| 101 | } |
| 102 | |
| 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. |
| 107 | Pathname path; |
| 108 | ASSERT_TRUE(Filesystem::GetAppDataFolder(&path, true)); |
| 109 | |
| 110 | int64 free1 = 0; |
| 111 | EXPECT_TRUE(Filesystem::IsFolder(path)); |
| 112 | EXPECT_FALSE(Filesystem::IsFile(path)); |
| 113 | EXPECT_TRUE(Filesystem::GetDiskFreeSpace(path, &free1)); |
| 114 | EXPECT_GT(free1, 0); |
| 115 | |
| 116 | int64 free2 = 0; |
| 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)); |
| 125 | |
| 126 | int64 free3 = 0; |
| 127 | path.clear(); |
| 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)); |
| 133 | EXPECT_GT(free3, 0); |
| 134 | } |
| 135 | |
| 136 | // Tests that GetCurrentDirectory() returns something. |
| 137 | TEST(FilesystemTest, TestGetCurrentDirectory) { |
| 138 | EXPECT_FALSE(Filesystem::GetCurrentDirectory().empty()); |
| 139 | } |
| 140 | |
| 141 | // Tests that GetAppPathname returns something. |
| 142 | TEST(FilesystemTest, TestGetAppPathname) { |
| 143 | Pathname path; |
| 144 | EXPECT_TRUE(Filesystem::GetAppPathname(&path)); |
| 145 | EXPECT_FALSE(path.empty()); |
| 146 | } |
| 147 | |
| 148 | } // namespace talk_base |