OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015 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 "base/at_exit.h" |
| 6 #include "base/command_line.h" |
| 7 #include "base/files/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "components/tracing/trace_config_file.h" |
| 10 #include "components/tracing/tracing_switches.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace tracing { |
| 14 |
| 15 namespace { |
| 16 |
| 17 const char kTraceConfig[] = |
| 18 "{" |
| 19 "\"enable_argument_filter\":true," |
| 20 "\"enable_sampling\":true," |
| 21 "\"enable_systrace\":true," |
| 22 "\"excluded_categories\":[\"excluded\",\"exc_pattern*\"]," |
| 23 "\"included_categories\":[\"included\"," |
| 24 "\"inc_pattern*\"," |
| 25 "\"disabled-by-default-cc\"]," |
| 26 "\"record_mode\":\"record-continuously\"," |
| 27 "\"synthetic_delays\":[\"test.Delay1;16\",\"test.Delay2;32\"]" |
| 28 "}"; |
| 29 |
| 30 std::string GetTraceConfigFileContent(std::string trace_config, |
| 31 std::string startup_duration, |
| 32 std::string result_file) { |
| 33 std::string content = "{"; |
| 34 if (!trace_config.empty()) |
| 35 content += "\"trace_config\":" + trace_config; |
| 36 |
| 37 if (!startup_duration.empty()) { |
| 38 if (content != "{") |
| 39 content += ","; |
| 40 content += "\"startup_duration\":" + startup_duration; |
| 41 } |
| 42 |
| 43 if (!result_file.empty()) { |
| 44 if (content != "{") |
| 45 content += ","; |
| 46 content += "\"result_file\":\"" + result_file + "\""; |
| 47 } |
| 48 |
| 49 content += "}"; |
| 50 return content; |
| 51 } |
| 52 |
| 53 } // namespace |
| 54 |
| 55 TEST(TraceConfigFileTest, TraceStartupEnabled) { |
| 56 base::ShadowingAtExitManager sem; |
| 57 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 58 switches::kTraceStartup); |
| 59 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 60 switches::kTraceConfigFile); |
| 61 |
| 62 EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled()); |
| 63 } |
| 64 |
| 65 TEST(TraceConfigFileTest, TraceShutdownEnabled) { |
| 66 base::ShadowingAtExitManager sem; |
| 67 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 68 switches::kTraceShutdown); |
| 69 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 70 switches::kTraceConfigFile); |
| 71 |
| 72 EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled()); |
| 73 } |
| 74 |
| 75 TEST(TraceConfigFileTest, TraceConfigFileNotEnabled) { |
| 76 base::ShadowingAtExitManager sem; |
| 77 EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled()); |
| 78 } |
| 79 |
| 80 TEST(TraceConfigFileTest, TraceConfigFileEnabledWithoutPath) { |
| 81 base::ShadowingAtExitManager sem; |
| 82 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 83 switches::kTraceConfigFile); |
| 84 |
| 85 ASSERT_TRUE(TraceConfigFile::GetInstance()->IsEnabled()); |
| 86 EXPECT_EQ(base::trace_event::TraceConfig().ToString(), |
| 87 TraceConfigFile::GetInstance()->GetTraceConfig().ToString()); |
| 88 EXPECT_EQ(5, TraceConfigFile::GetInstance()->GetStartupDuration()); |
| 89 EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("chrometrace.log")), |
| 90 TraceConfigFile::GetInstance()->GetResultFile()); |
| 91 } |
| 92 |
| 93 TEST(TraceConfigFileTest, TraceConfigFileEnabledWithInvalidPath) { |
| 94 base::ShadowingAtExitManager sem; |
| 95 base::CommandLine::ForCurrentProcess()->AppendSwitchPath( |
| 96 switches::kTraceConfigFile, |
| 97 base::FilePath(FILE_PATH_LITERAL("invalid-trace-config-file-path"))); |
| 98 |
| 99 EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled()); |
| 100 } |
| 101 |
| 102 TEST(TraceConfigFileTest, ValidContent) { |
| 103 base::ShadowingAtExitManager sem; |
| 104 std::string content = GetTraceConfigFileContent( |
| 105 kTraceConfig, "10", "trace_result_file.log"); |
| 106 |
| 107 base::FilePath trace_config_file; |
| 108 base::ScopedTempDir temp_dir; |
| 109 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 110 ASSERT_TRUE( |
| 111 base::CreateTemporaryFileInDir(temp_dir.path(), &trace_config_file)); |
| 112 ASSERT_NE( |
| 113 -1, |
| 114 base::WriteFile(trace_config_file, content.c_str(), content.length())); |
| 115 base::CommandLine::ForCurrentProcess()->AppendSwitchPath( |
| 116 switches::kTraceConfigFile, trace_config_file); |
| 117 |
| 118 ASSERT_TRUE(TraceConfigFile::GetInstance()->IsEnabled()); |
| 119 EXPECT_STREQ( |
| 120 kTraceConfig, |
| 121 TraceConfigFile::GetInstance()->GetTraceConfig().ToString().c_str()); |
| 122 EXPECT_EQ(10, TraceConfigFile::GetInstance()->GetStartupDuration()); |
| 123 EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("trace_result_file.log")), |
| 124 TraceConfigFile::GetInstance()->GetResultFile()); |
| 125 } |
| 126 |
| 127 TEST(TraceConfigFileTest, ValidContentWithOnlyTraceConfig) { |
| 128 base::ShadowingAtExitManager sem; |
| 129 std::string content = GetTraceConfigFileContent(kTraceConfig, "", ""); |
| 130 |
| 131 base::FilePath trace_config_file; |
| 132 base::ScopedTempDir temp_dir; |
| 133 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 134 ASSERT_TRUE( |
| 135 base::CreateTemporaryFileInDir(temp_dir.path(), &trace_config_file)); |
| 136 ASSERT_NE( |
| 137 -1, |
| 138 base::WriteFile(trace_config_file, content.c_str(), content.length())); |
| 139 base::CommandLine::ForCurrentProcess()->AppendSwitchPath( |
| 140 switches::kTraceConfigFile, trace_config_file); |
| 141 |
| 142 ASSERT_TRUE(TraceConfigFile::GetInstance()->IsEnabled()); |
| 143 EXPECT_STREQ( |
| 144 kTraceConfig, |
| 145 TraceConfigFile::GetInstance()->GetTraceConfig().ToString().c_str()); |
| 146 EXPECT_EQ(0, TraceConfigFile::GetInstance()->GetStartupDuration()); |
| 147 EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("chrometrace.log")), |
| 148 TraceConfigFile::GetInstance()->GetResultFile()); |
| 149 } |
| 150 |
| 151 TEST(TraceConfigFileTest, ContentWithNegtiveDuration) { |
| 152 base::ShadowingAtExitManager sem; |
| 153 std::string content = GetTraceConfigFileContent(kTraceConfig, "-1", ""); |
| 154 |
| 155 base::FilePath trace_config_file; |
| 156 base::ScopedTempDir temp_dir; |
| 157 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 158 ASSERT_TRUE( |
| 159 base::CreateTemporaryFileInDir(temp_dir.path(), &trace_config_file)); |
| 160 ASSERT_NE( |
| 161 -1, |
| 162 base::WriteFile(trace_config_file, content.c_str(), content.length())); |
| 163 base::CommandLine::ForCurrentProcess()->AppendSwitchPath( |
| 164 switches::kTraceConfigFile, trace_config_file); |
| 165 |
| 166 ASSERT_TRUE(TraceConfigFile::GetInstance()->IsEnabled()); |
| 167 EXPECT_STREQ( |
| 168 kTraceConfig, |
| 169 TraceConfigFile::GetInstance()->GetTraceConfig().ToString().c_str()); |
| 170 EXPECT_EQ(0, TraceConfigFile::GetInstance()->GetStartupDuration()); |
| 171 EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("chrometrace.log")), |
| 172 TraceConfigFile::GetInstance()->GetResultFile()); |
| 173 } |
| 174 |
| 175 TEST(TraceConfigFileTest, ContentWithoutTraceConfig) { |
| 176 base::ShadowingAtExitManager sem; |
| 177 std::string content = GetTraceConfigFileContent( |
| 178 "", "10", "trace_result_file.log"); |
| 179 |
| 180 base::FilePath trace_config_file; |
| 181 base::ScopedTempDir temp_dir; |
| 182 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 183 ASSERT_TRUE( |
| 184 base::CreateTemporaryFileInDir(temp_dir.path(), &trace_config_file)); |
| 185 ASSERT_NE( |
| 186 -1, |
| 187 base::WriteFile(trace_config_file, content.c_str(), content.length())); |
| 188 base::CommandLine::ForCurrentProcess()->AppendSwitchPath( |
| 189 switches::kTraceConfigFile, trace_config_file); |
| 190 |
| 191 EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled()); |
| 192 } |
| 193 |
| 194 TEST(TraceConfigFileTest, InvalidContent) { |
| 195 base::ShadowingAtExitManager sem; |
| 196 std::string content = "invalid trace config file content"; |
| 197 |
| 198 base::FilePath trace_config_file; |
| 199 base::ScopedTempDir temp_dir; |
| 200 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 201 ASSERT_TRUE( |
| 202 base::CreateTemporaryFileInDir(temp_dir.path(), &trace_config_file)); |
| 203 ASSERT_NE( |
| 204 -1, |
| 205 base::WriteFile(trace_config_file, content.c_str(), content.length())); |
| 206 base::CommandLine::ForCurrentProcess()->AppendSwitchPath( |
| 207 switches::kTraceConfigFile, trace_config_file); |
| 208 |
| 209 EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled()); |
| 210 } |
| 211 |
| 212 TEST(TraceConfigFileTest, EmptyContent) { |
| 213 base::ShadowingAtExitManager sem; |
| 214 base::FilePath trace_config_file; |
| 215 base::ScopedTempDir temp_dir; |
| 216 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 217 ASSERT_TRUE( |
| 218 base::CreateTemporaryFileInDir(temp_dir.path(), &trace_config_file)); |
| 219 base::CommandLine::ForCurrentProcess()->AppendSwitchPath( |
| 220 switches::kTraceConfigFile, trace_config_file); |
| 221 |
| 222 EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled()); |
| 223 } |
| 224 |
| 225 } // namespace tracing |
OLD | NEW |