1 : // Copyright 2013 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/common/unittest_util.h"
16 :
17 : namespace testing {
18 :
19 : ApplicationTestBase* ApplicationTestBase::self_ = NULL;
20 :
21 : void ApplicationTestBase::InitStreams(const base::FilePath& in_path,
22 : const base::FilePath& out_path,
23 E : const base::FilePath& err_path) {
24 E : ASSERT_FALSE(in_path.empty());
25 E : ASSERT_FALSE(out_path.empty());
26 E : ASSERT_FALSE(err_path.empty());
27 :
28 E : in_.reset(base::OpenFile(in_path, "r"));
29 E : out_.reset(base::OpenFile(out_path, "w"));
30 E : err_.reset(base::OpenFile(err_path, "w"));
31 :
32 E : ASSERT_TRUE(in_.get() != NULL);
33 E : ASSERT_TRUE(out_.get() != NULL);
34 E : ASSERT_TRUE(err_.get() != NULL);
35 :
36 : // Intercept logging.
37 E : ASSERT_TRUE(self_ == NULL);
38 E : ASSERT_TRUE(log_handler_ == NULL);
39 E : self_ = this;
40 E : log_handler_ = logging::GetLogMessageHandler();
41 E : logging::SetLogMessageHandler(&HandleLogMessage);
42 E : }
43 :
44 E : void ApplicationTestBase::TearDownStreams() {
45 E : if (self_ != NULL) {
46 E : logging::SetLogMessageHandler(log_handler_);
47 E : log_handler_ = NULL;
48 E : self_ = NULL;
49 : }
50 :
51 E : ASSERT_NO_FATAL_FAILURE(TearDownStream(&in_));
52 E : ASSERT_NO_FATAL_FAILURE(TearDownStream(&out_));
53 E : ASSERT_NO_FATAL_FAILURE(TearDownStream(&err_));
54 E : }
55 :
56 E : void ApplicationTestBase::SetUp() {
57 E : Super::SetUp();
58 :
59 : // Save the log level so that we can restore it in TearDown.
60 E : log_level_ = logging::GetMinLogLevel();
61 :
62 : // By default we don't log to console.
63 E : log_to_console_ = false;
64 E : }
65 :
66 E : void ApplicationTestBase::TearDown() {
67 E : logging::SetMinLogLevel(log_level_);
68 :
69 : // These need to be shut down before we can delete the temporary
70 : // directories.
71 E : EXPECT_NO_FATAL_FAILURE(TearDownStreams());
72 :
73 E : DirList::const_iterator iter;
74 E : for (iter = temp_dirs_.begin(); iter != temp_dirs_.end(); ++iter) {
75 E : bool success = base::DeleteFile(*iter, true);
76 : // VS2013 holds open handles to any PDB file that has been loaded while
77 : // the debugger is active. This often prevents our unittests from
78 : // cleaning up after themselves.
79 E : EXPECT_TRUE(success || ::IsDebuggerPresent());
80 E : }
81 :
82 E : Super::TearDown();
83 E : }
84 :
85 : bool ApplicationTestBase::HandleLogMessage(int severity, const char* file,
86 E : int line, size_t message_start, const std::string& str) {
87 E : DCHECK(self_ != NULL);
88 E : if (severity < logging::GetMinLogLevel())
89 i : return true;
90 E : fprintf(self_->err(), "%s", str.c_str());
91 E : fflush(self_->err());
92 :
93 : // If we're logging to console then repeat the message there.
94 E : if (self_->log_to_console_) {
95 i : fprintf(stdout, "%s", str.c_str());
96 i : fflush(stdout);
97 : }
98 :
99 E : return true;
100 E : }
101 :
102 E : void ApplicationTestBase::TearDownStream(base::ScopedFILE* stream) {
103 E : ASSERT_TRUE(stream != NULL);
104 E : if (stream->get() == NULL)
105 E : return;
106 E : ASSERT_EQ(0, ::fclose(stream->get()));
107 E : stream->reset();
108 E : }
109 :
110 : FILE* ApplicationTestBase::GetOrInitFile(base::ScopedFILE* f,
111 E : const char* mode) {
112 E : DCHECK(f != NULL);
113 E : DCHECK(mode != NULL);
114 E : if (f->get() == NULL)
115 E : f->reset(base::OpenFile(base::FilePath(L"NUL"), mode));
116 E : return f->get();
117 E : }
118 :
119 : } // namespace testing
|