1 // Copyright 2013 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.
7 #include "base/command_line.h"
8 #include "base/environment.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "chrome/common/env_vars.h"
13 #include "chrome/common/logging_chrome.h"
14 #include "content/public/common/content_switches.h"
15 #include "testing/gmock/include/gmock/gmock-matchers.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 class ChromeLoggingTest : public testing::Test {
20 // Stores the current value of the log file name environment
21 // variable and sets the variable to new_value.
22 void SaveEnvironmentVariable(const std::string& new_value) {
23 std::unique_ptr<base::Environment> env(base::Environment::Create());
24 if (!env->GetVar(env_vars::kLogFileName, &environment_filename_))
25 environment_filename_ = "";
27 env->SetVar(env_vars::kLogFileName, new_value);
30 // Restores the value of the log file nave environment variable
31 // previously saved by SaveEnvironmentVariable().
32 void RestoreEnvironmentVariable() {
33 std::unique_ptr<base::Environment> env(base::Environment::Create());
34 env->SetVar(env_vars::kLogFileName, environment_filename_);
37 void SetLogFileFlag(const std::string& value) {
38 cmd_line_.AppendSwitchASCII(switches::kLogFile, value);
41 const base::CommandLine& cmd_line() { return cmd_line_; }
44 std::string environment_filename_; // Saves real environment value.
45 base::CommandLine cmd_line_ =
46 base::CommandLine(base::CommandLine::NO_PROGRAM);
49 // Tests the log file name getter without an environment variable.
50 TEST_F(ChromeLoggingTest, LogFileName) {
51 SaveEnvironmentVariable(std::string());
53 base::FilePath filename = logging::GetLogFileName(cmd_line());
54 ASSERT_NE(base::FilePath::StringType::npos,
55 filename.value().find(FILE_PATH_LITERAL("chrome_debug.log")));
57 RestoreEnvironmentVariable();
60 // Tests the log file name getter with an environment variable.
61 TEST_F(ChromeLoggingTest, EnvironmentLogFileName) {
62 SaveEnvironmentVariable("test env value");
64 base::FilePath filename = logging::GetLogFileName(cmd_line());
65 ASSERT_EQ(base::FilePath(FILE_PATH_LITERAL("test env value")).value(),
68 RestoreEnvironmentVariable();
71 // Tests the log file name getter with a command-line flag.
72 TEST_F(ChromeLoggingTest, FlagLogFileName) {
73 SetLogFileFlag("test flag value");
75 base::FilePath filename = logging::GetLogFileName(cmd_line());
76 ASSERT_EQ(base::FilePath(FILE_PATH_LITERAL("test flag value")).value(),
80 // Tests the log file name getter with with an environment variable and a
81 // command-line flag. The flag takes precedence.
82 TEST_F(ChromeLoggingTest, EnvironmentAndFlagLogFileName) {
83 SaveEnvironmentVariable("test env value");
84 SetLogFileFlag("test flag value");
86 base::FilePath filename = logging::GetLogFileName(cmd_line());
87 ASSERT_EQ(base::FilePath(FILE_PATH_LITERAL("test flag value")).value(),
90 RestoreEnvironmentVariable();
93 #if defined(OS_CHROMEOS)
94 TEST_F(ChromeLoggingTest, TimestampedName) {
95 base::FilePath path = base::FilePath(FILE_PATH_LITERAL("xy.zzy"));
96 base::FilePath timestamped_path =
97 logging::GenerateTimestampedName(path, base::Time::Now());
98 EXPECT_THAT(timestamped_path.value(),
99 ::testing::MatchesRegex("^xy_\\d+-\\d+\\.zzy$"));
102 TEST_F(ChromeLoggingTest, SetUpSymlink) {
103 base::ScopedTempDir temp_dir;
104 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
105 const base::FilePath temp_dir_path = temp_dir.GetPath();
106 base::FilePath bare_symlink_path =
107 temp_dir_path.Append(FILE_PATH_LITERAL("chrome-test-log"));
108 base::FilePath latest_symlink_path =
109 temp_dir_path.Append(FILE_PATH_LITERAL("chrome-test-log.LATEST"));
110 base::FilePath previous_symlink_path =
111 temp_dir_path.Append(FILE_PATH_LITERAL("chrome-test-log.PREVIOUS"));
113 // Start from a legacy situation, where "chrome-test-log" is a symlink
114 // pointing to the latest log, which has a time-stamped name from a while
116 base::FilePath old_target_path = logging::GenerateTimestampedName(
117 bare_symlink_path, base::Time::UnixEpoch());
119 ASSERT_TRUE(base::CreateSymbolicLink(old_target_path, bare_symlink_path));
121 // Call the testee with the new symlink path, as if starting a new session.
122 logging::SetUpSymlinkIfNeeded(latest_symlink_path,
123 /*start_new_log=*/true);
127 // chrome-test-log --> chrome-test-log.LATEST
128 // chrome-test-log.LATEST --> <new time-stamped path>
129 // no chrome-test-log.PREVIOUS on the legacy transition.
130 base::FilePath target_path;
131 ASSERT_TRUE(base::ReadSymbolicLink(bare_symlink_path, &target_path));
132 EXPECT_EQ(target_path.value(), latest_symlink_path.value());
134 base::FilePath latest_target_path;
135 ASSERT_TRUE(base::ReadSymbolicLink(latest_symlink_path, &latest_target_path));
136 EXPECT_NE(latest_target_path.value(), old_target_path.value());
137 EXPECT_THAT(latest_target_path.value(),
138 ::testing::MatchesRegex("^.*chrome-test-log_\\d+-\\d+$"));
140 // Simulate one more session cycle.
141 logging::SetUpSymlinkIfNeeded(latest_symlink_path, /*start_new_log=*/true);
145 // chrome-test-log.PREVIOUS --> <previous target of chrome-test-log.LATEST>
147 // We also expect that the .LATEST file is now pointing to a file with a
148 // newer time stamp. Unfortunately it's probably not newer enough to tell
149 // the difference since the time stamp granularity is 1 second.
150 ASSERT_TRUE(base::ReadSymbolicLink(previous_symlink_path, &target_path));
151 EXPECT_EQ(target_path.value(), latest_target_path.value());
153 ASSERT_TRUE(base::ReadSymbolicLink(latest_symlink_path, &latest_target_path));
154 EXPECT_THAT(latest_target_path.value(),
155 ::testing::MatchesRegex("^.*chrome-test-log_\\d+-\\d+$"));
158 #endif // defined(OS_CHROMEOS)