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