OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2015 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 <sys/types.h> | |
12 #include <sys/stat.h> | |
13 #include <fcntl.h> | |
14 | |
15 #include <limits> | |
16 #include <memory> | |
17 #include <string> | |
18 | |
19 #include "webrtc/base/gunit.h" | |
20 #include "webrtc/base/file.h" | |
21 #include "webrtc/test/testsupport/fileutils.h" | |
22 | |
23 #if defined(WEBRTC_WIN) | |
24 #include <windows.h> | |
25 | |
26 #include <type_traits> | |
27 | |
28 #include "webrtc/system_wrappers/include/utf_util_win.h" | |
29 #include "webrtc/base/checks.h" | |
30 | |
31 #else | |
sprang_webrtc
2016/08/15 09:43:07
I usually like
#else // if defined(WEBRTC_WIN)
f
palmkvist
2016/08/16 08:55:35
Done.
| |
32 | |
33 #include <errno.h> | |
34 | |
35 #endif | |
36 | |
37 namespace rtc { | |
38 | |
39 rtc::PlatformFile OpenFile(const std::string& path) { | |
40 #if defined(WEBRTC_WIN) | |
41 HANDLE handle = | |
42 ::CreateFile(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0, | |
43 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); | |
44 return handle; | |
45 #else | |
46 int fd = ::open(path.c_str(), O_RDWR); | |
47 return fd; | |
48 #endif | |
49 } | |
50 | |
51 void RemoveFile(const std::string& path) { | |
52 #if defined(WEBRTC_WIN) | |
53 ::DeleteFile(ToUtf16(path).c_str()); | |
54 #else | |
55 ::unlink(path.c_str()); | |
56 #endif | |
57 } | |
58 | |
59 bool VerifyBuffer(uint8_t* buffer, size_t length, uint8_t start_value) { | |
60 for (size_t i = 0; i < length; ++i) { | |
61 uint8_t val = start_value++; | |
62 EXPECT_EQ(val, buffer[i]); | |
63 if (buffer[i] != val) | |
64 return false; | |
65 } | |
66 // Prevent the same buffer from being verified multiple times simply | |
67 // because some operation that should have written to it failed | |
68 memset(buffer, 0, length); | |
69 return true; | |
70 } | |
71 | |
72 class FileTest : public ::testing::Test { | |
73 protected: | |
74 std::string path_; | |
75 void SetUp() { | |
76 path_ = webrtc::test::TempFilename(webrtc::test::OutputPath(), "test_file"); | |
77 ASSERT_FALSE(path_.empty()); | |
78 } | |
79 rtc::PlatformFile OpenTempFile() { return OpenFile(path_); } | |
80 void TearDown() { RemoveFile(path_); } | |
81 }; | |
82 | |
83 TEST_F(FileTest, SimpleReadWrite) { | |
84 rtc::PlatformFile p_file = OpenTempFile(); | |
85 #if defined(WEBRTC_WIN) | |
86 ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) | |
87 << "Error code: " << ::GetLastError() << " Path: " << path_; | |
88 #else | |
89 ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Errno: " << errno | |
90 << " Path: " << path_; | |
91 #endif | |
92 File file(p_file); | |
93 | |
94 uint8_t data[100] = {0}; | |
95 uint8_t out[100] = {0}; | |
96 for (int i = 0; i < 100; ++i) { | |
97 data[i] = i; | |
98 } | |
99 | |
100 EXPECT_EQ(10u, file.Write(data, 10)); | |
101 | |
102 EXPECT_TRUE(file.Seek(0)); | |
103 EXPECT_EQ(10u, file.Read(out, 10)); | |
104 EXPECT_TRUE(VerifyBuffer(out, 10, 0)); | |
105 | |
106 EXPECT_TRUE(file.Seek(0)); | |
107 EXPECT_EQ(100u, file.Write(data, 100)); | |
108 | |
109 EXPECT_TRUE(file.Seek(0)); | |
110 EXPECT_EQ(100u, file.Read(out, 100)); | |
111 EXPECT_TRUE(VerifyBuffer(out, 100, 0)); | |
112 | |
113 EXPECT_TRUE(file.Seek(1)); | |
114 EXPECT_EQ(50u, file.Write(data, 50)); | |
115 EXPECT_EQ(50u, file.Write(data + 50, 50)); | |
116 | |
117 EXPECT_TRUE(file.Seek(1)); | |
118 EXPECT_EQ(100u, file.Read(out, 100)); | |
119 EXPECT_TRUE(VerifyBuffer(out, 100, 0)); | |
120 } | |
121 | |
122 TEST_F(FileTest, ReadWriteClose) { | |
123 rtc::PlatformFile p_file = OpenTempFile(); | |
124 #if defined(WEBRTC_WIN) | |
125 ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Error code: " | |
126 << ::GetLastError(); | |
127 #else | |
128 ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Errno: " << errno; | |
129 #endif | |
sprang_webrtc
2016/08/15 09:43:08
Looks like a LastError() method in FileTest could
| |
130 File file(p_file); | |
131 | |
132 uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | |
133 uint8_t out[10] = {0}; | |
134 EXPECT_EQ(10u, file.Write(data, 10)); | |
135 EXPECT_TRUE(file.Close()); | |
136 | |
137 p_file = OpenTempFile(); | |
138 #if defined(WEBRTC_WIN) | |
139 ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Error code: " | |
140 << ::GetLastError(); | |
141 #else | |
142 ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Errno: " << errno; | |
143 #endif | |
144 File file2(p_file); | |
145 EXPECT_EQ(10u, file2.Read(out, 10)); | |
146 EXPECT_TRUE(VerifyBuffer(out, 10, 0)); | |
147 } | |
148 | |
149 TEST_F(FileTest, RandomAccessRead) { | |
150 rtc::PlatformFile p_file = OpenTempFile(); | |
151 #if defined(WEBRTC_WIN) | |
152 ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Error code: " | |
153 << ::GetLastError(); | |
154 #else | |
155 ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Errno: " << errno; | |
156 #endif | |
157 File file(p_file); | |
158 | |
159 uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | |
160 uint8_t out[10] = {0}; | |
161 EXPECT_EQ(10u, file.Write(data, 10)); | |
162 | |
163 EXPECT_EQ(4u, file.ReadAt(out, 4, 0)); | |
164 EXPECT_TRUE(VerifyBuffer(out, 4, 0)); | |
165 | |
166 EXPECT_EQ(4u, file.ReadAt(out, 4, 4)); | |
167 EXPECT_TRUE(VerifyBuffer(out, 4, 4)); | |
168 | |
169 EXPECT_EQ(5u, file.ReadAt(out, 5, 5)); | |
170 EXPECT_TRUE(VerifyBuffer(out, 5, 5)); | |
171 } | |
172 | |
173 TEST_F(FileTest, RandomAccessReadWrite) { | |
174 rtc::PlatformFile p_file = OpenTempFile(); | |
175 #if defined(WEBRTC_WIN) | |
176 ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Error code: " | |
177 << ::GetLastError(); | |
178 #else | |
179 ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Errno: " << errno; | |
180 #endif | |
181 File file(p_file); | |
182 | |
183 uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | |
184 uint8_t out[10] = {0}; | |
185 EXPECT_EQ(10u, file.Write(data, 10)); | |
186 EXPECT_TRUE(file.Seek(4)); | |
187 | |
188 EXPECT_EQ(4u, file.WriteAt(data, 4, 4)); | |
189 EXPECT_EQ(4u, file.Read(out, 4)); | |
190 EXPECT_TRUE(VerifyBuffer(out, 4, 0)); | |
191 | |
192 EXPECT_EQ(2u, file.WriteAt(data, 2, 8)); | |
193 EXPECT_EQ(2u, file.ReadAt(out, 2, 8)); | |
194 EXPECT_TRUE(VerifyBuffer(out, 2, 0)); | |
195 } | |
196 | |
197 TEST_F(FileTest, RandomAccessDoesNotMovePosition) { | |
198 rtc::PlatformFile p_file = OpenTempFile(); | |
199 #if defined(WEBRTC_WIN) | |
200 ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Error code: " | |
201 << ::GetLastError(); | |
202 #else | |
203 ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Errno: " << errno; | |
204 #endif | |
205 File file(p_file); | |
206 | |
207 uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | |
208 uint8_t out[10] = {0}; | |
209 EXPECT_EQ(10u, file.Write(data, 10)); | |
210 | |
211 EXPECT_TRUE(file.Seek(0)); | |
212 EXPECT_EQ(2u, file.ReadAt(out, 2, 5)); | |
213 EXPECT_EQ(4u, file.Read(out, 4)); | |
214 EXPECT_TRUE(VerifyBuffer(out, 4, 0)); | |
215 | |
216 EXPECT_EQ(6u, file.WriteAt(data + 1, 6, 0)); | |
217 EXPECT_EQ(2u, file.Read(out, 2)); | |
218 EXPECT_TRUE(VerifyBuffer(out, 2, 5)); | |
219 | |
220 EXPECT_EQ(4u, file.WriteAt(data, 4, 6)); | |
221 EXPECT_EQ(4u, file.Read(out, 4)); | |
222 EXPECT_TRUE(VerifyBuffer(out, 4, 0)); | |
223 } | |
224 | |
225 } // namespace rtc | |
OLD | NEW |