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