forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogging_installer_unittest.cc
125 lines (96 loc) · 4.65 KB
/
logging_installer_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "chrome/installer/util/logging_installer.h"
#include <stdint.h>
#include <optional>
#include <string>
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/win/scoped_handle.h"
#include "testing/gtest/include/gtest/gtest.h"
TEST(LoggingInstallerTest, TestTruncate) {
const std::string test_data(installer::kMaxInstallerLogFileSize + 1, 'a');
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath temp_file = temp_dir.GetPath().Append(L"temp");
EXPECT_TRUE(base::WriteFile(temp_file, test_data));
ASSERT_TRUE(base::PathExists(temp_file));
std::optional<int64_t> file_size = base::GetFileSize(temp_file);
ASSERT_TRUE(file_size.has_value());
EXPECT_EQ(static_cast<int64_t>(test_data.size()), file_size.value());
EXPECT_EQ(installer::LOGFILE_TRUNCATED,
installer::TruncateLogFileIfNeeded(temp_file));
file_size = base::GetFileSize(temp_file);
ASSERT_TRUE(file_size.has_value());
EXPECT_EQ(installer::kTruncatedInstallerLogFileSize, file_size.value());
// Check that the temporary file was deleted.
EXPECT_FALSE(base::PathExists(temp_file.Append(L".tmp")));
}
TEST(LoggingInstallerTest, TestTruncationNotNeeded) {
const std::string test_data(installer::kMaxInstallerLogFileSize, 'a');
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath temp_file = temp_dir.GetPath().Append(L"temp");
EXPECT_TRUE(base::WriteFile(temp_file, test_data));
ASSERT_TRUE(base::PathExists(temp_file));
std::optional<int64_t> file_size = base::GetFileSize(temp_file);
ASSERT_TRUE(file_size.has_value());
EXPECT_EQ(static_cast<int64_t>(test_data.size()), file_size.value());
EXPECT_EQ(installer::LOGFILE_UNTOUCHED,
installer::TruncateLogFileIfNeeded(temp_file));
EXPECT_TRUE(base::PathExists(temp_file));
file_size = base::GetFileSize(temp_file);
ASSERT_TRUE(file_size.has_value());
EXPECT_EQ(static_cast<int64_t>(test_data.size()), file_size.value());
}
TEST(LoggingInstallerTest, TestInUseNeedsTruncation) {
const std::string test_data(installer::kMaxInstallerLogFileSize + 1, 'a');
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath temp_file = temp_dir.GetPath().Append(L"temp");
EXPECT_TRUE(base::WriteFile(temp_file, test_data));
ASSERT_TRUE(base::PathExists(temp_file));
std::optional<int64_t> file_size = base::GetFileSize(temp_file);
ASSERT_TRUE(file_size.has_value());
EXPECT_EQ(static_cast<int64_t>(test_data.size()), file_size.value());
// Prevent the log file from being moved or deleted.
uint32_t file_flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
base::File::FLAG_WIN_EXCLUSIVE_READ;
base::File temp_platform_file(temp_file, file_flags);
ASSERT_TRUE(temp_platform_file.IsValid());
EXPECT_EQ(installer::LOGFILE_UNTOUCHED,
installer::TruncateLogFileIfNeeded(temp_file));
EXPECT_TRUE(base::PathExists(temp_file));
file_size = base::GetFileSize(temp_file);
ASSERT_TRUE(file_size.has_value());
EXPECT_EQ(static_cast<int64_t>(test_data.size()), file_size.value());
}
TEST(LoggingInstallerTest, TestMoveFailsNeedsTruncation) {
const std::string test_data(installer::kMaxInstallerLogFileSize + 1, 'a');
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath temp_file = temp_dir.GetPath().Append(L"temp");
EXPECT_TRUE(base::WriteFile(temp_file, test_data));
ASSERT_TRUE(base::PathExists(temp_file));
std::optional<int64_t> file_size = base::GetFileSize(temp_file);
ASSERT_TRUE(file_size.has_value());
EXPECT_EQ(static_cast<int64_t>(test_data.size()), file_size.value());
// Create an inconvenient, non-deletable file in the location that
// TruncateLogFileIfNeeded would like to move the log file to.
uint32_t file_flags = base::File::FLAG_CREATE | base::File::FLAG_READ |
base::File::FLAG_WIN_EXCLUSIVE_READ;
base::FilePath temp_file_move_dest(temp_file.value() +
FILE_PATH_LITERAL(".tmp"));
base::File temp_move_destination_file(temp_file_move_dest, file_flags);
ASSERT_TRUE(temp_move_destination_file.IsValid());
EXPECT_EQ(installer::LOGFILE_DELETED,
installer::TruncateLogFileIfNeeded(temp_file));
EXPECT_FALSE(base::PathExists(temp_file));
}