1 // Copyright 2016 The Chromium Embedded Framework Authors. Postions copyright
2 // 2012 The Chromium Authors. All rights reserved. Use of this source code is
3 // governed by a BSD-style license that can be found in the LICENSE file.
4
5 #include "tests/ceftests/test_suite.h"
6
7 #include "include/cef_file_util.h"
8 #include "include/wrapper/cef_scoped_temp_dir.h"
9 #include "tests/gtest/include/gtest/gtest.h"
10 #include "tests/shared/common/client_switches.h"
11
12 namespace {
13
14 CefTestSuite* g_test_suite = nullptr;
15
16 #if defined(OS_WIN)
17
18 // From base/process/launch_win.cc.
RouteStdioToConsole(bool create_console_if_not_found)19 void RouteStdioToConsole(bool create_console_if_not_found) {
20 // Don't change anything if stdout or stderr already point to a
21 // valid stream.
22 //
23 // If we are running under Buildbot or under Cygwin's default
24 // terminal (mintty), stderr and stderr will be pipe handles. In
25 // that case, we don't want to open CONOUT$, because its output
26 // likely does not go anywhere.
27 //
28 // We don't use GetStdHandle() to check stdout/stderr here because
29 // it can return dangling IDs of handles that were never inherited
30 // by this process. These IDs could have been reused by the time
31 // this function is called. The CRT checks the validity of
32 // stdout/stderr on startup (before the handle IDs can be reused).
33 // _fileno(stdout) will return -2 (_NO_CONSOLE_FILENO) if stdout was
34 // invalid.
35 if (_fileno(stdout) >= 0 || _fileno(stderr) >= 0) {
36 return;
37 }
38
39 if (!AttachConsole(ATTACH_PARENT_PROCESS)) {
40 unsigned int result = GetLastError();
41 // Was probably already attached.
42 if (result == ERROR_ACCESS_DENIED)
43 return;
44 // Don't bother creating a new console for each child process if the
45 // parent process is invalid (eg: crashed).
46 if (result == ERROR_GEN_FAILURE)
47 return;
48 if (create_console_if_not_found) {
49 // Make a new console if attaching to parent fails with any other error.
50 // It should be ERROR_INVALID_HANDLE at this point, which means the
51 // browser was likely not started from a console.
52 AllocConsole();
53 } else {
54 return;
55 }
56 }
57
58 // Arbitrary byte count to use when buffering output lines. More
59 // means potential waste, less means more risk of interleaved
60 // log-lines in output.
61 enum { kOutputBufferSize = 64 * 1024 };
62
63 if (freopen("CONOUT$", "w", stdout)) {
64 setvbuf(stdout, nullptr, _IOLBF, kOutputBufferSize);
65 // Overwrite FD 1 for the benefit of any code that uses this FD
66 // directly. This is safe because the CRT allocates FDs 0, 1 and
67 // 2 at startup even if they don't have valid underlying Windows
68 // handles. This means we won't be overwriting an FD created by
69 // _open() after startup.
70 _dup2(_fileno(stdout), 1);
71 }
72 if (freopen("CONOUT$", "w", stderr)) {
73 setvbuf(stderr, nullptr, _IOLBF, kOutputBufferSize);
74 _dup2(_fileno(stderr), 2);
75 }
76
77 // Fix all cout, wcout, cin, wcin, cerr, wcerr, clog and wclog.
78 std::ios::sync_with_stdio();
79 }
80
81 #endif // defined(OS_WIN)
82
83 } // namespace
84
CefTestSuite(int argc,char ** argv)85 CefTestSuite::CefTestSuite(int argc, char** argv)
86 : argc_(argc), argv_(argc, argv), retval_(0) {
87 g_test_suite = this;
88
89 // Keep a representation of the original command-line.
90 command_line_ = CefCommandLine::CreateCommandLine();
91 #if defined(OS_WIN)
92 command_line_->InitFromString(::GetCommandLineW());
93 #else
94 command_line_->InitFromArgv(argc, argv);
95 #endif
96
97 if (!command_line_->HasSwitch("type")) {
98 // Initialize in the main process only.
99 root_cache_path_ =
100 command_line_->GetSwitchValue(client::switches::kCachePath);
101 if (root_cache_path_.empty()) {
102 CefScopedTempDir temp_dir;
103 CHECK(temp_dir.CreateUniqueTempDir());
104 root_cache_path_ = temp_dir.Take();
105 RegisterTempDirectory(root_cache_path_);
106 }
107 }
108 }
109
~CefTestSuite()110 CefTestSuite::~CefTestSuite() {
111 g_test_suite = nullptr;
112 }
113
114 // static
GetInstance()115 CefTestSuite* CefTestSuite::GetInstance() {
116 return g_test_suite;
117 }
118
InitMainProcess()119 void CefTestSuite::InitMainProcess() {
120 PreInitialize();
121
122 // This will modify |argc_| and |argv_|.
123 testing::InitGoogleTest(&argc_, argv_.array());
124 }
125
126 // Don't add additional code to this method. Instead add it to Initialize().
Run()127 int CefTestSuite::Run() {
128 Initialize();
129 retval_ = RUN_ALL_TESTS();
130 Shutdown();
131 return retval_;
132 }
133
GetSettings(CefSettings & settings) const134 void CefTestSuite::GetSettings(CefSettings& settings) const {
135 // Enable the experimental Chrome runtime. See issue #2969 for details.
136 settings.chrome_runtime =
137 command_line_->HasSwitch(client::switches::kEnableChromeRuntime);
138
139 CefString(&settings.cache_path) = root_cache_path_;
140 CefString(&settings.root_cache_path) = root_cache_path_;
141 CefString(&settings.user_data_path) = root_cache_path_;
142
143 // Always expose the V8 gc() function to give tests finer-grained control over
144 // memory management.
145 std::string javascript_flags = "--expose-gc";
146 // Value of kJavascriptFlags switch.
147 std::string other_javascript_flags =
148 command_line_->GetSwitchValue("js-flags");
149 if (!other_javascript_flags.empty())
150 javascript_flags += " " + other_javascript_flags;
151 CefString(&settings.javascript_flags) = javascript_flags;
152
153 // Necessary for V8Test.OnUncaughtException tests.
154 settings.uncaught_exception_stack_size = 10;
155
156 // Necessary for the OSRTest tests.
157 settings.windowless_rendering_enabled = true;
158
159 // For Accept-Language test
160 CefString(&settings.accept_language_list) = CEF_SETTINGS_ACCEPT_LANGUAGE;
161 }
162
RegisterTempDirectory(const CefString & directory)163 void CefTestSuite::RegisterTempDirectory(const CefString& directory) {
164 base::AutoLock lock_scope(temp_directories_lock_);
165 temp_directories_.push_back(directory);
166 }
167
DeleteTempDirectories()168 void CefTestSuite::DeleteTempDirectories() {
169 base::AutoLock lock_scope(temp_directories_lock_);
170 for (size_t i = 0U; i < temp_directories_.size(); ++i) {
171 CefDeleteFile(temp_directories_[i], true);
172 }
173 temp_directories_.clear();
174 }
175
PreInitialize()176 void CefTestSuite::PreInitialize() {
177 #if defined(OS_WIN)
178 testing::GTEST_FLAG(catch_exceptions) = false;
179
180 // Enable termination on heap corruption.
181 // Ignore the result code. Supported starting with XP SP3 and Vista.
182 HeapSetInformation(nullptr, HeapEnableTerminationOnCorruption, nullptr, 0);
183 #endif
184
185 #if defined(OS_LINUX)
186 // When calling native char conversion functions (e.g wrctomb) we need to
187 // have the locale set. In the absence of such a call the "C" locale is the
188 // default. In the gtk code (below) gtk_init() implicitly sets a locale.
189 setlocale(LC_ALL, "");
190 #endif // defined(OS_LINUX)
191
192 // Don't add additional code to this function. Instead add it to Initialize().
193 }
194
Initialize()195 void CefTestSuite::Initialize() {
196 #if defined(OS_WIN)
197 RouteStdioToConsole(true);
198 #endif // defined(OS_WIN)
199 }
200
Shutdown()201 void CefTestSuite::Shutdown() {}
202