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