summaryrefslogtreecommitdiff
path: root/src/TestExe/Task.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/TestExe/Task.cs')
-rw-r--r--src/TestExe/Task.cs330
1 files changed, 34 insertions, 296 deletions
diff --git a/src/TestExe/Task.cs b/src/TestExe/Task.cs
index 577acbea..7d39bfd9 100644
--- a/src/TestExe/Task.cs
+++ b/src/TestExe/Task.cs
@@ -4,9 +4,6 @@ using System;
4using System.Collections.Generic; 4using System.Collections.Generic;
5using System.Diagnostics; 5using System.Diagnostics;
6using System.IO; 6using System.IO;
7using System.Linq;
8using System.Management;
9using System.Text;
10using Microsoft.Win32; 7using Microsoft.Win32;
11 8
12namespace TestExe 9namespace TestExe
@@ -17,7 +14,7 @@ namespace TestExe
17 14
18 public Task(string Data) 15 public Task(string Data)
19 { 16 {
20 data = Data; 17 this.data = Data;
21 } 18 }
22 19
23 public abstract void RunTask(); 20 public abstract void RunTask();
@@ -52,8 +49,8 @@ namespace TestExe
52 49
53 public override void RunTask() 50 public override void RunTask()
54 { 51 {
55 int low = int.Parse(data.Split(new string[] { ":" }, 2, StringSplitOptions.None)[0]); 52 int low = int.Parse(this.data.Split(new string[] { ":" }, 2, StringSplitOptions.None)[0]);
56 int high = int.Parse(data.Split(new string[] { ":" }, 2, StringSplitOptions.None)[1]); 53 int high = int.Parse(this.data.Split(new string[] { ":" }, 2, StringSplitOptions.None)[1]);
57 54
58 Random r = new Random(); 55 Random r = new Random();
59 int milliseconds = r.Next(high - low) + low; 56 int milliseconds = r.Next(high - low) + low;
@@ -62,13 +59,30 @@ namespace TestExe
62 } 59 }
63 } 60 }
64 61
62 public class LargeFileTask : Task
63 {
64 public LargeFileTask(string Data) : base(Data) { }
65
66 public override void RunTask()
67 {
68 string[] tokens = this.data.Split(new char[] { '|' }, 2);
69 string filePath = System.Environment.ExpandEnvironmentVariables(tokens[0]);
70 long size = long.Parse(tokens[1]);
71 using (var stream = File.Create(filePath))
72 {
73 stream.Seek(size - 1, SeekOrigin.Begin);
74 stream.WriteByte(1);
75 }
76 }
77 }
78
65 public class LogTask : Task 79 public class LogTask : Task
66 { 80 {
67 string[] argsUsed; 81 string[] argsUsed;
68 public LogTask(string Data, string[] args) 82 public LogTask(string Data, string[] args)
69 : base(Data) 83 : base(Data)
70 { 84 {
71 argsUsed = args; 85 this.argsUsed = args;
72 } 86 }
73 87
74 public override void RunTask() 88 public override void RunTask()
@@ -76,14 +90,14 @@ namespace TestExe
76 string logFile = ""; 90 string logFile = "";
77 string argsUsedString = ""; 91 string argsUsedString = "";
78 92
79 foreach (string a in argsUsed) 93 foreach (string a in this.argsUsed)
80 { 94 {
81 argsUsedString += a + " "; 95 argsUsedString += a + " ";
82 } 96 }
83 97
84 try 98 try
85 { 99 {
86 logFile = System.Environment.ExpandEnvironmentVariables(data); 100 logFile = System.Environment.ExpandEnvironmentVariables(this.data);
87 Console.WriteLine("creating log file: " + logFile); 101 Console.WriteLine("creating log file: " + logFile);
88 StreamWriter textFile = File.CreateText(logFile); 102 StreamWriter textFile = File.CreateText(logFile);
89 textFile.WriteLine("This is a log file created by TestExe.exe"); 103 textFile.WriteLine("This is a log file created by TestExe.exe");
@@ -98,90 +112,13 @@ namespace TestExe
98 } 112 }
99 } 113 }
100 114
101 public class ProcessInfoTask : Task
102 {
103 public ProcessInfoTask(string Data) : base(Data) { }
104
105 public override void RunTask()
106 {
107 try
108 {
109 string processInfoXml = "";
110
111 // Get information about the process and who is running it
112 Process thisProc = Process.GetCurrentProcess();
113 string username = thisProc.StartInfo.EnvironmentVariables["username"].ToString();
114
115 int parentProcId = GetParentProcess(thisProc.Id);
116 Process parentProc = Process.GetProcessById(parentProcId);
117 string parentUsername = parentProc.StartInfo.EnvironmentVariables["username"].ToString();
118
119 int grandparentProcId = GetParentProcess(parentProc.Id);
120 Process grandparentProc = Process.GetProcessById(grandparentProcId);
121 string grandparentUsername = grandparentProc.StartInfo.EnvironmentVariables["username"].ToString();
122
123 processInfoXml += "<ProcessInfo>";
124 processInfoXml += " <ProcessName>" + thisProc.ProcessName + "</ProcessName>";
125 processInfoXml += " <Id>" + thisProc.Id.ToString() + "</Id>";
126 processInfoXml += " <SessionId>" + thisProc.SessionId.ToString() + "</SessionId>";
127 processInfoXml += " <MachineName>" + thisProc.MachineName + "</MachineName>";
128 // this stuff isn't set since we didn't start the process and tell it what to use. So don't bother
129 //processInfoXml += " <StartInfo>";
130 //processInfoXml += " <FileName>" + thisProc.StartInfo.FileName + "</FileName>";
131 //processInfoXml += " <UserName>" + thisProc.StartInfo.UserName + "</UserName>";
132 //processInfoXml += " <WorkingDirectory>" + thisProc.StartInfo.WorkingDirectory + "</WorkingDirectory>";
133 //processInfoXml += " <Arguments>" + thisProc.StartInfo.Arguments + "</Arguments>";
134 //processInfoXml += " </StartInfo>";
135 processInfoXml += " <StartTime>" + thisProc.StartTime.ToString() + "</StartTime>";
136 processInfoXml += " <Username>" + username + "</Username>";
137 processInfoXml += " <ParentProcess>";
138 processInfoXml += " <ProcessName>" + parentProc.ProcessName + "</ProcessName>";
139 processInfoXml += " <Id>" + parentProc.Id.ToString() + "</Id>";
140 processInfoXml += " <StartTime>" + parentProc.StartTime.ToString() + "</StartTime>";
141 processInfoXml += " <Username>" + parentUsername + "</Username>";
142 processInfoXml += " </ParentProcess>";
143 processInfoXml += " <GrandparentProcess>";
144 processInfoXml += " <ProcessName>" + grandparentProc.ProcessName + "</ProcessName>";
145 processInfoXml += " <Id>" + grandparentProc.Id.ToString() + "</Id>";
146 processInfoXml += " <StartTime>" + grandparentProc.StartTime.ToString() + "</StartTime>";
147 processInfoXml += " <Username>" + grandparentUsername + "</Username>";
148 processInfoXml += " </GrandparentProcess>";
149 processInfoXml += "</ProcessInfo>";
150
151 string logFile = System.Environment.ExpandEnvironmentVariables(data);
152 Console.WriteLine("Creating Process Info data file: " + logFile);
153 StreamWriter textFile = File.CreateText(logFile);
154 textFile.WriteLine(processInfoXml);
155 textFile.Close();
156 }
157 catch (Exception eX)
158 {
159 Console.WriteLine("Creating Process Info data file failed");
160 Console.WriteLine(eX.Message);
161 }
162
163
164 }
165
166 private static int GetParentProcess(int Id)
167 {
168 int parentPid = 0;
169 using (ManagementObject mo = new ManagementObject("win32_process.handle='" + Id.ToString() + "'"))
170 {
171 mo.Get();
172 parentPid = Convert.ToInt32(mo["ParentProcessId"]);
173 }
174 return parentPid;
175 }
176 }
177
178 public class FileExistsTask : Task 115 public class FileExistsTask : Task
179 { 116 {
180 public FileExistsTask(string Data) : base(Data) { } 117 public FileExistsTask(string Data) : base(Data) { }
181 118
182 public override void RunTask() 119 public override void RunTask()
183 { 120 {
184 string fileToExist = System.Environment.ExpandEnvironmentVariables(data); 121 string fileToExist = System.Environment.ExpandEnvironmentVariables(this.data);
185 122
186 if (!String.IsNullOrEmpty(fileToExist)) 123 if (!String.IsNullOrEmpty(fileToExist))
187 { 124 {
@@ -196,211 +133,6 @@ namespace TestExe
196 } 133 }
197 } 134 }
198 135
199 /// <summary>
200 /// Task class that will create a registry key and write a name and value in it
201 /// </summary>
202 public class RegistryWriterTask : Task
203 {
204 private string hive;
205 private string keyPath;
206 private string[] keyPathArray;
207 private string name;
208 private RegistryValueKind regValueKind;
209 private object value;
210
211 public RegistryWriterTask(string Data) : base(Data) { }
212
213 public override void RunTask()
214 {
215 if (parseRegKeyNameTypeValue(System.Environment.ExpandEnvironmentVariables(data)))
216 {
217 RegistryKey rk = Registry.LocalMachine;
218
219 if (hive == "HKCU") rk = Microsoft.Win32.Registry.CurrentUser;
220 if (hive == "HKCC") rk = Microsoft.Win32.Registry.CurrentConfig;
221 if (hive == "HKLM") rk = Microsoft.Win32.Registry.LocalMachine;
222
223 foreach (string key in keyPathArray)
224 {
225 rk = rk.CreateSubKey(key, RegistryKeyPermissionCheck.ReadWriteSubTree);
226 }
227
228 rk.SetValue(name, value, regValueKind);
229 Console.WriteLine("Created registry key: '{0}' name: '{1}' value: '{2}' of type: '{3}'",
230 hive + "\\" + keyPath,
231 name,
232 value.ToString(),
233 regValueKind.ToString());
234 }
235 else
236 {
237 Console.WriteLine("Unable to write registry key.");
238 }
239
240 }
241
242 private bool parseRegKeyNameTypeValue(string delimittedData)
243 {
244 string[] splitString = delimittedData.Split(new string[] { "," }, StringSplitOptions.None);
245 if (splitString.Length != 4)
246 {
247 Console.WriteLine("Invalid regkey. Unable to parse key,name,type,value from: \"" + delimittedData + "\"");
248 return false;
249 }
250 else
251 {
252 keyPath = splitString[0];
253 name = splitString[1];
254 string datatype = splitString[2];
255 if (datatype == "DWord")
256 {
257 value = UInt32.Parse(splitString[3]);
258 }
259 else if (datatype == "QWord")
260 {
261 value = UInt64.Parse(splitString[3]);
262 }
263 else
264 {
265 value = splitString[3];
266 }
267
268 if (keyPath.ToUpper().StartsWith("HKLM\\"))
269 {
270 hive = "HKLM";
271 keyPath = keyPath.Replace("HKLM\\", "");
272 }
273 else if (keyPath.ToUpper().StartsWith("HKCC\\"))
274 {
275 hive = "HKCC";
276 keyPath = keyPath.Replace("HKCC\\", "");
277 }
278 else if (keyPath.ToUpper().StartsWith("HKCU\\"))
279 {
280 hive = "HKCU";
281 keyPath = keyPath.Replace("HKCU\\", "");
282 }
283 else
284 {
285 Console.WriteLine("Invalid regkey. Unable to determin hive. regkey must start with either: [HKLM], [HKCU], or [HKCC]");
286 return false;
287 }
288 keyPathArray = keyPath.Split(new string[] { "\\" }, StringSplitOptions.None);
289
290 try
291 {
292 regValueKind = (RegistryValueKind)System.Enum.Parse(typeof(RegistryValueKind), datatype);
293 }
294 catch (Exception ex)
295 {
296 Console.WriteLine("Invalid datatype. It must be: String, DWord, or QWord (case sensitive)");
297 Console.WriteLine(ex.Message);
298 return false;
299 }
300 }
301 return true;
302 }
303 }
304
305 /// <summary>
306 /// Task class that will delete a registry key value or registry key and all of its children
307 /// </summary>
308 public class RegistryDeleterTask : Task
309 {
310 private string hive;
311 private string keyPath;
312 private string[] keyPathArray;
313 private string name;
314
315 public RegistryDeleterTask(string Data) : base(Data) { }
316
317 public override void RunTask()
318 {
319 if (parseRegKeyName(System.Environment.ExpandEnvironmentVariables(data)))
320 {
321 try
322 {
323 RegistryKey rk = Registry.LocalMachine;
324
325 if (hive == "HKCU") rk = Microsoft.Win32.Registry.CurrentUser;
326 if (hive == "HKCC") rk = Microsoft.Win32.Registry.CurrentConfig;
327 if (hive == "HKLM") rk = Microsoft.Win32.Registry.LocalMachine;
328
329 RegistryKey rkParent = null;
330 foreach (string key in keyPathArray)
331 {
332 rkParent = rk;
333 rk = rk.OpenSubKey(key, true);
334 }
335
336 if (String.IsNullOrEmpty(name))
337 {
338 // delete the key and all of its children
339 string subkeyToDelete = keyPathArray[keyPathArray.Length - 1];
340 rkParent.DeleteSubKeyTree(subkeyToDelete);
341 Console.WriteLine("Deleted registry key: '{0}'", hive + "\\" + keyPath);
342 }
343 else
344 {
345 // just delete this value
346 rk.DeleteValue(name);
347 Console.WriteLine("Deleted registry key: '{0}' name: '{1}'", hive + "\\" + keyPath, name);
348 }
349 }
350 catch (Exception ex)
351 {
352 Console.WriteLine("Unable to delete registry key: '{0}'", hive + "\\" + keyPath);
353 Console.WriteLine(ex.Message);
354 }
355 }
356 else
357 {
358 Console.WriteLine("Unable to delete registry key.");
359 }
360
361 }
362
363 private bool parseRegKeyName(string delimittedData)
364 {
365 string[] splitString = delimittedData.Split(new string[] { "," }, StringSplitOptions.None);
366
367 if (splitString.Length > 2)
368 {
369 Console.WriteLine("Unable to parse registry key and name.");
370 return false;
371 }
372
373 keyPath = splitString[0];
374 if (splitString.Length == 2)
375 {
376 name = splitString[1];
377 }
378
379 if (keyPath.ToUpper().StartsWith("HKLM\\"))
380 {
381 hive = "HKLM";
382 keyPath = keyPath.Replace("HKLM\\", "");
383 }
384 else if (keyPath.ToUpper().StartsWith("HKCC\\"))
385 {
386 hive = "HKCC";
387 keyPath = keyPath.Replace("HKCC\\", "");
388 }
389 else if (keyPath.ToUpper().StartsWith("HKCU\\"))
390 {
391 hive = "HKCU";
392 keyPath = keyPath.Replace("HKCU\\", "");
393 }
394 else
395 {
396 Console.WriteLine("Invalid regkey. Unable to determine hive. regkey must start with either: [HKLM], [HKCU], or [HKCC]");
397 return false;
398 }
399 keyPathArray = keyPath.Split(new string[] { "\\" }, StringSplitOptions.None);
400 return true;
401 }
402 }
403
404 public class TaskParser 136 public class TaskParser
405 { 137 {
406 138
@@ -431,18 +163,23 @@ namespace TestExe
431 t = new SleepRandomTask(args[i + 1]); 163 t = new SleepRandomTask(args[i + 1]);
432 tasks.Add(t); 164 tasks.Add(t);
433 break; 165 break;
434 case "/log": 166 case "/lf":
435 t = new LogTask(args[i + 1], args); 167 t = new LargeFileTask(args[i + 1]);
436 tasks.Add(t); 168 tasks.Add(t);
437 break; 169 break;
438 case "/pinfo": 170 case "/log":
439 t = new ProcessInfoTask(args[i + 1]); 171 t = new LogTask(args[i + 1], args);
440 tasks.Add(t); 172 tasks.Add(t);
441 break; 173 break;
442 case "/fe": 174 case "/fe":
443 t = new FileExistsTask(args[i + 1]); 175 t = new FileExistsTask(args[i + 1]);
444 tasks.Add(t); 176 tasks.Add(t);
445 break; 177 break;
178#if NET35
179 case "/pinfo":
180 t = new ProcessInfoTask(args[i + 1]);
181 tasks.Add(t);
182 break;
446 case "/regw": 183 case "/regw":
447 t = new RegistryWriterTask(args[i + 1]); 184 t = new RegistryWriterTask(args[i + 1]);
448 tasks.Add(t); 185 tasks.Add(t);
@@ -451,6 +188,7 @@ namespace TestExe
451 t = new RegistryDeleterTask(args[i + 1]); 188 t = new RegistryDeleterTask(args[i + 1]);
452 tasks.Add(t); 189 tasks.Add(t);
453 break; 190 break;
191#endif
454 192
455 default: 193 default:
456 Console.WriteLine("Error: Invalid switch specified."); 194 Console.WriteLine("Error: Invalid switch specified.");