github.com/tumi8/quic-go@v0.37.4-tum/fuzzing/internal/helper/helper_test.go (about)

     1  package helper
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	. "github.com/onsi/ginkgo/v2"
     9  	. "github.com/onsi/gomega"
    10  )
    11  
    12  var _ = Describe("exporting", func() {
    13  	var dir string
    14  
    15  	BeforeEach(func() {
    16  		var err error
    17  		dir, err = os.MkdirTemp("", "fuzzing-helper")
    18  		Expect(err).ToNot(HaveOccurred())
    19  		fmt.Fprintf(GinkgoWriter, "Created temporary directory %s", dir)
    20  	})
    21  
    22  	AfterEach(func() {
    23  		Expect(dir).ToNot(BeEmpty())
    24  		Expect(os.RemoveAll(dir)).To(Succeed())
    25  	})
    26  
    27  	It("writes a file", func() {
    28  		const data = "lorem ipsum"
    29  		// calculated by running sha1sum on the generated file
    30  		const expectedShaSum = "bfb7759a67daeb65410490b4d98bb9da7d1ea2ce"
    31  		Expect(WriteCorpusFile(dir, []byte("lorem ipsum"))).To(Succeed())
    32  		path := filepath.Join(dir, expectedShaSum)
    33  		Expect(path).To(BeARegularFile())
    34  		b, err := os.ReadFile(path)
    35  		Expect(err).ToNot(HaveOccurred())
    36  		Expect(string(b)).To(Equal(data))
    37  	})
    38  
    39  	It("writes a file and prepends data", func() {
    40  		const data = "lorem ipsum"
    41  		// calculated by running sha1sum on the generated file
    42  		const expectedShaSum = "523f5cab80fab0c7889dbf50dd310ab8c8879f9c"
    43  		const prefixLen = 7
    44  		Expect(WriteCorpusFileWithPrefix(dir, []byte("lorem ipsum"), prefixLen)).To(Succeed())
    45  		path := filepath.Join(dir, expectedShaSum)
    46  		Expect(path).To(BeARegularFile())
    47  		b, err := os.ReadFile(path)
    48  		Expect(err).ToNot(HaveOccurred())
    49  		Expect(b[:prefixLen]).To(Equal(make([]byte, prefixLen)))
    50  		Expect(string(b[prefixLen:])).To(Equal(data))
    51  	})
    52  
    53  	It("creates the directory, if it doesn't yet", func() {
    54  		subdir := filepath.Join(dir, "corpus")
    55  		Expect(subdir).ToNot(BeADirectory())
    56  		Expect(WriteCorpusFile(subdir, []byte("lorem ipsum"))).To(Succeed())
    57  		Expect(subdir).To(BeADirectory())
    58  	})
    59  
    60  	It("gets the nth bit of a byte", func() {
    61  		const val = 0b10010001
    62  		Expect(NthBit(val, 0)).To(BeTrue())
    63  		Expect(NthBit(val, 1)).To(BeFalse())
    64  		Expect(NthBit(val, 2)).To(BeFalse())
    65  		Expect(NthBit(val, 3)).To(BeFalse())
    66  		Expect(NthBit(val, 4)).To(BeTrue())
    67  		Expect(NthBit(val, 5)).To(BeFalse())
    68  		Expect(NthBit(val, 6)).To(BeFalse())
    69  		Expect(NthBit(val, 7)).To(BeTrue())
    70  	})
    71  })