Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(564)

Side by Side Diff: webrtc/base/file_unittest.cc

Issue 2877023002: Move webrtc/{base => rtc_base} (Closed)
Patch Set: update presubmit.py and DEPS include rules Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/file_posix.cc ('k') | webrtc/base/file_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
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
11 #include <limits>
12 #include <memory>
13 #include <string>
14
15 #include "webrtc/base/gunit.h"
16 #include "webrtc/base/file.h"
17 #include "webrtc/test/testsupport/fileutils.h"
18
19 #if defined(WEBRTC_WIN)
20
21 #include "webrtc/base/win32.h"
22
23 #else // if defined(WEBRTC_WIN)
24
25 #include <errno.h>
26
27 #endif
28
29 namespace rtc {
30
31 int LastError() {
32 #if defined(WEBRTC_WIN)
33 return ::GetLastError();
34 #else
35 return errno;
36 #endif
37 }
38
39 bool 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
52 class FileTest : public ::testing::Test {
53 protected:
54 std::string path_;
55 void SetUp() override {
56 path_ = webrtc::test::TempFilename(webrtc::test::OutputPath(), "test_file");
57 ASSERT_FALSE(path_.empty());
58 }
59 void TearDown() override { RemoveFile(path_); }
60 };
61
62 TEST_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
75 TEST_F(FileTest, DoubleClose) {
76 File file = File::Open(path_);
77 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
78
79 EXPECT_TRUE(file.Close());
80 EXPECT_FALSE(file.Close());
81 }
82
83 TEST_F(FileTest, SimpleReadWrite) {
84 File file = File::Open(path_);
85 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
115 TEST_F(FileTest, ReadWriteClose) {
116 File file = File::Open(path_);
117 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
124 File file2 = File::Open(path_);
125 ASSERT_TRUE(file2.IsOpen()) << "Error: " << LastError();
126 EXPECT_EQ(10u, file2.Read(out, 10));
127 EXPECT_TRUE(VerifyBuffer(out, 10, 0));
128 }
129
130 TEST_F(FileTest, RandomAccessRead) {
131 File file = File::Open(path_);
132 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
148 TEST_F(FileTest, RandomAccessReadWrite) {
149 File file = File::Open(path_);
150 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
166 TEST_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
179 TEST_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
192 TEST_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
201 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/file_posix.cc ('k') | webrtc/base/file_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698