henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 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 | */ |
| 10 | |
jbauch | 1286d0e | 2016-04-26 10:13:22 | [diff] [blame^] | 11 | #include <memory> |
| 12 | |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 13 | #include "webrtc/base/fileutils.h" |
| 14 | #include "webrtc/base/gunit.h" |
| 15 | #include "webrtc/base/pathutils.h" |
| 16 | #include "webrtc/base/stream.h" |
| 17 | |
| 18 | namespace rtc { |
| 19 | |
| 20 | // Make sure we can get a temp folder for the later tests. |
| 21 | TEST(FilesystemTest, GetTemporaryFolder) { |
| 22 | Pathname path; |
| 23 | EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL)); |
| 24 | } |
| 25 | |
| 26 | // Test creating a temp file, reading it back in, and deleting it. |
| 27 | TEST(FilesystemTest, TestOpenFile) { |
| 28 | Pathname path; |
| 29 | EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL)); |
| 30 | path.SetPathname(Filesystem::TempFilename(path, "ut")); |
| 31 | |
| 32 | FileStream* fs; |
| 33 | char buf[256]; |
| 34 | size_t bytes; |
| 35 | |
| 36 | fs = Filesystem::OpenFile(path, "wb"); |
| 37 | ASSERT_TRUE(fs != NULL); |
| 38 | EXPECT_EQ(SR_SUCCESS, fs->Write("test", 4, &bytes, NULL)); |
| 39 | EXPECT_EQ(4U, bytes); |
| 40 | delete fs; |
| 41 | |
| 42 | EXPECT_TRUE(Filesystem::IsFile(path)); |
| 43 | |
| 44 | fs = Filesystem::OpenFile(path, "rb"); |
| 45 | ASSERT_TRUE(fs != NULL); |
| 46 | EXPECT_EQ(SR_SUCCESS, fs->Read(buf, sizeof(buf), &bytes, NULL)); |
| 47 | EXPECT_EQ(4U, bytes); |
| 48 | delete fs; |
| 49 | |
| 50 | EXPECT_TRUE(Filesystem::DeleteFile(path)); |
| 51 | EXPECT_FALSE(Filesystem::IsFile(path)); |
| 52 | } |
| 53 | |
| 54 | // Test opening a non-existent file. |
| 55 | TEST(FilesystemTest, TestOpenBadFile) { |
| 56 | Pathname path; |
| 57 | EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL)); |
| 58 | path.SetFilename("not an actual file"); |
| 59 | |
| 60 | EXPECT_FALSE(Filesystem::IsFile(path)); |
| 61 | |
| 62 | FileStream* fs = Filesystem::OpenFile(path, "rb"); |
| 63 | EXPECT_FALSE(fs != NULL); |
| 64 | } |
| 65 | |
| 66 | // Test that CreatePrivateFile fails for existing files and succeeds for |
| 67 | // non-existent ones. |
| 68 | TEST(FilesystemTest, TestCreatePrivateFile) { |
| 69 | Pathname path; |
| 70 | EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL)); |
| 71 | path.SetFilename("private_file_test"); |
| 72 | |
| 73 | // First call should succeed because the file doesn't exist yet. |
| 74 | EXPECT_TRUE(Filesystem::CreatePrivateFile(path)); |
| 75 | // Next call should fail, because now it exists. |
| 76 | EXPECT_FALSE(Filesystem::CreatePrivateFile(path)); |
| 77 | |
| 78 | // Verify that we have permission to open the file for reading and writing. |
jbauch | 1286d0e | 2016-04-26 10:13:22 | [diff] [blame^] | 79 | std::unique_ptr<FileStream> fs(Filesystem::OpenFile(path, "wb")); |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 80 | EXPECT_TRUE(fs.get() != NULL); |
| 81 | // Have to close the file on Windows before it will let us delete it. |
| 82 | fs.reset(); |
| 83 | |
| 84 | // Verify that we have permission to delete the file. |
| 85 | EXPECT_TRUE(Filesystem::DeleteFile(path)); |
| 86 | } |
| 87 | |
| 88 | // Test checking for free disk space. |
| 89 | TEST(FilesystemTest, TestGetDiskFreeSpace) { |
| 90 | // Note that we should avoid picking any file/folder which could be located |
| 91 | // at the remotely mounted drive/device. |
| 92 | Pathname path; |
| 93 | ASSERT_TRUE(Filesystem::GetAppDataFolder(&path, true)); |
| 94 | |
Peter Boström | 07e22e6 | 2015-10-07 10:23:21 | [diff] [blame] | 95 | int64_t free1 = 0; |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 96 | EXPECT_TRUE(Filesystem::IsFolder(path)); |
| 97 | EXPECT_FALSE(Filesystem::IsFile(path)); |
| 98 | EXPECT_TRUE(Filesystem::GetDiskFreeSpace(path, &free1)); |
| 99 | EXPECT_GT(free1, 0); |
| 100 | |
Peter Boström | 07e22e6 | 2015-10-07 10:23:21 | [diff] [blame] | 101 | int64_t free2 = 0; |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 102 | path.AppendFolder("this_folder_doesnt_exist"); |
| 103 | EXPECT_FALSE(Filesystem::IsFolder(path)); |
| 104 | EXPECT_TRUE(Filesystem::IsAbsent(path)); |
| 105 | EXPECT_TRUE(Filesystem::GetDiskFreeSpace(path, &free2)); |
| 106 | // These should be the same disk, and disk free space should not have changed |
| 107 | // by more than 1% between the two calls. |
Peter Boström | 07e22e6 | 2015-10-07 10:23:21 | [diff] [blame] | 108 | EXPECT_LT(static_cast<int64_t>(free1 * .9), free2); |
| 109 | EXPECT_LT(free2, static_cast<int64_t>(free1 * 1.1)); |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 110 | |
Peter Boström | 07e22e6 | 2015-10-07 10:23:21 | [diff] [blame] | 111 | int64_t free3 = 0; |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 112 | path.clear(); |
| 113 | EXPECT_TRUE(path.empty()); |
| 114 | EXPECT_TRUE(Filesystem::GetDiskFreeSpace(path, &free3)); |
| 115 | // Current working directory may not be where exe is. |
Peter Boström | 07e22e6 | 2015-10-07 10:23:21 | [diff] [blame] | 116 | // EXPECT_LT(static_cast<int64_t>(free1 * .9), free3); |
| 117 | // EXPECT_LT(free3, static_cast<int64_t>(free1 * 1.1)); |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 118 | EXPECT_GT(free3, 0); |
| 119 | } |
| 120 | |
| 121 | // Tests that GetCurrentDirectory() returns something. |
| 122 | TEST(FilesystemTest, TestGetCurrentDirectory) { |
| 123 | EXPECT_FALSE(Filesystem::GetCurrentDirectory().empty()); |
| 124 | } |
| 125 | |
| 126 | // Tests that GetAppPathname returns something. |
| 127 | TEST(FilesystemTest, TestGetAppPathname) { |
| 128 | Pathname path; |
| 129 | EXPECT_TRUE(Filesystem::GetAppPathname(&path)); |
| 130 | EXPECT_FALSE(path.empty()); |
| 131 | } |
| 132 | |
| 133 | } // namespace rtc |