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("read_write_file"); |
| 218 base::PlatformFile file = base::CreatePlatformFile( |
| 219 file_path, |
| 220 base::PLATFORM_FILE_CREATE | |
| 221 base::PLATFORM_FILE_READ | |
| 222 base::PLATFORM_FILE_WRITE, |
| 223 NULL, NULL); |
| 224 EXPECT_NE(base::kInvalidPlatformFileValue, file); |
| 225 |
| 226 char data_to_write[] = "test"; |
| 227 const int kTestDataSize = 4; |
| 228 |
| 229 // Write 0 bytes to the file. |
| 230 int bytes_written = WriteFully(file, 0, data_to_write, 0); |
| 231 EXPECT_EQ(0, bytes_written); |
| 232 |
| 233 // Write "test" to the file. |
| 234 bytes_written = WriteFully(file, 0, data_to_write, kTestDataSize); |
| 235 EXPECT_EQ(kTestDataSize, bytes_written); |
| 236 |
| 237 base::ClosePlatformFile(file); |
| 238 file = base::CreatePlatformFile( |
| 239 file_path, |
| 240 base::PLATFORM_FILE_OPEN | |
| 241 base::PLATFORM_FILE_READ | |
| 242 base::PLATFORM_FILE_APPEND, |
| 243 NULL, NULL); |
| 244 EXPECT_NE(base::kInvalidPlatformFileValue, file); |
| 245 if (file == base::kInvalidPlatformFileValue) { |
| 246 printf("errno: %d\n", errno); |
| 247 } |
| 248 |
| 249 char append_data_to_write[] = "78"; |
| 250 const int kAppendDataSize = 2; |
| 251 |
| 252 // Append "test" to the file. |
| 253 bytes_written = WriteFully(file, 0, append_data_to_write, kAppendDataSize); |
| 254 EXPECT_EQ(2, bytes_written); |
| 255 |
| 256 // Read the entire file |
| 257 char data_read_1[32]; |
| 258 int bytes_read = ReadFully(file, 0, data_read_1, |
| 259 kTestDataSize + kAppendDataSize); |
| 260 EXPECT_EQ(6, bytes_read); |
| 261 |
| 262 // Close the file handle to allow the temp directory to be deleted. |
| 263 base::ClosePlatformFile(file); |
| 264 } |
| 265 |
| 266 |
214 TEST(PlatformFile, TruncatePlatformFile) { | 267 TEST(PlatformFile, TruncatePlatformFile) { |
215 base::ScopedTempDir temp_dir; | 268 base::ScopedTempDir temp_dir; |
216 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 269 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
217 FilePath file_path = temp_dir.path().AppendASCII("truncate_file"); | 270 FilePath file_path = temp_dir.path().AppendASCII("truncate_file"); |
218 base::PlatformFile file = base::CreatePlatformFile( | 271 base::PlatformFile file = base::CreatePlatformFile( |
219 file_path, | 272 file_path, |
220 base::PLATFORM_FILE_CREATE | | 273 base::PLATFORM_FILE_CREATE | |
221 base::PLATFORM_FILE_READ | | 274 base::PLATFORM_FILE_READ | |
222 base::PLATFORM_FILE_WRITE, | 275 base::PLATFORM_FILE_WRITE, |
223 NULL, NULL); | 276 NULL, NULL); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 EXPECT_EQ(info.last_modified.ToInternalValue(), | 381 EXPECT_EQ(info.last_modified.ToInternalValue(), |
329 new_last_modified.ToInternalValue()); | 382 new_last_modified.ToInternalValue()); |
330 #endif | 383 #endif |
331 | 384 |
332 EXPECT_EQ(info.creation_time.ToInternalValue(), | 385 EXPECT_EQ(info.creation_time.ToInternalValue(), |
333 creation_time.ToInternalValue()); | 386 creation_time.ToInternalValue()); |
334 | 387 |
335 // Close the file handle to allow the temp directory to be deleted. | 388 // Close the file handle to allow the temp directory to be deleted. |
336 base::ClosePlatformFile(file); | 389 base::ClosePlatformFile(file); |
337 } | 390 } |
OLD | NEW |