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(file_util::OpenFile(in_path, "r"));
29 E : out_.reset(file_util::OpenFile(out_path, "w"));
30 E : err_.reset(file_util::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 : EXPECT_TRUE(file_util::Delete(*iter, true));
76 E : }
77 :
78 E : Super::TearDown();
79 E : }
80 :
81 : bool ApplicationTestBase::HandleLogMessage(int severity, const char* file,
82 E : int line, size_t message_start, const std::string& str) {
83 E : DCHECK(self_ != NULL);
84 E : if (severity < logging::GetMinLogLevel())
85 i : return true;
86 E : fprintf(self_->err(), "%s", str.c_str());
87 E : fflush(self_->err());
88 :
89 : // If we're logging to console then repeat the message there.
90 E : if (self_->log_to_console_) {
91 i : fprintf(stdout, "%s", str.c_str());
92 i : fflush(stdout);
93 : }
94 :
95 E : return true;
96 E : }
97 :
98 E : void ApplicationTestBase::TearDownStream(file_util::ScopedFILE* stream) {
99 E : ASSERT_TRUE(stream != NULL);
100 E : if (stream->get() == NULL)
101 E : return;
102 E : ASSERT_EQ(0, ::fclose(stream->get()));
103 E : stream->reset();
104 E : }
105 :
106 : FILE* ApplicationTestBase::GetOrInitFile(file_util::ScopedFILE* f,
107 E : const char* mode) {
108 E : DCHECK(f != NULL);
109 E : DCHECK(mode != NULL);
110 E : if (f->get() == NULL)
111 E : f->reset(file_util::OpenFile(base::FilePath(L"NUL"), mode));
112 E : return f->get();
113 E : }
114 :
115 : } // namespace testing
|