Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add info to README for how to run the fuzzer #136

Open
jasikpark opened this issue Feb 23, 2022 · 0 comments
Open

Add info to README for how to run the fuzzer #136

jasikpark opened this issue Feb 23, 2022 · 0 comments

Comments

@jasikpark
Copy link

jasikpark commented Feb 23, 2022

It was unclear how to run the fuzzer - I eventually figured out it should be compiled with tsc and run with node:

import { chunkStringIntoFours } from "../src/chunkStringIntoFours";

function chunkStringIntoFoursForLoop(input: string): string {
  if (typeof input !== "string") {
    throw Error("chunkStringIntoFours can only chunk strings");
  }
  if (/[\uD800-\uDBFF]/.test(input)) {
    throw Error("chunkStringIntoFours does not support non BMP strings");
  }
  if (/[\r\n]/.test(input)) {
    throw Error("chunkStringIntoFours does not support whitespace");
  }

  const array = [];

  // make input.length / 4 strings that contain 4 characters
  for (let i = 0; i < input.length; i++) {
    if (i % 4 === 0) {
      array[Math.floor(i / 4)] = input[i];
    } else {
      array[Math.floor(i / 4)] += input[i];
    }
  }

  return array.join(" ");
}

import { fuzz } from "fuzzing";

const errors = fuzz((input) => {
  try {
    if (chunkStringIntoFours(input) !== chunkStringIntoFoursForLoop(input)) {
        throw Error("chunkStringIntoFours did not output the same as chunkStringIntoFoursForLoop");
    }
  } catch (e: unknown) {
    if (
      e instanceof Error &&
      ["chunkStringIntoFours can only chunk strings"].includes(e.message)
    ) {
      // no error
      return true;
    }

    throw e;
  }
})
  .string()
  .errors();

console.log(errors);

Example script that I made that imports the function to be tested and then I have an npm script:

{
	"scripts": {
		"fuzz": "tsc --incremental && node ./dist/fuzz/chunkStringIntoFours.js",
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant