forked from JumpingYang001/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_unittest.cc
201 lines (159 loc) · 5.02 KB
/
file_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Copyright 2016 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include <limits>
#include <memory>
#include <string>
#include "rtc_base/file.h"
#include "rtc_base/gunit.h"
#include "test/testsupport/fileutils.h"
#if defined(WEBRTC_WIN)
#include "rtc_base/win32.h"
#else // if defined(WEBRTC_WIN)
#include <errno.h>
#endif
namespace rtc {
int LastError() {
#if defined(WEBRTC_WIN)
return ::GetLastError();
#else
return errno;
#endif
}
bool VerifyBuffer(uint8_t* buffer, size_t length, uint8_t start_value) {
for (size_t i = 0; i < length; ++i) {
uint8_t val = start_value++;
EXPECT_EQ(val, buffer[i]);
if (buffer[i] != val)
return false;
}
// Prevent the same buffer from being verified multiple times simply
// because some operation that should have written to it failed
memset(buffer, 0, length);
return true;
}
class FileTest : public ::testing::Test {
protected:
std::string path_;
void SetUp() override {
path_ = webrtc::test::TempFilename(webrtc::test::OutputPath(), "test_file");
ASSERT_FALSE(path_.empty());
}
void TearDown() override { RemoveFile(path_); }
};
TEST_F(FileTest, DefaultConstructor) {
File file;
uint8_t buffer[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
EXPECT_FALSE(file.IsOpen());
EXPECT_EQ(0u, file.Write(buffer, 10));
EXPECT_FALSE(file.Seek(0));
EXPECT_EQ(0u, file.Read(buffer, 10));
EXPECT_EQ(0u, file.WriteAt(buffer, 10, 0));
EXPECT_EQ(0u, file.ReadAt(buffer, 10, 0));
EXPECT_FALSE(file.Close());
}
TEST_F(FileTest, DoubleClose) {
File file = File::Open(path_);
ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
EXPECT_TRUE(file.Close());
EXPECT_FALSE(file.Close());
}
TEST_F(FileTest, SimpleReadWrite) {
File file = File::Open(path_);
ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
uint8_t data[100] = {0};
uint8_t out[100] = {0};
for (int i = 0; i < 100; ++i) {
data[i] = i;
}
EXPECT_EQ(10u, file.Write(data, 10));
EXPECT_TRUE(file.Seek(0));
EXPECT_EQ(10u, file.Read(out, 10));
EXPECT_TRUE(VerifyBuffer(out, 10, 0));
EXPECT_TRUE(file.Seek(0));
EXPECT_EQ(100u, file.Write(data, 100));
EXPECT_TRUE(file.Seek(0));
EXPECT_EQ(100u, file.Read(out, 100));
EXPECT_TRUE(VerifyBuffer(out, 100, 0));
EXPECT_TRUE(file.Seek(1));
EXPECT_EQ(50u, file.Write(data, 50));
EXPECT_EQ(50u, file.Write(data + 50, 50));
EXPECT_TRUE(file.Seek(1));
EXPECT_EQ(100u, file.Read(out, 100));
EXPECT_TRUE(VerifyBuffer(out, 100, 0));
}
TEST_F(FileTest, ReadWriteClose) {
File file = File::Open(path_);
ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
uint8_t out[10] = {0};
EXPECT_EQ(10u, file.Write(data, 10));
EXPECT_TRUE(file.Close());
File file2 = File::Open(path_);
ASSERT_TRUE(file2.IsOpen()) << "Error: " << LastError();
EXPECT_EQ(10u, file2.Read(out, 10));
EXPECT_TRUE(VerifyBuffer(out, 10, 0));
}
TEST_F(FileTest, RandomAccessRead) {
File file = File::Open(path_);
ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
uint8_t out[10] = {0};
EXPECT_EQ(10u, file.Write(data, 10));
EXPECT_EQ(4u, file.ReadAt(out, 4, 0));
EXPECT_TRUE(VerifyBuffer(out, 4, 0));
EXPECT_EQ(4u, file.ReadAt(out, 4, 4));
EXPECT_TRUE(VerifyBuffer(out, 4, 4));
EXPECT_EQ(5u, file.ReadAt(out, 5, 5));
EXPECT_TRUE(VerifyBuffer(out, 5, 5));
}
TEST_F(FileTest, RandomAccessReadWrite) {
File file = File::Open(path_);
ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
uint8_t out[10] = {0};
EXPECT_EQ(10u, file.Write(data, 10));
EXPECT_TRUE(file.Seek(4));
EXPECT_EQ(4u, file.WriteAt(data, 4, 4));
EXPECT_EQ(4u, file.ReadAt(out, 4, 4));
EXPECT_TRUE(VerifyBuffer(out, 4, 0));
EXPECT_EQ(2u, file.WriteAt(data, 2, 8));
EXPECT_EQ(2u, file.ReadAt(out, 2, 8));
EXPECT_TRUE(VerifyBuffer(out, 2, 0));
}
TEST_F(FileTest, OpenFromPathname) {
{
File file = File::Open(Pathname(path_));
ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
}
{
Pathname path(path_);
File file = File::Open(path);
ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
}
}
TEST_F(FileTest, CreateFromPathname) {
{
File file = File::Create(Pathname(path_));
ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
}
{
Pathname path(path_);
File file = File::Create(path);
ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
}
}
TEST_F(FileTest, ShouldBeAbleToRemoveFile) {
{
File file = File::Open(Pathname(path_));
ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
}
ASSERT_TRUE(File::Remove(Pathname(path_))) << "Error: " << LastError();
}
} // namespace rtc