Beispiel #1
0
func testFuzz(t *testing.T, srcname, start string) {
	var err error
	var grammar ebnf.Grammar
	var tempfile *os.File

	grammar, err = rebnf.Parse(srcname, nil)
	if err != nil {
		t.Error(err)
	}
	dst := new(bytes.Buffer)
	ctx := rebnf.NewCtx(20, 20, " ", false)
	err = ctx.Random(dst, grammar, start)
	if err != nil {
		t.Error(err)
	}
	dstbytes := dst.Bytes()

	// Test gofmt.
	testFmt(t, "-", bytes.NewBuffer(dstbytes))

	// Test go build.
	tempfile, err = ioutil.TempFile("test", "tmp_")
	if err != nil {
		t.Error(err)
	}
	tempfile.Close()
	testBuild(t, tempfile.Name(), "-", bytes.NewBuffer(dstbytes))
}
Beispiel #2
0
func main() {
	var (
		name string
		r    io.Reader
	)
	switch flag.NArg() {
	case 0:
		name, r = "-", os.Stdin
	case 1:
		name = flag.Arg(0)
	default:
		usage()
	}

	grammar, err := rebnf.Parse(name, r)
	if err != nil {
		log.Fatal(err)
	}

	// Verify grammar before attempting any productions.
	err = ebnf.Verify(grammar, args.start)
	if err != nil {
		log.Fatal(err)
	}

	ctx := rebnf.NewCtx(args.maxreps, args.maxdepth, args.padding, args.debug)
	log.Printf("seed %d", args.seed)
	err = ctx.Random(os.Stdout, grammar, args.start)
	if err != nil {
		log.Fatal(err)
	}
}
Beispiel #3
0
func main() {
	args := os.Args
	if len(args) != 2 && len(args) != 3 {
		log.Fatalln("Usage: " + os.Args[0] + " NUM_PRODUCTIONS [PRODUCTION=statement]")
	}
	numProductions, err := strconv.Atoi(args[1])
	if err != nil {
		log.Fatalln(err)
	}

	production := "statement"
	if len(args) == 3 {
		production = args[2]
	}

	grammar, err := rebnf.Parse("influxql.ebnf", nil)
	if err != nil {
		log.Fatalln(err)
	}

	if _, ok := grammar[production]; !ok {
		log.Fatalln(production + " is not a known production!")
	}

	maxRepetitions := 30
	maxRecursionDepth := 15
	padding := "???"
	isDebug := false
	ctx := rebnf.NewCtx(maxRepetitions, maxRecursionDepth, padding, isDebug)

	for i := 0; i < numProductions; i++ {
		buf := new(bytes.Buffer)
		ctx.Random(buf, grammar, production)

		line, _ := ioutil.ReadAll(buf)

		fmt.Println("GENERATED: " + string(line))
		if stmt, err := influxql.ParseStatement(string(line)); err != nil {
			fmt.Fprintln(os.Stderr, "ERROR    : "+err.Error())
		} else {
			sanitized := stmt.String()
			fmt.Println("SANITIZED: " + sanitized)
			resanitized := influxql.MustParseStatement(sanitized).String()
			if resanitized != sanitized {
				fmt.Println("IMPURE   : " + resanitized)
			}
		}

		fmt.Println()
	}
}