OLD | NEW |
1 // Copyright 2009, Google Inc. | 1 // Copyright 2009, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 18 matching lines...) Expand all Loading... |
29 | 29 |
30 #include <windows.h> | 30 #include <windows.h> |
31 #include <dbghelp.h> | 31 #include <dbghelp.h> |
32 #include <strsafe.h> | 32 #include <strsafe.h> |
33 #include <objbase.h> | 33 #include <objbase.h> |
34 #include <shellapi.h> | 34 #include <shellapi.h> |
35 | 35 |
36 #include "../../../breakpad_googletest_includes.h" | 36 #include "../../../breakpad_googletest_includes.h" |
37 #include "../crash_generation/crash_generation_server.h" | 37 #include "../crash_generation/crash_generation_server.h" |
38 #include "../handler/exception_handler.h" | 38 #include "../handler/exception_handler.h" |
| 39 #include "dump_analysis.h" // NOLINT |
39 | 40 |
40 namespace { | 41 namespace { |
41 const wchar_t kPipeName[] = L"\\\\.\\pipe\\BreakpadCrashTest\\TestCaseServer"; | 42 const wchar_t kPipeName[] = L"\\\\.\\pipe\\BreakpadCrashTest\\TestCaseServer"; |
42 const char kSuccessIndicator[] = "success"; | 43 const char kSuccessIndicator[] = "success"; |
43 const char kFailureIndicator[] = "failure"; | 44 const char kFailureIndicator[] = "failure"; |
44 | 45 |
45 // Utility function to test for a path's existence. | 46 const MINIDUMP_TYPE kFullDumpType = static_cast<MINIDUMP_TYPE>( |
46 BOOL DoesPathExist(const TCHAR *path_name); | 47 MiniDumpWithFullMemory | // Full memory from process. |
| 48 MiniDumpWithProcessThreadData | // Get PEB and TEB. |
| 49 MiniDumpWithHandleData); // Get all handle information. |
47 | 50 |
48 class ExceptionHandlerDeathTest : public ::testing::Test { | 51 class ExceptionHandlerTest : public ::testing::Test { |
49 protected: | 52 protected: |
50 // Member variable for each test that they can use | 53 // Member variable for each test that they can use |
51 // for temporary storage. | 54 // for temporary storage. |
52 TCHAR temp_path_[MAX_PATH]; | 55 TCHAR temp_path_[MAX_PATH]; |
| 56 |
53 // Actually constructs a temp path name. | 57 // Actually constructs a temp path name. |
54 virtual void SetUp(); | 58 virtual void SetUp(); |
55 // A helper method that tests can use to crash. | 59 |
| 60 // Deletes temporary files. |
| 61 virtual void TearDown(); |
| 62 |
56 void DoCrash(); | 63 void DoCrash(); |
| 64 |
| 65 // Utility function to test for a path's existence. |
| 66 static BOOL DoesPathExist(const TCHAR *path_name); |
| 67 |
| 68 // Client callback. |
| 69 static void ClientDumpCallback( |
| 70 void *dump_context, |
| 71 const google_breakpad::ClientInfo *client_info, |
| 72 const std::wstring *dump_path); |
| 73 |
| 74 static std::wstring dump_file; |
| 75 static std::wstring full_dump_file; |
57 }; | 76 }; |
58 | 77 |
59 void ExceptionHandlerDeathTest::SetUp() { | 78 std::wstring ExceptionHandlerTest::dump_file; |
| 79 std::wstring ExceptionHandlerTest::full_dump_file; |
| 80 |
| 81 void ExceptionHandlerTest::SetUp() { |
60 const ::testing::TestInfo* const test_info = | 82 const ::testing::TestInfo* const test_info = |
61 ::testing::UnitTest::GetInstance()->current_test_info(); | 83 ::testing::UnitTest::GetInstance()->current_test_info(); |
62 TCHAR temp_path[MAX_PATH] = { '\0' }; | 84 TCHAR temp_path[MAX_PATH] = { '\0' }; |
63 TCHAR test_name_wide[MAX_PATH] = { '\0' }; | 85 TCHAR test_name_wide[MAX_PATH] = { '\0' }; |
64 // We want the temporary directory to be what the OS returns | 86 // We want the temporary directory to be what the OS returns |
65 // to us, + the test case name. | 87 // to us, + the test case name. |
66 GetTempPath(MAX_PATH, temp_path); | 88 GetTempPath(MAX_PATH, temp_path); |
67 // THe test case name is exposed to use as a c-style string, | 89 // THe test case name is exposed to use as a c-style string, |
68 // But we might be working in UNICODE here on Windows. | 90 // But we might be working in UNICODE here on Windows. |
69 int dwRet = MultiByteToWideChar(CP_ACP, 0, test_info->name(), | 91 int dwRet = MultiByteToWideChar(CP_ACP, 0, test_info->name(), |
70 strlen(test_info->name()), | 92 strlen(test_info->name()), |
71 test_name_wide, | 93 test_name_wide, |
72 MAX_PATH); | 94 MAX_PATH); |
73 if (!dwRet) { | 95 if (!dwRet) { |
74 assert(false); | 96 assert(false); |
75 } | 97 } |
76 StringCchPrintfW(temp_path_, MAX_PATH, L"%s%s", temp_path, test_name_wide); | 98 StringCchPrintfW(temp_path_, MAX_PATH, L"%s%s", temp_path, test_name_wide); |
77 CreateDirectory(temp_path_, NULL); | 99 CreateDirectory(temp_path_, NULL); |
78 } | 100 } |
79 | 101 |
80 BOOL DoesPathExist(const TCHAR *path_name) { | 102 void ExceptionHandlerTest::TearDown() { |
| 103 if (!dump_file.empty()) { |
| 104 ::DeleteFile(dump_file.c_str()); |
| 105 dump_file = L""; |
| 106 } |
| 107 if (!full_dump_file.empty()) { |
| 108 ::DeleteFile(full_dump_file.c_str()); |
| 109 full_dump_file = L""; |
| 110 } |
| 111 } |
| 112 |
| 113 BOOL ExceptionHandlerTest::DoesPathExist(const TCHAR *path_name) { |
81 DWORD flags = GetFileAttributes(path_name); | 114 DWORD flags = GetFileAttributes(path_name); |
82 if (flags == INVALID_FILE_ATTRIBUTES) { | 115 if (flags == INVALID_FILE_ATTRIBUTES) { |
83 return FALSE; | 116 return FALSE; |
84 } | 117 } |
85 return TRUE; | 118 return TRUE; |
86 } | 119 } |
87 | 120 |
88 bool MinidumpWrittenCallback(const wchar_t* dump_path, | 121 void ExceptionHandlerTest::ClientDumpCallback( |
89 const wchar_t* minidump_id, | 122 void *dump_context, |
90 void* context, | 123 const google_breakpad::ClientInfo *client_info, |
91 EXCEPTION_POINTERS* exinfo, | 124 const std::wstring *dump_path) { |
92 MDRawAssertionInfo* assertion, | 125 dump_file = *dump_path; |
93 bool succeeded) { | 126 // Create the full dump file name from the dump path. |
94 if (succeeded && DoesPathExist(dump_path)) { | 127 full_dump_file = dump_file.substr(0, dump_file.length() - 4) + L"-full.dmp"; |
95 fprintf(stderr, kSuccessIndicator); | |
96 } else { | |
97 fprintf(stderr, kFailureIndicator); | |
98 } | |
99 // If we don't flush, the output doesn't get sent before | |
100 // this process dies. | |
101 fflush(stderr); | |
102 return succeeded; | |
103 } | 128 } |
104 | 129 |
105 TEST_F(ExceptionHandlerDeathTest, InProcTest) { | 130 void ExceptionHandlerTest::DoCrash() { |
106 // For the in-proc test, we just need to instantiate an exception | |
107 // handler in in-proc mode, and crash. Since the entire test is | |
108 // reexecuted in the child process, we don't have to worry about | |
109 // the semantics of the exception handler being inherited/not | |
110 // inherited across CreateProcess(). | |
111 ASSERT_TRUE(DoesPathExist(temp_path_)); | |
112 google_breakpad::ExceptionHandler *exc = | 131 google_breakpad::ExceptionHandler *exc = |
113 new google_breakpad::ExceptionHandler( | 132 new google_breakpad::ExceptionHandler( |
114 temp_path_, NULL, &MinidumpWrittenCallback, NULL, | 133 temp_path_, NULL, NULL, NULL, |
115 google_breakpad::ExceptionHandler::HANDLER_ALL); | 134 google_breakpad::ExceptionHandler::HANDLER_INVALID_PARAMETER, |
116 int *i = NULL; | 135 kFullDumpType, kPipeName, NULL); |
117 ASSERT_DEATH((*i)++, kSuccessIndicator); | |
118 delete exc; | |
119 } | |
120 | 136 |
121 static bool gDumpCallbackCalled = false; | 137 // Disable the message box for assertions |
| 138 _CrtSetReportMode(_CRT_ASSERT, 0); |
122 | 139 |
123 void clientDumpCallback(void *dump_context, | |
124 const google_breakpad::ClientInfo *client_info, | |
125 const std::wstring *dump_path) { | |
126 gDumpCallbackCalled = true; | |
127 } | |
128 | |
129 void ExceptionHandlerDeathTest::DoCrash() { | |
130 google_breakpad::ExceptionHandler *exc = | |
131 new google_breakpad::ExceptionHandler( | |
132 temp_path_, NULL, NULL, NULL, | |
133 google_breakpad::ExceptionHandler::HANDLER_ALL, MiniDumpNormal, kPipeName, | |
134 NULL); | |
135 // Although this is executing in the child process of the death test, | 140 // Although this is executing in the child process of the death test, |
136 // if it's not true we'll still get an error rather than the crash | 141 // if it's not true we'll still get an error rather than the crash |
137 // being expected. | 142 // being expected. |
138 ASSERT_TRUE(exc->IsOutOfProcess()); | 143 ASSERT_TRUE(exc->IsOutOfProcess()); |
139 int *i = NULL; | 144 printf(NULL); |
140 printf("%d\n", (*i)++); | |
141 } | 145 } |
142 | 146 |
143 TEST_F(ExceptionHandlerDeathTest, OutOfProcTest) { | 147 // This test validates that the minidump is written correctly. |
144 // We can take advantage of a detail of google test here to save some | 148 TEST_F(ExceptionHandlerTest, InvalidParameterMiniDumpTest) { |
145 // complexity in testing: when you do a death test, it actually forks. | 149 ASSERT_TRUE(DoesPathExist(temp_path_)); |
146 // So we can make the main test harness the crash generation server, | |
147 // and call ASSERT_DEATH on a NULL dereference, it to expecting test | |
148 // the out of process scenario, since it's happening in a different | |
149 // process! This is different from the above because, above, we pass | |
150 // a NULL pipe name, and we also don't start a crash generation server. | |
151 | 150 |
| 151 // Call with a bad argument |
152 ASSERT_TRUE(DoesPathExist(temp_path_)); | 152 ASSERT_TRUE(DoesPathExist(temp_path_)); |
153 std::wstring dump_path(temp_path_); | 153 std::wstring dump_path(temp_path_); |
154 google_breakpad::CrashGenerationServer server( | 154 google_breakpad::CrashGenerationServer server( |
155 kPipeName, NULL, NULL, NULL, &clientDumpCallback, NULL, NULL, NULL, true, | 155 kPipeName, NULL, NULL, NULL, ClientDumpCallback, NULL, NULL, NULL, true, |
156 &dump_path); | 156 &dump_path); |
| 157 |
| 158 ASSERT_TRUE(dump_file.empty() && full_dump_file.empty()); |
157 | 159 |
158 // This HAS to be EXPECT_, because when this test case is executed in the | 160 // This HAS to be EXPECT_, because when this test case is executed in the |
159 // child process, the server registration will fail due to the named pipe | 161 // child process, the server registration will fail due to the named pipe |
160 // being the same. | 162 // being the same. |
161 EXPECT_TRUE(server.Start()); | 163 EXPECT_TRUE(server.Start()); |
162 EXPECT_FALSE(gDumpCallbackCalled); | 164 EXPECT_EXIT(this->DoCrash(), ::testing::ExitedWithCode(0), ""); |
163 ASSERT_DEATH(this->DoCrash(), ""); | 165 ASSERT_TRUE(!dump_file.empty() && !full_dump_file.empty()); |
164 EXPECT_TRUE(gDumpCallbackCalled); | 166 ASSERT_TRUE(DoesPathExist(dump_file.c_str())); |
| 167 |
| 168 // Verify the dump for infos. |
| 169 DumpAnalysis mini(dump_file); |
| 170 DumpAnalysis full(full_dump_file); |
| 171 |
| 172 // The dump should have all of these streams. |
| 173 EXPECT_TRUE(mini.HasStream(ThreadListStream)); |
| 174 EXPECT_TRUE(full.HasStream(ThreadListStream)); |
| 175 EXPECT_TRUE(mini.HasStream(ModuleListStream)); |
| 176 EXPECT_TRUE(full.HasStream(ModuleListStream)); |
| 177 EXPECT_TRUE(mini.HasStream(ExceptionStream)); |
| 178 EXPECT_TRUE(full.HasStream(ExceptionStream)); |
| 179 EXPECT_TRUE(mini.HasStream(SystemInfoStream)); |
| 180 EXPECT_TRUE(full.HasStream(SystemInfoStream)); |
| 181 EXPECT_TRUE(mini.HasStream(MiscInfoStream)); |
| 182 EXPECT_TRUE(full.HasStream(MiscInfoStream)); |
| 183 EXPECT_TRUE(mini.HasStream(HandleDataStream)); |
| 184 EXPECT_TRUE(full.HasStream(HandleDataStream)); |
| 185 |
| 186 // We expect PEB and TEBs in this dump. |
| 187 EXPECT_TRUE(mini.HasTebs() || full.HasTebs()); |
| 188 EXPECT_TRUE(mini.HasPeb() || full.HasPeb()); |
| 189 |
| 190 // Minidump should have a memory listing, but no 64-bit memory. |
| 191 EXPECT_TRUE(mini.HasStream(MemoryListStream)); |
| 192 EXPECT_FALSE(mini.HasStream(Memory64ListStream)); |
| 193 |
| 194 EXPECT_FALSE(full.HasStream(MemoryListStream)); |
| 195 EXPECT_TRUE(full.HasStream(Memory64ListStream)); |
| 196 |
| 197 // This is the only place we don't use OR because we want both not |
| 198 // to have the streams. |
| 199 EXPECT_FALSE(mini.HasStream(ThreadExListStream)); |
| 200 EXPECT_FALSE(full.HasStream(ThreadExListStream)); |
| 201 EXPECT_FALSE(mini.HasStream(CommentStreamA)); |
| 202 EXPECT_FALSE(full.HasStream(CommentStreamA)); |
| 203 EXPECT_FALSE(mini.HasStream(CommentStreamW)); |
| 204 EXPECT_FALSE(full.HasStream(CommentStreamW)); |
| 205 EXPECT_FALSE(mini.HasStream(FunctionTableStream)); |
| 206 EXPECT_FALSE(full.HasStream(FunctionTableStream)); |
| 207 EXPECT_FALSE(mini.HasStream(MemoryInfoListStream)); |
| 208 EXPECT_FALSE(full.HasStream(MemoryInfoListStream)); |
| 209 EXPECT_FALSE(mini.HasStream(ThreadInfoListStream)); |
| 210 EXPECT_FALSE(full.HasStream(ThreadInfoListStream)); |
| 211 EXPECT_FALSE(mini.HasStream(HandleOperationListStream)); |
| 212 EXPECT_FALSE(full.HasStream(HandleOperationListStream)); |
| 213 EXPECT_FALSE(mini.HasStream(TokenStream)); |
| 214 EXPECT_FALSE(full.HasStream(TokenStream)); |
165 } | 215 } |
166 } | 216 } |
OLD | NEW |