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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace TestExe
{
public abstract class Task
{
public string data;
public Task(string Data)
{
this.data = Data;
}
public abstract void RunTask();
}
public class ExitCodeTask : Task
{
public ExitCodeTask(string Data) : base(Data) { }
public override void RunTask()
{
// this task does nothing. Just stores data about what exit code to return.
}
}
public class SleepTask : Task
{
public SleepTask(string Data) : base(Data) { }
public override void RunTask()
{
int milliseconds = int.Parse(this.data);
Console.WriteLine("Starting to sleep for {0} milliseconds", milliseconds);
System.Threading.Thread.Sleep(milliseconds);
}
}
public class SleepRandomTask : Task
{
public SleepRandomTask(string Data) : base(Data) { }
public override void RunTask()
{
int low = int.Parse(this.data.Split(new string[] { ":" }, 2, StringSplitOptions.None)[0]);
int high = int.Parse(this.data.Split(new string[] { ":" }, 2, StringSplitOptions.None)[1]);
Random r = new Random();
int milliseconds = r.Next(high - low) + low;
Console.WriteLine("Starting to sleep for {0} milliseconds", milliseconds);
System.Threading.Thread.Sleep(milliseconds);
}
}
public class GenerateFilesTask : Task
{
public GenerateFilesTask(string Data) : base(Data) { }
public override void RunTask()
{
string[] tokens = this.data.Split(new char[] { '|' }, 2);
string folderPath = System.Environment.ExpandEnvironmentVariables(tokens[0]);
long size = long.Parse(tokens[1]);
Directory.CreateDirectory(folderPath);
var bytes = new byte[0];
for (long i = 1; i <= size; i++)
{
File.WriteAllBytes(Path.Combine(folderPath, $"{i}.txt"), bytes);
}
}
}
public class LargeFileTask : Task
{
public LargeFileTask(string Data) : base(Data) { }
public override void RunTask()
{
string[] tokens = this.data.Split(new char[] { '|' }, 2);
string filePath = System.Environment.ExpandEnvironmentVariables(tokens[0]);
long size = long.Parse(tokens[1]);
using (var stream = File.Create(filePath))
{
stream.Seek(size - 1, SeekOrigin.Begin);
stream.WriteByte(1);
}
}
}
public class LogTask : Task
{
string[] argsUsed;
public LogTask(string Data, string[] args)
: base(Data)
{
this.argsUsed = args;
}
public override void RunTask()
{
string logFile = "";
string argsUsedString = "";
foreach (string a in this.argsUsed)
{
argsUsedString += a + " ";
}
try
{
logFile = System.Environment.ExpandEnvironmentVariables(this.data);
Console.WriteLine("creating log file: " + logFile);
StreamWriter textFile = File.CreateText(logFile);
textFile.WriteLine("This is a log file created by TestExe.exe");
textFile.WriteLine("Args used: " + argsUsedString);
textFile.Close();
}
catch
{
Console.WriteLine("creating a log file failed for: {0}", logFile);
}
}
}
public class FileExistsTask : Task
{
public FileExistsTask(string Data) : base(Data) { }
public override void RunTask()
{
string fileToExist = System.Environment.ExpandEnvironmentVariables(this.data);
if (!String.IsNullOrEmpty(fileToExist))
{
Console.WriteLine("Waiting for this file to exist: \"" + fileToExist + "\"");
while (!System.IO.File.Exists(fileToExist))
{
System.Threading.Thread.Sleep(250);
}
Console.WriteLine("Found: \"" + fileToExist + "\"");
}
}
}
public class DeleteManifestsTask : Task
{
public DeleteManifestsTask(string Data) : base(Data) { }
public override void RunTask()
{
string filePath = System.Environment.ExpandEnvironmentVariables(this.data);
IntPtr type = new IntPtr(24); //RT_MANIFEST
IntPtr name = new IntPtr(1); //CREATEPROCESS_MANIFEST_RESOURCE_ID
DeleteResource(filePath, type, name, 1033);
}
private static void DeleteResource(string filePath, IntPtr type, IntPtr name, ushort language, bool throwOnError = false)
{
bool discard = true;
IntPtr handle = BeginUpdateResourceW(filePath, false);
try
{
if (handle == IntPtr.Zero)
{
throw new Win32Exception();
}
if (!UpdateResourceW(handle, type, name, language, IntPtr.Zero, 0))
{
throw new Win32Exception();
}
discard = false;
}
catch
{
if (throwOnError)
{
throw;
}
}
finally
{
if (handle != IntPtr.Zero)
{
if (!EndUpdateResourceW(handle, discard) && throwOnError)
{
throw new Win32Exception();
}
}
}
}
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
private extern static IntPtr BeginUpdateResourceW(string fileName, [MarshalAs(UnmanagedType.Bool)] bool deleteExistingResources);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private extern static bool UpdateResourceW(IntPtr hUpdate, IntPtr type, IntPtr name, ushort language, IntPtr pData, uint cb);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private extern static bool EndUpdateResourceW(IntPtr hUpdate, [MarshalAs(UnmanagedType.Bool)] bool discard);
}
public class TaskParser
{
public static List<Task> ParseTasks(string[] args)
{
List<Task> tasks = new List<Task>();
try
{
// for invalid args. return empty list
if (args.Length % 2 == 0)
{
Task t;
for (int i = 0; i < args.Length; i += 2)
{
switch (args[i].ToLower())
{
case "/ec":
t = new ExitCodeTask(args[i + 1]);
tasks.Add(t);
break;
case "/s":
t = new SleepTask(args[i + 1]);
tasks.Add(t);
break;
case "/sr":
t = new SleepRandomTask(args[i + 1]);
tasks.Add(t);
break;
case "/gf":
t = new GenerateFilesTask(args[i + 1]);
tasks.Add(t);
break;
case "/lf":
t = new LargeFileTask(args[i + 1]);
tasks.Add(t);
break;
case "/log":
t = new LogTask(args[i + 1], args);
tasks.Add(t);
break;
case "/fe":
t = new FileExistsTask(args[i + 1]);
tasks.Add(t);
break;
case "/dm":
t = new DeleteManifestsTask(args[i + 1]);
tasks.Add(t);
break;
#if NETFRAMEWORK
case "/pinfo":
t = new ProcessInfoTask(args[i + 1]);
tasks.Add(t);
break;
case "/regw":
t = new RegistryWriterTask(args[i + 1]);
tasks.Add(t);
break;
case "/regd":
t = new RegistryDeleterTask(args[i + 1]);
tasks.Add(t);
break;
#endif
default:
Console.WriteLine("Error: Invalid switch specified.");
return new List<Task>();
}
}
}
}
catch
{
Console.WriteLine("Error: Invalid switch data specified. Couldn't parse the data.");
return new List<Task>();
}
return tasks;
}
}
}
|