-
Notifications
You must be signed in to change notification settings - Fork 0
/
CopyFuzz.cs
230 lines (213 loc) · 8.16 KB
/
CopyFuzz.cs
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
using System;
using System.Collections.Generic;
using System.IO;
namespace CopyFuzz
{
/// <summary>
/// Starts a single instance of the application under test.
/// </summary>
/// <param name="seed">Seed for random number generators.</param>
/// <returns>An <see cref="ICopyFuzzifyer" /> object that FuzzBalls can manipulate.</returns>
public delegate ICopyFuzzifyer ApplicationStarter(int seed);
/// <summary>
/// A CopyFuzz tester object. When given a function that will generate an <see cref="ICopyFuzzifyer" /> object
/// handles console I/O.
/// </summary>
public class CopyFuzz
{
private const string Prompt = " > ";
private readonly ApplicationStarter _applicationStarter;
private int _fuzzIterations = 100;
private string _path = "learnedpaths.txt";
/// <summary>
/// Starting point for CopyFuzz testing.
/// </summary>
/// <param name="applicationStarter">Delegate that will create a new instance of the application under test.</param>
public CopyFuzz(ApplicationStarter applicationStarter)
{
Welcome();
_applicationStarter = applicationStarter;
var seed = new Random().Next();
var exit = false;
while (!exit)
{
Console.Write(Prompt);
var input = Console.ReadLine()?.ToLower();
if (input == null) continue;
var splinput = input.Split(' ');
Switch(splinput, seed, exit, input);
}
}
private void Switch(string[] splinput, int seed, bool exit, string input)
{
switch (splinput[0])
{
case "learn":
Learn(seed);
break;
case "test":
Test(splinput.Length != 2 ? 1 : int.Parse(splinput[1]));
break;
case "path":
SetPath(splinput);
break;
case "reset":
File.Delete(_path);
break;
case "exit":
exit = true;
break;
case "param":
Params(splinput);
break;
case "?":
Help();
break;
case "":
break;
default:
Console.WriteLine($"'{input}' not a valid command. '?' for help");
break;
}
}
/// <summary>
/// Sets the path of the file the copyfuzz reads and writes to.
/// </summary>
/// <param name="splinput">Split input arguments.</param>
/// <remarks>Just a text file.</remarks>
private void SetPath(IReadOnlyList<string> splinput)
{
if (splinput.Count < 2)
{
Console.WriteLine(@"path requires a path argument");
return;
}
if (!File.Exists(splinput[1]))
{
Console.WriteLine(@"file does not exist");
return;
}
_path = splinput[1];
Console.WriteLine($"path set to {_path}");
}
/// <summary>
/// Starts CopyFuzz in learning mode, where it will listen to and record your input as you move through the
/// application.
/// </summary>
/// <param name="seed">The seed to start the application with.</param>
private void Learn(int seed)
{
var sw = new StreamWriter(_path, true);
// ReSharper disable once UnusedVariable
var student = new StudentFuzzBall(_applicationStarter, sw, seed);
sw.Close();
}
/// <summary>
/// Starts CopyFuzz in testing mode, where it will begin randomly providing inputs to the application.
/// </summary>
/// <param name="iterations">How many time you want to run testing mode.</param>
private void Test(int iterations)
{
if (!File.Exists(_path))
{
Console.WriteLine(@"No user session data found");
Console.WriteLine(@"Run CopyFuzz on your application in learn mode first");
return;
}
for (var i = 0; i < iterations; i++)
{
var sr = new StreamReader(_path);
// ReSharper disable once UnusedVariable
var tester = new TesterFuzzBall(_applicationStarter, sr, _fuzzIterations);
sr.Close();
}
}
/// <summary>
/// Prints help.
/// </summary>
private static void Help()
{
Console.WriteLine(@"");
Console.WriteLine(@"cmd desc");
Console.WriteLine(@"");
Console.WriteLine(@"learn starts the application and records user input");
Console.WriteLine(@"test [n] starts the application n times and applys copyfuzz");
Console.WriteLine(@"reset deletes the file containing user sessions");
Console.WriteLine(@"param [n] [v] settings and paramaters used while testing");
Console.WriteLine(@"exit");
Console.WriteLine(@"?");
Console.WriteLine(@"");
}
/// <summary>
/// Prints "splash screen".
/// </summary>
private static void Welcome()
{
Console.WriteLine(@"");
Console.WriteLine(@"");
Console.WriteLine(@"CopyFuzz v0.1");
Console.WriteLine(@"");
}
/// <summary>
/// Handles reading and setting of various testing parameters.
/// </summary>
/// <param name="args">The arguments that the user called 'param' with.</param>
private void Params(IReadOnlyList<string> args)
{
Console.WriteLine(@"");
// ReSharper disable once ConvertIfStatementToSwitchStatement
if (args.Count == 1)
{
oneArgPrint();
}
else if (args.Count == 2)
{
if (args[1] == "fuzz")
{
Console.Write(MakeConstWidthString("fuzz", 10));
Console.WriteLine(_fuzzIterations);
} else if (args[1] == "path")
{
Console.Write(MakeConstWidthString("path", 10));
Console.WriteLine(_path);
}
}
else if (args.Count == 3)
{
int n;
if (args[1] == "fuzz" && int.TryParse(args[2], out n))
{
Console.Write($"changed fuzz from {_fuzzIterations} to {n}");
_fuzzIterations = n;
}
else if (args[1] == "path" && File.Exists(args[2]))
{
Console.Write($"changed path from {_path} to {args[2]}");
_path = args[2];
}
}
Console.WriteLine(@"");
}
private void oneArgPrint()
{
Console.WriteLine(@"usage: param [name] [value]");
Console.WriteLine(@"");
Console.WriteLine(@"name value");
Console.Write(MakeConstWidthString("fuzz", 10));
Console.WriteLine(_fuzzIterations);
Console.Write(MakeConstWidthString("path", 10));
Console.WriteLine(_path);
}
/// <summary>
/// Forces the given string to be the given width.
/// </summary>
/// <param name="string">Original string.</param>
/// <param name="width">Desired width.</param>
/// <returns>A new string that is either a substring of the original or the orinial concated with spaces.</returns>
private static string MakeConstWidthString(string @string, int width)
{
while (@string.Length < width) @string += " ";
return @string.Length > width ? @string.Substring(0, width) : @string;
}
}
}