blob: e9999040f21b352366daf534cc77069aaf7a376b [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
jbauch555604a2016-04-26 03:13:22 -070011#include <memory>
12
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013#include "webrtc/base/fileutils.h"
14#include "webrtc/base/gunit.h"
15#include "webrtc/base/pathutils.h"
16#include "webrtc/base/stream.h"
17
18namespace rtc {
19
phoglundbb738732016-07-15 03:57:12 -070020#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.orgf0488722014-05-13 18:00:26 +000027// Make sure we can get a temp folder for the later tests.
phoglundbb738732016-07-15 03:57:12 -070028TEST(MAYBE_FilesystemTest, GetTemporaryFolder) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000029 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.
phoglundbb738732016-07-15 03:57:12 -070034TEST(MAYBE_FilesystemTest, TestOpenFile) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035 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.
phoglundbb738732016-07-15 03:57:12 -070062TEST(MAYBE_FilesystemTest, TestOpenBadFile) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063 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
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073// Test checking for free disk space.
phoglundbb738732016-07-15 03:57:12 -070074TEST(MAYBE_FilesystemTest, TestGetDiskFreeSpace) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075 // Note that we should avoid picking any file/folder which could be located
76 // at the remotely mounted drive/device.
77 Pathname path;
78 ASSERT_TRUE(Filesystem::GetAppDataFolder(&path, true));
79
Peter Boström0c4e06b2015-10-07 12:23:21 +020080 int64_t free1 = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000081 EXPECT_TRUE(Filesystem::IsFolder(path));
82 EXPECT_FALSE(Filesystem::IsFile(path));
83 EXPECT_TRUE(Filesystem::GetDiskFreeSpace(path, &free1));
84 EXPECT_GT(free1, 0);
85
Peter Boström0c4e06b2015-10-07 12:23:21 +020086 int64_t free2 = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000087 path.AppendFolder("this_folder_doesnt_exist");
88 EXPECT_FALSE(Filesystem::IsFolder(path));
89 EXPECT_TRUE(Filesystem::IsAbsent(path));
90 EXPECT_TRUE(Filesystem::GetDiskFreeSpace(path, &free2));
91 // These should be the same disk, and disk free space should not have changed
92 // by more than 1% between the two calls.
Peter Boström0c4e06b2015-10-07 12:23:21 +020093 EXPECT_LT(static_cast<int64_t>(free1 * .9), free2);
94 EXPECT_LT(free2, static_cast<int64_t>(free1 * 1.1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000095
Peter Boström0c4e06b2015-10-07 12:23:21 +020096 int64_t free3 = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000097 path.clear();
98 EXPECT_TRUE(path.empty());
99 EXPECT_TRUE(Filesystem::GetDiskFreeSpace(path, &free3));
100 // Current working directory may not be where exe is.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200101 // EXPECT_LT(static_cast<int64_t>(free1 * .9), free3);
102 // EXPECT_LT(free3, static_cast<int64_t>(free1 * 1.1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000103 EXPECT_GT(free3, 0);
104}
105
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000106} // namespace rtc