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/file.h" | 6 #include "base/files/file.h" |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 | 151 |
152 // Write past the end of the file. | 152 // Write past the end of the file. |
153 const int kOffsetBeyondEndOfFile = 10; | 153 const int kOffsetBeyondEndOfFile = 10; |
154 const int kPartialWriteLength = 2; | 154 const int kPartialWriteLength = 2; |
155 bytes_written = file.Write(kOffsetBeyondEndOfFile, | 155 bytes_written = file.Write(kOffsetBeyondEndOfFile, |
156 data_to_write, kPartialWriteLength); | 156 data_to_write, kPartialWriteLength); |
157 EXPECT_EQ(kPartialWriteLength, bytes_written); | 157 EXPECT_EQ(kPartialWriteLength, bytes_written); |
158 | 158 |
159 // Make sure the file was extended. | 159 // Make sure the file was extended. |
160 int64 file_size = 0; | 160 int64 file_size = 0; |
161 EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size)); | 161 EXPECT_TRUE(GetFileSize(file_path, &file_size)); |
162 EXPECT_EQ(kOffsetBeyondEndOfFile + kPartialWriteLength, file_size); | 162 EXPECT_EQ(kOffsetBeyondEndOfFile + kPartialWriteLength, file_size); |
163 | 163 |
164 // Make sure the file was zero-padded. | 164 // Make sure the file was zero-padded. |
165 char data_read_2[32]; | 165 char data_read_2[32]; |
166 bytes_read = file.Read(0, data_read_2, static_cast<int>(file_size)); | 166 bytes_read = file.Read(0, data_read_2, static_cast<int>(file_size)); |
167 EXPECT_EQ(file_size, bytes_read); | 167 EXPECT_EQ(file_size, bytes_read); |
168 for (int i = 0; i < kTestDataSize; i++) | 168 for (int i = 0; i < kTestDataSize; i++) |
169 EXPECT_EQ(data_to_write[i], data_read_2[i]); | 169 EXPECT_EQ(data_to_write[i], data_read_2[i]); |
170 for (int i = kTestDataSize; i < kOffsetBeyondEndOfFile; i++) | 170 for (int i = kTestDataSize; i < kOffsetBeyondEndOfFile; i++) |
171 EXPECT_EQ(0, data_read_2[i]); | 171 EXPECT_EQ(0, data_read_2[i]); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 // Write "test" to the file. | 233 // Write "test" to the file. |
234 char data_to_write[] = "test"; | 234 char data_to_write[] = "test"; |
235 int kTestDataSize = 4; | 235 int kTestDataSize = 4; |
236 int bytes_written = file.Write(0, data_to_write, kTestDataSize); | 236 int bytes_written = file.Write(0, data_to_write, kTestDataSize); |
237 EXPECT_EQ(kTestDataSize, bytes_written); | 237 EXPECT_EQ(kTestDataSize, bytes_written); |
238 | 238 |
239 // Extend the file. | 239 // Extend the file. |
240 const int kExtendedFileLength = 10; | 240 const int kExtendedFileLength = 10; |
241 int64 file_size = 0; | 241 int64 file_size = 0; |
242 EXPECT_TRUE(file.Truncate(kExtendedFileLength)); | 242 EXPECT_TRUE(file.Truncate(kExtendedFileLength)); |
243 EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size)); | 243 EXPECT_TRUE(GetFileSize(file_path, &file_size)); |
244 EXPECT_EQ(kExtendedFileLength, file_size); | 244 EXPECT_EQ(kExtendedFileLength, file_size); |
245 | 245 |
246 // Make sure the file was zero-padded. | 246 // Make sure the file was zero-padded. |
247 char data_read[32]; | 247 char data_read[32]; |
248 int bytes_read = file.Read(0, data_read, static_cast<int>(file_size)); | 248 int bytes_read = file.Read(0, data_read, static_cast<int>(file_size)); |
249 EXPECT_EQ(file_size, bytes_read); | 249 EXPECT_EQ(file_size, bytes_read); |
250 for (int i = 0; i < kTestDataSize; i++) | 250 for (int i = 0; i < kTestDataSize; i++) |
251 EXPECT_EQ(data_to_write[i], data_read[i]); | 251 EXPECT_EQ(data_to_write[i], data_read[i]); |
252 for (int i = kTestDataSize; i < file_size; i++) | 252 for (int i = kTestDataSize; i < file_size; i++) |
253 EXPECT_EQ(0, data_read[i]); | 253 EXPECT_EQ(0, data_read[i]); |
254 | 254 |
255 // Truncate the file. | 255 // Truncate the file. |
256 const int kTruncatedFileLength = 2; | 256 const int kTruncatedFileLength = 2; |
257 EXPECT_TRUE(file.Truncate(kTruncatedFileLength)); | 257 EXPECT_TRUE(file.Truncate(kTruncatedFileLength)); |
258 EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size)); | 258 EXPECT_TRUE(GetFileSize(file_path, &file_size)); |
259 EXPECT_EQ(kTruncatedFileLength, file_size); | 259 EXPECT_EQ(kTruncatedFileLength, file_size); |
260 | 260 |
261 // Make sure the file was truncated. | 261 // Make sure the file was truncated. |
262 bytes_read = file.Read(0, data_read, kTestDataSize); | 262 bytes_read = file.Read(0, data_read, kTestDataSize); |
263 EXPECT_EQ(file_size, bytes_read); | 263 EXPECT_EQ(file_size, bytes_read); |
264 for (int i = 0; i < file_size; i++) | 264 for (int i = 0; i < file_size; i++) |
265 EXPECT_EQ(data_to_write[i], data_read[i]); | 265 EXPECT_EQ(data_to_write[i], data_read[i]); |
266 } | 266 } |
267 | 267 |
268 // Flakily fails: http://crbug.com/86494 | 268 // Flakily fails: http://crbug.com/86494 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 | 351 |
352 char buffer[kDataSize]; | 352 char buffer[kDataSize]; |
353 int first_chunk_size = kDataSize / 2; | 353 int first_chunk_size = kDataSize / 2; |
354 EXPECT_EQ(first_chunk_size, file.ReadAtCurrentPos(buffer, first_chunk_size)); | 354 EXPECT_EQ(first_chunk_size, file.ReadAtCurrentPos(buffer, first_chunk_size)); |
355 EXPECT_EQ(kDataSize - first_chunk_size, | 355 EXPECT_EQ(kDataSize - first_chunk_size, |
356 file.ReadAtCurrentPos(buffer + first_chunk_size, | 356 file.ReadAtCurrentPos(buffer + first_chunk_size, |
357 kDataSize - first_chunk_size)); | 357 kDataSize - first_chunk_size)); |
358 EXPECT_EQ(std::string(buffer, buffer + kDataSize), | 358 EXPECT_EQ(std::string(buffer, buffer + kDataSize), |
359 std::string(kData)); | 359 std::string(kData)); |
360 } | 360 } |
OLD | NEW |