| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/win/scoped_handle.h" | 13 #include "base/win/scoped_handle.h" |
| 14 #include "chrome/installer/util/logging_installer.h" | 14 #include "chrome/installer/util/logging_installer.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 TEST(LoggingInstallerTest, TestTruncate) { | 17 TEST(LoggingInstallerTest, TestTruncate) { |
| 18 const std::string test_data(installer::kMaxInstallerLogFileSize + 1, 'a'); | 18 const std::string test_data(installer::kMaxInstallerLogFileSize + 1, 'a'); |
| 19 | 19 |
| 20 base::ScopedTempDir temp_dir; | 20 base::ScopedTempDir temp_dir; |
| 21 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 21 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 22 | 22 |
| 23 base::FilePath temp_file = temp_dir.path().Append(L"temp"); | 23 base::FilePath temp_file = temp_dir.GetPath().Append(L"temp"); |
| 24 EXPECT_EQ(static_cast<int>(test_data.size()), | 24 EXPECT_EQ(static_cast<int>(test_data.size()), |
| 25 base::WriteFile(temp_file, &test_data[0], | 25 base::WriteFile(temp_file, &test_data[0], |
| 26 static_cast<int>(test_data.size()))); | 26 static_cast<int>(test_data.size()))); |
| 27 ASSERT_TRUE(base::PathExists(temp_file)); | 27 ASSERT_TRUE(base::PathExists(temp_file)); |
| 28 | 28 |
| 29 int64_t file_size = 0; | 29 int64_t file_size = 0; |
| 30 EXPECT_TRUE(base::GetFileSize(temp_file, &file_size)); | 30 EXPECT_TRUE(base::GetFileSize(temp_file, &file_size)); |
| 31 EXPECT_EQ(static_cast<int64_t>(test_data.size()), file_size); | 31 EXPECT_EQ(static_cast<int64_t>(test_data.size()), file_size); |
| 32 | 32 |
| 33 EXPECT_EQ(installer::LOGFILE_TRUNCATED, | 33 EXPECT_EQ(installer::LOGFILE_TRUNCATED, |
| 34 installer::TruncateLogFileIfNeeded(temp_file)); | 34 installer::TruncateLogFileIfNeeded(temp_file)); |
| 35 | 35 |
| 36 EXPECT_TRUE(base::GetFileSize(temp_file, &file_size)); | 36 EXPECT_TRUE(base::GetFileSize(temp_file, &file_size)); |
| 37 EXPECT_EQ(installer::kTruncatedInstallerLogFileSize , file_size); | 37 EXPECT_EQ(installer::kTruncatedInstallerLogFileSize , file_size); |
| 38 | 38 |
| 39 // Check that the temporary file was deleted. | 39 // Check that the temporary file was deleted. |
| 40 EXPECT_FALSE(base::PathExists(temp_file.Append(L".tmp"))); | 40 EXPECT_FALSE(base::PathExists(temp_file.Append(L".tmp"))); |
| 41 } | 41 } |
| 42 | 42 |
| 43 TEST(LoggingInstallerTest, TestTruncationNotNeeded) { | 43 TEST(LoggingInstallerTest, TestTruncationNotNeeded) { |
| 44 const std::string test_data(installer::kMaxInstallerLogFileSize, 'a'); | 44 const std::string test_data(installer::kMaxInstallerLogFileSize, 'a'); |
| 45 | 45 |
| 46 base::ScopedTempDir temp_dir; | 46 base::ScopedTempDir temp_dir; |
| 47 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 47 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 48 | 48 |
| 49 base::FilePath temp_file = temp_dir.path().Append(L"temp"); | 49 base::FilePath temp_file = temp_dir.GetPath().Append(L"temp"); |
| 50 EXPECT_EQ(static_cast<int>(test_data.size()), | 50 EXPECT_EQ(static_cast<int>(test_data.size()), |
| 51 base::WriteFile(temp_file, &test_data[0], | 51 base::WriteFile(temp_file, &test_data[0], |
| 52 static_cast<int>(test_data.size()))); | 52 static_cast<int>(test_data.size()))); |
| 53 ASSERT_TRUE(base::PathExists(temp_file)); | 53 ASSERT_TRUE(base::PathExists(temp_file)); |
| 54 | 54 |
| 55 int64_t file_size = 0; | 55 int64_t file_size = 0; |
| 56 EXPECT_TRUE(base::GetFileSize(temp_file, &file_size)); | 56 EXPECT_TRUE(base::GetFileSize(temp_file, &file_size)); |
| 57 EXPECT_EQ(static_cast<int64_t>(test_data.size()), file_size); | 57 EXPECT_EQ(static_cast<int64_t>(test_data.size()), file_size); |
| 58 | 58 |
| 59 EXPECT_EQ(installer::LOGFILE_UNTOUCHED, | 59 EXPECT_EQ(installer::LOGFILE_UNTOUCHED, |
| 60 installer::TruncateLogFileIfNeeded(temp_file)); | 60 installer::TruncateLogFileIfNeeded(temp_file)); |
| 61 EXPECT_TRUE(base::PathExists(temp_file)); | 61 EXPECT_TRUE(base::PathExists(temp_file)); |
| 62 EXPECT_TRUE(base::GetFileSize(temp_file, &file_size)); | 62 EXPECT_TRUE(base::GetFileSize(temp_file, &file_size)); |
| 63 EXPECT_EQ(static_cast<int64_t>(test_data.size()), file_size); | 63 EXPECT_EQ(static_cast<int64_t>(test_data.size()), file_size); |
| 64 } | 64 } |
| 65 | 65 |
| 66 TEST(LoggingInstallerTest, TestInUseNeedsTruncation) { | 66 TEST(LoggingInstallerTest, TestInUseNeedsTruncation) { |
| 67 const std::string test_data(installer::kMaxInstallerLogFileSize + 1, 'a'); | 67 const std::string test_data(installer::kMaxInstallerLogFileSize + 1, 'a'); |
| 68 | 68 |
| 69 base::ScopedTempDir temp_dir; | 69 base::ScopedTempDir temp_dir; |
| 70 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 70 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 71 | 71 |
| 72 base::FilePath temp_file = temp_dir.path().Append(L"temp"); | 72 base::FilePath temp_file = temp_dir.GetPath().Append(L"temp"); |
| 73 EXPECT_EQ(static_cast<int>(test_data.size()), | 73 EXPECT_EQ(static_cast<int>(test_data.size()), |
| 74 base::WriteFile(temp_file, &test_data[0], | 74 base::WriteFile(temp_file, &test_data[0], |
| 75 static_cast<int>(test_data.size()))); | 75 static_cast<int>(test_data.size()))); |
| 76 ASSERT_TRUE(base::PathExists(temp_file)); | 76 ASSERT_TRUE(base::PathExists(temp_file)); |
| 77 int64_t file_size = 0; | 77 int64_t file_size = 0; |
| 78 EXPECT_TRUE(base::GetFileSize(temp_file, &file_size)); | 78 EXPECT_TRUE(base::GetFileSize(temp_file, &file_size)); |
| 79 EXPECT_EQ(static_cast<int64_t>(test_data.size()), file_size); | 79 EXPECT_EQ(static_cast<int64_t>(test_data.size()), file_size); |
| 80 | 80 |
| 81 // Prevent the log file from being moved or deleted. | 81 // Prevent the log file from being moved or deleted. |
| 82 uint32_t file_flags = base::File::FLAG_OPEN | base::File::FLAG_READ | | 82 uint32_t file_flags = base::File::FLAG_OPEN | base::File::FLAG_READ | |
| 83 base::File::FLAG_EXCLUSIVE_READ; | 83 base::File::FLAG_EXCLUSIVE_READ; |
| 84 base::File temp_platform_file(temp_file, file_flags); | 84 base::File temp_platform_file(temp_file, file_flags); |
| 85 ASSERT_TRUE(temp_platform_file.IsValid()); | 85 ASSERT_TRUE(temp_platform_file.IsValid()); |
| 86 | 86 |
| 87 EXPECT_EQ(installer::LOGFILE_UNTOUCHED, | 87 EXPECT_EQ(installer::LOGFILE_UNTOUCHED, |
| 88 installer::TruncateLogFileIfNeeded(temp_file)); | 88 installer::TruncateLogFileIfNeeded(temp_file)); |
| 89 EXPECT_TRUE(base::PathExists(temp_file)); | 89 EXPECT_TRUE(base::PathExists(temp_file)); |
| 90 EXPECT_TRUE(base::GetFileSize(temp_file, &file_size)); | 90 EXPECT_TRUE(base::GetFileSize(temp_file, &file_size)); |
| 91 EXPECT_EQ(static_cast<int64_t>(test_data.size()), file_size); | 91 EXPECT_EQ(static_cast<int64_t>(test_data.size()), file_size); |
| 92 } | 92 } |
| 93 | 93 |
| 94 TEST(LoggingInstallerTest, TestMoveFailsNeedsTruncation) { | 94 TEST(LoggingInstallerTest, TestMoveFailsNeedsTruncation) { |
| 95 const std::string test_data(installer::kMaxInstallerLogFileSize + 1, 'a'); | 95 const std::string test_data(installer::kMaxInstallerLogFileSize + 1, 'a'); |
| 96 | 96 |
| 97 base::ScopedTempDir temp_dir; | 97 base::ScopedTempDir temp_dir; |
| 98 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 98 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 99 | 99 |
| 100 base::FilePath temp_file = temp_dir.path().Append(L"temp"); | 100 base::FilePath temp_file = temp_dir.GetPath().Append(L"temp"); |
| 101 EXPECT_EQ(static_cast<int>(test_data.size()), | 101 EXPECT_EQ(static_cast<int>(test_data.size()), |
| 102 base::WriteFile(temp_file, &test_data[0], | 102 base::WriteFile(temp_file, &test_data[0], |
| 103 static_cast<int>(test_data.size()))); | 103 static_cast<int>(test_data.size()))); |
| 104 ASSERT_TRUE(base::PathExists(temp_file)); | 104 ASSERT_TRUE(base::PathExists(temp_file)); |
| 105 int64_t file_size = 0; | 105 int64_t file_size = 0; |
| 106 EXPECT_TRUE(base::GetFileSize(temp_file, &file_size)); | 106 EXPECT_TRUE(base::GetFileSize(temp_file, &file_size)); |
| 107 EXPECT_EQ(static_cast<int64_t>(test_data.size()), file_size); | 107 EXPECT_EQ(static_cast<int64_t>(test_data.size()), file_size); |
| 108 | 108 |
| 109 // Create an inconvenient, non-deletable file in the location that | 109 // Create an inconvenient, non-deletable file in the location that |
| 110 // TruncateLogFileIfNeeded would like to move the log file to. | 110 // TruncateLogFileIfNeeded would like to move the log file to. |
| 111 uint32_t file_flags = base::File::FLAG_CREATE | base::File::FLAG_READ | | 111 uint32_t file_flags = base::File::FLAG_CREATE | base::File::FLAG_READ | |
| 112 base::File::FLAG_EXCLUSIVE_READ; | 112 base::File::FLAG_EXCLUSIVE_READ; |
| 113 base::FilePath temp_file_move_dest( | 113 base::FilePath temp_file_move_dest( |
| 114 temp_file.value() + FILE_PATH_LITERAL(".tmp")); | 114 temp_file.value() + FILE_PATH_LITERAL(".tmp")); |
| 115 base::File temp_move_destination_file(temp_file_move_dest, file_flags); | 115 base::File temp_move_destination_file(temp_file_move_dest, file_flags); |
| 116 ASSERT_TRUE(temp_move_destination_file.IsValid()); | 116 ASSERT_TRUE(temp_move_destination_file.IsValid()); |
| 117 | 117 |
| 118 EXPECT_EQ(installer::LOGFILE_DELETED, | 118 EXPECT_EQ(installer::LOGFILE_DELETED, |
| 119 installer::TruncateLogFileIfNeeded(temp_file)); | 119 installer::TruncateLogFileIfNeeded(temp_file)); |
| 120 EXPECT_FALSE(base::PathExists(temp_file)); | 120 EXPECT_FALSE(base::PathExists(temp_file)); |
| 121 } | 121 } |
| OLD | NEW |