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