blob: a8e39dd98ec4aa596ec8fb7bf75998f241478076 [file] [log] [blame]
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +02001/*
2 * Copyright 2016 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
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020011#include <limits>
12#include <memory>
13#include <string>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/file.h"
16#include "rtc_base/gunit.h"
17#include "test/testsupport/fileutils.h"
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020018
19#if defined(WEBRTC_WIN)
20
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/win32.h"
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020022
23#else // if defined(WEBRTC_WIN)
24
25#include <errno.h>
26
27#endif
28
29namespace rtc {
30
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020031int LastError() {
32#if defined(WEBRTC_WIN)
33 return ::GetLastError();
34#else
35 return errno;
36#endif
37}
38
39bool VerifyBuffer(uint8_t* buffer, size_t length, uint8_t start_value) {
40 for (size_t i = 0; i < length; ++i) {
41 uint8_t val = start_value++;
42 EXPECT_EQ(val, buffer[i]);
43 if (buffer[i] != val)
44 return false;
45 }
46 // Prevent the same buffer from being verified multiple times simply
47 // because some operation that should have written to it failed
48 memset(buffer, 0, length);
49 return true;
50}
51
52class FileTest : public ::testing::Test {
53 protected:
54 std::string path_;
ehmaldonadoda8dcfb2017-01-04 07:11:23 -080055 void SetUp() override {
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020056 path_ = webrtc::test::TempFilename(webrtc::test::OutputPath(), "test_file");
57 ASSERT_FALSE(path_.empty());
58 }
ehmaldonadoda8dcfb2017-01-04 07:11:23 -080059 void TearDown() override { RemoveFile(path_); }
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020060};
61
Viktor Palmkvist1d477ea2016-10-03 13:16:02 +020062TEST_F(FileTest, DefaultConstructor) {
63 File file;
64 uint8_t buffer[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
65
66 EXPECT_FALSE(file.IsOpen());
67 EXPECT_EQ(0u, file.Write(buffer, 10));
68 EXPECT_FALSE(file.Seek(0));
69 EXPECT_EQ(0u, file.Read(buffer, 10));
70 EXPECT_EQ(0u, file.WriteAt(buffer, 10, 0));
71 EXPECT_EQ(0u, file.ReadAt(buffer, 10, 0));
72 EXPECT_FALSE(file.Close());
73}
74
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020075TEST_F(FileTest, DoubleClose) {
Viktor Palmkvist1d477ea2016-10-03 13:16:02 +020076 File file = File::Open(path_);
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020077 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
78
79 EXPECT_TRUE(file.Close());
80 EXPECT_FALSE(file.Close());
81}
82
83TEST_F(FileTest, SimpleReadWrite) {
Viktor Palmkvist1d477ea2016-10-03 13:16:02 +020084 File file = File::Open(path_);
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020085 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
86
87 uint8_t data[100] = {0};
88 uint8_t out[100] = {0};
89 for (int i = 0; i < 100; ++i) {
90 data[i] = i;
91 }
92
93 EXPECT_EQ(10u, file.Write(data, 10));
94
95 EXPECT_TRUE(file.Seek(0));
96 EXPECT_EQ(10u, file.Read(out, 10));
97 EXPECT_TRUE(VerifyBuffer(out, 10, 0));
98
99 EXPECT_TRUE(file.Seek(0));
100 EXPECT_EQ(100u, file.Write(data, 100));
101
102 EXPECT_TRUE(file.Seek(0));
103 EXPECT_EQ(100u, file.Read(out, 100));
104 EXPECT_TRUE(VerifyBuffer(out, 100, 0));
105
106 EXPECT_TRUE(file.Seek(1));
107 EXPECT_EQ(50u, file.Write(data, 50));
108 EXPECT_EQ(50u, file.Write(data + 50, 50));
109
110 EXPECT_TRUE(file.Seek(1));
111 EXPECT_EQ(100u, file.Read(out, 100));
112 EXPECT_TRUE(VerifyBuffer(out, 100, 0));
113}
114
115TEST_F(FileTest, ReadWriteClose) {
Viktor Palmkvist1d477ea2016-10-03 13:16:02 +0200116 File file = File::Open(path_);
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +0200117 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
118
119 uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
120 uint8_t out[10] = {0};
121 EXPECT_EQ(10u, file.Write(data, 10));
122 EXPECT_TRUE(file.Close());
123
Viktor Palmkvist1d477ea2016-10-03 13:16:02 +0200124 File file2 = File::Open(path_);
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +0200125 ASSERT_TRUE(file2.IsOpen()) << "Error: " << LastError();
126 EXPECT_EQ(10u, file2.Read(out, 10));
127 EXPECT_TRUE(VerifyBuffer(out, 10, 0));
128}
129
130TEST_F(FileTest, RandomAccessRead) {
Viktor Palmkvist1d477ea2016-10-03 13:16:02 +0200131 File file = File::Open(path_);
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +0200132 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
133
134 uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
135 uint8_t out[10] = {0};
136 EXPECT_EQ(10u, file.Write(data, 10));
137
138 EXPECT_EQ(4u, file.ReadAt(out, 4, 0));
139 EXPECT_TRUE(VerifyBuffer(out, 4, 0));
140
141 EXPECT_EQ(4u, file.ReadAt(out, 4, 4));
142 EXPECT_TRUE(VerifyBuffer(out, 4, 4));
143
144 EXPECT_EQ(5u, file.ReadAt(out, 5, 5));
145 EXPECT_TRUE(VerifyBuffer(out, 5, 5));
146}
147
148TEST_F(FileTest, RandomAccessReadWrite) {
Viktor Palmkvist1d477ea2016-10-03 13:16:02 +0200149 File file = File::Open(path_);
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +0200150 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
151
152 uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
153 uint8_t out[10] = {0};
154 EXPECT_EQ(10u, file.Write(data, 10));
155 EXPECT_TRUE(file.Seek(4));
156
157 EXPECT_EQ(4u, file.WriteAt(data, 4, 4));
158 EXPECT_EQ(4u, file.ReadAt(out, 4, 4));
159 EXPECT_TRUE(VerifyBuffer(out, 4, 0));
160
161 EXPECT_EQ(2u, file.WriteAt(data, 2, 8));
162 EXPECT_EQ(2u, file.ReadAt(out, 2, 8));
163 EXPECT_TRUE(VerifyBuffer(out, 2, 0));
164}
165
zijiehedd87d582016-12-06 15:04:02 -0800166TEST_F(FileTest, OpenFromPathname) {
167 {
168 File file = File::Open(Pathname(path_));
169 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
170 }
171
172 {
173 Pathname path(path_);
174 File file = File::Open(path);
175 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
176 }
177}
178
179TEST_F(FileTest, CreateFromPathname) {
180 {
181 File file = File::Create(Pathname(path_));
182 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
183 }
184
185 {
186 Pathname path(path_);
187 File file = File::Create(path);
188 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
189 }
190}
191
zijiehe2769ec62016-12-14 15:03:03 -0800192TEST_F(FileTest, ShouldBeAbleToRemoveFile) {
193 {
194 File file = File::Open(Pathname(path_));
195 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
196 }
197
198 ASSERT_TRUE(File::Remove(Pathname(path_))) << "Error: " << LastError();
199}
200
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +0200201} // namespace rtc