summaryrefslogtreecommitdiff
path: root/libnetwork/etchosts/fuzz_test.go
blob: be7b498b1523917005bcf42b0a1688c66aaff65f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package etchosts

import (
	"os"
	"path/filepath"
	"testing"

	fuzz "github.com/AdaLogics/go-fuzz-headers"
)

func FuzzAdd(f *testing.F) {
	f.Fuzz(func(t *testing.T, data []byte) {
		ff := fuzz.NewConsumer(data)
		fileBytes, err := ff.GetBytes()
		if err != nil {
			return
		}
		noOfRecords, err := ff.GetInt()
		if err != nil {
			return
		}

		recs := make([]Record, 0)
		for i := 0; i < noOfRecords%40; i++ {
			r := Record{}
			err = ff.GenerateStruct(&r)
			if err != nil {
				return
			}
			recs = append(recs, r)
		}
		tmpDir := t.TempDir()
		testFile := filepath.Join(tmpDir, "testFile")
		err = os.WriteFile(testFile, fileBytes, 0o644)
		if err != nil {
			return
		}
		_ = Add(testFile, recs)
	})
}