Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(456)

Side by Side Diff: remoting/base/breakpad_win_unittest.cc

Issue 10535082: /C++ readability/ - Make Chromoting Host report crashes to Breakpad (Windows only). The user must e… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stdio.h>
6
7 #include "base/compiler_specific.h"
8 #include "base/environment.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/string16.h"
12 #include "base/stringprintf.h"
13 #include "base/utf_string_conversions.h"
14 #include "breakpad/src/client/windows/crash_generation/client_info.h"
15 #include "breakpad/src/client/windows/crash_generation/crash_generation_server.h "
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace remoting {
Peter Kasting 2012/06/14 20:16:27 Nit: Does the anonymous namespace need to be insid
alexeypa (please no reviews) 2012/06/15 18:45:49 Done.
20
21 namespace {
22
23 // The name of the environment variable used to pass the crash server pipe name
24 // to the crashing child process.
25 const char kPipeVariableName[] = "REMOTING_BREAKPAD_WIN_DEATH_TEST_PIPE_NAME";
26
27 // The prefix string used to generate a unique crash server pipe name.
28 // The name has to be unique as multiple test instances can be running
29 // simultaneously.
30 const wchar_t kPipeNamePrefix[] = L"\\\\.\\pipe\\";
31
32 class MockCrashServerCallbacks {
33 public:
34 MockCrashServerCallbacks() {}
35 virtual ~MockCrashServerCallbacks() {}
Peter Kasting 2012/06/14 20:16:27 Nit: Even in tests, try to avoid inlining function
alexeypa (please no reviews) 2012/06/15 18:45:49 Done. I removed inlining of the constructor as wel
36
37 // |google_breakpad::CrashGenerationServer| invokes callbacks from artitrary
38 // thread pool threads. |OnClientDumpRequested| is the only one that happened
39 // to be called in synchronous manner. While it is still called on
40 // a thread pool thread, the crashing process will wait until the server
41 // signals an event after |OnClientDumpRequested| completes (or until 15
42 // seconds timeout expires).
43 MOCK_METHOD0(OnClientDumpRequested, void());
44
45 static void OnClientDumpRequestCallback(
46 void* context,
47 const google_breakpad::ClientInfo* client_info,
48 const string16* file_path) {
49 reinterpret_cast<MockCrashServerCallbacks*>(context)->
50 OnClientDumpRequested();
51 }
52 };
53
54 } // namespace
55
56 void InitializeCrashReportingForTest(const wchar_t*);
57
58 class BreakpadWinDeathTest : public testing::Test {
59 public:
60 BreakpadWinDeathTest() {}
61 virtual void SetUp() OVERRIDE {
62 scoped_ptr<base::Environment> environment(base::Environment::Create());
63 std::string pipe_name;
64 if (environment->GetVar(kPipeVariableName, &pipe_name)) {
65 // This is a child process. Initialize crash dump reporting to the crash
66 // dump server.
67 pipe_name_ = UTF8ToUTF16(pipe_name);
Peter Kasting 2012/06/14 20:16:27 Use UTF8ToWide() here (see below).
alexeypa (please no reviews) 2012/06/15 18:45:49 Done.
68 ::remoting::InitializeCrashReportingForTest(pipe_name_.c_str());
69 } else {
70 // This is the parent process. Generate a unique pipe name and setup
71 // a dummy crash dump server.
72 UUID guid = {0};
73 RPC_STATUS status = ::UuidCreate(&guid);
Peter Kasting 2012/06/14 20:16:27 Nit: Is the :: really necessary?
alexeypa (please no reviews) 2012/06/15 18:45:49 Done.
74 EXPECT_TRUE(status == RPC_S_OK || status == RPC_S_UUID_LOCAL_ONLY);
75
76 pipe_name_ = kPipeNamePrefix +
Peter Kasting 2012/06/14 20:16:27 Nit: If using StringPrintf() anyway, add a "%s" to
alexeypa (please no reviews) 2012/06/15 18:45:49 Done.
77 base::StringPrintf(
78 L"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
79 guid.Data1, guid.Data2, guid.Data3,
80 guid.Data4[0], guid.Data4[1], guid.Data4[2],
81 guid.Data4[3], guid.Data4[4], guid.Data4[5],
82 guid.Data4[6], guid.Data4[7]);
83 bool result = environment->SetVar(kPipeVariableName,
84 UTF16ToUTF8(pipe_name_));
Peter Kasting 2012/06/14 20:16:27 Use WideToUTF8 here (see below).
alexeypa (please no reviews) 2012/06/15 18:45:49 Done.
85 EXPECT_TRUE(result);
Peter Kasting 2012/06/14 20:16:27 Nit: Unlike with DCHECK(), it's OK to directly wra
alexeypa (please no reviews) 2012/06/15 18:45:49 Done.
86
87 // Setup a dummy crash dump server.
88 callbacks_.reset(new MockCrashServerCallbacks());
89 crash_server_.reset(
90 new google_breakpad::CrashGenerationServer(
91 pipe_name_, NULL,
Peter Kasting 2012/06/14 20:16:27 Nit: Strange wrapping here. More common choices a
alexeypa (please no reviews) 2012/06/15 18:45:49 Done.
92 NULL, 0,
93 MockCrashServerCallbacks::OnClientDumpRequestCallback,
94 callbacks_.get(),
95 NULL, 0,
96 false, NULL));
97
98 result = crash_server_->Start();
99 ASSERT_TRUE(result);
100 }
101 }
102
103 protected:
104 scoped_ptr<google_breakpad::CrashGenerationServer> crash_server_;
105 scoped_ptr<MockCrashServerCallbacks> callbacks_;
106 string16 pipe_name_;
Peter Kasting 2012/06/14 20:16:27 This should be a wstring.
alexeypa (please no reviews) 2012/06/15 18:45:49 Done.
107 };
108
109 TEST_F(BreakpadWinDeathTest, TestAccessViolation) {
110 if (callbacks_.get()) {
111 EXPECT_CALL(*callbacks_, OnClientDumpRequested());
112 }
113
114 // Generate access violation exception.
115 ASSERT_DEATH(*reinterpret_cast<int*>(0) = 1, "");
Peter Kasting 2012/06/14 20:16:27 Nit: 0 -> NULL?
alexeypa (please no reviews) 2012/06/15 18:45:49 Done.
116 }
117
118 TEST_F(BreakpadWinDeathTest, TestInvalidParameter) {
119 if (callbacks_.get()) {
120 EXPECT_CALL(*callbacks_, OnClientDumpRequested());
121 }
122
123 // Cause the invalid parameter callback to be called.
124 ASSERT_EXIT(printf(NULL), ::testing::ExitedWithCode(0), "");
125 }
126
127 TEST_F(BreakpadWinDeathTest, TestDebugbreak) {
128 if (callbacks_.get()) {
129 EXPECT_CALL(*callbacks_, OnClientDumpRequested());
130 }
131
132 // See if __debugbreak() is intercepted.
133 ASSERT_DEATH(__debugbreak(), "");
134 }
135
136 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698