OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/files/scoped_temp_dir.h" | 6 #include "base/files/scoped_temp_dir.h" |
7 #include "base/platform_file.h" | 7 #include "base/platform_file.h" |
8 #include "base/time.h" | 8 #include "base/time.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
204 EXPECT_EQ(data_to_write[i], data_read_2[i]); | 204 EXPECT_EQ(data_to_write[i], data_read_2[i]); |
205 for (int i = kTestDataSize; i < kOffsetBeyondEndOfFile; i++) | 205 for (int i = kTestDataSize; i < kOffsetBeyondEndOfFile; i++) |
206 EXPECT_EQ(0, data_read_2[i]); | 206 EXPECT_EQ(0, data_read_2[i]); |
207 for (int i = kOffsetBeyondEndOfFile; i < file_size; i++) | 207 for (int i = kOffsetBeyondEndOfFile; i < file_size; i++) |
208 EXPECT_EQ(data_to_write[i - kOffsetBeyondEndOfFile], data_read_2[i]); | 208 EXPECT_EQ(data_to_write[i - kOffsetBeyondEndOfFile], data_read_2[i]); |
209 | 209 |
210 // Close the file handle to allow the temp directory to be deleted. | 210 // Close the file handle to allow the temp directory to be deleted. |
211 base::ClosePlatformFile(file); | 211 base::ClosePlatformFile(file); |
212 } | 212 } |
213 | 213 |
214 TEST(PlatformFile, AppendPlatformFile) { | |
215 base::ScopedTempDir temp_dir; | |
216 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
217 FilePath file_path = temp_dir.path().AppendASCII("append_file"); | |
218 base::PlatformFile file = base::CreatePlatformFile( | |
219 file_path, | |
220 base::PLATFORM_FILE_CREATE | | |
221 base::PLATFORM_FILE_APPEND, | |
jar (doing other things)
2013/06/04 23:31:50
nit: This should fit nicely (and more readably) on
| |
222 NULL, NULL); | |
223 EXPECT_NE(base::kInvalidPlatformFileValue, file); | |
224 | |
225 char data_to_write[] = "test"; | |
226 const int kTestDataSize = 4; | |
227 | |
228 // Write 0 bytes to the file. | |
229 int bytes_written = WriteFully(file, 0, data_to_write, 0); | |
230 EXPECT_EQ(0, bytes_written); | |
231 | |
232 // Write "test" to the file. | |
233 bytes_written = WriteFully(file, 0, data_to_write, kTestDataSize); | |
234 EXPECT_EQ(kTestDataSize, bytes_written); | |
235 | |
236 base::ClosePlatformFile(file); | |
237 file = base::CreatePlatformFile( | |
238 file_path, | |
239 base::PLATFORM_FILE_OPEN | | |
240 base::PLATFORM_FILE_READ | | |
241 base::PLATFORM_FILE_APPEND, | |
242 NULL, NULL); | |
jar (doing other things)
2013/06/04 23:31:50
nit: I can see this odd style used elsewhere in th
| |
243 EXPECT_NE(base::kInvalidPlatformFileValue, file); | |
244 if (file == base::kInvalidPlatformFileValue) { | |
245 printf("errno: %d\n", errno); | |
246 } | |
247 | |
248 char append_data_to_write[] = "78"; | |
249 const int kAppendDataSize = 2; | |
250 | |
251 // Append "test" to the file. | |
252 bytes_written = WriteFully(file, 0, append_data_to_write, kAppendDataSize); | |
253 EXPECT_EQ(2, bytes_written); | |
254 | |
255 // Read the entire file | |
jar (doing other things)
2013/06/04 23:31:50
nit: Periods at end of sentences.
| |
256 char data_read_1[32]; | |
257 int bytes_read = ReadFully(file, 0, data_read_1, | |
258 kTestDataSize + kAppendDataSize); | |
259 EXPECT_EQ(6, bytes_read); | |
260 | |
261 // Close the file handle to allow the temp directory to be deleted. | |
262 base::ClosePlatformFile(file); | |
263 } | |
264 | |
265 | |
214 TEST(PlatformFile, TruncatePlatformFile) { | 266 TEST(PlatformFile, TruncatePlatformFile) { |
215 base::ScopedTempDir temp_dir; | 267 base::ScopedTempDir temp_dir; |
216 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 268 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
217 FilePath file_path = temp_dir.path().AppendASCII("truncate_file"); | 269 FilePath file_path = temp_dir.path().AppendASCII("truncate_file"); |
218 base::PlatformFile file = base::CreatePlatformFile( | 270 base::PlatformFile file = base::CreatePlatformFile( |
219 file_path, | 271 file_path, |
220 base::PLATFORM_FILE_CREATE | | 272 base::PLATFORM_FILE_CREATE | |
221 base::PLATFORM_FILE_READ | | 273 base::PLATFORM_FILE_READ | |
222 base::PLATFORM_FILE_WRITE, | 274 base::PLATFORM_FILE_WRITE, |
223 NULL, NULL); | 275 NULL, NULL); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
328 EXPECT_EQ(info.last_modified.ToInternalValue(), | 380 EXPECT_EQ(info.last_modified.ToInternalValue(), |
329 new_last_modified.ToInternalValue()); | 381 new_last_modified.ToInternalValue()); |
330 #endif | 382 #endif |
331 | 383 |
332 EXPECT_EQ(info.creation_time.ToInternalValue(), | 384 EXPECT_EQ(info.creation_time.ToInternalValue(), |
333 creation_time.ToInternalValue()); | 385 creation_time.ToInternalValue()); |
334 | 386 |
335 // Close the file handle to allow the temp directory to be deleted. | 387 // Close the file handle to allow the temp directory to be deleted. |
336 base::ClosePlatformFile(file); | 388 base::ClosePlatformFile(file); |
337 } | 389 } |
OLD | NEW |