OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/file_util.h" |
| 6 #include "base/platform_file.h" |
| 7 #include "chrome/browser/image_writer/image_writer_test_utils.h" |
| 8 |
| 9 namespace image_writer { |
| 10 |
| 11 const int kTestFileSize = 1024 * 1024; // 1 MB |
| 12 const char kTestBytePattern = 0x55; // 10101010 |
| 13 |
| 14 bool TempFilesForTesting::SetUp() { |
| 15 return temp_dir_.CreateUniqueTempDir() |
| 16 && file_util::CreateTemporaryFileInDir(temp_dir_.path(), &image_path_) |
| 17 && file_util::CreateTemporaryFileInDir(temp_dir_.path(), &device_path_); |
| 18 } |
| 19 |
| 20 base::FilePath TempFilesForTesting::ImagePath() { |
| 21 return image_path_; |
| 22 } |
| 23 |
| 24 base::FilePath TempFilesForTesting::DevicePath() { |
| 25 return device_path_; |
| 26 } |
| 27 |
| 28 bool TempFilesForTesting::InitializeImage() { |
| 29 return InitializeFile(image_path_, kTestFileSize, kTestBytePattern); |
| 30 } |
| 31 |
| 32 bool TempFilesForTesting::InitializeFile(const base::FilePath& path, |
| 33 int length, |
| 34 char pattern) { |
| 35 char buffer[length]; |
| 36 memset(buffer, pattern, length); |
| 37 bool created; |
| 38 base::PlatformFileError error; |
| 39 |
| 40 base::PlatformFile file = base::CreatePlatformFile( |
| 41 path, |
| 42 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE, |
| 43 &created, |
| 44 &error); |
| 45 |
| 46 if (!created || error != base::PLATFORM_FILE_OK) { |
| 47 DLOG(ERROR) << "Unable to create/truncate temporary file."; |
| 48 return false; |
| 49 } |
| 50 |
| 51 |
| 52 int bytes_written = base::WritePlatformFile(file, 0, buffer, length); |
| 53 |
| 54 if (bytes_written != length) { |
| 55 DLOG(ERROR) << "Unable to write all bytes to temporary file." |
| 56 << " Expected " << length << " but was " << bytes_written; |
| 57 return false; |
| 58 } |
| 59 |
| 60 if (!base::ClosePlatformFile(file)) { |
| 61 DLOG(ERROR) << "Unable to close temporary file."; |
| 62 return false; |
| 63 } |
| 64 |
| 65 return true; |
| 66 } |
| 67 |
| 68 bool TempFilesForTesting::CompareImageAndDevice() { |
| 69 char image_buffer[kTestFileSize]; |
| 70 char device_buffer[kTestFileSize]; |
| 71 |
| 72 int bytes_read_image = |
| 73 file_util::ReadFile(image_path_, image_buffer, kTestFileSize); |
| 74 int bytes_read_device = |
| 75 file_util::ReadFile(device_path_, device_buffer, kTestFileSize); |
| 76 |
| 77 if (bytes_read_image != bytes_read_device) { |
| 78 DLOG(ERROR) << "Byte counts do not match." |
| 79 << " Image: " << bytes_read_image |
| 80 << " Device: " << bytes_read_device; |
| 81 return false; |
| 82 } |
| 83 |
| 84 return memcmp(image_buffer, device_buffer, kTestFileSize) == 0; |
| 85 } |
| 86 |
| 87 } // namespace image_writer |
OLD | NEW |