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/stringprintf.h"
22 : #include "base/utf_string_conversions.h"
23 : #include "syzygy/agent/asan/asan_runtime.h"
24 : #include "syzygy/trace/protocol/call_trace_defs.h"
25 :
26 : namespace testing {
27 :
28 : TestWithAsanLogger::TestWithAsanLogger()
29 E : : log_service_instance_(&log_service_), log_contents_read_(false) {
30 E : }
31 :
32 E : void TestWithAsanLogger::SetUp() {
33 : // Create and open the log file.
34 E : ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
35 E : CHECK(file_util::CreateTemporaryFileInDir(temp_dir_.path(), &log_file_path_));
36 E : log_file_.reset(file_util::OpenFile(log_file_path_, "wb"));
37 :
38 : // Configure the environment (to pass the instance id to the agent DLL).
39 E : std::string instance_id;
40 E : scoped_ptr<base::Environment> env(base::Environment::Create());
41 E : env->GetVar(kSyzygyRpcInstanceIdEnvVar, &instance_id);
42 : instance_id.append(base::StringPrintf(";%ls,%u",
43 : agent::asan::AsanRuntime::SyzyAsanDll(),
44 E : ::GetCurrentProcessId()));
45 E : env->SetVar(kSyzygyRpcInstanceIdEnvVar, instance_id);
46 :
47 : // Configure and start the log service.
48 E : instance_id_ = base::UintToString16(::GetCurrentProcessId());
49 E : log_service_.set_instance_id(instance_id_);
50 E : log_service_.set_destination(log_file_.get());
51 E : log_service_.set_minidump_dir(temp_dir_.path());
52 E : log_service_.set_symbolize_stack_traces(false);
53 E : ASSERT_TRUE(log_service_.Start());
54 :
55 E : log_contents_read_ = false;
56 E : }
57 :
58 E : void TestWithAsanLogger::TearDown() {
59 E : log_service_.Stop();
60 E : log_service_.Join();
61 E : log_file_.reset(NULL);
62 E : LogContains("");
63 E : }
64 :
65 E : bool TestWithAsanLogger::LogContains(const base::StringPiece& message) {
66 E : if (!log_contents_read_ && log_file_.get() != NULL) {
67 E : CHECK(file_util::ReadFileToString(log_file_path_, &log_contents_));
68 E : log_contents_read_ = true;
69 : }
70 E : return log_contents_.find(message.as_string()) != std::string::npos;
71 E : }
72 :
73 E : void TestWithAsanLogger::DeleteTempFileAndDirectory() {
74 E : log_file_.reset();
75 E : if (temp_dir_.IsValid())
76 E : temp_dir_.Delete();
77 E : }
78 :
79 E : void TestWithAsanLogger::ResetLog() {
80 E : DCHECK(log_file_.get() != NULL);
81 E : CHECK(file_util::CreateTemporaryFileInDir(temp_dir_.path(), &log_file_path_));
82 E : file_util::ScopedFILE log_file(file_util::OpenFile(log_file_path_, "wb"));
83 E : log_service_.set_destination(log_file.get());
84 E : log_file_.reset(log_file.release());
85 E : log_contents_read_ = false;
86 E : }
87 :
88 : } // namespace testing
|