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