|
136 | 136 |
|
137 | 137 |
|
138 | 138 |
|
| 139 | +--- Sandbox handler of the "consoleCommand" keyword. |
| 140 | +-- Simulates an admin executing a console command |
| 141 | +local function sandboxConsoleCommand(a_Table) |
| 142 | + -- Check the attributes: |
| 143 | + local command = a_Table.command |
| 144 | + if not(command) then |
| 145 | + error("Error in scenario file, consoleCommand doesn't have the required \"command\" attribute", 2) |
| 146 | + end |
| 147 | + |
| 148 | + -- Return the action implementation: |
| 149 | + return function(a_Simulator) |
| 150 | + a_Simulator.logger:trace( |
| 151 | + "Scenario: processing action \"consoleCommand\", command \"%s\".", |
| 152 | + command |
| 153 | + ) |
| 154 | + a_Simulator:executeConsoleCommand(command) |
| 155 | + end |
| 156 | +end |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + |
139 | 162 | --- Fuzzes a single command |
140 | 163 | -- a_Simulator is the simulator instance on which to fuzz the commands |
141 | 164 | -- a_Command is the registered command (string) being fuzzed |
|
176 | 199 |
|
177 | 200 |
|
178 | 201 |
|
| 202 | +--- Fuzzes a single console command |
| 203 | +-- a_Simulator is the simulator instance on which to fuzz the commands |
| 204 | +-- a_Command is the registered command (string) being fuzzed |
| 205 | +-- a_Choices is the array-table of choices for the command parameters |
| 206 | +-- a_MinLen is the minimum length of the fuzzed command parameter array |
| 207 | +-- a_MaxLen is the maximum length of the fuzzed command parameter array |
| 208 | +local function fuzzConsoleCommand(a_Simulator, a_Command, a_Choices, a_MinLen, a_MaxLen) |
| 209 | + -- Recursively fuzzes a single console command |
| 210 | + -- a_CurrentIndex is the index into the a_Split array specifying the index that this recursion level should modify |
| 211 | + -- a_Split is the command params split array |
| 212 | + -- The recursion is called "backwards", the last param is chosen first and then the previous param is recursed |
| 213 | + -- When a_CurrentIndex is zero, the actual command handlers are invoked |
| 214 | + local function fuzzSingleCommand(a_Simulator, a_Command, a_Choices, a_NumChoices, a_CurrentIndex, a_Split) |
| 215 | + if (a_CurrentIndex == 0) then |
| 216 | + -- We've built the whole command, serialize the params into a string and execute it: |
| 217 | + a_Simulator.logger:info("Scenario: fuzzing console command \"%s\".", a_Command .. " " .. table.concat(a_Split, " ")) |
| 218 | + a_Simulator:executeConsoleCommand(a_Command .. " " .. table.concat(a_Split, " ")) |
| 219 | + -- Process all queued callbacks: |
| 220 | + a_Simulator:processAllQueuedCallbackRequests() |
| 221 | + return |
| 222 | + end |
| 223 | + |
| 224 | + -- Try all choices on position <a_CurrentIndex> and recurse: |
| 225 | + for ch = 1, a_NumChoices do |
| 226 | + a_Split[a_CurrentIndex] = a_Choices[ch] |
| 227 | + fuzzSingleCommand(a_Simulator, a_Command, a_Choices, a_NumChoices, a_CurrentIndex - 1, a_Split) |
| 228 | + end |
| 229 | + end |
| 230 | + |
| 231 | + -- Start the fuzzing: |
| 232 | + for len = a_MinLen, a_MaxLen do |
| 233 | + fuzzSingleCommand(a_Simulator, a_Command, a_Choices, #a_Choices, len, {}) |
| 234 | + end -- for len - number of chosen params |
| 235 | +end |
| 236 | + |
| 237 | + |
| 238 | + |
| 239 | + |
| 240 | + |
179 | 241 | --- Sandbox handler of the "fuzzAllCommands" keyword. |
180 | 242 | -- Simulates a player executing each registered command with all kinds of parameters |
181 | 243 | local function sandboxFuzzAllCommands(a_Table) |
|
213 | 275 |
|
214 | 276 |
|
215 | 277 |
|
| 278 | +--- Sandbox handler of the "fuzzConsoleCommand" keyword. |
| 279 | +-- Simulates an admin executing a registered console command with all kinds of parameters |
| 280 | +local function sandboxFuzzConsoleCommand(a_Table) |
| 281 | + -- Check the attributes: |
| 282 | + local choices = a_Table.choices |
| 283 | + if (not(choices) or (type(choices) ~= "table") or not(choices[1])) then |
| 284 | + error("Error in scenario file, fuzzConsoleCommand doesn't have the required \"choices\" array-table attribute", 2) |
| 285 | + end |
| 286 | + local cmd = a_Table.command |
| 287 | + if not(cmd) then |
| 288 | + error("Error in scenario file, fuzzConsoleCommand doesn't have the required \"command\" string attribute", 2) |
| 289 | + end |
| 290 | + local maxLen = tonumber(a_Table.maxLen) |
| 291 | + if not(maxLen) then |
| 292 | + error("Error in scenario file, fuzzConsoleCommand doesn't have the required \"maxLen\" number attribute", 2) |
| 293 | + end |
| 294 | + a_Table.maxLen = maxLen |
| 295 | + a_Table.minLen = a_Table.minLen or 0 |
| 296 | + |
| 297 | + -- Return the action implementation: |
| 298 | + return function(a_Simulator) |
| 299 | + a_Simulator.logger:trace("Scenario: processing action \"fuzzConsoleCommand(\"%s\")\".", cmd) |
| 300 | + fuzzConsoleCommand(a_Simulator, cmd, a_Table.choices, a_Table.minLen, a_Table.maxLen) |
| 301 | + end |
| 302 | +end |
| 303 | + |
| 304 | + |
| 305 | + |
| 306 | + |
| 307 | + |
| 308 | +--- Sandbox handler of the "fuzzAllConsoleCommands" keyword. |
| 309 | +-- Simulates an admin executing each registered console command with all kinds of parameters |
| 310 | +local function sandboxFuzzAllConsoleCommands(a_Table) |
| 311 | + -- Check the attributes: |
| 312 | + local choices = a_Table.choices |
| 313 | + if (not(choices) or (type(choices) ~= "table") or not(choices[1])) then |
| 314 | + error("Error in scenario file, fuzzAllConsoleCommands doesn't have the required \"choices\" array-table attribute", 2) |
| 315 | + end |
| 316 | + local maxLen = tonumber(a_Table.maxLen) |
| 317 | + if not(maxLen) then |
| 318 | + error("Error in scenario file, fuzzAllConsoleCommands doesn't have the required \"maxLen\" number attribute", 2) |
| 319 | + end |
| 320 | + a_Table.maxLen = maxLen |
| 321 | + a_Table.minLen = a_Table.minLen or 0 |
| 322 | + |
| 323 | + -- Return the action implementation: |
| 324 | + return function(a_Simulator) |
| 325 | + a_Simulator.logger:trace("Scenario: processing action \"fuzzAllConsoleCommands\".") |
| 326 | + for cmd, cmdReg in pairs(a_Simulator.registeredConsoleCommandHandlers) do |
| 327 | + fuzzConsoleCommand(a_Simulator, cmd, a_Table.choices, a_Table.minLen, a_Table.maxLen) |
| 328 | + end |
| 329 | + end |
| 330 | +end |
| 331 | + |
| 332 | + |
| 333 | + |
| 334 | + |
| 335 | + |
216 | 336 | --- Sandbox handler of the "initializePlugin" keyword. |
217 | 337 | -- Initializes the plugin (and loads it before that if not loaded yet) |
218 | 338 | local function sandboxInitializePlugin(a_Table) |
@@ -397,21 +517,24 @@ end |
397 | 517 | -- Provides only the scenario functions |
398 | 518 | local scenarioSandbox = |
399 | 519 | { |
400 | | - scenario = nil, -- Will be explicitly modified for each file being loaded |
401 | | - redirect = sandboxRedirect, |
402 | | - world = sandboxWorld, |
403 | | - connectPlayer = sandboxConnectPlayer, |
404 | | - playerCommand = sandboxPlayerCommand, |
405 | | - fuzzAllCommands = sandboxFuzzAllCommands, |
406 | | - initializePlugin = sandboxInitializePlugin, |
407 | | - loadPluginFiles = sandboxLoadPluginFiles, |
408 | | - fsCreateFile = sandboxFsCreateFile, |
409 | | - fsCopyFile = sandboxFsCopyFile, |
410 | | - fsRenameFile = sandboxFsRenameFile, |
411 | | - fsDeleteFile = sandboxFsDeleteFile, |
412 | | - fsCreateFolder = sandboxFsCreateFolder, |
413 | | - fsRenameFolder = sandboxFsRenameFolder, |
414 | | - fsDeleteFolder = sandboxFsDeleteFolder, |
| 520 | + scenario = nil, -- Will be explicitly modified for each file being loaded |
| 521 | + redirect = sandboxRedirect, |
| 522 | + world = sandboxWorld, |
| 523 | + connectPlayer = sandboxConnectPlayer, |
| 524 | + playerCommand = sandboxPlayerCommand, |
| 525 | + fuzzAllCommands = sandboxFuzzAllCommands, |
| 526 | + consoleCommand = sandboxConsoleCommand, |
| 527 | + fuzzConsoleCommand = sandboxFuzzConsoleCommand, |
| 528 | + fuzzAllConsoleCommands = sandboxFuzzAllConsoleCommands, |
| 529 | + initializePlugin = sandboxInitializePlugin, |
| 530 | + loadPluginFiles = sandboxLoadPluginFiles, |
| 531 | + fsCreateFile = sandboxFsCreateFile, |
| 532 | + fsCopyFile = sandboxFsCopyFile, |
| 533 | + fsRenameFile = sandboxFsRenameFile, |
| 534 | + fsDeleteFile = sandboxFsDeleteFile, |
| 535 | + fsCreateFolder = sandboxFsCreateFolder, |
| 536 | + fsRenameFolder = sandboxFsRenameFolder, |
| 537 | + fsDeleteFolder = sandboxFsDeleteFolder, |
415 | 538 | } |
416 | 539 |
|
417 | 540 |
|
|
0 commit comments