Permalink
Browse files

OS.parse fuzzer

  • Loading branch information...
1 parent e51e7b1 commit b5087602cb93d2b5741f8fd80d3284be4a32fdd5 Tom Robinson committed Mar 2, 2010
Showing with 54 additions and 0 deletions.
  1. +54 −0 tests/os/parse-fuzzer.js
View
@@ -0,0 +1,54 @@
+var OS = require("os");
+var ASSERT = require("assert");
+var UTIL = require("UTIL");
+var stream = require("term").stream;
+
+var random = false;
+var tokens = ["x", "'", '"', " "];
+
+function shellParse(argString) {
+ var p = OS.popen(["sh", "-c", 'for i in "$@"; do echo "$i"; done', "--"].map(OS.enquote).join(" ") + " " + argString);
+ var result = p.stdout.read().split("\n").slice(0,-1);
+ if (p.wait())
+ throw result;
+ return result;
+}
+
+var testIndex = -1;
+function buildTestString() {
+
+ if (random) {
+ var components = [];
+ var n = Math.round(Math.random()*10);
+ while (components.length < n)
+ components.push(tokens[Math.floor(Math.random()*tokens.length)]);
+ return components.join("");
+ }
+ else {
+ var index = testIndex++;
+ if (index < 0)
+ return "";
+ // convert to a base tokens.length number and replace each digit with corresponding token
+ return index.toString(tokens.length).replace(/./g, function(x) {
+ return tokens[parseInt(x, tokens.length)];
+ });
+ }
+}
+
+while (true) {
+ var argString = buildTestString();
+
+ try {
+ var expectedArgs = shellParse(argString);
+ } catch (e) {
+ // TODO: test invalid behavior matches?
+ continue;
+ }
+
+ var actualArgs = OS.parse(argString);
+
+ var pass = UTIL.eq(expectedArgs, actualArgs);
+
+ stream.print("[" + testIndex + "] " + (pass ? "\0green(PASS\0)" : "\0red(FAIL\0)") +
+ ": string=["+argString+"] expected=["+expectedArgs+"] actual=["+actualArgs+"]");
+}

0 comments on commit b508760

Please sign in to comment.