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 : log_file_path_ = temp_dir_.path().Append(L"log.txt");
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 : ASSERT_TRUE(log_service_.Start());
53 :
54 E : log_contents_read_ = false;
55 E : }
56 :
57 E : void TestWithAsanLogger::TearDown() {
58 E : log_service_.Stop();
59 E : log_service_.Join();
60 E : log_file_.reset(NULL);
61 E : LogContains("");
62 E : }
63 :
64 E : bool TestWithAsanLogger::LogContains(const base::StringPiece& message) {
65 E : if (!log_contents_read_ && log_file_.get() != NULL) {
66 E : std::string contents;
67 E : CHECK(file_util::ReadFileToString(log_file_path_, &log_contents_));
68 E : }
69 E : return log_contents_.find(message.as_string()) != std::string::npos;
70 E : }
71 :
72 E : void TestWithAsanLogger::DeleteTempFileAndDirectory() {
73 E : log_file_.reset();
74 E : if (temp_dir_.IsValid())
75 E : temp_dir_.Delete();
76 E : }
77 :
78 : } // namespace testing
|