forked from Outerra/crashrpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrproberTests.cpp
231 lines (180 loc) · 5.89 KB
/
CrproberTests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*************************************************************************************
This file is a part of CrashRpt library.
Copyright (c) 2003-2013 The CrashRpt project authors. All Rights Reserved.
Use of this source code is governed by a BSD-style license
that can be found in the License.txt file in the root of the source
tree. All contributing project authors may
be found in the Authors.txt file in the root of the source tree.
***************************************************************************************/
#include "stdafx.h"
#include "Tests.h"
#include "CrashRpt.h"
#include "Utility.h"
#include "strconv.h"
#include "Shellapi.h"
#include "TestUtils.h"
class CrproberTests : public CTestSuite
{
BEGIN_TEST_MAP(CrproberTests, "crprober.exe tests")
REGISTER_TEST(Test_help)
REGISTER_TEST(Test_output)
REGISTER_TEST(Test_extract_file)
REGISTER_TEST(Test_get)
END_TEST_MAP()
public:
void SetUp();
void TearDown();
void Test_help();
void Test_output();
void Test_extract_file();
void Test_get();
CString m_sTmpFolder;
CString m_sErrorReportName;
CString m_sMD5Hash;
};
REGISTER_TEST_SUITE( CrproberTests );
void CrproberTests::SetUp()
{
CString sAppDataFolder;
// Create a temporary folder
Utility::GetSpecialFolder(CSIDL_APPDATA, sAppDataFolder);
m_sTmpFolder = sAppDataFolder+_T("\\CrashRpt 应用程序名称");
BOOL bCreate = Utility::CreateFolder(m_sTmpFolder);
TEST_ASSERT(bCreate);
// Create error report ZIP
BOOL bCreateReport = TestUtils::CreateErrorReport(m_sTmpFolder, m_sErrorReportName, m_sMD5Hash);
TEST_ASSERT(bCreateReport);
__TEST_CLEANUP__;
}
void CrproberTests::TearDown()
{
// Delete tmp folder
Utility::RecycleFile(m_sTmpFolder, TRUE);
}
void CrproberTests::Test_help()
{
// This test runs 'crprober.exe /?' (help)
// and checks the return code.
int nRetCode=-1;
CString sExeName;
CString sParams;
#ifdef _DEBUG
sExeName = Utility::GetModulePath(NULL)+_T("\\crproberd.exe");
#else
sExeName = Utility::GetModulePath(NULL)+_T("\\crprober.exe");
#endif
sParams = _T("/?");
// Run - assume zero ret code
nRetCode = TestUtils::RunProgram(sExeName, sParams);
TEST_ASSERT(nRetCode==0);
__TEST_CLEANUP__;
}
void CrproberTests::Test_output()
{
// This test calls crprober.exe with /f, fmd5 and /o flags to
// generate a text output file from error report ZIP archive
CString sExeName;
CString sParams;
BOOL bExecute = FALSE;
SHELLEXECUTEINFO sei;
memset(&sei, 0, sizeof(SHELLEXECUTEINFO));
DWORD dwExitCode = 1;
DWORD dwFileAttrs = INVALID_FILE_ATTRIBUTES;
#ifdef _DEBUG
sExeName = Utility::GetModulePath(NULL)+_T("\\crproberd.exe");
#else
sExeName = Utility::GetModulePath(NULL)+_T("\\crprober.exe");
#endif
sParams.Format(_T("/f \"%s\" /fmd5 \"%s\" /o \"%s\""),
m_sErrorReportName,
m_sErrorReportName+_T(".md5"),
m_sTmpFolder+_T("\\out.txt"));
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.fMask = SEE_MASK_NOCLOSEPROCESS|SEE_MASK_FLAG_NO_UI;
sei.lpVerb = _T("open");
sei.lpFile = sExeName;
sei.lpParameters = sParams;
sei.lpDirectory = m_sTmpFolder;
bExecute = ShellExecuteEx(&sei);
TEST_ASSERT(bExecute);
// Wait until process exits
WaitForSingleObject(sei.hProcess, 10000);
// Check crprober.exe process exit code - it should equal to 0
GetExitCodeProcess(sei.hProcess, &dwExitCode);
TEST_ASSERT(dwExitCode==0);
// Check that out.txt file exists
dwFileAttrs = GetFileAttributes(m_sTmpFolder+_T("\\out.txt"));
TEST_ASSERT(dwFileAttrs!=INVALID_FILE_ATTRIBUTES);
__TEST_CLEANUP__;
if(sei.hProcess)
{
CloseHandle(sei.hProcess);
sei.hProcess = NULL;
}
}
void CrproberTests::Test_extract_file()
{
// This test calls crprober.exe with /ext flag to
// extract all files from error report ZIP archive
CString sExeName;
CString sParams;
BOOL bExecute = FALSE;
SHELLEXECUTEINFO sei;
memset(&sei, 0, sizeof(SHELLEXECUTEINFO));
DWORD dwExitCode = 1;
DWORD dwFileAttrs = INVALID_FILE_ATTRIBUTES;
std::vector<CString> asFileList;
UINT i;
asFileList.push_back(_T("crashrpt.xml"));
asFileList.push_back(_T("crashdump.dmp"));
asFileList.push_back(_T("regkey.xml"));
#ifdef _DEBUG
sExeName = Utility::GetModulePath(NULL)+_T("\\crproberd.exe");
#else
sExeName = Utility::GetModulePath(NULL)+_T("\\crprober.exe");
#endif
sParams.Format(_T("/f \"%s\" /ext \"%s\""), m_sErrorReportName, m_sTmpFolder);
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.fMask = SEE_MASK_NOCLOSEPROCESS|SEE_MASK_FLAG_NO_UI;
sei.lpVerb = _T("open");
sei.lpFile = sExeName;
sei.lpParameters = sParams;
sei.lpDirectory = m_sTmpFolder;
bExecute = ShellExecuteEx(&sei);
TEST_ASSERT(bExecute);
// Wait until process exits
WaitForSingleObject(sei.hProcess, 10000);
// Check crprober.exe process exit code - it should equal to 0
GetExitCodeProcess(sei.hProcess, &dwExitCode);
TEST_ASSERT(dwExitCode==0);
// Check that extracted files exist
for(i=0; i<asFileList.size(); i++)
{
dwFileAttrs = GetFileAttributes(m_sTmpFolder+_T("\\")+asFileList[i]);
TEST_ASSERT(dwFileAttrs!=INVALID_FILE_ATTRIBUTES);
}
__TEST_CLEANUP__;
if(sei.hProcess)
{
CloseHandle(sei.hProcess);
sei.hProcess = NULL;
}
}
void CrproberTests::Test_get()
{
if(g_bRunningFromUNICODEFolder)
return; // Skip this test for UNICODE case
CString sExeName;
std::wstring sOut;
#ifdef _DEBUG
sExeName = Utility::GetModulePath(NULL)+_T("\\crproberd.exe");
#else
sExeName = Utility::GetModulePath(NULL)+_T("\\crprober.exe");
#endif
sExeName += _T(" /f \"");
sExeName += m_sErrorReportName;
sExeName += _T("\" /o \"\" /get XmlDescMisc AppName 0");
sOut = TestUtils::exec(sExeName);
TEST_ASSERT(sOut==L"My& app Name &");
__TEST_CLEANUP__;
}