/*
Fuzz testing support files

https://github.com/dvyukov/go-fuzz

Usage:

    go-fuzz-build github.com/lytics/confl
    mkdir fuzz
    cp _examples/*.conf fuzz/
    go-fuzz -bin=confl-fuzz.zip -workdir=fuzz

See fuzz/crashers for results.
*/
338348
func Fuzz(data []byte) int {
	var v map[string]interface{}
	if err := Unmarshal(data, &v); err != nil {
		return 0
	}
	return 1
}
343860
func (p *parser) parsePackage(fileNames []string) error {
	if len(fileNames) == 0 {
		return fmt.Errorf("fileNames is empty")
	}
	for i, name := range fileNames {
		if i > 0 && filepath.Dir(name) != filepath.Dir(fileNames[0]) {
			return fmt.Errorf("Go files belong to different directories")
		}
		if !strings.HasSuffix(name, ".go") {
			continue
		}
		file, err := goparser.ParseFile(p.fileSet, name, nil, 0)
		if err != nil {
			return fmt.Errorf("parsing %s: %s", name, err)
		}
		if len(p.files) > 0 && file.Name.Name != p.files[0].Name.Name {
			return fmt.Errorf("Go files belong to different packages")
		}
		p.files = append(p.files, file)
	}
	if len(p.files) == 0 {
		return fmt.Errorf("no buildable Go files")
	}
	return nil
}
259294
func (c Continue) Fuzz(obj interface{}) {
	v := reflect.ValueOf(obj)
	if v.Kind() != reflect.Ptr {
		panic("needed ptr!")
	}
	v = v.Elem()
	c.fc.doFuzz(v, 0)
}
732200
func FuzzSmat(data []byte) int {
	return smat.Fuzz(&smatContext{}, smat.ActionID('S'), smat.ActionID('T'),
		smatActionMap, data)
}
584148
func Fuzz(data []byte) int {
	// Because go-fuzz requires this function signature with a []byte parameter,
	// and we want to emulate the behavior of mainScenario in lnwire_test.go,
	// we first parse the []byte parameter into a Message type.

	// Parsing []byte into Message
	r := bytes.NewReader(data)
	msg, err := lnwire.ReadMessage(r, 0)
	if err != nil {
		// Ignore this input - go-fuzz generated []byte that cannot be represented as Message
		return 0
	}

	// We will serialize Message into a new bytes buffer
	var b bytes.Buffer
	if _, err := lnwire.WriteMessage(&b, msg, 0); err != nil {
		// Could not serialize Message into bytes buffer, panic
		panic(err)
	}

	// Make sure serialized bytes buffer (excluding 2 bytes for message type
	// is less than max payload size for this specific message,.
	payloadLen := uint32(b.Len()) - 2
	if payloadLen > msg.MaxPayloadLength(0) {
		// Ignore this input - max payload constraint violated
		return 0
	}

	// Deserialize the message from the serialized bytes buffer and
	// assert that the original message is equal to the newly deserialized message.
	newMsg, err := lnwire.ReadMessage(&b, 0)
	if err != nil {
		// Could not deserialize message from bytes buffer, panic
		panic(err)
	}
	if !reflect.DeepEqual(msg, newMsg) {
		// Deserialized message and original message are not deeply equal
		panic(fmt.Errorf("Deserialized message and original message " +
			"are not deeply equal."))
	}

	return 1
}
31959
func (c *CommandConfig) InitGoPaths() {
	utils.Logger.Info("InitGoPaths")
	// lookup go path
	c.GoPath = build.Default.GOPATH
	if c.GoPath == "" {
		utils.Logger.Fatal("Abort: GOPATH environment variable is not set. " +
			"Please refer to http://golang.org/doc/code.html to configure your Go environment.")
	}

	// check for go executable
	var err error
	c.GoCmd, err = exec.LookPath("go")
	if err != nil {
		utils.Logger.Fatal("Go executable not found in PATH.")
	}

	// revel/revel#1004 choose go path relative to current working directory

	// What we want to do is to add the import to the end of the
	// gopath, and discover which import exists - If none exist this is an error except in the case
	// where we are dealing with new which is a special case where we will attempt to target the working directory first
	workingDir, _ := os.Getwd()
	goPathList := filepath.SplitList(c.GoPath)
	bestpath := ""
	for _, path := range goPathList {
		if c.Index == NEW {
			// If the GOPATH is part of the working dir this is the most likely target
			if strings.HasPrefix(workingDir, path) {
				bestpath = path
			}
		} else {
			if utils.Exists(filepath.Join(path, "src", c.ImportPath)) {
				c.SrcRoot = path
				break
			}
		}
	}

	utils.Logger.Info("Source root", "path", c.SrcRoot, "cwd", workingDir, "gopath", c.GoPath, "bestpath",bestpath)
	if len(c.SrcRoot) == 0 && len(bestpath) > 0 {
		c.SrcRoot = bestpath
	}

	// If source root is empty and this isn't a version then skip it
	if len(c.SrcRoot) == 0 {
		if c.Index != VERSION {
			utils.Logger.Fatal("Abort: could not create a Revel application outside of GOPATH.")
		}
		return
	}

	// set go src path
	c.SrcRoot = filepath.Join(c.SrcRoot, "src")

	c.AppPath = filepath.Join(c.SrcRoot, filepath.FromSlash(c.ImportPath))
	utils.Logger.Info("Set application path", "path", c.AppPath)
}
682006
func packDir(srcPath string, recPath string, zw *zip.Writer, fn cae.HookFunc) error {
	dir, err := os.Open(srcPath)
	if err != nil {
		return err
	}
	defer dir.Close()

	fis, err := dir.Readdir(0)
	if err != nil {
		return err
	}
	for _, fi := range fis {
		if cae.IsFilter(fi.Name()) {
			continue
		}
		curPath := srcPath + "/" + fi.Name()
		tmpRecPath := filepath.Join(recPath, fi.Name())
		if err = fn(curPath, fi); err != nil {
			continue
		}

		if fi.IsDir() {
			if err = packFile(srcPath, tmpRecPath, zw, fi); err != nil {
				return err
			}
			err = packDir(curPath, tmpRecPath, zw, fn)
		} else {
			err = packFile(curPath, tmpRecPath, zw, fi)
		}
		if err != nil {
			return err
		}
	}
	return nil
}
684218
func fuzzDecode(data []byte) int {
	blob, err := bitsetDecodeBytes(data, 1024)
	if err != nil {
		return 0
	}
	if comp := bitsetEncodeBytes(blob); !bytes.Equal(comp, data) {
		panic("content mismatch")
	}
	return 0
}
951612
func Fuzz(data []byte) int {
	// check for zero length data
	if len(data) == 0 {
		return 1
	}

	// detect packet
	_, mt := DetectPacket(data)

	// for testing purposes we will not cancel
	// on incomplete buffers

	// create a new packet
	pkt, err := mt.New()
	if err != nil {
		return 0
	}

	// decode it from the buffer.
	_, err = pkt.Decode(data)
	if err != nil {
		return 0
	}

	// everything was ok
	return 1
}
1178638
func (f FileInfo) GoPluginOutputFilename() string {
	name := f.Name
	ext := path.Ext(name)
	if ext == ".proto" || ext == ".protodevel" {
		name = name[0 : len(name)-len(ext)]
	}
	return name + ".pb.go"
}
1027740
func CompileIgnoreFileAndLines(fpath string, lines ...string) (*GitIgnore, error) {
	buffer, error := ioutil.ReadFile(fpath)
	if error == nil {
		s := strings.Split(string(buffer), "\n")
		return CompileIgnoreLines(append(s, lines...)...)
	}
	return nil, error
}
286374
func (pb *PackageBuilder) addFileToWorkingDir(metaName string, fpath string) (err error) {
	// Get relative dir from source
	srcDir := filepath.Dir(filepath.Dir(fpath))
	for _, mp := range metapaths {
		if metaName == mp.name && mp.hasFolder {
			srcDir = filepath.Dir(srcDir)
		}
	}
	frel, _ := filepath.Rel(srcDir, fpath)

	// Try to find meta file
	hasMeta := true
	fmeta := fpath + "-meta.xml"
	fmetarel := ""
	if _, err = os.Stat(fmeta); err != nil {
		if os.IsNotExist(err) {
			hasMeta = false
		} else {
			// Has error
			return
		}
	} else {
		// Should be present since we worked back to srcDir
		fmetarel, _ = filepath.Rel(srcDir, fmeta)
	}

	fdata, err := ioutil.ReadFile(fpath)
	if err != nil {
		return
	}

	pb.Files[frel] = fdata
	if hasMeta {
		fdata, err = ioutil.ReadFile(fmeta)
		pb.Files[fmetarel] = fdata
		return
	}

	return
}
330448
func goGetFiles(dir string, pkgs []string) []string {
	params := []string{
		"Dir",
		"GoFiles",
		"CgoFiles",
	}
	var allFiles []string
	rawTuples := goRun(goList(params, pkgs))
	for _, raw := range rawTuples {
		tuple := goSliceRawTuple(raw)
		moduleDir := filepath.Clean(tuple[0])
		dirSep := fmt.Sprintf("%s%c", dir, filepath.Separator)
		moduleDirSep := fmt.Sprintf("%s%c", moduleDir, filepath.Separator)
		if !strings.HasPrefix(moduleDirSep, dirSep) {
			continue
		}
		relModuleDir, err := filepath.Rel(dir, moduleDir)
		if err != nil {
			common.Die("Could not make a relative path from %q to %q, even if they have the same prefix", moduleDir, dir)
		}
		files := append(goSliceRawSlice(tuple[1]), goSliceRawSlice(tuple[2])...)
		for i := 0; i < len(files); i++ {
			files[i] = filepath.Join(relModuleDir, files[i])
		}
		allFiles = append(allFiles, files...)
	}
	return allFiles
}
351184
func Dir(t *testing.T, path string) <-chan Case {
	path = filepath.Join(BasePath, path)
	ch := make(chan Case)
	walker := func(path string, info os.FileInfo, err error) error {
		must(t, err)
		if info.Mode().IsRegular() && !strings.HasSuffix(path, Extension) {
			ch <- NewCase(t, path)
		}
		return nil
	}
	go func() {
		must(t, filepath.Walk(path, walker))
		close(ch)
	}()
	return ch
}
532018
func DefaultFuzzConnConfig() *FuzzConnConfig {
	return &FuzzConnConfig{
		Mode:         FuzzModeDrop,
		MaxDelay:     3 * time.Second,
		ProbDropRW:   0.2,
		ProbDropConn: 0.00,
		ProbSleep:    0.00,
	}
}
812631
func cfgOpt(cfg *conf.ConfigFile, section, option string) string {
	if !cfg.HasOption(section, option) {
		return ""
	}
	s, err := cfg.GetString(section, option)
	if err != nil {
		log.Exitf("Failed to get %s for %s: %v", option, section, err)
	}
	return s
}
91367
func doPackageCfg(cfgFile string) {
	js, err := ioutil.ReadFile(cfgFile)
	if err != nil {
		errorf("%v", err)
	}
	if err := json.Unmarshal(js, &vcfg); err != nil {
		errorf("parsing vet config %s: %v", cfgFile, err)
	}
	stdImporter = &vcfg
	inittypes()
	mustTypecheck = true
	doPackage(vcfg.GoFiles, nil)
}