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