github.com/tumi8/quic-go@v0.37.4-tum/noninternal/qerr/errorcodes_test.go (about)

     1  package qerr
     2  
     3  import (
     4  	"go/ast"
     5  	"go/parser"
     6  	"go/token"
     7  	"path"
     8  	"runtime"
     9  	"strconv"
    10  
    11  	. "github.com/onsi/ginkgo/v2"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("error codes", func() {
    16  	// If this test breaks, you should run `go generate ./...`
    17  	It("has a string representation for every error code", func() {
    18  		// We parse the error code file, extract all constants, and verify that
    19  		// each of them has a string version. Go FTW!
    20  		_, thisfile, _, ok := runtime.Caller(0)
    21  		if !ok {
    22  			panic("Failed to get current frame")
    23  		}
    24  		filename := path.Join(path.Dir(thisfile), "error_codes.go")
    25  		fileAst, err := parser.ParseFile(token.NewFileSet(), filename, nil, 0)
    26  		Expect(err).NotTo(HaveOccurred())
    27  		constSpecs := fileAst.Decls[2].(*ast.GenDecl).Specs
    28  		Expect(len(constSpecs)).To(BeNumerically(">", 4)) // at time of writing
    29  		for _, c := range constSpecs {
    30  			valString := c.(*ast.ValueSpec).Values[0].(*ast.BasicLit).Value
    31  			val, err := strconv.ParseInt(valString, 0, 64)
    32  			Expect(err).NotTo(HaveOccurred())
    33  			Expect(TransportErrorCode(val).String()).ToNot(Equal("unknown error code"))
    34  		}
    35  	})
    36  
    37  	It("has a string representation for unknown error codes", func() {
    38  		Expect(TransportErrorCode(0x1337).String()).To(Equal("unknown error code: 0x1337"))
    39  	})
    40  
    41  	It("says if an error is a crypto error", func() {
    42  		for i := 0; i < 0x100; i++ {
    43  			Expect(TransportErrorCode(i).IsCryptoError()).To(BeFalse())
    44  		}
    45  		for i := 0x100; i < 0x200; i++ {
    46  			Expect(TransportErrorCode(i).IsCryptoError()).To(BeTrue())
    47  		}
    48  		for i := 0x200; i < 0x300; i++ {
    49  			Expect(TransportErrorCode(i).IsCryptoError()).To(BeFalse())
    50  		}
    51  	})
    52  })