blob: 068d60e3ac4940d6b91d48bc1e46572e1397c657 [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} // namespace rtc