Esempio n. 1
0
func (n *Nop) Run(in chan Request, out chan data.TestCase, errOut chan error) {
	testCasesGenerated := 0
	testCasesPerSeed := make(map[string]int)

	for {
		req := <-in

		if len(req.SourceFiles) == 0 {
			close(out)
			break
		}

		sourceFile := req.SourceFiles[0]
		fileName := filepath.Base(sourceFile)
		outputFilePath := filepath.Join(n.WorkingDir, fileName)

		fileData, err := ioutil.ReadFile(sourceFile)
		if err != nil {
			errOut <- err
			continue
		}
		ioutil.WriteFile(outputFilePath, fileData, 0777)

		// If this is the first time we've seen this seed then initialize
		// its test case count to 0
		_, ok := testCasesPerSeed[sourceFile]
		if !ok {
			testCasesPerSeed[sourceFile] = 0
		}

		testCasesGenerated++
		testCasesPerSeed[sourceFile]++

		testCase := data.NewTestCase()
		testCase.FuzzFilePath = outputFilePath
		testCase.SeedFilePaths = []string{sourceFile}
		testCase.SeedFuzzCounts[sourceFile] = testCasesPerSeed[sourceFile]
		testCase.TotalFuzzCount = testCasesGenerated

		out <- testCase
	}
}
Esempio n. 2
0
// Run starts a work loop that consumes MutateRequests specifying a source file
// and the number of fuzz files to generate. As output it produces the paths to
// each of these fuzz files. On error a message will be sent on the errOut
// channel.
func (r *Radamsa) Run(in chan Request, out chan data.TestCase,
	errOut chan error) {

	cfg := r.S.Config
	// Used to change the seed on each iteration
	seedInc := 1
	testCasesGenerated := 0
	testCasesPerSeed := make(map[string]int)

	for {
		req := <-in

		if len(req.SourceFiles) == 0 {
			close(out)
			break
		}

		if len(req.SourceFiles) > 1 {
			log.Fatalf("For multiple source files use RadamsaMultiFile, not" +
				" Radamsa")
		}

		sourceFile := req.SourceFiles[0]
		fileName := filepath.Base(sourceFile)
		outputFileName := fmt.Sprintf("%%n_%s", fileName)

		var workingDir string
		if cfg.TestProcessing.GenerateTestsInPlace {
			workingDir = filepath.Dir(sourceFile)
		} else {
			workingDir = r.S.TestCasesDir
		}

		outputFilePath := filepath.Join(workingDir, outputFileName)

		countStr := strconv.Itoa(req.Count)

		seedStr := strconv.Itoa(cfg.General.Seed + seedInc)
		seedInc++
		cmdList := []string{}
		if len(cfg.Radamsa.Mutations) != 0 {
			cmdList = append(cmdList, "-m")
			cmdList = append(cmdList, cfg.Radamsa.Mutations)
		}
		cmdList = append(cmdList, "--seed")
		cmdList = append(cmdList, seedStr)
		cmdList = append(cmdList, "-n")
		cmdList = append(cmdList, countStr)
		cmdList = append(cmdList, "-o")
		cmdList = append(cmdList, outputFilePath)
		cmdList = append(cmdList, sourceFile)

		r.L.DEBUGF("Running radamsa with the following arguments : %s",
			cmdList)
		cmd := exec.Command("radamsa", cmdList...)

		if err := cmd.Run(); err != nil {
			msg := fmt.Sprintf("Error running radamsa: %s", err)
			err := errors.New(msg)
			errOut <- err
			continue
		}

		// If this is the first time we've seen this seed then initialize
		// its test case count to 0
		_, ok := testCasesPerSeed[sourceFile]
		if !ok {
			testCasesPerSeed[sourceFile] = 0
		}

		for i := 0; i < req.Count; i++ {
			expectedFileName := fmt.Sprintf("%d_%s", i+1, fileName)
			var expectedFilePath string
			var err error
			if expectedFilePath, err = filepath.Abs(
				filepath.Join(workingDir, expectedFileName)); err != nil {
				err := errors.New(
					fmt.Sprintf("Fuzz file %s was not generated",
						expectedFilePath))
				errOut <- err
				continue
			}

			if _, err := os.Stat(expectedFilePath); err != nil {
				err := errors.New(
					fmt.Sprintf("Fuzz file %s was not generated",
						expectedFilePath))
				errOut <- err
				continue
			}

			testCasesGenerated++
			testCasesPerSeed[sourceFile]++

			testCase := data.NewTestCase()
			testCase.FuzzFilePath = expectedFilePath
			testCase.SeedFilePaths = req.SourceFiles
			testCase.SeedFuzzCounts[sourceFile] = testCasesPerSeed[sourceFile]
			testCase.TotalFuzzCount = testCasesGenerated

			out <- testCase
		}
	}
}