1 krisbash 1.1 //*****************************************************************************
2 // Copyright (C) 2008 Microsoft Corporation
3 // All rights reserved.
4 //*****************************************************************************
5 #ifndef __RUN_H__
6 #define __RUN_H__
7
8 #include "Globals.h"
9
10 namespace TestSystem
11 {
12
13 using namespace std;
14
15 bool ConfigureLocalInjection();
16
17 class Param
18 {
19 public:
20 Param(_In_z_ const PAL_Char * name, _In_z_ const PAL_Char * value);
21 ~Param();
22 krisbash 1.1
23 const PAL_Char * m_name;
24 const PAL_Char * m_value;
25 };
26
27 class Module
28 {
29 public:
30 Module(_In_z_ const PAL_Char * name);
31 ~Module();
32
33 const PAL_Char * GetName() const {return m_name;}
34
35 void AddTest(_In_ Test *test);
36 void AddRun(_In_z_ const PAL_Char * command);
37 void Execute();
38
39 void SetLibrary(_In_ Shlib *lib);
40 void Unload();
41 private:
42 size_t Find(_In_z_ const PAL_Char * test) const;
43 krisbash 1.1 void RunTests(size_t start);
44 void RunMatchingTests(_In_z_ const PAL_Char * testNameSubstring);
45 void RunModuleLevelTests(bool runCleanup);
46
47 const PAL_Char * m_name;
48 Shlib *m_library; //Freed after the test run.
49 vector<Test *> m_tests; //List of all tests in this module.
50 vector<PAL_Char *> m_runs; //List of selected tests to run.
51 };
52
53 NITS_EXPORT bool NitsParseArgument(_In_z_ char *str);
54 NITS_EXPORT int NitsExecuteRun();
55
56 #ifdef _MSC_VER
57
58 enum WTTLOGGER_TEST_RESULT
59 {
60 WTTLOG_TESTCASE_RESULT_PASS =0x1,
61 WTTLOG_TESTCASE_RESULT_FAIL =0x2,
62 WTTLOG_TESTCASE_RESULT_BLOCKED =0x3,
63 WTTLOG_TESTCASE_RESULT_WARN =0x4,
64 krisbash 1.1 WTTLOG_TESTCASE_RESULT_SKIPPED =0x5
65 };
66
67 class CWTTLogger
68 {
69
70 public:
71 CWTTLogger()
72 {
73 library = LoadLibraryExW(L"WttLog.dll", NULL, 0);
74 WTTLogInit = (HRESULT (WINAPI *)())GetProcAddress(library, "WTTLogInit");
75 WTTLogUninit = (void (WINAPI *)())GetProcAddress(library, "WTTLogUninit");
76 WTTLogCreateLogDevice = (HRESULT (WINAPI *)(_In_opt_ LPWSTR, _Out_ LONG *))GetProcAddress(library, "WTTLogCreateLogDevice");
77 WTTLogCloseLogDevice = (HRESULT (WINAPI *)(_In_ LONG, _In_opt_ LPWSTR))GetProcAddress(library, "WTTLogCloseLogDevice");
78 WTTLogStartTest = (HRESULT (WINAPI *)(_In_ LONG, _In_ LPWSTR))GetProcAddress(library, "WTTLogStartTest");
79 WTTLogTrace = (HRESULT (WINAPI *)(LONG, DWORD, ...))GetProcAddress(library, "WTTLogTrace");
80 WTTLogEndTest = (HRESULT (WINAPI *)(_In_ LONG, _In_ LPWSTR, _In_ DWORD, _In_opt_ LPWSTR))GetProcAddress(library, "WTTLogEndTest");
81
82 WTTLogInit();
83 }
84
85 krisbash 1.1 ~CWTTLogger()
86 {
87 WTTLogUninit();
88 FreeLibrary(library);
89 }
90
91 HRESULT CreateLogDevice
92 (
93 _In_opt_ LPWSTR pwszLogDeviceName,
94 _Out_ LONG* phDevice
95 )
96 {
97 return WTTLogCreateLogDevice(pwszLogDeviceName, phDevice);
98 }
99
100 HRESULT CloseLogDevice
101 (
102 _In_opt_ LPWSTR pwszLogDeviceName,
103 _In_ LONG hDevice
104 )
105 {
106 krisbash 1.1 return WTTLogCloseLogDevice(hDevice, pwszLogDeviceName);
107 }
108
109 HRESULT StartTest
110 (
111 _In_ LPWSTR pwszTestName, // Name of the test case
112 _In_ LONG hDevice // handle to the device
113 )
114 {
115 return WTTLogStartTest(hDevice, pwszTestName);
116 }
117
118 HRESULT TraceMsg
119 (
120 _In_ LPWSTR pwszTraceStr, // widechar string to trace
121 _In_ LONG hDevice // handle to the device
122 )
123 {
124 // WTT_LVL_MSG will need us to includ wttlogdef.h from test to avoid that, I am doing this here
125 #define WTT_LVL_MSG 0x10000000
126 return WTTLogTrace(hDevice, WTT_LVL_MSG, pwszTraceStr);
127 krisbash 1.1 }
128
129 HRESULT EndTest
130 (
131 _In_ LPWSTR pwszTestName, // Name of the test case
132 _In_ DWORD dwResult, // test result
133 _In_opt_ LPWSTR pwszRepro, // repro line
134 _In_ LONG hDevice // handle to the device
135 )
136 {
137 return WTTLogEndTest(hDevice, pwszTestName, dwResult, pwszRepro);
138 }
139 private:
140 HMODULE library;
141 HRESULT (WINAPI *WTTLogInit)();
142 void (WINAPI *WTTLogUninit)();
143 HRESULT (WINAPI *WTTLogCreateLogDevice)(_In_opt_ LPWSTR, _Out_ LONG *);
144 HRESULT (WINAPI *WTTLogCloseLogDevice)(_In_ LONG, _In_opt_ LPWSTR);
145 HRESULT (WINAPI *WTTLogStartTest)(_In_ LONG, _In_ LPWSTR);
146 HRESULT (WINAPI *WTTLogTrace)(LONG, DWORD, ...);
147 HRESULT (WINAPI *WTTLogEndTest)(_In_ LONG, _In_ LPWSTR, _In_ DWORD, _In_opt_ LPWSTR);
148 krisbash 1.1 };
149 #endif
150
151 NITS_EXTERN_C PAL_Uint32 THREAD_API _PipeThread(void* param);
152
153 // Represents the info needed to invoke a fixture
154 // used internally inside NITS to run fault simulation loop
155 class FixtureInvocation
156 {
157 public:
158 FixtureInvocation(_In_opt_ FixtureInvocation *parentFixtureInvocationParam, _In_ BodyProc funcParam, _In_ void *selfContextParam, TestFixtureType fixtureTypeParam);
159
160 void Execute();
161
162 FixtureInvocation *parentFixtureInvocation; // this will be NULL for all fixtures except Cleanup
163 // for cleanup fixture it will be used to keep track of the fixture for which this is Cleanup
164 BodyProc func;
165 void *selfContext;
166 TestFixtureType fixtureType;
167 bool executedInCurrentIteration;
168 };
169 krisbash 1.1
170 class Run
171 {
172 public:
173 static Run &GetInstance();
174 static bool IsValid();
175
176 Run();
177 ~Run();
178
179 Module &GetCurrentModule() const {return *m_loadingModule;}
180 Test &GetCurrentTest() const {return *m_currentTest;}
181
182 void AddTestVariationNode(Switch *mySwitch) { m_currentTestVariationNodes.push_back(mySwitch); }
183 void ClearTestVariationNodes() { m_currentTestVariationNodes.clear(); }
184
185 void AddFixtureInvocation(_In_opt_ BodyProc parentFixtureFuncParam, _In_ BodyProc funcParam, _In_ void *selfContextParam, TestFixtureType fixtureTypeParam); // adds fixture invocation, for cleanups, links fixture invocation to the parent
186 void ClearFixtureInvocations(); // cleans up the fixture invocations
187
188 void ResetFixtureInvocationStateForRerun(); // resets the executedInCurrentIteration to false for all fixture invocations
189 void ExecuteNextFixtureInvocationDuringFaultSim(); // looks at current result state from globals and decides what the next fixture invocation should be
190 krisbash 1.1
191 PAL_Char *GetTestVariationNodeNames();
192 bool CurrentTestMatchesTestFilter();
193
194 const PAL_Char * GetParam(_In_z_ const PAL_Char * name) const;
195 const PAL_Char * GetDebugger() const {return m_debugger;}
196 bool GetEnabled() const {return m_enabled;}
197 bool GetFlaky() const {return m_flaky;}
198 NitsFaultMode GetFaultMode() const {return m_simMode;}
199 vector<int> const &GetFaultBreakpointList() const {return *m_bpFault;}
200 Configuration const &GetConfiguration() const {return m_config;}
201
202 bool IsParent() const {return !m_child;}
203 bool IsChild() const {return m_child || !m_isolation;}
204 bool IsSiteExcluded(int id) const;
205
206 int Execute();
207 void ExecuteTest(_In_ Test *test, _In_opt_ const PAL_Char * choices, TestFixtureType executeIfTypeIs = BodyFixture);
208
209 void ExecuteDeferredCleanup(_In_ Test *test);
210
211 krisbash 1.1 void SetDefaultOptions();
212 void SetEnabled(bool enabled) {m_enabled = enabled;}
213 void SetFlaky(bool flaky) {m_flaky = flaky;}
214 void SetFaultMode(NitsFaultMode mode) {m_simMode = mode;}
215 void SetExclusions(_In_reads_opt_(count) int sites[], int count);
216 void ReportResult();
217 bool ParseArgument(_In_ PWSTR arg);
218 void PipeThread();
219
220 void SetGlobalFaultSimBehavior(bool enabled) { m_fault = enabled; };
221
222 void SetFaultInjectionLoopStatus(bool running) { m_runningFaultInjectionLoop = running; }
223 bool InMiddleOfFaultInjectionLoop() { return m_runningFaultInjectionLoop; };
224 bool DoneExecutingAllFixtureInvocations();
225 private:
226 static Run *s_run;
227
228 void AddModule(_In_z_ const PAL_Char * name);
229 bool AddParam(_In_z_ const PAL_Char * name, _In_z_ const PAL_Char * value);
230 bool AddRun(_In_z_ const PAL_Char * name, _In_z_ const PAL_Char * test);
231 void DumpPipe();
232 krisbash 1.1 unsigned ReportStatistics();
233
234 vector<Module *> &Modules() {return *m_modules;}
235 vector<Param *> &Params() {return *m_params;}
236 vector<Module *> const &Modules() const {return *m_modules;}
237 vector<Param *> const &Params() const {return *m_params;}
238
239 Test *m_currentTest;
240 vector<Switch *> m_currentTestVariationNodes;
241 // used to record all fixture invocations in the current test starting from
242 // topmost setup to all cleanups
243 // this will be used to play back the test during fault simulation loop
244 vector<FixtureInvocation *> m_currentTestFixtureInvocations;
245
246 Module *m_loadingModule;
247 vector<Module *> *m_modules;
248 vector<Param *> *m_params;
249
250 //New options from command line.
251 vector<int> *m_bpFault; //List of fault iterations to break on.
252 Configuration m_config;
253 krisbash 1.1 bool m_pause; //Wait for input after testing?
254 bool m_reset; //Ignore existing test runs.
255 const PAL_Char * m_debugger; //Debug command line.
256 const PAL_Char * m_binaryFilter; //Semicolon-separated list; processes must contain one or more.
257 const PAL_Char * m_binaryTarget; //Semicolon-separated list of binaries to trap.
258
259 // for filtering functionality based on fixture names
260 const PAL_Char *m_testFilter;
261 PAL_Char m_translatedTestFilter[1024];
262
263 //Old options from command line.
264 bool m_isolation; //Run tests in child process?
265 bool m_child; //Run as child process?
266 bool m_fault; //Run automatic fault sim (affects V1 tests only!).
267 const PAL_Char * m_wtt; //WTT log file.
268
269 bool m_finished; //Stops pipe thread.
270
271 int *m_statistics;
272 int m_faultIterations; //Cumulative fault injection iteration total.
273
274 krisbash 1.1 //TODO: Deprecate all these variables.
275 //Run-time options set by Test object.
276 bool m_enabled; //If false, skips the test body.
277 bool m_flaky; //If true, skips the test body only if nits was called with parameter "-skipflaky".
278 NitsFaultMode m_simMode; //Default to Auto/Manual fault sim.
279
280 int *m_exclusions; //Sites waived during auto fault sim.
281 int m_exclusionsCount;
282
283 bool m_runningFaultInjectionLoop;
284
285 size_t m_nextFixtureInvocationToExecute; // keeps track of what should be the next fixture invocation to be executed after current one returns
286 #ifdef _MSC_VER
287 CWTTLogger *m_logger;
288 long m_loggerHandle;
289 PAL_Char *m_testCase;
290 PAL_Char *m_testCaseRepro;
291 long m_loggerTestResult;
292 #endif
293 };
294
295 krisbash 1.1 } //namespace TestSystem;
296
297 #endif // ndef __RUN_H__
|