Big testing system commit
[reactos.git] / rostests / rosautotest / CJournaledTestList.cpp
1 /*
2 * PROJECT: ReactOS Automatic Testing Utility
3 * LICENSE: GNU GPLv2 or any later version as published by the Free Software Foundation
4 * PURPOSE: Class implementing a journaled test list for the Crash Recovery feature
5 * COPYRIGHT: Copyright 2009 Colin Finck <colin@reactos.org>
6 */
7
8 #include "precomp.h"
9
10 static const char szJournalHeader[] = "RAT_J-V1";
11 static const WCHAR szJournalFileName[] = L"rosautotest.journal";
12
13 /**
14 * Constructs a CJournaledTestList object for an associated CTest-derived object.
15 *
16 * @param Test
17 * Pointer to a CTest-derived object, for which this test list shall serve.
18 */
19 CJournaledTestList::CJournaledTestList(CTest* Test)
20 : CTestList(Test)
21 {
22 WCHAR JournalFile[MAX_PATH];
23
24 m_hJournal = INVALID_HANDLE_VALUE;
25
26 /* Build the path to the journal file */
27 if(SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, JournalFile) != S_OK)
28 FATAL("SHGetFolderPathW failed\n");
29
30 m_JournalFile = JournalFile;
31 m_JournalFile += L"\\rosautotest\\";
32
33 /* Create the directory if necessary */
34 if(GetFileAttributesW(m_JournalFile.c_str()) == INVALID_FILE_ATTRIBUTES)
35 CreateDirectoryW(m_JournalFile.c_str(), NULL);
36
37 m_JournalFile += szJournalFileName;
38
39 /* Check if the journal already exists */
40 if(GetFileAttributesW(m_JournalFile.c_str()) == INVALID_FILE_ATTRIBUTES)
41 WriteInitialJournalFile();
42 else
43 LoadJournalFile();
44 }
45
46 /**
47 * Destructs a CJournaledTestList object.
48 */
49 CJournaledTestList::~CJournaledTestList()
50 {
51 if(m_hJournal != INVALID_HANDLE_VALUE)
52 CloseHandle(m_hJournal);
53 }
54
55 /**
56 * Opens the journal file through the CreateFileW API using the m_hJournal handle.
57 *
58 * @param DesiredAccess
59 * dwDesiredAccess parameter passed to CreateFileW
60 *
61 * @param CreateNew
62 * true if the journal file shall be created, false if an existing one shall be opened
63 */
64 void
65 CJournaledTestList::OpenJournal(DWORD DesiredAccess, bool CreateNew)
66 {
67 m_hJournal = CreateFileW(m_JournalFile.c_str(), DesiredAccess, 0, NULL, (CreateNew ? CREATE_ALWAYS : OPEN_EXISTING), FILE_ATTRIBUTE_NORMAL, NULL);
68
69 if(m_hJournal == INVALID_HANDLE_VALUE)
70 FATAL("CreateFileW failed\n");
71 }
72
73 /**
74 * Serializes a std::string and writes it into the opened journal file.
75 *
76 * @param String
77 * The std::string to serialize
78 *
79 * @see UnserializeFromBuffer
80 */
81 void
82 CJournaledTestList::SerializeIntoJournal(const string& String)
83 {
84 DWORD BytesWritten;
85 WriteFile(m_hJournal, String.c_str(), String.size() + 1, &BytesWritten, NULL);
86 }
87
88 /**
89 * Serializes a std::wstring and writes it into the opened journal file.
90 *
91 * @param String
92 * The std::wstring to serialize
93 *
94 * @see UnserializeFromBuffer
95 */
96 void
97 CJournaledTestList::SerializeIntoJournal(const wstring& String)
98 {
99 DWORD BytesWritten;
100 WriteFile(m_hJournal, String.c_str(), (String.size() + 1) * sizeof(WCHAR), &BytesWritten, NULL);
101 }
102
103 /**
104 * Unserializes the next std::string from the journal buffer.
105 * The passed buffer pointer will point at the next element afterwards.
106 *
107 * @param Buffer
108 * Pointer to a pointer to a char array containing the journal buffer
109 *
110 * @param Output
111 * The std::string to unserialize the value into.
112 */
113 void
114 CJournaledTestList::UnserializeFromBuffer(char** Buffer, string& Output)
115 {
116 Output = string(*Buffer);
117 *Buffer += Output.size() + 1;
118 }
119
120 /**
121 * Unserializes the next std::wstring from the journal buffer.
122 * The passed buffer pointer will point at the next element afterwards.
123 *
124 * @param Buffer
125 * Pointer to a pointer to a char array containing the journal buffer
126 *
127 * @param Output
128 * The std::wstring to unserialize the value into.
129 */
130 void
131 CJournaledTestList::UnserializeFromBuffer(char** Buffer, wstring& Output)
132 {
133 Output = wstring((PWSTR)*Buffer);
134 *Buffer += (Output.size() + 1) * sizeof(WCHAR);
135 }
136
137 /**
138 * Gets all tests to be run and writes an initial journal file with this information.
139 */
140 void
141 CJournaledTestList::WriteInitialJournalFile()
142 {
143 char TerminatingNull = 0;
144 CTestInfo* TestInfo;
145 DWORD BytesWritten;
146
147 StringOut("Writing initial journal file...\n\n");
148
149 m_ListIterator = 0;
150
151 /* Store all command lines in the m_List vector */
152 while((TestInfo = m_Test->GetNextTestInfo()) != 0)
153 {
154 m_List.push_back(*TestInfo);
155 delete TestInfo;
156 }
157
158 /* Serialize the vector and the iterator into a file */
159 OpenJournal(GENERIC_WRITE, true);
160
161 WriteFile(m_hJournal, szJournalHeader, sizeof(szJournalHeader), &BytesWritten, NULL);
162 WriteFile(m_hJournal, &m_ListIterator, sizeof(m_ListIterator), &BytesWritten, NULL);
163
164 for(size_t i = 0; i < m_List.size(); i++)
165 {
166 SerializeIntoJournal(m_List[i].CommandLine);
167 SerializeIntoJournal(m_List[i].Module);
168 SerializeIntoJournal(m_List[i].Test);
169 }
170
171 WriteFile(m_hJournal, &TerminatingNull, sizeof(TerminatingNull), &BytesWritten, NULL);
172
173 CloseHandle(m_hJournal);
174 m_hJournal = INVALID_HANDLE_VALUE;
175
176 /* m_ListIterator will be incremented before its first use */
177 m_ListIterator = (size_t)-1;
178 }
179
180 /**
181 * Loads the existing journal file and sets all members to the values saved in that file.
182 */
183 void
184 CJournaledTestList::LoadJournalFile()
185 {
186 char* Buffer;
187 char* pBuffer;
188 char FileHeader[sizeof(szJournalHeader)];
189 DWORD BytesRead;
190 DWORD RemainingSize;
191
192 StringOut("Loading journal file...\n\n");
193
194 OpenJournal(GENERIC_READ);
195 RemainingSize = GetFileSize(m_hJournal, NULL);
196
197 /* Verify the header of the journal file */
198 ReadFile(m_hJournal, FileHeader, sizeof(szJournalHeader), &BytesRead, NULL);
199 RemainingSize -= BytesRead;
200
201 if(BytesRead != sizeof(szJournalHeader))
202 EXCEPTION("Journal file contains no header!\n");
203
204 if(strcmp(FileHeader, szJournalHeader))
205 EXCEPTION("Journal file has an unsupported header!\n");
206
207 /* Read the iterator */
208 ReadFile(m_hJournal, &m_ListIterator, sizeof(m_ListIterator), &BytesRead, NULL);
209 RemainingSize -= BytesRead;
210
211 if(BytesRead != sizeof(m_ListIterator))
212 EXCEPTION("Journal file contains no m_ListIterator member!\n");
213
214 /* Read the rest of the file into a buffer */
215 Buffer = new char[RemainingSize];
216 pBuffer = Buffer;
217 ReadFile(m_hJournal, Buffer, RemainingSize, &BytesRead, NULL);
218
219 CloseHandle(m_hJournal);
220 m_hJournal = NULL;
221
222 /* Now recreate the m_List vector out of that information */
223 while(*pBuffer)
224 {
225 CTestInfo TestInfo;
226
227 UnserializeFromBuffer(&pBuffer, TestInfo.CommandLine);
228 UnserializeFromBuffer(&pBuffer, TestInfo.Module);
229 UnserializeFromBuffer(&pBuffer, TestInfo.Test);
230
231 m_List.push_back(TestInfo);
232 }
233
234 delete[] Buffer;
235 }
236
237 /**
238 * Writes the current m_ListIterator value into the journal.
239 */
240 void
241 CJournaledTestList::UpdateJournal()
242 {
243 DWORD BytesWritten;
244
245 OpenJournal(GENERIC_WRITE);
246
247 /* Skip the header */
248 SetFilePointer(m_hJournal, sizeof(szJournalHeader), NULL, FILE_CURRENT);
249
250 WriteFile(m_hJournal, &m_ListIterator, sizeof(m_ListIterator), &BytesWritten, NULL);
251
252 CloseHandle(m_hJournal);
253 m_hJournal = NULL;
254 }
255
256 /**
257 * Interface to other classes for receiving information about the next test to be run.
258 *
259 * @return
260 * A pointer to a CTestInfo object, which contains all available information about the next test.
261 * The caller needs to free that object.
262 */
263 CTestInfo*
264 CJournaledTestList::GetNextTestInfo()
265 {
266 CTestInfo* TestInfo;
267
268 /* Always jump to the next test here.
269 - If we're at the beginning of a new test list, the iterator will be set to 0.
270 - If we started with a loaded one, we assume that the test m_ListIterator is currently set
271 to crashed, so we move to the next test. */
272 ++m_ListIterator;
273
274 /* Check whether the iterator would already exceed the number of stored elements */
275 if(m_ListIterator == m_List.size())
276 {
277 /* Delete the journal and return no pointer */
278 DeleteFileW(m_JournalFile.c_str());
279
280 TestInfo = NULL;
281 }
282 else
283 {
284 /* Update the journal with the current iterator and return the test information */
285 UpdateJournal();
286
287 TestInfo = new CTestInfo(m_List[m_ListIterator]);
288 }
289
290 return TestInfo;
291 }