OLD | NEW |
| (Empty) |
1 // Copyright 2009, Google Inc. | |
2 // All rights reserved. | |
3 // | |
4 // Redistribution and use in source and binary forms, with or without | |
5 // modification, are permitted provided that the following conditions are | |
6 // met: | |
7 // | |
8 // * Redistributions of source code must retain the above copyright | |
9 // notice, this list of conditions and the following disclaimer. | |
10 // * Redistributions in binary form must reproduce the above | |
11 // copyright notice, this list of conditions and the following disclaimer | |
12 // in the documentation and/or other materials provided with the | |
13 // distribution. | |
14 // * Neither the name of Google Inc. nor the names of its | |
15 // contributors may be used to endorse or promote products derived from | |
16 // this software without specific prior written permission. | |
17 // | |
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | |
30 #include "breakpad_googletest_includes.h" | |
31 #include "client/windows/crash_generation/crash_generation_server.h" | |
32 #include "client/windows/handler/exception_handler.h" | |
33 #include <windows.h> | |
34 #include <dbghelp.h> | |
35 #include <strsafe.h> | |
36 #include <objbase.h> | |
37 #include <shellapi.h> | |
38 | |
39 namespace { | |
40 const wchar_t kPipeName[] = L"\\\\.\\pipe\\BreakpadCrashTest\\TestCaseServer"; | |
41 const char kSuccessIndicator[] = "success"; | |
42 const char kFailureIndicator[] = "failure"; | |
43 | |
44 // Utility function to test for a path's existence. | |
45 BOOL DoesPathExist(const TCHAR *path_name); | |
46 | |
47 class ExceptionHandlerDeathTest : public ::testing::Test { | |
48 protected: | |
49 // Member variable for each test that they can use | |
50 // for temporary storage. | |
51 TCHAR temp_path_[MAX_PATH]; | |
52 // Actually constructs a temp path name. | |
53 virtual void SetUp(); | |
54 // A helper method that tests can use to crash. | |
55 void DoCrash(); | |
56 }; | |
57 | |
58 void ExceptionHandlerDeathTest::SetUp() { | |
59 const ::testing::TestInfo* const test_info = | |
60 ::testing::UnitTest::GetInstance()->current_test_info(); | |
61 TCHAR temp_path[MAX_PATH] = { '\0' }; | |
62 TCHAR test_name_wide[MAX_PATH] = { '\0' }; | |
63 // We want the temporary directory to be what the OS returns | |
64 // to us, + the test case name. | |
65 GetTempPath(MAX_PATH, temp_path); | |
66 // THe test case name is exposed to use as a c-style string, | |
67 // But we might be working in UNICODE here on Windows. | |
68 int dwRet = MultiByteToWideChar(CP_ACP, 0, test_info->name(), | |
69 (int)strlen(test_info->name()), test_name_wide, MAX_PATH); | |
70 if (!dwRet) { | |
71 assert(false); | |
72 } | |
73 StringCchPrintfW(temp_path_, MAX_PATH, L"%s%s", temp_path, test_name_wide); | |
74 CreateDirectory(temp_path_, NULL); | |
75 } | |
76 | |
77 BOOL DoesPathExist(const TCHAR *path_name) { | |
78 DWORD flags = GetFileAttributes(path_name); | |
79 if (flags == INVALID_FILE_ATTRIBUTES) { | |
80 return FALSE; | |
81 } | |
82 return TRUE; | |
83 } | |
84 | |
85 bool MinidumpWrittenCallback(const wchar_t* dump_path, | |
86 const wchar_t* minidump_id, | |
87 void* context, | |
88 EXCEPTION_POINTERS* exinfo, | |
89 MDRawAssertionInfo* assertion, | |
90 bool succeeded) { | |
91 if (succeeded && DoesPathExist(dump_path)) { | |
92 fprintf(stderr, kSuccessIndicator); | |
93 } else { | |
94 fprintf(stderr, kFailureIndicator); | |
95 } | |
96 // If we don't flush, the output doesn't get sent before | |
97 // this process dies. | |
98 fflush(stderr); | |
99 return succeeded; | |
100 } | |
101 | |
102 TEST_F(ExceptionHandlerDeathTest, InProcTest) { | |
103 // For the in-proc test, we just need to instantiate an exception | |
104 // handler in in-proc mode, and crash. Since the entire test is | |
105 // reexecuted in the child process, we don't have to worry about | |
106 // the semantics of the exception handler being inherited/not | |
107 // inherited across CreateProcess(). | |
108 ASSERT_TRUE(DoesPathExist(temp_path_)); | |
109 google_breakpad::ExceptionHandler *exc = | |
110 new google_breakpad::ExceptionHandler( | |
111 temp_path_, NULL, &MinidumpWrittenCallback, NULL, | |
112 google_breakpad::ExceptionHandler::HANDLER_ALL); | |
113 int *i = NULL; | |
114 ASSERT_DEATH((*i)++, kSuccessIndicator); | |
115 delete exc; | |
116 } | |
117 | |
118 static bool gDumpCallbackCalled = false; | |
119 | |
120 void clientDumpCallback(void *dump_context, | |
121 const google_breakpad::ClientInfo *client_info, | |
122 const std::wstring *dump_path){ | |
123 | |
124 gDumpCallbackCalled = true; | |
125 } | |
126 | |
127 void ExceptionHandlerDeathTest::DoCrash() { | |
128 google_breakpad::ExceptionHandler *exc = | |
129 new google_breakpad::ExceptionHandler( | |
130 temp_path_, NULL, NULL, NULL, | |
131 google_breakpad::ExceptionHandler::HANDLER_ALL, MiniDumpNormal, kPipeName, | |
132 NULL); | |
133 // Although this is executing in the child process of the death test, | |
134 // if it's not true we'll still get an error rather than the crash | |
135 // being expected. | |
136 ASSERT_TRUE(exc->IsOutOfProcess()); | |
137 int *i = NULL; | |
138 printf("%d\n", (*i)++); | |
139 } | |
140 | |
141 TEST_F(ExceptionHandlerDeathTest, OutOfProcTest) { | |
142 // We can take advantage of a detail of google test here to save some | |
143 // complexity in testing: when you do a death test, it actually forks. | |
144 // So we can make the main test harness the crash generation server, | |
145 // and call ASSERT_DEATH on a NULL dereference, it to expecting test | |
146 // the out of process scenario, since it's happening in a different | |
147 // process! This is different from the above because, above, we pass | |
148 // a NULL pipe name, and we also don't start a crash generation server. | |
149 | |
150 ASSERT_TRUE(DoesPathExist(temp_path_)); | |
151 std::wstring dump_path(temp_path_); | |
152 google_breakpad::CrashGenerationServer server( | |
153 kPipeName, NULL, NULL, NULL, &clientDumpCallback, NULL, NULL, NULL, true, | |
154 &dump_path); | |
155 | |
156 // This HAS to be EXPECT_, because when this test case is executed in the | |
157 // child process, the server registration will fail due to the named pipe | |
158 // being the same. | |
159 EXPECT_TRUE(server.Start()); | |
160 EXPECT_FALSE(gDumpCallbackCalled); | |
161 ASSERT_DEATH(this->DoCrash(), ""); | |
162 EXPECT_TRUE(gDumpCallbackCalled); | |
163 } | |
164 } | |
OLD | NEW |