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 : // Common unittest fixtures and utilities for the ASAN runtime library.
16 :
17 : #include "syzygy/agent/asan/unittest_util.h"
18 :
19 : #include "base/environment.h"
20 : #include "base/string_number_conversions.h"
21 : #include "base/utf_string_conversions.h"
22 : #include "syzygy/trace/protocol/call_trace_defs.h"
23 :
24 : namespace testing {
25 :
26 : TestWithAsanLogger::TestWithAsanLogger()
27 E : : log_service_instance_(&log_service_), log_contents_read_(false) {
28 E : }
29 :
30 E : void TestWithAsanLogger::SetUp() {
31 : // Create and open the log file.
32 E : ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
33 E : log_file_path_ = temp_dir_.path().Append(L"log.txt");
34 E : log_file_.reset(file_util::OpenFile(log_file_path_, "wb"));
35 :
36 : // Configure the environment (to pass the instance id to the agent DLL).
37 E : std::string instance_id = base::UintToString(::GetCurrentProcessId());
38 E : scoped_ptr<base::Environment> env(base::Environment::Create());
39 E : env->SetVar(kSyzygyRpcInstanceIdEnvVar, instance_id);
40 :
41 : // Configure and start the log service.
42 E : instance_id_ = UTF8ToWide(instance_id);
43 E : log_service_.set_instance_id(instance_id_);
44 E : log_service_.set_destination(log_file_.get());
45 E : ASSERT_TRUE(log_service_.Start());
46 :
47 E : log_contents_read_ = false;
48 E : }
49 :
50 E : void TestWithAsanLogger::TearDown() {
51 E : log_service_.Stop();
52 E : log_service_.RunToCompletion();
53 E : log_file_.reset(NULL);
54 E : LogContains("");
55 E : }
56 :
57 E : bool TestWithAsanLogger::LogContains(const base::StringPiece& message) {
58 E : if (!log_contents_read_) {
59 E : std::string contents;
60 E : CHECK(file_util::ReadFileToString(log_file_path_, &log_contents_));
61 E : }
62 E : return log_contents_.find(message.as_string()) != std::string::npos;
63 E : }
64 :
65 : } // namespace testing
|