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>
10 static const char szJournalHeader
[] = "RAT_J-V1";
11 static const WCHAR szJournalFileName
[] = L
"rosautotest.journal";
14 * Constructs a CJournaledTestList object for an associated CTest-derived object.
17 * Pointer to a CTest-derived object, for which this test list shall serve.
19 CJournaledTestList::CJournaledTestList(CTest
* Test
)
22 WCHAR JournalFile
[MAX_PATH
];
24 m_hJournal
= INVALID_HANDLE_VALUE
;
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");
30 m_JournalFile
= JournalFile
;
31 m_JournalFile
+= L
"\\rosautotest\\";
33 /* Create the directory if necessary */
34 if(GetFileAttributesW(m_JournalFile
.c_str()) == INVALID_FILE_ATTRIBUTES
)
35 CreateDirectoryW(m_JournalFile
.c_str(), NULL
);
37 m_JournalFile
+= szJournalFileName
;
39 /* Check if the journal already exists */
40 if(GetFileAttributesW(m_JournalFile
.c_str()) == INVALID_FILE_ATTRIBUTES
)
41 WriteInitialJournalFile();
47 * Destructs a CJournaledTestList object.
49 CJournaledTestList::~CJournaledTestList()
51 if(m_hJournal
!= INVALID_HANDLE_VALUE
)
52 CloseHandle(m_hJournal
);
56 * Opens the journal file through the CreateFileW API using the m_hJournal handle.
58 * @param DesiredAccess
59 * dwDesiredAccess parameter passed to CreateFileW
62 * true if the journal file shall be created, false if an existing one shall be opened
65 CJournaledTestList::OpenJournal(DWORD DesiredAccess
, bool CreateNew
)
67 m_hJournal
= CreateFileW(m_JournalFile
.c_str(), DesiredAccess
, 0, NULL
, (CreateNew
? CREATE_ALWAYS
: OPEN_EXISTING
), FILE_ATTRIBUTE_NORMAL
, NULL
);
69 if(m_hJournal
== INVALID_HANDLE_VALUE
)
70 FATAL("CreateFileW failed\n");
74 * Serializes a std::string and writes it into the opened journal file.
77 * The std::string to serialize
79 * @see UnserializeFromBuffer
82 CJournaledTestList::SerializeIntoJournal(const string
& String
)
85 WriteFile(m_hJournal
, String
.c_str(), String
.size() + 1, &BytesWritten
, NULL
);
89 * Serializes a std::wstring and writes it into the opened journal file.
92 * The std::wstring to serialize
94 * @see UnserializeFromBuffer
97 CJournaledTestList::SerializeIntoJournal(const wstring
& String
)
100 WriteFile(m_hJournal
, String
.c_str(), (String
.size() + 1) * sizeof(WCHAR
), &BytesWritten
, NULL
);
104 * Unserializes the next std::string from the journal buffer.
105 * The passed buffer pointer will point at the next element afterwards.
108 * Pointer to a pointer to a char array containing the journal buffer
111 * The std::string to unserialize the value into.
114 CJournaledTestList::UnserializeFromBuffer(char** Buffer
, string
& Output
)
116 Output
= string(*Buffer
);
117 *Buffer
+= Output
.size() + 1;
121 * Unserializes the next std::wstring from the journal buffer.
122 * The passed buffer pointer will point at the next element afterwards.
125 * Pointer to a pointer to a char array containing the journal buffer
128 * The std::wstring to unserialize the value into.
131 CJournaledTestList::UnserializeFromBuffer(char** Buffer
, wstring
& Output
)
133 Output
= wstring((PWSTR
)*Buffer
);
134 *Buffer
+= (Output
.size() + 1) * sizeof(WCHAR
);
138 * Gets all tests to be run and writes an initial journal file with this information.
141 CJournaledTestList::WriteInitialJournalFile()
143 char TerminatingNull
= 0;
147 StringOut("Writing initial journal file...\n\n");
151 /* Store all command lines in the m_List vector */
152 while((TestInfo
= m_Test
->GetNextTestInfo()) != 0)
154 m_List
.push_back(*TestInfo
);
158 /* Serialize the vector and the iterator into a file */
159 OpenJournal(GENERIC_WRITE
, true);
161 WriteFile(m_hJournal
, szJournalHeader
, sizeof(szJournalHeader
), &BytesWritten
, NULL
);
162 WriteFile(m_hJournal
, &m_ListIterator
, sizeof(m_ListIterator
), &BytesWritten
, NULL
);
164 for(size_t i
= 0; i
< m_List
.size(); i
++)
166 SerializeIntoJournal(m_List
[i
].CommandLine
);
167 SerializeIntoJournal(m_List
[i
].Module
);
168 SerializeIntoJournal(m_List
[i
].Test
);
171 WriteFile(m_hJournal
, &TerminatingNull
, sizeof(TerminatingNull
), &BytesWritten
, NULL
);
173 CloseHandle(m_hJournal
);
174 m_hJournal
= INVALID_HANDLE_VALUE
;
176 /* m_ListIterator will be incremented before its first use */
177 m_ListIterator
= (size_t)-1;
181 * Loads the existing journal file and sets all members to the values saved in that file.
184 CJournaledTestList::LoadJournalFile()
188 char FileHeader
[sizeof(szJournalHeader
)];
192 StringOut("Loading journal file...\n\n");
194 OpenJournal(GENERIC_READ
);
195 RemainingSize
= GetFileSize(m_hJournal
, NULL
);
197 /* Verify the header of the journal file */
198 ReadFile(m_hJournal
, FileHeader
, sizeof(szJournalHeader
), &BytesRead
, NULL
);
199 RemainingSize
-= BytesRead
;
201 if(BytesRead
!= sizeof(szJournalHeader
))
202 EXCEPTION("Journal file contains no header!\n");
204 if(strcmp(FileHeader
, szJournalHeader
))
205 EXCEPTION("Journal file has an unsupported header!\n");
207 /* Read the iterator */
208 ReadFile(m_hJournal
, &m_ListIterator
, sizeof(m_ListIterator
), &BytesRead
, NULL
);
209 RemainingSize
-= BytesRead
;
211 if(BytesRead
!= sizeof(m_ListIterator
))
212 EXCEPTION("Journal file contains no m_ListIterator member!\n");
214 /* Read the rest of the file into a buffer */
215 Buffer
= new char[RemainingSize
];
217 ReadFile(m_hJournal
, Buffer
, RemainingSize
, &BytesRead
, NULL
);
219 CloseHandle(m_hJournal
);
222 /* Now recreate the m_List vector out of that information */
227 UnserializeFromBuffer(&pBuffer
, TestInfo
.CommandLine
);
228 UnserializeFromBuffer(&pBuffer
, TestInfo
.Module
);
229 UnserializeFromBuffer(&pBuffer
, TestInfo
.Test
);
231 m_List
.push_back(TestInfo
);
238 * Writes the current m_ListIterator value into the journal.
241 CJournaledTestList::UpdateJournal()
245 OpenJournal(GENERIC_WRITE
);
247 /* Skip the header */
248 SetFilePointer(m_hJournal
, sizeof(szJournalHeader
), NULL
, FILE_CURRENT
);
250 WriteFile(m_hJournal
, &m_ListIterator
, sizeof(m_ListIterator
), &BytesWritten
, NULL
);
252 CloseHandle(m_hJournal
);
257 * Interface to other classes for receiving information about the next test to be run.
260 * A pointer to a CTestInfo object, which contains all available information about the next test.
261 * The caller needs to free that object.
264 CJournaledTestList::GetNextTestInfo()
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. */
274 /* Check whether the iterator would already exceed the number of stored elements */
275 if(m_ListIterator
== m_List
.size())
277 /* Delete the journal and return no pointer */
278 DeleteFileW(m_JournalFile
.c_str());
284 /* Update the journal with the current iterator and return the test information */
287 TestInfo
= new CTestInfo(m_List
[m_ListIterator
]);